Package de.labathome.trustconstr.matrix
Class Matrix
java.lang.Object
de.labathome.trustconstr.matrix.Matrix
- Direct Known Subclasses:
DenseMatrix,SparseMatrix
Abstract dense/sparse matrix base. Two concrete subclasses:
DenseMatrix-- column-majordouble[]buffer; the BLAS and LAPACK kernels inLinAlgtakeDenseMatrix.data()directly with no copy.SparseMatrix-- Dictionary-of-Keys; cheap incrementalsetAsDouble(double, long, long)plusSparseMatrix.toCSR()to materialise aCSRMatrixfor compute.
The base class exposes only the operations both layouts must support:
shape queries, element get/set, arithmetic, norms, slicing, scalar
extraction, and an in-place fill/absInPlace. The fast
paths (BLAS dgemm / dsyr / dsyr2, LU/QR/SVD/Cholesky via LAPACK) live on
DenseMatrix and LinAlg.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract Matrixabs()In-place absolute value.abstract Matrixcopy()abstract Matrixdivide(double alpha) abstract MatrixdoubleExtract the single scalar value of a1 x 1matrix.booleanequalsContent(Matrix other) fill(double v) In-place fill with the given scalar.abstract doublegetAsDouble(long row, long col) abstract longabstract longlong[]getSize()booleanbooleanLenient shape predicate: returnstrueif either dimension is 1 -- i.e. the matrix is shaped like a vector (row, column, or 1×1 scalar).booleanisSparse()protected MatrixmapAdd(double scalar) Helper used byplus(double)andminus(double): returns a fresh dense matrix withscalaradded to every entry.minus(double scalar) abstract Matrixabstract Matrixabstract doublenorm2()doublenormF()abstract doublenormInf()plus(double scalar) abstract Matrixlongrank()Rank computed via SVD: count singular values strictly greater than the default LAPACK numerical-zero thresholdmax(m,n) * ulp(sigma_max).selectColumns(long col) abstract voidsetAsDouble(double v, long row, long col) Solvethis * x = rhsvia LAPACKdgesv(general LU).subMatrix(long r0, long c0, long r1, long c1) Slice rows[r0..r1]and columns[c0..c1](inclusive).abstract Matrixtimes(double alpha) abstract Matrixdouble[]double[][]abstract Matrix
-
Constructor Details
-
Matrix
public Matrix()
-
-
Method Details
-
getRowCount
public abstract long getRowCount()- Returns:
- number of rows
-
getColumnCount
public abstract long getColumnCount()- Returns:
- number of columns
-
getSize
public long[] getSize()- Returns:
- shape as a length-2 array
[rows, cols]
-
getAsDouble
public abstract double getAsDouble(long row, long col) - Parameters:
row- 0-based row indexcol- 0-based column index- Returns:
- entry at
(row, col)
-
setAsDouble
public abstract void setAsDouble(double v, long row, long col) - Parameters:
v- value to storerow- 0-based row indexcol- 0-based column index
-
solve
Solvethis * x = rhsvia LAPACKdgesv(general LU). Routes throughLinAlg.solve(DenseMatrix, DenseMatrix)after densifying both operands.- Parameters:
rhs- right-hand side- Returns:
- solution
x
-
isSparse
public boolean isSparse()- Returns:
trueif this is a sparse matrix
-
toDoubleArray
public double[][] toDoubleArray()- Returns:
- a fresh row-major
double[][]copy of the contents
-
copy
- Returns:
- a deep copy of this matrix
-
toColumnArray
public double[] toColumnArray()- Returns:
- contents of an
n x 1column vector as a freshdouble[n]; throws if the matrix is not a column vector
-
mtimes
- Parameters:
other- right operand- Returns:
- matrix product
this * other
-
plus
- Parameters:
other- right operand- Returns:
- element-wise sum
this + other
-
minus
- Parameters:
other- right operand- Returns:
- element-wise difference
this - other
-
plus
- Parameters:
scalar- value to add elementwise- Returns:
this + scalar
-
minus
- Parameters:
scalar- value to subtract elementwise- Returns:
this - scalar
-
times
- Parameters:
alpha- scalar- Returns:
alpha * this
-
times
- Parameters:
elementwise- same-shape factor- Returns:
- Hadamard product
this .* elementwise
-
divide
- Parameters:
alpha- scalar- Returns:
this / alpha
-
divide
- Parameters:
elementwise- same-shape divisor- Returns:
- Hadamard division
this ./ elementwise
-
transpose
- Returns:
- the transpose of this matrix
-
abs
- Returns:
- element-wise absolute value as a fresh matrix
-
norm2
public abstract double norm2()- Returns:
- Frobenius norm
-
normInf
public abstract double normInf()- Returns:
- infinity-norm (largest absolute entry)
-
subMatrix
Slice rows[r0..r1]and columns[c0..c1](inclusive). Always returns a fresh dense matrix.- Parameters:
r0- first row (inclusive)c0- first column (inclusive)r1- last row (inclusive)c1- last column (inclusive)- Returns:
- the
(r1-r0+1) x (c1-c0+1)slice
-
selectColumns
- Parameters:
col- 0-based column index- Returns:
- that single column as an
n x 1matrix
-
absInPlace
In-place absolute value.- Returns:
thisafter replacing every entry by its absolute value
-
rank
public long rank()Rank computed via SVD: count singular values strictly greater than the default LAPACK numerical-zero thresholdmax(m,n) * ulp(sigma_max).- Returns:
- numerical rank (zero for empty matrices)
-
isRowVector
public boolean isRowVector()Lenient shape predicate: returnstrueif either dimension is 1 -- i.e. the matrix is shaped like a vector (row, column, or 1×1 scalar). Used by BFGS / SR1 / FullHessianUpdateStrategy as a shape precondition on their step / gradient inputs.- Returns:
trueif eitherrows == 1orcols == 1
-
isColumnVector
public boolean isColumnVector()- Returns:
trueiff this matrix has exactly one column
-
normF
public double normF()- Returns:
- the Frobenius norm; alias for
norm2()kept for clarity
-
equalsContent
- Parameters:
other- matrix to compare against- Returns:
trueiffotherhas the same shape and every entry compares bit-equal under==
-
fill
In-place fill with the given scalar.- Parameters:
v- value to assign to every entry- Returns:
thisafter the mutation
-
doubleValue
public double doubleValue()Extract the single scalar value of a1 x 1matrix.- Returns:
- the only entry
- Throws:
IllegalStateException- if the matrix is not1 x 1
-
mapAdd
Helper used byplus(double)andminus(double): returns a fresh dense matrix withscalaradded to every entry.- Parameters:
scalar- value to add elementwise- Returns:
- a fresh
DenseMatrixof the same shape
-