Package org.la4j
Class Matrix
- java.lang.Object
-
- org.la4j.Matrix
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.Double>
- Direct Known Subclasses:
DenseMatrix
,SparseMatrix
public abstract class Matrix extends java.lang.Object implements java.lang.Iterable<java.lang.Double>
A real matrix.
-
-
Field Summary
Fields Modifier and Type Field Description protected int
columns
private static java.lang.String
DEFAULT_COLUMNS_DELIMITER
private static java.text.NumberFormat
DEFAULT_FORMATTER
private static java.lang.String
DEFAULT_ROWS_DELIMITER
private static java.lang.String[]
INDENTS
protected int
rows
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Matrix
add(double value)
Adds givenvalue
(v) to every element of this matrix (A).Matrix
add(Matrix that)
Adds giventhat
matrix (B) to this matrix (A).abstract <T> T
apply(MatrixMatrixOperation<T> operation, Matrix that)
Pipes this matrix to a givenoperation
.abstract <T> T
apply(MatrixOperation<T> operation)
Pipes this matrix to a givenoperation
.abstract <T> T
apply(MatrixVectorOperation<T> operation, Vector that)
Pipes this matrix to a givenoperation
.Matrix
blank()
Creates the blank matrix (a zero matrix with same size) of this matrix.Matrix
blankOfColumns(int columns)
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:columns
.Matrix
blankOfRows(int rows)
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
.abstract Matrix
blankOfShape(int rows, int columns)
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
xcolumns
.static Matrix
block(Matrix a, Matrix b, Matrix c, Matrix d)
ColumnMajorMatrixIterator
columnMajorIterator()
Returns a column-major matrix iterator.int
columns()
Returns the number of columns of this matrix.static Matrix
constant(int rows, int columns, double constant)
Creates a constantMatrix
of the given shape andvalue
.Matrix
copy()
Copies this matrix.Matrix
copyOfColumns(int columns)
Copies this matrix into the new matrix with specified column dimension:columns
.Matrix
copyOfRows(int rows)
Copies this matrix into the new matrix with specified row dimension:rows
.abstract Matrix
copyOfShape(int rows, int columns)
Copies this matrix into the new matrix with specified dimensions:rows
andcolumns
.double
determinant()
Calculates the determinant of this matrix.static Matrix
diagonal(int size, double diagonal)
double
diagonalProduct()
Calculates the product of diagonal elements of this matrix.Matrix
divide(double value)
Divides every element of this matrix (A) by givenvalue
(v).void
each(MatrixProcedure procedure)
Applies givenprocedure
to each element of this matrix.void
eachInColumn(int j, VectorProcedure procedure)
Applies givenprocedure
to each element of specified column of this matrix.void
eachInRow(int i, VectorProcedure procedure)
Applies givenprocedure
to each element of specified row of this matrix.protected void
ensureDimensionsAreCorrect(int rows, int columns)
protected void
ensureIndexesAreInBounds(int i, int j)
boolean
equals(java.lang.Object o)
boolean
equals(Matrix matrix, double precision)
Returns true when matrix is equal to givenmatrix
with givenprecision
double
euclideanNorm()
Calculates an Euclidean norm of this matrix, a.k.a.protected void
fail(java.lang.String message)
double
fold(MatrixAccumulator accumulator)
Folds all elements of this matrix with givenaccumulator
.double
foldColumn(int j, VectorAccumulator accumulator)
Folds all elements of specified column in this matrix with givenaccumulator
.double[]
foldColumns(VectorAccumulator accumulator)
Folds all elements (in a column-by-column manner) of this matrix with givenaccumulator
.double
foldRow(int i, VectorAccumulator accumulator)
Folds all elements of specified row in this matrix with givenaccumulator
.double[]
foldRows(VectorAccumulator accumulator)
Folds all elements (in row-by-row manner) of this matrix with givenaccumulator
.static Matrix
from1DArray(int rows, int columns, double[] array)
Creates aMatrix
of the given 1Darray
w/o copying the underlying array.static Matrix
from2DArray(double[][] array)
Creates aMatrix
of the given 2Darray
w/o copying the underlying array.static Matrix
fromCSV(java.lang.String csv)
ParsesMatrix
from the given CSV string.static Matrix
fromMatrixMarket(java.lang.String mm)
ParsesMatrix
from the given Matrix Market string.abstract double
get(int i, int j)
Gets the specified element of this matrix.abstract Vector
getColumn(int j)
Copies the specified column of this matrix into the vector.abstract Vector
getRow(int i)
Copies the specified row of this matrix into the vector.Matrix
hadamardProduct(Matrix that)
Calculates the Hadamard (element-wise) product of this and giventhat
matrix.int
hashCode()
static Matrix
identity(int size)
Creates an identityMatrix
of the givensize
.private void
indent(java.lang.StringBuilder sb, int howMany)
double
infinityNorm()
Calculates an Infinity norm of this matrix.Matrix
insert(Matrix that)
Inserts a giventhat
(B) into this matrix (A).Matrix
insert(Matrix that, int rows, int columns)
Inserts a giventhat
matrix (B) into this matrix (A).Matrix
insert(Matrix that, int destRow, int destColumn, int rows, int columns)
Inserts a giventhat
matrix (B) into this matrix (A).Matrix
insert(Matrix that, int srcRow, int srcColumn, int destRow, int destColumn, int rows, int columns)
Inserts a giventhat
matrix (B) into this matrix (A).Matrix
insertColumn(int j, Vector column)
Adds one column to matrix.Matrix
insertRow(int i, Vector row)
Adds one row to matrix.boolean
is(AdvancedMatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.boolean
is(MatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.MatrixIterator
iterator()
Returns a matrix iterator.VectorIterator
iteratorOfColumn(int j)
Returns a vector iterator of the given column {code j}.VectorIterator
iteratorOfRow(int i)
Returns a vector iterator of the given row {code i}.Matrix
kroneckerProduct(Matrix that)
Calculates the Kronecker product of this matrix (A) and giventhat
matrix (B).double
manhattanNorm()
Calculates a Manhattan norm of this matrix, a.k.a.double
max()
Searches for the maximum value of the elements of this matrix.double
maxInColumn(int j)
Searches for the maximum value of specified column in this matrix.double
maxInRow(int i)
Searches for the maximum value of specified row in this matrix.double
min()
Searches for the minimum value of the elements of this matrix.double
minInColumn(int j)
Searches for the minimum value of specified column in this matrix.double
minInRow(int i)
Searches for the minimum value of specified row in this matrix.java.lang.String
mkString(java.lang.String rowsDelimiter, java.lang.String columnsDelimiter)
Converts this matrix into the string representation.java.lang.String
mkString(java.text.NumberFormat formatter)
Converts this matrix into the string representation.java.lang.String
mkString(java.text.NumberFormat formatter, java.lang.String rowsDelimiter, java.lang.String columnsDelimiter)
Converts this matrix into the string representation.Matrix
multiply(double value)
Scales this matrix by givenvalue
(v).Matrix
multiply(Matrix that)
Multiplies this matrix (A) by giventhat
matrix (B).Vector
multiply(Vector that)
Multiplies this matrix (A) by giventhat
vector (x).Matrix
multiplyByItsTranspose()
Multiplies this matrix by its transpose.boolean
non(AdvancedMatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.boolean
non(MatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.double
norm()
Calculates an Euclidean norm of this matrix, a.k.a.Matrix
power(int n)
Powers this matrix of given exponent {code n}.double
product()
Multiplies up all elements of this matrix.static Matrix
random(int rows, int columns, java.util.Random random)
static Matrix
randomSymmetric(int size, java.util.Random random)
Creates a random symmetricMatrix
of the givensize
.int
rank()
Calculates the rank of this matrix.Matrix
removeColumn(int j)
Removes one column from matrix.Matrix
removeFirstColumn()
Removes first column from matrix.Matrix
removeFirstRow()
Removes first row from matrix.Matrix
removeLastColumn()
Removes last column from matrix.Matrix
removeLastRow()
Removes last row from matrix.Matrix
removeRow(int i)
Removes one row from matrix.Matrix
rotate()
Rotates this matrix by 90 degrees to the right.RowMajorMatrixIterator
rowMajorIterator()
Returns a row-major matrix iterator.int
rows()
Returns the number of rows of this matrix.Matrix
select(int[] rowIndices, int[] columnIndices)
Returns a new matrix with the selected rows and columns.abstract void
set(int i, int j, double value)
Sets the specified element of this matrix to givenvalue
.void
setAll(double value)
Sets all elements of this matrix to the givenvalue
.void
setColumn(int j, double value)
Sets all elements of the specified column of this matrix to givenvalue
.void
setColumn(int j, Vector column)
Copies givencolumn
into the specified column of this matrix.void
setRow(int i, double value)
Sets all elements of the specified row of this matrix to givenvalue
.void
setRow(int i, Vector row)
Copies givenrow
into the specified row of this matrix.Matrix
shuffle()
Shuffles this matrix.Matrix
slice(int fromRow, int fromColumn, int untilRow, int untilColumn)
Retrieves the specified sub-matrix of this matrix.Matrix
sliceBottomRight(int fromRow, int fromColumn)
Retrieves the specified sub-matrix of this matrix.Matrix
sliceTopLeft(int untilRow, int untilColumn)
Retrieves the specified sub-matrix of this matrix.Matrix
subtract(double value)
Subtracts givenvalue
(v) from every element of this matrix (A).Matrix
subtract(Matrix that)
Subtracts giventhat
matrix (B) from this matrix (A).double
sum()
Summarizes up all elements of this matrix.void
swapColumns(int i, int j)
Swaps the specified columns of this matrix.void
swapRows(int i, int j)
Swaps the specified rows of this matrix.<T extends Matrix>
Tto(MatrixFactory<T> factory)
Converts this matrix using the givenfactory
.abstract byte[]
toBinary()
Encodes this matrix into a byte array.ColumnMajorSparseMatrix
toColumnMajorSparseMatrix()
Converts this matrix into a column-major sparse matrix.Vector
toColumnVector()
Converts this matrix into the column vector.java.lang.String
toCSV()
Converts this matrix into the CSV (Comma Separated Value) string.java.lang.String
toCSV(java.text.NumberFormat formatter)
Converts this matrix into the CSV (Comma Separated Value) string using the givenformatter
.DenseMatrix
toDenseMatrix()
Converts this matrix into a dense matrix.java.lang.String
toMatrixMarket()
Converts this matrix into the Matrix Market string.abstract java.lang.String
toMatrixMarket(java.text.NumberFormat formatter)
Converts this matrix into the Matrix Market string using the given numberformatter
.RowMajorSparseMatrix
toRowMajorSparseMatrix()
Converts this matrix into a row-major sparse matrix.Vector
toRowVector()
Converts this matrix into the row vector.SparseMatrix
toSparseMatrix()
Converts this matrix into a sparse matrix.java.lang.String
toString()
double
trace()
Calculates the trace of this matrix.Matrix
transform(MatrixFunction function)
Builds a new matrix by applying givenfunction
to each element of this matrix.Matrix
transformColumn(int j, VectorFunction function)
Builds a new matrix by applying givenfunction
to each element of specified column in this matrix.Matrix
transformRow(int i, VectorFunction function)
Builds a new matrix by applying givenfunction
to each element of specified row in this matrix.Matrix
transpose()
Transposes this matrix.static Matrix
unit(int rows, int columns)
void
update(MatrixFunction function)
Updates all elements of this matrix by applying givenfunction
.void
updateAt(int i, int j, MatrixFunction function)
Updates the specified element of this matrix by applying givenfunction
.void
updateColumn(int j, VectorFunction function)
Updates all elements of the specified column in this matrix by applying givenfunction
.void
updateRow(int i, VectorFunction function)
Updates all elements of the specified row in this matrix by applying givenfunction
.MatrixDecompositor
withDecompositor(LinearAlgebra.DecompositorFactory factory)
Creates a new decompositor by givenfactory
of this matrix.MatrixInverter
withInverter(LinearAlgebra.InverterFactory factory)
Creates a new inverter by givenfactory
of this matrix.LinearSystemSolver
withSolver(LinearAlgebra.SolverFactory factory)
Creates a new solver by givenfactory
of this matrix.static Matrix
zero(int rows, int columns)
-
-
-
Field Detail
-
DEFAULT_ROWS_DELIMITER
private static final java.lang.String DEFAULT_ROWS_DELIMITER
- See Also:
- Constant Field Values
-
DEFAULT_COLUMNS_DELIMITER
private static final java.lang.String DEFAULT_COLUMNS_DELIMITER
- See Also:
- Constant Field Values
-
DEFAULT_FORMATTER
private static final java.text.NumberFormat DEFAULT_FORMATTER
-
INDENTS
private static final java.lang.String[] INDENTS
-
rows
protected int rows
-
columns
protected int columns
-
-
Method Detail
-
zero
public static Matrix zero(int rows, int columns)
-
constant
public static Matrix constant(int rows, int columns, double constant)
Creates a constantMatrix
of the given shape andvalue
.
-
diagonal
public static Matrix diagonal(int size, double diagonal)
-
unit
public static Matrix unit(int rows, int columns)
-
random
public static Matrix random(int rows, int columns, java.util.Random random)
-
randomSymmetric
public static Matrix randomSymmetric(int size, java.util.Random random)
Creates a random symmetricMatrix
of the givensize
.
-
from1DArray
public static Matrix from1DArray(int rows, int columns, double[] array)
Creates aMatrix
of the given 1Darray
w/o copying the underlying array.
-
from2DArray
public static Matrix from2DArray(double[][] array)
Creates aMatrix
of the given 2Darray
w/o copying the underlying array.
-
fromCSV
public static Matrix fromCSV(java.lang.String csv)
ParsesMatrix
from the given CSV string.- Parameters:
csv
- the string in CSV format- Returns:
- a parsed matrix
-
fromMatrixMarket
public static Matrix fromMatrixMarket(java.lang.String mm)
ParsesMatrix
from the given Matrix Market string.- Parameters:
mm
- the string in Matrix Market format- Returns:
- a parsed matrix
-
get
public abstract double get(int i, int j)
Gets the specified element of this matrix.- Parameters:
i
- element's row indexj
- element's column index- Returns:
- the element of this matrix
-
set
public abstract void set(int i, int j, double value)
Sets the specified element of this matrix to givenvalue
.- Parameters:
i
- element's row indexj
- element's column indexvalue
- element's new value
-
getRow
public abstract Vector getRow(int i)
Copies the specified row of this matrix into the vector.- Parameters:
i
- the row index- Returns:
- the row represented as vector
-
getColumn
public abstract Vector getColumn(int j)
Copies the specified column of this matrix into the vector.- Parameters:
j
- the column index- Returns:
- the column represented as vector
-
blankOfShape
public abstract Matrix blankOfShape(int rows, int columns)
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
xcolumns
.- Returns:
- blank matrix
-
copyOfShape
public abstract Matrix copyOfShape(int rows, int columns)
Copies this matrix into the new matrix with specified dimensions:rows
andcolumns
.- Parameters:
rows
- the number of rows in new matrixcolumns
- the number of columns in new matrix- Returns:
- the copy of this matrix with new size
-
apply
public abstract <T> T apply(MatrixOperation<T> operation)
Pipes this matrix to a givenoperation
.- Type Parameters:
T
- the result type- Parameters:
operation
- the matrix operation (an operation that takes a matrix and returnsT
)- Returns:
- the result of an operation applied to this matrix
-
apply
public abstract <T> T apply(MatrixMatrixOperation<T> operation, Matrix that)
Pipes this matrix to a givenoperation
.- Type Parameters:
T
- the result type- Parameters:
operation
- the matrix-matrix operation (an operation that takes two matrices and returnsT
)that
- the right hand matrix of the given operation- Returns:
- the result of an operation applied to this matrix
-
apply
public abstract <T> T apply(MatrixVectorOperation<T> operation, Vector that)
Pipes this matrix to a givenoperation
.- Type Parameters:
T
- the result type- Parameters:
operation
- the matrix-vector operation (an operation that takes matrix and vector and returnsT
)that
- the right hand vector of the given operation- Returns:
- the result of an operation applied to this matrix
-
toBinary
public abstract byte[] toBinary()
Encodes this matrix into a byte array.- Returns:
- a byte array representing this matrix
-
toMatrixMarket
public abstract java.lang.String toMatrixMarket(java.text.NumberFormat formatter)
Converts this matrix into the Matrix Market string using the given numberformatter
.- Returns:
- a string in Matrix Market format representing this matrix;
-
setAll
public void setAll(double value)
Sets all elements of this matrix to the givenvalue
.- Parameters:
value
- the element's new value
-
setRow
public void setRow(int i, double value)
Sets all elements of the specified row of this matrix to given
value
.- Parameters:
i
- the row indexvalue
- the element's new value
-
setColumn
public void setColumn(int j, double value)
Sets all elements of the specified column of this matrix to given
value
.- Parameters:
j
- the column indexvalue
- the element's new value
-
swapRows
public void swapRows(int i, int j)
Swaps the specified rows of this matrix.- Parameters:
i
- the row indexj
- the row index
-
swapColumns
public void swapColumns(int i, int j)
Swaps the specified columns of this matrix.- Parameters:
i
- the column indexj
- the column index
-
rows
public int rows()
Returns the number of rows of this matrix.- Returns:
- the number of rows
-
columns
public int columns()
Returns the number of columns of this matrix.- Returns:
- the number of columns
-
transpose
public Matrix transpose()
Transposes this matrix.- Returns:
- the transposed matrix
-
rotate
public Matrix rotate()
Rotates this matrix by 90 degrees to the right.- Returns:
- the rotated matrix
-
power
public Matrix power(int n)
Powers this matrix of given exponent {code n}.- Parameters:
n
- the exponent- Returns:
- the powered matrix
-
multiply
public Matrix multiply(double value)
Scales this matrix by givenvalue
(v).- Parameters:
value
- the scale factor- Returns:
- A * v
-
multiply
public Vector multiply(Vector that)
Multiplies this matrix (A) by giventhat
vector (x).- Parameters:
that
- the vector- Returns:
- A * x
-
multiply
public Matrix multiply(Matrix that)
Multiplies this matrix (A) by giventhat
matrix (B).- Parameters:
that
- the right hand matrix for multiplication- Returns:
- A * B
-
multiplyByItsTranspose
public Matrix multiplyByItsTranspose()
Multiplies this matrix by its transpose.- Returns:
- this matrix multiplied by its transpose
-
subtract
public Matrix subtract(double value)
Subtracts givenvalue
(v) from every element of this matrix (A).- Parameters:
value
- the right hand value for subtraction- Returns:
- A - v
-
subtract
public Matrix subtract(Matrix that)
Subtracts giventhat
matrix (B) from this matrix (A).- Parameters:
that
- the right hand matrix for subtraction- Returns:
- A - B
-
add
public Matrix add(double value)
Adds givenvalue
(v) to every element of this matrix (A).- Parameters:
value
- the right hand value for addition- Returns:
- A + v
-
add
public Matrix add(Matrix that)
Adds giventhat
matrix (B) to this matrix (A).- Parameters:
that
- the right hand matrix for addition- Returns:
- A + B
-
insert
public Matrix insert(Matrix that)
Inserts a giventhat
(B) into this matrix (A). The original values are overwritten by the new ones.- Parameters:
that
- the matrix to insert, from the first row and column- Returns:
- a matrix with the parameter inserted into it
-
insert
public Matrix insert(Matrix that, int rows, int columns)
Inserts a giventhat
matrix (B) into this matrix (A). The original values are overwritten by the new ones.- Parameters:
that
- the matrix to insertrows
- number of rows to insertcolumns
- number of columns to insert- Returns:
- a matrix with the parameter inserted into it
-
insert
public Matrix insert(Matrix that, int destRow, int destColumn, int rows, int columns)
Inserts a giventhat
matrix (B) into this matrix (A). The original values are overwritten by the new ones.- Parameters:
that
- the matrix to insertdestRow
- the row to insert at in the destination matrixdestColumn
- the column to insert at in the destination matrixrows
- number of rows to insertcolumns
- number of columns to insert- Returns:
- a matrix with the parameter inserted into it
-
insert
public Matrix insert(Matrix that, int srcRow, int srcColumn, int destRow, int destColumn, int rows, int columns)
Inserts a giventhat
matrix (B) into this matrix (A). The original values are overwritten by the new ones.- Parameters:
that
- the matrix to insertsrcRow
- the row to start at in the source matrixsrcColumn
- the column to start at in the source matrixdestRow
- the row to insert at in the destination matrixdestColumn
- the column to insert at in the destination matrixrows
- number of rows to insertcolumns
- number of columns to insert- Returns:
- a matrix with the parameter inserted into it
-
divide
public Matrix divide(double value)
Divides every element of this matrix (A) by givenvalue
(v).- Parameters:
value
- the right hand value for division- Returns:
- A / v
-
kroneckerProduct
public Matrix kroneckerProduct(Matrix that)
Calculates the Kronecker product of this matrix (A) and giventhat
matrix (B).- Parameters:
that
- the right hand matrix for Kronecker product- Returns:
- A (+) B
-
trace
public double trace()
Calculates the trace of this matrix.See http://mathworld.wolfram.com/MatrixTrace.html for more details.
- Returns:
- the trace of this matrix
-
diagonalProduct
public double diagonalProduct()
Calculates the product of diagonal elements of this matrix.- Returns:
- the product of diagonal elements of this matrix
-
norm
public double norm()
Calculates an Euclidean norm of this matrix, a.k.a. frobenius norm- Returns:
- an Euclidean norm
-
euclideanNorm
public double euclideanNorm()
Calculates an Euclidean norm of this matrix, a.k.a. frobenius norm- Returns:
- an Euclidean norm
-
manhattanNorm
public double manhattanNorm()
Calculates a Manhattan norm of this matrix, a.k.a. taxicab norm- Returns:
- a Manhattan norm
-
infinityNorm
public double infinityNorm()
Calculates an Infinity norm of this matrix.- Returns:
- an Infinity norm
-
product
public double product()
Multiplies up all elements of this matrix.- Returns:
- the product of all elements of this matrix
-
sum
public double sum()
Summarizes up all elements of this matrix.- Returns:
- the sum of all elements of this matrix
-
hadamardProduct
public Matrix hadamardProduct(Matrix that)
Calculates the Hadamard (element-wise) product of this and giventhat
matrix.- Parameters:
that
- the right hand matrix for Hadamard product- Returns:
- the Hadamard product of two matrices
-
determinant
public double determinant()
Calculates the determinant of this matrix.See http://mathworld.wolfram.com/Determinant.html for more details.
- Returns:
- the determinant of this matrix
-
rank
public int rank()
Calculates the rank of this matrix.See http://mathworld.wolfram.com/MatrixRank.html for more details.
- Returns:
- the rank of this matrix
-
setRow
public void setRow(int i, Vector row)
Copies givenrow
into the specified row of this matrix.- Parameters:
i
- the row indexrow
- the row represented as vector
-
setColumn
public void setColumn(int j, Vector column)
Copies givencolumn
into the specified column of this matrix.- Parameters:
j
- the column indexcolumn
- the column represented as vector
-
insertRow
public Matrix insertRow(int i, Vector row)
Adds one row to matrix.- Parameters:
i
- the row index- Returns:
- matrix with row.
-
insertColumn
public Matrix insertColumn(int j, Vector column)
Adds one column to matrix.- Parameters:
j
- the column index- Returns:
- matrix with column.
-
removeRow
public Matrix removeRow(int i)
Removes one row from matrix.- Parameters:
i
- the row index- Returns:
- matrix without row.
-
removeColumn
public Matrix removeColumn(int j)
Removes one column from matrix.- Parameters:
j
- the column index- Returns:
- matrix without column.
-
removeFirstRow
public Matrix removeFirstRow()
Removes first row from matrix.- Returns:
- matrix without first row.
-
removeFirstColumn
public Matrix removeFirstColumn()
Removes first column from matrix.- Returns:
- matrix without first column
-
removeLastRow
public Matrix removeLastRow()
Removes last row from matrix.- Returns:
- matrix without last row
-
removeLastColumn
public Matrix removeLastColumn()
Removes last column from matrix.- Returns:
- matrix without last column
-
blank
public Matrix blank()
Creates the blank matrix (a zero matrix with same size) of this matrix.- Returns:
- blank matrix
-
blankOfRows
public Matrix blankOfRows(int rows)
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:rows
. Thecolumns
number remains the same.- Returns:
- blank matrix
-
blankOfColumns
public Matrix blankOfColumns(int columns)
Creates the blank matrix (a zero matrix with same size) of this matrix of the given shape:columns
. Therows
number remains the same.- Returns:
- blank matrix
-
copy
public Matrix copy()
Copies this matrix.- Returns:
- the copy of this matrix
-
copyOfRows
public Matrix copyOfRows(int rows)
Copies this matrix into the new matrix with specified row dimension:rows
.- Parameters:
rows
- the number of rows in new matrix- Returns:
- the copy of this matrix with new size
-
copyOfColumns
public Matrix copyOfColumns(int columns)
Copies this matrix into the new matrix with specified column dimension:columns
.- Parameters:
columns
- the number of columns in new matrix- Returns:
- the copy of this matrix with new size
-
shuffle
public Matrix shuffle()
Shuffles this matrix.Copies this matrix into the matrix that contains the same elements but with the elements shuffled around (which might also result in the same matrix (with a small likelihood)).
- Returns:
- the shuffled matrix
-
slice
public Matrix slice(int fromRow, int fromColumn, int untilRow, int untilColumn)
Retrieves the specified sub-matrix of this matrix. The sub-matrix is specified by intervals for row indices and column indices.- Parameters:
fromRow
- the beginning of the row indices intervalfromColumn
- the beginning of the column indices intervaluntilRow
- the ending of the row indices intervaluntilColumn
- the ending of the column indices interval- Returns:
- the sub-matrix of this matrix
-
sliceTopLeft
public Matrix sliceTopLeft(int untilRow, int untilColumn)
Retrieves the specified sub-matrix of this matrix. The sub-matrix is specified by intervals for row indices and column indices. The top left points of both intervals are fixed to zero.- Parameters:
untilRow
- the ending of the row indices intervaluntilColumn
- the ending of the column indices interval- Returns:
- the sub-matrix of this matrix
-
sliceBottomRight
public Matrix sliceBottomRight(int fromRow, int fromColumn)
Retrieves the specified sub-matrix of this matrix. The sub-matrix is specified by intervals for row indices and column indices. The bottom right points of both intervals are fixed to matrix dimensions - it's rows and columns correspondingly.- Parameters:
fromRow
- the beginning of the row indices intervalfromColumn
- the beginning of the column indices interval- Returns:
- the sub-matrix of this matrix
-
select
public Matrix select(int[] rowIndices, int[] columnIndices)
Returns a new matrix with the selected rows and columns. This method can be used either return a specific subset of rows and/or columns or to permute the indices in an arbitrary order. The list of indices are allowed to contain duplicates indices. This is more general than slice() which selects only contiguous blocks. However, where applicable slice() is probably more efficient.- Parameters:
rowIndices
- the array of row indicescolumnIndices
- the array of column indices- Returns:
- the new matrix with the selected rows and columns
- Throws:
java.lang.IllegalArgumentException
- if invalid row or column indices are provided
-
each
public void each(MatrixProcedure procedure)
Applies givenprocedure
to each element of this matrix.- Parameters:
procedure
- the matrix procedure
-
eachInRow
public void eachInRow(int i, VectorProcedure procedure)
Applies givenprocedure
to each element of specified row of this matrix.- Parameters:
i
- the row indexprocedure
- the vector procedure
-
eachInColumn
public void eachInColumn(int j, VectorProcedure procedure)
Applies givenprocedure
to each element of specified column of this matrix.- Parameters:
j
- the column indexprocedure
- the vector procedure
-
max
public double max()
Searches for the maximum value of the elements of this matrix.- Returns:
- maximum value of this matrix
-
min
public double min()
Searches for the minimum value of the elements of this matrix.- Returns:
- minimum value of this matrix
-
maxInRow
public double maxInRow(int i)
Searches for the maximum value of specified row in this matrix.- Parameters:
i
- the row index- Returns:
- maximum value of specified row in this matrix
-
minInRow
public double minInRow(int i)
Searches for the minimum value of specified row in this matrix.- Parameters:
i
- the row index- Returns:
- minimum value of specified row in this matrix
-
maxInColumn
public double maxInColumn(int j)
Searches for the maximum value of specified column in this matrix.- Parameters:
j
- the column index- Returns:
- maximum value of specified column in this matrix
-
minInColumn
public double minInColumn(int j)
Searches for the minimum value of specified column in this matrix.- Parameters:
j
- the column index- Returns:
- minimum value of specified column in this matrix
-
transform
public Matrix transform(MatrixFunction function)
Builds a new matrix by applying givenfunction
to each element of this matrix.- Parameters:
function
- the matrix function- Returns:
- the transformed matrix
-
transformRow
public Matrix transformRow(int i, VectorFunction function)
Builds a new matrix by applying givenfunction
to each element of specified row in this matrix.- Parameters:
i
- the row indexfunction
- the vector function- Returns:
- the transformed matrix
-
transformColumn
public Matrix transformColumn(int j, VectorFunction function)
Builds a new matrix by applying givenfunction
to each element of specified column in this matrix.- Parameters:
j
- the column indexfunction
- the vector function- Returns:
- the transformed matrix
-
update
public void update(MatrixFunction function)
Updates all elements of this matrix by applying givenfunction
.- Parameters:
function
- the matrix function
-
updateAt
public void updateAt(int i, int j, MatrixFunction function)
Updates the specified element of this matrix by applying givenfunction
.- Parameters:
i
- the row indexj
- the column indexfunction
- the matrix function
-
updateRow
public void updateRow(int i, VectorFunction function)
Updates all elements of the specified row in this matrix by applying givenfunction
.- Parameters:
i
- the row indexfunction
- the vector function
-
updateColumn
public void updateColumn(int j, VectorFunction function)
Updates all elements of the specified column in this matrix by applying givenfunction
.- Parameters:
j
- the column indexfunction
- the vector function
-
fold
public double fold(MatrixAccumulator accumulator)
Folds all elements of this matrix with givenaccumulator
.- Parameters:
accumulator
- the matrix accumulator- Returns:
- the accumulated value
-
foldRow
public double foldRow(int i, VectorAccumulator accumulator)
Folds all elements of specified row in this matrix with givenaccumulator
.- Parameters:
i
- the row indexaccumulator
- the vector accumulator- Returns:
- the accumulated value
-
foldRows
public double[] foldRows(VectorAccumulator accumulator)
Folds all elements (in row-by-row manner) of this matrix with givenaccumulator
.- Parameters:
accumulator
- the vector accumulator- Returns:
- the accumulated double array
-
foldColumn
public double foldColumn(int j, VectorAccumulator accumulator)
Folds all elements of specified column in this matrix with givenaccumulator
.- Parameters:
j
- the column indexaccumulator
- the vector accumulator- Returns:
- the accumulated value
-
foldColumns
public double[] foldColumns(VectorAccumulator accumulator)
Folds all elements (in a column-by-column manner) of this matrix with givenaccumulator
.- Parameters:
accumulator
- the vector accumulator- Returns:
- the accumulated double array
-
is
public boolean is(MatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.- Parameters:
predicate
- the matrix predicate- Returns:
- whether this matrix compiles with predicate
-
is
public boolean is(AdvancedMatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.- Parameters:
predicate
- the advanced matrix predicate- Returns:
- whether this matrix compiles with predicate
-
non
public boolean non(MatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.- Parameters:
predicate
- the matrix predicate- Returns:
- whether this matrix compiles with predicate
-
non
public boolean non(AdvancedMatrixPredicate predicate)
Checks whether this matrix compiles with givenpredicate
or not.- Parameters:
predicate
- the advanced matrix predicate- Returns:
- whether this matrix compiles with predicate
-
toRowVector
public Vector toRowVector()
Converts this matrix into the row vector.- Returns:
- the row vector of this matrix
-
toColumnVector
public Vector toColumnVector()
Converts this matrix into the column vector.- Returns:
- the column vector of this matrix
-
withSolver
public LinearSystemSolver withSolver(LinearAlgebra.SolverFactory factory)
Creates a new solver by givenfactory
of this matrix.- Parameters:
factory
- the solver factory- Returns:
- the linear system solver of this matrix
-
withInverter
public MatrixInverter withInverter(LinearAlgebra.InverterFactory factory)
Creates a new inverter by givenfactory
of this matrix.- Parameters:
factory
- the inverter factory- Returns:
- the inverter of this matrix
-
withDecompositor
public MatrixDecompositor withDecompositor(LinearAlgebra.DecompositorFactory factory)
Creates a new decompositor by givenfactory
of this matrix.- Parameters:
factory
- the decompositor factory- Returns:
- the decompositor of this matrix
-
equals
public boolean equals(Matrix matrix, double precision)
Returns true when matrix is equal to givenmatrix
with givenprecision
- Parameters:
matrix
- matrixprecision
- given precision- Returns:
- equals of this matrix to that
-
mkString
public java.lang.String mkString(java.text.NumberFormat formatter)
Converts this matrix into the string representation.- Parameters:
formatter
- the number formatter- Returns:
- the matrix converted to a string
-
mkString
public java.lang.String mkString(java.lang.String rowsDelimiter, java.lang.String columnsDelimiter)
Converts this matrix into the string representation.- Parameters:
rowsDelimiter
- the rows' delimitercolumnsDelimiter
- the columns' delimiter- Returns:
- the matrix converted to a string
-
mkString
public java.lang.String mkString(java.text.NumberFormat formatter, java.lang.String rowsDelimiter, java.lang.String columnsDelimiter)
Converts this matrix into the string representation.- Parameters:
formatter
- the number formatterrowsDelimiter
- the rows' delimitercolumnsDelimiter
- the columns' delimiter- Returns:
- the matrix converted to a string
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
iterator
public MatrixIterator iterator()
Returns a matrix iterator.- Specified by:
iterator
in interfacejava.lang.Iterable<java.lang.Double>
- Returns:
- a matrix iterator
-
rowMajorIterator
public RowMajorMatrixIterator rowMajorIterator()
Returns a row-major matrix iterator.- Returns:
- a row-major matrix iterator.
-
columnMajorIterator
public ColumnMajorMatrixIterator columnMajorIterator()
Returns a column-major matrix iterator.- Returns:
- a column-major matrix iterator.
-
iteratorOfRow
public VectorIterator iteratorOfRow(int i)
Returns a vector iterator of the given row {code i}.- Returns:
- a vector iterator
-
iteratorOfColumn
public VectorIterator iteratorOfColumn(int j)
Returns a vector iterator of the given column {code j}.- Returns:
- a vector iterator
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
to
public <T extends Matrix> T to(MatrixFactory<T> factory)
Converts this matrix using the givenfactory
.- Type Parameters:
T
- type of the result matrix- Parameters:
factory
- the factory that creates an output matrix- Returns:
- converted matrix
-
toSparseMatrix
public SparseMatrix toSparseMatrix()
Converts this matrix into a sparse matrix.- Returns:
- a sparse matrix
-
toDenseMatrix
public DenseMatrix toDenseMatrix()
Converts this matrix into a dense matrix.- Returns:
- a dense matrix
-
toRowMajorSparseMatrix
public RowMajorSparseMatrix toRowMajorSparseMatrix()
Converts this matrix into a row-major sparse matrix.- Returns:
- a row-major sparse matrix
-
toColumnMajorSparseMatrix
public ColumnMajorSparseMatrix toColumnMajorSparseMatrix()
Converts this matrix into a column-major sparse matrix.- Returns:
- a row-major sparse matrix
-
toCSV
public java.lang.String toCSV()
Converts this matrix into the CSV (Comma Separated Value) string.- Returns:
- a CSV string representing this matrix
-
toMatrixMarket
public java.lang.String toMatrixMarket()
Converts this matrix into the Matrix Market string.- Returns:
- a string in Matrix Market format representing this matrix;
-
toCSV
public java.lang.String toCSV(java.text.NumberFormat formatter)
Converts this matrix into the CSV (Comma Separated Value) string using the givenformatter
.- Parameters:
formatter
- the number formatter- Returns:
- a CSV string representing this matrix
-
ensureDimensionsAreCorrect
protected void ensureDimensionsAreCorrect(int rows, int columns)
-
ensureIndexesAreInBounds
protected void ensureIndexesAreInBounds(int i, int j)
-
fail
protected void fail(java.lang.String message)
-
indent
private void indent(java.lang.StringBuilder sb, int howMany)
-
-