org.mbari.util
Class MathUtil

java.lang.Object
  extended byorg.mbari.util.MathUtil

public class MathUtil
extends java.lang.Object

Static methods for doing useful math


Version:
: $Revision: 1.5 $

The Monterey Bay Aquarium Research Institute (MBARI) provides this documentation and code "as is", with no warranty, express or implied, of its quality or consistency. It is provided without support and without obligation on the part of MBARI to assist in its use, correction, modification, or enhancement. This information should not be published or distributed to third parties without specific written permission from MBARI.


Copyright 2002 MBARI.
MBARI Proprietary Information. All rights reserved.



Author:
: $Author: brian $

Constructor Summary
MathUtil()
           
 
Method Summary
static int binarySearch(double[] a, double key)
          Searches the specified array of doubles for the specified value using the binary search algorithm.
static double[] cumSum(double[] n)
          Cumulatively sum a vector Example: cumSum([1 1 1 1 2]) = [1 2 3 4 6]
static int find(double[] array, double valueToFind)
           
static double fix(double x)
           
static double[] interpLinear(double[] x, double[] y, double[] xi)
           
static double[] interpLinear(long[] x, double[] y, long[] xi)
           
static boolean isEven(double x)
           
static double mod(double x, double y)
           
static int near(double[] values, double key)
          Find the index of the value nearest to the key.
static double[] orderVector(double[] values, int[] order)
          Useful method for ordering a 1-D array based on an array of indices
static float[] orderVector(float[] values, int[] order)
           
static long[] orderVector(long[] values, int[] order)
           
static double rem(double x, double y)
           
static int sign(double x)
           
static int[] uniqueSort(double[] x)
          Returns an array of indices indicating the order the data should be sorted in.
static int[] uniqueSort(long[] x)
          Returns an array of indices indicating the order the data should be sorted in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathUtil

public MathUtil()
Method Detail

interpLinear

public static final double[] interpLinear(double[] x,
                                          double[] y,
                                          double[] xi)
                                   throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

interpLinear

public static final double[] interpLinear(long[] x,
                                          double[] y,
                                          long[] xi)
                                   throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

find

public static final int find(double[] array,
                             double valueToFind)

mod

public static double mod(double x,
                         double y)

rem

public static double rem(double x,
                         double y)

fix

public static double fix(double x)

sign

public static int sign(double x)

cumSum

public static double[] cumSum(double[] n)
Cumulatively sum a vector Example: cumSum([1 1 1 1 2]) = [1 2 3 4 6]


isEven

public static boolean isEven(double x)

uniqueSort

public static final int[] uniqueSort(double[] x)
Returns an array of indices indicating the order the data should be sorted in. Duplicate values are discarded with the first one being kept. This method is useful when a number of data arrays have to be sorted based on the values in some coordinate array, such as time. To convert a array of values to a sorted monooic array try:
double[] x; // some 1-D array of data
int[] i = MathUtil.uniqueSort(x);
double[] xSorted = MathUtil.orderVector(x, i);

Parameters:
x - An array of data that is to be sorted.
Returns:
order An array of indexes such that y = Array.sort(x) and y = x(order) are the same.

uniqueSort

public static final int[] uniqueSort(long[] x)
Returns an array of indices indicating the order the data should be sorted in. Duplicate values are discarded with the first one being kept. This method is useful when a number of data arrays have to be sorted based on the values in some coordinate array, such as time. To convert a array of values to a sorted monooic array try:
double[] x; // some 1-D array of data
int[] i = MathUtil.uniqueSort(x);
double[] xSorted = MathUtil.orderVector(x, i);

Parameters:
x - An array of data that is to be sorted.
Returns:
order An array of indexes such that y = Array.sort(x) and y = x(order) are the same.

orderVector

public static final double[] orderVector(double[] values,
                                         int[] order)
Useful method for ordering a 1-D array based on an array of indices

Parameters:
values - A 1-D array of data to be sorted based on an array of indices
order - A 1-D array of indices specifying the ordering of the data.
See Also:
uniqueSort

orderVector

public static final float[] orderVector(float[] values,
                                        int[] order)

orderVector

public static final long[] orderVector(long[] values,
                                       int[] order)

near

public static final int near(double[] values,
                             double key)
Find the index of the value nearest to the key. The values array can contain only unique values. If it doesn't the first occurence of a value in the values array is the one used, subsequent duplicate are ignored.

Parameters:
values - Values to search through for the nearest point.
key - The key to search for the nearest neighbor in values.
Returns:
the index of the value nearest to the key.
Since:
20011207

binarySearch

public static int binarySearch(double[] a,
                               double key)
Searches the specified array of doubles for the specified value using the binary search algorithm. The array must be sorted (as by the sort method, above) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found. The array can be sorted from low values to high or from high values to low.

Parameters:
a - the array to be searched.
key - the value to be searched for.
Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
See Also:
#sort(double[])