Package lejos.utility
Class Matrix
- java.lang.Object
-
- lejos.utility.Matrix
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public class Matrix extends java.lang.Object implements java.lang.Cloneable, java.io.SerializableMatrix implementation derived from the JAMA project- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor 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 arrayMatrix(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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MatrixarrayLeftDivide(Matrix B)Element-by-element left division, C = A.\BMatrixarrayLeftDivideEquals(Matrix B)Element-by-element left division in place, A = A.\BMatrixarrayRightDivide(Matrix B)Element-by-element right division, C = A./BMatrixarrayRightDivideEquals(Matrix B)Element-by-element right division in place, A = A./BMatrixarrayTimes(Matrix B)Element-by-element multiplication, C = A.*BMatrixarrayTimesEquals(Matrix B)java.lang.Objectclone()Clone the Matrix object.static MatrixconstructWithCopy(double[][] A)Construct a matrix from a copy of a 2-D array.Matrixcopy()Make a deep copy of a matrixdoubleget(int i, int j)Get a single element.double[][]getArray()Access the internal two-dimensional array.double[][]getArrayCopy()Copy the internal two-dimensional array.intgetColumnDimension()Get column dimension.double[]getColumnPackedCopy()Make a one-dimensional column packed copy of the internal array.MatrixgetMatrix(int[] r, int[] c)Get a submatrix.MatrixgetMatrix(int[] r, int j0, int j1)Get a submatrix.MatrixgetMatrix(int i0, int i1, int[] c)Get a submatrix.MatrixgetMatrix(int i0, int i1, int j0, int j1)Get a submatrix.intgetRowDimension()Get row dimension.double[]getRowPackedCopy()Make a one-dimensional row packed copy of the internal array.static Matrixidentity(int m, int n)Generate identity matrixMatrixinverse()Matrix inverse or pseudoinverseMatrixminus(Matrix B)C = A - BMatrixminusEquals(Matrix B)A = A - Bdoublenorm1()One normdoublenormF()Frobenius normdoublenormInf()Infinity normMatrixplus(Matrix B)C = A + BMatrixplusEquals(Matrix B)A = A + Bvoidprint(java.io.PrintStream out)static Matrixrandom(int m, int n)Generate matrix with random elementsvoidset(int i, int j, double s)Set a single element.voidsetMatrix(int[] r, int[] c, Matrix X)Set a submatrix.voidsetMatrix(int[] r, int j0, int j1, Matrix X)Set a submatrix.voidsetMatrix(int i0, int i1, int[] c, Matrix X)Set a submatrix.voidsetMatrix(int i0, int i1, int j0, int j1, Matrix X)Set a submatrix.Matrixsolve(Matrix B)Solve A*X = BMatrixtimes(double s)Multiply a matrix by a scalar, C = s*AMatrixtimes(Matrix B)Linear algebraic matrix multiplication, A * BMatrixtimesEquals(double s)Multiply a matrix by a scalar in place, A = s*Adoubletrace()Matrix trace.Matrixtranspose()Matrix transpose.Matrixuminus()Unary minus
-
-
-
Constructor Detail
-
Matrix
public Matrix(int m, int n)Construct an m-by-n matrix of zeros.- Parameters:
m- Number of rows.n- Number of columns.
-
Matrix
public Matrix(int m, int n, double s)Construct an m-by-n constant matrix.- Parameters:
m- Number of rows.n- Number of columns.s- Fill the matrix with this scalar value.
-
Matrix
public Matrix(double[][] A) throws java.lang.IllegalArgumentExceptionConstruct a matrix from a 2-D array.- Parameters:
A- Two-dimensional array of doubles.- Throws:
java.lang.IllegalArgumentException- All rows must have the same length- See Also:
constructWithCopy(double[][])
-
Matrix
public Matrix(double[][] A, int m, int n)Construct a matrix quickly without checking arguments.- Parameters:
A- Two-dimensional array of doubles.m- Number of rows.n- Number of colums.
-
Matrix
public Matrix(double[] vals, int m) throws java.lang.IllegalArgumentExceptionConstruct a matrix from a one-dimensional packed array- Parameters:
vals- One-dimensional array of doubles, 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 Matrix constructWithCopy(double[][] A) throws java.lang.IllegalArgumentException
Construct a matrix from a copy of a 2-D array.- Parameters:
A- Two-dimensional array of doubles.- Throws:
java.lang.IllegalArgumentException- All rows must have the same length
-
copy
public Matrix copy()
Make a deep copy of a matrix
-
clone
public java.lang.Object clone()
Clone the Matrix object.- Overrides:
clonein classjava.lang.Object
-
getArray
public double[][] getArray()
Access the internal two-dimensional array.- Returns:
- Pointer to the two-dimensional array of matrix elements.
-
getArrayCopy
public double[][] getArrayCopy()
Copy the internal two-dimensional array.- Returns:
- Two-dimensional array copy of matrix elements.
-
getColumnPackedCopy
public double[] 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 double[] 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 double get(int i, int j)Get a single element.- Parameters:
i- Row index.j- Column index.- Returns:
- A(i,j)
-
getMatrix
public Matrix getMatrix(int i0, int i1, int j0, int j1) throws java.lang.ArrayIndexOutOfBoundsException
Get a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexj0- Initial column indexj1- Final column index- Returns:
- A(i0:i1,j0:j1)
- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
getMatrix
public Matrix getMatrix(int[] r, int[] c) throws java.lang.ArrayIndexOutOfBoundsException
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 Matrix getMatrix(int i0, int i1, int[] c) throws java.lang.ArrayIndexOutOfBoundsException
Get a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexc- Array of column indices.- Returns:
- A(i0:i1,c(:))
- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
getMatrix
public Matrix getMatrix(int[] r, int j0, int j1) throws java.lang.ArrayIndexOutOfBoundsException
Get a submatrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column index- Returns:
- A(r(:),j0:j1)
- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
set
public void set(int i, int j, double 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, Matrix X) throws java.lang.ArrayIndexOutOfBoundsExceptionSet a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexj0- Initial column indexj1- Final column indexX- A(i0:i1,j0:j1)- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
public void setMatrix(int[] r, int[] c, Matrix X) throws java.lang.ArrayIndexOutOfBoundsExceptionSet 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, Matrix X) throws java.lang.ArrayIndexOutOfBoundsExceptionSet a submatrix.- Parameters:
r- Array of row indices.j0- Initial column indexj1- Final column indexX- A(r(:),j0:j1)- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
setMatrix
public void setMatrix(int i0, int i1, int[] c, Matrix X) throws java.lang.ArrayIndexOutOfBoundsExceptionSet a submatrix.- Parameters:
i0- Initial row indexi1- Final row indexc- Array of column indices.X- A(i0:i1,c(:))- Throws:
java.lang.ArrayIndexOutOfBoundsException- Submatrix indices
-
transpose
public Matrix transpose()
Matrix transpose.- Returns:
- A'
-
norm1
public double norm1()
One norm- Returns:
- maximum column sum.
-
normInf
public double normInf()
Infinity norm- Returns:
- maximum row sum.
-
normF
public double normF()
Frobenius norm- Returns:
- sqrt of sum of squares of all elements.
-
uminus
public Matrix uminus()
Unary minus- Returns:
- -A
-
plusEquals
public Matrix plusEquals(Matrix B)
A = A + B- Parameters:
B- another matrix- Returns:
- A + B
-
minusEquals
public Matrix minusEquals(Matrix B)
A = A - B- Parameters:
B- another matrix- Returns:
- A - B
-
arrayTimes
public Matrix arrayTimes(Matrix B)
Element-by-element multiplication, C = A.*B- Parameters:
B- another matrix- Returns:
- A.*B
-
arrayRightDivide
public Matrix arrayRightDivide(Matrix B)
Element-by-element right division, C = A./B- Parameters:
B- another matrix- Returns:
- A./B
-
arrayRightDivideEquals
public Matrix arrayRightDivideEquals(Matrix B)
Element-by-element right division in place, A = A./B- Parameters:
B- another matrix- Returns:
- A./B
-
arrayLeftDivide
public Matrix arrayLeftDivide(Matrix B)
Element-by-element left division, C = A.\B- Parameters:
B- another matrix- Returns:
- A.\B
-
arrayLeftDivideEquals
public Matrix arrayLeftDivideEquals(Matrix B)
Element-by-element left division in place, A = A.\B- Parameters:
B- another matrix- Returns:
- A.\B
-
times
public Matrix times(double s)
Multiply a matrix by a scalar, C = s*A- Parameters:
s- scalar- Returns:
- s*A
-
timesEquals
public Matrix timesEquals(double s)
Multiply a matrix by a scalar in place, A = s*A- Parameters:
s- scalar- Returns:
- replace A by s*A
-
times
public Matrix times(Matrix B) throws java.lang.IllegalArgumentException
Linear algebraic matrix multiplication, A * B- Parameters:
B- another matrix- Returns:
- Matrix product, A * B
- Throws:
java.lang.IllegalArgumentException- Matrix inner dimensions must agree.
-
trace
public double trace()
Matrix trace.- Returns:
- sum of the diagonal elements.
-
random
public static Matrix 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 Matrix 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.
-
inverse
public Matrix inverse()
Matrix inverse or pseudoinverse- Returns:
- inverse(A) if A is square, pseudoinverse otherwise.
-
solve
public Matrix solve(Matrix B)
Solve A*X = B- Parameters:
B- right hand side- Returns:
- solution if A is square, least squares solution otherwise
-
print
public void print(java.io.PrintStream out)
-
-