Class Matrix

java.lang.Object
de.labathome.trustconstr.matrix.Matrix
Direct Known Subclasses:
DenseMatrix, SparseMatrix

public abstract class Matrix extends Object
Abstract dense/sparse matrix base. Two concrete subclasses:

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.

  • 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 index
      col - 0-based column index
      Returns:
      entry at (row, col)
    • setAsDouble

      public abstract void setAsDouble(double v, long row, long col)
      Parameters:
      v - value to store
      row - 0-based row index
      col - 0-based column index
    • solve

      public Matrix solve(Matrix rhs)
      Solve this * x = rhs via LAPACK dgesv (general LU). Routes through LinAlg.solve(DenseMatrix, DenseMatrix) after densifying both operands.
      Parameters:
      rhs - right-hand side
      Returns:
      solution x
    • isSparse

      public boolean isSparse()
      Returns:
      true if this is a sparse matrix
    • toDoubleArray

      public double[][] toDoubleArray()
      Returns:
      a fresh row-major double[][] copy of the contents
    • copy

      public abstract Matrix copy()
      Returns:
      a deep copy of this matrix
    • toColumnArray

      public double[] toColumnArray()
      Returns:
      contents of an n x 1 column vector as a fresh double[n]; throws if the matrix is not a column vector
    • mtimes

      public abstract Matrix mtimes(Matrix other)
      Parameters:
      other - right operand
      Returns:
      matrix product this * other
    • plus

      public abstract Matrix plus(Matrix other)
      Parameters:
      other - right operand
      Returns:
      element-wise sum this + other
    • minus

      public abstract Matrix minus(Matrix other)
      Parameters:
      other - right operand
      Returns:
      element-wise difference this - other
    • plus

      public Matrix plus(double scalar)
      Parameters:
      scalar - value to add elementwise
      Returns:
      this + scalar
    • minus

      public Matrix minus(double scalar)
      Parameters:
      scalar - value to subtract elementwise
      Returns:
      this - scalar
    • times

      public abstract Matrix times(double alpha)
      Parameters:
      alpha - scalar
      Returns:
      alpha * this
    • times

      public abstract Matrix times(Matrix elementwise)
      Parameters:
      elementwise - same-shape factor
      Returns:
      Hadamard product this .* elementwise
    • divide

      public abstract Matrix divide(double alpha)
      Parameters:
      alpha - scalar
      Returns:
      this / alpha
    • divide

      public abstract Matrix divide(Matrix elementwise)
      Parameters:
      elementwise - same-shape divisor
      Returns:
      Hadamard division this ./ elementwise
    • transpose

      public abstract Matrix transpose()
      Returns:
      the transpose of this matrix
    • abs

      public abstract 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

      public Matrix subMatrix(long r0, long c0, long r1, long c1)
      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

      public Matrix selectColumns(long col)
      Parameters:
      col - 0-based column index
      Returns:
      that single column as an n x 1 matrix
    • absInPlace

      public Matrix absInPlace()
      In-place absolute value.
      Returns:
      this after 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 threshold max(m,n) * ulp(sigma_max).
      Returns:
      numerical rank (zero for empty matrices)
    • isRowVector

      public boolean isRowVector()
      Lenient shape predicate: returns true if 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:
      true if either rows == 1 or cols == 1
    • isColumnVector

      public boolean isColumnVector()
      Returns:
      true iff this matrix has exactly one column
    • normF

      public double normF()
      Returns:
      the Frobenius norm; alias for norm2() kept for clarity
    • equalsContent

      public boolean equalsContent(Matrix other)
      Parameters:
      other - matrix to compare against
      Returns:
      true iff other has the same shape and every entry compares bit-equal under ==
    • fill

      public Matrix fill(double v)
      In-place fill with the given scalar.
      Parameters:
      v - value to assign to every entry
      Returns:
      this after the mutation
    • doubleValue

      public double doubleValue()
      Extract the single scalar value of a 1 x 1 matrix.
      Returns:
      the only entry
      Throws:
      IllegalStateException - if the matrix is not 1 x 1
    • mapAdd

      protected Matrix mapAdd(double scalar)
      Helper used by plus(double) and minus(double): returns a fresh dense matrix with scalar added to every entry.
      Parameters:
      scalar - value to add elementwise
      Returns:
      a fresh DenseMatrix of the same shape