Class DenseMatrix

java.lang.Object
de.labathome.trustconstr.matrix.Matrix
de.labathome.trustconstr.matrix.DenseMatrix

public final class DenseMatrix extends Matrix
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).

  • Constructor Details

    • DenseMatrix

      public DenseMatrix(int rows, int cols)
      Allocate a fresh rows x cols zero-filled DenseMatrix.
      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 int accessor)
    • cols

      public int cols()
      Returns:
      number of columns (primitive int accessor)
    • 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 index i + j * rows
    • get

      public double get(int i, int j)
      Parameters:
      i - 0-based row index
      j - 0-based column index
      Returns:
      entry at (i, j)
    • set

      public void set(int i, int j, double v)
      Parameters:
      i - 0-based row index
      j - 0-based column index
      v - value to store at (i, j)
    • getRowCount

      public long getRowCount()
      Specified by:
      getRowCount in class Matrix
      Returns:
      number of rows
    • getColumnCount

      public long getColumnCount()
      Specified by:
      getColumnCount in class Matrix
      Returns:
      number of columns
    • getAsDouble

      public double getAsDouble(long row, long col)
      Specified by:
      getAsDouble in class Matrix
      Parameters:
      row - 0-based row index
      col - 0-based column index
      Returns:
      entry at (row, col)
    • setAsDouble

      public void setAsDouble(double v, long row, long col)
      Specified by:
      setAsDouble in class Matrix
      Parameters:
      v - value to store
      row - 0-based row index
      col - 0-based column index
    • copy

      public DenseMatrix copy()
      Specified by:
      copy in class Matrix
      Returns:
      a deep copy of this matrix
    • toDoubleArray

      public double[][] toDoubleArray()
      Overrides:
      toDoubleArray in class Matrix
      Returns:
      a fresh row-major double[][] copy of the contents
    • toColumnArray

      public double[] toColumnArray()
      Overrides:
      toColumnArray in class Matrix
      Returns:
      contents of an n x 1 column vector as a fresh double[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 index i*cols + j)
    • mtimes

      public DenseMatrix mtimes(Matrix other)
      Specified by:
      mtimes in class Matrix
      Parameters:
      other - right operand
      Returns:
      matrix product this * other
    • plus

      public DenseMatrix plus(Matrix other)
      Specified by:
      plus in class Matrix
      Parameters:
      other - right operand
      Returns:
      element-wise sum this + other
    • minus

      public DenseMatrix minus(Matrix other)
      Specified by:
      minus in class Matrix
      Parameters:
      other - right operand
      Returns:
      element-wise difference this - other
    • times

      public DenseMatrix times(double alpha)
      Specified by:
      times in class Matrix
      Parameters:
      alpha - scalar
      Returns:
      alpha * this
    • times

      public DenseMatrix times(Matrix elementwise)
      Specified by:
      times in class Matrix
      Parameters:
      elementwise - same-shape factor
      Returns:
      Hadamard product this .* elementwise
    • divide

      public DenseMatrix divide(double alpha)
      Specified by:
      divide in class Matrix
      Parameters:
      alpha - scalar
      Returns:
      this / alpha
    • divide

      public DenseMatrix divide(Matrix elementwise)
      Specified by:
      divide in class Matrix
      Parameters:
      elementwise - same-shape divisor
      Returns:
      Hadamard division this ./ elementwise
    • transpose

      public DenseMatrix transpose()
      Specified by:
      transpose in class Matrix
      Returns:
      the transpose of this matrix
    • abs

      public DenseMatrix abs()
      Specified by:
      abs in class Matrix
      Returns:
      element-wise absolute value as a fresh matrix
    • absInPlace

      public DenseMatrix absInPlace()
      In-place absolute value.
      Overrides:
      absInPlace in class Matrix
      Returns:
      this after replacing every entry by its absolute value
    • scaleInPlace

      public DenseMatrix scaleInPlace(double alpha)
      In-place scaling.
      Parameters:
      alpha - scalar multiplier
      Returns:
      this after multiplying every entry by alpha
    • norm2

      public double norm2()
      Specified by:
      norm2 in class Matrix
      Returns:
      Frobenius norm
    • normInf

      public double normInf()
      Specified by:
      normInf in class Matrix
      Returns:
      infinity-norm (largest absolute entry)
    • subMatrix

      public DenseMatrix subMatrix(int r0, int r1, int c0, int c1)
      Inclusive submatrix slice rows[r0..r1] x cols[c0..c1]. Always returns a fresh DenseMatrix; 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

      public DenseMatrix selectColumns(int... colsToKeep)
      Pick an arbitrary subset of columns into a fresh DenseMatrix.
      Parameters:
      colsToKeep - column indices to keep, in the desired output order
      Returns:
      rows x colsToKeep.length matrix
    • selectRows

      public DenseMatrix selectRows(int... rowsToKeep)
      Pick an arbitrary subset of rows into a fresh DenseMatrix.
      Parameters:
      rowsToKeep - row indices to keep, in the desired output order
      Returns:
      rowsToKeep.length x cols matrix
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • zeros

      public static DenseMatrix zeros(int rows, int cols)
      Parameters:
      rows - number of rows (must be non-negative)
      cols - number of columns (must be non-negative)
      Returns:
      a fresh rows x cols matrix filled with zeros
    • eye

      public static DenseMatrix eye(int n)
      Parameters:
      n - dimension (must be non-negative)
      Returns:
      the n x n identity matrix
    • column

      public static DenseMatrix column(double... v)
      Parameters:
      v - entries of the resulting column vector (copied)
      Returns:
      an n x 1 column vector
    • row

      public static DenseMatrix row(double... v)
      Parameters:
      v - entries of the resulting row vector (copied)
      Returns:
      a 1 x n row vector
    • fromRows

      public static DenseMatrix fromRows(double[][] rows)
      Repack a row-major double[][] into a fresh column-major DenseMatrix.
      Parameters:
      rows - row-major source (rows[i][j] = entry at (i,j))
      Returns:
      a fresh DenseMatrix with the same shape and values
    • fromColumnMajor

      public static DenseMatrix fromColumnMajor(int rows, int cols, double[] data)
      Wrap an already-column-major buffer without copying. The caller surrenders ownership of data: subsequent mutations through this matrix will be visible to anyone else holding the array.
      Parameters:
      rows - number of rows
      cols - number of columns
      data - column-major buffer of length rows * cols; the entry at (i, j) is data[i + j * rows]
      Returns:
      a DenseMatrix that aliases data
    • copyFromMatrix

      public static DenseMatrix copyFromMatrix(Matrix m)
      Densify any Matrix: if m is already a DenseMatrix, return a copy(); otherwise materialise via getAsDouble(long, long).
      Parameters:
      m - source matrix
      Returns:
      a fresh dense copy