jcolibri.extensions.textual.IE.common.crn.matrix
Class FloatMatrix

java.lang.Object
  extended by jcolibri.extensions.textual.IE.common.crn.matrix.FloatMatrix
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Matrix

public class FloatMatrix
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable, Matrix

Jama = Java Matrix class.

The Java Matrix Class provides the fundamental operations of numerical linear algebra. Various constructors create Matrices from two dimensional arrays of double precision floating point numbers. Various "gets" and "sets" provide access to submatrices and matrix elements. Several methods implement basic matrix arithmetic, including matrix addition and multiplication, matrix norms, and element-by-element array operations. Methods for reading and printing matrices are also included. All the operations in this version of the Matrix Class involve real matrices. Complex matrices may be handled in a future version.

Five fundamental matrix decompositions, which consist of pairs or triples of matrices, permutation vectors, and the like, produce results in five decomposition classes. These decompositions are accessed by the Matrix class to compute solutions of simultaneous linear equations, determinants, inverses and other matrix functions. The five decompositions are:

Example of use:

Solve a linear system A x = b and compute the residual norm, ||b - A x||.

      double[][] vals = {{1.,2.,3},{4.,5.,6.},{7.,8.,10.}};
      Matrix A = new Matrix(vals);
      Matrix b = Matrix.random(3,1);
      Matrix x = A.solve(b);
      Matrix r = A.times(x).minus(b);
      double rnorm = r.normInf();

Version:
5 August 1998
Author:
The MathWorks, Inc. and the National Institute of Standards and Technology.
See Also:
Serialized Form

Constructor Summary
FloatMatrix(float[][] A)
          Construct a matrix from a 2-D array.
FloatMatrix(float[][] A, int m, int n)
          Construct a matrix quickly without checking arguments.
FloatMatrix(float[] vals, int m)
          Construct a matrix from a one-dimensional packed array
FloatMatrix(int m, int n)
          Construct an m-by-n matrix of zeros.
FloatMatrix(int m, int n, float s)
          Construct an m-by-n constant matrix.
 
Method Summary
 FloatMatrix arrayLeftDivide(FloatMatrix B)
          Element-by-element left division, C = A.
 FloatMatrix arrayLeftDivideEquals(FloatMatrix B)
          Element-by-element left division in place, A = A.
 FloatMatrix arrayRightDivide(FloatMatrix B)
          Element-by-element right division, C = A.
 FloatMatrix arrayRightDivideEquals(FloatMatrix B)
          Element-by-element right division in place, A = A.
 FloatMatrix arrayTimes(FloatMatrix B)
          Element-by-element multiplication, C = A.
 FloatMatrix arrayTimesEquals(FloatMatrix B)
          Element-by-element multiplication in place, A = A.
 java.lang.Object clone()
          Clone the Matrix object.
static FloatMatrix constructWithCopy(float[][] A)
          Construct a matrix from a copy of a 2-D array.
 FloatMatrix copy()
          Make a deep copy of a matrix
 float get(int i, int j)
          Get a single element.
 float[][] getArray()
          Access the internal two-dimensional array.
 float[][] getArrayCopy()
          Copy the internal two-dimensional array.
 int getColumnDimension()
          Get column dimension.
 float[] getColumnPackedCopy()
          Make a one-dimensional column packed copy of the internal array.
 int getColumns()
           
 Matrix getDeepTraspose()
           
 FloatMatrix getMatrix(int[] r, int[] c)
          Get a submatrix.
 FloatMatrix getMatrix(int[] r, int j0, int j1)
          Get a submatrix.
 FloatMatrix getMatrix(int i0, int i1, int[] c)
          Get a submatrix.
 FloatMatrix getMatrix(int i0, int i1, int j0, int j1)
          Get a submatrix.
 int getRowDimension()
          Get row dimension.
 float[] getRowPackedCopy()
          Make a one-dimensional row packed copy of the internal array.
 int getRows()
           
 Matrix getShallowTraspose()
           
 float getValue(int row, int column)
           
static FloatMatrix identity(int m, int n)
          Generate identity matrix
 FloatMatrix minus(FloatMatrix B)
          C = A - B
 FloatMatrix minusEquals(FloatMatrix B)
          A = A - B
 Matrix multiply(Matrix other)
           
 float multiply(Matrix other, int thisRow, int otherColumn)
           
 float norm1()
          One norm
 float normInf()
          Infinity norm
 FloatMatrix plus(FloatMatrix B)
          C = A + B
 FloatMatrix plusEquals(FloatMatrix B)
          A = A + B
 void print(int w, int d)
          Print the matrix to stdout.
 void print(java.text.NumberFormat format, int width)
          Print the matrix to stdout.
 void print(java.io.PrintWriter output, int w, int d)
          Print the matrix to the output stream.
 void print(java.io.PrintWriter output, java.text.NumberFormat format, int width)
          Print the matrix to the output stream.
