bijnum
Class BIJmatrix

java.lang.Object
  |
  +--bijnum.BIJmatrix

public class BIJmatrix
extends java.lang.Object

Implements methods for large (by necessity, float) matrices, including covariance of matrix, eigenvectors and others. Syntax is kept similar to Matlab, including order of operations. The basic difference with BIJMatrix is that here the matrices are all float, the syntax is the smae as Matlab and it is active. I do not want to touch BIJMatrix anymore. Copyright (c) 1999-2003, Michael Abramoff. All rights reserved.


Constructor Summary
BIJmatrix()
           
 
Method Summary
static float[] abs(float[] v)
          Return a vector with the absolutes of all elements in vector v.
static void add(float[][] m, double scalar)
          Add a scalar to all entries in a matrix m in place.
static void add(float[][] a, float[][] b)
          Add (elements in) a matrix b to a matrix a.
static void add(float[][] r, float[][] a, float[][] b)
          Add the elements of two matrices a and b and save the results in a matrix r.
static float[] add(float[] v, float scalar, float[] mask)
           
static void addElements(double[] r, double[] a, double[] b)
           
static float[] addElements(float[] a, float[] b)
           
static void addElements(float[] r, float[] a, float[] b)
           
static boolean checkinverse(double[][] J, double[][] JI)
          Check whether JI is really the inverse of J.
static float[] col(float[][] m, int n)
          Return the n'th column of matrix m as a vector.
static float[] concat(float[] a, float[] b)
          Concatenate a vector to another vector in the same order.
static boolean containsNaN(float[] v)
           
static boolean containsNaN(float[][] m)
           
static float[] copy(float[] v)
          Copy a vector.
static float[][] copy(float[][] m)
          Copy a matrix.
static float determinant(float[][] m)
          Compute determinant of m.
static float[][] diag(float[] v)
          Get a square matrix with v on the diagonal.
static float[] diag(float[][] m)
          Get the diagonal vector of (square) matrix m.
static float[] div(float[] v, double divisor)
          Divide each entry in v by divisor.
static void divElements(float[][] m, double divisor)
          Divide each entry in m by divisor.
static void divElements(float[] r, float[] a, float[] b)
          Divide all elements in vector a by elements in b.
static float[] flatten(float[][] m)
          Flatten a float[][] matrix into a float[] matrix of rows each of m[0].width.
static int GaussElim(float[] a, float[] x)
          Gaussian elimination method with backward substitution to solve a system of linear equations.
static float[][] inverse(float[][] m)
          Invert a symmetric matrix m.
static void ln(float[] r, float[] v)
          Compute the natural logarithmr of each element in vector v and put into r.
static float[] mask(float[] v, float[] mask)
          Return a vector containing only the elements in v that have a mask value != 0.
static float[] minmax(float[] v)
           
static float[] minmax(float[][] m)
           
static float[] minmaxNot0(float[] v)
           
static float[] minmaxNot0(float[][] m)
           
static float[] mul(float[][] a, float[] v)
          Compute the outer product of a matrix and a vector v
static float[][] mul(float[][] a, float[][] b)
          Compute the matrix product of m and n.
static float[][] mul(float[][] a, float[][] b, boolean doShowProgress)
          Compute the matrix product of m and n.
static void mul(float[][] r, float[][] m, float scalar)
          Multiply each element in matrix m by a scalar and put result in r.
static void mul(float[][] m, float[][] a, float[][] b, boolean doShowProgress)
          Compute the matrix product of m and n into r.
static float[] mul(float[] v, float scalar)
          Deprecated.  
static void mul(float[] r, float[][] a, float[] v)
          Compute the outer product of a matrix and a vector v and put the result in r.
static void mulElements(double[] r, double[] v, double scalar)
          Multiply each element in v by a scalar and put in r.
static void mulElements(double[] r, double[] a, double[] b)
          Multiply all elements in vector a by elements in b and put in r.
static float[] mulElements(float[] a, float[] b)
          Multiply all elements in vector a by elements in b.
static void mulElements(float[] r, float[] v, double scalar)
          Multiply each element in v by a scalar and put in r.
static void mulElements(float[] r, float[] a, float[] b)
          Multiply all elements in vector a by elements in b and put in r.
static float[][] mulOuter(float[] a, float[] b)
          Compute outer product of vector a and b.
