public class Matrix
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable
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();
Constructor and Description |
---|
Matrix(double[] vals)
Construct a one-dimensional matrix from a one-column array
|
Matrix(double[][] inA)
Construct a matrix from a 2-D array.
|
Matrix(double[][] inA,
int rows,
int cols)
Construct a matrix from a 2-D array.
|
Matrix(double[] vals,
int rows)
Construct a matrix from a one-dimensional packed array
|
Matrix(double s,
int rows,
int cols)
Construct an rows-by-cols constant matrix.
|
Matrix(int rows,
int cols)
Construct an rows-by-cols matrix of zeros.
|
Modifier and Type | Method and Description |
---|---|
Matrix |
arrayLeftDivide(Matrix B)
Element-by-element left division, C = A.\B
|
Matrix |
arrayLeftDivideEquals(Matrix B)
Element-by-element left division in place, A = A.\B
|
Matrix |
arrayRightDivide(Matrix B)
Element-by-element right division, C = A./B
|
Matrix |
arrayRightDivideEquals(Matrix B)
Element-by-element right division in place, A = A./B
|
Matrix |
arrayTimes(Matrix B)
Element-by-element multiplication, C = A.*B
|
Matrix |
arrayTimesEquals(Matrix B)
Element-by-element multiplication in place, A = A.*B
|
CholeskyDecomposition |
chol()
Cholesky Decomposition
|
java.lang.Object |
clone()
Clone the Matrix object.
|
double |
cond()
Matrix condition (2 norm)
|
static Matrix |
constructWithCopy(double[][] A)
Construct a matrix from a copy of a 2-D array.
|
Matrix |
copy()
Make a deep copy of a matrix
|
double |
det()
Matrix determinant
|
Matrix |
divide(double s)
Divide a matrix by a scalar, C = s*A
|
Matrix |
divideEquals(double s)
Divide a matrix by a scalar, C = s*A
|
EigenvalueDecomposition |
eig()
Eigenvalue Decomposition
|
double |
get(int i,
int j)
Get a single element.
|
double[][] |
getArray()
Access the internal two-dimensional array.
|
double[][] |
getArrayCopy()
Copy the internal two-dimensional array.
|
Matrix |
getColSums()
returns the sums for each row as a 1 dimensional Matrix
|
double[] |
getColumnArrayCopy(int column)
Make a one-dimensional column copy of the internal array.
|
double[] |
getColumnPackedCopy()
Make a one-dimensional column packed copy of the internal array.
|
Matrix |
getMatrix(int[] r,
int[] c)
Get a submatrix.
|
Matrix |
getMatrix(int[] r,
int j0,
int j1)
Get a submatrix.
|
Matrix |
getMatrix(int i0,
int i1,
int[] c)
Get a submatrix.
|
Matrix |
getMatrix(int i0,
int i1,
int j0,
int j1)
Get a submatrix.
|
int |
getNColumns()
Get column dimension.
|
int |
getNRows()
Get row dimension.
|
double[] |
getRowArrayCopy(int row)
Make a one-dimensional row copy of the internal array.
|
double[] |
getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.
|
Matrix |
getRowSums()
returns the sums for each row as a 1 dimensional Matrix
|
static Matrix |
identity(int rows,
int cols)
Generate identity matrix
|
Matrix |
inverse()
Matrix inverse or pseudoinverse
|
void |
list()
lists the table
|
void |
list(boolean rowTotals,
boolean colTotals,
boolean totTotals)
lists the table
|
LUDecomposition |
lu()
LU Decomposition
|
Matrix |
minus(Matrix B)
C = A - B
|
Matrix |
minusEquals(Matrix B)
A = A - B
|
double |
norm1()
One norm
|
double |
norm2()
Two norm
|
double |
normF()
Frobenius norm
|
double |
normInf()
Infinity norm
|
Matrix |
plus(Matrix B)
C = A + B
|
Matrix |
plusEquals(Matrix 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.
|
QRDecomposition |
qr()
QR Decomposition
|
static Matrix |
random(int rows,
int cols)
Generate matrix with random elements
|
int |
rank()
Matrix rank
|
static Matrix |
read(java.io.BufferedReader input)
Read a matrix from a stream.
|
void |
set(int i,
int j,
double s)
Set a single element.
|
void |
setMatrix(int[] r,
int[] c,
Matrix X)
Set a submatrix.
|
void |
setMatrix(int[] r,
int j0,
int j1,
Matrix X)
Set a submatrix.
|
void |
setMatrix(int i0,
int i1,
int[] c,
Matrix X)
Set a submatrix.
|
void |
setMatrix(int i0,
int i1,
int j0,
int j1,
Matrix X)
Set a submatrix.
|
void |
setNColumns(int theCols)
Get column dimension.
|
void |
setNRows(int theRows)
Set row dimension.
|
Matrix |
solve(Matrix B)
Solve A*X = B
|
Matrix |
solveTranspose(Matrix B)
Solve X*A = B, which is also A'*X' = B'
|
Matrix |
sqr()
calculate square for each cell
|
Matrix |
sqrEquals()
calculate square for each cell
|
Matrix |
sqrt()
calculate square root for each cell
|
Matrix |
sqrtEquals()
calculate square root for each cell
|
SingularValueDecomposition |
svd()
Singular Value Decomposition
|
Matrix |
times(double s)
Multiply a matrix by a scalar, C = s*A
|
Matrix |
times(Matrix B)
Linear algebraic matrix multiplication, A * B
|
Matrix |
timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A
|
java.lang.String |
toString()
returns the formatted object as a string
|
java.lang.String |
toString(boolean rowTotals,
boolean colTotals,
boolean totTotals)
returns the formatted object as a string
|
double |
trace()
Matrix trace.
|
Matrix |
transpose()
Matrix transpose.
|
Matrix |
uminus()
Unary minus
|
public Matrix(int rows, int cols)
rows
- Number of rows.cols
- Number of colums.public Matrix(double s, int rows, int cols)
s
- Fill the matrix with this scalar value.rows
- Number of rows.cols
- Number of colums.public Matrix(double[][] inA)
inA
- Two-dimensional array of doubles.java.lang.IllegalArgumentException
- All rows must have the same lengthconstructWithCopy(double[][])
public Matrix(double[][] inA, int rows, int cols)
inA
- Two-dimensional array of doubles.java.lang.IllegalArgumentException
- All rows must have the same lengthconstructWithCopy(double[][])
public Matrix(double[] vals)
vals
- one column of doublespublic Matrix(double[] vals, int rows)
vals
- One-dimensional array of doubles, packed by columns (ala Fortran).rows
- Number of rows.java.lang.IllegalArgumentException
- Array length must be a multiple of rows.public void setNRows(int theRows)
theRows
- sets the number of rowspublic void setNColumns(int theCols)
theCols
- sets the number of columnspublic int getNRows()
public int getNColumns()
public static Matrix constructWithCopy(double[][] A)
A
- Two-dimensional array of doubles.java.lang.IllegalArgumentException
- All rows must have the same lengthpublic Matrix copy()
public java.lang.Object clone()
clone
in class java.lang.Object
public double[][] getArray()
public double[][] getArrayCopy()
public double[] getColumnArrayCopy(int column)
public double[] getRowArrayCopy(int row)
public double[] getColumnPackedCopy()
public double[] getRowPackedCopy()
public Matrix getRowSums()
public Matrix getColSums()
public double get(int i, int j)
i
- Row index.j
- Column index.public Matrix getMatrix(int i0, int i1, int j0, int j1)
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column indexjava.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int[] r, int[] c)
r
- Array of row indices.c
- Array of column indices.java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int i0, int i1, int[] c)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int[] r, int j0, int j1) throws java.lang.Exception
r
- Array of row indices.j0
- Initial column indexj1
- Final column indexjava.lang.ArrayIndexOutOfBoundsException
- Submatrix indicesjava.lang.Exception
public void set(int i, int j, double s)
i
- Row index.j
- Column index.s
- A(i,j).public void setMatrix(int i0, int i1, int j0, int j1, Matrix X)
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column indexX
- A(i0:i1,j0:j1)java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void setMatrix(int[] r, int[] c, Matrix X)
r
- Array of row indices.c
- Array of column indices.X
- A(r(:),c(:))java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void setMatrix(int[] r, int j0, int j1, Matrix X)
r
- Array of row indices.j0
- Initial column indexj1
- Final column indexX
- A(r(:),j0:j1)java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void setMatrix(int i0, int i1, int[] c, Matrix X)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.X
- A(i0:i1,c(:))java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix transpose()
public double norm1()
public double norm2()
public double normInf()
public double normF()
public Matrix uminus()
public Matrix plus(Matrix B)
B
- another matrixpublic Matrix plusEquals(Matrix B)
B
- another matrixpublic Matrix minusEquals(Matrix B)
B
- another matrixpublic Matrix arrayTimes(Matrix B)
B
- another matrixpublic Matrix arrayTimesEquals(Matrix B)
B
- another matrixpublic Matrix arrayRightDivide(Matrix B)
B
- another matrixpublic Matrix arrayRightDivideEquals(Matrix B)
B
- another matrixpublic Matrix arrayLeftDivide(Matrix B)
B
- another matrixpublic Matrix arrayLeftDivideEquals(Matrix B)
B
- another matrixpublic Matrix divide(double s)
s
- scalarpublic Matrix divideEquals(double s)
s
- scalarpublic Matrix times(double s)
s
- scalarpublic Matrix timesEquals(double s)
s
- scalarpublic Matrix times(Matrix B)
B
- another matrixjava.lang.IllegalArgumentException
- Matrix inner dimensions must agree.public LUDecomposition lu()
LUDecomposition
public QRDecomposition qr()
QRDecomposition
public CholeskyDecomposition chol()
CholeskyDecomposition
public SingularValueDecomposition svd()
SingularValueDecomposition
public EigenvalueDecomposition eig()
EigenvalueDecomposition
public Matrix solve(Matrix B) throws java.lang.Exception
B
- right hand sidejava.lang.Exception
public Matrix solveTranspose(Matrix B) throws java.lang.Exception
B
- right hand sidejava.lang.Exception
public Matrix inverse() throws java.lang.Exception
java.lang.Exception
public double det()
public int rank()
public double cond()
public double trace()
public static Matrix random(int rows, int cols)
rows
- Number of rows.cols
- Number of colums.public static Matrix identity(int rows, int cols)
rows
- Number of rows.cols
- Number of colums.public void print(int w, int d)
w
- Column width.d
- Number of digits after the decimal.public void print(java.io.PrintWriter output, int w, int d)
output
- Output stream.w
- Column width.d
- Number of digits after the decimal.public void print(java.text.NumberFormat format, int width)
format
- A Formatting object for individual elements.width
- Field width for each column.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
public void print(java.io.PrintWriter output, java.text.NumberFormat format, int width)
output
- the output stream.format
- A formatting object to format the matrix elementswidth
- Column width.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
public static Matrix read(java.io.BufferedReader input) throws java.io.IOException
input
- the input stream.java.io.IOException
public Matrix sqrt()
public Matrix sqrtEquals()
public Matrix sqr()
public Matrix sqrEquals()
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(boolean rowTotals, boolean colTotals, boolean totTotals)
rowTotals
- include totals for the rowscolTotals
- include totals for the columnstotTotals
- include sum totalpublic void list()
public void list(boolean rowTotals, boolean colTotals, boolean totTotals)
RH, 2017