static FloatMatrix random(int m, int n)
          Generate matrix with random elements
 void set(int i, int j, float s)
          Set a single element.
 void setMatrix(int[] r, int[] c, FloatMatrix X)
          Set a submatrix.
 void setMatrix(int[] r, int j0, int j1, FloatMatrix X)
          Set a submatrix.
 void setMatrix(int i0, int i1, int[] c, FloatMatrix X)
          Set a submatrix.
 void setMatrix(int i0, int i1, int j0, int j1, FloatMatrix X)
          Set a submatrix.
 void setValue(int row, int column, float value)
           
 FloatMatrix times(float s)
          Multiply a matrix by a scalar, C = s*A
 FloatMatrix times(FloatMatrix B)
          Linear algebraic matrix multiplication, A * B
 FloatMatrix timesEquals(float s)
          Multiply a matrix by a scalar in place, A = s*A
 float trace()
          FloatMatrix trace.
 FloatMatrix transpose()
          Matrix transpose.
 FloatMatrix uminus()
          Unary minus
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FloatMatrix

public FloatMatrix(int m,
                   int n)
Construct an m-by-n matrix of zeros.

Parameters:
m - Number of rows.
n - Number of colums.

FloatMatrix

public FloatMatrix(int m,
                   int n,
                   float s)
Construct an m-by-n constant matrix.

Parameters:
m - Number of rows.
n - Number of colums.
s - Fill the matrix with this scalar value.

FloatMatrix

public FloatMatrix(float[][] A)
Construct a matrix from a 2-D array.

Parameters:
A - Two-dimensional array of floats.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length
See Also:
constructWithCopy(float[][])

FloatMatrix

public FloatMatrix(float[][] A,
                   int m,
                   int n)
Construct a matrix quickly without checking arguments.

Parameters:
A - Two-dimensional array of floats.
m - Number of rows.
n - Number of colums.

FloatMatrix

public FloatMatrix(float[] vals,
                   int m)
Construct a matrix from a one-dimensional packed array

Parameters:
vals - One-dimensional array of floats, packed by columns (ala Fortran).
m - Number of rows.
Throws:
java.lang.IllegalArgumentException - Array length must be a multiple of m.
Method Detail

constructWithCopy

public static FloatMatrix constructWithCopy(float[][] A)
Construct a matrix from a copy of a 2-D array.

Parameters:
A - Two-dimensional array of floats.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length

copy

public FloatMatrix copy()
Make a deep copy of a matrix

Specified by:
copy in interface Matrix

clone

public java.lang.Object clone()
Clone the Matrix object.

Overrides:
clone in class java.lang.Object

getArray

public float[][] getArray()
Access the internal two-dimensional array.

Returns:
Pointer to the two-dimensional array of matrix elements.

getArrayCopy

public float[][] getArrayCopy()
Copy the internal two-dimensional array.

Returns:
Two-dimensional array copy of matrix elements.

getColumnPackedCopy

public float[] getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.

Returns:
Matrix elements packed in a one-dimensional array by columns.

getRowPackedCopy

public float[] getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.

Returns:
Matrix elements packed in a one-dimensional array by rows.

getRowDimension

public int getRowDimension()
Get row dimension.

Returns:
m, the number of rows.

getColumnDimension

public int getColumnDimension()
Get column dimension.

Returns:
n, the number of columns.

get

public float get(int i,
                 int j)
Get a single element.

Parameters:
i - Row index.
j - Column index.
Returns:
A(i,j)
Throws:
java.lang.ArrayIndexOutOfBoundsException

getMatrix

public FloatMatrix getMatrix(int i0,
                             int i1,
                             int j0,
                             int j1)