static float[][] mulT(float[][] a, float[][] b, boolean doShowProgress)
          Compute the transpose of the outer product of the tranpose of a with b.
static float norm(float[] v)
          Return length or norm of vector v.
static void pow(double[] r, double[] v, double power)
          Compute the power'th power of each element in vector v and put into r.
static float[][] pow(float[][] m, float power)
          Compute the power'th power of each entry in the matrix m.
static void pow(float[] r, float[] v, double power)
          Compute the power'th power of each element in vector v and put into r.
static double pseudoinverse(double[][] JI, double[][] J, double singularLimit)
          Deprecated.  
static float pseudoinverse(float[][] JI, float[][] J, double singularLimit)
          Deprecated.  
static float[][] repmat(float[][] a, int n, int m)
          repmat replicates a matrix in tiled form.
static float[][] repmat(float[] a, int n, int m)
          repmat replicates a vector in tiled form.
static int[] sort(float[] v)
          Sort a vector, but return the result as a vector of indices, conserving the original vector.
static void sort(float[] indicator, float[] v, float[][] m)
          Sort a vector and a matrix simultaneously based on the order of items in a third vector.
static void sub(float[][] r, float[][] m, float[] v)
          Subtract the elements of a vector v from each element of a matrix m, columnwise.
static void sub(float[][] r, float[][] m, float[][] n)
          Subtract a matrix n from a matrix m.
static float[] sub(float[] a, float[] b)
           
static void sub(float[] r, float[] a, double scalar)
           
static void subElements(float[] r, float[] a, float[] b)
           
static float[][] subset(float[][] m, int[] indices)
          Make a subset of m, include only those row vectors with an index which is in indices.
static float[][] subset(float[][] m, int start, int last)
          Make a subset of m, including only those with an index which is equal or larger than start up to and including last.
static float[] subset(float[] v, int start)
          Make a subset of v, including only those with an index which is equal or larger than start.
static float[] subset(float[] v, int[] indices)
          Make a subset of v, including only those with an index which is in indices.
static float[] subset(float[] v, int start, int last)
          Make a subset of v, including only those with an index which is equal or larger than start up to and including last.
static float[][] subsetColumns(float[][] m, int[] indices)
          Make a subset of m, including only those columns of the row vectors with an index which is in indices.
static void test()
           
static byte[][] transpose(byte[][] m)
          Compute the transpose of a matrix m.
static float[][] transpose(float[][] m)
          Compute the transpose of a matrix m.
static float[][] trunc(float[][] m, int n)
          Truncate a matrix to a new column length n.
static float[] trunc(float[] v, int n)
          Truncate a vector to a new length n.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BIJmatrix

public BIJmatrix()
Method Detail

mul

public static float[][] mul(float[][] a,
                            float[][] b)
Compute the matrix product of m and n.
Parameters:
a - a matrix of float[N][M]
b - a matrix of float[M][N]
Returns:
a new matrix of float[N][M]

mul

public static float[][] mul(float[][] a,
                            float[][] b,
                            boolean doShowProgress)
Compute the matrix product of m and n. Show progress.
Parameters:
a - a matrix of float[N][M]
b - a matrix of float[M][N]
Returns:
a matrix of float[N][N]

mul

public static void mul(float[][] m,
                       float[][] a,
                       float[][] b,
                       boolean doShowProgress)
Compute the matrix product of m and n into r. Show progress.
Parameters:
r - a matrix of float[N][N]
a - a matrix of float[N][M]
b - a matrix of float[M][N]
Returns:
a matrix of float[N][N]

mul

public static float[] mul(float[][] a,
                          float[] v)
                   throws java.lang.IllegalArgumentException
Compute the outer product of a matrix and a vector v
Parameters:
a - a float[][] matrix
v - a float[] vector.
Returns:
a float[] vector of the same size as v. Preferably use mul(3) instead.

mulElements

public static void mulElements(double[] r,
                               double[] a,
                               double[] b)
Multiply all elements in vector a by elements in b and put in r.
Parameters:
r - a double[] vector
a - a double[] vector
b - a double[] vector

mulElements

public static void mulElements(float[] r,
                               float[] a,
                               float[] b)
Multiply all elements in vector a by elements in b and put in r.
Parameters:
r - a float[] vector
a - a float[] vector
b - a float[] vector

mulElements

public static float[] mulElements(float[] a,
                                  float[] b)
Multiply all elements in vector a by elements in b.
Parameters:
a - a float[] vector
b - a float[] vector
Returns:
a float[] vector

