edu.mines.jtk.lapack
Class DMatrix

java.lang.Object
  extended by edu.mines.jtk.lapack.DMatrix

public class DMatrix
extends java.lang.Object

A double-precision matrix. Elements of an m-by-n matrix are stored contiguously in an array a[m*n], such that array element a[i+j*m] corresponds to the i'th row and the j'th column of the m-by-n matrix. For example, a 3-by-3 matrix is stored as:


   a[0] a[3] a[6]
   a[1] a[4] a[7]
   a[2] a[5] a[8]
 
This is the column-major order required by LAPACK.

Decompositions of a matrix A facilate the solutions of various problems. For example, the QR decomposition A = Q*R of a rectangular matrix A (where Q is orthogonal and R is upper right triangular) is useful in least-square approximations. Decompositions currently provided by this class include

Version:
2005.12.12
Author:
Dave Hale, Colorado School of Mines

Constructor Summary
DMatrix(DMatrix a)
          Constructs a copy of the specified matrix.
DMatrix(double[][] a)
          Constructs a matrix from the specified array.
DMatrix(int m, int n)
          Constructs an m-by-n matrix of zeros.
DMatrix(int m, int n, double v)
          Constructs an m-by-n matrix filled with the specified value.
DMatrix(int m, int n, double[] a)
          Constructs a matrix from the specified array.
 
Method Summary
 DMatrix arrayLeftDivide(DMatrix b)
          Returns C = A .\ B, where A is this matrix.
 DMatrix arrayLeftDivideEquals(DMatrix b)
          Returns A = A .\ B, where A is this matrix.
 DMatrix arrayRightDivide(DMatrix b)
          Returns C = A ./ B, where A is this matrix.
 DMatrix arrayRightDivideEquals(DMatrix b)
          Returns A = A ./ B, where A is this matrix.
 DMatrix arrayTimes(DMatrix b)
          Returns C = A .* B, where A is this matrix.
 DMatrix arrayTimesEquals(DMatrix b)
          Returns A = A .* B, where A is this matrix.
 DMatrixChd chd()
          Returns the Cholesky decomposition of this matrix.
 double cond()
          Returns the condition number of this matrix.
 double det()
          Returns the determinant of this matrix.
static DMatrix diagonal(double[] d)
          Returns a new diagonal matrix with specified elements.
 boolean equals(java.lang.Object obj)
           
 DMatrixEvd evd()
          Returns the eigenvalue and eigenvector decomposition of this matrix.
 double[][] get()
          Gets all elements of this matrix into a new array.
 void get(double[][] a)
          Gets all elements of this matrix into the specified array.
 DMatrix get(int[] r, int j)
          Gets a matrix from specified rows and one column of this matrix.
 DMatrix get(int[] r, int[] c)
          Gets a new matrix from the specified rows and columns of this matrix.
 DMatrix get(int[] r, int j0, int j1)
          Gets a matrix from specified rows and columns of this matrix.
 double get(int i, int j)
          Gets a matrix element.
 DMatrix get(int i, int[] c)
          Gets a matrix from specified one row and columns of this matrix.
 DMatrix get(int i0, int i1, int[] c)
          Gets a matrix from specified rows and columns of this matrix.
 DMatrix get(int i0, int i1, int j0, int j1)
          Gets the specified submatrix a[i0:i1][j0:j1] of this matrix.
 double[] getArray()
          Gets the array in which matrix elements are stored.
 int getColumnCount()
          Gets the number of columns in this matrix.
 int getM()
          Gets the number of rows in this matrix.
 int getN()
          Gets the number of columns in this matrix.
 double[] getPackedColumns()
          Gets the elements of this matrix packed by columns.
 double[] getPackedRows()
          Gets the elements of this matrix packed by rows.
 int getRowCount()
          Gets the number of rows in this matrix.
 int hashCode()
           
static DMatrix identity(int n)
          Returns a new square identity matrix.
