edu.mines.jtk.la
Class DMatrix

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

public class DMatrix
extends java.lang.Object

A double-precision matrix. Matrix elements are stored in an array of arrays of doubles a[m][n], such that array element a[i][j] corresponds to the i'th row and the j'th column of the m-by-n matrix.

This class was adapted from the package Jama, which was developed by Joe Hicklin, Cleve Moler, and Peter Webb of The MathWorks, Inc., and by Ronald Boisvert, Bruce Miller, Roldan Pozo, and Karin Remington of the National Institue of Standards and Technology.

Version:
2005.12.01
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.
 
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.
 boolean equals(java.lang.Object obj)
           
 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 m, int n)
          Returns a new identity matrix.
 boolean isSquare()
          Determines whether this matrix is square.
 boolean isSymmetric()
          Determines whether this matrix is symmetric (and square).
 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.
static DMatrix random(int m, int n)
          Returns a new matrix with random elements.
 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 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.
 java.lang.String toString()
           
 double trace()
          Returns the trace (sum of diagonal elements) of this matrix.
 DMatrix transpose()
          Returns the transpose of this matrix.
 
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(double[][] a)
Constructs a matrix from the specified array. Does not copy array elements into a new array. Rather, the new matrix simply references the specified array.

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.

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.

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.

trace

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

Returns:
the trace.

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.

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.

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