Class Matlib

java.lang.Object
org.mbari.jcommons.math.Matlib

public class Matlib extends Object
Static methods for doing useful math
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    cumsum(double[] n)
    Cumulatively sum a vector Example: cumsum([1 1 1 1 2]) = [1 2 3 4 6]
    static double[]
    diff(double[] x)
     
    static int
    find(double[] array, double valueToFind)
     
    static double
    fix(double x)
    Rounds the x to the nearest integer towards zero.
    static double[]
    interpolate(double[] x, double[] y, double[] xi)
     
    static double[]
    interpolate(long[] x, double[] y, long[] xi)
     
    static BigDecimal[]
     
    static double[]
    linspace(double d1, double d2, int n)
    generates n linearly-spaced points between d1 and d2.
    static double[]
    logspace(double d1, double d2, int n)
    generates n logarithmically-spaced points between d1 and d2.
    static double[]
    logspace(double d1, double d2, int n, double base)
    generates n logarithmically-spaced points between d1 and d2 using the provided base.
    static int
    near(double[] values, double key)
    Find the index of the value nearest to the key.
    static int
    near(double[] values, double key, boolean inclusive)
    Find the index of the array nearest to the value.
    static Integer
    nearInclusive(double[] array, double value)
    Deprecated.
    Use near instead
    static double[]
    subset(double[] values, int[] order)
    Useful method for ordering a 1-D array based on an array of indices
    static float[]
    subset(float[] values, int[] order)
     
    static long[]
    subset(long[] values, int[] order)
     
    static double
    sum(double[] values)
    Sum up all the values in an array
    static double
    trapz(double[] x, double[] y)
     
    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 Details

    • Matlib

      public Matlib()
  • Method Details

    • cumsum

      public static double[] cumsum(double[] n)
      Cumulatively sum a vector Example: cumsum([1 1 1 1 2]) = [1 2 3 4 6]
      Parameters:
      n -
      Returns:
    • diff

      public static double[] diff(double[] x)
      Parameters:
      x -
      Returns:
    • find

      public static int find(double[] array, double valueToFind)
      Parameters:
      array -
      valueToFind -
      Returns:
    • fix

      public static double fix(double x)
      Rounds the x to the nearest integer towards zero.
      Parameters:
      x -
      Returns:
    • interpolate

      public static double[] interpolate(double[] x, double[] y, double[] xi) throws IllegalArgumentException
      Parameters:
      x -
      y -
      xi -
      Returns:
      Throws:
      IllegalArgumentException
    • interpolate

      public static BigDecimal[] interpolate(BigDecimal[] x, BigDecimal[] y, BigDecimal[] xi)
      Parameters:
      x -
      y -
      xi -
      Returns:
    • interpolate

      public static double[] interpolate(long[] x, double[] y, long[] xi) throws IllegalArgumentException
      Parameters:
      x -
      y -
      xi -
      Returns:
      Throws:
      IllegalArgumentException
    • linspace

      public static double[] linspace(double d1, double d2, int n)
      generates n linearly-spaced points between d1 and d2.
      Parameters:
      d1 - The min value
      d2 - The max value
      n - The number of points to generated
      Returns:
      an array of lineraly space points.
    • logspace

      public static double[] logspace(double d1, double d2, int n)
      generates n logarithmically-spaced points between d1 and d2.
      Parameters:
      d1 - The min value
      d2 - The max value
      n - The number of points to generated
      Returns:
      an array of lineraly space points.
    • logspace

      public static double[] logspace(double d1, double d2, int n, double base)
      generates n logarithmically-spaced points between d1 and d2 using the provided base.
      Parameters:
      d1 - The min value
      d2 - The max value
      n - The number of points to generated
      base - the logarithmic base to use
      Returns:
      an array of lineraly space points.
    • near

      public static 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
    • near

      public static int near(double[] values, double key, boolean inclusive)
      Find the index of the array nearest to the value. 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. If the value falls outside the bounds of the array, null is returned
      Parameters:
      values - Values to search through for the nearest point
      key - THe value to search for the nearest neighbor in the array
      inclusive - If true the key must be within the values array
      Returns:
      The index of the array value nearest the value. -1 will be returned if the key is outside the array values
    • nearInclusive

      public static Integer nearInclusive(double[] array, double value)
      Deprecated.
      Use near instead
      Find the index of the array nearest to the value. 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. If the value falls outside the bounds of the array, null is returned
      Parameters:
      array - Values to search through for the nearest point
      value - THe value to search for the nearest neighbor in the array
      Returns:
      The index of the array value nearest the value. null if the value is larger or smaller than any values in the array.
    • subset

      public static double[] subset(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.
      Returns:
    • subset

      public static float[] subset(float[] values, int[] order)
      Parameters:
      values -
      order -
      Returns:
    • subset

      public static long[] subset(long[] values, int[] order)
      Parameters:
      values -
      order -
      Returns:
    • sum

      public static double sum(double[] values)
      Sum up all the values in an array
      Parameters:
      values - an array of values
      Returns:
      The sum of all values in the Array
    • trapz

      public static double trapz(double[] x, double[] y)
      Parameters:
      x -
      y -
      Returns:
    • uniqueSort

      public static 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 = Matlib.uniqueSort(x);
      double[] xSorted = Matlib.reorder(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 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 = Matlib.uniqueSort(x);
      double[] xSorted = Matlib.reorder(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.