divElements

public static void divElements(float[] r,
                               float[] a,
                               float[] b)
Divide all elements in vector a by elements in b.
Parameters:
r - a float[] vector for the result.
a - a float[] vector
b - a float[] vector

mulOuter

public static float[][] mulOuter(float[] a,
                                 float[] b)
Compute outer product of vector a and b.
Parameters:
a - a float[] vector.
b - a float[] vector.
Returns:
a float[] vector

mulT

public static float[][] mulT(float[][] a,
                             float[][] b,
                             boolean doShowProgress)
Compute the transpose of the outer product of the tranpose of a with b. Is the same as transpose(mul(transpose(a), b)), but saves a lot of space!
Parameters:
a - a matrix of float[M][N]
b - a matrix of float[M][N]
Returns:
a matrix of float[M][M]

mul

public static float[] mul(float[] v,
                          float scalar)
Deprecated.  

Multiply each element in v by a scalar.
Parameters:
v - a float[] vector.
scalar - the value to multiply by.
Returns:
the resulting vector.

mulElements

public static void mulElements(float[] r,
                               float[] v,
                               double scalar)
Multiply each element in v by a scalar and put in r.
Parameters:
r - a float[] vector for the result
v - a float[] vector.
scalar - the value to multiply by.

mulElements

public static void mulElements(double[] r,
                               double[] v,
                               double scalar)
Multiply each element in v by a scalar and put in r.
Parameters:
r - a float[] vector for the result
v - a float[] vector.
scalar - the value to multiply by.

mul

public static void mul(float[][] r,
                       float[][] m,
                       float scalar)
Multiply each element in matrix m by a scalar and put result in r.
Parameters:
r - a float[][] matrix that will receive the result.
m - a float[][] matrix.
scalar - the value to multiply by

mul

public static void mul(float[] r,
                       float[][] a,
                       float[] v)
                throws java.lang.IllegalArgumentException
Compute the outer product of a matrix and a vector v and put the result in r.
Parameters:
a - a float[][] matrix
v - a float[] vector.
Returns:
a float[] vector of the same size as v.

flatten

public static float[] flatten(float[][] m)
Flatten a float[][] matrix into a float[] matrix of rows each of m[0].width.
Parameters:
m - the matrix
Returns:
the flattened matrix, a float[m.length*m[0].length].

containsNaN

public static boolean containsNaN(float[] v)

containsNaN

public static boolean containsNaN(float[][] m)

transpose

public static float[][] transpose(float[][] m)
Compute the transpose of a matrix m.
Parameters:
m - a float[][] matrix.
Returns:
a float[][] which is the transpose of m.

transpose

public static byte[][] transpose(byte[][] m)
Compute the transpose of a matrix m.
Parameters:
m - a byte[][] matrix.
Returns:
a byte[][] which is the transpose of m.

minmax

public static float[] minmax(float[][] m)

minmax

public static float[] minmax(float[] v)

minmaxNot0

public static float[] minmaxNot0(float[] v)

minmaxNot0

public static float[] minmaxNot0(float[][] m)

abs

public static float[] abs(float[] v)
Return a vector with the absolutes of all elements in vector v. v is not modified.
Parameters:
a - float[] vector
Returns:
a new vector with the abs of v[].

mask

public static float[] mask(float[] v,
                           float[] mask)
Return a vector containing only the elements in v that have a mask value != 0.
Parameters:
v - a vector.
mask - a vector of the same size as v, containing non-zero values for all elements in v that are valid.
Returns:
a float[] vector containing only the masked elements of v (unordered).

add

public static float[] add(float[] v,
                          float scalar,
                          float[] mask)

addElements

public static void addElements(double[] r,
                               double[] a,
                               double[] b)

addElements

public static void addElements(float[] r,
                               float[] a,
                               float[] b)

add

public static void add(float[][] r,
                       float[][] a,
                       float[][] b)
Add the elements of two matrices a and b and save the results in a matrix r.
Parameters:
r - a float[][] matrix for the result.
a - a float[][] matrix.
b - a float[][] matrix.

addElements

public static float[] addElements(float[] a,
                                  float[] b)

pow

public static float[][] pow(float[][] m,
                            float power)
Compute the power'th power of each entry in the matrix m.
Parameters:
m - a float[][] matrix.
power - the exponent to subtract.
Returns:
the modified matrix.

