Package de.labathome.trustconstr.matrix
Class DenseMatrix
java.lang.Object
de.labathome.trustconstr.matrix.Matrix
de.labathome.trustconstr.matrix.DenseMatrix
Dense matrix backed by a column-major
double[] buffer of length
rows * cols. Element (i, j) is stored at
data[i + j * rows].
Column-major is the layout LAPACK and BLAS expect, so data()
can be passed directly to dgesv / dgeqrf / dgesvd /
dpotrf / dsyr / dsyr2 with no copy. The hot kernel
mtimes(Matrix) dispatches to BLAS dgemm when both operands
are DenseMatrix; everything else falls back to a triple loop.
Subclass of Matrix so existing call sites that accept a generic
Matrix keep working unchanged. As the migration progresses, callers
narrow their declared types to DenseMatrix where the dense
specialisation matters (BLAS dgemm fast path or data() pass-through
to LAPACK).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBackwards-compatible nested factory class. -
Constructor Summary
ConstructorsConstructorDescriptionDenseMatrix(int rows, int cols) Allocate a freshrows x colszero-filledDenseMatrix. -
Method Summary
Modifier and TypeMethodDescriptionabs()In-place absolute value.intcols()static DenseMatrixcolumn(double... v) copy()static DenseMatrixDensify anyMatrix: ifmis already aDenseMatrix, return acopy(); otherwise materialise viagetAsDouble(long, long).double[]data()The underlying column-major buffer.divide(double alpha) static DenseMatrixeye(int n) static DenseMatrixfromColumnMajor(int rows, int cols, double[] data) Wrap an already-column-major buffer without copying.static DenseMatrixfromRows(double[][] rows) Repack a row-majordouble[][]into a fresh column-majorDenseMatrix.doubleget(int i, int j) doublegetAsDouble(long row, long col) longlongdoublenorm2()doublenormInf()static DenseMatrixrow(double... v) introws()scaleInPlace(double alpha) In-place scaling.selectColumns(int... colsToKeep) Pick an arbitrary subset of columns into a freshDenseMatrix.selectRows(int... rowsToKeep) Pick an arbitrary subset of rows into a freshDenseMatrix.voidset(int i, int j, double v) voidsetAsDouble(double v, long row, long col) subMatrix(int r0, int r1, int c0, int c1) Inclusive submatrix slicerows[r0..r1] x cols[c0..c1].times(double alpha) double[]double[][]double[]toString()static DenseMatrixzeros(int rows, int cols) Methods inherited from class de.labathome.trustconstr.matrix.Matrix
doubleValue, equalsContent, fill, getSize, isColumnVector, isRowVector, isSparse, mapAdd, minus, normF, plus, rank, selectColumns, solve, subMatrix
-
Constructor Details
-
DenseMatrix
public DenseMatrix(int rows, int cols) Allocate a freshrows x colszero-filledDenseMatrix.- Parameters:
rows- number of rows (must be non-negative)cols- number of columns (must be non-negative)
-
-
Method Details
-
rows
public int rows()- Returns:
- number of rows (primitive
intaccessor)
-
cols
public int cols()- Returns:
- number of columns (primitive
intaccessor)
-
data
public double[] data()The underlying column-major buffer. Mutable: callers may overwrite entries directly (used by BLAS rank updates and LAPACK in-place ops).- Returns:
- the live column-major buffer of length
rows * cols; entry(i, j)at indexi + j * rows
-
get
public double get(int i, int j) - Parameters:
i- 0-based row indexj- 0-based column index- Returns:
- entry at
(i, j)
-
set
public void set(int i, int j, double v) - Parameters:
i- 0-based row indexj- 0-based column indexv- value to store at(i, j)
-
getRowCount
public long getRowCount()- Specified by:
getRowCountin classMatrix- Returns:
- number of rows
-
getColumnCount
public long getColumnCount()- Specified by:
getColumnCountin classMatrix- Returns:
- number of columns
-
getAsDouble
public double getAsDouble(long row, long col) - Specified by:
getAsDoublein classMatrix- Parameters:
row- 0-based row indexcol- 0-based column index- Returns:
- entry at
(row, col)
-
setAsDouble
public void setAsDouble(double v, long row, long col) - Specified by:
setAsDoublein classMatrix- Parameters:
v- value to storerow- 0-based row indexcol- 0-based column index
-
copy
-
toDoubleArray
public double[][] toDoubleArray()- Overrides:
toDoubleArrayin classMatrix- Returns:
- a fresh row-major
double[][]copy of the contents
-
toColumnArray
public double[] toColumnArray()- Overrides:
toColumnArrayin classMatrix- Returns:
- contents of an
n x 1column vector as a freshdouble[n]; throws if the matrix is not a column vector
-
toRowMajor
public double[] toRowMajor()- Returns:
- contents repacked as a fresh row-major flat
double[rows*cols](entry(i, j)at indexi*cols + j)
-
mtimes
-
plus
-
minus
-
times
-
times
-
divide
-
divide
-
transpose
-
abs
-
absInPlace
In-place absolute value.- Overrides:
absInPlacein classMatrix- Returns:
thisafter replacing every entry by its absolute value
-
scaleInPlace
In-place scaling.- Parameters:
alpha- scalar multiplier- Returns:
thisafter multiplying every entry byalpha
-
norm2
public double norm2() -
normInf
public double normInf() -
subMatrix
Inclusive submatrix slicerows[r0..r1] x cols[c0..c1]. Always returns a freshDenseMatrix; no view aliasing.- Parameters:
r0- first row (inclusive)r1- last row (inclusive)c0- first column (inclusive)c1- last column (inclusive)- Returns:
(r1-r0+1) x (c1-c0+1)slice
-
selectColumns
Pick an arbitrary subset of columns into a freshDenseMatrix.- Parameters:
colsToKeep- column indices to keep, in the desired output order- Returns:
rows x colsToKeep.lengthmatrix
-
selectRows
Pick an arbitrary subset of rows into a freshDenseMatrix.- Parameters:
rowsToKeep- row indices to keep, in the desired output order- Returns:
rowsToKeep.length x colsmatrix
-
toString
-
zeros
- Parameters:
rows- number of rows (must be non-negative)cols- number of columns (must be non-negative)- Returns:
- a fresh
rows x colsmatrix filled with zeros
-
eye
- Parameters:
n- dimension (must be non-negative)- Returns:
- the
n x nidentity matrix
-
column
- Parameters:
v- entries of the resulting column vector (copied)- Returns:
- an
n x 1column vector
-
row
- Parameters:
v- entries of the resulting row vector (copied)- Returns:
- a
1 x nrow vector
-
fromRows
Repack a row-majordouble[][]into a fresh column-majorDenseMatrix.- Parameters:
rows- row-major source (rows[i][j]= entry at(i,j))- Returns:
- a fresh
DenseMatrixwith the same shape and values
-
fromColumnMajor
Wrap an already-column-major buffer without copying. The caller surrenders ownership ofdata: subsequent mutations through this matrix will be visible to anyone else holding the array.- Parameters:
rows- number of rowscols- number of columnsdata- column-major buffer of lengthrows * cols; the entry at(i, j)isdata[i + j * rows]- Returns:
- a
DenseMatrixthat aliasesdata
-
copyFromMatrix
Densify anyMatrix: ifmis already aDenseMatrix, return acopy(); otherwise materialise viagetAsDouble(long, long).- Parameters:
m- source matrix- Returns:
- a fresh dense copy
-