Class DoubleMatrix
- All Implemented Interfaces:
Serializable
Construction
To construct a two-dimensional matrices, you can use the following constructors and static methods.
Method | Description |
---|---|
DoubleMatrix(m,n, [value1, value2, value3...]) | Values are filled in column by column. |
DoubleMatrix(new double[][] {{value1, value2, ...}, ...} | Inner arrays are rows. |
DoubleMatrix.zeros(m,n) | Initial values set to 0.0. |
DoubleMatrix.ones(m,n) | Initial values set to 1.0. |
DoubleMatrix.rand(m,n) | Values drawn at random between 0.0 and 1.0. |
DoubleMatrix.randn(m,n) | Values drawn from normal distribution. |
DoubleMatrix.eye(n) | Unit matrix (values 0.0 except for 1.0 on the diagonal). |
DoubleMatrix.diag(array) | Diagonal matrix with given diagonal elements.
|
Alternatively, you can construct (column) vectors, if you just supply the length using the following constructors and static methods.
Method | Description |
---|---|
DoubleMatrix(m) | Constructs a column vector. |
DoubleMatrix(new double[] {value1, value2, ...}) | Constructs a column vector. |
DoubleMatrix.zeros(m) | Initial values set to 0.0. |
DoubleMatrix.ones(m) | Initial values set to 1.0. |
DoubleMatrix.rand(m) | Values drawn at random between 0.0 and 1.0. |
DoubleMatrix.randn(m) | Values drawn from normal distribution. |
DoubleMatrix.linspace(a, b, n) | n linearly spaced values from a to b. |
DoubleMatrix.logspace(a, b, n) | n logarithmically spaced values form 10^a to 10^b. |
You can also construct new matrices by concatenating matrices either horziontally or vertically:
Method | Description |
---|---|
x.concatHorizontally(y) | New matrix will be x next to y. |
x.concatVertically(y) | New matrix will be x atop y.
|
Element Access, Copying and Duplication
To access individual elements, or whole rows and columns, use the following methods:
x.Method | Description |
---|---|
x.get(i,j) | Get element in row i and column j. |
x.put(i, j, v) | Set element in row i and column j to value v |
x.get(i) | Get the ith element of the matrix (traversing rows first). |
x.put(i, v) | Set the ith element of the matrix (traversing rows first). |
x.getColumn(i) | Get a copy of column i. |
x.putColumn(i, c) | Put matrix c into column i. |
x.getRow(i) | Get a copy of row i. |
x.putRow(i, c) | Put matrix c into row i. |
x.swapColumns(i, j) | Swap the contents of columns i and j. |
x.swapRows(i, j) | Swap the contents of rows i and j.
|
For get and put, you can also pass integer arrays, DoubleMatrix objects, or Range objects, which then specify the indices used as follows:
- integer array: the elements will be used as indices.
- DoubleMatrix object: non-zero entries specify the indices.
- Range object: see below.
When using put with multiple indices, the assigned object must have the correct size or be a scalar.
There exist the following Range objects. The Class RangeUtils also contains the a number of handy helper methods for constructing these ranges.
Class | RangeUtils method | Indices |
---|---|---|
AllRange | all() | All legal indices. |
PointRange | point(i) | A single point. |
IntervalRange | interval(a, b) | All indices from a to b (inclusive) |
IndicesRange | indices(int[]) | The specified indices. |
indices(DoubleMatrix) | The specified indices. | |
find(DoubleMatrix) | The non-zero entries of the matrix.
|
The following methods can be used for duplicating and copying matrices.
Method | Description |
---|---|
x.dup() | Get a copy of x. |
x.copy(y) | Copy the contents of y to x (possible resizing x).
|
Size and Shape
The following methods permit to access the size of a matrix and change its size or shape.
x.Method | Description |
---|---|
x.rows | Number of rows. |
x.columns | Number of columns. |
x.length | Total number of elements. |
x.isEmpty() | Checks whether rows == 0 and columns == 0. |
x.isRowVector() | Checks whether rows == 1. |
x.isColumnVector() | Checks whether columns == 1. |
x.isVector() | Checks whether rows == 1 or columns == 1. |
x.isSquare() | Checks whether rows == columns. |
x.isScalar() | Checks whether length == 1. |
x.resize(r, c) | Resize the matrix to r rows and c columns, discarding the content. |
x.reshape(r, c) | Resize the matrix to r rows and c columns. Number of elements must not change. |
The size is stored in the rows and columns member variables. The total number of elements is stored in length. Do not change these values unless you know what you're doing!
Arithmetics
The usual arithmetic operations are implemented. Each operation exists in a in-place version, recognizable by the suffix "i", to which you can supply the result matrix (or this is used, if missing). Using in-place operations can also lead to a smaller memory footprint, as the number of temporary objects is reduced (although the JVM garbage collector is usually pretty good at reusing these temporary object immediately with little overhead.)
Whenever you specify a result vector, the result vector must already have the correct dimensions.
For example, you can add two matrices using the add method. If you want to store the result in of x + y in z, type x.addi(y, z) // computes x = y + z. Even in-place methods return the result, such that you can easily chain in-place methods, for example: x.addi(y).addi(z) // computes x += y; x += z
Methods which operate element-wise only make sure that the length of the matrices is correct. Therefore, you can add a 3 * 3 matrix to a 1 * 9 matrix, for example.
Finally, there exist versions which take doubles instead of DoubleMatrix Objects as arguments. These then compute the operation with the same value as the right-hand-side. The same effect can be achieved by passing a DoubleMatrix with exactly one element.
Operation | Method | Comment |
---|---|---|
x + y | x.add(y) | |
x - y | x.sub(y), y.rsub(x) | rsub subtracts left from right hand side |
x * y | x.mul(y) | element-wise multiplication |
x.mmul(y) | matrix-matrix multiplication | |
x.dot(y) | scalar-product | |
x / y | x.div(y), y.rdiv(x) | rdiv divides right hand side by left hand side. |
- x | x.neg() |
|
There also exist operations which work on whole columns or rows.
Method | Description |
---|---|
x.addRowVector | adds a vector to each row (addiRowVector works in-place) |
x.addColumnVector | adds a vector to each column |
x.subRowVector | subtracts a vector from each row |
x.subColumnVector | subtracts a vector from each column |
x.mulRowVector | Multiplies each row by a vector (elementwise) |
x.mulColumnVector | Multiplies each column by a vector (elementwise) |
x.divRowVector | Divide each row by a vector (elementwise) |
x.divColumnVector | Divide each column by a vector (elementwise) |
x.mulRow | Multiplies a row by a scalar |
x.mulColumn | Multiplies a column by a scalar |
In principle, you could achieve the same result by first calling getColumn(), adding, and then calling putColumn, but these methods are much faster.
The following comparison operations are available
Operation | Method |
---|---|
x < y | x.lt(y) |
x <= y | x.le(y) |
x > y | x.gt(y) |
x >= y | x.ge(y) |
x == y | x.eq(y) |
x != y | x.ne(y)
|
Logical operations are also supported. For these operations, a value different from zero is treated as "true" and zero is treated as "false". All operations are carried out elementwise.
Operation | Method |
---|---|
x & y | x.and(y) |
x | y | x.or(y) |
x ^ y | x.xor(y) |
! x | x.not()
|
Finally, there are a few more methods to compute various things:
Method | Description |
---|---|
x.max() | Return maximal element |
x.argmax() | Return index of largest element |
x.min() | Return minimal element |
x.argmin() | Return index of smallest element |
x.columnMins() | Return column-wise minima |
x.columnArgmins() | Return column-wise index of minima |
x.columnMaxs() | Return column-wise maxima |
x.columnArgmaxs() | Return column-wise index of maxima
|
- Author:
- Mikio Braun, Johannes Schaback
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
class
A wrapper which allows to view a matrix as a List of Doubles (read-only!).class
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new DoubleMatrix of size 0 times 0.DoubleMatrix
(double[] newData) Create a a column vector using newData as the data array.DoubleMatrix
(double[][] data) Creates a new n times m DoubleMatrix from the given n times m 2D data array.DoubleMatrix
(int len) Create a Matrix of length len.DoubleMatrix
(int newRows, int newColumns) Creates a new n times m DoubleMatrix.DoubleMatrix
(int newRows, int newColumns, double... newData) Create a new matrix with newRows rows, newColumns columns using newData as the data.DoubleMatrix
(String filename) Creates a new matrix by reading it from a file.DoubleMatrix
(List<Double> data) Creates a DoubleMatrix column vector from the given List<Double>. -
Method Summary
Modifier and TypeMethodDescriptionadd
(double v) Add a scalar.add
(DoubleMatrix other) Add a matrix.Add a vector to all columns of the matrix.addi
(double v) Add a scalar (in place).addi
(double v, DoubleMatrix result) Add a scalar to a matrix (in-place).addi
(DoubleMatrix other) Add a matrix (in place).addi
(DoubleMatrix other, DoubleMatrix result) Add two matrices (in-place).Add a vector to all columns of the matrix (in-place).Add a row vector to all rows of the matrix (in place).Add a row to all rows of the matrix.and
(double value) Compute elementwise logical and against a scalar.and
(DoubleMatrix other) Compute elementwise logical and.andi
(double value) Compute elementwise logical and against a scalar (in-place).andi
(double value, DoubleMatrix result) Compute elementwise logical and against a scalar (in-place).andi
(DoubleMatrix other) Compute elementwise logical and (in-place).andi
(DoubleMatrix other, DoubleMatrix result) Compute elementwise logical and (in-place).int
argmax()
Returns the linear index of the maximal element of the matrix.int
argmin()
Returns the linear index of the minimal element.void
Throws SizeException unless matrices can be multiplied with one another.void
Throws SizeException unless matrices have the same length.void
Throws SizeException unless two matrices have the same size.void
Throw SizeException unless matrix is square.void
checkColumns
(int c) Asserts that the amtrix has a certain number of columns.void
checkLength
(int l) Assert that the matrix has a certain length.void
checkRows
(int r) Asserts that the matrix has a certain number of rows.int[]
Return index of minimal element per column.int[]
Return index of minimal element per column.Return column-wise maximums.Return a vector containing the means of all columns.Return column-wise minimums.int[][]
Return matrix of indices which sort all columns.Return a vector containing the sums of the columns (having number of columns many entries)boolean
Compare two matrices.static DoubleMatrix
Concatenates two matrices horizontally.static DoubleMatrix
Concatenates two matrices vertically.copy
(DoubleMatrix a) Copy DoubleMatrix a to this.Computes the cumulative sum, that is, the sum of all elements of the matrix up to a given index in linear addressing.Computes the cumulative sum, that is, the sum of all elements of the matrix up to a given index in linear addressing (in-place).diag()
Returns the diagonal of the matrix.static DoubleMatrix
diag
(DoubleMatrix x) Creates a new matrix where the values of the given vector are the diagonal values of the matrix.static DoubleMatrix
diag
(DoubleMatrix x, int rows, int columns) Construct a matrix of arbitrary shape and set the diagonal according to a passed vector.double
distance1
(DoubleMatrix other) Returns the (1-norm) distance.double
distance2
(DoubleMatrix other) Returns the (euclidean) distance.div
(double v) Elementwise divide by a scalar.div
(DoubleMatrix other) Elementwise divide by a matrix.divi
(double v) Elementwise divide by a scalar (in place).divi
(double a, DoubleMatrix result) Elementwise division with a scalar (in-place).divi
(DoubleMatrix other) Elementwise divide by a matrix (in place).divi
(DoubleMatrix other, DoubleMatrix result) Elementwise division (in-place).double
dot
(DoubleMatrix other) The scalar product of this with other.dup()
Returns a duplicate of this matrix.eq
(double value) test for equality against a scalar.eq
(DoubleMatrix other) Test for equality.eqi
(double value) Test for equality against a scalar (in-place).eqi
(double value, DoubleMatrix result) Test for equality against a scalar (in-place).eqi
(DoubleMatrix other) Test for equality (in-place).eqi
(DoubleMatrix other, DoubleMatrix result) Test for equality (in-place).boolean
static DoubleMatrix
eye
(int n) Construct a new n-by-n identity matrix.fill
(double value) Set all elements to a value.int[]
Find the linear indices of all non-zero elements.ge
(double value) test for "greater than or equal" against a scalar.ge
(DoubleMatrix other) Test for "greater than or equal".gei
(double value) Test for "greater than or equal" against a scalar (in-place).gei
(double value, DoubleMatrix result) Test for "greater than or equal" against a scalar (in-place).gei
(DoubleMatrix other) Test for "greater than or equal" (in-place).gei
(DoubleMatrix other, DoubleMatrix result) Test for "greater than or equal" (in-place).double
get
(int i) Get a matrix element (linear indexing).get
(int[] indices) Get all elements specified by the linear indices.get
(int[] indices, int c) Get all elements for a given column and the specified rows.get
(int[] rindices, int[] cindices) Get all elements from the specified rows and columns.double
get
(int rowIndex, int columnIndex) Retrieve matrix elementget
(int r, int[] indices) Get all elements for a given row and the specified columns.get
(int r, DoubleMatrix indices) Get elements from a row and columns as specified by the non-zero entries of a matrix.get
(DoubleMatrix indices) Get elements specified by the non-zero entries of the passed matrix.get
(DoubleMatrix indices, int c) Get elements from a column and rows as specified by the non-zero entries of a matrix.get
(DoubleMatrix rindices, DoubleMatrix cindices) Get elements from columns and rows as specified by the non-zero entries of the passed matrices.Get elements from specified rows and columns.getColumn
(int c) Get a copy of a column.getColumn
(int c, DoubleMatrix result) Copy a column to the given vector.getColumnRange
(int r, int a, int b) Get elements from a row and columns a to b.int
Get number of columns.getColumns
(int[] cindices) Get whole columns from the passed indices.getColumns
(DoubleMatrix cindices) Get whole columns as specified by the non-zero entries of a matrix.getColumns
(Range indices) getColumns
(Range indices, DoubleMatrix result) Get whole columns as specified by Range.int
Get total number of elements.getRange
(int a, int b) Return all elements with linear index a, a + 1, ..., b - 1.getRange
(int ra, int rb, int ca, int cb) Get elements from rows ra to rb and columns ca to cb.getRow
(int r) Get a copy of a row.getRow
(int r, DoubleMatrix result) Copy a row to a given vector.getRowRange
(int a, int b, int c) Get elements from a column and rows a to b.int
getRows()
Get number of rows.getRows
(int[] rindices) Get whole rows from the passed indices.getRows
(DoubleMatrix rindices) Get whole rows as specified by the non-zero entries of a matrix.getRows
(Range indices, DoubleMatrix result) gt
(double value) test for "greater than" against a scalar.gt
(DoubleMatrix other) Test for "greater than".gti
(double value) Test for "greater than" against a scalar (in-place).gti
(double value, DoubleMatrix result) Test for "greater than" against a scalar (in-place).gti
(DoubleMatrix other) Test for "greater than" (in-place).gti
(DoubleMatrix other, DoubleMatrix result) Test for "greater than" (in-place).int
hashCode()
void
in
(DataInputStream dis) Reads in a matrix from the given data stream.int
index
(int rowIndex, int columnIndex) Get index of an elementint
indexColumns
(int i) Compute the column index of a linear index.int
indexRows
(int i) Compute the row index of a linear index.boolean
Checks whether the matrix is a column vector.boolean
isEmpty()
Checks whether the matrix is empty.boolean
Checks whether all entries (i, j) with i >= j are zero.isNaN()
isNaNi()
boolean
Checks whether the matrix is a row vector.boolean
isScalar()
Test whether a matrix is scalar.boolean
isSquare()
Checks whether the matrix is square.boolean
Checks whether all entries (i, j) with i <= j are zero.boolean
isVector()
Checks whether the matrix is a vector.le
(double value) test for "less than or equal" against a scalar.le
(DoubleMatrix other) Test for "less than or equal".lei
(double value) Test for "less than or equal" against a scalar (in-place).lei
(double value, DoubleMatrix result) Test for "less than or equal" against a scalar (in-place).lei
(DoubleMatrix other) Test for "less than or equal" (in-place).lei
(DoubleMatrix other, DoubleMatrix result) Test for "less than or equal" (in-place).static DoubleMatrix
linspace
(int lower, int upper, int size) Construct a column vector whose entries are linearly spaced points from lower to upper with size many steps.void
Loads a matrix from a file into this matrix.static DoubleMatrix
loadAsciiFile
(String filename) static DoubleMatrix
loadCSVFile
(String filename) static DoubleMatrix
logspace
(double lower, double upper, int size) Construct a column vector whose entries are logarithmically spaced points from 10^lower to 10^upper using the specified number of stepslt
(double value) test for "less than" against a scalar.lt
(DoubleMatrix other) Test for "less than".lti
(double value) Test for "less than" against a scalar (in-place).lti
(double value, DoubleMatrix result) Test for "less than" against a scalar (in-place).lti
(DoubleMatrix other) Test for "less than" (in-place).lti
(DoubleMatrix other, DoubleMatrix result) Test for "less than" (in-place).double
max()
Returns the maximal element of the matrix.max
(double v) max
(DoubleMatrix other) Computes the maximum between two matrices.maxi
(double v) maxi
(double v, DoubleMatrix result) maxi
(DoubleMatrix other) Computes the maximum between two matrices.maxi
(DoubleMatrix other, DoubleMatrix result) Computes the maximum between two matrices.double
mean()
Computes the mean value of all elements in the matrix, that is,x.sum() / x.length
.double
min()
Returns the minimal element of the matrix.min
(double v) min
(DoubleMatrix other) Computes the minimum between two matrices.mini
(double v) mini
(double v, DoubleMatrix result) mini
(DoubleMatrix other) Computes the minimum between two matrices.mini
(DoubleMatrix other, DoubleMatrix result) Computes the minimum between two matrices.mmul
(double v) Matrix-multiply by a scalar.mmul
(DoubleMatrix other) Matrix-multiply by a matrix.mmuli
(double v) Matrix-multiply by a scalar (in place).mmuli
(double v, DoubleMatrix result) Matrix-matrix multiplication with a scalar (for symmetry, does the same asmuli(scalar)
(in-place).mmuli
(DoubleMatrix other) Matrix-multiply by a matrix (in place).mmuli
(DoubleMatrix other, DoubleMatrix result) Matrix-matrix multiplication (in-place).mul
(double v) Elementwise multiply by a scalar.mul
(DoubleMatrix other) Elementwise multiply by a matrix.mulColumn
(int c, double scale) Multiply a column by a scalar.Multiply all columns with a column vector.muli
(double v) Elementwise multiply by a scalar (in place).muli
(double v, DoubleMatrix result) Elementwise multiplication with a scalar (in-place).muli
(DoubleMatrix other) Elementwise multiply by a matrix (in place).muli
(DoubleMatrix other, DoubleMatrix result) Elementwise multiplication (in-place).Multiply all columns with a column vector (in-place).Multiply all rows with a row vector (in-place).mulRow
(int r, double scale) Multiply a row by a scalar.Multiply all rows with a row vector.boolean
Checks whether two matrices can be multiplied (that is, number of columns of this must equal number of rows of a.ne
(double value) test for inequality against a scalar.ne
(DoubleMatrix other) Test for inequality.neg()
Negate each element.negi()
Negate each element (in-place).nei
(double value) Test for inequality against a scalar (in-place).nei
(double value, DoubleMatrix result) Test for inequality against a scalar (in-place).nei
(DoubleMatrix other) Test for inequality (in-place).nei
(DoubleMatrix other, DoubleMatrix result) Test for inequality (in-place).double
norm1()
The 1-norm of the matrix as vector (sum of absolute values of elements).double
norm2()
The Euclidean norm of the matrix as vector, also the Frobenius norm of the matrix.double
normmax()
The maximum norm of the matrix (maximal absolute value of the elements).not()
Maps zero to 1.0 and all non-zero values to 0.0.noti()
Maps zero to 1.0 and all non-zero values to 0.0 (in-place).static DoubleMatrix
ones
(int length) Creates a column vector with all elements equal to 1.static DoubleMatrix
ones
(int rows, int columns) Creates a new matrix in which all values are equal 1.or
(double value) Compute elementwise logical or against a scalar.or
(DoubleMatrix other) Compute elementwise logical or.ori
(double value) Compute elementwise logical or against a scalar (in-place).ori
(double value, DoubleMatrix result) Compute elementwise logical or against a scalar (in-place).ori
(DoubleMatrix other) Compute elementwise logical or (in-place).ori
(DoubleMatrix other, DoubleMatrix result) Compute elementwise logical or (in-place).void
out
(DataOutputStream dos) Writes out this matrix to the given data stream.void
print()
Pretty-print this matrix to System.out.double
prod()
Computes the product of all elements of the matrixdouble
project
(DoubleMatrix other) Computes the projection coefficient of other on this.put
(int[] indices, double v) Put a single value into the specified indices (linear adressing).put
(int[] rindices, int[] cindices, double v) Put a single value into the specified rows and columns.put
(int[] rindices, int[] cindices, DoubleMatrix x) Put a sub-matrix as specified by the indices.put
(int[] indices, int c, double v) Put a single value into the specified rows of a column.put
(int[] indices, int c, DoubleMatrix x) Set multiple elements in a row.put
(int[] indices, DoubleMatrix x) Set elements in linear ordering in the specified indices.put
(int i, double v) Set a matrix element (linear indexing).put
(int r, int[] indices, double v) Put a single value into a row and the specified columns.put
(int r, int[] indices, DoubleMatrix x) Set multiple elements in a row.put
(int rowIndex, int columnIndex, double value) Set matrix elementput
(int r, DoubleMatrix indices, double v) Put a single value into the specified columns (non-zero entries of indices) of a row.put
(int r, DoubleMatrix indices, DoubleMatrix v) Put a sub-vector into the specified columns (non-zero entries of indices) of a row.put
(DoubleMatrix indices, double v) Put a single value into the elements specified by the non-zero entries of indices (linear adressing).put
(DoubleMatrix indices, int c, double v) Put a single value into the specified rows (non-zero entries of indices) of a column.put
(DoubleMatrix indices, int c, DoubleMatrix v) Put a sub-vector into the specified rows (non-zero entries of indices) of a column.put
(DoubleMatrix indices, DoubleMatrix v) Put a sub-matrix into the indices specified by the non-zero entries of indices (linear adressing).put
(DoubleMatrix rindices, DoubleMatrix cindices, double v) Put a single value in the specified rows and columns (non-zero entries of rindices and cindices.put
(DoubleMatrix rindices, DoubleMatrix cindices, DoubleMatrix v) Put a sub-matrix into the specified rows and columns (non-zero entries of rindices and cindices.put
(Range rs, Range cs, DoubleMatrix x) Put a matrix into specified indices.void
putColumn
(int c, DoubleMatrix v) Copy a column back into the matrix.void
putRow
(int r, DoubleMatrix v) Copy a row back into the matrix.static DoubleMatrix
rand
(int len) Creates a column vector with random values uniformly in 0..1.static DoubleMatrix
rand
(int rows, int columns) Create matrix with random values uniformly in 0..1.static DoubleMatrix
randn
(int len) Create column vector with normally distributed random values.static DoubleMatrix
randn
(int rows, int columns) Create matrix with normally distributed random values.rankOneUpdate
(double alpha, DoubleMatrix x) Computes a rank-1-update A = A + alpha * x * x'.rankOneUpdate
(double alpha, DoubleMatrix x, DoubleMatrix y) Computes a rank-1-update A = A + alpha * x * y'.Computes a rank-1-update A = A + x * x'.Computes a rank-1-update A = A + x * y'.rdiv
(double v) (right-)elementwise divide by a scalar.rdiv
(DoubleMatrix other) (right-)elementwise divide by a matrix.rdivi
(double v) (right-)elementwise divide by a scalar (in place).rdivi
(double a, DoubleMatrix result) (Elementwise) division with a scalar, with operands switched.rdivi
(DoubleMatrix other) (right-)elementwise divide by a matrix (in place).rdivi
(DoubleMatrix other, DoubleMatrix result) Elementwise division, with operands switched.repmat
(int rowMult, int columnMult) Generate a new matrix which has the given number of replications of this.reshape
(int newRows, int newColumns) Reshape the matrix.void
resize
(int newRows, int newColumns) Resize the matrix.int[]
Return index of maximum element per row.int[]
Return index of minimal element per row.rowMaxs()
Return row-wise maximums.rowMeans()
Return a vector containing the means of the rows.rowMins()
Return row-wise minimums.int[][]
Return matrix of indices which sort all columns.rowSums()
Return a vector containing the sum of the rows.rsub
(double v) (right-)subtract a scalar.rsub
(DoubleMatrix other) (right-)subtract a matrix.rsubi
(double v) (right-)subtract a scalar (in place).rsubi
(double a, DoubleMatrix result) Subtract a matrix from a scalar (in-place).rsubi
(DoubleMatrix other) (right-)subtract a matrix (in place).rsubi
(DoubleMatrix other, DoubleMatrix result) Subtract two matrices, but subtract first from second matrix, that is, compute result = other - this (in-place).boolean
Checks whether two matrices have the same length.boolean
Checks whether two matrices have the same size.void
Saves this matrix to the specified file.double
scalar()
Return the first element of the matrix.static DoubleMatrix
scalar
(double s) Create a 1-by-1 matrix.select
(DoubleMatrix where) selecti
(DoubleMatrix where) sort()
Return a new matrix with all elements sorted.Sort columns.Sort columns (in-place).sorti()
Sort elements in-place.int[]
Get the sorting permutation.sortRows()
Sort rows.Sort rows (in-place).double
squaredDistance
(DoubleMatrix other) Returns the squared (Euclidean) distance.sub
(double v) Subtract a scalar.sub
(DoubleMatrix other) Subtract a matrix.Subtract a vector from all columns of the matrix.subi
(double v) Subtract a scalar (in place).subi
(double v, DoubleMatrix result) Subtract a scalar from a matrix (in-place).subi
(DoubleMatrix other) Subtract a matrix (in place).subi
(DoubleMatrix other, DoubleMatrix result) Subtract two matrices (in-place).Subtract a column vector from all columns of the matrix (in-place).Subtract a row vector from all rows of the matrix (in-place).Subtract a row vector from all rows of the matrix.double
sum()
Computes the sum of all elements of the matrix.swapColumns
(int i, int j) Swap two columns of a matrix.swapRows
(int i, int j) Swap two rows of a matrix.double[]
toArray()
Converts the matrix to a one-dimensional array of doubles.double[][]
toArray2()
Converts the matrix to a two-dimensional array of doubles.boolean[]
Convert the matrix to a one-dimensional array of boolean values.boolean[][]
Convert the matrix to a two-dimensional array of boolean values.toFloat()
int[]
Converts the matrix to a one-dimensional array of integers.int[][]
Convert the matrix to a two-dimensional array of integers.toString()
Generate string representation of the matrix.Generate string representation of the matrix, with specified format for the entries.Generate string representation of the matrix, with specified format for the entries, and delimiters.Return transposed copy of this matrix.truth()
Maps zero to 0.0 and all non-zero values to 1.0.truthi()
Maps zero to 0.0 and all non-zero values to 1.0 (in-place).static DoubleMatrix
Construct DoubleMatrix from ASCII representation.xor
(double value) Compute elementwise logical xor against a scalar.xor
(DoubleMatrix other) Compute elementwise logical xor.xori
(double value) Compute elementwise logical xor against a scalar (in-place).xori
(double value, DoubleMatrix result) Compute elementwise logical xor against a scalar (in-place).xori
(DoubleMatrix other) Compute elementwise logical xor (in-place).xori
(DoubleMatrix other, DoubleMatrix result) Compute elementwise logical xor (in-place).static DoubleMatrix
zeros
(int length) Creates a column vector of given length.static DoubleMatrix
zeros
(int rows, int columns) Creates a new matrix in which all values are equal 0.
-
Field Details
-
rows
Number of rows. -
columns
Number of columns. -
length
Total number of elements (for convenience). -
data
The actual data stored by rows (that is, row 0, row 1...). -
EMPTY
-
-
Constructor Details
-
DoubleMatrix
Create a new matrix with newRows rows, newColumns columns using newData as the data. Note that any change to the DoubleMatrix will change the input array, too.- Parameters:
newRows
- the number of rows of the new matrixnewColumns
- the number of columns of the new matrixnewData
- the data array to be used. Data must be stored by column (column-major)
-
DoubleMatrix
Creates a new n times m DoubleMatrix.- Parameters:
newRows
- the number of rows (n) of the new matrix.newColumns
- the number of columns (m) of the new matrix.
-
DoubleMatrix
public DoubleMatrix()Creates a new DoubleMatrix of size 0 times 0. -
DoubleMatrix
Create a Matrix of length len. This creates a column vector.- Parameters:
len
-
-
DoubleMatrix
Create a a column vector using newData as the data array. Note that any change to the created DoubleMatrix will change in input array. -
DoubleMatrix
Creates a new matrix by reading it from a file.- Parameters:
filename
- the path and name of the file to read the matrix from- Throws:
IOException
-
DoubleMatrix
Creates a new n times m DoubleMatrix from the given n times m 2D data array. Note that the input array is copied and any change to the DoubleMatrix will not change the input array. The first dimension of the array makes the rows (n) and the second dimension the columns (m). For example, the given code
new DoubleMatrix(new double[][]{{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}}).print();
will constructs the following matrix:1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
.- Parameters:
data
- n times m data array
-
DoubleMatrix
Creates a DoubleMatrix column vector from the given List<Double>.- Parameters:
data
- data from which the entries are taken.
-
-
Method Details
-
valueOf
Construct DoubleMatrix from ASCII representation. This is not very fast, but can be quiet useful when you want to "just" construct a matrix, for example when testing. The format is semicolon separated rows of space separated values, for example "1 2 3; 4 5 6; 7 8 9". -
rand
Create matrix with random values uniformly in 0..1. -
rand
Creates a column vector with random values uniformly in 0..1. -
randn
Create matrix with normally distributed random values. -
randn
Create column vector with normally distributed random values. -
zeros
Creates a new matrix in which all values are equal 0. -
zeros
Creates a column vector of given length. -
ones
Creates a new matrix in which all values are equal 1. -
ones
Creates a column vector with all elements equal to 1. -
eye
Construct a new n-by-n identity matrix. -
diag
Creates a new matrix where the values of the given vector are the diagonal values of the matrix. -
diag
Construct a matrix of arbitrary shape and set the diagonal according to a passed vector. length of needs to be smaller than rows or columns.- Parameters:
x
- vector to fill the diagonal withrows
- number of rows of the resulting matrixcolumns
- number of columns of the resulting matrix- Returns:
- a matrix with dimensions rows * columns whose diagonal elements are filled by x
-
scalar
Create a 1-by-1 matrix. For many operations, this matrix functions like a normal double. -
isScalar
Test whether a matrix is scalar. -
scalar
Return the first element of the matrix. -
logspace
Construct a column vector whose entries are logarithmically spaced points from 10^lower to 10^upper using the specified number of steps- Parameters:
lower
- starting exponentupper
- ending exponentsize
- number of steps- Returns:
- a column vector with (10^lower, ... 10^upper) with size many entries.
-
linspace
Construct a column vector whose entries are linearly spaced points from lower to upper with size many steps.- Parameters:
lower
- starting valueupper
- end valuesize
- number of steps- Returns:
- a column vector of size (lower, ..., upper) with size many entries.
-
concatHorizontally
Concatenates two matrices horizontally. Matrices must have identical numbers of rows. -
concatVertically
Concatenates two matrices vertically. Matrices must have identical numbers of columns. -
get
Get all elements specified by the linear indices. -
get
Get all elements for a given row and the specified columns. -
get
Get all elements for a given column and the specified rows. -
get
Get all elements from the specified rows and columns. -
get
Get elements from specified rows and columns. -
get
-
get
-
get
Get elements specified by the non-zero entries of the passed matrix. -
get
Get elements from a row and columns as specified by the non-zero entries of a matrix. -
get
Get elements from a column and rows as specified by the non-zero entries of a matrix. -
get
Get elements from columns and rows as specified by the non-zero entries of the passed matrices. -
getRange
Return all elements with linear index a, a + 1, ..., b - 1. -
getColumnRange
Get elements from a row and columns a to b. -
getRowRange
Get elements from a column and rows a to b. -
getRange
Get elements from rows ra to rb and columns ca to cb. -
getRows
Get whole rows from the passed indices. -
getRows
Get whole rows as specified by the non-zero entries of a matrix. -
getRows
-
getRows
-
getColumns
Get whole columns from the passed indices. -
getColumns
Get whole columns as specified by the non-zero entries of a matrix. -
getColumns
Get whole columns as specified by Range. -
getColumns
-
checkLength
Assert that the matrix has a certain length.- Throws:
SizeException
-
checkRows
Asserts that the matrix has a certain number of rows.- Throws:
SizeException
-
checkColumns
Asserts that the amtrix has a certain number of columns.- Throws:
SizeException
-
put
Set elements in linear ordering in the specified indices. For example,a.put(new int[]{ 1, 2, 0 }, new DoubleMatrix(3, 1, 2.0, 4.0, 8.0)
doesa.put(1, 2.0), a.put(2, 4.0), a.put(0, 8.0)
. -
put
Set multiple elements in a row. -
put
Set multiple elements in a row. -
put
Put a sub-matrix as specified by the indices. -
put
Put a matrix into specified indices. -
put
Put a single value into the specified indices (linear adressing). -
put
Put a single value into a row and the specified columns. -
put
Put a single value into the specified rows of a column. -
put
Put a single value into the specified rows and columns. -
put
Put a sub-matrix into the indices specified by the non-zero entries of indices (linear adressing). -
put
Put a sub-vector into the specified columns (non-zero entries of indices) of a row. -
put
Put a sub-vector into the specified rows (non-zero entries of indices) of a column. -
put
Put a sub-matrix into the specified rows and columns (non-zero entries of rindices and cindices. -
put
Put a single value into the elements specified by the non-zero entries of indices (linear adressing). -
put
Put a single value into the specified columns (non-zero entries of indices) of a row. -
put
Put a single value into the specified rows (non-zero entries of indices) of a column. -
put
Put a single value in the specified rows and columns (non-zero entries of rindices and cindices. -
findIndices
Find the linear indices of all non-zero elements. -
transpose
Return transposed copy of this matrix. -
compare
Compare two matrices. Returns true if and only if other is also a DoubleMatrix which has the same size and the maximal absolute difference in matrix elements is smaller than the specified tolerance -
equals
-
hashCode
-
resize
Resize the matrix. All elements will be set to zero. -
reshape
Reshape the matrix. Number of elements must not change. -
repmat
Generate a new matrix which has the given number of replications of this. -
sameSize
Checks whether two matrices have the same size. -
assertSameSize
Throws SizeException unless two matrices have the same size. -
multipliesWith
Checks whether two matrices can be multiplied (that is, number of columns of this must equal number of rows of a. -
assertMultipliesWith
Throws SizeException unless matrices can be multiplied with one another. -
sameLength
Checks whether two matrices have the same length. -
assertSameLength
Throws SizeException unless matrices have the same length. -
copy
Copy DoubleMatrix a to this. this a is resized if necessary. -
dup
Returns a duplicate of this matrix. Geometry is the same (including offsets, transpose, etc.), but the buffer is not shared. -
swapColumns
Swap two columns of a matrix. -
swapRows
Swap two rows of a matrix. -
put
Set matrix element -
get
Retrieve matrix element -
index
Get index of an element -
indexRows
Compute the row index of a linear index. -
indexColumns
Compute the column index of a linear index. -
get
Get a matrix element (linear indexing). -
put
Set a matrix element (linear indexing). -
fill
Set all elements to a value. -
getRows
Get number of rows. -
getColumns
Get number of columns. -
getLength
Get total number of elements. -
isEmpty
Checks whether the matrix is empty. -
isSquare
Checks whether the matrix is square. -
assertSquare
Throw SizeException unless matrix is square. -
isVector
Checks whether the matrix is a vector. -
isRowVector
Checks whether the matrix is a row vector. -
isColumnVector
Checks whether the matrix is a column vector. -
diag
Returns the diagonal of the matrix. -
print
Pretty-print this matrix to System.out. -
toString
Generate string representation of the matrix. -
toString
Generate string representation of the matrix, with specified format for the entries. For example,x.toString("%.1f")
generates a string representations having only one position after the decimal point. -
toString
Generate string representation of the matrix, with specified format for the entries, and delimiters.- Parameters:
fmt
- entry format (passed to String.format())open
- opening parenthesisclose
- closing parenthesiscolSep
- separator between columnsrowSep
- separator between rows
-
toArray
Converts the matrix to a one-dimensional array of doubles. -
toArray2
Converts the matrix to a two-dimensional array of doubles. -
toIntArray
Converts the matrix to a one-dimensional array of integers. -
toIntArray2
Convert the matrix to a two-dimensional array of integers. -
toBooleanArray
Convert the matrix to a one-dimensional array of boolean values. -
toBooleanArray2
Convert the matrix to a two-dimensional array of boolean values. -
toFloat
-
elementsAsList
-
rowsAsList
-
columnsAsList
-
addi
Add two matrices (in-place). -
addi
Add a scalar to a matrix (in-place). -
subi
Subtract two matrices (in-place). -
subi
Subtract a scalar from a matrix (in-place). -
rsubi
Subtract two matrices, but subtract first from second matrix, that is, compute result = other - this (in-place). -
rsubi
Subtract a matrix from a scalar (in-place). -
muli
Elementwise multiplication (in-place). -
muli
Elementwise multiplication with a scalar (in-place). -
mmuli
Matrix-matrix multiplication (in-place). -
mmuli
Matrix-matrix multiplication with a scalar (for symmetry, does the same asmuli(scalar)
(in-place). -
divi
Elementwise division (in-place). -
divi
Elementwise division with a scalar (in-place). -
rdivi
Elementwise division, with operands switched. Computesresult = other / this
(in-place). -
rdivi
(Elementwise) division with a scalar, with operands switched. Computesresult = a / this
(in-place). -
negi
Negate each element (in-place). -
neg
Negate each element. -
noti
Maps zero to 1.0 and all non-zero values to 0.0 (in-place). -
not
Maps zero to 1.0 and all non-zero values to 0.0. -
truthi
Maps zero to 0.0 and all non-zero values to 1.0 (in-place). -
truth
Maps zero to 0.0 and all non-zero values to 1.0. -
isNaNi
-
isNaN
-
isInfinitei
-
isInfinite
-
isLowerTriangular
Checks whether all entries (i, j) with i >= j are zero. -
isUpperTriangular
Checks whether all entries (i, j) with i <= j are zero. -
selecti
-
select
-
rankOneUpdate
Computes a rank-1-update A = A + alpha * x * y'. -
rankOneUpdate
Computes a rank-1-update A = A + alpha * x * x'. -
rankOneUpdate
Computes a rank-1-update A = A + x * x'. -
rankOneUpdate
Computes a rank-1-update A = A + x * y'. -
min
Returns the minimal element of the matrix. -
argmin
Returns the linear index of the minimal element. If there are more than one elements with this value, the first one is returned. -
mini
Computes the minimum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place). -
mini
Computes the minimum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place on this). -
min
Computes the minimum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place on this). -
mini
-
mini
-
min
-
max
Returns the maximal element of the matrix. -
argmax
Returns the linear index of the maximal element of the matrix. If there are more than one elements with this value, the first one is returned. -
maxi
Computes the maximum between two matrices. Returns the larger of the corresponding elements in the matrix (in-place). -
maxi
Computes the maximum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place on this). -
max
Computes the maximum between two matrices. Returns the larger of the corresponding elements in the matrix (in-place on this). -
maxi
-
maxi
-
max
-
sum
Computes the sum of all elements of the matrix. -
prod
Computes the product of all elements of the matrix -
mean
Computes the mean value of all elements in the matrix, that is,x.sum() / x.length
. -
cumulativeSumi
Computes the cumulative sum, that is, the sum of all elements of the matrix up to a given index in linear addressing (in-place). -
cumulativeSum
Computes the cumulative sum, that is, the sum of all elements of the matrix up to a given index in linear addressing. -
dot
The scalar product of this with other. -
project
Computes the projection coefficient of other on this. The returned scalar times this is the orthogonal projection of other on this. -
norm2
The Euclidean norm of the matrix as vector, also the Frobenius norm of the matrix. -
normmax
The maximum norm of the matrix (maximal absolute value of the elements). -
norm1
The 1-norm of the matrix as vector (sum of absolute values of elements). -
squaredDistance
Returns the squared (Euclidean) distance. -
distance2
Returns the (euclidean) distance. -
distance1
Returns the (1-norm) distance. -
sort
Return a new matrix with all elements sorted. -
sorti
Sort elements in-place. -
sortingPermutation
Get the sorting permutation.- Returns:
- an int[] array such that which indexes the elements in sorted order.
-
sortColumnsi
Sort columns (in-place). -
sortColumns
Sort columns. -
columnSortingPermutations
Return matrix of indices which sort all columns. -
sortRowsi
Sort rows (in-place). -
sortRows
Sort rows. -
rowSortingPermutations
Return matrix of indices which sort all columns. -
columnSums
Return a vector containing the sums of the columns (having number of columns many entries) -
columnMeans
Return a vector containing the means of all columns. -
rowSums
Return a vector containing the sum of the rows. -
rowMeans
Return a vector containing the means of the rows. -
getColumn
Get a copy of a column. -
getColumn
Copy a column to the given vector. -
putColumn
Copy a column back into the matrix. -
getRow
Get a copy of a row. -
getRow
Copy a row to a given vector. -
putRow
Copy a row back into the matrix. -
columnMins
Return column-wise minimums. -
columnArgmins
Return index of minimal element per column. -
columnMaxs
Return column-wise maximums. -
columnArgmaxs
Return index of minimal element per column. -
rowMins
Return row-wise minimums. -
rowArgmins
Return index of minimal element per row. -
rowMaxs
Return row-wise maximums. -
rowArgmaxs
Return index of maximum element per row. -
addiRowVector
Add a row vector to all rows of the matrix (in place). -
addRowVector
Add a row to all rows of the matrix. -
addiColumnVector
Add a vector to all columns of the matrix (in-place). -
addColumnVector
Add a vector to all columns of the matrix. -
subiRowVector
Subtract a row vector from all rows of the matrix (in-place). -
subRowVector
Subtract a row vector from all rows of the matrix. -
subiColumnVector
Subtract a column vector from all columns of the matrix (in-place). -
subColumnVector
Subtract a vector from all columns of the matrix. -
mulRow
Multiply a row by a scalar. -
mulColumn
Multiply a column by a scalar. -
muliColumnVector
Multiply all columns with a column vector (in-place). -
mulColumnVector
Multiply all columns with a column vector. -
muliRowVector
Multiply all rows with a row vector (in-place). -
mulRowVector
Multiply all rows with a row vector. -
diviRowVector
-
divRowVector
-
diviColumnVector
-
divColumnVector
-
out
Writes out this matrix to the given data stream.- Parameters:
dos
- the data output stream to write to.- Throws:
IOException
-
in
Reads in a matrix from the given data stream. Note that the old data of this matrix will be discarded.- Parameters:
dis
- the data input stream to read from.- Throws:
IOException
-
save
Saves this matrix to the specified file.- Parameters:
filename
- the file to write the matrix in.- Throws:
IOException
- thrown on errors while writing the matrix to the file
-
load
Loads a matrix from a file into this matrix. Note that the old data of this matrix will be discarded.- Parameters:
filename
- the file to read the matrix from- Throws:
IOException
- thrown on errors while reading the matrix
-
loadAsciiFile
- Throws:
IOException
-
loadCSVFile
- Throws:
IOException
-
addi
Add a matrix (in place). -
add
Add a matrix. -
addi
Add a scalar (in place). -
add
Add a scalar. -
subi
Subtract a matrix (in place). -
sub
Subtract a matrix. -
subi
Subtract a scalar (in place). -
sub
Subtract a scalar. -
rsubi
(right-)subtract a matrix (in place). -
rsub
(right-)subtract a matrix. -
rsubi
(right-)subtract a scalar (in place). -
rsub
(right-)subtract a scalar. -
divi
Elementwise divide by a matrix (in place). -
div
Elementwise divide by a matrix. -
divi
Elementwise divide by a scalar (in place). -
div
Elementwise divide by a scalar. -
rdivi
(right-)elementwise divide by a matrix (in place). -
rdiv
(right-)elementwise divide by a matrix. -
rdivi
(right-)elementwise divide by a scalar (in place). -
rdiv
(right-)elementwise divide by a scalar. -
muli
Elementwise multiply by a matrix (in place). -
mul
Elementwise multiply by a matrix. -
muli
Elementwise multiply by a scalar (in place). -
mul
Elementwise multiply by a scalar. -
mmuli
Matrix-multiply by a matrix (in place). -
mmul
Matrix-multiply by a matrix. -
mmuli
Matrix-multiply by a scalar (in place). -
mmul
Matrix-multiply by a scalar. -
lti
Test for "less than" (in-place). -
lti
Test for "less than" (in-place). -
lt
Test for "less than". -
lti
Test for "less than" against a scalar (in-place). -
lti
Test for "less than" against a scalar (in-place). -
lt
test for "less than" against a scalar. -
gti
Test for "greater than" (in-place). -
gti
Test for "greater than" (in-place). -
gt
Test for "greater than". -
gti
Test for "greater than" against a scalar (in-place). -
gti
Test for "greater than" against a scalar (in-place). -
gt
test for "greater than" against a scalar. -
lei
Test for "less than or equal" (in-place). -
lei
Test for "less than or equal" (in-place). -
le
Test for "less than or equal". -
lei
Test for "less than or equal" against a scalar (in-place). -
lei
Test for "less than or equal" against a scalar (in-place). -
le
test for "less than or equal" against a scalar. -
gei
Test for "greater than or equal" (in-place). -
gei
Test for "greater than or equal" (in-place). -
ge
Test for "greater than or equal". -
gei
Test for "greater than or equal" against a scalar (in-place). -
gei
Test for "greater than or equal" against a scalar (in-place). -
ge
test for "greater than or equal" against a scalar. -
eqi
Test for equality (in-place). -
eqi
Test for equality (in-place). -
eq
Test for equality. -
eqi
Test for equality against a scalar (in-place). -
eqi
Test for equality against a scalar (in-place). -
eq
test for equality against a scalar. -
nei
Test for inequality (in-place). -
nei
Test for inequality (in-place). -
ne
Test for inequality. -
nei
Test for inequality against a scalar (in-place). -
nei
Test for inequality against a scalar (in-place). -
ne
test for inequality against a scalar. -
andi
Compute elementwise logical and (in-place). -
andi
Compute elementwise logical and (in-place). -
and
Compute elementwise logical and. -
andi
Compute elementwise logical and against a scalar (in-place). -
andi
Compute elementwise logical and against a scalar (in-place). -
and
Compute elementwise logical and against a scalar. -
ori
Compute elementwise logical or (in-place). -
ori
Compute elementwise logical or (in-place). -
or
Compute elementwise logical or. -
ori
Compute elementwise logical or against a scalar (in-place). -
ori
Compute elementwise logical or against a scalar (in-place). -
or
Compute elementwise logical or against a scalar. -
xori
Compute elementwise logical xor (in-place). -
xori
Compute elementwise logical xor (in-place). -
xor
Compute elementwise logical xor. -
xori
Compute elementwise logical xor against a scalar (in-place). -
xori
Compute elementwise logical xor against a scalar (in-place). -
xor
Compute elementwise logical xor against a scalar. -
toComplex
-