static DMatrix identity(int m, int n)
          Returns a new identity matrix.
 DMatrix inverse()
          Returns the inverse of this matrix.
 boolean isSquare()
          Determines whether this matrix is square.
 boolean isSymmetric()
          Determines whether this matrix is symmetric (and square).
 DMatrixLud lud()
          Returns the LU decomposition of this matrix.
 DMatrix minus(DMatrix b)
          Returns C = A - B, where A is this matrix.
 DMatrix minusEquals(DMatrix b)
          Returns A = A - B, where A is this matrix.
 DMatrix negate()
          Returns C = -A, where A is this matrix.
 double norm1()
          Returns the one-norm (maximum column sum) of this matrix.
 double norm2()
          Returns the two-norm (maximum singular value) of this matrix.
 double normF()
          Returns the Frobenius norm (sqrt of sum of squares) of this matrix.
 double normI()
          Returns the infinity-norm (maximum row sum) of this matrix.
 DMatrix plus(DMatrix b)
          Returns C = A + B, where A is this matrix.
 DMatrix plusEquals(DMatrix b)
          Returns A = A + B, where A is this matrix.
 DMatrixQrd qrd()
          Returns the QR decomposition of this matrix.
static DMatrix random(int n)
          Returns a new square matrix with random elements.
static DMatrix random(int m, int n)
          Returns a new matrix with random elements.
 double rank()
          Returns the effective numerical rank of this matrix.
 void set(double[][] a)
          Sets all elements of this matrix from the specified array.
 void set(int[] r, int[] c, DMatrix x)
          Sets the specified rows and columns of this matrix.
 void set(int[] r, int j, DMatrix x)
          Sets the specified rows and one column of this matrix.
 void set(int[] r, int j0, int j1, DMatrix x)
          Sets the specified rows and columns of this matrix.
 void set(int i, int[] c, DMatrix x)
          Sets the specified one row and columns of this matrix.
 void set(int i, int j, double v)
          Sets a matrix element.
 void set(int i0, int i1, int[] c, DMatrix x)
          Sets the specified rows and columns of this matrix.
 void set(int i0, int i1, int j0, int j1, DMatrix x)
          Sets the specified submatrix a[i0:i1][j0:j1] of this matrix.
 void setPackedColumns(double[] c)
          Sets the elements of this matrix from an array packed by columns.
 void setPackedRows(double[] r)
          Sets the elements of this matrix from an array packed by rows.
 DMatrix solve(DMatrix b)
          Solves the system A*X = B.
 DMatrixSvd svd()
          Returns the singular value decomposition of this matrix.
 DMatrix times(DMatrix b)
          Returns C = A * B, where A is this matrix.
 DMatrix times(double s)
          Returns C = A * s, where A is this matrix, and s is a scalar.
 DMatrix timesEquals(double s)
          Returns A = A * s, where A is this matrix, and s is a scalar.
 DMatrix timesTranspose(DMatrix b)
          Returns C = A * B', where A is this matrix and B' is B transposed.
 java.lang.String toString()
           
 double trace()
          Returns the trace (sum of diagonal elements) of this matrix.
 DMatrix transpose()
          Returns the transpose of this matrix.
 DMatrix transposeTimes(DMatrix b)
          Returns C = A' * B, where A' is this matrix transposed.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DMatrix

public DMatrix(int m,
               int n)
Constructs an m-by-n matrix of zeros.

Parameters:
m - the number of rows.
n - the number of columns.

DMatrix

public DMatrix(int m,
               int n,
               double v)
Constructs an m-by-n matrix filled with the specified value.

Parameters:
m - the number of rows.
n - the number of columns.
v - the value.

DMatrix

public DMatrix(int m,
               int n,
               double[] a)
Constructs a matrix from the specified array. Does not copy array elements into a new array. Rather, this matrix simply references the specified array.

That array contains packed columns of this matrix. In other words, array element a[i+j*m] corresponds to the i'th row and j'th column of this matrix.

Parameters:
m - the number of rows.
n - the number of columns.
a - the array.

DMatrix

