jhplot.math
Class DoubleArray
- java.lang.Object
-
- jhplot.math.DoubleArray
-
- Direct Known Subclasses:
- LinearAlgebra
public class DoubleArray extends java.lang.Object
A class for dealing with double arrays and matrices. Copyright : BSD License
-
-
Constructor Summary
Constructors Constructor and Description DoubleArray()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description static double[][]
add(double[][] M, double a)
Adds a single value to a matrix, e.g.static double[][]
add(double[][] M1, double[][] M2)
Adds a matrix to another matrix, which should be of the same dimension.static double[][]
buildXY(double[] X, double[] Y)
Join two arrays into a matrix.static double[][]
buildXY(double Xmin, double Xmax, double[] Y)
Generate a two column matrix.static void
checkColumnDimension(double[][] M, int n)
Checks to make sure each row of a matrix is of a specified dimension.static void
checkLength(double[] M, int n)
Checks to make sure an array is of a specified length.static void
checkRowDimension(double[][] M, int m)
Checks to make sure each column of a matrix is of a specified dimension.static double[][]
columnVector(double[] x)
Converts an n element array into an n x 1 matrix.static double[]
copy(double[] M)
Generate a copy of an arraystatic double[][]
copy(double[][] M)
Generate a copy of a matrixstatic double[]
cumProduct(double[] v)
Calculates cumulative product of the values in an array.static double[][]
cumProduct(double[][] v)
Calculates the cumulative product of each column in a matrix.static double[]
cumSum(double[] v)
Calculates the cumulative sum of an array.static double[][]
cumSum(double[][] v)
Calculates the cumulative sum of each column in a matrix.static double[]
delete(double[] x, int... J)
Delete a list of elements from an array.static double[][]
deleteColumns(double[][] x, int... J)
Deletes a list of columns from a matrix.static double[][]
deleteColumnsRange(double[][] x, int J1, int J2)
Deletes a range of columns from a matrix.static double[]
deleteRange(double[] x, int J1, int J2)
Delete a range of elements from an array.static double[][]
deleteRows(double[][] x, int... I)
Deletes a list of rows from a matrix.static double[][]
deleteRowsRange(double[][] x, int I1, int I2)
Deletes a range of rows from a matrix.static double[][]
diagonal(double... c)
Returns an m x m matrix.static double[][]
diagonal(int m, double c)
Returns an m x m matrix.static double[][]
f(double[][] M, Function f)
Apply a scalar function to every element of an array.static double[]
f(double[] M, Function func)
Apply a scalar function to every element of an array.static double[]
fill(int m, double c)
Provides an m element array filled with constant c.static double[][]
fill(int m, int n, double c)
Fills an m x n matrix of doubles with constant c.static double[]
findex(int m, IndexFunction f)
Provides a sequence of values to which some function has been applied.static double[]
getColumnCopy(double[][][] M, int j, int k)
Extract one column from a three dimensional array.static double[]
getColumnCopy(double[][] M, int j)
Extract one column from a matrix.static int
getColumnDimension(double[][] M, int i)
Get the number of columns in a specified row of a matrix.static double[][]
getColumnsCopy(double[][] M, int... J)
Extract specific columns from a matrix.static double[][]
getColumnsRangeCopy(double[][] M, int j1, int j2)
Extract a range of columns from a matrix.static double[]
getCopy(double[] M, int... I)
Extract specific elements from an array.static double[]
getDiagonal(double[][] M, int I)
Extract diagonal from a matrix.static double[]
getRangeCopy(double[] M, int j1, int j2)
Extract a section of an array.static double[]
getRowCopy(double[][] M, int i)
Extract a row from a matrix.static double[][]
getRowsCopy(double[][] M, int... I)
Extract specific rows from a matrix.static double[][]
getRowsRangeCopy(double[][] M, int i1, int i2)
Extract a range of rows from a matrix.static double[][]
getSubMatrixRangeCopy(double[][] M, int i1, int i2, int j1, int j2)
Carve out a submatrix from the input matrix and return a copy.static double[][]
identity(int m)
Generates an m x m identity matrix.static double[]
increment(double begin, double pitch, double end)
Generates an array of successive values from begin to end with step size pitch.static double[]
increment(int m, double begin, double pitch)
Provides a sequence of successive values in an array.static double[][]
increment(int m, int n, double[] begin, double[] pitch)
Generates an mxn matrix.static double[][]
increment(int m, int n, double begin, double pitch)
Provides an mxn matrix.static double[]
insert(double[] x, int I, double... y)
Insert any number of values, or a single array, between 2 elements of an array.static double[][]
insertColumns(double[][] x, int J, double[]... y)
Insert any number of arrays between 2 columns of a matrix.static double[][]
insertRows(double[][] x, int I, double[]... y)
Insert any number of arrays between 2 rows of a matrix.static boolean
isColumnDimension(double[][] M, int n)
Same as checkColumnDimension() but just returns a boolean value rather than throwing an exception.static boolean
isLength(double[] M, int n)
Same as checkLength but returns a boolean value rather than throwing an exception.static boolean
isRowDimension(double[][] M, int m)
Same as checkRowDimension() but just returns a boolean value rather than throwing an exception.static void
main(java.lang.String[] args)
static double
max(double... M)
Finds maximum value in either a list of numbers or a single array.static double[]
max(double[][] M)
Finds the maximum value in each column of a matrix.static int
maxIndex(double... M)
Finds the index of the maximum value in a list of values or an array.static int[]
maxIndex(double[][] M)
Finds the indices of the minimum values in each column of a matrix.static double[]
merge(double[]... x)
Concatenates arrays.static double[][]
mergeColumns(double[]... x)
Combine a set of arrays into a matrix.static double[][]
mergeRows(double[]... x)
Combine a set of arrays into a matrix.static double
min(double... M)
Finds minimum value in either a list of numbers or a single array.static double[]
min(double[][] M)
Finds the minimum value in each column of a matrix.static int
minIndex(double... M)
Finds the index of the minumum value in a list of values or an array.static int[]
minIndex(double[][] M)
Finds the indices of the minimum values in each column of a matrix.static double[]
one(int m)
Deprecated.usefill(int m, double c)
instead.static double[]
one(int m, double c)
Deprecated.Usefill(int m, double c)
instead.static double[][]
one(int m, int n)
Deprecated.usefill(int m, int n, 1.0)
instead.static double[][]
one(int m, int n, double c)
Deprecated.usefill(int m, int n, double c)
instead.static double
product(double[] v)
Calculates the product of the values in an array.static double[]
product(double[][] v)
Calculates the product of the values in each column of a matrix.static double[]
random(int m)
Generates an m element array of random numbers uniformly distributed between 0 and 1.static double[]
random(int m, double min, double max)
Generates an m element array of random numbers uniformly distributed between min and max.static double[][]
random(int m, int n)
Generates an m x n matrix of random numbers uniformly distributed between 0 and 1.static double[][]
random(int m, int n, double[] min, double[] max)
Generates an m x n matrix of random numbers uniformly distributed numbers.static double[][]
random(int m, int n, double min, double max)
Generates an mxn matrix of random numbers uniformly distributed between min and max.static double[][]
resize(double[][] M, int m, int n)
Generate a resized copy of a matrixstatic double[][]
rowVector(double[] x)
Converts an n element array into an 1 x n matrix.static double[]
sort(double[] values)
Sorts an array in ascending order.static double[][]
sort(double[][] values, int column)
Sorts the rows of a matrix using a specified column as the key.static double
sum(double[] v)
Calculate the sum of the values in an arraystatic double[]
sum(double[][] v)
Calculates the sum of each column in a matrix.static void
throwError(java.lang.String msg)
Throws an error exception if an argument is invalid.static java.lang.String
toString(double[]... v)
Generates a string that holds a nicely organized space-seperated version of a matrix or array.static java.lang.String
toString(java.lang.String format, double[]... v)
Generates a string that holds a nicely organized version of a matrix or array.static double[][]
transpose(double[][] M)
Transposes an mxn matrix into an nxm matrix.
-
-
-
Method Detail
-
main
public static void main(java.lang.String[] args)
-
add
public static double[][] add(double[][] M, double a)
Adds a single value to a matrix, e.g. adds the constant value a to all element of the matrix M.- Returns:
- The resulting matrix.
-
add
public static double[][] add(double[][] M1, double[][] M2)
Adds a matrix to another matrix, which should be of the same dimension.- Returns:
- The resulting matrix.
-
identity
public static double[][] identity(int m)
Generates an m x m identity matrix. Result has ones along the diagonal and zeros everywhere else. Example:
double[][] id = identity(3);
Result is:
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0
- Parameters:
m
- an integer > 0.- Returns:
- m x m two dimensional array of doubles.
-
diagonal
public static double[][] diagonal(int m, double c)
Returns an m x m matrix. result has constants along the diagonal and zeros everywhere else. Example:
double[][] eig = diagonal(3, 2.1);
Result is:
2.1 0.0 0.0
0.0 2.1 0.0
0.0 0.0 2.1
- Parameters:
m
- an integer > 0.c
- Constant that lies along diagonal. Set c=1 for identity matrix.- Returns:
- m x m 2D array of doubles.
-
diagonal
public static double[][] diagonal(double... c)
Returns an m x m matrix. result has specified values along the diagonal and zeros everywhere else. Example:
double[][] eig = diagonal(1.0,2.0,3.0);
Result is:
1.0 0.0 0.0
0.0 2.0 0.0
0.0 0.0 3.0
- Parameters:
c
- Values that lies along diagonal.- Returns:
- c.length x c.length 2D array of doubles.
-
one
public static double[][] one(int m, int n)
Deprecated. usefill(int m, int n, 1.0)
instead.Provides an m x n matrix filled with ones.- Parameters:
m
- Number of rows in returned matrixn
- Number of columns in returned matrix- Returns:
- m x n 2D array of ones
-
one
public static double[][] one(int m, int n, double c)
Deprecated. usefill(int m, int n, double c)
instead.Provides an mxn matrix filled with constant c- Parameters:
m
- Number of rows in returned matrixn
- Number of columns in returned matrixc
- Constant that fills matrix- Returns:
- m x n 2D array of constants
-
one
public static double[] one(int m)
Deprecated. usefill(int m, double c)
instead.Provides an m element array filled with ones- Parameters:
m
- Number of elements in returned array- Returns:
- m element array of ones
-
one
public static double[] one(int m, double c)
Deprecated. Usefill(int m, double c)
instead.Provides an m element array filled with constant c- Parameters:
m
- Number of elements in returned arrayc
- Constant that fills array- Returns:
- m element array of constants
-
fill
public static double[][] fill(int m, int n, double c)
Fills an m x n matrix of doubles with constant c. Example:
double[][] u = fill(2, 3, 1.0);
Result is:
1.0 1.0 1.0
1.0 1.0 1.0
- Parameters:
m
- Number of rows in matrixn
- Number of columns in matrixc
- constant that fills matrix- Returns:
- m x n 2D array of constants c
-
fill
public static double[] fill(int m, double c)
Provides an m element array filled with constant c. Example:
double[] u = fill(3, 1.0);
Result is:
1.0 1.0 1.0
- Parameters:
m
- Number of elements in returned arrayc
- Constant that fills array- Returns:
- m element array of constants
-
random
public static double[][] random(int m, int n)
Generates an m x n matrix of random numbers uniformly distributed between 0 and 1.- Parameters:
m
- Number of rows in matrixn
- Number of columns in matrix- Returns:
- 2D array of random numbers.
-
random
public static double[] random(int m)
Generates an m element array of random numbers uniformly distributed between 0 and 1.- Parameters:
m
- Size of array- Returns:
- Array of random numbers.
-
random
public static double[][] random(int m, int n, double min, double max)
Generates an mxn matrix of random numbers uniformly distributed between min and max.- Parameters:
m
- Number of rows in matrixn
- Number of columns in matrixmin
- minimum value of the random numbersmax
- maximum value of the random numbers- Returns:
- 2D array of random numbers.
-
random
public static double[] random(int m, double min, double max)
Generates an m element array of random numbers uniformly distributed between min and max.- Parameters:
m
- Size of arraymin
- minimum value of the random numbersmax
- maximum value of the random numbers- Returns:
- Array of random numbers.
-
random
public static double[][] random(int m, int n, double[] min, double[] max)
Generates an m x n matrix of random numbers uniformly distributed numbers. The bounds of the random numbers each column are in the arrays min and max. So the values in column j are uniformly distributed random numbers between min[j] and max[j]- Parameters:
m
- Number of rows in matrixn
- Number of columns in matrixmin
- minimum value of the random numbers in a columnmax
- maximum value of the random numbers in a column- Returns:
- 2D array of random numbers.
-
increment
public static double[][] increment(int m, int n, double begin, double pitch)
Provides an mxn matrix. Each column is a sequence of successive values of stepsize pitch and starts at begin. The columns are all identical.- Parameters:
m
- Number of rows in matrixn
- Number of columns in matrixbegin
- First value in sequencepitch
- Step size of sequence- Returns:
- m x n 2D array
-
increment
public static double[] increment(int m, double begin, double pitch)
Provides a sequence of successive values in an array. e.g. increment(4, 3, 2) = {3,5,7,9}- Parameters:
m
- Size of arraybegin
- Starting valuepitch
- Step size- Returns:
- m point array. Sequence of values from begin to begin + (m-1)*pitch
-
increment
public static double[][] increment(int m, int n, double[] begin, double[] pitch)
Generates an mxn matrix. Each column is a sequence of succesive values. Each column has it's own starting value and step size.- Parameters:
m
- Number of rows in matrixn
- Number of columns in matrixbegin
- Array of starting values for each column. length must = n.pitch
- Array of step sizes for each column. length must = n.- Returns:
- 2D array
-
increment
public static double[] increment(double begin, double pitch, double end)
Generates an array of successive values from begin to end with step size pitch.- Parameters:
begin
- First value in sequencepitch
- Step size of sequenceend
- Last value of sequence- Returns:
- Array of successive values
-
copy
public static double[] copy(double[] M)
Generate a copy of an array- Parameters:
M
- Input array.- Returns:
- A copy of the input array.
-
copy
public static double[][] copy(double[][] M)
Generate a copy of a matrix- Parameters:
M
- Input matrix.- Returns:
- A copy of the input matrix
-
resize
public static double[][] resize(double[][] M, int m, int n)
Generate a resized copy of a matrix- Parameters:
M
- Input matrix.m
- number of rows of new matrix.n
- number of columns of new matrix.- Returns:
- A resized copy of the input matrix
-
getSubMatrixRangeCopy
public static double[][] getSubMatrixRangeCopy(double[][] M, int i1, int i2, int j1, int j2)
Carve out a submatrix from the input matrix and return a copy. Result is the intersection of rows i1 through i2 and columns j1 through j2 inclusive. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[][] b = getSubMatrixRangeCopy(a, 2, 3, 1, 3);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
result is:
13 14 15
19 20 21
- Parameters:
M
- Input matrixi1
- Index of first row in cuti2
- Index of last row in cutj1
- Index of first column in cutj2
- Index of last column in cut- Returns:
- submatrix. Input matrix is left unharmed.
-
getColumnsRangeCopy
public static double[][] getColumnsRangeCopy(double[][] M, int j1, int j2)
Extract a range of columns from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[][] z = getColumnsRangeCopy(a, 2, 4);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
result is:
2 3 4
8 9 10
14 15 16
20 21 22
26 27 28
- Parameters:
M
- Input matrixj1
- Index of first column to be extracted.j2
- Index of last column to be extracted.- Returns:
- An mxn matrix where m=number of rows in M and n=j2-j1+1
-
getColumnsCopy
public static double[][] getColumnsCopy(double[][] M, int... J)
Extract specific columns from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[][] z = getColumnsCopy(a, 2, 4);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
result is:
0 2 4
1 8 10
2 14 16
3 20 22
4 26 28
- Parameters:
M
- Input matrixJ
- Each is the index of a column. There can be as many indices listed as there are columns in M.- Returns:
- An mxn matrix where m=number of rows in M and n=number of indices listed
-
getColumnCopy
public static double[] getColumnCopy(double[][] M, int j)
Extract one column from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[][] z = getColumnCopy(a, 2);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
Result is:
2 8 14 20 26
- Parameters:
M
- Input matrixj
- Index of desired column- Returns:
- Array of values from extracted column.
-
getColumnCopy
public static double[] getColumnCopy(double[][][] M, int j, int k)
Extract one column from a three dimensional array. The result is an array of the values in the first dimension.- Parameters:
M
- Input 3D arrayj
- The index of the second dimension.k
- The index of the third dimension- Returns:
- An array
-
getRowsCopy
public static double[][] getRowsCopy(double[][] M, int... I)
Extract specific rows from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[][] z = getRowsCopy(a, 2, 4);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
result is:
2 13 14 15 16
4 25 26 27 28
- Parameters:
M
- Input matrixI
- Each is the index of a column.- Returns:
- An mxn matrix where m=number of indices listed and n=number of columns in m
-
getRowCopy
public static double[] getRowCopy(double[][] M, int i)
Extract a row from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[] z = getRowCopy(a, 2);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
result is:
2 13 14 15 16
- Parameters:
M
- Input matrixi
- index of row to copy- Returns:
- An array of n values where n = number of columns in M
-
getRowsRangeCopy
public static double[][] getRowsRangeCopy(double[][] M, int i1, int i2)
Extract a range of rows from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[][] z = getRowsRangeCopy(a, 2, 4);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
result is:
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
- Parameters:
M
- Input matrixi1
- Index of first row to be extracted.i2
- Index of last row to be extracted.- Returns:
- An mxn matrix where m=j2-j1+1 and n=number of columns in M
-
getRangeCopy
public static double[] getRangeCopy(double[] M, int j1, int j2)
Extract a section of an array. Example:
double[] a = {00,11,22,33,44,55,66,77,88,99};
double[] z = getRangeCopy(a, 2, 5);
result is:
22 33 44 55
- Parameters:
M
- Input arrayj1
- Index of first term to getj2
- Index of last term to get- Returns:
- An array with j2-j1+1 elements
-
getCopy
public static double[] getCopy(double[] M, int... I)
Extract specific elements from an array. Example:
double[] a = {00,11,22,33,44,55,66,77,88,99};
double[] z = getCopy(a, 2, 5);
result is:
22 55
- Parameters:
M
- The input array.I
- the indices of the elements to extract- Returns:
- The output array of n elements where n=number of indices listed.
-
getColumnDimension
public static int getColumnDimension(double[][] M, int i)
Get the number of columns in a specified row of a matrix. Used for oddly sized matrices.- Parameters:
M
- Input matrixi
- Index of row whose column length will be returned- Returns:
- The number of columns in row i
-
getDiagonal
public static double[] getDiagonal(double[][] M, int I)
Extract diagonal from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,25,26,27,28}};
double[] z = getDiagonal(a, 1);
input is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 25 26 27 28
result is:
1 8 15 22
- Parameters:
M
- Input matrixI
- index of diagonal to copy- Returns:
- An array
-
mergeRows
public static double[][] mergeRows(double[]... x)
Combine a set of arrays into a matrix. Each array becomes a row. Rows may be of different lengths. Example:
double[] a = {00,11,22,33,44}, b = {55,66,77,88}; double[][] z = mergeRows(a, b); result is:
0 11 22 33 44 55 66 77 88- Parameters:
x
- Input arrays- Returns:
- A matrix. Will be non-rectangular if arrays are not all the same length.
-
mergeColumns
public static double[][] mergeColumns(double[]... x)
Combine a set of arrays into a matrix. Each array becomes a column. Arrays must all be of same length. Example:
double[] a = {00,11,22,33,44}, b = {55,66,77,88,99}; double[][] z = mergeColumns(a, b); result is:
0 55 11 66 22 77 33 88 44 99- Parameters:
x
- Input arrays- Returns:
- An mxn matrix where m=size of any input array and n is the number of arrays.
-
columnVector
public static double[][] columnVector(double[] x)
Converts an n element array into an n x 1 matrix. Handy for matrix math.- Parameters:
x
- n element array- Returns:
- n x 1 matrix
-
rowVector
public static double[][] rowVector(double[] x)
Converts an n element array into an 1 x n matrix. Handy for matrix math.- Parameters:
x
- n element array- Returns:
- 1 x n matrix
-
merge
public static double[] merge(double[]... x)
Concatenates arrays. Example:
double[] a = {00,11,22,33,44}, b = {55,66,77,88,99};
double[] z = merge(a, b, a);
result is:
0 11 22 33 44 55 66 77 88 99 0 11 22 33 44- Parameters:
x
- Input arrays- Returns:
- Output array.
-
insertColumns
public static double[][] insertColumns(double[][] x, int J, double[]... y)
Insert any number of arrays between 2 columns of a matrix. Size of the arrays must equal number of rows in the matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,23,24,25,26}};
double[] b = {00,11,22,33,44}, c = {55,66,77,88,99};
double[][] z = insertColumns(a, 2, b, c);
input matrix is:
0 1 2 3 4
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
4 23 24 25 26
result is:
0 1 0 55 2 3 4
1 7 11 66 8 9 10
2 13 22 77 14 15 16
3 19 33 88 20 21 22
4 23 44 99 24 25 26
- Parameters:
x
- Input m x n matrix.J
- Index of column before which the new columns will be inserted.y
- The arrays to be inserted- Returns:
- New matrix with added columns.
-
insertRows
public static double[][] insertRows(double[][] x, int I, double[]... y)
Insert any number of arrays between 2 rows of a matrix. Size of the arrays must equal number of columns in the matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22}};
double[] b = {0,11,22,33,44}, c = {55,66,77,88,99};
double[][] z = insertRows(a, 1, b, c);
result is:
0 1 2 3 4
0 11 22 33 44
55 66 77 88 99
1 7 8 9 10
2 13 14 15 16
3 19 20 21 22
- Parameters:
x
- Input m x n matrix.I
- Index of row before which the new rows will be inserted.y
- The arrays to be inserted- Returns:
- New matrix with added rows.
-
insert
public static double[] insert(double[] x, int I, double... y)
Insert any number of values, or a single array, between 2 elements of an array. Example:
double[] b = {00,11,22,33,44}, c = {55,66,77,88,99};
double[] z = insert(b, 2, 333, 444);
result is:
0 11 333 444 22 33 44 double[] z = insert(b, 2, c);
result is:
0 11 55 66 77 88 99 22 33 44
- Parameters:
x
- Input array.I
- Index of element before which the values will be inserted.y
- Values to be inserted. Can also be a single array.- Returns:
- Expanded array.
-
deleteColumnsRange
public static double[][] deleteColumnsRange(double[][] x, int J1, int J2)
Deletes a range of columns from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,23,24,25,26}};
double[][] z = deleteColumnsRange(a, 1, 3);
result is:
0 4
1 10
2 16
3 22
4 26
- Parameters:
x
- The input matrixJ1
- The Index of the first column to delete.J2
- The index of the last column to delete.- Returns:
- The reduced matrix.
-
deleteColumns
public static double[][] deleteColumns(double[][] x, int... J)
Deletes a list of columns from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,23,24,25,26}};
double[][] z = deleteColumns(a, 1, 3);
result is:
0 2 4
1 8 10
2 14 16
3 20 22
4 24 26
- Parameters:
x
- The input matrixJ
- The indices of the columns to be deleted. There must be no more indices listed than there are columns in the input matrix.- Returns:
- The reduced matrix.
-
deleteRowsRange
public static double[][] deleteRowsRange(double[][] x, int I1, int I2)
Deletes a range of rows from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,23,24,25,26}};
double[][] z = deleteRowsRange(a, 1, 3);
result is:
0 1 2 3 4
4 23 24 25 26
- Parameters:
x
- The input matrixI1
- The Index of the first row to delete.I2
- The index of the last row to delete.- Returns:
- The reduced matrix.
-
deleteRows
public static double[][] deleteRows(double[][] x, int... I)
Deletes a list of rows from a matrix. Example:
double[][] a = {{0,1,2,3,4},{1,7,8,9,10},{2,13,14,15,16},{3,19,20,21,22},{4,23,24,25,26}};
double[][] z = deleteRows(a, 1, 3);
result is:
0 1 2 3 4
2 13 14 15 16
4 23 24 25 26
- Parameters:
x
- The input matrixI
- The indices of the rows to delete.- Returns:
- The reduced matrix.
-
deleteRange
public static double[] deleteRange(double[] x, int J1, int J2)
Delete a range of elements from an array. Example:
double[] b = {00,11,22,33,44};
double[] z = deleteRange(b, 1, 3);
Result is:
0 44
- Parameters:
x
- Input arrayJ1
- Index of first element to delete. Must be >= 0 and <= x.lengthJ2
- Index of last element to delete. Must be >= J1 and <= x.length- Returns:
- Reduced array.
-
delete
public static double[] delete(double[] x, int... J)
Delete a list of elements from an array. Example:
double[] b = {00,11,22,33,44};
double[] z = deleteRange(b, 1, 3);
Result is:
0 22 44
- Parameters:
x
- Input arrayJ
- Index of elements to delete. Each must be >= 0 and <= x.length- Returns:
- Reduced array.
-
buildXY
public static double[][] buildXY(double Xmin, double Xmax, double[] Y)
Generate a two column matrix. Second column is just the values in Y The first column is a uniform sequence of values from Xmin to Xmax. Step size is automatic. Useful for generating values for an x axis when y is already defined and bundling the pairs into a matrix. Example:
double[] y = {0.0, 1.0, 4.0, 9.0, 16.0};
double[][] xy = buildXY(0.0, 4.0, y);
result:
0.0 0.0
1.0 1.0
2.0 4.0
3.0 9.0
4.0 16.0
- Parameters:
Xmin
- The first value in the first columnXmax
- The last value in the first columnY
- An array that will fill the second column.- Returns:
- nx2 array of values where n = length of y.
- Throws:
java.lang.IllegalArgumentException
-
buildXY
public static double[][] buildXY(double[] X, double[] Y)
Join two arrays into a matrix. Each array becomes a column in the matrix. Example:
double[] x = {0,1,2};
double[] y = {1,7,8};
double[][] z = buildXY(x, y);
result is:
0 1
1 7
2 8
- See Also:
mergeColumns(double[][])
-
min
public static double[] min(double[][] M)
Finds the minimum value in each column of a matrix. Example:
double[][] a = {{0, 1, 2, 3, 4},
{1, .7, 8, 9, 10},
{2, 13, 1.4, 1.5, 16},
{3, 19, 20, 21, 22},
{4, 23, 24, 25, 2.6}};
double[] z = min(a);
Result is:
0.0 0.7 1.4 1.5 2.6
- Parameters:
M
- The input matrix- Returns:
- Array of minimums from each column of M.
-
min
public static double min(double... M)
Finds minimum value in either a list of numbers or a single array.- Parameters:
M
- Can be a list of values e.g. min(1,22,333,.4) or an array.- Returns:
- The minimum value.
-
max
public static double[] max(double[][] M)
Finds the maximum value in each column of a matrix. Example:
double[][] a = {{0, 1, 2, 3, 4},
{1, 7, 88, 9, 10},
{2, 13, 14, 15, 16},
{3, 19, 20, 201, 22},
{4, 23, 24, 25, 26}};
double[] z = max(a);
Result is:
4 23 88 201 26
- Parameters:
M
- The input matrix- Returns:
- Array of maximums from each column of M.
-
max
public static double max(double... M)
Finds maximum value in either a list of numbers or a single array.- Parameters:
M
- Can be a list of values e.g. min(1,22,333,.4) or an array.- Returns:
- The maximum value.
-
minIndex
public static int[] minIndex(double[][] M)
Finds the indices of the minimum values in each column of a matrix. Example:
double[][] a = {{0, 1, 2, 3, 4},
{1, .7, 8, 9, 10},
{2, 13, 1.4, 1.5, 16},
{3, 19, 20, 21, 22},
{4, 23, 24, 25, 2.6}};
int[] z = minIndex(a);
Result is:
0 1 2 2 4
- Parameters:
M
- Input matrix.- Returns:
- Array of indices of the minimums from each column of M.
-
minIndex
public static int minIndex(double... M)
Finds the index of the minumum value in a list of values or an array.- Parameters:
M
- Can be a list of values e.g. minIndex(11,22,44,2) or an array- Returns:
- index of minimum value in M
-
maxIndex
public static int[] maxIndex(double[][] M)
Finds the indices of the minimum values in each column of a matrix. Example:
double[][] a = {{0, 1, 2, 3, 4},
{1, 7, 88, 9, 10},
{2, 13, 14, 15, 16},
{3, 19, 20, 201, 22},
{4, 23, 24, 25, 26}};
int[] z = maxIndex(a);
Result is:
4 4 1 3 4
- Parameters:
M
- Input matrix.- Returns:
- Array of indices of the maximums from each column of M.
-
maxIndex
public static int maxIndex(double... M)
Finds the index of the maximum value in a list of values or an array.- Parameters:
M
- Can be a list of values e.g. maxIndex(11,22,44,2) or an array- Returns:
- index of maximum value in M
-
sum
public static double sum(double[] v)
Calculate the sum of the values in an array- Parameters:
v
- input array- Returns:
- sum of values in v
-
sum
public static double[] sum(double[][] v)
Calculates the sum of each column in a matrix.- Parameters:
v
-- Returns:
- Array. value of i'th element is sum of values in column(i)
-
cumSum
public static double[] cumSum(double[] v)
Calculates the cumulative sum of an array. Think of it as an integral. Example:
double[] b = {0,1,2,3,4};
double[] z = cumSum(b);
result is:
0 1 3 6 10
- Parameters:
v
- Input array.- Returns:
- Output array of same length as v.
-
cumSum
public static double[][] cumSum(double[][] v)
Calculates the cumulative sum of each column in a matrix. Example:
double[][] a = {{0, 1, 2, 3, 4},
{1, 7, 8, 9, 10},
{2, 13, 14, 15, 16},
{3, 19, 20, 21, 22},
{4, 23, 24, 25, 26}};
double[][] z = cumSum(a);
result is:
0 1 2 3 4
1 8 10 12 14
3 21 24 27 30
6 40 44 48 52
10 63 68 73 78
- Parameters:
v
-- Returns:
- Output matrix. Each column is the cumulative sum of each column in v.
-
product
public static double product(double[] v)
Calculates the product of the values in an array.- Parameters:
v
- Input array.- Returns:
- The product of the values in v.
-
product
public static double[] product(double[][] v)
Calculates the product of the values in each column of a matrix.- Parameters:
v
- Input matrix.- Returns:
- An array of n values where n=number of columns in v. The i'th element is product of the values in the i'th column of v.
-
cumProduct
public static double[] cumProduct(double[] v)
Calculates cumulative product of the values in an array. Example:
double[] b = {1,2,3,4};
double[] z = cumProduct(b);
Result is:
1 2 6 24
- Parameters:
v
- Input array- Returns:
- Out put array of same size as v.
-
cumProduct
public static double[][] cumProduct(double[][] v)
Calculates the cumulative product of each column in a matrix. Example:
double[][] a = {{0, 1, 2, 3, 4},
{1, 7, 8, 9, 10},
{2, 13, 14, 15, 16},
{3, 19, 20, 21, 22},
{4, 23, 24, 25, 26}};
result is:
0 1 2 3 4
0 7 16 27 40
0 91 224 405 640
0 1729 4480 8505 14080
0 39767 107520 212625 366080
- Parameters:
v
-- Returns:
- Output matrix. Each column is the cumulative product of each column in v.
-
toString
public static java.lang.String toString(double[]... v)
Generates a string that holds a nicely organized space-seperated version of a matrix or array. An extra space is automatically included. Note the lower case S. It's tostring() not toString(). Example:
double[][] a = {{1.234, 4.5, 6.78},{1.2, 3.45, 6.789}};
System.out.println(tostring(a));
result is:
1.234 4.5 6.78
1.2 3.45 6.789- Parameters:
v
- Matrix or array- Returns:
- A string of a nicely organized version of the matrix or array.
-
toString
public static java.lang.String toString(java.lang.String format, double[]... v)
Generates a string that holds a nicely organized version of a matrix or array. Uses format specifier, e.g. "%5.3f", "%11.1E", ... An extra space is automatically included. Note the lower case S. It's tostring() not toString(). Example:
double[][] a = random(2, 3);
System.out.println(tostring("%7.3f", a));
result is:
0.654 0.115 0.422
0.560 0.839 0.280
- Parameters:
format
- A standard format specifier for one valuev
- Matrix or array- Returns:
- A string of a nicely organized version of the matrix or array.
-
throwError
public static void throwError(java.lang.String msg)
Throws an error exception if an argument is invalid. Handy to make sure the right number of values is passed around.- Parameters:
msg
- Message to be printed if the error occurs.- Throws:
java.lang.IllegalArgumentException
-
checkColumnDimension
public static void checkColumnDimension(double[][] M, int n)
Checks to make sure each row of a matrix is of a specified dimension. Handy for making sure matrices are rectangular.- Parameters:
M
- Input matrixn
- Number of elements that should be in each row of M- Throws:
java.lang.IllegalArgumentException
-
isColumnDimension
public static boolean isColumnDimension(double[][] M, int n)
Same as checkColumnDimension() but just returns a boolean value rather than throwing an exception.- Parameters:
M
- Input matrixn
- Number of elements that should be in each row of M- Returns:
- true if each row of M has n values. false otherwise.
- See Also:
checkColumnDimension(double[][], int)
-
checkRowDimension
public static void checkRowDimension(double[][] M, int m)
Checks to make sure each column of a matrix is of a specified dimension. Handy for making sure matrices are rectangular.- Parameters:
M
- Input matrixm
- Number of elements that should be in each column of M.- Throws:
java.lang.IllegalArgumentException
-
isRowDimension
public static boolean isRowDimension(double[][] M, int m)
Same as checkRowDimension() but just returns a boolean value rather than throwing an exception.- Parameters:
M
- Input matrixm
- Number of elements that should be in each column of M- Returns:
- true if each column of M has n values. false otherwise.
- See Also:
checkRowDimension(double[][], int)
-
checkLength
public static void checkLength(double[] M, int n)
Checks to make sure an array is of a specified length.- Parameters:
M
- Input array.n
- Required number of elements in M- Throws:
java.lang.IllegalArgumentException
-
isLength
public static boolean isLength(double[] M, int n)
Same as checkLength but returns a boolean value rather than throwing an exception.- Parameters:
M
- Input array.n
- Required number of elements in M.- Returns:
- true if M.length==n. false otherwise.
-
f
public static double[][] f(double[][] M, Function f)
Apply a scalar function to every element of an array. Must import org.math.array.util.*; to get Function interface. The function named f in Function must be overridden. Be careful not to define any other functions named f in your code. Example:
double[] b = {{1,2,3,4},{5,6,7,8}};
Function inverse = new Function() { public double f(double x) { return 1/x; }};
double[] z = f(b, inverse);
Result is:
1.00 0.50 0.333 0.25
0.20 0.167 0.143 0.125
- Parameters:
M
- The input arrayf
- Function object- Returns:
- array of same size as M.
-
f
public static double[] f(double[] M, Function func)
Apply a scalar function to every element of an array. Must import org.math.array.util.*; to get Function interface. Example:
double[] b = {1,2,3,4};
Function inverse = new Function() { public double f(double x) { return 1/x; }};
double[] z = f(b, inverse);
Result is:
1.00 0.50 0.33 0.25- Parameters:
M
- The input arrayfunc
- Function object whose sole method f(double) has already been overridden.- Returns:
- array of same size as M.
-
findex
public static double[] findex(int m, IndexFunction f)
Provides a sequence of values to which some function has been applied. Sort of a mutant version of increment(). Example:
IndexFunction square = new IndexFunction()
{ public double fi(int x) { return x*x; }};
double[] z = findex(5, square);
Result is:
0 1 4 9 16
- Parameters:
m
- Number of terms in the output array. The values 0..m-1 will be fed to the functionf
- An IndexFunction object whose method fi(int) has already been overridden.- Returns:
- An array of m elements. fi(0), fi(1), ... fi(m-1)
-
sort
public static double[] sort(double[] values)
Sorts an array in ascending order.- Parameters:
values
- Input array- Returns:
- Sorted version of input array.
-
sort
public static double[][] sort(double[][] values, int column)
Sorts the rows of a matrix using a specified column as the key. Example:
double[][] a = {{3, 5, 2, 1, 2},
Fixed (I hope) 3/28/06.
{1, 3, 8, 9, 0},
{2, 4, 2, 5, 6},
{0, 1, 2, 3, 4},
{4, 2, 1, 5, 2}};
double[][] z = sort(a,1);
Result is:
0 1 2 3 4
4 2 1 5 2
1 3 8 9 0
2 4 2 5 6
3 5 2 1 2
- Parameters:
values
- Input matrixcolumn
- Index of column to be used as the sorting key.- Returns:
- Matrix whose rows have been shuffled around.
-
transpose
public static double[][] transpose(double[][] M)
Transposes an mxn matrix into an nxm matrix. Each row of the input matrix becomes a column in the output matrix.- Parameters:
M
- Input matrix.- Returns:
- Transposed version of M.
-
-
DMelt 3.0 © DataMelt by jWork.ORG