Class FloatMatrix
- All Implemented Interfaces:
Serializable
Construction
To construct a two-dimensional matrices, you can use the following constructors and static methods.
Method | Description |
---|---|
FloatMatrix(m,n, [value1, value2, value3...]) | Values are filled in column by column. |
FloatMatrix(new float[][] {{value1, value2, ...}, ...} | Inner arrays are rows. |
FloatMatrix.zeros(m,n) | Initial values set to 0.0f. |
FloatMatrix.ones(m,n) | Initial values set to 1.0f. |
FloatMatrix.rand(m,n) | Values drawn at random between 0.0f and 1.0f. |
FloatMatrix.randn(m,n) | Values drawn from normal distribution. |
FloatMatrix.eye(n) | Unit matrix (values 0.0f except for 1.0f on the diagonal). |
FloatMatrix.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 |
---|---|
FloatMatrix(m) | Constructs a column vector. |
FloatMatrix(new float[] {value1, value2, ...}) | Constructs a column vector. |
FloatMatrix.zeros(m) | Initial values set to 0.0f. |
FloatMatrix.ones(m) | Initial values set to 1.0f. |
FloatMatrix.rand(m) | Values drawn at random between 0.0f and 1.0f. |
FloatMatrix.randn(m) | Values drawn from normal distribution. |
FloatMatrix.linspace(a, b, n) | n linearly spaced values from a to b. |
FloatMatrix.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, FloatMatrix objects, or Range objects, which then specify the indices used as follows:
- integer array: the elements will be used as indices.
- FloatMatrix 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(FloatMatrix) | The specified indices. | |
find(FloatMatrix) | 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 floats instead of FloatMatrix 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 FloatMatrix 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 FloatMatrix of size 0 times 0.FloatMatrix
(float[] newData) Create a a column vector using newData as the data array.FloatMatrix
(float[][] data) Creates a new n times m FloatMatrix from the given n times m 2D data array.FloatMatrix
(int len) Create a Matrix of length len.FloatMatrix
(int newRows, int newColumns) Creates a new n times m FloatMatrix.FloatMatrix
(int newRows, int newColumns, float... newData) Create a new matrix with newRows rows, newColumns columns using newData as the data.FloatMatrix
(String filename) Creates a new matrix by reading it from a file.FloatMatrix
(List<Float> data) Creates a FloatMatrix column vector from the given List<Double>. -
Method Summary
Modifier and TypeMethodDescriptionadd
(float v) Add a scalar.add
(FloatMatrix other) Add a matrix.Add a vector to all columns of the matrix.addi
(float v) Add a scalar (in place).addi
(float v, FloatMatrix result) Add a scalar to a matrix (in-place).addi
(FloatMatrix other) Add a matrix (in place).addi
(FloatMatrix other, FloatMatrix 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
(float value) Compute elementwise logical and against a scalar.and
(FloatMatrix other) Compute elementwise logical and.andi
(float value) Compute elementwise logical and against a scalar (in-place).andi
(float value, FloatMatrix result) Compute elementwise logical and against a scalar (in-place).andi
(FloatMatrix other) Compute elementwise logical and (in-place).andi
(FloatMatrix other, FloatMatrix 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 FloatMatrix
Concatenates two matrices horizontally.static FloatMatrix
Concatenates two matrices vertically.copy
(FloatMatrix a) Copy FloatMatrix 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 FloatMatrix
diag
(FloatMatrix x) Creates a new matrix where the values of the given vector are the diagonal values of the matrix.static FloatMatrix
diag
(FloatMatrix x, int rows, int columns) Construct a matrix of arbitrary shape and set the diagonal according to a passed vector.float
distance1
(FloatMatrix other) Returns the (1-norm) distance.float
distance2
(FloatMatrix other) Returns the (euclidean) distance.div
(float v) Elementwise divide by a scalar.div
(FloatMatrix other) Elementwise divide by a matrix.divi
(float v) Elementwise divide by a scalar (in place).divi
(float a, FloatMatrix result) Elementwise division with a scalar (in-place).divi
(FloatMatrix other) Elementwise divide by a matrix (in place).divi
(FloatMatrix other, FloatMatrix result) Elementwise division (in-place).float
dot
(FloatMatrix other) The scalar product of this with other.dup()
Returns a duplicate of this matrix.eq
(float value) test for equality against a scalar.eq
(FloatMatrix other) Test for equality.eqi
(float value) Test for equality against a scalar (in-place).eqi
(float value, FloatMatrix result) Test for equality against a scalar (in-place).eqi
(FloatMatrix other) Test for equality (in-place).eqi
(FloatMatrix other, FloatMatrix result) Test for equality (in-place).boolean
static FloatMatrix
eye
(int n) Construct a new n-by-n identity matrix.fill
(float value) Set all elements to a value.int[]
Find the linear indices of all non-zero elements.ge
(float value) test for "greater than or equal" against a scalar.ge
(FloatMatrix other) Test for "greater than or equal".gei
(float value) Test for "greater than or equal" against a scalar (in-place).gei
(float value, FloatMatrix result) Test for "greater than or equal" against a scalar (in-place).gei
(FloatMatrix other) Test for "greater than or equal" (in-place).gei
(FloatMatrix other, FloatMatrix result) Test for "greater than or equal" (in-place).float
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.float
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, FloatMatrix indices) Get elements from a row and columns as specified by the non-zero entries of a matrix.get
(FloatMatrix indices) Get elements specified by the non-zero entries of the passed matrix.get
(FloatMatrix indices, int c) Get elements from a column and rows as specified by the non-zero entries of a matrix.get
(FloatMatrix rindices, FloatMatrix 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, FloatMatrix 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
(FloatMatrix cindices) Get whole columns as specified by the non-zero entries of a matrix.getColumns
(Range indices) getColumns
(Range indices, FloatMatrix 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, FloatMatrix 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
(FloatMatrix rindices) Get whole rows as specified by the non-zero entries of a matrix.getRows
(Range indices, FloatMatrix result) gt
(float value) test for "greater than" against a scalar.gt
(FloatMatrix other) Test for "greater than".gti
(float value) Test for "greater than" against a scalar (in-place).gti
(float value, FloatMatrix result) Test for "greater than" against a scalar (in-place).gti
(FloatMatrix other) Test for "greater than" (in-place).gti
(FloatMatrix other, FloatMatrix 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
(float value) test for "less than or equal" against a scalar.le
(FloatMatrix other) Test for "less than or equal".lei
(float value) Test for "less than or equal" against a scalar (in-place).lei
(float value, FloatMatrix result) Test for "less than or equal" against a scalar (in-place).lei
(FloatMatrix other) Test for "less than or equal" (in-place).lei
(FloatMatrix other, FloatMatrix result) Test for "less than or equal" (in-place).static FloatMatrix
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 FloatMatrix
loadAsciiFile
(String filename) static FloatMatrix
loadCSVFile
(String filename) static FloatMatrix
logspace
(float lower, float 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
(float value) test for "less than" against a scalar.lt
(FloatMatrix other) Test for "less than".lti
(float value) Test for "less than" against a scalar (in-place).lti
(float value, FloatMatrix result) Test for "less than" against a scalar (in-place).lti
(FloatMatrix other) Test for "less than" (in-place).lti
(FloatMatrix other, FloatMatrix result) Test for "less than" (in-place).float
max()
Returns the maximal element of the matrix.max
(float v) max
(FloatMatrix other) Computes the maximum between two matrices.maxi
(float v) maxi
(float v, FloatMatrix result) maxi
(FloatMatrix other) Computes the maximum between two matrices.maxi
(FloatMatrix other, FloatMatrix result) Computes the maximum between two matrices.float
mean()
Computes the mean value of all elements in the matrix, that is,x.sum() / x.length
.float
min()
Returns the minimal element of the matrix.min
(float v) min
(FloatMatrix other) Computes the minimum between two matrices.mini
(float v) mini
(float v, FloatMatrix result) mini
(FloatMatrix other) Computes the minimum between two matrices.mini
(FloatMatrix other, FloatMatrix result) Computes the minimum between two matrices.mmul
(float v) Matrix-multiply by a scalar.mmul
(FloatMatrix other) Matrix-multiply by a matrix.mmuli
(float v) Matrix-multiply by a scalar (in place).mmuli
(float v, FloatMatrix result) Matrix-matrix multiplication with a scalar (for symmetry, does the same asmuli(scalar)
(in-place).mmuli
(FloatMatrix other) Matrix-multiply by a matrix (in place).mmuli
(FloatMatrix other, FloatMatrix result) Matrix-matrix multiplication (in-place).mul
(float v) Elementwise multiply by a scalar.mul
(FloatMatrix other) Elementwise multiply by a matrix.mulColumn
(int c, float scale) Multiply a column by a scalar.Multiply all columns with a column vector.muli
(float v) Elementwise multiply by a scalar (in place).muli
(float v, FloatMatrix result) Elementwise multiplication with a scalar (in-place).muli
(FloatMatrix other) Elementwise multiply by a matrix (in place).muli
(FloatMatrix other, FloatMatrix 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, float 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
(float value) test for inequality against a scalar.ne
(FloatMatrix other) Test for inequality.neg()
Negate each element.negi()
Negate each element (in-place).nei
(float value) Test for inequality against a scalar (in-place).nei
(float value, FloatMatrix result) Test for inequality against a scalar (in-place).nei
(FloatMatrix other) Test for inequality (in-place).nei
(FloatMatrix other, FloatMatrix result) Test for inequality (in-place).float
norm1()
The 1-norm of the matrix as vector (sum of absolute values of elements).float
norm2()
The Euclidean norm of the matrix as vector, also the Frobenius norm of the matrix.float
normmax()
The maximum norm of the matrix (maximal absolute value of the elements).not()
Maps zero to 1.0f and all non-zero values to 0.0f.noti()
Maps zero to 1.0f and all non-zero values to 0.0f (in-place).static FloatMatrix
ones
(int length) Creates a column vector with all elements equal to 1.static FloatMatrix
ones
(int rows, int columns) Creates a new matrix in which all values are equal 1.or
(float value) Compute elementwise logical or against a scalar.or
(FloatMatrix other) Compute elementwise logical or.ori
(float value) Compute elementwise logical or against a scalar (in-place).ori
(float value, FloatMatrix result) Compute elementwise logical or against a scalar (in-place).ori
(FloatMatrix other) Compute elementwise logical or (in-place).ori
(FloatMatrix other, FloatMatrix 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.float
prod()
Computes the product of all elements of the matrixfloat
project
(FloatMatrix other) Computes the projection coefficient of other on this.put
(int[] indices, float v) Put a single value into the specified indices (linear adressing).put
(int[] rindices, int[] cindices, float v) Put a single value into the specified rows and columns.put
(int[] rindices, int[] cindices, FloatMatrix x) Put a sub-matrix as specified by the indices.put
(int[] indices, int c, float v) Put a single value into the specified rows of a column.put
(int[] indices, int c, FloatMatrix x) Set multiple elements in a row.put
(int[] indices, FloatMatrix x) Set elements in linear ordering in the specified indices.put
(int i, float v) Set a matrix element (linear indexing).put
(int r, int[] indices, float v) Put a single value into a row and the specified columns.put
(int r, int[] indices, FloatMatrix x) Set multiple elements in a row.put
(int rowIndex, int columnIndex, float value) Set matrix elementput
(int r, FloatMatrix indices, float v) Put a single value into the specified columns (non-zero entries of indices) of a row.put
(int r, FloatMatrix indices, FloatMatrix v) Put a sub-vector into the specified columns (non-zero entries of indices) of a row.put
(FloatMatrix indices, float v) Put a single value into the elements specified by the non-zero entries of indices (linear adressing).put
(FloatMatrix indices, int c, float v) Put a single value into the specified rows (non-zero entries of indices) of a column.put
(FloatMatrix indices, int c, FloatMatrix v) Put a sub-vector into the specified rows (non-zero entries of indices) of a column.put
(FloatMatrix indices, FloatMatrix v) Put a sub-matrix into the indices specified by the non-zero entries of indices (linear adressing).put
(FloatMatrix rindices, FloatMatrix cindices, float v) Put a single value in the specified rows and columns (non-zero entries of rindices and cindices.put
(FloatMatrix rindices, FloatMatrix cindices, FloatMatrix v) Put a sub-matrix into the specified rows and columns (non-zero entries of rindices and cindices.put
(Range rs, Range cs, FloatMatrix x) Put a matrix into specified indices.void
putColumn
(int c, FloatMatrix v) Copy a column back into the matrix.void
putRow
(int r, FloatMatrix v) Copy a row back into the matrix.static FloatMatrix
rand
(int len) Creates a column vector with random values uniformly in 0..1.static FloatMatrix
rand
(int rows, int columns) Create matrix with random values uniformly in 0..1.static FloatMatrix
randn
(int len) Create column vector with normally distributed random values.static FloatMatrix
randn
(int rows, int columns) Create matrix with normally distributed random values.rankOneUpdate
(float alpha, FloatMatrix x) Computes a rank-1-update A = A + alpha * x * x'.rankOneUpdate
(float alpha, FloatMatrix x, FloatMatrix 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
(float v) (right-)elementwise divide by a scalar.rdiv
(FloatMatrix other) (right-)elementwise divide by a matrix.rdivi
(float v) (right-)elementwise divide by a scalar (in place).rdivi
(float a, FloatMatrix result) (Elementwise) division with a scalar, with operands switched.rdivi
(FloatMatrix other) (right-)elementwise divide by a matrix (in place).rdivi
(FloatMatrix other, FloatMatrix 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
(float v) (right-)subtract a scalar.rsub
(FloatMatrix other) (right-)subtract a matrix.rsubi
(float v) (right-)subtract a scalar (in place).rsubi
(float a, FloatMatrix result) Subtract a matrix from a scalar (in-place).rsubi
(FloatMatrix other) (right-)subtract a matrix (in place).rsubi
(FloatMatrix other, FloatMatrix 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.float
scalar()
Return the first element of the matrix.static FloatMatrix
scalar
(float s) Create a 1-by-1 matrix.select
(FloatMatrix where) selecti
(FloatMatrix 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).float
squaredDistance
(FloatMatrix other) Returns the squared (Euclidean) distance.sub
(float v) Subtract a scalar.sub
(FloatMatrix other) Subtract a matrix.Subtract a vector from all columns of the matrix.subi
(float v) Subtract a scalar (in place).subi
(float v, FloatMatrix result) Subtract a scalar from a matrix (in-place).subi
(FloatMatrix other) Subtract a matrix (in place).subi
(FloatMatrix other, FloatMatrix 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.float
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.float[]
toArray()
Converts the matrix to a one-dimensional array of floats.float[][]
toArray2()
Converts the matrix to a two-dimensional array of floats.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.0f and all non-zero values to 1.0f.truthi()
Maps zero to 0.0f and all non-zero values to 1.0f (in-place).static FloatMatrix
Construct FloatMatrix from ASCII representation.xor
(float value) Compute elementwise logical xor against a scalar.xor
(FloatMatrix other) Compute elementwise logical xor.xori
(float value) Compute elementwise logical xor against a scalar (in-place).xori
(float value, FloatMatrix result) Compute elementwise logical xor against a scalar (in-place).xori
(FloatMatrix other) Compute elementwise logical xor (in-place).xori
(FloatMatrix other, FloatMatrix result) Compute elementwise logical xor (in-place).static FloatMatrix
zeros
(int length) Creates a column vector of given length.static FloatMatrix
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
-
FloatMatrix
Create a new matrix with newRows rows, newColumns columns using newData as the data. Note that any change to the FloatMatrix 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)
-
FloatMatrix
Creates a new n times m FloatMatrix.- Parameters:
newRows
- the number of rows (n) of the new matrix.newColumns
- the number of columns (m) of the new matrix.
-
FloatMatrix
public FloatMatrix()Creates a new FloatMatrix of size 0 times 0. -
FloatMatrix
Create a Matrix of length len. This creates a column vector.- Parameters:
len
-
-
FloatMatrix
Create a a column vector using newData as the data array. Note that any change to the created FloatMatrix will change in input array. -
FloatMatrix
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
-
FloatMatrix
Creates a new n times m FloatMatrix from the given n times m 2D data array. Note that the input array is copied and any change to the FloatMatrix 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 FloatMatrix(new float[][]{{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}}).print();
will constructs the following matrix:1.0f 2.0f 3.0f 4.0f 5.0f 6.0f 7.0f 8.0f 9.0f
.- Parameters:
data
- n times m data array
-
FloatMatrix
Creates a FloatMatrix column vector from the given List<Double>.- Parameters:
data
- data from which the entries are taken.
-
-
Method Details
-
valueOf
Construct FloatMatrix 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 float. -
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 FloatMatrix(3, 1, 2.0f, 4.0f, 8.0f)
doesa.put(1, 2.0f), a.put(2, 4.0f), a.put(0, 8.0f)
. -
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 FloatMatrix 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 FloatMatrix 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 floats. -
toArray2
Converts the matrix to a two-dimensional array of floats. -
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.0f and all non-zero values to 0.0f (in-place). -
not
Maps zero to 1.0f and all non-zero values to 0.0f. -
truthi
Maps zero to 0.0f and all non-zero values to 1.0f (in-place). -
truth
Maps zero to 0.0f and all non-zero values to 1.0f. -
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
-