public DMatrix(double[][] a)
Constructs a matrix from the specified array. Copies array elements a[i][j] to the i'th row and j'th column of this matrix. In other words, the specified array contains matrix elements ordered by rows.

The specified array must be regular. That is, each row much contain the same number of columns, and each column must contain the same number of rows.

Parameters:
a - the array.

DMatrix

public DMatrix(DMatrix a)
Constructs a copy of the specified matrix.

Parameters:
a - the matrix.
Method Detail

getM

public int getM()
Gets the number of rows in this matrix.

Returns:
the number of rows.

getRowCount

public int getRowCount()
Gets the number of rows in this matrix.

Returns:
the number of rows.

getN

public int getN()
Gets the number of columns in this matrix.

Returns:
the number of columns.

getColumnCount

public int getColumnCount()
Gets the number of columns in this matrix.

Returns:
the number of columns.

getArray

public double[] getArray()
Gets the array in which matrix elements are stored.

Returns:
the array; by reference, not by copy.

isSquare

public boolean isSquare()
Determines whether this matrix is square.

Returns:
true, if square; false, otherwise.

isSymmetric

public boolean isSymmetric()
Determines whether this matrix is symmetric (and square).

Returns:
true, if symmetric (and square); false, otherwise.

get

public double[][] get()
Gets all elements of this matrix into a new array.

Returns:
the array.

get

public void get(double[][] a)
Gets all elements of this matrix into the specified array.

Parameters:
a - the array.

get

public double get(int i,
                  int j)
Gets a matrix element.

Parameters:
i - the row index.
j - the column index.
Returns:
the element.

get

public DMatrix get(int i0,
                   int i1,
                   int j0,
                   int j1)
Gets the specified submatrix a[i0:i1][j0:j1] of this matrix.

Parameters:
i0 - the index of first row.
i1 - the index of last row.
j0 - the index of first column.
j1 - the index of last column.

get

public DMatrix get(int[] r,
                   int[] c)
Gets a new matrix from the specified rows and columns of this matrix.

Parameters:
r - the array of row indices; null, for all rows.
c - the array of column indices; null, for all columns.

get

public DMatrix get(int i,
                   int[] c)
Gets a matrix from specified one row and columns of this matrix.

Parameters:
i - the row index.
c - the array of column indices; null, for all columns.

get

public DMatrix get(int[] r,
                   int j)
Gets a matrix from specified rows and one column of this matrix.

Parameters:
r - the array of row indices; null, for all rows.
j - the column index.

get

public DMatrix get(int i0,
                   int i1,
                   int[] c)
Gets a matrix from specified rows and columns of this matrix.

Parameters:
i0 - the index of the first row.
i1 - the index of the last row.
c - the array of column indices; null, for all columns.

get

public DMatrix get(int[] r,
                   int j0,
                   int j1)
Gets a matrix from specified rows and columns of this matrix.

Parameters:
r - the array of row indices; null, for all rows.
j0 - the index of the first column.
j1 - the index of the last column.

getPackedColumns

public double[] getPackedColumns()
Gets the elements of this matrix packed by columns.

Returns:
the array of matrix elements packed by columns.

getPackedRows

public double[] getPackedRows()
Gets the elements of this matrix packed by rows.

Returns:
the array of matrix elements packed by rows.

set

public void set(double[][] a)
Sets all elements of this matrix from the specified array. Copies each array element into this matrix.

Parameters:
a - the array.

set

public void set(int i,
                int j,
                double v)
Sets a matrix element.

Parameters:
i - the row index.
j - the column index.
v - the element value.

set

public void set(int i0,
                int i1,
                int j0,
                int j1,
                DMatrix x)
Sets the specified submatrix a[i0:i1][j0:j1] of this matrix.

Parameters:
i0 - the index of first row.
i1 - the index of last row.
j0 - the index of first column.
j1 - the index of last column.
x - the matrix from which to copy elements.

set

public void set(int[] r,
                int[] c,
                DMatrix x)
Sets the specified rows and columns of this matrix.