norm

public static float norm(float[] v)
Return length or norm of vector v.
Parameters:
v - a float[] matrix.
Returns:
the norm of v.

ln

public static void ln(float[] r,
                      float[] v)
Compute the natural logarithmr of each element in vector v and put into r. If an element is < 0 and power is between -1 and 1, 0 is used to avoid taking the root of negative numbers.
Parameters:
r - a float[] vector for the result.
v - a float[] vector.

pow

public static void pow(double[] r,
                       double[] v,
                       double power)
Compute the power'th power of each element in vector v and put into r. If an element is < 0 and power is between -1 and 1, 0 is used to avoid taking the root of negative numbers.
Parameters:
r - a float[] vector for the result.
v - a float[] vector.
power - the power the exponent.

pow

public static void pow(float[] r,
                       float[] v,
                       double power)
Compute the power'th power of each element in vector v and put into r. If an element is < 0 and power is between -1 and 1, 0 is used to avoid taking the root of negative numbers.
Parameters:
r - a float[] vector for the result.
v - a float[] vector.
power - the power the exponent.

div

public static float[] div(float[] v,
                          double divisor)
Divide each entry in v by divisor.
Parameters:
v - a float[] vector.
divisor - the value to divide by.
Returns:
a copy of v.

divElements

public static void divElements(float[][] m,
                               double divisor)
Divide each entry in m by divisor.
Parameters:
m - a float[] matrix.
divisor - the value to divide by.

copy

public static float[] copy(float[] v)
Copy a vector.
Parameters:
v - a float[] vector.
Returns:
a copy of v.

copy

public static float[][] copy(float[][] m)
Copy a matrix.
Parameters:
m - a float[][] matrix.
Returns:
a copy of m.

add

public static void add(float[][] m,
                       double scalar)
Add a scalar to all entries in a matrix m in place. m is modified!
Parameters:
m - a float[][] matrix.
scalar - a scalar to subtract.

add

public static void add(float[][] a,
                       float[][] b)
                throws java.lang.IllegalArgumentException
Add (elements in) a matrix b to a matrix a. a is MODIFIED!
Parameters:
m - a float[][] matrix.
n - a float[][] matrix.

sub

public static void sub(float[][] r,
                       float[][] m,
                       float[][] n)
Subtract a matrix n from a matrix m.
Parameters:
r - a float[][] matrix for the result.
m - a float[][] matrix.
n - a float[][] matrix.
Returns:
the subtracted modified matrix.

sub

public static void sub(float[][] r,
                       float[][] m,
                       float[] v)
                throws java.lang.IllegalArgumentException
Subtract the elements of a vector v from each element of a matrix m, columnwise.
Parameters:
r - a float[][] matrix for the result.
m - a float[][] matrix.
v - a float[] vector.

sub

public static float[] sub(float[] a,
                          float[] b)
                   throws java.lang.IllegalArgumentException

subElements

public static void subElements(float[] r,
                               float[] a,
                               float[] b)

sub

public static void sub(float[] r,
                       float[] a,
                       double scalar)

diag

public static float[] diag(float[][] m)
Get the diagonal vector of (square) matrix m.
Parameters:
m - a float[][] matrix.
Returns:
a new float[] vector with the diagonal of m.

diag

public static float[][] diag(float[] v)
Get a square matrix with v on the diagonal.
Parameters:
v - a float[]
Returns:
m a float[][] matrix of the same length as v.

trunc

public static float[] trunc(float[] v,
                            int n)
Truncate a vector to a new length n.
Parameters:
v - a float[] vector.
Returns:
a vector with the first n entries of v.

trunc

public static float[][] trunc(float[][] m,
                              int n)
Truncate a matrix to a new column length n.
Parameters:
m - a float[][] matrix.
Returns:
a matrix with the first n columns of m.

subset

public static float[] subset(float[] v,
                             int[] indices)
Make a subset of v, including only those with an index which is in indices.
Parameters:
v - a float[] vector
indices - an int[] vector containing the valid indices for the subset
Returns:
a float[] vector of the same length as indices.

subset

public static float[] subset(float[] v,
                             int start)
Make a subset of v, including only those with an index which is equal or larger than start.
Parameters:
v - a float[] vector
start - the start index of the set
Returns:
a float[] vector

subset

public static float[] subset(float[] v,
                             int start,
                             int last)
