Package linalg
Class Vector
java.lang.Object
linalg.Matrix
linalg.Vector
This class supports the creation, manipulations, and operations of Vectors.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionVector()
Creates an empty column vector.Vector(double[] entries)
Creates a column vector from the entries array.Vector(double[] entries, int type)
Creates a row/column vector depending on the value passed totype
.Vector(int size)
Creates a column vector of specified size filled with zeros.Vector(int[] entries)
Creates a column vector from the entries array.Vector(int[] entries, int type)
Creates a row/column vector depending on the value passed totype
.Vector(int size, int type)
Creates a row/column vector of specified size filled with zeros.Creates a column vector from the entries array.Creates a row/column vector depending on the value passed totype
. -
Method Summary
Modifier and TypeMethodDescriptiondefault Matrix
abs()
Computes absolute value, element-wise, of a matrix.default Matrix
add(double a)
Adds the value of a to all entries of matrix.default Matrix
Performs matrix addition on two matrices of the same dimensions.Adds two vectors element-wise and stores in a new vector.default Matrix
Augments two matrices.default Matrix
colSpace()
Finds an orthonormal basis for the column space of this matrix.default Matrix
conjT()
Computes the conjugate transpose of this matrix.default Matrix
Conjugates a matrix element-wise.default CNumber
det()
Computes determinant of matrix.default Matrix
diag()
Extracts diagonal elements from matrix.default Vector
Extracts diagonal elements form matrix and stores in vector.default Matrix
Computes the matrix direct sum.default Matrix
divRow(int rowIndex, double divisor)
Divides a specified row by a constant value.default Matrix
Divides a specified row by a constant value.default Matrix[]
eig()
Computes the eigenvalues and associated eigenvectors for a square matrix.default Matrix
eigVals()
Computes the eigenvalues of a matrix.default Matrix
eigVecs()
Computes the right eigenvectors of a matrix.default Matrix
Performs element-wise division on two matrices of the same dimensions.default Matrix
Performs element-wise multiplication of two matrices.default boolean
Checks if two matrices are element-wise equivalent.default Matrix
erref()
Computes reduced extended row-echelon form of matrix.default Matrix
extend(int n)
Extends a row or column vector to form a matrix.default CNumber
Computes the Frobenius inner product of two matrices A and B,<A, B>
F.default Matrix
flatten()
Flattens anMxN
matrix to a 1x(m*n) matrix.default Matrix
flatten(int axis)
Flattens anMxN
matrix to a 1x(m*n) or (m*n)x1 matrix depending on axis.CNumber[]
default Matrix
H()
Computes the Hermation adjoint of a matrix.default Matrix
Computes the Hermation adjoint of a matrix.default Matrix
hessu()
Converts a matrix similar to this matrix that is in upper Hessenburg form.default Matrix[]
hessuV()
Converts a matrix similar to this matrix that is in upper Hessenburg form.infNorm()
Computes the infinity or maximum norm.innerProduct(Vector b)
Computs inner prodcut of two vectors.default Matrix
inv()
Computes the matrix inverse if it exists.default Matrix
inverse()
Computes the matrix inverse if it exists.default boolean
Checks if matrix has at least one complex entry.default boolean
Checks if this matrix is diagonal.default boolean
Checks if a matrix is diagonalizable.default boolean
isEmpty()
Checks if the matrix is empty.default boolean
Checks to see if a matrices rank is the same as its number of rows.default boolean
Checks if matrix is hermation.default boolean
isI()
Checks if a matrix is an identity matrix.default boolean
Checks if B is the inverse of this matrix.default boolean
isNeg()
default boolean
Checks if this matrix is orthogonal.default boolean
isPos()
default boolean
isPosDef()
Checks if matrix is positive-definite.default boolean
Checks if matrix is positive-semidefinite.default boolean
isReal()
Checks if matrix is real.default boolean
Checks if matrix is self-adjoint.default boolean
default boolean
Checks if a matrix is skew-symmetric.default boolean
isSquare()
Checks if the matrix is square.default boolean
Checks if a matrix is symmetric.default boolean
isSymmetric(String skewOption)
Checks if a matrix is symmetric or skew-symmetric depending on the skewOption.default int
isTri()
Checks if a matrix is Triangular.
A triangular matrix has all zeros above and/or below the principle diagonal.
- Diagonal: A square matrix is diagonal if every element above and below the principle diagonal is zero.
- Lower Triangular: A square matrix is lower triangular if every element above the principle diagonal is zero.
- Upper Triangular: A square matrix is upper triangular if every element below the principle diagonal is zero
See
-isTriU()
-isTriL()
-isDiagonal()
default boolean
isTriL()
Checks if matrix is lower triangular.default boolean
isTriU()
Checks if matrix is upper triangular.default boolean
Checks if this matrix is unitary.default boolean
isVector()
Checks if a given matrix is a column or row vector.default boolean
isZero()
Checks if this matrix has only zero entries.default Matrix
Computes an orthonormal basis of the left null space of this matrix.default CNumber
max()
Finds the maximum value in the matrix.default CNumber
Finds value with maximum magnitude.default double
maxReal()
Finds the maximum real value in the matrix.default CNumber
min()
Finds the minimum value in the matrix.default CNumber
Finds value with minimum magnitude.default double
minReal()
Finds the minimum real value in the matrix.default Matrix
Performs matrix multiplication on two matrices.default Matrix
multRow(int rowIndex, double factor)
Multiplies a specified row by a constant value.default Matrix
Multiplies a specified row by a constant value.norm()
norm(double p)
Computes the p-norm of a vector.default CNumber
norm(double p, double q)
Computes the Lp, q norm of this matrix.default int
nullity()
The rank of a matrix A is the dimension of the vector space spanned by the nullspace of this matrix.default Matrix
Computes a orthonormal basis for the null space of this matrix.default int
numCols()
Number of columns in this matrixdefault int
numRows()
Number of rows in this matrix.outerProduct(Vector b)
Computes outer product of two vectors.default int
rank()
The rank of a matrix A is the dimension of the vector space generated (or spanned) by its columns.default Matrix
recep()
Creates a new matrix that contains the reciprocals of this matrixdefault Matrix
ref()
Computes row-echelon form of matrix.default Matrix
removeCol(int colIndex)
Removes specified single column from matrix.default Matrix
removeCols(int... colIndices)
Removes specified columns from matrix.default Matrix
removeRow(int rowIndex)
Removes specified single row from matrix.default Matrix
removeRows(int... rowIndices)
Removes specified rows from matrix.default Matrix
reshape(int newM, int newN)
default Matrix
default Matrix
round(int decimals)
Rounds all entries of a matrix.default void
roundToZero(int decimals)
Rounds values in this matrix within a specified number of decimals of zero to zero.default Matrix
rowSpace()
Finds an orthonormal basis for the row space of this matrix.default Matrix
rref()
Computes reduced row-echelon form of matrix.default Matrix
rref(boolean partialPivoting)
Computes reduced row-echelon form of matrix.default Matrix
Computes reduced row-echelon form of matrix.default boolean
Compares the dimensions, rows and columns, of two matrices.default boolean
Compares the dimensions of two matrices.scalDiv(double value)
default Matrix
scalMult(double factor)
Performs scalar multiplication of a matrix.default Matrix
Performs scalar multiplication of a matrix.default void
set(double value, int row, int col)
Sets specified element in matrix to value.default void
Sets specified element in matrix to value.default void
setCol(double[] col, int colIndex)
Sets specified column of this matrix to the values stored within the passed array.default void
Sets specified column of this matrix to the values stored within the passed array.default void
Sets specified column of this matrix to the values stored within the passed column vector.default void
setRow(double[] row, int rowIndex)
Sets specified row of this matrix to the values stored within the passed array.default void
Sets specified row of this matrix to the values stored within the passed array.default void
Sets specified row of this matrix to the values stored within the passed row vector.default void
Sets a specified slice of this matrix to the values stored in thevalues
matrix.default Matrix
setSliceCopy(int rowStart, int colStart, Matrix values)
Creates a copy of this matrix and sets a specified slice of the copy to the values stored in thevalues
matrix.default void
setValues(double[][] values)
Copies values from array into matrix.default void
Copies values from array into matrix.default String
shape()
Shape of this matrix i.e.default Matrix
sqrt()
Computes element wise square root of the matrix.default Matrix
Stacks matrices along rows.default Matrix
Stacks matrices along specified axis.default Matrix
sub(double a)
Subtracts the value of a from all entries of matrix.default Matrix
Performs matrix subtraction on two matrices of the same dimensions.subtracts two vectors element-wise and stores in a new vector.default Matrix
swapCols(int colIndex1, int colIndex2)
Swaps two columns in a matrix.default Matrix
swapRows(int rowIndex1, int rowIndex2)
Swaps two rows in a matrix.default Matrix
T()
Transposes Matrix.Converts Vector to a column vector.toMatrix()
Converts a vector to a like matrix objectstatic Matrix
Converts a vector to a matrix.Converts Vector to a row vector.toString()
Formats vector as a string.default Vector
toVector()
If possible, converts matrix to a Vector object.
A matrix will be converted to a row vector if it contains only a single row.default CNumber
tr()
Computes the trace of a square matrix.default CNumber
trace()
Computes the trace of square matrix.default Matrix
Transposes Matrix.default Matrix
tril()
Generates lower triangle of Matrix.default Matrix
tril(int k)
Generates lower triangle of Matrix.default Matrix
triu()
Generates upper triangle of Matrix.default Matrix
triu(int k)
Generates upper triangle of Matrix.int
Get the type of this vectorMethods inherited from class linalg.Matrix
copy, get, getAsDouble, getCol, getColAsVector, getRow, getRowAsVector, getSlice, getValues, getValuesAsDouble, I, I, identity, ones, ones, ones, print, println, printSep, randn, random, random, random, random, random, random, randomComplex, randomOrthogonal, randomUnitary, van, zeros, zeros, zeros
-
Field Details
-
COLUMN_VECTOR
public static final int COLUMN_VECTOR- See Also:
- Constant Field Values
-
ROW_VECTOR
public static final int ROW_VECTOR- See Also:
- Constant Field Values
-
-
Constructor Details
-
Vector
public Vector()Creates an empty column vector. -
Vector
public Vector(int size)Creates a column vector of specified size filled with zeros.- Parameters:
size
- - size of the column vector
-
Vector
public Vector(int size, int type)Creates a row/column vector of specified size filled with zeros.- Parameters:
size
- - Size of the vector.type
- - Specifies the type of vector. Pass a 0 for a column vector or a 1 for a row vector.
-
Vector
public Vector(int[] entries)Creates a column vector from the entries array.- Parameters:
entries
- - entries of the column vector.
-
Vector
public Vector(int[] entries, int type)Creates a row/column vector depending on the value passed totype
.- Parameters:
entries
- - entries of the vector.type
- - Specifies the type of vector. Pass a 0 for a column vector or a 1 for a row vector.
-
Vector
public Vector(double[] entries)Creates a column vector from the entries array.- Parameters:
entries
- - entries of the column vector.
-
Vector
public Vector(double[] entries, int type)Creates a row/column vector depending on the value passed totype
.- Parameters:
entries
- - entries of the vector.type
- - Specifies the type of vector. Pass a 0 for a column vector or a 1 for a row vector.
-
Vector
Creates a column vector from the entries array.- Parameters:
entries
- - entries of the column vector.
-
Vector
Creates a row/column vector depending on the value passed totype
.- Parameters:
entries
- - entries of the vector.type
- - Specifies the type of vector. Pass a 0 for a column vector or a 1 for a row vector.
-
Vector
-
-
Method Details
-
toMatrix
Converts a vector to a matrix.- Parameters:
v
- - vector to convert to matrix- Returns:
- Matrix of vector. If the vector is a row vector the matrix will have one row and the same number of columns as entries in the vector. If the vector is a column vector the matrix will have one column and the same number of rows as entries in the vector.
-
toRowVector
Converts Vector to a row vector.- Returns:
- If the vector is already a row vector, then the same vector is returned. If the vector is a column vecctor, then a new row vector with identical entires to the column vector is returned.
-
toColVector
Converts Vector to a column vector.- Returns:
- If the vector is already a column vector, then the same vector is returned. If the vector is a row vecctor, then a new column vector with identical entires to the row vector is returned.
-
vectorType
public int vectorType()Get the type of this vector- Returns:
- Returns 0 for a column vector, 1 for a row vector.
-
innerProduct
Computs inner prodcut of two vectors. Note, vectors do not have to be the same type. That is, any mixture of row and column vectors is fine as long as they have the same number of entries.- Parameters:
b
- - Vector to compute inner product with.- Returns:
- Inner product of this vector with b.
-
outerProduct
Computes outer product of two vectors. The result of vector outer products is a matrix.
Vectors must be of oposite types. That is, exactly one of the two vectors must be a row vector and exactly one of the two vectors must be a column vector.
In this method, vectors are treated as matrices with 1 row or 1 column depending on the vector type. Then,Matrix.mult(Matrix B)
is used.- Parameters:
b
- - Vector to compute outer product with.- Returns:
- Outer product of this vector with b.
-
norm
- Returns:
- 2-norm of this vector. This will be a real value.
-
norm
Computes the p-norm of a vector. If p=1, this is called the Taxicab norm or Manhattan norm.
If p=2, this is equivalent tonorm()
which is the euclidian norm. If p isDouble.POSITIVE_INFINITY
this is equivalent to- Parameters:
p
- - norm value. Must satisfy p >= 1 and can beDouble.POSITIVE_INFINITY
.- Returns:
-
infNorm
Computes the infinity or maximum norm. That is, the value with the maximum magnitude. If the vector is real this is equivalent to the maximum absolute value.
Also see
-norm(double p)
-norm(double)
- Returns:
- The infinity or maximum norm of this vector.
-
getEntries
- Returns:
-
add
Adds two vectors element-wise and stores in a new vector. Vectors must be of the same type.- Parameters:
b
- - Vector to add to this vector- Returns:
- Result of vector addition.
-
sub
subtracts two vectors element-wise and stores in a new vector. Vectors must be of the same type.- Parameters:
b
- - Vector to subtract- Returns:
- Result of vector subtraction.
-
scalDiv
-
scalDiv
-
toMatrix
Converts a vector to a like matrix object- Returns:
-
toString
Formats vector as a string. -
add
Performs matrix addition on two matrices of the same dimensions.- Parameters:
B
- - matrix to add to the instance matrix- Returns:
- result of matrix addition
-
add
Adds the value of a to all entries of matrix.- Parameters:
a
- Value to add to matrix.- Returns:
- A new matrix with the value of a added to this matrix.
-
sub
Performs matrix subtraction on two matrices of the same dimensions.- Parameters:
B
- - matrix to subtract to the instance matrix- Returns:
- result of matrix subtraction
-
sub
Subtracts the value of a from all entries of matrix.- Parameters:
a
- Value to subtract from matrix.- Returns:
- A new matrix with the value of a added to this matrix.
-
mult
Performs matrix multiplication on two matrices. The instance matrix must have the same number of columns as the rows of B. If the instance matrix is a kxm matrix and B is a m x n matrix then the result will be a k x n matrix.- Parameters:
B
- - matrix to multiply to the instance matrix- Returns:
- result of matrix multiplication
-
elemMult
Performs element-wise multiplication of two matrices.- Parameters:
B
- - matrix to multiply element-wise to this matrix.- Returns:
- result of element-wise matrix multiplication.
- Throws:
IllegalArgumentException
- If matrices do not have the same dimension.
-
scalMult
Performs scalar multiplication of a matrix.- Parameters:
factor
- - value to multiply this matrix by.- Returns:
- The scalar multiplication of the matrix and the factor.
-
scalMult
Performs scalar multiplication of a matrix.- Parameters:
factor
- - value to multiply matrix by.- Returns:
- The scalar multiplication of the matrix and the factor.
-
elemDiv
Performs element-wise division on two matrices of the same dimensions.- Parameters:
B
- - matrix to divide element-wise the instance matrix with.- Returns:
- result of element-wise matrix multiplication.
-
fip
Computes the Frobenius inner product of two matrices A and B,<A, B>
F.- Parameters:
B
- - Second matrix for the Frobenius inner product.- Returns:
- the Frobenius inner product.
-
directSum
Computes the matrix direct sum. That is, a block diagonal matrix containing all matrices from a set of matrices.- Parameters:
matrixList
- - List of matrices from which to compute the matrix direct sum.- Returns:
- The result of direct summing the matrices in matrixList to this matrix.
-
sqrt
Computes element wise square root of the matrix. All square roots are the positive root or, in the case of complex entries, the root with positive real part.- Returns:
- The element-wise square root of this matrix.
-
abs
Computes absolute value, element-wise, of a matrix. If any of the matrix cells are complex, this will result in the magnitude of that value.- Returns:
- - element-wise absolute value of matrix.
-
transpose
Transposes Matrix. Same asMatrix.T()
- Returns:
- transpose of matrix
-
T
Transposes Matrix. Same asMatrix.transpose()
- Returns:
- transpose of matrix
-
conjugate
Conjugates a matrix element-wise.- Returns:
- Conjugate of matrix
-
conjT
- Returns:
- The conjugate transpose of this matrix.
-
hermAdjoint
Computes the Hermation adjoint of a matrix. This is the transpose of the conjugate matrix.
This method is the same asconjT()
andH()
.- Returns:
- adjoint of matrix.
-
H
Computes the Hermation adjoint of a matrix. This is the transpose of the conjugate matrix.
This method is the same asconjT()
andhermAdjpint()
.- Returns:
- adjoint of matrix.
-
det
Computes determinant of matrix. If the matrix has any complex entries, this may be a complex value. Note: Currently this method only works for real matirces.- Returns:
- determinant of matrix.
-
stack
Stacks matrices along rows. Both matrices must have the same number of columns Also seestack(Matrix B, int axis)
- Parameters:
B
-- Returns:
-
stack
Stacks matrices along specified axis. Axis 0 will stack matrices along the rows. Axis 1 will stack matrices along columns. Note: To stack matrices along axis 0 they must have the same number of columns. To stack matrices along axis 1 they must have the same number of rows.- Parameters:
B
- - Matrix to stackaxis
- - Axis along which to stack matrices.- Returns:
- Returns A and B stacked along specified axis.
-
augment
Augments two matrices. This is the same asstack(B, 1)
- Parameters:
B
- - Matrix to augment to this matrix.- Returns:
- The matrix B augmented to this matrix.
-
ref
Computes row-echelon form of matrix. This will be an upper-triangular matrix.
A matrix is in row-echelon form if: - The first non-zero element in each row, called the leading entry (also called the pivot), is 1. - The pivot of a nonzero row is always strictly to the right of the leading coefficient of the row above it. - Rows with all zero elements, if any, are below rows having a non-zero element.
A matrix can be transformed into a row equivalent matrix in row-echelon form using row operations. This is done using Gaussian (Gauss-Jordan) elimination.
Also see
for reduced row-echelon form.rref()
- Returns:
- Row-echelon form of matrix
-
rref
Computes reduced row-echelon form of matrix.
A matrix is in reduced row-echelon form if:
A matrix can be transformed into a row eqivalent matrix in reduced row-echelon form using row operations. This is done using Gaussian (Gauss-Jordan) elimination.
- It is in row-echelon form. This is, ~ The first non-zero element in each row, called the leading entry (also called the pivot), is 1. ~ The pivot of a nonzero row is always strictly to the right of the leading coefficient of the row above it. ~ Rows with all zero elements, if any, are below rows having a non-zero element.
- The pivot in each row is the only non-zero entry in its column.- Parameters:
partialPivoting
- - Falg for use of partial pivoting.- If true then the rref will be computed using partial pivoting. ~ This is equivalent to the method
rref()
. - If false then the rref will be computed WITHOUT using partial pivoting.- Returns:
-
rref
Computes reduced row-echelon form of matrix. This is done using partial pivoting.
A matrix is in reduced row-echelon form if:
A matrix can be transformed into a row eqivalent matrix in reduced row-echelon form using row operations. This is done using Gaussian (Gauss-Jordan) elimination.
- It is in row-echelon form. This is, ~ The first non-zero element in each row, called the leading entry (also called the pivot), is 1. ~ The pivot of a nonzero row is always strictly to the right of the leading coefficient of the row above it. ~ Rows with all zero elements, if any, are below rows having a non-zero element.
- The pivot in each row is the only non-zero entry in its column.
Also see
for row-echelon form.ref()
- Returns:
- Row-echelon form of matrix.
-
rrefNoPivot
Computes reduced row-echelon form of matrix. This is done WITHOUT using partial pivoting.
A matrix is in reduced row-echelon form if:
A matrix can be transformed into a row eqivalent matrix in reduced row-echelon form using row operations. This is done using Gaussian (Gauss-Jordan) elimination.
- It is in row-echelon form. This is, ~ The first non-zero element in each row, called the leading entry (also called the pivot), is 1. ~ The pivot of a nonzero row is always strictly to the right of the leading coefficient of the row above it. ~ Rows with all zero elements, if any, are below rows having a non-zero element.
- The pivot in each row is the only non-zero entry in its column.
Also see
for row-echelon form.ref()
- Returns:
- Row-echelon form of matrix.
-
erref
Computes reduced extended row-echelon form of matrix. That is, a Matrix with the same number of rows isaugmented
with this matrix and then this augmented matrix is put intoreduced row-echelon form
.- Returns:
- Returns extended row-echelon form of this matrix.
-
trace
Computes the trace of square matrix. That is, the sum of the entries along the principle diagonal.
This method is the same astr()
.- Returns:
- trace of this matrix.
-
tr
Computes the trace of a square matrix. That is, the sum of the entries along the principle diagonal.
This method is the same astrace()
.- Returns:
- trace of this matrix.
-
rank
default int rank()The rank of a matrix A is the dimension of the vector space generated (or spanned) by its columns. This is always an integer. This corresponds to the maximal number of linearly independent columns of A. This, in turn, is identical to the dimension of the vector space spanned by its rows- Returns:
- Returns the rank of this matrix.
-
nullity
default int nullity()The rank of a matrix A is the dimension of the vector space spanned by the nullspace of this matrix. The nullify is always an integer.- Returns:
- Returns the rank of this matrix.
-
inverse
Computes the matrix inverse if it exists. This is done by first computing theQR decomposition
The inverse of a Matrix A is A-1 satisfying AA-1=I where I is the appropriately sized Identity matrix.- Returns:
- The inverse of this matrix.
-
inv
Computes the matrix inverse if it exists. This is done by first computing theQR decomposition
The inverse of a Matrix A is A-1 satisfying AA-1=I where I is the appropriately sized Identity matrix.- Returns:
- The inverse of this matrix.
-
recep
Creates a new matrix that contains the reciprocals of this matrix- Returns:
- new matrix that contains the reciprocals of this matrix
-
reshape
-
reshape
-
flatten
Flattens anMxN
matrix to a 1x(m*n) matrix. Each row of the Matrix is appended to the end of the first row of the Matrix in order.- Returns:
-
flatten
Flattens anMxN
matrix to a 1x(m*n) or (m*n)x1 matrix depending on axis.- Parameters:
axis
- - axis along which to flatten- Returns:
-
extend
Extends a row or column vector to form a matrix. That is, for a row vector,- Parameters:
n
- Number of times to make the extension. That is, for a row vector with m entries, a matrix of shape n-by-m will be formed. For a column vector with m entries, a matrix of shape m-by-n will be formed.- Returns:
- The extended matrix formed from the vector.
- Throws:
IllegalCallerException
- if the caller is not aVector
-
setValues
Copies values from array into matrix. The given array must have the same dimensions as the matrix object. This will replace any current values in the matrix.- Parameters:
values
- - Values to copy into array
-
setValues
default void setValues(double[][] values)Copies values from array into matrix. The given array must have the same dimensions as the matrix object. A will replace any current values in the matrix.- Parameters:
values
- - Values to copy into array
-
set
Sets specified element in matrix to value. A will replace the current value at that index.- Parameters:
value
- - value to insert into matrixrow
- - row to insert valuecol
- - column to insert value
-
set
default void set(double value, int row, int col)Sets specified element in matrix to value. A will replace the current value at that index.- Parameters:
value
- - value to insert into matrixrow
- - row index to insert valuecol
- - column index to insert value
-
setCol
Sets specified column of this matrix to the values stored within the passed column vector.
Also see
setCol(CNumber[] col, int colIndex)
setCol(double[] col, int colIndex)
- Parameters:
col
- - Column vector containing the new entries for the specified column.colIndex
- - Index of new column to set.- Throws:
IllegalArgumentException
- if the vector is not a column vector or if the vector and matrix do not have the same number of rows.
-
setCol
Sets specified column of this matrix to the values stored within the passed array.
Also see
setCol(Vector col, int colIndex)
setCol(double[] col, int colIndex)
- Parameters:
col
- - array containing the new entries for the specified column.colIndex
- - Index of new column to set.- Throws:
IllegalArgumentException
- if the array of values has a different length then the number of rows in the matrix.
-
setCol
default void setCol(double[] col, int colIndex)Sets specified column of this matrix to the values stored within the passed array. Also see
setCol(Vector col, int colIndex)
setCol(CNumber[] col, int colIndex)
- Parameters:
col
- - array containing the new entries for the specified column.colIndex
- - Index of new column to set.- Throws:
IllegalArgumentException
- if the array of values has a different length then the number of rows in the matrix.
-
setRow
Sets specified row of this matrix to the values stored within the passed row vector.
Also see
setRow(CNumber[] col, int colIndex)
setRow(double[] col, int colIndex)
- Parameters:
row
- - row vector containing the new entries for the specified column.rowIndex
- - Index of new row to set.- Throws:
IllegalArgumentException
- if the vector is not a row vector or if the vector and matrix do not have the same number of columns.
-
setRow
Sets specified row of this matrix to the values stored within the passed array.
Also see
setRow(Vector col, int colIndex)
setRow(double[] col, int colIndex)
- Parameters:
row
- - array containing the new entries for the specified column.rowIndex
- - Index of new column to set.- Throws:
IllegalArgumentException
- if the array of values has a different length then the number of columns in the matrix.
-
setRow
default void setRow(double[] row, int rowIndex)Sets specified row of this matrix to the values stored within the passed array.
Also see
setRow(Vector col, int colIndex)
setRow(CNumber[] col, int colIndex)
- Parameters:
row
- - array containing the new entries for the specified column.rowIndex
- - Index of new column to set.- Throws:
IllegalArgumentException
- if the array of values has a different length then the number of columns in the matrix.
-
setSliceCopy
Creates a copy of this matrix and sets a specified slice of the copy to the values stored in thevalues
matrix. Together the parametersrowStart
andcolStart
define the upper left corner of the slice to set.
If the values matrix does not fit within this matrix, an error will be thrown.
If want to adjust this matrix instance directly seesetSlice(Matrix values, int rowStart, int colStart)
- Returns:
-
setSlice
Sets a specified slice of this matrix to the values stored in thevalues
matrix. Together the parametersrowStart
andcolStart
define the upper left corner of the slice to set.
If the values matrix does not fit within this matrix, an error will be thrown.
If you do not want to adjust this matrix instance seesetSliceCopy(Matrix values, int rowStart, int colStart)
- Parameters:
rowStart
- - Row on original matrix to place top row of values matrixcolStart
- - Column on original matrix to place left-most column of values matrixvalues
- - New values to set within specified slice.
-
removeRow
Removes specified single row from matrix. To remove more than one row at a time seeremoveRows(int... rowIndices)
.- Parameters:
rowIndex
- - Index of row to remove.- Returns:
- Matrix with the specified row removed.
-
removeRows
Removes specified rows from matrix. If k row indices are specified for a mxn matrix, the resulting matrix will have shape (m-k)xn. Also seeremoveRow(int rowIndex)
.- Parameters:
rowIndices
- - list of row indices to remove from matrix.- Returns:
- Matrix with specified rows removed
-
removeCol
Removes specified single column from matrix. To remove more than one column at a time seeremoveCols(int... colIndices)
.- Parameters:
colIndex
- - Index of column to remove.- Returns:
- Matrix with the specified column removed.
-
removeCols
Removes specified columns from matrix. If k column indices are specified for a mxn matrix, the resulting matrix will have shape m-by-(n-k). Also seeremoveCol(int colIndex)
- Parameters:
colIndices
- - list of column indices to remove from matrix.- Returns:
- Matrix with specified columns removed
-
tril
Generates lower triangle of Matrix. That is, all values above the middle diagonal will be zeroed. Also seetril(int k)
- Returns:
- Lower triangle of Matrix.
-
tril
Generates lower triangle of Matrix. That is, all values above the kth diagonal will be zeroed. Also seetril()
- Parameters:
k
- - diagonal above which to zero. k=0 whould be middile diagonal, k=-1 would be the diagonal to the left of the middle diagonal, and k=1 would be the diagonal to the right of the middle diagonal.- Returns:
- Lower triangle of Matrix.
-
triu
Generates upper triangle of Matrix. That is, all values above the middle diagonal will be zeroed. Also seetriu(int k)
- Returns:
- upper triangle of Matrix.
-
triu
Generates upper triangle of Matrix. That is, all values below the kth diagonal will be zeroed. Also seetriu()
- Parameters:
k
- - diagonal above which to zero. k=0 would be middle diagonal, k=-1 would be the diagonal to the left of the middle diagonal, and k=1 would be the diagonal to the right of the middle diagonal.- Returns:
- upper triangle of Matrix.
-
diag
Extracts diagonal elements from matrix.- Returns:
- Returns an equivalently sized matrix containing only the diagonal elements of this matrix.
-
diagAsVector
Extracts diagonal elements form matrix and stores in vector.- Returns:
- Column vector containing diagonal elements of this matrix.
-
hessu
Converts a matrix similar to this matrix that is in upper Hessenburg form. A matrix is in upper Hessenburg form if all entries below the first subdiagonal are zero. Two n-by-n matrices A and B are similar if there exists an invertible n-by-n matrix P, such that B=P-1AP. Similar matrices share many properties including the same eigenvalues.- Returns:
- A matrix similar to this matrix which is in upper Hessenburg form.
-
hessuV
Converts a matrix similar to this matrix that is in upper Hessenburg form. A matrix is in upper Hessenburg form if all entries below the first subdiagonal are zero. Two n-by-n matrices A and B are similar if there exists an invertible n-by-n matrix P, such that B=P-1AP. Similar matrices share many properties including the same eigenvalues.- Returns:
- A matrix similar to this matrix which is in upper Hessenburg form.
-
swapRows
Swaps two rows in a matrix.- Parameters:
rowIndex1
- - Index of first row to be swapped.rowIndex2
- - Index of second row to be swapped.- Returns:
- Matrix with specified rows swapped.
-
swapCols
Swaps two columns in a matrix.- Parameters:
colIndex1
- - Index of first column to be swapped.colIndex2
- - Index of second column to be swapped.- Returns:
- Matrix with specified columns swapped.
-
multRow
Multiplies a specified row by a constant value.- Parameters:
factor
- - number to multiply row by.- Returns:
- Matrix with specified row multiplied by the factor.
-
multRow
Multiplies a specified row by a constant value.- Parameters:
factor
- - number to multiply row by.- Returns:
- Matrix with specified row multiplied by the factor
-
divRow
Divides a specified row by a constant value.- Parameters:
divisor
- - number to divide row by.- Returns:
- Matrix with specified row divided by the divisor.
-
divRow
Divides a specified row by a constant value.- Parameters:
divisor
- - number to divide row by.- Returns:
- Matrix with specified row divided by the divisor.
-
toVector
If possible, converts matrix to a Vector object.
A matrix will be converted to a row vector if it contains only a single row.
A matrix will be converted to a column vector if it contains only a single column.
If a matrix contains only one row and one column or is empty, then it will be converted to a column vector by default.- Returns:
- Vector equivalent of matrix.
-
round
Rounds all entries of a matrix. If entry is complex, both the real and imaginary part will be rounded.- Parameters:
decimals
- - Number of decimals to round number to.- Returns:
- Returns copy of Matrix
A
with entries rounded to Specified number of decimal places.
-
roundToZero
default void roundToZero(int decimals)Rounds values in this matrix within a specified number of decimals of zero to zero.- Parameters:
decimals
- - Number of decimals
-
numCols
default int numCols()Number of columns in this matrix- Returns:
- Number of rows in matrix.
-
numRows
default int numRows()Number of rows in this matrix.- Returns:
- Number of columns in matrix.
-
shape
Shape of this matrix i.e. number of rows and columns.- Returns:
- Returns shape of this matrix shape as String e.g. "m x n".
-
isSquare
default boolean isSquare()Checks if the matrix is square. That is, does the matrix have the same number of rows and columns?- Returns:
- True if the matrix is square, false otherwise.
-
isEmpty
default boolean isEmpty()Checks if the matrix is empty. That is, does the matrix have exactly zero entries?- Returns:
- True if the matrix is empty, false otherwise.
-
min
Finds the minimum value in the matrix. If the matrix is complex, the value with the smallest magnitude will be returned. If the matrix is real, the smallest real number will be returned.
Also seeminReal()
andminComplex()
- Returns:
- minimum value of this matrix
-
minReal
default double minReal()Finds the minimum real value in the matrix. If the matrix contains non-real values, the imaginary component will be ignored.
Also seemin()
andminComplex()
- Returns:
- Returns minimum real value of this matrix.
-
max
Finds the maximum value in the matrix. If the matrix is complex, the value with the largest magnitude will be returned. If the matrix is real, the largest real number will be returned.
Also seemaxReal()
andmaxComplex()
- Returns:
- Returns maximum value of this matrix.
-
maxReal
default double maxReal()Finds the maximum real value in the matrix. If the matrix contains any non-real entries, the imaginary component will be ignored.
Also seemax()
andmaxComplex()
- Returns:
- Returns maximum real value of this matrix.
-
minComplex
- Returns:
- Returns value with minimum magnitude in this matrix.
-
maxComplex
- Returns:
- Returns value with maximum magnitude in this matrix.
-
isReal
default boolean isReal()Checks if matrix is real. That is, the matrix only has real entries.- Returns:
- True if matrix has no complex entries. Otherwise, false.
-
isComplex
default boolean isComplex()Checks if matrix has at least one complex entry.- Returns:
- True if matrix has at least one non-real entry. Otherwise, false.
-
isVector
default boolean isVector()Checks if a given matrix is a column or row vector. A column vector will be a a matrix of shapemx1
and a row vector will be a matrix of shape1xn
- Returns:
- True if matrix is a column or row vector, otherwise returns false.
-
isSelfAdjoint
default boolean isSelfAdjoint()Checks if matrix is self-adjoint. That is, if the matrix is equal to its own conjugate transpose.
Same asisHermation()
.- Returns:
- True if the matrix is self-adjoint. Otherwise, returns false.
-
isHermation
default boolean isHermation()Checks if matrix is hermation. That is, if the matrix is equal to its own conjugate transpose.
Same asisSelfAdjoint()
.- Returns:
- True if the matrix is hermation. Otherwise, returns false.
-
isSymmetric
default boolean isSymmetric()Checks if a matrix is symmetric.
For an square matrix A, A is symmetric if and only ifA[i][j] = A[j][i]
for all i, j. That is, if A is equal its own transpose, then it is symmetric.
Also seeisSkewSymmetric()
andisSymmetric(String skewOption)
- Returns:
- True if this matrix is symmetric, false otherwise.
-
isSkewSymmetric
default boolean isSkewSymmetric()Checks if a matrix is skew-symmetric.
For an square matrix A, A is skew-symmetric if and only ifA[i][j] = -A[j][i]
for all i, j. That is, if A is equal to the negative of its own transpose, then it is skew-symmetric.
Also seeisSymmetric()
andisSymmetric(String skewOption)
- Returns:
- True if this matrix is symmetric, false otherwise.
-
isSymmetric
Checks if a matrix is symmetric or skew-symmetric depending on the skewOption.
- If skewOption is passed "skew", this method is identical toisSkewSymmetric()
.
- If skewOption is passed "no-skew", this method is identical toisSymmetric()
.
For an square matrix A, A is symmetric if and only ifA[i][j] = A[j][i]
for all i, j. That is, if A is equal to A transpose.
A is skew-symmetric if and only ifA[i][j] = -A[j][i]
for all i, j. That is, if A is equal to -A transpose.- Parameters:
skewOption
- - String to indicate whether to check for symmetry or skew-symmetry. Specify skewOption as "skew" to check for skew-symmetry or "no-skew" to check for regular symmetry.- Returns:
- True if the matrix is symmetric/skew-symmetric. Otherwise, returns false.
-
isOrthogonal
default boolean isOrthogonal()Checks if this matrix is orthogonal. A square matrixQ
is orthogonal if and only ifQQ^T = I
where I is the appropriately sized identity matrix.
Please note, this method only checks ifQQ^T = I
. If the matrix is complex you may be looking forisUnitary()
which checks if a matrix is unitary orQQ^* = I
whereQ^*
is the conjugate transpose ofQ
. TheisUnitary()
method will behave the same as this method for real matrices.- Returns:
- True if the matrix is orthogonal. Otherwise, returns false.
-
isUnitary
default boolean isUnitary()Checks if this matrix is unitary. A square matrixQ
is unitary if and only ifQQ^* = I
whereI
is the appropriately sized identity matrix andQ^*
is the conjugate transpose.
For real matrices, this method is the same asisOrthogonal()
.- Returns:
- True if the matrix is unitary. Otherwise, returns false.
-
isTri
default int isTri()Checks if a matrix is Triangular.
A triangular matrix has all zeros above and/or below the principle diagonal.
- Diagonal: A square matrix is diagonal if every element above and below the principle diagonal is zero.
- Lower Triangular: A square matrix is lower triangular if every element above the principle diagonal is zero.
- Upper Triangular: A square matrix is upper triangular if every element below the principle diagonal is zero
See
-isTriU()
-isTriL()
-isDiagonal()
- Returns:
- - 1 if matrix is not triangular.
- 0 if Matrix is diagonal.
- 1 if Matrix is lower triangular.
- 2 if Matrix is upper triangular.
-
isTriU
default boolean isTriU()Checks if matrix is upper triangular. A square matrix is upper triangular if every element below the principle diagonal is zero.- Returns:
- True if this matrix is upper triangular. Otherwise, returns false.
-
isTriL
default boolean isTriL()Checks if matrix is lower triangular. A square matrix is lower triangular if every element above the principle diagonal is zero.- Returns:
- True if this matrix is lower triangular. Otherwise, returns false.
-
isDiagonal
default boolean isDiagonal()Checks if this matrix is diagonal. A matrix is diagonal if every element above and below the principle diagonal is zero.- Returns:
- True if this matrix is diagonal. Otherwise, returns false.
-
isFullRank
default boolean isFullRank()Checks to see if a matrices rank is the same as its number of rows.- Returns:
- Returns true if matrix is full rank. Otherwise, returns false.
-
isSingular
default boolean isSingular() -
norm
Computes the Lp, q norm of this matrix.- Parameters:
p
- - norm parameter 1q
- - norm parameter 2- Returns:
- Returns Lp, q norm of this matrix
-
rowSpace
Finds an orthonormal basis for the row space of this matrix.
The row space of a matrix is the span of all column vectors. A orthonormal row space basis is a linearly independent set of unit vectors which also spans the row space.- Returns:
- A matrix whose rows are row vectors forming an orthonormal basis for the row space of this matrix.
-
colSpace
Finds an orthonormal basis for the column space of this matrix.
The column space of a matrix is the span of all column vectors. A orthonormal column space basis is a linearly independent set of unit vectors which also spans the column space.- Returns:
- A matrix whose columns are column vectors forming an orthonormal basis for the columns space of this matrix.
-
nullSpace
Computes a orthonormal basis for the null space of this matrix. The null space of a matrix A is all vectors x that satisfy Ax =0 .- Returns:
- A matrix whose column vectors from an orthonormal basis for the null space of this matrix.
-
leftNullSpace
Computes an orthonormal basis of the left null space of this matrix. The left null space of a matrix A is all column vectors x which satisfy xTA=0T. This is equivalent to the thenull space
of AT.- Returns:
-
isPos
default boolean isPos()- Returns:
- True if matrix only contains positive real entries.
-
isNeg
default boolean isNeg()- Returns:
- True if matrix only contains negative entries.
-
isPosDef
default boolean isPosDef()Checks if matrix is positive-definite. An m-by-m hermation (or symmetric if real) matrix M is positive-definite if for all non-zero column vectors z, zHMz is positive where zH is the conjugate transpose of z.
Also seepositive-semidefinite
.- Returns:
- True if the matrix is positive-definite. Otherwise, returns false.
-
isPosSemidef
default boolean isPosSemidef()Checks if matrix is positive-semidefinite. An m-by-m hermation (or symmetric if real) matrix M is positive-definite if for all non-zero \ column vector z, z*Mz is non-negative where z* is the conjugate transpose of z.
Also seepositive-definite
.- Returns:
- True if the matrix is positive-semidefinite. Otherwise, returns false.
-
eig
Computes the eigenvalues and associated eigenvectors for a square matrix.
Also see
-eigVecs()
to compute just the eigenvectors.
-eigVals()
to compute just the eigenvalues. This is recommended if the eigenvectors are not needed as it will be faster.- Returns:
- Returns an array of two matrices. The first matrix is a row vector which contains the eigenvalues of A (no necessarily ordered but grouped by equality), repeated per there multiplicity. The columns of the second matrix are the eigenvectors of A associated with each eigenvalue in the first matrix. For repeated eigenvalues, each associated eigenvector in the second matrix is an associated eigenvector.
-
eigVecs
Computes the right eigenvectors of a matrix. This is done by first computing the Schur decomposition.
Also see
-eig()
to compute both eigenvalues and eigenvectors.
-eigVals()
to compute just the eigenvalues.- Returns:
- Returns a matrix containing the eigenvectors of this matrix as its column vectors.
-
eigVals
Computes the eigenvalues of a matrix. This is done by first computing the Schur decomposition.
Also see
-eig()
to compute both eigenvalues and eigenvectors.
-eigVecs()
to compute just the eigenvectors.- Returns:
- Returns a column vector containing the eigenvalues of this matrix.
-
isDiagonalizable
default boolean isDiagonalizable()Checks if a matrix is diagonalizable. A matrix B is diagonalizable if and only if the multiplicity for each eigenvalue is equivalent to the eigenspace for that eigenvalue.- Returns:
- True if the matrix is diagonalizable. Otherwise, returns false.
-
equals
Checks if two matrices are element-wise equivalent. If the two matrices are different shapes, this method will return false.- Parameters:
B
- - Matrix to compare equality with- Returns:
- True if both matrices are element-wise equivalent. Otherwise, false.
-
sameShape
Compares the dimensions of two matrices. Ifcode
is passed zero then this method will check if both matrices have the same number of rows and columns. Ifcode
is passed one then this method will check if both matrices have the same number of rows only. Ifcode
is passed two then this method will check if both matrices have the same number of columns only. To check rows against columns usematMultCheck(Matrix A, Matrix B)
- Parameters:
B
- - Another matrixcode
- - Choice for dimension(s) along which to check equivalence- Returns:
- - true if the dimensions specified by
code
are equvalent. Otherwise. returns false
-
sameShape
Compares the dimensions, rows and columns, of two matrices.- Parameters:
B
- - Another matrix- Returns:
- - True if the number of rows and number of column of the two matrices are both equal. Otherwise, returns false.
-
isZero
default boolean isZero()Checks if this matrix has only zero entries. If so, then the matrix is the so called zero matrix and is the additive identity for matrices of the same size.- Returns:
- Returns true if matrix has all zero elements. Otherwise, returns false.
-
isI
default boolean isI()Checks if a matrix is an identity matrix.- Returns:
- Returns true if the matrix in question is an identity matrix. Otherwise, returns false.
-
isInv
Checks if B is the inverse of this matrix.
Seematrix inverse
- Returns:
- True if B is the inverse of this matrix. Otherwise, returns false.
-