Parameters:
r - the array of row indices; null, for all rows.
c - the array of column indices; null, for all columns.
x - the matrix from which to copy elements.

set

public void set(int i,
                int[] c,
                DMatrix x)
Sets the specified one row and columns of this matrix.

Parameters:
i - the row index.
c - the array of column indices; null, for all columns.
x - the matrix from which to copy elements.

set

public void set(int[] r,
                int j,
                DMatrix x)
Sets the specified rows and one column of this matrix.

Parameters:
r - the array of row indices; null, for all rows.
j - the column index.
x - the matrix from which to copy elements.

set

public void set(int i0,
                int i1,
                int[] c,
                DMatrix x)
Sets the specified rows and columns of this matrix.

Parameters:
i0 - the index of the first row.
i1 - the index of the last row.
c - the array of column indices; null, for all columns.
x - the matrix from which to copy elements.

set

public void set(int[] r,
                int j0,
                int j1,
                DMatrix x)
Sets the specified rows and columns of this matrix.

Parameters:
r - the array of row indices; null, for all rows.
j0 - the index of the first column.
j1 - the index of the last column.
x - the matrix from which to copy elements.

setPackedColumns

public void setPackedColumns(double[] c)
Sets the elements of this matrix from an array packed by columns.

Parameters:
c - the array of matrix elements packed by columns.

setPackedRows

public void setPackedRows(double[] r)
Sets the elements of this matrix from an array packed by rows.

Parameters:
r - the array of matrix elements packed by rows.

transpose

public DMatrix transpose()
Returns the transpose of this matrix.

Returns:
the transpose.

norm1

public double norm1()
Returns the one-norm (maximum column sum) of this matrix.

Returns:
the one-norm.

norm2

public double norm2()
Returns the two-norm (maximum singular value) of this matrix.

Returns:
the two-norm.
See Also:
DMatrixSvd

normI

public double normI()
Returns the infinity-norm (maximum row sum) of this matrix.

Returns:
the infinity-norm.

normF

public double normF()
Returns the Frobenius norm (sqrt of sum of squares) of this matrix.

Returns:
the Frobenius norm.

det

public double det()
Returns the determinant of this matrix.

Returns:
the determinant.

cond

public double cond()
Returns the condition number of this matrix.

Returns:
the condition number.
See Also:
DMatrixSvd

rank

public double rank()
Returns the effective numerical rank of this matrix.

Returns:
the rank.
See Also:
DMatrixSvd

trace

public double trace()
Returns the trace (sum of diagonal elements) of this matrix.

Returns:
the trace.

lud

public DMatrixLud lud()
Returns the LU decomposition of this matrix.

Returns:
the LU decomposition.

qrd

public DMatrixQrd qrd()
Returns the QR decomposition of this matrix.

Returns:
the QR decomposition.

chd

public DMatrixChd chd()
Returns the Cholesky decomposition of this matrix.

Returns:
the Cholesky decomposition.

evd

public DMatrixEvd evd()
Returns the eigenvalue and eigenvector decomposition of this matrix.

Returns:
the eigenvalue and eigenvector decomposition.

svd

public DMatrixSvd svd()
Returns the singular value decomposition of this matrix.

Returns:
the singular value decomposition.

solve

public DMatrix solve(DMatrix b)
Solves the system A*X = B. Requires m>=n for this m-by-n matrix A. If m>n, then the returned solution X is a least-squares solution.

Parameters:
b - the matrix B of right-hand-side column vectors.
Returns:
the matrix X of solution vectors.

inverse

public DMatrix inverse()
Returns the inverse of this matrix. If m>n for this m-by-n matrix A, then returns the pseudo-inverse.


negate

public DMatrix negate()
Returns C = -A, where A is this matrix.

Returns:
C = -A.

plus

public DMatrix plus(DMatrix b)
Returns C = A + B, where A is this matrix.

Parameters:
b - the matrix B.
Returns:
C = A + B.

plusEquals

public DMatrix plusEquals(DMatrix b)
Returns A = A + B, where A is this matrix.

