public final class DoubleMatrix extends Object implements Matrix, Serializable, org.joda.beans.ImmutableBean
double values.
This provides functionality similar to List but for a rectangular double[][].
In mathematical terms, this is a two-dimensional matrix.
| Modifier and Type | Class and Description |
|---|---|
static class |
DoubleMatrix.Meta
The meta-bean for
DoubleMatrix. |
| Modifier and Type | Field and Description |
|---|---|
static DoubleMatrix |
EMPTY
An empty array.
|
| Modifier and Type | Method and Description |
|---|---|
DoubleArray |
column(int column)
Gets the column at the specified index.
|
double[] |
columnArray(int column)
Gets the column at the specified index as an independent array.
|
int |
columnCount()
Gets the number of columns of this matrix.
|
DoubleMatrix |
combine(DoubleMatrix other,
DoubleBinaryOperator operator)
Returns an instance where each element is formed by some combination of the matching
values in this matrix and the other matrix.
|
static DoubleMatrix |
copyOf(double[][] array)
Obtains an instance from a
double[][]. |
static DoubleMatrix |
diagonal(DoubleArray array)
Obtains a diagonal matrix from the specified array.
|
int |
dimensions()
Gets the number of dimensions of this matrix.
|
boolean |
equals(Object obj) |
static DoubleMatrix |
filled(int rows,
int columns)
Obtains an instance with all entries equal to the zero.
|
static DoubleMatrix |
filled(int rows,
int columns,
double value)
Obtains an instance with all entries equal to the same value.
|
void |
forEach(IntIntDoubleConsumer action)
Applies an action to each value in the matrix.
|
double |
get(int row,
int column)
Gets the value at the specified row and column in this matrix.
|
int |
hashCode() |
static DoubleMatrix |
identity(int size)
Obtains an identity matrix.
|
boolean |
isEmpty()
Checks if this matrix is empty.
|
boolean |
isSquare()
Checks if this matrix is square.
|
DoubleMatrix |
map(DoubleUnaryOperator operator)
Returns an instance with an operation applied to each value in the matrix.
|
DoubleMatrix |
mapWithIndex(IntIntDoubleToDoubleFunction function)
Returns an instance with an operation applied to each indexed value in the matrix.
|
static DoubleMatrix.Meta |
meta()
The meta-bean for
DoubleMatrix. |
DoubleMatrix.Meta |
metaBean() |
DoubleMatrix |
minus(DoubleMatrix other)
Returns an instance where each element is equal to the difference between the
matching values in this matrix and the other matrix.
|
DoubleMatrix |
multipliedBy(double factor)
Returns an instance with each value multiplied by the specified factor.
|
static DoubleMatrix |
of()
Obtains an empty instance.
|
static DoubleMatrix |
of(int rows,
int columns,
double... values)
Obtains an immutable array with the specified size and values.
|
static DoubleMatrix |
of(int rows,
int columns,
IntIntToDoubleFunction valueFunction)
Obtains an instance with entries filled using a function.
|
static DoubleMatrix |
ofArrayObjects(int rows,
int columns,
IntFunction<DoubleArray> valuesFunction)
Obtains an instance with entries filled using a function.
|
static DoubleMatrix |
ofArrays(int rows,
int columns,
IntFunction<double[]> valuesFunction)
Obtains an instance with entries filled using a function.
|
static DoubleMatrix |
ofUnsafe(double[][] array)
Obtains an instance by wrapping a
double[][]. |
DoubleMatrix |
plus(DoubleMatrix other)
Returns an instance where each element is the sum of the matching values
in this array and the other matrix.
|
double |
reduce(double identity,
DoubleBinaryOperator operator)
Reduces this matrix returning a single value.
|
DoubleArray |
row(int row)
Gets the row at the specified index.
|
double[] |
rowArray(int row)
Gets the row at the specified index as an independent array.
|
int |
rowCount()
Gets the number of rows of this matrix.
|
int |
size()
Gets the size of this matrix.
|
double[][] |
toArray()
Converts this instance to an independent
double[][]. |
double[][] |
toArrayUnsafe()
Returns the underlying array.
|
String |
toString() |
double |
total()
Returns the total of all the values in the matrix.
|
DoubleMatrix |
transpose()
Transposes the matrix.
|
DoubleMatrix |
with(int row,
int column,
double newValue)
Returns an instance with the value at the specified index changed.
|
public static final DoubleMatrix EMPTY
public static DoubleMatrix of()
public static DoubleMatrix of(int rows, int columns, double... values)
The first two arguments specify the size.
The remaining arguments specify the values, all of row 0, then row 1, and so on.
There must be be rows * columns values.
rows - the number of rowscolumns - the number of columnsvalues - the valuesIllegalArgumentException - if the values array if the incorrect lengthpublic static DoubleMatrix of(int rows, int columns, IntIntToDoubleFunction valueFunction)
The function is passed the row and column index, returning the value.
rows - the number of rowscolumns - the number of columnsvalueFunction - the function used to populate the valuepublic static DoubleMatrix ofArrays(int rows, int columns, IntFunction<double[]> valuesFunction)
The function is passed the row index, returning the column values.
rows - the number of rowscolumns - the number of columnsvaluesFunction - the function used to populate the valuespublic static DoubleMatrix ofArrayObjects(int rows, int columns, IntFunction<DoubleArray> valuesFunction)
The function is passed the row index, returning the column values.
rows - the number of rowscolumns - the number of columnsvaluesFunction - the function used to populate the valuespublic static DoubleMatrix ofUnsafe(double[][] array)
double[][].
This method is inherently unsafe as it relies on good behavior by callers. Callers must never make any changes to the passed in array after calling this method. Doing so would violate the immutability of this class.
The double[][] must be rectangular, with the same length for each row.
This is not validated.
array - the array to assignpublic static DoubleMatrix copyOf(double[][] array)
double[][].
The input array is copied and not mutated.
array - the array to copy, clonedpublic static DoubleMatrix filled(int rows, int columns)
rows - the number of rowscolumns - the number of columnspublic static DoubleMatrix filled(int rows, int columns, double value)
rows - the number of rowscolumns - the number of columnsvalue - the value of all the elementspublic static DoubleMatrix identity(int size)
An identity matrix is square. It has every value equal to zero, except those on the primary diagonal, which are one.
size - the size of the matrixpublic static DoubleMatrix diagonal(DoubleArray array)
A diagonal matrix is square. It only has values on the primary diagonal, and those values are taken from the specified array.
array - the array to use to create the matrixpublic int dimensions()
dimensions in interface Matrixpublic int size()
This is the total number of elements.
public int rowCount()
public int columnCount()
public boolean isSquare()
A square matrix has the same number of rows and columns.
public boolean isEmpty()
public double get(int row,
int column)
row - the zero-based row index to retrievecolumn - the zero-based column index to retrieveIndexOutOfBoundsException - if either index is invalidpublic DoubleArray row(int row)
row - the zero-based row index to retrievepublic double[] rowArray(int row)
row - the zero-based row index to retrievepublic DoubleArray column(int column)
column - the zero-based column index to retrievepublic double[] columnArray(int column)
column - the zero-based column index to retrievepublic double[][] toArray()
double[][].public double[][] toArrayUnsafe()
This method is inherently unsafe as it relies on good behavior by callers. Callers must never make any changes to the array returned by this method. Doing so would violate the immutability of this class.
public void forEach(IntIntDoubleConsumer action)
This is used to perform an action on the contents of this matrix. The action receives the row, the column and the value. For example, the action could print out the matrix.
base.forEach((row, col, value) -> System.out.println(row + ": " + col + ": " + value));
This instance is immutable and unaffected by this method.
action - the action to be appliedpublic DoubleMatrix with(int row, int column, double newValue)
This instance is immutable and unaffected by this method.
row - the zero-based row index to retrievecolumn - the zero-based column index to retrievenewValue - the new value to storeIndexOutOfBoundsException - if either index is invalidpublic DoubleMatrix multipliedBy(double factor)
This is used to multiply the contents of this matrix, returning a new matrix.
This is a special case of map(DoubleUnaryOperator).
This instance is immutable and unaffected by this method.
factor - the multiplicative factorpublic DoubleMatrix map(DoubleUnaryOperator operator)
This is used to perform an operation on the contents of this matrix, returning a new matrix. The operator only receives the value. For example, the operator could take the inverse of each element.
result = base.map(value -> 1 / value);
This instance is immutable and unaffected by this method.
operator - the operator to be appliedpublic DoubleMatrix mapWithIndex(IntIntDoubleToDoubleFunction function)
This is used to perform an operation on the contents of this matrix, returning a new matrix. The function receives the row index, column index and the value. For example, the operator could multiply the value by the index.
result = base.mapWithIndex((index, value) -> index * value);
This instance is immutable and unaffected by this method.
function - the function to be appliedpublic DoubleMatrix plus(DoubleMatrix other)
This is used to add two matrices, returning a new matrix.
Element (i,j) in the resulting matrix is equal to element (i,j) in this matrix
plus element (i,j) in the other matrix.
The matrices must be of the same size.
This is a special case of combine(DoubleMatrix, DoubleBinaryOperator).
This instance is immutable and unaffected by this method.
other - the other matrixIllegalArgumentException - if the matrices have different sizespublic DoubleMatrix minus(DoubleMatrix other)
This is used to subtract the second matrix from the first, returning a new matrix.
Element (i,j) in the resulting matrix is equal to element (i,j) in this matrix
minus element (i,j) in the other matrix.
The matrices must be of the same size.
This is a special case of combine(DoubleMatrix, DoubleBinaryOperator).
This instance is immutable and unaffected by this method.
other - the other matrixIllegalArgumentException - if the matrices have different sizespublic DoubleMatrix combine(DoubleMatrix other, DoubleBinaryOperator operator)
This is used to combine two matrices, returning a new matrix.
Element (i,j) in the resulting matrix is equal to the result of the operator
when applied to element (i,j) in this array and element (i,j) in the other array.
The arrays must be of the same size.
This instance is immutable and unaffected by this method.
other - the other matrixoperator - the operator used to combine each pair of valuesIllegalArgumentException - if the matrices have different sizespublic double total()
This is a special case of reduce(double, DoubleBinaryOperator).
public double reduce(double identity,
DoubleBinaryOperator operator)
This is used to reduce the values in this matrix to a single value. The operator is called once for each element in the matrix. The first argument to the operator is the running total of the reduction, starting from zero. The second argument to the operator is the element.
This instance is immutable and unaffected by this method.
identity - the identity value to start fromoperator - the operator used to combine the value with the current totalpublic DoubleMatrix transpose()
This converts a matrix of m x n into a matrix of n x m.
Each element is moved to the opposite position.
public static DoubleMatrix.Meta meta()
DoubleMatrix.public DoubleMatrix.Meta metaBean()
metaBean in interface org.joda.beans.BeanCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.