Get a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
Returns:
A(i0:i1,j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public FloatMatrix getMatrix(int[] r,
                             int[] c)
Get a submatrix.

Parameters:
r - Array of row indices.
c - Array of column indices.
Returns:
A(r(:),c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public FloatMatrix getMatrix(int i0,
                             int i1,
                             int[] c)
Get a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
Returns:
A(i0:i1,c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

getMatrix

public FloatMatrix getMatrix(int[] r,
                             int j0,
                             int j1)
Get a submatrix.

Parameters:
r - Array of row indices.
i0 - Initial column index
i1 - Final column index
Returns:
A(r(:),j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

set

public void set(int i,
                int j,
                float s)
Set a single element.

Parameters:
i - Row index.
j - Column index.
s - A(i,j).
Throws:
java.lang.ArrayIndexOutOfBoundsException

setMatrix

public void setMatrix(int i0,
                      int i1,
                      int j0,
                      int j1,
                      FloatMatrix X)
Set a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
j0 - Initial column index
j1 - Final column index
X - A(i0:i1,j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int[] r,
                      int[] c,
                      FloatMatrix X)
Set a submatrix.

Parameters:
r - Array of row indices.
c - Array of column indices.
X - A(r(:),c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int[] r,
                      int j0,
                      int j1,
                      FloatMatrix X)
Set a submatrix.

Parameters:
r - Array of row indices.
j0 - Initial column index
j1 - Final column index
X - A(r(:),j0:j1)
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

setMatrix

public void setMatrix(int i0,
                      int i1,
                      int[] c,
                      FloatMatrix X)
Set a submatrix.

Parameters:
i0 - Initial row index
i1 - Final row index
c - Array of column indices.
X - A(i0:i1,c(:))
Throws:
java.lang.ArrayIndexOutOfBoundsException - Submatrix indices

transpose

public FloatMatrix transpose()
Matrix transpose.

Returns:
A'

norm1

public float norm1()
One norm

Returns:
maximum column sum.

normInf

public float normInf()
Infinity norm

Returns:
maximum row sum.

uminus

public FloatMatrix uminus()
Unary minus

Returns:
-A

plus

public FloatMatrix plus(FloatMatrix B)
C = A + B

Parameters:
B - another matrix
Returns:
A + B

plusEquals

public FloatMatrix plusEquals(FloatMatrix B)
A = A + B

Parameters:
B - another matrix
Returns:
A + B

minus

public FloatMatrix minus(FloatMatrix B)
C = A - B

Parameters:
B - another matrix
Returns:
A - B

minusEquals

public FloatMatrix minusEquals(FloatMatrix B)
A = A - B

Parameters:
B - another matrix
Returns:
A - B

arrayTimes

public FloatMatrix arrayTimes(FloatMatrix B)
Element-by-element multiplication, C = A.*B

Parameters:
B - another matrix
Returns:
A.*B

arrayTimesEquals

public FloatMatrix arrayTimesEquals(FloatMatrix B)
Element-by-element multiplication in place, A = A.*B

Parameters:
B - another matrix
Returns:
A.*B

arrayRightDivide

public FloatMatrix arrayRightDivide(FloatMatrix B)
Element-by-element right division, C = A./B

Parameters:
B - another matrix
Returns:
A./B

arrayRightDivideEquals

public FloatMatrix arrayRightDivideEquals(FloatMatrix B)
Element-by-element right division in place, A = A./B

Parameters:
B - another matrix
Returns:
A./B

arrayLeftDivide

public FloatMatrix arrayLeftDivide(FloatMatrix B)
Element-by-element left division, C = A.\B

Parameters:
B - another matrix
Returns:
A.\B

arrayLeftDivideEquals

public FloatMatrix arrayLeftDivideEquals(FloatMatrix B)
Element-by-element left division in place, A = A.\B

Parameters:
B - another matrix
Returns:
A.\B

times

public FloatMatrix times(float s)
Multiply a matrix by a scalar, C = s*A

Parameters:
s - scalar
Returns:
s*A

timesEquals

public FloatMatrix timesEquals(float s)
Multiply a matrix by a scalar in place, A = s*A

Parameters:
s - scalar
Returns:
replace A by s*A

times

public FloatMatrix times(FloatMatrix B)
Linear algebraic matrix multiplication, A * B

Parameters:
B - another matrix
Returns:
FloatMatrix product, A * B
Throws:
java.lang.IllegalArgumentException - FloatMatrix inner dimensions must agree.

trace

public float trace()
FloatMatrix trace.

Returns:
sum of the diagonal elements.

random

public static FloatMatrix random(int m,
                                 int n)
Generate matrix with random elements

Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with uniformly distributed random elements.

identity

public static FloatMatrix identity(int m,
                                   int n)
Generate identity matrix

Parameters:
m - Number of rows.
n - Number of colums.
Returns:
An m-by-n matrix with ones on the diagonal and zeros elsewhere.

print

public void print(int w,
                  int d)
Print the matrix to stdout. Line the elements up in columns with a Fortran-like 'Fw.d' style format.

Parameters:
w - Column width.
d - Number of digits after the decimal.

print

public void print(java.io.PrintWriter output,
                  int w,
                  int d)
Print the matrix to the output stream. Line the elements up in columns with a Fortran-like 'Fw.d' style format.

Parameters:
output - Output stream.
w - Column width.
d - Number of digits after the decimal.

print

public void print(java.text.NumberFormat format,
                  int width)
Print the matrix to stdout. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Parameters:
format - A Formatting object for individual elements.
width - Field width for each column.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)

print

public void print(java.io.PrintWriter output,
                  java.text.NumberFormat format,
                  int width)
Print the matrix to the output stream. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.

Parameters:
output - the output stream.
format - A formatting object to format the matrix elements
width - Column width.
See Also:
DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)

getColumns

public int getColumns()
Specified by:
getColumns in interface Matrix

getDeepTraspose

public Matrix getDeepTraspose()
Specified by:
getDeepTraspose in interface Matrix

getRows

public int getRows()
Specified by:
getRows in interface Matrix

getShallowTraspose

public Matrix getShallowTraspose()
Specified by:
getShallowTraspose in interface Matrix

getValue

public float getValue(int row,
                      int column)
Specified by:
getValue in interface Matrix

multiply

public Matrix multiply(Matrix other)
Specified by:
multiply in interface Matrix

setValue

public void setValue(int row,
                     int column,
                     float value)
Specified by:
setValue in interface Matrix

multiply

public float multiply(Matrix other,
                      int thisRow,
                      int otherColumn)
Specified by:
multiply in interface Matrix