Parameters:
b - the matrix B.
Returns:
A = A + B.

minus

public DMatrix minus(DMatrix b)
Returns C = A - B, where A is this matrix.

Parameters:
b - the matrix B.
Returns:
C = A - B.

minusEquals

public DMatrix minusEquals(DMatrix b)
Returns A = A - B, where A is this matrix.

Parameters:
b - the matrix B.
Returns:
A = A - B.

arrayTimes

public DMatrix arrayTimes(DMatrix b)
Returns C = A .* B, where A is this matrix. The symbol .* denotes element-by-element multiplication.

Parameters:
b - the matrix B.
Returns:
C = A .* B.

arrayTimesEquals

public DMatrix arrayTimesEquals(DMatrix b)
Returns A = A .* B, where A is this matrix. The symbol .* denotes element-by-element multiplication.

Parameters:
b - the matrix B.
Returns:
A = A .* B.

arrayRightDivide

public DMatrix arrayRightDivide(DMatrix b)
Returns C = A ./ B, where A is this matrix. The symbol ./ denotes element-by-element right division.

Parameters:
b - the matrix B.
Returns:
C = A ./ B.

arrayRightDivideEquals

public DMatrix arrayRightDivideEquals(DMatrix b)
Returns A = A ./ B, where A is this matrix. The symbol ./ denotes element-by-element right division.

Parameters:
b - the matrix B.
Returns:
A = A ./ B.

arrayLeftDivide

public DMatrix arrayLeftDivide(DMatrix b)
Returns C = A .\ B, where A is this matrix. The symbol .\ denotes element-by-element left division.

Parameters:
b - the matrix B.
Returns:
C = A .\ B.

arrayLeftDivideEquals

public DMatrix arrayLeftDivideEquals(DMatrix b)
Returns A = A .\ B, where A is this matrix. The symbol .\ denotes element-by-element left division.

Parameters:
b - the matrix B.
Returns:
A = A .\ B.

times

public DMatrix times(double s)
Returns C = A * s, where A is this matrix, and s is a scalar.

Parameters:
s - the scalar s.
Returns:
C = A * s.

timesEquals

public DMatrix timesEquals(double s)
Returns A = A * s, where A is this matrix, and s is a scalar.

Parameters:
s - the scalar s.
Returns:
A = A * s.

times

public DMatrix times(DMatrix b)
Returns C = A * B, where A is this matrix. The number of columns in this matrix A must equal the number of rows in the specified matrix B.

Parameters:
b - the matrix B.
Returns:
C = A * B.

timesTranspose

public DMatrix timesTranspose(DMatrix b)
Returns C = A * B', where A is this matrix and B' is B transposed. The number of columns in this matrix A must equal the number of columns in the specified matrix B.

Parameters:
b - the matrix B.
Returns:
C = A * B'.

transposeTimes

public DMatrix transposeTimes(DMatrix b)
Returns C = A' * B, where A' is this matrix transposed. The number of rows in this matrix A must equal the number of rows in the specified matrix B.

Parameters:
b - the matrix B.
Returns:
C = A' * B.

random

public static DMatrix random(int m,
                             int n)
Returns a new matrix with random elements. The distribution of the random numbers is uniform in the interval [0,1).

Parameters:
m - the number of rows.
n - the number of columns.
Returns:
the random matrix.

random

public static DMatrix random(int n)
Returns a new square matrix with random elements. The distribution of the random numbers is uniform in the interval [0,1).

Parameters:
n - the number of rows and columns.
Returns:
the random matrix.

identity

public static DMatrix identity(int m,
                               int n)
Returns a new identity matrix.

Parameters:
m - the number of rows.
n - the number of columns.
Returns:
the identity matrix.

identity

public static DMatrix identity(int n)
Returns a new square identity matrix.

Parameters:
n - the number of rows and columns.
Returns:
the identity matrix.

diagonal

public static DMatrix diagonal(double[] d)
Returns a new diagonal matrix with specified elements.

Parameters:
d - array of diagonal elements d[k] = A(k,k).

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object