papaya
Class Mat

java.lang.Object
  extended by papaya.Mat

public final class Mat
extends Object

Static class for performing some basic matrix operations.

Guidelines
Methods that modify an array by a value always take the array as the first parameter, and the value as the second. For example,

I try to follow that guiding principle with all methods that take in an input array (e.g. swap(float[] array, int index1, int index2), populate(float[] array, int[] indices)).

Remarks
Almost all the methods here take in float arrays or matrices, although I've also added in a few methods that operate on integers. In addition, to avoid accidental array manipulation, all methods return a new array or matrix instead of modifying the original one. E.g.
C[i][j] = A[i][j]*B[i][j] return C
as opposed to
A[i][j] = A[i][j]*B[i][j], return A
in dotMultiply(float[][],float[][]). (If you find it performing to the contrary though, please let me know!)

Some non-trivial matrix-math related methods, including those for getting the condition number, inverse, rank, and norms of a 2D input matrix are also included. Making everything float-based instead of double-based naturally results in some precision loss but the internal computations are done using doubles to minimize this.

See Also:
Cast, Find, NaNs, Rank, Sorting

Method Summary
static float[] abs(float[] array)
          Return absolute values of an array
static int[] abs(int[] array)
          Return absolute values of an array
static float ceilToNearest(float currentVal, int interval)
          Function for ceiling a number to the closest interval.
static float[] column(float[][] inputMat, int columnIndex)
          Function to get one column of a 2D matrix
static float[][] column(float[][] inputMat, int indexStart, int indexEnd)
          Function to get columns of a 2-D matrix.
static int[] column(int[][] inputMat, int columnIndex)
           
static float compare(float a, float b)
          Returns -1 if a is less than b, or 1 if a is greater than b, or 0.
static int compare(int a, int b)
          Returns -1 if a is less than b, or 1 if a is greater than b, or 0.
static float[] concat(float[] a, float[] b)
          Concatenates two arrays a and b into a new array c such that c = {a,b}.
static int[] concat(int[] a, int[] b)
           
static float[] constant(float constValue, int size)
          Returns an array with of length size with each element equal to the specified constValue.
static int[] constant(int constValue, int size)
          Returns an array with of length size with each element equal to the specified constValue.
static float[][] copy(float[][] data)
          Make a deep copy of a matrix
static float[] copyThenSort(float[] data)
          Sorts and returns a copy of the input array in ascending order
static float[] cross(float[] a, float[] b)
          vector cross product: c = a (cross) b
static float det(float[][] data)
          returns the determinant of the square input matrix.
static float[][] divide(float[][] A, float value)
          Divides each element of the input matrix A by the input value
static float[] divide(float[] data, float value)
          Divides all elements of input array by value.
static float[] divide(float[] x, float[] y)
          Element by element division such that z[i] = x[i]/y[i].
static float[][] dotDivide(float[][] A, float[][] B)
          Element by element matrix division such that C_ij = A_ij/B_ij.
static float[][] dotMultiply(float[][] A, float[][] B)
          Element by element matrix multiplication such that C_ij = A_ij*B_ij.
static float dotProduct(float[] a, float[] b)
          dot product of two arrays such that z = sum(a_i*b_i)
static float floorToNearest(float currentVal, int interval)
          Function for flooring a number to the closest interval.
static float[][] identity(int dimension)
          returns the identity matrix of the specified dimension
static float[][] inverse(float[][] A)
          Matrix inverse if A is square, pseudoinverse otherwise
static float[] inverse(float[] data, float replaceZeroWith)
          Returns the array z[i] = 1/x[i]; If x[i] is zero, it sets z[i] to the specified replaceZeroWith value instead.
static boolean isConstant(float[] a)
           
static float[] linspace(float start, float end, float stepSize)
           
static int[] linspace(int start, int end)
          Returns the array of (end-start+1) points going from start (inclusive) to end (inclusive): y[0] = start; y[1] = start+1; ...
static int[] linspace(int start, int end, int stepSize)
           
