public abstract class Matrix extends Object implements Iterable<Double>
Modifier and Type | Field and Description |
---|---|
protected int |
columns |
protected int |
rows |
Constructor and Description |
---|
Matrix()
Creates a zero-shape matrix.
|
Matrix(int rows,
int columns)
Creates a matrix of given shape
rows x columns ; |
Modifier and Type | Method and Description |
---|---|
Matrix |
add(double value)
Adds given
value (v) to every element of this matrix (A). |
Matrix |
add(Matrix that)
Adds given
that matrix (B) to this matrix (A). |
abstract <T> T |
apply(MatrixMatrixOperation<T> operation,
Matrix that)
Pipes this matrix to a given
operation . |
abstract <T> T |
apply(MatrixOperation<T> operation)
Pipes this matrix to a given
operation . |
abstract <T> T |
apply(MatrixVectorOperation<T> operation,
Vector that)
Pipes this matrix to a given
operation . |
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 x columns . |
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 constant
Matrix of the given shape and value . |
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 and columns . |
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 given
value (v). |
void |
each(MatrixProcedure procedure)
Applies given
procedure to each element of this matrix. |
void |
eachInColumn(int j,
VectorProcedure procedure)
Applies given
procedure to each element of specified column of this matrix. |
void |
eachInRow(int i,
VectorProcedure procedure)
Applies given
procedure 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(Matrix matrix,
double precision)
Returns true when matrix is equal to given
matrix with given precision |
boolean |
equals(Object o) |
double |
euclideanNorm()
Calculates an Euclidean norm of this matrix, a.k.a.
|
protected void |
fail(String message) |
double |
fold(MatrixAccumulator accumulator)
Folds all elements of this matrix with given
accumulator . |
double |
foldColumn(int j,
VectorAccumulator accumulator)
Folds all elements of specified column in this matrix with given
accumulator . |
double[] |
foldColumns(VectorAccumulator accumulator)
Folds all elements (in a column-by-column manner) of this matrix with given
accumulator . |
double |
foldRow(int i,
VectorAccumulator accumulator)
Folds all elements of specified row in this matrix with given
accumulator . |
double[] |
foldRows(VectorAccumulator accumulator)
Folds all elements (in row-by-row manner) of this matrix with given
accumulator . |
static Matrix |
from1DArray(int rows,
int columns,
double[] array)
Creates a
Matrix of the given 1D array w/o
copying the underlying array. |
static Matrix |
from2DArray(double[][] array)
Creates a
Matrix of the given 2D array w/o
copying the underlying array. |
static Matrix |
fromCSV(String csv)
Parses
Matrix from the given CSV string. |
static Matrix |
fromMatrixMarket(String mm)
Parses
Matrix 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 given
that matrix. |
int |
hashCode() |
static Matrix |
identity(int size)
Creates an identity
Matrix of the given size . |
double |
infinityNorm()
Calculates an Infinity norm of this matrix.
|
Matrix |
insert(Matrix that)
Inserts a given
that (B) into this matrix (A). |
Matrix |
insert(Matrix that,
int rows,
int columns)
Inserts a given
that matrix (B) into this matrix (A). |
Matrix |
insert(Matrix that,
int destRow,
int destColumn,
int rows,
int columns)
Inserts a given
that matrix (B) into this matrix (A). |
Matrix |
insert(Matrix that,
int srcRow,
int srcColumn,
int destRow,
int destColumn,
int rows,
int columns)
Inserts a given
that 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 given
predicate or not. |
boolean |
is(MatrixPredicate predicate)
Checks whether this matrix compiles with given
predicate 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 given
that 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.
|
String |
mkString(NumberFormat formatter)
Converts this matrix into the string representation.
|
String |
mkString(NumberFormat formatter,
String rowsDelimiter,
String columnsDelimiter)
Converts this matrix into the string representation.
|
String |
mkString(String rowsDelimiter,
String columnsDelimiter)
Converts this matrix into the string representation.
|
Matrix |
multiply(double value)
Scales this matrix by given
value (v). |
Matrix |
multiply(Matrix that)
Multiplies this matrix (A) by given
that matrix (B). |
Vector |
multiply(Vector that)
Multiplies this matrix (A) by given
that vector (x). |
Matrix |
multiplyByItsTranspose()
Multiplies this matrix by its transpose.
|
boolean |
non(AdvancedMatrixPredicate predicate)
Checks whether this matrix compiles with given
predicate or not. |
boolean |
non(MatrixPredicate predicate)
Checks whether this matrix compiles with given
predicate 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,
Random random)
|
static Matrix |
randomSymmetric(int size,
Random random)
Creates a random symmetric
Matrix of the given size . |
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 given
value . |
void |
setAll(double value)
Sets all elements of this matrix to the given
value . |
void |
setColumn(int j,
double value)
Sets all elements of the specified column of this matrix to given
value . |
void |
setColumn(int j,
Vector column)
Copies given
column into the specified column of this matrix. |
void |
setRow(int i,
double value)
Sets all elements of the specified row of this matrix to given
value . |
void |
setRow(int i,
Vector row)
Copies given
row 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 given
value (v) from every element of this matrix (A). |
Matrix |
subtract(Matrix that)
Subtracts given
that 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> |
to(MatrixFactory<T> factory)
Converts this matrix using the given
factory . |
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.
|
String |
toCSV()
Converts this matrix into the CSV (Comma Separated Value) string.
|
String |
toCSV(NumberFormat formatter)
Converts this matrix into the CSV (Comma Separated Value) string
using the given
formatter . |
DenseMatrix |
toDenseMatrix()
Converts this matrix into a dense matrix.
|
String |
toMatrixMarket()
Converts this matrix into the Matrix Market string.
|
abstract String |
toMatrixMarket(NumberFormat formatter)
Converts this matrix into the Matrix Market string using the
given number
formatter . |
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.
|
String |
toString() |
double |
trace()
Calculates the trace of this matrix.
|
Matrix |
transform(MatrixFunction function)
Builds a new matrix by applying given
function to each element of this matrix. |
Matrix |
transformColumn(int j,
VectorFunction function)
Builds a new matrix by applying given
function to each element of specified
column in this matrix. |
Matrix |
transformRow(int i,
VectorFunction function)
Builds a new matrix by applying given
function 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 given
function . |
void |
updateAt(int i,
int j,
MatrixFunction function)
Updates the specified element of this matrix by applying given
function . |
void |
updateColumn(int j,
VectorFunction function)
Updates all elements of the specified column in this matrix by applying given
function . |
void |
updateRow(int i,
VectorFunction function)
Updates all elements of the specified row in this matrix by applying given
function . |
MatrixDecompositor |
withDecompositor(LinearAlgebra.DecompositorFactory factory)
Creates a new decompositor by given
factory of this matrix. |
MatrixInverter |
withInverter(LinearAlgebra.InverterFactory factory)
Creates a new inverter by given
factory of this matrix. |
LinearSystemSolver |
withSolver(LinearAlgebra.SolverFactory factory)
Creates a new solver by given
factory of this matrix. |
static Matrix |
zero(int rows,
int columns)
|
public Matrix()
public Matrix(int rows, int columns)
rows
x columns
;public static Matrix zero(int rows, int columns)
public static Matrix constant(int rows, int columns, double constant)
Matrix
of the given shape and value
.public static Matrix diagonal(int size, double diagonal)
public static Matrix unit(int rows, int columns)
public static Matrix randomSymmetric(int size, Random random)
Matrix
of the given size
.public static Matrix from1DArray(int rows, int columns, double[] array)
Matrix
of the given 1D array
w/o
copying the underlying array.public static Matrix from2DArray(double[][] array)
Matrix
of the given 2D array
w/o
copying the underlying array.public static Matrix fromCSV(String csv)
Matrix
from the given CSV string.csv
- the string in CSV formatpublic static Matrix fromMatrixMarket(String mm)
Matrix
from the given Matrix Market string.mm
- the string in Matrix Market formatpublic abstract double get(int i, int j)
i
- element's row indexj
- element's column indexpublic abstract void set(int i, int j, double value)
value
.i
- element's row indexj
- element's column indexvalue
- element's new valuepublic abstract Vector getRow(int i)
i
- the row indexpublic abstract Vector getColumn(int j)
j
- the column indexpublic abstract Matrix blankOfShape(int rows, int columns)
rows
x columns
.public abstract Matrix copyOfShape(int rows, int columns)
rows
and columns
.rows
- the number of rows in new matrixcolumns
- the number of columns in new matrixpublic abstract <T> T apply(MatrixOperation<T> operation)
operation
.T
- the result typeoperation
- the matrix operation
(an operation that takes a matrix and returns T
)public abstract <T> T apply(MatrixMatrixOperation<T> operation, Matrix that)
operation
.T
- the result typeoperation
- the matrix-matrix operation
(an operation that takes two matrices and returns T
)that
- the right hand matrix of the given operationpublic abstract <T> T apply(MatrixVectorOperation<T> operation, Vector that)
operation
.T
- the result typeoperation
- the matrix-vector operation
(an operation that takes matrix and vector and returns T
)that
- the right hand vector of the given operationpublic abstract byte[] toBinary()
public abstract String toMatrixMarket(NumberFormat formatter)
formatter
.public void setAll(double value)
value
.value
- the element's new valuepublic void setRow(int i, double value)
Sets all elements of the specified row of this matrix to given value
.
i
- the row indexvalue
- the element's new valuepublic void setColumn(int j, double value)
Sets all elements of the specified column of this matrix to given value
.
j
- the column indexvalue
- the element's new valuepublic void swapRows(int i, int j)
i
- the row indexj
- the row indexpublic void swapColumns(int i, int j)
i
- the column indexj
- the column indexpublic int rows()
public int columns()
public Matrix transpose()
public Matrix rotate()
public Matrix power(int n)
n
- the exponentpublic Matrix multiply(double value)
value
(v).value
- the scale factorpublic Vector multiply(Vector that)
that
vector (x).that
- the vectorpublic Matrix multiply(Matrix that)
that
matrix (B).that
- the right hand matrix for multiplicationpublic Matrix multiplyByItsTranspose()
public Matrix subtract(double value)
value
(v) from every element of this matrix (A).value
- the right hand value for subtractionpublic Matrix subtract(Matrix that)
that
matrix (B) from this matrix (A).that
- the right hand matrix for subtractionpublic Matrix add(double value)
value
(v) to every element of this matrix (A).value
- the right hand value for additionpublic Matrix add(Matrix that)
that
matrix (B) to this matrix (A).that
- the right hand matrix for additionpublic Matrix insert(Matrix that)
that
(B) into this matrix (A). The original
values are overwritten by the new ones.that
- the matrix to insert, from the first row and columnpublic Matrix insert(Matrix that, int rows, int columns)
that
matrix (B) into this matrix (A). The original
values are overwritten by the new ones.that
- the matrix to insertrows
- number of rows to insertcolumns
- number of columns to insertpublic Matrix insert(Matrix that, int destRow, int destColumn, int rows, int columns)
that
matrix (B) into this matrix (A). The original
values are overwritten by the new ones.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 insertpublic Matrix insert(Matrix that, int srcRow, int srcColumn, int destRow, int destColumn, int rows, int columns)
that
matrix (B) into this matrix (A). The original
values are overwritten by the new ones.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 insertpublic Matrix divide(double value)
value
(v).value
- the right hand value for divisionpublic Matrix kroneckerProduct(Matrix that)
that
matrix (B).that
- the right hand matrix for Kronecker productpublic double trace()
See http://mathworld.wolfram.com/MatrixTrace.html for more details.
public double diagonalProduct()
public double norm()
public double euclideanNorm()
public double manhattanNorm()
public double infinityNorm()
public double product()
public double sum()
public Matrix hadamardProduct(Matrix that)
that
matrix.that
- the right hand matrix for Hadamard productpublic double determinant()
See http://mathworld.wolfram.com/Determinant.html for more details.
public int rank()
See http://mathworld.wolfram.com/MatrixRank.html for more details.
public void setRow(int i, Vector row)
row
into the specified row of this matrix.i
- the row indexrow
- the row represented as vectorpublic void setColumn(int j, Vector column)
column
into the specified column of this matrix.j
- the column indexcolumn
- the column represented as vectorpublic Matrix insertRow(int i, Vector row)
i
- the row indexpublic Matrix insertColumn(int j, Vector column)
j
- the column indexpublic Matrix removeRow(int i)
i
- the row indexpublic Matrix removeColumn(int j)
j
- the column indexpublic Matrix removeFirstRow()
public Matrix removeFirstColumn()
public Matrix removeLastRow()
public Matrix removeLastColumn()
public Matrix blank()
public Matrix blankOfRows(int rows)
rows
. The columns
number remains the
same.public Matrix blankOfColumns(int columns)
columns
. The rows
number remains the
same.public Matrix copy()
public Matrix copyOfRows(int rows)
rows
.rows
- the number of rows in new matrixpublic Matrix copyOfColumns(int columns)
columns
.columns
- the number of columns in new matrixpublic Matrix shuffle()
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)).
public Matrix slice(int fromRow, int fromColumn, int untilRow, int untilColumn)
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 intervalpublic Matrix sliceTopLeft(int untilRow, int untilColumn)
untilRow
- the ending of the row indices intervaluntilColumn
- the ending of the column indices intervalpublic Matrix sliceBottomRight(int fromRow, int fromColumn)
fromRow
- the beginning of the row indices intervalfromColumn
- the beginning of the column indices intervalpublic Matrix select(int[] rowIndices, int[] columnIndices)
rowIndices
- the array of row indicescolumnIndices
- the array of column indicesIllegalArgumentException
- if invalid row or column indices are providedpublic void each(MatrixProcedure procedure)
procedure
to each element of this matrix.procedure
- the matrix procedurepublic void eachInRow(int i, VectorProcedure procedure)
procedure
to each element of specified row of this matrix.i
- the row indexprocedure
- the vector procedurepublic void eachInColumn(int j, VectorProcedure procedure)
procedure
to each element of specified column of this matrix.j
- the column indexprocedure
- the vector procedurepublic double max()
public double min()
public double maxInRow(int i)
i
- the row indexpublic double minInRow(int i)
i
- the row indexpublic double maxInColumn(int j)
j
- the column indexpublic double minInColumn(int j)
j
- the column indexpublic Matrix transform(MatrixFunction function)
function
to each element of this matrix.function
- the matrix functionpublic Matrix transformRow(int i, VectorFunction function)
function
to each element of specified
row in this matrix.i
- the row indexfunction
- the vector functionpublic Matrix transformColumn(int j, VectorFunction function)
function
to each element of specified
column in this matrix.j
- the column indexfunction
- the vector functionpublic void update(MatrixFunction function)
function
.function
- the matrix functionpublic void updateAt(int i, int j, MatrixFunction function)
function
.i
- the row indexj
- the column indexfunction
- the matrix functionpublic void updateRow(int i, VectorFunction function)
function
.i
- the row indexfunction
- the vector functionpublic void updateColumn(int j, VectorFunction function)
function
.j
- the column indexfunction
- the vector functionpublic double fold(MatrixAccumulator accumulator)
accumulator
.accumulator
- the matrix accumulatorpublic double foldRow(int i, VectorAccumulator accumulator)
accumulator
.i
- the row indexaccumulator
- the vector accumulatorpublic double[] foldRows(VectorAccumulator accumulator)
accumulator
.accumulator
- the vector accumulatorpublic double foldColumn(int j, VectorAccumulator accumulator)
accumulator
.j
- the column indexaccumulator
- the vector accumulatorpublic double[] foldColumns(VectorAccumulator accumulator)
accumulator
.accumulator
- the vector accumulatorpublic boolean is(MatrixPredicate predicate)
predicate
or not.predicate
- the matrix predicatepublic boolean is(AdvancedMatrixPredicate predicate)
predicate
or not.predicate
- the advanced matrix predicatepublic boolean non(MatrixPredicate predicate)
predicate
or not.predicate
- the matrix predicatepublic boolean non(AdvancedMatrixPredicate predicate)
predicate
or not.predicate
- the advanced matrix predicatepublic Vector toRowVector()
public Vector toColumnVector()
public LinearSystemSolver withSolver(LinearAlgebra.SolverFactory factory)
factory
of this matrix.factory
- the solver factorypublic MatrixInverter withInverter(LinearAlgebra.InverterFactory factory)
factory
of this matrix.factory
- the inverter factorypublic MatrixDecompositor withDecompositor(LinearAlgebra.DecompositorFactory factory)
factory
of this matrix.factory
- the decompositor factorypublic boolean equals(Matrix matrix, double precision)
matrix
with given precision
matrix
- matrixprecision
- given precisionpublic String mkString(NumberFormat formatter)
formatter
- the number formatterpublic String mkString(String rowsDelimiter, String columnsDelimiter)
rowsDelimiter
- the rows' delimitercolumnsDelimiter
- the columns' delimiterpublic String mkString(NumberFormat formatter, String rowsDelimiter, String columnsDelimiter)
formatter
- the number formatterrowsDelimiter
- the rows' delimitercolumnsDelimiter
- the columns' delimiterpublic MatrixIterator iterator()
public RowMajorMatrixIterator rowMajorIterator()
public ColumnMajorMatrixIterator columnMajorIterator()
public VectorIterator iteratorOfRow(int i)
public VectorIterator iteratorOfColumn(int j)
public <T extends Matrix> T to(MatrixFactory<T> factory)
factory
.T
- type of the result matrixfactory
- the factory that creates an output matrixpublic SparseMatrix toSparseMatrix()
public DenseMatrix toDenseMatrix()
public RowMajorSparseMatrix toRowMajorSparseMatrix()
public ColumnMajorSparseMatrix toColumnMajorSparseMatrix()
public String toCSV()
public String toMatrixMarket()
public String toCSV(NumberFormat formatter)
formatter
.formatter
- the number formatterprotected void ensureDimensionsAreCorrect(int rows, int columns)
protected void ensureIndexesAreInBounds(int i, int j)
protected void fail(String message)
Copyright © 2015. All rights reserved.