|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectpapaya.Mat
public final class Mat
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,
divide(float[] array, float numberToDivideBy)
multiply(float[] array, float numberToMultiplyBy)
sum(float[] array, float numberToAdd)
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.
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 |
---|
public static float[] abs(float[] array)
public static int[] abs(int[] array)
public static float[][] column(float[][] inputMat, int indexStart, int indexEnd)
inputMat
- inputMatrix.indexStart
- index of the start column (inclusive)indexEnd
- index of the end column (inclusive)
public static float[] column(float[][] inputMat, int columnIndex)
inputMat
- input matrix.columnIndex
- index of the column to get
public static int[] column(int[][] inputMat, int columnIndex)
public static int compare(int a, int b)
public static float compare(float a, float b)
public static float[] concat(float[] a, float[] b)
a
and b
into a new array
c
such that c = {a,b}
.
public static int[] concat(int[] a, int[] b)
public static float[] constant(float constValue, int size)
size
with
each element equal to the specified constValue
.
Arrays.fill
public static int[] constant(int constValue, int size)
size
with
each element equal to the specified constValue
.
Arrays.fill
public static float[][] copy(float[][] data)
public static float[] copyThenSort(float[] data)
public static float[] cross(float[] a, float[] b)
public static float det(float[][] data)
IllegalArgumentException
- if the matrix is not square.public static float[] divide(float[] data, float value)
value
.
That is, z[i] = x[i] / value
.
IllegalArgumentException
- if value
is zero.public static float[] divide(float[] x, float[] y)
public static float[][] divide(float[][] A, float value)
public static float[][] dotDivide(float[][] A, float[][] B)
public static float[][] dotMultiply(float[][] A, float[][] B)
public static float dotProduct(float[] a, float[] b)
public static float[][] identity(int dimension)
dimension
public static float[] inverse(float[] data, float replaceZeroWith)
replaceZeroWith
value instead.
public static float[][] inverse(float[][] A)
public static boolean isConstant(float[] a)
public static float[] log(float[] data)
public static float[] log10(float[] data)
public static float[] logToBase(float[] data, float base)
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.
base
- the log base. E.g. to get log2, we would use logToBase(data,2).public static int[] linspace(int start, int end)
start
(inclusive) to end
(inclusive):
y[0] = start; y[1] = start+1; ... , y[n] = end;
.
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;
}
start
- the start index (inclusive)end
- the end index (inclusive);public static int[] linspace(int start, int end, int stepSize)
start
- the start index (inclusive)end
- the end index (inclusive);stepSize
- the stepsize to take
public static float[] linspace(float start, float end, float stepSize)
start
- the start index (inclusive)end
- the end index (inclusive);stepSize
- the stepsize to take
public static float mag(float[] data)
public static float map(float data, float low1, float high1, float low2, float high2)
map(value, low1, high1, low2, high2) = low2 + (value - low1) * ( (high2-low2) / (high1-low1) )
Similar to Processing's "map" function.
public static float[] map(float[] data, float from, float to)
public static float[] map(float[] data, float low1, float high1, float low2, float high2)
public static float[][] multiply(float[][] A, float[][] B)
public static float[] multiply(float[][] A, float[] x)
public static float[][] multiply(float[][] A, float a)
public static float[] multiply(float[] data1, float[] data2)
z[i] = x[i] * y[i]
.
public static float[] multiply(float[] data, float number)
z[i] = k * x[i]
.
public static float norm1(float[][] data)
public static float norm2(float[][] data)
public static float norm2(float[] data)
mag(float[])
.
public static float normF(float[][] data)
public static float normInf(float[][] data)
public static float[] normalizeToMinMax(float[] data)
public static float[] normalizeToSum(float[] data)
public static double[] populate(double[] fullDataset, int[] indices)
y[0] = fullDataset[ indices[0] ]; y[1] = fullDataset[ indices[1] ]; ... ... y[n-1] = fullDataset[ indices[n-1] ];
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.public static float[] populate(float[] fullDataset, int[] indices)
y[0] = fullDataset[ indices[0] ]; y[1] = fullDataset[ indices[1] ]; ... ... y[n-1] = fullDataset[ indices[n-1] ];
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.public static int[] populate(int[] fullDataset, int[] indices)
y[0] = fullDataset[ indices[0] ]; y[1] = fullDataset[ indices[1] ]; ... ... y[n-1] = fullDataset[ indices[n-1] ];
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.public static String[] populate(String[] fullDataset, int[] indices)
y[0] = fullDataset[ indices[0] ]; y[1] = fullDataset[ indices[1] ]; ... ... y[n-1] = fullDataset[ indices[n-1] ];
indices
- the indices in the fullDataset you want to extract and store.fullDataset
- the full dataset.public static void print(double[] data, int d)
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.
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(double[][] data, int d)
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.
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(double[][] data, String[] columnLabels, String[] rowLabels, int d)
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(float[] data, int d)
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.
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(float[][] data, int d)
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.
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(float[][] data, String[] columnLabels, String[] rowLabels, int d)
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(int[] data, int d)
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.
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(int[][] data, int d)
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.
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static void print(int[][] data, String[] columnLabels, String[] rowLabels, int d)
data
- the matrix to be printed.d
- Number of digits after the decimal to display..public static int rank(float[][] data)
Rank
for ranking the elements of an input 1D array.
public static float[][] replace(float[][] A, float oldValue, float newValue)
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...
public static float[] replace(float[] data, float oldValue, float newValue)
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...
public static float[][] reshape(float[][] arr, int numRows, int numColumns)
public static double[] reverse(double[] data)
public static float[] reverse(float[] data)
public static int[] reverse(int[] data)
public static float roundToDecimalPlace(float number, int decimal)
Examples:
roundToDecimalPlace(1.234567, 2) --> 1.23;
roundToDecimalPlace(100, 2) --> 100;
number
- number to round.decimal
- decimal place to round to.
public static float floorToNearest(float currentVal, int interval)
Examples:
floorToNearest(173.2,5) --> 170;
floorToNearest(28,10) --> 20;
currentVal
- number to floor.interval
- to floor to. E.g. in fives, twos, tens, etc.
public static float ceilToNearest(float currentVal, int interval)
Examples:
ceilToNearest(173.2,5) --> 175;
ceilToNearest(28,10) --> 30;
currentVal
- number to ceil.interval
- to ceil to. E.g. in fives, twos, tens, etc.
public static ArrayList[] split(float[] sortedList, float[] splitters)
splitters=(a,b,c,...,y,z)
defines the ranges [-inf,a), [a,b), [b,c), ..., [y,z), [z,inf]
.
Examples:
data = (1,2,3,4,5,8,8,8,10,11)
.
splitters=(2,8)
yields 3 bins: (1), (2,3,4,5) (8,8,8,10,11)
.
splitters=()
yields 1 bin: (1,2,3,4,5,8,8,8,10,11)
.
splitters=(-5)
yields 2 bins: (), (1,2,3,4,5,8,8,8,10,11)
.
splitters=(100)
yields 2 bins: (1,2,3,4,5,8,8,8,10,11), ()
.
sortedList
- the list to be partitioned (must be sorted ascending).splitters
- the points at which the list shall be partitioned (must be sorted ascending).
length == splitters.length + 1
.
Each sublist is returned sorted ascending.public static float[][] subMatrix(float[][] inputMat, int rowStart, int rowEnd, int columnStart, int columnEnd)
inputMat
- input matrixrowStart
- row to start at (inclusive)rowEnd
- row to end at (inclusive)columnStart
- column to start at (inclusive)columnEnd
- column to end at (inclusive)
A(rowStart:rowEnd, columnStart:columnEnd)
.
IllegalArgumentException
- if the rowStart, rowEnd, columnStart, or columnEnd parameters
are not within the inputMatrix size bounds.public static float[][] subMatrix(float[][] inputMat, int[] rowIndices, int columnStart, int columnEnd)
inputMat
- input matrixrowIndices
- the row indicescolumnStart
- column to start at (inclusive)columnEnd
- column to end at (inclusive)
A(rowIndices(:), columnStart:columnEnd)
.
IllegalArgumentException
- if the inputs are not within the inputMatrix size bounds.public static float[][] subMatrix(float[][] inputMat, int rowStart, int rowEnd, int[] columnIndices)
inputMat
- input matrixrowStart
- row to start at (inclusive)rowEnd
- column to end at (inclusive)columnIndices
- the column indices
A(rowIndices(:), columnStart:columnEnd)
.
IllegalArgumentException
- if the inputs are not within the inputMatrix size bounds.public static float[][] subMatrix(float[][] inputMat, int[] rowIndices, int[] columnIndices)
inputMat
- input matrixrowIndices
- the row indicescolumnIndices
- the column indices
A(rowIndices(:), columnStart:columnEnd)
.
IllegalArgumentException
- if the inputs are not within the inputMatrix size bounds.public static int[][] subMatrix(int[][] inputMat, int[] rowIndices, int[] columnIndices)
public static float[] subtract(float[] x, float[] y)
public static float[][] subtract(float[][] x, float[][] y)
public static float sum(float[] data)
public static float[] sum(float[] data1, float[] data2)
public static float[] sum(float[] data, float number)
public static int sum(int[] data)
public static int[] sum(int[] data1, int[] data2)
public static int[] sum(int[] data, int number)
public static float[][] sum(float[][] x, float[][] y)
public static void swap(float[] data, int oldIndex, int newIndex)
data
- is an array of floatsoldIndex
- newIndex
- e.g. data[newIndex,oldIndex] = data[oldIndex,newIndex]public static void swap(int[] data, int oldIndex, int newIndex)
data
- is an array of integersoldIndex
- newIndex
- e.g. data[newIndex,oldIndex] = data[oldIndex,newIndex]public static float[][] symmetric(float[][] data)
public static float[][] skewSymmetric(float[][] data)
public static float trace(float[][] data)
public static float[][] transpose(float[][] data)
public static int[][] transpose(int[][] data)
public static boolean within(float number, float min, float max)
min
- left-most limit (inclusive)max
- right-most limit (inclusive)number
- the value to check for.
public static float[] within(float[] numbers, float min, float max)
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |