Class DoubleMatrix
- java.lang.Object
-
- org.jblas.DoubleMatrix
-
- All Implemented Interfaces:
java.io.Serializable
public class DoubleMatrix extends java.lang.Object implements java.io.SerializableA general matrix class for double typed values. Don't be intimidated by the large number of methods this function defines. Most are overloads provided for ease of use. For example, for each arithmetic operation, up to six overloaded versions exist to handle in-place computations, and scalar arguments (like adding a number to all elements of a matrix).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. Matrix constructors. 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. Column vector constructors. 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. Matrix concatenation. 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. Element access. 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. Range objects. 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). Copying matrices. 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.Size and size checks. 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() Basic arithmetics. 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 Row and column arithmetics. 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) Comparison operations. 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() Logical operations. 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 Minimum and maximum. - See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classDoubleMatrix.ColumnsAsListViewclassDoubleMatrix.ElementsAsListViewA wrapper which allows to view a matrix as a List of Doubles (read-only!).classDoubleMatrix.RowsAsListView
-
Field Summary
Fields Modifier and Type Field Description intcolumnsNumber of columns.private static java.util.regex.PatternCOMMAdouble[]dataThe actual data stored by rows (that is, row 0, row 1...).static DoubleMatrixEMPTYintlengthTotal number of elements (for convenience).introwsNumber of rows.private static java.util.regex.PatternSEMICOLON(package private) static longserialVersionUIDprivate static java.util.regex.PatternWHITESPACES
-
Constructor Summary
Constructors Constructor Description DoubleMatrix()Creates 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(java.lang.String filename)Creates a new matrix by reading it from a file.DoubleMatrix(java.util.List<java.lang.Double> data)Creates a DoubleMatrix column vector from the given List<Double>.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DoubleMatrixadd(double v)Add a scalar.DoubleMatrixadd(DoubleMatrix other)Add a matrix.DoubleMatrixaddColumnVector(DoubleMatrix x)Add a vector to all columns of the matrix.DoubleMatrixaddi(double v)Add a scalar (in place).DoubleMatrixaddi(double v, DoubleMatrix result)Add a scalar to a matrix (in-place).DoubleMatrixaddi(DoubleMatrix other)Add a matrix (in place).DoubleMatrixaddi(DoubleMatrix other, DoubleMatrix result)Add two matrices (in-place).DoubleMatrixaddiColumnVector(DoubleMatrix x)Add a vector to all columns of the matrix (in-place).DoubleMatrixaddiRowVector(DoubleMatrix x)Add a row vector to all rows of the matrix (in place).DoubleMatrixaddRowVector(DoubleMatrix x)Add a row to all rows of the matrix.DoubleMatrixand(double value)Compute elementwise logical and against a scalar.DoubleMatrixand(DoubleMatrix other)Compute elementwise logical and.DoubleMatrixandi(double value)Compute elementwise logical and against a scalar (in-place).DoubleMatrixandi(double value, DoubleMatrix result)Compute elementwise logical and against a scalar (in-place).DoubleMatrixandi(DoubleMatrix other)Compute elementwise logical and (in-place).DoubleMatrixandi(DoubleMatrix other, DoubleMatrix result)Compute elementwise logical and (in-place).intargmax()Returns the linear index of the maximal element of the matrix.intargmin()Returns the linear index of the minimal element.voidassertMultipliesWith(DoubleMatrix a)Throws SizeException unless matrices can be multiplied with one another.voidassertSameLength(DoubleMatrix a)Throws SizeException unless matrices have the same length.voidassertSameSize(DoubleMatrix a)Throws SizeException unless two matrices have the same size.voidassertSquare()Throw SizeException unless matrix is square.voidcheckColumns(int c)Asserts that the amtrix has a certain number of columns.voidcheckLength(int l)Assert that the matrix has a certain length.voidcheckRows(int r)Asserts that the matrix has a certain number of rows.int[]columnArgmaxs()Return index of minimal element per column.int[]columnArgmins()Return index of minimal element per column.DoubleMatrixcolumnMaxs()Return column-wise maximums.DoubleMatrixcolumnMeans()Return a vector containing the means of all columns.DoubleMatrixcolumnMins()Return column-wise minimums.java.util.List<DoubleMatrix>columnsAsList()int[][]columnSortingPermutations()Return matrix of indices which sort all columns.DoubleMatrixcolumnSums()Return a vector containing the sums of the columns (having number of columns many entries)booleancompare(java.lang.Object o, double tolerance)Compare two matrices.static DoubleMatrixconcatHorizontally(DoubleMatrix A, DoubleMatrix B)Concatenates two matrices horizontally.static DoubleMatrixconcatVertically(DoubleMatrix A, DoubleMatrix B)Concatenates two matrices vertically.DoubleMatrixcopy(DoubleMatrix a)Copy DoubleMatrix a to this.DoubleMatrixcumulativeSum()Computes the cumulative sum, that is, the sum of all elements of the matrix up to a given index in linear addressing.DoubleMatrixcumulativeSumi()Computes the cumulative sum, that is, the sum of all elements of the matrix up to a given index in linear addressing (in-place).DoubleMatrixdiag()Returns the diagonal of the matrix.static DoubleMatrixdiag(DoubleMatrix x)Creates a new matrix where the values of the given vector are the diagonal values of the matrix.static DoubleMatrixdiag(DoubleMatrix x, int rows, int columns)Construct a matrix of arbitrary shape and set the diagonal according to a passed vector.doubledistance1(DoubleMatrix other)Returns the (1-norm) distance.doubledistance2(DoubleMatrix other)Returns the (euclidean) distance.DoubleMatrixdiv(double v)Elementwise divide by a scalar.DoubleMatrixdiv(DoubleMatrix other)Elementwise divide by a matrix.DoubleMatrixdivColumnVector(DoubleMatrix x)DoubleMatrixdivi(double v)Elementwise divide by a scalar (in place).DoubleMatrixdivi(double a, DoubleMatrix result)Elementwise division with a scalar (in-place).DoubleMatrixdivi(DoubleMatrix other)Elementwise divide by a matrix (in place).DoubleMatrixdivi(DoubleMatrix other, DoubleMatrix result)Elementwise division (in-place).DoubleMatrixdiviColumnVector(DoubleMatrix x)DoubleMatrixdiviRowVector(DoubleMatrix x)DoubleMatrixdivRowVector(DoubleMatrix x)doubledot(DoubleMatrix other)The scalar product of this with other.DoubleMatrixdup()Returns a duplicate of this matrix.java.util.List<java.lang.Double>elementsAsList()private voidensureResultLength(DoubleMatrix other, DoubleMatrix result)Ensures that the result vector has the same length as this.DoubleMatrixeq(double value)test for equality against a scalar.DoubleMatrixeq(DoubleMatrix other)Test for equality.DoubleMatrixeqi(double value)Test for equality against a scalar (in-place).DoubleMatrixeqi(double value, DoubleMatrix result)Test for equality against a scalar (in-place).DoubleMatrixeqi(DoubleMatrix other)Test for equality (in-place).DoubleMatrixeqi(DoubleMatrix other, DoubleMatrix result)Test for equality (in-place).booleanequals(java.lang.Object o)static DoubleMatrixeye(int n)Construct a new n-by-n identity matrix.DoubleMatrixfill(double value)Set all elements to a value.int[]findIndices()Find the linear indices of all non-zero elements.DoubleMatrixge(double value)test for "greater than or equal" against a scalar.DoubleMatrixge(DoubleMatrix other)Test for "greater than or equal".DoubleMatrixgei(double value)Test for "greater than or equal" against a scalar (in-place).DoubleMatrixgei(double value, DoubleMatrix result)Test for "greater than or equal" against a scalar (in-place).DoubleMatrixgei(DoubleMatrix other)Test for "greater than or equal" (in-place).DoubleMatrixgei(DoubleMatrix other, DoubleMatrix result)Test for "greater than or equal" (in-place).doubleget(int i)Get a matrix element (linear indexing).DoubleMatrixget(int[] indices)Get all elements specified by the linear indices.DoubleMatrixget(int[] indices, int c)Get all elements for a given column and the specified rows.DoubleMatrixget(int[] rindices, int[] cindices)Get all elements from the specified rows and columns.doubleget(int rowIndex, int columnIndex)Retrieve matrix elementDoubleMatrixget(int r, int[] indices)Get all elements for a given row and the specified columns.DoubleMatrixget(int r, DoubleMatrix indices)Get elements from a row and columns as specified by the non-zero entries of a matrix.DoubleMatrixget(int r, Range cs)DoubleMatrixget(DoubleMatrix indices)Get elements specified by the non-zero entries of the passed matrix.DoubleMatrixget(DoubleMatrix indices, int c)Get elements from a column and rows as specified by the non-zero entries of a matrix.DoubleMatrixget(DoubleMatrix rindices, DoubleMatrix cindices)Get elements from columns and rows as specified by the non-zero entries of the passed matrices.DoubleMatrixget(Range rs, int c)DoubleMatrixget(Range rs, Range cs)Get elements from specified rows and columns.DoubleMatrixgetColumn(int c)Get a copy of a column.DoubleMatrixgetColumn(int c, DoubleMatrix result)Copy a column to the given vector.DoubleMatrixgetColumnRange(int r, int a, int b)Get elements from a row and columns a to b.intgetColumns()Get number of columns.DoubleMatrixgetColumns(int[] cindices)Get whole columns from the passed indices.DoubleMatrixgetColumns(DoubleMatrix cindices)Get whole columns as specified by the non-zero entries of a matrix.DoubleMatrixgetColumns(Range indices)DoubleMatrixgetColumns(Range indices, DoubleMatrix result)Get whole columns as specified by Range.intgetLength()Get total number of elements.DoubleMatrixgetRange(int a, int b)Return all elements with linear index a, a + 1, ..., b - 1.DoubleMatrixgetRange(int ra, int rb, int ca, int cb)Get elements from rows ra to rb and columns ca to cb.DoubleMatrixgetRow(int r)Get a copy of a row.DoubleMatrixgetRow(int r, DoubleMatrix result)Copy a row to a given vector.DoubleMatrixgetRowRange(int a, int b, int c)Get elements from a column and rows a to b.intgetRows()Get number of rows.DoubleMatrixgetRows(int[] rindices)Get whole rows from the passed indices.DoubleMatrixgetRows(DoubleMatrix rindices)Get whole rows as specified by the non-zero entries of a matrix.DoubleMatrixgetRows(Range indices)DoubleMatrixgetRows(Range indices, DoubleMatrix result)DoubleMatrixgt(double value)test for "greater than" against a scalar.DoubleMatrixgt(DoubleMatrix other)Test for "greater than".DoubleMatrixgti(double value)Test for "greater than" against a scalar (in-place).DoubleMatrixgti(double value, DoubleMatrix result)Test for "greater than" against a scalar (in-place).DoubleMatrixgti(DoubleMatrix other)Test for "greater than" (in-place).DoubleMatrixgti(DoubleMatrix other, DoubleMatrix result)Test for "greater than" (in-place).inthashCode()voidin(java.io.DataInputStream dis)Reads in a matrix from the given data stream.intindex(int rowIndex, int columnIndex)Get index of an elementintindexColumns(int i)Compute the column index of a linear index.intindexRows(int i)Compute the row index of a linear index.booleanisColumnVector()Checks whether the matrix is a column vector.booleanisEmpty()Checks whether the matrix is empty.DoubleMatrixisInfinite()DoubleMatrixisInfinitei()booleanisLowerTriangular()Checks whether all entries (i, j) with i >= j are zero.DoubleMatrixisNaN()DoubleMatrixisNaNi()booleanisRowVector()Checks whether the matrix is a row vector.booleanisScalar()Test whether a matrix is scalar.booleanisSquare()Checks whether the matrix is square.booleanisUpperTriangular()Checks whether all entries (i, j) with i <= j are zero.booleanisVector()Checks whether the matrix is a vector.DoubleMatrixle(double value)test for "less than or equal" against a scalar.DoubleMatrixle(DoubleMatrix other)Test for "less than or equal".DoubleMatrixlei(double value)Test for "less than or equal" against a scalar (in-place).DoubleMatrixlei(double value, DoubleMatrix result)Test for "less than or equal" against a scalar (in-place).DoubleMatrixlei(DoubleMatrix other)Test for "less than or equal" (in-place).DoubleMatrixlei(DoubleMatrix other, DoubleMatrix result)Test for "less than or equal" (in-place).static DoubleMatrixlinspace(int lower, int upper, int size)Construct a column vector whose entries are linearly spaced points from lower to upper with size many steps.voidload(java.lang.String filename)Loads a matrix from a file into this matrix.static DoubleMatrixloadAsciiFile(java.lang.String filename)static DoubleMatrixloadCSVFile(java.lang.String filename)static DoubleMatrixlogspace(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 stepsDoubleMatrixlt(double value)test for "less than" against a scalar.DoubleMatrixlt(DoubleMatrix other)Test for "less than".DoubleMatrixlti(double value)Test for "less than" against a scalar (in-place).DoubleMatrixlti(double value, DoubleMatrix result)Test for "less than" against a scalar (in-place).DoubleMatrixlti(DoubleMatrix other)Test for "less than" (in-place).DoubleMatrixlti(DoubleMatrix other, DoubleMatrix result)Test for "less than" (in-place).doublemax()Returns the maximal element of the matrix.DoubleMatrixmax(double v)DoubleMatrixmax(DoubleMatrix other)Computes the maximum between two matrices.DoubleMatrixmaxi(double v)DoubleMatrixmaxi(double v, DoubleMatrix result)DoubleMatrixmaxi(DoubleMatrix other)Computes the maximum between two matrices.DoubleMatrixmaxi(DoubleMatrix other, DoubleMatrix result)Computes the maximum between two matrices.doublemean()Computes the mean value of all elements in the matrix, that is,x.sum() / x.length.doublemin()Returns the minimal element of the matrix.DoubleMatrixmin(double v)DoubleMatrixmin(DoubleMatrix other)Computes the minimum between two matrices.DoubleMatrixmini(double v)DoubleMatrixmini(double v, DoubleMatrix result)DoubleMatrixmini(DoubleMatrix other)Computes the minimum between two matrices.DoubleMatrixmini(DoubleMatrix other, DoubleMatrix result)Computes the minimum between two matrices.DoubleMatrixmmul(double v)Matrix-multiply by a scalar.DoubleMatrixmmul(DoubleMatrix other)Matrix-multiply by a matrix.DoubleMatrixmmuli(double v)Matrix-multiply by a scalar (in place).DoubleMatrixmmuli(double v, DoubleMatrix result)Matrix-matrix multiplication with a scalar (for symmetry, does the same asmuli(scalar)(in-place).DoubleMatrixmmuli(DoubleMatrix other)Matrix-multiply by a matrix (in place).DoubleMatrixmmuli(DoubleMatrix other, DoubleMatrix result)Matrix-matrix multiplication (in-place).DoubleMatrixmul(double v)Elementwise multiply by a scalar.DoubleMatrixmul(DoubleMatrix other)Elementwise multiply by a matrix.DoubleMatrixmulColumn(int c, double scale)Multiply a column by a scalar.DoubleMatrixmulColumnVector(DoubleMatrix x)Multiply all columns with a column vector.DoubleMatrixmuli(double v)Elementwise multiply by a scalar (in place).DoubleMatrixmuli(double v, DoubleMatrix result)Elementwise multiplication with a scalar (in-place).DoubleMatrixmuli(DoubleMatrix other)Elementwise multiply by a matrix (in place).DoubleMatrixmuli(DoubleMatrix other, DoubleMatrix result)Elementwise multiplication (in-place).DoubleMatrixmuliColumnVector(DoubleMatrix x)Multiply all columns with a column vector (in-place).DoubleMatrixmuliRowVector(DoubleMatrix x)Multiply all rows with a row vector (in-place).DoubleMatrixmulRow(int r, double scale)Multiply a row by a scalar.DoubleMatrixmulRowVector(DoubleMatrix x)Multiply all rows with a row vector.booleanmultipliesWith(DoubleMatrix a)Checks whether two matrices can be multiplied (that is, number of columns of this must equal number of rows of a.DoubleMatrixne(double value)test for inequality against a scalar.DoubleMatrixne(DoubleMatrix other)Test for inequality.DoubleMatrixneg()Negate each element.DoubleMatrixnegi()Negate each element (in-place).DoubleMatrixnei(double value)Test for inequality against a scalar (in-place).DoubleMatrixnei(double value, DoubleMatrix result)Test for inequality against a scalar (in-place).DoubleMatrixnei(DoubleMatrix other)Test for inequality (in-place).DoubleMatrixnei(DoubleMatrix other, DoubleMatrix result)Test for inequality (in-place).doublenorm1()The 1-norm of the matrix as vector (sum of absolute values of elements).doublenorm2()The Euclidean norm of the matrix as vector, also the Frobenius norm of the matrix.doublenormmax()The maximum norm of the matrix (maximal absolute value of the elements).DoubleMatrixnot()Maps zero to 1.0 and all non-zero values to 0.0.DoubleMatrixnoti()Maps zero to 1.0 and all non-zero values to 0.0 (in-place).static DoubleMatrixones(int length)Creates a column vector with all elements equal to 1.static DoubleMatrixones(int rows, int columns)Creates a new matrix in which all values are equal 1.DoubleMatrixor(double value)Compute elementwise logical or against a scalar.DoubleMatrixor(DoubleMatrix other)Compute elementwise logical or.DoubleMatrixori(double value)Compute elementwise logical or against a scalar (in-place).DoubleMatrixori(double value, DoubleMatrix result)Compute elementwise logical or against a scalar (in-place).DoubleMatrixori(DoubleMatrix other)Compute elementwise logical or (in-place).DoubleMatrixori(DoubleMatrix other, DoubleMatrix result)Compute elementwise logical or (in-place).voidout(java.io.DataOutputStream dos)Writes out this matrix to the given data stream.voidprint()Pretty-print this matrix to System.out.doubleprod()Computes the product of all elements of the matrixdoubleproject(DoubleMatrix other)Computes the projection coefficient of other on this.DoubleMatrixput(int[] indices, double v)Put a single value into the specified indices (linear adressing).DoubleMatrixput(int[] rindices, int[] cindices, double v)Put a single value into the specified rows and columns.DoubleMatrixput(int[] rindices, int[] cindices, DoubleMatrix x)Put a sub-matrix as specified by the indices.DoubleMatrixput(int[] indices, int c, double v)Put a single value into the specified rows of a column.DoubleMatrixput(int[] indices, int c, DoubleMatrix x)Set multiple elements in a row.DoubleMatrixput(int[] indices, DoubleMatrix x)Set elements in linear ordering in the specified indices.DoubleMatrixput(int i, double v)Set a matrix element (linear indexing).DoubleMatrixput(int r, int[] indices, double v)Put a single value into a row and the specified columns.DoubleMatrixput(int r, int[] indices, DoubleMatrix x)Set multiple elements in a row.DoubleMatrixput(int rowIndex, int columnIndex, double value)Set matrix elementDoubleMatrixput(int r, DoubleMatrix indices, double v)Put a single value into the specified columns (non-zero entries of indices) of a row.DoubleMatrixput(int r, DoubleMatrix indices, DoubleMatrix v)Put a sub-vector into the specified columns (non-zero entries of indices) of a row.DoubleMatrixput(DoubleMatrix indices, double v)Put a single value into the elements specified by the non-zero entries of indices (linear adressing).DoubleMatrixput(DoubleMatrix indices, int c, double v)Put a single value into the specified rows (non-zero entries of indices) of a column.DoubleMatrixput(DoubleMatrix indices, int c, DoubleMatrix v)Put a sub-vector into the specified rows (non-zero entries of indices) of a column.DoubleMatrixput(DoubleMatrix indices, DoubleMatrix v)Put a sub-matrix into the indices specified by the non-zero entries of indices (linear adressing).DoubleMatrixput(DoubleMatrix rindices, DoubleMatrix cindices, double v)Put a single value in the specified rows and columns (non-zero entries of rindices and cindices.DoubleMatrixput(DoubleMatrix rindices, DoubleMatrix cindices, DoubleMatrix v)Put a sub-matrix into the specified rows and columns (non-zero entries of rindices and cindices.DoubleMatrixput(Range rs, Range cs, DoubleMatrix x)Put a matrix into specified indices.voidputColumn(int c, DoubleMatrix v)Copy a column back into the matrix.voidputRow(int r, DoubleMatrix v)Copy a row back into the matrix.static DoubleMatrixrand(int len)Creates a column vector with random values uniformly in 0..1.static DoubleMatrixrand(int rows, int columns)Create matrix with random values uniformly in 0..1.static DoubleMatrixrandn(int len)Create column vector with normally distributed random values.static DoubleMatrixrandn(int rows, int columns)Create matrix with normally distributed random values.DoubleMatrixrankOneUpdate(double alpha, DoubleMatrix x)Computes a rank-1-update A = A + alpha * x * x'.DoubleMatrixrankOneUpdate(double alpha, DoubleMatrix x, DoubleMatrix y)Computes a rank-1-update A = A + alpha * x * y'.DoubleMatrixrankOneUpdate(DoubleMatrix x)Computes a rank-1-update A = A + x * x'.DoubleMatrixrankOneUpdate(DoubleMatrix x, DoubleMatrix y)Computes a rank-1-update A = A + x * y'.DoubleMatrixrdiv(double v)(right-)elementwise divide by a scalar.DoubleMatrixrdiv(DoubleMatrix other)(right-)elementwise divide by a matrix.DoubleMatrixrdivi(double v)(right-)elementwise divide by a scalar (in place).DoubleMatrixrdivi(double a, DoubleMatrix result)(Elementwise) division with a scalar, with operands switched.DoubleMatrixrdivi(DoubleMatrix other)(right-)elementwise divide by a matrix (in place).DoubleMatrixrdivi(DoubleMatrix other, DoubleMatrix result)Elementwise division, with operands switched.private voidreadObject(java.io.ObjectInputStream s)DoubleMatrixrepmat(int rowMult, int columnMult)Generate a new matrix which has the given number of replications of this.DoubleMatrixreshape(int newRows, int newColumns)Reshape the matrix.voidresize(int newRows, int newColumns)Resize the matrix.int[]rowArgmaxs()Return index of maximum element per row.int[]rowArgmins()Return index of minimal element per row.DoubleMatrixrowMaxs()Return row-wise maximums.DoubleMatrixrowMeans()Return a vector containing the means of the rows.DoubleMatrixrowMins()Return row-wise minimums.java.util.List<DoubleMatrix>rowsAsList()int[][]rowSortingPermutations()Return matrix of indices which sort all columns.DoubleMatrixrowSums()Return a vector containing the sum of the rows.DoubleMatrixrsub(double v)(right-)subtract a scalar.DoubleMatrixrsub(DoubleMatrix other)(right-)subtract a matrix.DoubleMatrixrsubi(double v)(right-)subtract a scalar (in place).DoubleMatrixrsubi(double a, DoubleMatrix result)Subtract a matrix from a scalar (in-place).DoubleMatrixrsubi(DoubleMatrix other)(right-)subtract a matrix (in place).DoubleMatrixrsubi(DoubleMatrix other, DoubleMatrix result)Subtract two matrices, but subtract first from second matrix, that is, compute result = other - this (in-place).booleansameLength(DoubleMatrix a)Checks whether two matrices have the same length.booleansameSize(DoubleMatrix a)Checks whether two matrices have the same size.voidsave(java.lang.String filename)Saves this matrix to the specified file.doublescalar()Return the first element of the matrix.static DoubleMatrixscalar(double s)Create a 1-by-1 matrix.DoubleMatrixselect(DoubleMatrix where)DoubleMatrixselecti(DoubleMatrix where)DoubleMatrixsort()Return a new matrix with all elements sorted.DoubleMatrixsortColumns()Sort columns.DoubleMatrixsortColumnsi()Sort columns (in-place).DoubleMatrixsorti()Sort elements in-place.int[]sortingPermutation()Get the sorting permutation.DoubleMatrixsortRows()Sort rows.DoubleMatrixsortRowsi()Sort rows (in-place).doublesquaredDistance(DoubleMatrix other)Returns the squared (Euclidean) distance.DoubleMatrixsub(double v)Subtract a scalar.DoubleMatrixsub(DoubleMatrix other)Subtract a matrix.DoubleMatrixsubColumnVector(DoubleMatrix x)Subtract a vector from all columns of the matrix.DoubleMatrixsubi(double v)Subtract a scalar (in place).DoubleMatrixsubi(double v, DoubleMatrix result)Subtract a scalar from a matrix (in-place).DoubleMatrixsubi(DoubleMatrix other)Subtract a matrix (in place).DoubleMatrixsubi(DoubleMatrix other, DoubleMatrix result)Subtract two matrices (in-place).DoubleMatrixsubiColumnVector(DoubleMatrix x)Subtract a column vector from all columns of the matrix (in-place).DoubleMatrixsubiRowVector(DoubleMatrix x)Subtract a row vector from all rows of the matrix (in-place).DoubleMatrixsubRowVector(DoubleMatrix x)Subtract a row vector from all rows of the matrix.doublesum()Computes the sum of all elements of the matrix.DoubleMatrixswapColumns(int i, int j)Swap two columns of a matrix.DoubleMatrixswapRows(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[]toBooleanArray()Convert the matrix to a one-dimensional array of boolean values.boolean[][]toBooleanArray2()Convert the matrix to a two-dimensional array of boolean values.ComplexDoubleMatrixtoComplex()FloatMatrixtoFloat()int[]toIntArray()Converts the matrix to a one-dimensional array of integers.int[][]toIntArray2()Convert the matrix to a two-dimensional array of integers.java.lang.StringtoString()Generate string representation of the matrix.java.lang.StringtoString(java.lang.String fmt)Generate string representation of the matrix, with specified format for the entries.java.lang.StringtoString(java.lang.String fmt, java.lang.String open, java.lang.String close, java.lang.String colSep, java.lang.String rowSep)Generate string representation of the matrix, with specified format for the entries, and delimiters.DoubleMatrixtranspose()Return transposed copy of this matrix.DoubleMatrixtruth()Maps zero to 0.0 and all non-zero values to 1.0.DoubleMatrixtruthi()Maps zero to 0.0 and all non-zero values to 1.0 (in-place).static DoubleMatrixvalueOf(java.lang.String text)Construct DoubleMatrix from ASCII representation.private voidwriteObject(java.io.ObjectOutputStream s)SerializationDoubleMatrixxor(double value)Compute elementwise logical xor against a scalar.DoubleMatrixxor(DoubleMatrix other)Compute elementwise logical xor.DoubleMatrixxori(double value)Compute elementwise logical xor against a scalar (in-place).DoubleMatrixxori(double value, DoubleMatrix result)Compute elementwise logical xor against a scalar (in-place).DoubleMatrixxori(DoubleMatrix other)Compute elementwise logical xor (in-place).DoubleMatrixxori(DoubleMatrix other, DoubleMatrix result)Compute elementwise logical xor (in-place).static DoubleMatrixzeros(int length)Creates a column vector of given length.static DoubleMatrixzeros(int rows, int columns)Creates a new matrix in which all values are equal 0.
-
-
-
Field Detail
-
rows
public int rows
Number of rows.
-
columns
public int columns
Number of columns.
-
length
public int length
Total number of elements (for convenience).
-
data
public double[] data
The actual data stored by rows (that is, row 0, row 1...).
-
EMPTY
public static final DoubleMatrix EMPTY
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
SEMICOLON
private static final java.util.regex.Pattern SEMICOLON
-
WHITESPACES
private static final java.util.regex.Pattern WHITESPACES
-
COMMA
private static final java.util.regex.Pattern COMMA
-
-
Constructor Detail
-
DoubleMatrix
public DoubleMatrix(int newRows, int newColumns, double... newData)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
public DoubleMatrix(int newRows, int newColumns)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
public DoubleMatrix(int len)
Create a Matrix of length len. This creates a column vector.- Parameters:
len-
-
DoubleMatrix
public DoubleMatrix(double[] newData)
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
public DoubleMatrix(java.lang.String filename) throws java.io.IOExceptionCreates a new matrix by reading it from a file.- Parameters:
filename- the path and name of the file to read the matrix from- Throws:
java.io.IOException
-
DoubleMatrix
public DoubleMatrix(double[][] data)
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
public DoubleMatrix(java.util.List<java.lang.Double> data)
Creates a DoubleMatrix column vector from the given List<Double>.- Parameters:
data- data from which the entries are taken.
-
-
Method Detail
-
valueOf
public static DoubleMatrix valueOf(java.lang.String text)
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".
-
writeObject
private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOExceptionSerialization- Throws:
java.io.IOException
-
readObject
private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, java.lang.ClassNotFoundException- Throws:
java.io.IOExceptionjava.lang.ClassNotFoundException
-
rand
public static DoubleMatrix rand(int rows, int columns)
Create matrix with random values uniformly in 0..1.
-
rand
public static DoubleMatrix rand(int len)
Creates a column vector with random values uniformly in 0..1.
-
randn
public static DoubleMatrix randn(int rows, int columns)
Create matrix with normally distributed random values.
-
randn
public static DoubleMatrix randn(int len)
Create column vector with normally distributed random values.
-
zeros
public static DoubleMatrix zeros(int rows, int columns)
Creates a new matrix in which all values are equal 0.
-
zeros
public static DoubleMatrix zeros(int length)
Creates a column vector of given length.
-
ones
public static DoubleMatrix ones(int rows, int columns)
Creates a new matrix in which all values are equal 1.
-
ones
public static DoubleMatrix ones(int length)
Creates a column vector with all elements equal to 1.
-
eye
public static DoubleMatrix eye(int n)
Construct a new n-by-n identity matrix.
-
diag
public static DoubleMatrix diag(DoubleMatrix x)
Creates a new matrix where the values of the given vector are the diagonal values of the matrix.
-
diag
public static DoubleMatrix diag(DoubleMatrix x, int rows, int columns)
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
public static DoubleMatrix scalar(double s)
Create a 1-by-1 matrix. For many operations, this matrix functions like a normal double.
-
isScalar
public boolean isScalar()
Test whether a matrix is scalar.
-
scalar
public double scalar()
Return the first element of the matrix.
-
logspace
public 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 steps- Parameters:
lower- starting exponentupper- ending exponentsize- number of steps- Returns:
- a column vector with (10^lower, ... 10^upper) with size many entries.
-
linspace
public 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.- Parameters:
lower- starting valueupper- end valuesize- number of steps- Returns:
- a column vector of size (lower, ..., upper) with size many entries.
-
concatHorizontally
public static DoubleMatrix concatHorizontally(DoubleMatrix A, DoubleMatrix B)
Concatenates two matrices horizontally. Matrices must have identical numbers of rows.
-
concatVertically
public static DoubleMatrix concatVertically(DoubleMatrix A, DoubleMatrix B)
Concatenates two matrices vertically. Matrices must have identical numbers of columns.
-
get
public DoubleMatrix get(int[] indices)
Get all elements specified by the linear indices.
-
get
public DoubleMatrix get(int r, int[] indices)
Get all elements for a given row and the specified columns.
-
get
public DoubleMatrix get(int[] indices, int c)
Get all elements for a given column and the specified rows.
-
get
public DoubleMatrix get(int[] rindices, int[] cindices)
Get all elements from the specified rows and columns.
-
get
public DoubleMatrix get(Range rs, Range cs)
Get elements from specified rows and columns.
-
get
public DoubleMatrix get(Range rs, int c)
-
get
public DoubleMatrix get(int r, Range cs)
-
get
public DoubleMatrix get(DoubleMatrix indices)
Get elements specified by the non-zero entries of the passed matrix.
-
get
public DoubleMatrix get(int r, DoubleMatrix indices)
Get elements from a row and columns as specified by the non-zero entries of a matrix.
-
get
public DoubleMatrix get(DoubleMatrix indices, int c)
Get elements from a column and rows as specified by the non-zero entries of a matrix.
-
get
public DoubleMatrix get(DoubleMatrix rindices, DoubleMatrix cindices)
Get elements from columns and rows as specified by the non-zero entries of the passed matrices.
-
getRange
public DoubleMatrix getRange(int a, int b)
Return all elements with linear index a, a + 1, ..., b - 1.
-
getColumnRange
public DoubleMatrix getColumnRange(int r, int a, int b)
Get elements from a row and columns a to b.
-
getRowRange
public DoubleMatrix getRowRange(int a, int b, int c)
Get elements from a column and rows a to b.
-
getRange
public DoubleMatrix getRange(int ra, int rb, int ca, int cb)
Get elements from rows ra to rb and columns ca to cb.
-
getRows
public DoubleMatrix getRows(int[] rindices)
Get whole rows from the passed indices.
-
getRows
public DoubleMatrix getRows(DoubleMatrix rindices)
Get whole rows as specified by the non-zero entries of a matrix.
-
getRows
public DoubleMatrix getRows(Range indices, DoubleMatrix result)
-
getRows
public DoubleMatrix getRows(Range indices)
-
getColumns
public DoubleMatrix getColumns(int[] cindices)
Get whole columns from the passed indices.
-
getColumns
public DoubleMatrix getColumns(DoubleMatrix cindices)
Get whole columns as specified by the non-zero entries of a matrix.
-
getColumns
public DoubleMatrix getColumns(Range indices, DoubleMatrix result)
Get whole columns as specified by Range.
-
getColumns
public DoubleMatrix getColumns(Range indices)
-
checkLength
public void checkLength(int l)
Assert that the matrix has a certain length.- Throws:
SizeException
-
checkRows
public void checkRows(int r)
Asserts that the matrix has a certain number of rows.- Throws:
SizeException
-
checkColumns
public void checkColumns(int c)
Asserts that the amtrix has a certain number of columns.- Throws:
SizeException
-
put
public DoubleMatrix put(int[] indices, DoubleMatrix x)
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
public DoubleMatrix put(int r, int[] indices, DoubleMatrix x)
Set multiple elements in a row.
-
put
public DoubleMatrix put(int[] indices, int c, DoubleMatrix x)
Set multiple elements in a row.
-
put
public DoubleMatrix put(int[] rindices, int[] cindices, DoubleMatrix x)
Put a sub-matrix as specified by the indices.
-
put
public DoubleMatrix put(Range rs, Range cs, DoubleMatrix x)
Put a matrix into specified indices.
-
put
public DoubleMatrix put(int[] indices, double v)
Put a single value into the specified indices (linear adressing).
-
put
public DoubleMatrix put(int r, int[] indices, double v)
Put a single value into a row and the specified columns.
-
put
public DoubleMatrix put(int[] indices, int c, double v)
Put a single value into the specified rows of a column.
-
put
public DoubleMatrix put(int[] rindices, int[] cindices, double v)
Put a single value into the specified rows and columns.
-
put
public DoubleMatrix put(DoubleMatrix indices, DoubleMatrix v)
Put a sub-matrix into the indices specified by the non-zero entries of indices (linear adressing).
-
put
public DoubleMatrix put(int r, DoubleMatrix indices, DoubleMatrix v)
Put a sub-vector into the specified columns (non-zero entries of indices) of a row.
-
put
public DoubleMatrix put(DoubleMatrix indices, int c, DoubleMatrix v)
Put a sub-vector into the specified rows (non-zero entries of indices) of a column.
-
put
public DoubleMatrix 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
public DoubleMatrix put(DoubleMatrix indices, double v)
Put a single value into the elements specified by the non-zero entries of indices (linear adressing).
-
put
public DoubleMatrix put(int r, DoubleMatrix indices, double v)
Put a single value into the specified columns (non-zero entries of indices) of a row.
-
put
public DoubleMatrix put(DoubleMatrix indices, int c, double v)
Put a single value into the specified rows (non-zero entries of indices) of a column.
-
put
public DoubleMatrix put(DoubleMatrix rindices, DoubleMatrix cindices, double v)
Put a single value in the specified rows and columns (non-zero entries of rindices and cindices.
-
findIndices
public int[] findIndices()
Find the linear indices of all non-zero elements.
-
transpose
public DoubleMatrix transpose()
Return transposed copy of this matrix.
-
compare
public boolean compare(java.lang.Object o, double tolerance)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
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
resize
public void resize(int newRows, int newColumns)Resize the matrix. All elements will be set to zero.
-
reshape
public DoubleMatrix reshape(int newRows, int newColumns)
Reshape the matrix. Number of elements must not change.
-
repmat
public DoubleMatrix repmat(int rowMult, int columnMult)
Generate a new matrix which has the given number of replications of this.
-
sameSize
public boolean sameSize(DoubleMatrix a)
Checks whether two matrices have the same size.
-
assertSameSize
public void assertSameSize(DoubleMatrix a)
Throws SizeException unless two matrices have the same size.
-
multipliesWith
public boolean multipliesWith(DoubleMatrix a)
Checks whether two matrices can be multiplied (that is, number of columns of this must equal number of rows of a.
-
assertMultipliesWith
public void assertMultipliesWith(DoubleMatrix a)
Throws SizeException unless matrices can be multiplied with one another.
-
sameLength
public boolean sameLength(DoubleMatrix a)
Checks whether two matrices have the same length.
-
assertSameLength
public void assertSameLength(DoubleMatrix a)
Throws SizeException unless matrices have the same length.
-
copy
public DoubleMatrix copy(DoubleMatrix a)
Copy DoubleMatrix a to this. this a is resized if necessary.
-
dup
public DoubleMatrix dup()
Returns a duplicate of this matrix. Geometry is the same (including offsets, transpose, etc.), but the buffer is not shared.
-
swapColumns
public DoubleMatrix swapColumns(int i, int j)
Swap two columns of a matrix.
-
swapRows
public DoubleMatrix swapRows(int i, int j)
Swap two rows of a matrix.
-
put
public DoubleMatrix put(int rowIndex, int columnIndex, double value)
Set matrix element
-
get
public double get(int rowIndex, int columnIndex)Retrieve matrix element
-
index
public int index(int rowIndex, int columnIndex)Get index of an element
-
indexRows
public int indexRows(int i)
Compute the row index of a linear index.
-
indexColumns
public int indexColumns(int i)
Compute the column index of a linear index.
-
get
public double get(int i)
Get a matrix element (linear indexing).
-
put
public DoubleMatrix put(int i, double v)
Set a matrix element (linear indexing).
-
fill
public DoubleMatrix fill(double value)
Set all elements to a value.
-
getRows
public int getRows()
Get number of rows.
-
getColumns
public int getColumns()
Get number of columns.
-
getLength
public int getLength()
Get total number of elements.
-
isEmpty
public boolean isEmpty()
Checks whether the matrix is empty.
-
isSquare
public boolean isSquare()
Checks whether the matrix is square.
-
assertSquare
public void assertSquare()
Throw SizeException unless matrix is square.
-
isVector
public boolean isVector()
Checks whether the matrix is a vector.
-
isRowVector
public boolean isRowVector()
Checks whether the matrix is a row vector.
-
isColumnVector
public boolean isColumnVector()
Checks whether the matrix is a column vector.
-
diag
public DoubleMatrix diag()
Returns the diagonal of the matrix.
-
print
public void print()
Pretty-print this matrix to System.out.
-
toString
public java.lang.String toString()
Generate string representation of the matrix.- Overrides:
toStringin classjava.lang.Object
-
toString
public java.lang.String toString(java.lang.String fmt)
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
public java.lang.String toString(java.lang.String fmt, java.lang.String open, java.lang.String close, java.lang.String colSep, java.lang.String rowSep)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
public double[] toArray()
Converts the matrix to a one-dimensional array of doubles.
-
toArray2
public double[][] toArray2()
Converts the matrix to a two-dimensional array of doubles.
-
toIntArray
public int[] toIntArray()
Converts the matrix to a one-dimensional array of integers.
-
toIntArray2
public int[][] toIntArray2()
Convert the matrix to a two-dimensional array of integers.
-
toBooleanArray
public boolean[] toBooleanArray()
Convert the matrix to a one-dimensional array of boolean values.
-
toBooleanArray2
public boolean[][] toBooleanArray2()
Convert the matrix to a two-dimensional array of boolean values.
-
toFloat
public FloatMatrix toFloat()
-
elementsAsList
public java.util.List<java.lang.Double> elementsAsList()
-
rowsAsList
public java.util.List<DoubleMatrix> rowsAsList()
-
columnsAsList
public java.util.List<DoubleMatrix> columnsAsList()
-
ensureResultLength
private void ensureResultLength(DoubleMatrix other, DoubleMatrix result)
Ensures that the result vector has the same length as this. If not, resizing result is tried, which fails if result == this or result == other.
-
addi
public DoubleMatrix addi(DoubleMatrix other, DoubleMatrix result)
Add two matrices (in-place).
-
addi
public DoubleMatrix addi(double v, DoubleMatrix result)
Add a scalar to a matrix (in-place).
-
subi
public DoubleMatrix subi(DoubleMatrix other, DoubleMatrix result)
Subtract two matrices (in-place).
-
subi
public DoubleMatrix subi(double v, DoubleMatrix result)
Subtract a scalar from a matrix (in-place).
-
rsubi
public DoubleMatrix rsubi(DoubleMatrix other, DoubleMatrix result)
Subtract two matrices, but subtract first from second matrix, that is, compute result = other - this (in-place).
-
rsubi
public DoubleMatrix rsubi(double a, DoubleMatrix result)
Subtract a matrix from a scalar (in-place).
-
muli
public DoubleMatrix muli(DoubleMatrix other, DoubleMatrix result)
Elementwise multiplication (in-place).
-
muli
public DoubleMatrix muli(double v, DoubleMatrix result)
Elementwise multiplication with a scalar (in-place).
-
mmuli
public DoubleMatrix mmuli(DoubleMatrix other, DoubleMatrix result)
Matrix-matrix multiplication (in-place).
-
mmuli
public DoubleMatrix mmuli(double v, DoubleMatrix result)
Matrix-matrix multiplication with a scalar (for symmetry, does the same asmuli(scalar)(in-place).
-
divi
public DoubleMatrix divi(DoubleMatrix other, DoubleMatrix result)
Elementwise division (in-place).
-
divi
public DoubleMatrix divi(double a, DoubleMatrix result)
Elementwise division with a scalar (in-place).
-
rdivi
public DoubleMatrix rdivi(DoubleMatrix other, DoubleMatrix result)
Elementwise division, with operands switched. Computesresult = other / this(in-place).
-
rdivi
public DoubleMatrix rdivi(double a, DoubleMatrix result)
(Elementwise) division with a scalar, with operands switched. Computesresult = a / this(in-place).
-
negi
public DoubleMatrix negi()
Negate each element (in-place).
-
neg
public DoubleMatrix neg()
Negate each element.
-
noti
public DoubleMatrix noti()
Maps zero to 1.0 and all non-zero values to 0.0 (in-place).
-
not
public DoubleMatrix not()
Maps zero to 1.0 and all non-zero values to 0.0.
-
truthi
public DoubleMatrix truthi()
Maps zero to 0.0 and all non-zero values to 1.0 (in-place).
-
truth
public DoubleMatrix truth()
Maps zero to 0.0 and all non-zero values to 1.0.
-
isNaNi
public DoubleMatrix isNaNi()
-
isNaN
public DoubleMatrix isNaN()
-
isInfinitei
public DoubleMatrix isInfinitei()
-
isInfinite
public DoubleMatrix isInfinite()
-
isLowerTriangular
public boolean isLowerTriangular()
Checks whether all entries (i, j) with i >= j are zero.
-
isUpperTriangular
public boolean isUpperTriangular()
Checks whether all entries (i, j) with i <= j are zero.
-
selecti
public DoubleMatrix selecti(DoubleMatrix where)
-
select
public DoubleMatrix select(DoubleMatrix where)
-
rankOneUpdate
public DoubleMatrix rankOneUpdate(double alpha, DoubleMatrix x, DoubleMatrix y)
Computes a rank-1-update A = A + alpha * x * y'.
-
rankOneUpdate
public DoubleMatrix rankOneUpdate(double alpha, DoubleMatrix x)
Computes a rank-1-update A = A + alpha * x * x'.
-
rankOneUpdate
public DoubleMatrix rankOneUpdate(DoubleMatrix x)
Computes a rank-1-update A = A + x * x'.
-
rankOneUpdate
public DoubleMatrix rankOneUpdate(DoubleMatrix x, DoubleMatrix y)
Computes a rank-1-update A = A + x * y'.
-
min
public double min()
Returns the minimal element of the matrix.
-
argmin
public int 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
public DoubleMatrix mini(DoubleMatrix other, DoubleMatrix result)
Computes the minimum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place).
-
mini
public DoubleMatrix mini(DoubleMatrix other)
Computes the minimum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place on this).
-
min
public DoubleMatrix min(DoubleMatrix other)
Computes the minimum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place on this).
-
mini
public DoubleMatrix mini(double v, DoubleMatrix result)
-
mini
public DoubleMatrix mini(double v)
-
min
public DoubleMatrix min(double v)
-
max
public double max()
Returns the maximal element of the matrix.
-
argmax
public int 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
public DoubleMatrix maxi(DoubleMatrix other, DoubleMatrix result)
Computes the maximum between two matrices. Returns the larger of the corresponding elements in the matrix (in-place).
-
maxi
public DoubleMatrix maxi(DoubleMatrix other)
Computes the maximum between two matrices. Returns the smaller of the corresponding elements in the matrix (in-place on this).
-
max
public DoubleMatrix max(DoubleMatrix other)
Computes the maximum between two matrices. Returns the larger of the corresponding elements in the matrix (in-place on this).
-
maxi
public DoubleMatrix maxi(double v, DoubleMatrix result)
-
maxi
public DoubleMatrix maxi(double v)
-
max
public DoubleMatrix max(double v)
-
sum
public double sum()
Computes the sum of all elements of the matrix.
-
prod
public double prod()
Computes the product of all elements of the matrix
-
mean
public double mean()
Computes the mean value of all elements in the matrix, that is,x.sum() / x.length.
-
cumulativeSumi
public DoubleMatrix 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
public DoubleMatrix cumulativeSum()
Computes the cumulative sum, that is, the sum of all elements of the matrix up to a given index in linear addressing.
-
dot
public double dot(DoubleMatrix other)
The scalar product of this with other.
-
project
public double project(DoubleMatrix other)
Computes the projection coefficient of other on this. The returned scalar times this is the orthogonal projection of other on this.
-
norm2
public double norm2()
The Euclidean norm of the matrix as vector, also the Frobenius norm of the matrix.
-
normmax
public double normmax()
The maximum norm of the matrix (maximal absolute value of the elements).
-
norm1
public double norm1()
The 1-norm of the matrix as vector (sum of absolute values of elements).
-
squaredDistance
public double squaredDistance(DoubleMatrix other)
Returns the squared (Euclidean) distance.
-
distance2
public double distance2(DoubleMatrix other)
Returns the (euclidean) distance.
-
distance1
public double distance1(DoubleMatrix other)
Returns the (1-norm) distance.
-
sort
public DoubleMatrix sort()
Return a new matrix with all elements sorted.
-
sorti
public DoubleMatrix sorti()
Sort elements in-place.
-
sortingPermutation
public int[] sortingPermutation()
Get the sorting permutation.- Returns:
- an int[] array such that which indexes the elements in sorted order.
-
sortColumnsi
public DoubleMatrix sortColumnsi()
Sort columns (in-place).
-
sortColumns
public DoubleMatrix sortColumns()
Sort columns.
-
columnSortingPermutations
public int[][] columnSortingPermutations()
Return matrix of indices which sort all columns.
-
sortRowsi
public DoubleMatrix sortRowsi()
Sort rows (in-place).
-
sortRows
public DoubleMatrix sortRows()
Sort rows.
-
rowSortingPermutations
public int[][] rowSortingPermutations()
Return matrix of indices which sort all columns.
-
columnSums
public DoubleMatrix columnSums()
Return a vector containing the sums of the columns (having number of columns many entries)
-
columnMeans
public DoubleMatrix columnMeans()
Return a vector containing the means of all columns.
-
rowSums
public DoubleMatrix rowSums()
Return a vector containing the sum of the rows.
-
rowMeans
public DoubleMatrix rowMeans()
Return a vector containing the means of the rows.
-
getColumn
public DoubleMatrix getColumn(int c)
Get a copy of a column.
-
getColumn
public DoubleMatrix getColumn(int c, DoubleMatrix result)
Copy a column to the given vector.
-
putColumn
public void putColumn(int c, DoubleMatrix v)Copy a column back into the matrix.
-
getRow
public DoubleMatrix getRow(int r)
Get a copy of a row.
-
getRow
public DoubleMatrix getRow(int r, DoubleMatrix result)
Copy a row to a given vector.
-
putRow
public void putRow(int r, DoubleMatrix v)Copy a row back into the matrix.
-
columnMins
public DoubleMatrix columnMins()
Return column-wise minimums.
-
columnArgmins
public int[] columnArgmins()
Return index of minimal element per column.
-
columnMaxs
public DoubleMatrix columnMaxs()
Return column-wise maximums.
-
columnArgmaxs
public int[] columnArgmaxs()
Return index of minimal element per column.
-
rowMins
public DoubleMatrix rowMins()
Return row-wise minimums.
-
rowArgmins
public int[] rowArgmins()
Return index of minimal element per row.
-
rowMaxs
public DoubleMatrix rowMaxs()
Return row-wise maximums.
-
rowArgmaxs
public int[] rowArgmaxs()
Return index of maximum element per row.
-
addiRowVector
public DoubleMatrix addiRowVector(DoubleMatrix x)
Add a row vector to all rows of the matrix (in place).
-
addRowVector
public DoubleMatrix addRowVector(DoubleMatrix x)
Add a row to all rows of the matrix.
-
addiColumnVector
public DoubleMatrix addiColumnVector(DoubleMatrix x)
Add a vector to all columns of the matrix (in-place).
-
addColumnVector
public DoubleMatrix addColumnVector(DoubleMatrix x)
Add a vector to all columns of the matrix.
-
subiRowVector
public DoubleMatrix subiRowVector(DoubleMatrix x)
Subtract a row vector from all rows of the matrix (in-place).
-
subRowVector
public DoubleMatrix subRowVector(DoubleMatrix x)
Subtract a row vector from all rows of the matrix.
-
subiColumnVector
public DoubleMatrix subiColumnVector(DoubleMatrix x)
Subtract a column vector from all columns of the matrix (in-place).
-
subColumnVector
public DoubleMatrix subColumnVector(DoubleMatrix x)
Subtract a vector from all columns of the matrix.
-
mulRow
public DoubleMatrix mulRow(int r, double scale)
Multiply a row by a scalar.
-
mulColumn
public DoubleMatrix mulColumn(int c, double scale)
Multiply a column by a scalar.
-
muliColumnVector
public DoubleMatrix muliColumnVector(DoubleMatrix x)
Multiply all columns with a column vector (in-place).
-
mulColumnVector
public DoubleMatrix mulColumnVector(DoubleMatrix x)
Multiply all columns with a column vector.
-
muliRowVector
public DoubleMatrix muliRowVector(DoubleMatrix x)
Multiply all rows with a row vector (in-place).
-
mulRowVector
public DoubleMatrix mulRowVector(DoubleMatrix x)
Multiply all rows with a row vector.
-
diviRowVector
public DoubleMatrix diviRowVector(DoubleMatrix x)
-
divRowVector
public DoubleMatrix divRowVector(DoubleMatrix x)
-
diviColumnVector
public DoubleMatrix diviColumnVector(DoubleMatrix x)
-
divColumnVector
public DoubleMatrix divColumnVector(DoubleMatrix x)
-
out
public void out(java.io.DataOutputStream dos) throws java.io.IOExceptionWrites out this matrix to the given data stream.- Parameters:
dos- the data output stream to write to.- Throws:
java.io.IOException
-
in
public void in(java.io.DataInputStream dis) throws java.io.IOExceptionReads 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:
java.io.IOException
-
save
public void save(java.lang.String filename) throws java.io.IOExceptionSaves this matrix to the specified file.- Parameters:
filename- the file to write the matrix in.- Throws:
java.io.IOException- thrown on errors while writing the matrix to the file
-
load
public void load(java.lang.String filename) throws java.io.IOExceptionLoads 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:
java.io.IOException- thrown on errors while reading the matrix
-
loadAsciiFile
public static DoubleMatrix loadAsciiFile(java.lang.String filename) throws java.io.IOException
- Throws:
java.io.IOException
-
loadCSVFile
public static DoubleMatrix loadCSVFile(java.lang.String filename) throws java.io.IOException
- Throws:
java.io.IOException
-
addi
public DoubleMatrix addi(DoubleMatrix other)
Add a matrix (in place).
-
add
public DoubleMatrix add(DoubleMatrix other)
Add a matrix.
-
addi
public DoubleMatrix addi(double v)
Add a scalar (in place).
-
add
public DoubleMatrix add(double v)
Add a scalar.
-
subi
public DoubleMatrix subi(DoubleMatrix other)
Subtract a matrix (in place).
-
sub
public DoubleMatrix sub(DoubleMatrix other)
Subtract a matrix.
-
subi
public DoubleMatrix subi(double v)
Subtract a scalar (in place).
-
sub
public DoubleMatrix sub(double v)
Subtract a scalar.
-
rsubi
public DoubleMatrix rsubi(DoubleMatrix other)
(right-)subtract a matrix (in place).
-
rsub
public DoubleMatrix rsub(DoubleMatrix other)
(right-)subtract a matrix.
-
rsubi
public DoubleMatrix rsubi(double v)
(right-)subtract a scalar (in place).
-
rsub
public DoubleMatrix rsub(double v)
(right-)subtract a scalar.
-
divi
public DoubleMatrix divi(DoubleMatrix other)
Elementwise divide by a matrix (in place).
-
div
public DoubleMatrix div(DoubleMatrix other)
Elementwise divide by a matrix.
-
divi
public DoubleMatrix divi(double v)
Elementwise divide by a scalar (in place).
-
div
public DoubleMatrix div(double v)
Elementwise divide by a scalar.
-
rdivi
public DoubleMatrix rdivi(DoubleMatrix other)
(right-)elementwise divide by a matrix (in place).
-
rdiv
public DoubleMatrix rdiv(DoubleMatrix other)
(right-)elementwise divide by a matrix.
-
rdivi
public DoubleMatrix rdivi(double v)
(right-)elementwise divide by a scalar (in place).
-
rdiv
public DoubleMatrix rdiv(double v)
(right-)elementwise divide by a scalar.
-
muli
public DoubleMatrix muli(DoubleMatrix other)
Elementwise multiply by a matrix (in place).
-
mul
public DoubleMatrix mul(DoubleMatrix other)
Elementwise multiply by a matrix.
-
muli
public DoubleMatrix muli(double v)
Elementwise multiply by a scalar (in place).
-
mul
public DoubleMatrix mul(double v)
Elementwise multiply by a scalar.
-
mmuli
public DoubleMatrix mmuli(DoubleMatrix other)
Matrix-multiply by a matrix (in place).
-
mmul
public DoubleMatrix mmul(DoubleMatrix other)
Matrix-multiply by a matrix.
-
mmuli
public DoubleMatrix mmuli(double v)
Matrix-multiply by a scalar (in place).
-
mmul
public DoubleMatrix mmul(double v)
Matrix-multiply by a scalar.
-
lti
public DoubleMatrix lti(DoubleMatrix other, DoubleMatrix result)
Test for "less than" (in-place).
-
lti
public DoubleMatrix lti(DoubleMatrix other)
Test for "less than" (in-place).
-
lt
public DoubleMatrix lt(DoubleMatrix other)
Test for "less than".
-
lti
public DoubleMatrix lti(double value, DoubleMatrix result)
Test for "less than" against a scalar (in-place).
-
lti
public DoubleMatrix lti(double value)
Test for "less than" against a scalar (in-place).
-
lt
public DoubleMatrix lt(double value)
test for "less than" against a scalar.
-
gti
public DoubleMatrix gti(DoubleMatrix other, DoubleMatrix result)
Test for "greater than" (in-place).
-
gti
public DoubleMatrix gti(DoubleMatrix other)
Test for "greater than" (in-place).
-
gt
public DoubleMatrix gt(DoubleMatrix other)
Test for "greater than".
-
gti
public DoubleMatrix gti(double value, DoubleMatrix result)
Test for "greater than" against a scalar (in-place).
-
gti
public DoubleMatrix gti(double value)
Test for "greater than" against a scalar (in-place).
-
gt
public DoubleMatrix gt(double value)
test for "greater than" against a scalar.
-
lei
public DoubleMatrix lei(DoubleMatrix other, DoubleMatrix result)
Test for "less than or equal" (in-place).
-
lei
public DoubleMatrix lei(DoubleMatrix other)
Test for "less than or equal" (in-place).
-
le
public DoubleMatrix le(DoubleMatrix other)
Test for "less than or equal".
-
lei
public DoubleMatrix lei(double value, DoubleMatrix result)
Test for "less than or equal" against a scalar (in-place).
-
lei
public DoubleMatrix lei(double value)
Test for "less than or equal" against a scalar (in-place).
-
le
public DoubleMatrix le(double value)
test for "less than or equal" against a scalar.
-
gei
public DoubleMatrix gei(DoubleMatrix other, DoubleMatrix result)
Test for "greater than or equal" (in-place).
-
gei
public DoubleMatrix gei(DoubleMatrix other)
Test for "greater than or equal" (in-place).
-
ge
public DoubleMatrix ge(DoubleMatrix other)
Test for "greater than or equal".
-
gei
public DoubleMatrix gei(double value, DoubleMatrix result)
Test for "greater than or equal" against a scalar (in-place).
-
gei
public DoubleMatrix gei(double value)
Test for "greater than or equal" against a scalar (in-place).
-
ge
public DoubleMatrix ge(double value)
test for "greater than or equal" against a scalar.
-
eqi
public DoubleMatrix eqi(DoubleMatrix other, DoubleMatrix result)
Test for equality (in-place).
-
eqi
public DoubleMatrix eqi(DoubleMatrix other)
Test for equality (in-place).
-
eq
public DoubleMatrix eq(DoubleMatrix other)
Test for equality.
-
eqi
public DoubleMatrix eqi(double value, DoubleMatrix result)
Test for equality against a scalar (in-place).
-
eqi
public DoubleMatrix eqi(double value)
Test for equality against a scalar (in-place).
-
eq
public DoubleMatrix eq(double value)
test for equality against a scalar.
-
nei
public DoubleMatrix nei(DoubleMatrix other, DoubleMatrix result)
Test for inequality (in-place).
-
nei
public DoubleMatrix nei(DoubleMatrix other)
Test for inequality (in-place).
-
ne
public DoubleMatrix ne(DoubleMatrix other)
Test for inequality.
-
nei
public DoubleMatrix nei(double value, DoubleMatrix result)
Test for inequality against a scalar (in-place).
-
nei
public DoubleMatrix nei(double value)
Test for inequality against a scalar (in-place).
-
ne
public DoubleMatrix ne(double value)
test for inequality against a scalar.
-
andi
public DoubleMatrix andi(DoubleMatrix other, DoubleMatrix result)
Compute elementwise logical and (in-place).
-
andi
public DoubleMatrix andi(DoubleMatrix other)
Compute elementwise logical and (in-place).
-
and
public DoubleMatrix and(DoubleMatrix other)
Compute elementwise logical and.
-
andi
public DoubleMatrix andi(double value, DoubleMatrix result)
Compute elementwise logical and against a scalar (in-place).
-
andi
public DoubleMatrix andi(double value)
Compute elementwise logical and against a scalar (in-place).
-
and
public DoubleMatrix and(double value)
Compute elementwise logical and against a scalar.
-
ori
public DoubleMatrix ori(DoubleMatrix other, DoubleMatrix result)
Compute elementwise logical or (in-place).
-
ori
public DoubleMatrix ori(DoubleMatrix other)
Compute elementwise logical or (in-place).
-
or
public DoubleMatrix or(DoubleMatrix other)
Compute elementwise logical or.
-
ori
public DoubleMatrix ori(double value, DoubleMatrix result)
Compute elementwise logical or against a scalar (in-place).
-
ori
public DoubleMatrix ori(double value)
Compute elementwise logical or against a scalar (in-place).
-
or
public DoubleMatrix or(double value)
Compute elementwise logical or against a scalar.
-
xori
public DoubleMatrix xori(DoubleMatrix other, DoubleMatrix result)
Compute elementwise logical xor (in-place).
-
xori
public DoubleMatrix xori(DoubleMatrix other)
Compute elementwise logical xor (in-place).
-
xor
public DoubleMatrix xor(DoubleMatrix other)
Compute elementwise logical xor.
-
xori
public DoubleMatrix xori(double value, DoubleMatrix result)
Compute elementwise logical xor against a scalar (in-place).
-
xori
public DoubleMatrix xori(double value)
Compute elementwise logical xor against a scalar (in-place).
-
xor
public DoubleMatrix xor(double value)
Compute elementwise logical xor against a scalar.
-
toComplex
public ComplexDoubleMatrix toComplex()
-
-