public class Matrix
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
private double[][] |
A
Array for internal storage of elements.
|
private int |
m
Row and column dimensions.
|
private int |
n
Row and column dimensions.
|
private static long |
serialVersionUID |
Constructor and Description |
---|
Matrix(double[][] A)
Construct a matrix from a 2-D array.
|
Matrix(double[][] A,
int m,
int n)
Construct a matrix quickly without checking arguments.
|
Matrix(double[] vals,
int m)
Construct a matrix from a one-dimensional packed array
|
Matrix(int m,
int n)
Construct an m-by-n matrix of zeros.
|
Matrix(int m,
int n,
double s)
Construct an m-by-n constant matrix.
|
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) |
private void |
checkMatrixDimensions(Matrix B)
Check if size(A) == size(B)
|
java.lang.Object |
clone()
Clone the Matrix object.
|
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 |
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.
|
int |
getColumnDimension()
Get column dimension.
|
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 |
getRowDimension()
Get row dimension.
|
double[] |
getRowPackedCopy()
Make a one-dimensional row packed copy of the internal array.
|
private double |
hypot(double a,
double b)
sqrt(a^2 + b^2) without under/overflow.
|
static Matrix |
identity(int m,
int n)
Generate identity matrix
|
Matrix |
inverse()
Matrix inverse or pseudoinverse
|
Matrix |
minus(Matrix B)
C = A - B
|
Matrix |
minusEquals(Matrix B)
A = A - B
|
double |
norm1()
One 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(java.io.PrintStream out) |
static Matrix |
random(int m,
int n)
Generate matrix with random elements
|
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.
|
Matrix |
solve(Matrix B)
Solve A*X = B
|
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
|
double |
trace()
Matrix trace.
|
Matrix |
transpose()
Matrix transpose.
|
Matrix |
uminus()
Unary minus
|
private static final long serialVersionUID
private double[][] A
private int m
private int n
public Matrix(int m, int n)
m
- Number of rows.n
- Number of columns.public Matrix(int m, int n, double s)
m
- Number of rows.n
- Number of columns.s
- Fill the matrix with this scalar value.public Matrix(double[][] A) throws java.lang.IllegalArgumentException
A
- Two-dimensional array of doubles.java.lang.IllegalArgumentException
- All rows must have the same lengthconstructWithCopy(double[][])
public Matrix(double[][] A, int m, int n)
A
- Two-dimensional array of doubles.m
- Number of rows.n
- Number of colums.public Matrix(double[] vals, int m) throws java.lang.IllegalArgumentException
vals
- One-dimensional array of doubles, packed by columns (ala Fortran).m
- Number of rows.java.lang.IllegalArgumentException
- Array length must be a multiple of m.public static Matrix constructWithCopy(double[][] A) throws java.lang.IllegalArgumentException
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[] getColumnPackedCopy()
public double[] getRowPackedCopy()
public int getRowDimension()
public int getColumnDimension()
public double get(int i, int j)
i
- Row index.j
- Column index.public Matrix getMatrix(int i0, int i1, int j0, int j1) throws java.lang.ArrayIndexOutOfBoundsException
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column indexjava.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int[] r, int[] c) throws java.lang.ArrayIndexOutOfBoundsException
r
- Array of row indices.c
- Array of column indices.java.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic Matrix getMatrix(int i0, int i1, int[] c) throws java.lang.ArrayIndexOutOfBoundsException
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.ArrayIndexOutOfBoundsException
r
- Array of row indices.j0
- Initial column indexj1
- Final column indexjava.lang.ArrayIndexOutOfBoundsException
- Submatrix indicespublic void set(int i, int j, double s)
i
- Row index.j
- Column index.s
- A(i,j).java.lang.ArrayIndexOutOfBoundsException
public void setMatrix(int i0, int i1, int j0, int j1, Matrix X) throws java.lang.ArrayIndexOutOfBoundsException
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) throws java.lang.ArrayIndexOutOfBoundsException
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) throws java.lang.ArrayIndexOutOfBoundsException
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) throws java.lang.ArrayIndexOutOfBoundsException
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 normInf()
public double normF()
public Matrix uminus()
public Matrix plusEquals(Matrix B)
B
- another matrixpublic Matrix minusEquals(Matrix B)
B
- another matrixpublic Matrix arrayTimes(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 times(double s)
s
- scalarpublic Matrix timesEquals(double s)
s
- scalarpublic Matrix times(Matrix B) throws java.lang.IllegalArgumentException
B
- another matrixjava.lang.IllegalArgumentException
- Matrix inner dimensions must agree.public double trace()
public static Matrix random(int m, int n)
m
- Number of rows.n
- Number of colums.public static Matrix identity(int m, int n)
m
- Number of rows.n
- Number of colums.public Matrix inverse()
public Matrix solve(Matrix B)
B
- right hand sidepublic void print(java.io.PrintStream out)
private void checkMatrixDimensions(Matrix B) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
private double hypot(double a, double b)
a
- b
-