static float[] log(float[] data)
          Returns an array with each element equal to the natural logarithm (the base-e logarithm) the corresponding input array.
static float[] log10(float[] data)
          Returns an array with each element equal to the log 10 value (base 10 logarithm) of the corresponding input array.
static float[] logToBase(float[] data, float base)
          Returns an array with each element equal to the log A value of the corresponding input array.
static float mag(float[] data)
          Returns the magnitude of the input array = sqrt(x1^2 + x2^2 + ...
static float[] map(float[] data, float from, float to)
          Similar to map([] value, valueArrayMinimum, valueArrayMaximum, from, to)
static float[] map(float[] data, float low1, float high1, float low2, float high2)
          Function for mapping an input array of floats for plotting Extends processing "map" function to accomodate arrays.
static float map(float data, float low1, float high1, float low2, float high2)
          Convenience function to map a variable from one coordinate space to another.
static float[][] multiply(float[][] A, float a)
          Multiplies the matrix A with the scalar a returning a*A = B where B_ij = a*A_ij.
static float[] multiply(float[][] A, float[] x)
          Multiplies the matrix A with the vector x returning A*x = y where y_j= Sum(k=j to n) A_ij*x_j.
static float[][] multiply(float[][] A, float[][] B)
          Multiplies two matrices A and B returning C = A*B where C_ij = Sum(k=1 to n) A_ik*B_kj.
static float[] multiply(float[] data, float number)
          Multiplies each element of an array by a number, and returns the multiplied array.
static float[] multiply(float[] data1, float[] data2)
          Multiplies two arrays and returns the multiplied array.
static float norm1(float[][] data)
          Returns the norm1 of the input matrix, equal to the maximum absolute column sum of the matrix.
static float norm2(float[] data)
          Returns the norm2 or magnitude of an array.
static float norm2(float[][] data)
          Returns the norm2 (or maximum singular value) of the input matrix.
static float[] normalizeToMinMax(float[] data)
          Returns an array with elements consisting of the data normalized to the specified min/max; new data goes from 0 to 1.
static float[] normalizeToSum(float[] data)
          Returns an array with elements given by z[i] = data[i] / sum(data);
static float normF(float[][] data)
          /** Returns the Frobenius norm of the input matrix; sqrt of sum of squares of all elements.
static float normInf(float[][] data)
          Returns the Infinity norm of the input matrix, equal to the maximum row sum.
static double[] populate(double[] fullDataset, int[] indices)
          Returns an array with each element corresponding to fullDataset[indices[i]].
static float[] populate(float[] fullDataset, int[] indices)
          Returns an array with each element corresponding to fullDataset[indices[i]].
static int[] populate(int[] fullDataset, int[] indices)
          Returns an array with each element corresponding to fullDataset[indices[i]].
static String[] populate(String[] fullDataset, int[] indices)
          Returns an array with each element corresponding to fullDataset[indices[i]].
static void print(double[][] data, int d)
          Print the matrix to the screen with each row of the matrix taking up one line.
static void print(double[][] data, String[] columnLabels, String[] rowLabels, int d)
          Print the matrix to the screen with the columns and rows labeled according to the input strings.
static void print(double[] data, int d)
          Print the array to the screen in a single line.
static void print(float[][] data, int d)
          Print the matrix to the screen with each row of the matrix taking up one line.
static void print(float[][] data, String[] columnLabels, String[] rowLabels, int d)
          Print the matrix to the screen with the columns and rows labeled according to the input strings.
static void print(float[] data, int d)
          Print the array to the screen in a single line.
static void print(int[][] data, int d)
          Print the matrix to the screen with each row of the matrix taking up one line.
static void print(int[][] data, String[] columnLabels, String[] rowLabels, int d)
          Print the matrix to the screen with the columns and rows labeled according to the input strings.
static void print(int[] data, int d)
          Print the array to the screen in a single line.
static int rank(float[][] data)
          Returns the effective numerical rank (obtained from SVD) of the input matrix.
static float[][] replace(float[][] A, float oldValue, float newValue)
          Returns a new matrix B equal to A, except that all elements in A which are equal to oldValue with the newValue.
static float[] replace(float[] data, float oldValue, float newValue)
          Returns a new array data2 which is similar to data, except that all elements in data which are equal to oldValue are replaced with the newValue.
static float[][] reshape(float[][] arr, int numRows, int numColumns)
          Reshapes a matrix into the new specified dimensions.
static double[] reverse(double[] data)
          reverses the order of the elements in an array.
static float[] reverse(float[] data)
          reverses the order of the elements in an array.
static int[] reverse(int[] data)
          reverses the order of the elements in an array.
static float roundToDecimalPlace(float number, int decimal)
          Function for rounding to a given decimal place.
static float[][] skewSymmetric(float[][] data)
          Returns the skew symmetric part of the input matrix ((A-A')/2).
static ArrayList[] split(float[] sortedList, float[] splitters)
          Splits (partitions) an array into sublists such that each sublist contains the elements with a given range.
static float[][] subMatrix(float[][] inputMat, int[] rowIndices, int[] columnIndices)
          Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).
static float[][] subMatrix(float[][] inputMat, int[] rowIndices, int columnStart, int columnEnd)
          Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).
static float[][] subMatrix(float[][] inputMat, int rowStart, int rowEnd, int[] columnIndices)
          Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).
static float[][] subMatrix(float[][] inputMat, int rowStart, int rowEnd, int columnStart, int columnEnd)
          Get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).
static int[][] subMatrix(int[][] inputMat, int[] rowIndices, int[] columnIndices)
           
static float[][] subtract(float[][] x, float[][] y)
          Returns the matrix z = x-y where each element of z[i][j] = x[i][j] - y[i][j]
static float[] subtract(float[] x, float[] y)
          Returns the array z = x-y where each element of z[i] = x[i] - y[i]
static float sum(float[] data)
          Returns the sum of a data sequence.
static float[][] sum(float[][] x, float[][] y)
          Returns the matrix z = x+y where each element of z[i][j] = x[i][j] + y[i][j]
static float[] sum(float[] data, float number)
          Sum of an array with a number.
static float[] sum(float[] data1, float[] data2)
          Returns the sum of two data sequences.
static int sum(int[] data)
          Returns the sum of a data sequence.
static int[] sum(int[] data, int number)
          Sum of an array with a number.
static int[] sum(int[] data1, int[] data2)
          Returns the sum of two data sequences.
static void swap(float[] data, int oldIndex, int newIndex)
          Swap function that swaps the values in an array
static void swap(int[] data, int oldIndex, int newIndex)
          Swap function that swaps the values in an array (overload function)
static float[][] symmetric(float[][] data)
          Returns the symmetric part of the input matrix ((A+A')/2).
static float trace(float[][] data)
          Returns the matrix trace or sum of the diagonal elements.
static float[][] transpose(float[][] data)
          Returns the transpose of the input matrix.
static int[][] transpose(int[][] data)
           
static float[] within(float[] numbers, float min, float max)
          returns an array containing all numbers within the min and max (inclusive).
static boolean within(float number, float min, float max)
          Checks to see if a number is in the range specified by [min,max].
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

abs

public static float[] abs(float[] array)
Return absolute values of an array


abs

public static int[] abs(int[] array)
Return absolute values of an array


column

public static float[][] column(float[][] inputMat,
                               int indexStart,
                               int indexEnd)
Function to get columns of a 2-D matrix.

Parameters:
inputMat - inputMatrix.
indexStart - index of the start column (inclusive)
indexEnd - index of the end column (inclusive)
Returns:
outputMat consisting of the specified columns [indexStart, indexStart+1, ...., indexEnd]

column

public static float[] column(float[][] inputMat,
                             int columnIndex)
Function to get one column of a 2D matrix

Parameters:
inputMat - input matrix.
columnIndex - index of the column to get
Returns:
outputColumn the specified column

column

public static int[] column(int[][] inputMat,
                           int columnIndex)

compare

public static int compare(int a,
                          int b)
Returns -1 if a is less than b, or 1 if a is greater than b, or 0. This is the comparator function for natural order.


compare

public static float compare(float a,
                            float b)
Returns -1 if a is less than b, or 1 if a is greater than b, or 0. This is the comparator function for natural order.


concat

public static float[] concat(float[] a,
                             float[] b)
Concatenates two arrays a and b into a new array c such that c = {a,b}.


concat

public static int[] concat(int[] a,
                           int[] b)

constant

public static float[] constant(float constValue,
                               int size)
Returns an array with of length size with each element equal to the specified constValue.

See Also:
Arrays.fill

constant

public static int[] constant(int constValue,
                             int size)
Returns an array with of length size with each element equal to the specified constValue.

See Also:
Arrays.fill

copy

public static float[][] copy(float[][] data)
Make a deep copy of a matrix


copyThenSort

public static float[] copyThenSort(float[] data)
Sorts and returns a copy of the input array in ascending order

Returns:
a sorted copy of the input array.

cross

public static float[] cross(float[] a,
                            float[] b)
vector cross product: c = a (cross) b


det

public static float det(float[][] data)
returns the determinant of the square input matrix.

Throws:
IllegalArgumentException - if the matrix is not square.

divide

public static float[] divide(float[] data,
                             float value)
Divides all elements of input array by value. That is, z[i] = x[i] / value.

Throws:
IllegalArgumentException - if value is zero.

divide

public static float[] divide(float[] x,
                             float[] y)
Element by element division such that z[i] = x[i]/y[i]. No checks are done to see if any y[i] == 0 so make sure you know what you're doing.


divide

public static float[][] divide(float[][] A,
                               float value)
Divides each element of the input matrix A by the input value


dotDivide

public static float[][] dotDivide(float[][] A,
                                  float[][] B)
Element by element matrix division such that C_ij = A_ij/B_ij. No checks are done to see if the elements of B_ij == 0 so make sure you know what you're doing.


dotMultiply

public static float[][] dotMultiply(float[][] A,
                                    float[][] B)
Element by element matrix multiplication such that C_ij = A_ij*B_ij.


dotProduct

public static float dotProduct(float[] a,
                               float[] b)
dot product of two arrays such that z = sum(a_i*b_i)


identity

public static float[][] identity(int dimension)
returns the identity matrix of the specified dimension


inverse

public static float[] inverse(float[] data,
                              float replaceZeroWith)
Returns the array z[i] = 1/x[i]; If x[i] is zero, it sets z[i] to the specified replaceZeroWith value instead.


inverse

public static float[][] inverse(float[][] A)
Matrix inverse if A is square, pseudoinverse otherwise

Returns:
inverse(A) if A is square, pseudoinverse otherwise.

isConstant

public static boolean isConstant(float[] a)

log

public static float[] log(float[] data)
Returns an array with each element equal to the natural logarithm (the base-e logarithm) the corresponding input array. This function expects the values greater than 0.0; No internal checks are done.


log10

public static float[] log10(float[] data)
Returns an array with each element equal to the log 10 value (base 10 logarithm) of the corresponding input array. This function expects the values greater than 0.0; No internal checks are done.


logToBase

public static float[] logToBase(float[] data,
                                float base)
Returns an array with each element equal to the log A value of the corresponding input array. This is computed using the property log_A(x) = log(x) / log(A) where log is the natural logarithm (base e), and A is the new log base. This function expects the values greater than 0.0; No internal checks are done.

Parameters:
base - the log base. E.g. to get log2, we would use logToBase(data,2).

linspace

public static int[] linspace(int start,
                             int end)
Returns the array of (end-start+1) points going from start (inclusive) to end (inclusive): y[0] = start; y[1] = start+1; ... , y[n] = end;.
For example, int[] ii = linspace(2,5); produces the array ii = {2,3,4,5}; with length 4.
 It is similar to this:
 
 for(int i=0; i<=end-start+1; i++){
 
    y[i] = i + start;
}

Parameters:
start - the start index (inclusive)
end - the end index (inclusive);

linspace

public static int[] linspace(int start,
                             int end,
                             int stepSize)
Parameters:
start - the start index (inclusive)
end - the end index (inclusive);
stepSize - the stepsize to take
Returns:
array of length num = floor((end-start)/(float)stepSize) (meaning that it could end BEFORE 'end' if you don't size your stepsize properly.

linspace

public static float[] linspace(float start,
                               float end,
                               float stepSize)
Parameters:
start - the start index (inclusive)
end - the end index (inclusive);
stepSize - the stepsize to take
Returns:
array of length num = ((end-start)/stepSize)

mag

public static float mag(float[] data)
Returns the magnitude of the input array = sqrt(x1^2 + x2^2 + ... + xn^2)


map

public static float map(float data,
                        float low1,
                        float high1,
                        float low2,
                        float high2)
Convenience function to map a variable from one coordinate space to another. I.e. map(value, low1, high1, low2, high2) = low2 + (value - low1) * ( (high2-low2) / (high1-low1) )

Similar to Processing's "map" function.

Returns:
mapped data array

map

public static float[] map(float[] data,
                          float from,
                          float to)
Similar to map([] value, valueArrayMinimum, valueArrayMaximum, from, to)

Returns:
mapped data array

map

public static float[] map(float[] data,
                          float low1,
                          float high1,
                          float low2,
                          float high2)
Function for mapping an input array of floats for plotting Extends processing "map" function to accomodate arrays. E.g. map(value, low1, high1, low2, high2) is now map([] value, low1, high1, low2, high2)

Returns:
mapped data array

multiply

public static float[][] multiply(float[][] A,
                                 float[][] B)
Multiplies two matrices A and B returning C = A*B where C_ij = Sum(k=1 to n) A_ik*B_kj. Precondition: Number of columns in A = number of rows in B.


multiply

public static float[] multiply(float[][] A,
                               float[] x)
Multiplies the matrix A with the vector x returning A*x = y where y_j= Sum(k=j to n) A_ij*x_j. Precondition: Number of columns in A = number of elements in x.


multiply

public static float[][] multiply(float[][] A,
                                 float a)
Multiplies the matrix A with the scalar a returning a*A = B where B_ij = a*A_ij.


multiply

public static float[] multiply(float[] data1,
                               float[] data2)
Multiplies two arrays and returns the multiplied array. That is, z[i] = x[i] * y[i].


multiply

public static float[] multiply(float[] data,
                               float number)
Multiplies each element of an array by a number, and returns the multiplied array. That is, z[i] = k * x[i] .


norm1

public static float norm1(float[][] data)
Returns the norm1 of the input matrix, equal to the maximum absolute column sum of the matrix.


norm2

public static float norm2(float[][] data)
Returns the norm2 (or maximum singular value) of the input matrix.


norm2

public static float norm2(float[] data)
Returns the norm2 or magnitude of an array. Similar to mag(float[]).


normF

public static float normF(float[][] data)
/** Returns the Frobenius norm of the input matrix; sqrt of sum of squares of all elements.


normInf

public static float normInf(float[][] data)
Returns the Infinity norm of the input matrix, equal to the maximum row sum.


normalizeToMinMax

public static float[] normalizeToMinMax(float[] data)
Returns an array with elements consisting of the data normalized to the specified min/max; new data goes from 0 to 1.

Returns:
normalized data array

normalizeToSum

public static float[] normalizeToSum(float[] data)
Returns an array with elements given by z[i] = data[i] / sum(data);

Returns:
normalized data array

populate

public static double[] populate(double[] fullDataset,
                                int[] indices)
Returns an array with each element corresponding to fullDataset[indices[i]]. That is, if the indices.length = n, then
 y[0] = fullDataset[ indices[0] ];
 y[1] = fullDataset[ indices[1] ];
 ...
 ...
 y[n-1] = fullDataset[ indices[n-1] ];
 

Parameters:
indices - the indices in the fullDataset you want to extract and store.
fullDataset - the full dataset.

populate

public static float[] populate(float[] fullDataset,
                               int[] indices)
Returns an array with each element corresponding to fullDataset[indices[i]]. That is, if the indices.length = n, then
 y[0] = fullDataset[ indices[0] ];
 y[1] = fullDataset[ indices[1] ];
 ...
 ...
 y[n-1] = fullDataset[ indices[n-1] ];
 

Parameters:
indices - the indices in the fullDataset you want to extract and store.
fullDataset - the full dataset.

populate

public static int[] populate(int[] fullDataset,
                             int[] indices)
Returns an array with each element corresponding to fullDataset[indices[i]]. That is, if the indices.length = n, then
 y[0] = fullDataset[ indices[0] ];
 y[1] = fullDataset[ indices[1] ];
 ...
 ...
 y[n-1] = fullDataset[ indices[n-1] ];
 

Parameters:
indices - the indices in the fullDataset you want to extract and store.
fullDataset - the full dataset.

populate

public static String[] populate(String[] fullDataset,
                                int[] indices)
Returns an array with each element corresponding to fullDataset[indices[i]]. That is, if the indices.length = n, then
 y[0] = fullDataset[ indices[0] ];
 y[1] = fullDataset[ indices[1] ];
 ...
 ...
 y[n-1] = fullDataset[ indices[n-1] ];
 

Parameters:
indices - the indices in the fullDataset you want to extract and store.
fullDataset - the full dataset.

print

public static void print(double[] data,
                         int d)
Print the array to the screen in a single line. Similar to print, except that the entire array is printed on a single line on the screen and you can format the number of decimal digits directly instead of using Processing's nf or one of the other variants of it.

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(double[][] data,
                         int d)
Print the matrix to the screen with each row of the matrix taking up one line. Similar to println, except that each row of the matrix is printed on a single line on the screen and you can format the number of decimal digits directly instead of using Processing's nf or one of the other variants of it.

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(double[][] data,
                         String[] columnLabels,
                         String[] rowLabels,
                         int d)
Print the matrix to the screen with the columns and rows labeled according to the input strings. Output resembles what you'd see in a .txt file (assuming the matrix isn't too large).

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(float[] data,
                         int d)
Print the array to the screen in a single line. Similar to print, except that the entire array is printed on a single line on the screen and you can format the number of decimal digits directly instead of using Processing's nf or one of the other variants of it.

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(float[][] data,
                         int d)
Print the matrix to the screen with each row of the matrix taking up one line. Similar to println, except that each row of the matrix is printed on a single line on the screen and you can format the number of decimal digits directly instead of using Processing's nf or one of the other variants of it.

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(float[][] data,
                         String[] columnLabels,
                         String[] rowLabels,
                         int d)
Print the matrix to the screen with the columns and rows labeled according to the input strings. Output resembles what you'd see in a .txt file (assuming the matrix isn't too large).

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(int[] data,
                         int d)
Print the array to the screen in a single line. Similar to print, except that the entire array is printed on a single line on the screen and you can format the number of decimal digits directly instead of using Processing's nf or one of the other variants of it.

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(int[][] data,
                         int d)
Print the matrix to the screen with each row of the matrix taking up one line. Similar to println, except that each row of the matrix is printed on a single line on the screen and you can format the number of decimal digits directly instead of using Processing's nf or one of the other variants of it.

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

print

public static void print(int[][] data,
                         String[] columnLabels,
                         String[] rowLabels,
                         int d)
Print the matrix to the screen with the columns and rows labeled according to the input strings. Output resembles what you'd see in a .txt file (assuming the matrix isn't too large).

Parameters:
data - the matrix to be printed.
d - Number of digits after the decimal to display..

rank

public static int rank(float[][] data)
Returns the effective numerical rank (obtained from SVD) of the input matrix. See Rank for ranking the elements of an input 1D array.


replace

public static float[][] replace(float[][] A,
                                float oldValue,
                                float newValue)
Returns a new matrix B equal to A, except that all elements in A which are equal to oldValue with the newValue. Useful, for example, in replacing zeros with some other number (for dotDivide(float[][], float[][])), or for wall-street related book-keeping...


replace

public static float[] replace(float[] data,
                              float oldValue,
                              float newValue)
Returns a new array data2 which is similar to data, except that all elements in data which are equal to oldValue are replaced with the newValue. Useful, for example, in replacing zeros with some other number (for divide(float[], float[])), or for wall-street related book-keeping...


reshape

public static float[][] reshape(float[][] arr,
                                int numRows,
                                int numColumns)
Reshapes a matrix into the new specified dimensions.


reverse

public static double[] reverse(double[] data)
reverses the order of the elements in an array.


reverse

public static float[] reverse(float[] data)
reverses the order of the elements in an array.


reverse

public static int[] reverse(int[] data)
reverses the order of the elements in an array.


roundToDecimalPlace

public static float roundToDecimalPlace(float number,
                                        int decimal)
Function for rounding to a given decimal place. Convenient for displaying text when Processing's nf,nfc,etc is insufficient. Only works if the input number has got more decimal places than the number to round to.

Examples:
roundToDecimalPlace(1.234567, 2) --> 1.23;
roundToDecimalPlace(100, 2) --> 100;

Parameters:
number - number to round.
decimal - decimal place to round to.
Returns:
number rounded to the specified decimal place

floorToNearest

public static float floorToNearest(float currentVal,
                                   int interval)
Function for flooring a number to the closest interval. Useful for setting up the tick labels for plotting.

Examples:
floorToNearest(173.2,5) --> 170;
floorToNearest(28,10) --> 20;

Parameters:
currentVal - number to floor.
interval - to floor to. E.g. in fives, twos, tens, etc.
Returns:
number.

ceilToNearest

public static float ceilToNearest(float currentVal,
                                  int interval)
Function for ceiling a number to the closest interval. Useful for setting up the tick labels for plotting.

Examples:
ceilToNearest(173.2,5) --> 175;
ceilToNearest(28,10) --> 30;

Parameters:
currentVal - number to ceil.
interval - to ceil to. E.g. in fives, twos, tens, etc.
Returns:
number.

split

public static ArrayList[] split(float[] sortedList,
                                float[] splitters)
Splits (partitions) an array into sublists such that each sublist contains the elements with a given range. splitters=(a,b,c,...,y,z) defines the ranges [-inf,a), [a,b), [b,c), ..., [y,z), [z,inf].

Examples:

Parameters:
sortedList - the list to be partitioned (must be sorted ascending).
splitters - the points at which the list shall be partitioned (must be sorted ascending).
Returns:
the sublists (an array with length == splitters.length + 1. Each sublist is returned sorted ascending.

subMatrix

public static float[][] subMatrix(float[][] inputMat,
                                  int rowStart,
                                  int rowEnd,
                                  int columnStart,
                                  int columnEnd)
Get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).

Parameters:
inputMat - input matrix
rowStart - row to start at (inclusive)
rowEnd - row to end at (inclusive)
columnStart - column to start at (inclusive)
columnEnd - column to end at (inclusive)
Returns:
submatrix containing (rowEnd-rowStart+1) rows and (columnEnd-columnStart+1) columns; copy of A(rowStart:rowEnd, columnStart:columnEnd).
Throws:
IllegalArgumentException - if the rowStart, rowEnd, columnStart, or columnEnd parameters are not within the inputMatrix size bounds.

subMatrix

public static float[][] subMatrix(float[][] inputMat,
                                  int[] rowIndices,
                                  int columnStart,
                                  int columnEnd)
Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).

Parameters:
inputMat - input matrix
rowIndices - the row indices
columnStart - column to start at (inclusive)
columnEnd - column to end at (inclusive)
Returns:
the submatrix containing a copy of the input matrix's specified rows from columnStart to columnEnd; copy of A(rowIndices(:), columnStart:columnEnd).
Throws:
IllegalArgumentException - if the inputs are not within the inputMatrix size bounds.

subMatrix

public static float[][] subMatrix(float[][] inputMat,
                                  int rowStart,
                                  int rowEnd,
                                  int[] columnIndices)
Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).

Parameters:
inputMat - input matrix
rowStart - row to start at (inclusive)
rowEnd - column to end at (inclusive)
columnIndices - the column indices
Returns:
the submatrix containing the specified rows, and going from the original matrices rowStart to rowEnd columns; copy of A(rowIndices(:), columnStart:columnEnd).
Throws:
IllegalArgumentException - if the inputs are not within the inputMatrix size bounds.

subMatrix

public static float[][] subMatrix(float[][] inputMat,
                                  int[] rowIndices,
                                  int[] columnIndices)
Function to get a (2D) submatrix of a (2D) matrix (a copy, not a pointer).

Parameters:
inputMat - input matrix
rowIndices - the row indices
columnIndices - the column indices
Returns:
the submatrix containing the specified rows, and going from the original matrices rowStart to rowEnd columns; copy of A(rowIndices(:), columnStart:columnEnd).
Throws:
IllegalArgumentException - if the inputs are not within the inputMatrix size bounds.

subMatrix

public static int[][] subMatrix(int[][] inputMat,
                                int[] rowIndices,
                                int[] columnIndices)

subtract

public static float[] subtract(float[] x,
                               float[] y)
Returns the array z = x-y where each element of z[i] = x[i] - y[i]


subtract

public static float[][] subtract(float[][] x,
                                 float[][] y)
Returns the matrix z = x-y where each element of z[i][j] = x[i][j] - y[i][j]


sum

public static float sum(float[] data)
Returns the sum of a data sequence. That is Sum( data[i] ).


sum

public static float[] sum(float[] data1,
                          float[] data2)
Returns the sum of two data sequences. That is Sum( data[i] ).


sum

public static float[] sum(float[] data,
                          float number)
Sum of an array with a number. That is Sum( data[i] + number).

Returns:
array with each element equal to data[i] + number

sum

public static int sum(int[] data)
Returns the sum of a data sequence. That is Sum( data[i] ).


sum

public static int[] sum(int[] data1,
                        int[] data2)
Returns the sum of two data sequences. That is Sum( data[i] ).


sum

public static int[] sum(int[] data,
                        int number)
Sum of an array with a number. That is Sum( data[i] + number).

Returns:
array with each element equal to data[i] + number

sum

public static float[][] sum(float[][] x,
                            float[][] y)
Returns the matrix z = x+y where each element of z[i][j] = x[i][j] + y[i][j]


swap

public static void swap(float[] data,
                        int oldIndex,
                        int newIndex)
Swap function that swaps the values in an array

Parameters:
data - is an array of floats
oldIndex -
newIndex - e.g. data[newIndex,oldIndex] = data[oldIndex,newIndex]

swap

public static void swap(int[] data,
                        int oldIndex,
                        int newIndex)
Swap function that swaps the values in an array (overload function)

Parameters:
data - is an array of integers
oldIndex -
newIndex - e.g. data[newIndex,oldIndex] = data[oldIndex,newIndex]

symmetric

public static float[][] symmetric(float[][] data)
Returns the symmetric part of the input matrix ((A+A')/2).


skewSymmetric

public static float[][] skewSymmetric(float[][] data)
Returns the skew symmetric part of the input matrix ((A-A')/2).


trace

public static float trace(float[][] data)
Returns the matrix trace or sum of the diagonal elements.


transpose

public static float[][] transpose(float[][] data)
Returns the transpose of the input matrix.


transpose

public static int[][] transpose(int[][] data)
Returns:
the transpose of the input matrix

within

public static boolean within(float number,
                             float min,
                             float max)
Checks to see if a number is in the range specified by [min,max]. Returns true if min ≤ number ≤ max).

Parameters:
min - left-most limit (inclusive)
max - right-most limit (inclusive)
number - the value to check for.
Returns:
true if the number is within the bounds, else false

within

public static float[] within(float[] numbers,
                             float min,
                             float max)
returns an array containing all numbers within the min and max (inclusive). If nothing is within the min and max, an array of length 1 with the first element = -1 is returned



Processing library papaya by Adila Faruk. (C) 2014