Make a subset of v, including only those with an index which is equal or larger than start up to and including last.
Parameters:
v - a float[] vector
start - the start index of the set
Returns:
a float[] vector

subset

public static float[][] subset(float[][] m,
                               int start,
                               int last)
Make a subset of m, including only those with an index which is equal or larger than start up to and including last.
Parameters:
v - a float[] vector
start - the start index of the set
Returns:
a float[] vector

subset

public static float[][] subset(float[][] m,
                               int[] indices)
Make a subset of m, include only those row vectors with an index which is in indices.
Parameters:
m - a float[][] matrix
indices - an int[] vector containing the valid indices for the subset
Returns:
a float[][] matrix vector of the same length as indices.

subsetColumns

public static float[][] subsetColumns(float[][] m,
                                      int[] indices)
Make a subset of m, including only those columns of the row vectors with an index which is in indices.
Parameters:
m - a MxP float[][] matrix
indices - an 1xN int[] vector containing the valid indices for the subset
Returns:
a MxN float[][] matrix vector.

col

public static float[] col(float[][] m,
                          int n)
Return the n'th column of matrix m as a vector.
Parameters:
m - a float[][] matrix.
n - the column desired.
Returns:
a column vector with the n-th column.

concat

public static float[] concat(float[] a,
                             float[] b)
Concatenate a vector to another vector in the same order.
Parameters:
a - a float[] vector.
b - a float[] vector.
Returns:
a float[] vector of length a + b.

repmat

public static float[][] repmat(float[] a,
                               int n,
                               int m)
repmat replicates a vector in tiled form.

repmat

public static float[][] repmat(float[][] a,
                               int n,
                               int m)
repmat replicates a matrix in tiled form.

inverse

public static float[][] inverse(float[][] m)
                         throws java.lang.IllegalArgumentException
Invert a symmetric matrix m. Only works for 2x2, 3x3, 4x4 matrix now.
Parameters:
m - the matrix to be inverted.
Returns:
a float[][] with the inverse of m.
Throws:
java.lang.IllegalArgumentException -  

GaussElim

public static int GaussElim(float[] a,
                            float[] x)
Gaussian elimination method with backward substitution to solve a system of linear equations. E1: a1,1 x1 + a1,2 x2 + ... a1,n xn = a1,n+1 E2: a2,1 x1 + a2,2 x2 + ... a2,n xn = a2,n+1 . = . En: an,1 x1 + an,2 x2 + ... an,n xn = an,n+1
Parameters:
a - a vector containing the matrix of coefficients a1,1...an,n and also the y's which are called a1,n+1...an,n+1.
x - a vector containing
Returns:
if succesful, 0 if error.

determinant

public static float determinant(float[][] m)
Compute determinant of m. Only works for 2x2 or 3x3 matrix now.
Parameters:
m - a 2x2 or 3x3 double[][] matrix

test

public static void test()

sort

public static void sort(float[] indicator,
                        float[] v,
                        float[][] m)
Sort a vector and a matrix simultaneously based on the order of items in a third vector. Straight insertion method.

sort

public static int[] sort(float[] v)
Sort a vector, but return the result as a vector of indices, conserving the original vector.
Parameters:
v - a vector
Returns:
an int[] a vector of indices into v, sorted descending.

pseudoinverse

public static float pseudoinverse(float[][] JI,
                                  float[][] J,
                                  double singularLimit)
Deprecated.  

Compute the inverse of J[m][n], a float[][] matrix, using the pseudoinverse computed by SVD. J is conserved. If singularLimit != 0, the singular values smaller than singularLimit are replaced by singularLimit.
Returns:
the condition number of the W matrix.

pseudoinverse

public static double pseudoinverse(double[][] JI,
                                   double[][] J,
                                   double singularLimit)
Deprecated.  

LONG revoked by other routines. This is legacy code which should be replaced by better routines. Compute the inverse of J[m][n], a double[][] matrix, using the pseudoinverse computed by SVD. J is conserved. If singularLimit != 0, the singular values smaller than singularLimit are replaced by singularLimit.
Parameters:
JI - the inverse of J after the call.
J - the matrix for which the pseudoinverse will be computed.
singularLimit, - the threshold for computed values in the matrix below which they are treated as 0.
Returns:
the condition number of the W matrix.

checkinverse

public static boolean checkinverse(double[][] J,
                                   double[][] JI)
Check whether JI is really the inverse of J.