Class DenseMatrix

java.lang.Object
smile.tensor.DenseMatrix
All Implemented Interfaces:
Serializable, Matrix, Tensor
Direct Known Subclasses:
Vector

public abstract class DenseMatrix extends Object implements Matrix, Serializable
A dense matrix is a matrix where a large proportion of its elements are non-zero. This class provides a skeletal implementation of the Matrix interface.
See Also:
  • Method Details

    • toString

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

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • copy

      public abstract DenseMatrix copy()
      Description copied from interface: Matrix
      Returns a deep copy of matrix.
      Specified by:
      copy in interface Matrix
      Returns:
      a deep copy of matrix.
    • transpose

      public DenseMatrix transpose()
      Description copied from interface: Matrix
      Returns the transpose of matrix. The transpose may share the storage with this matrix.
      Specified by:
      transpose in interface Matrix
      Returns:
      the transpose of matrix.
    • scale

      public DenseMatrix scale(double alpha)
      Description copied from interface: Matrix
      A *= alpha
      Specified by:
      scale in interface Matrix
      Parameters:
      alpha - the scaling factor.
      Returns:
      this matrix.
    • nrow

      public int nrow()
      Description copied from interface: Matrix
      Returns the number of rows.
      Specified by:
      nrow in interface Matrix
      Returns:
      the number of rows.
    • ncol

      public int ncol()
      Description copied from interface: Matrix
      Returns the number of columns.
      Specified by:
      ncol in interface Matrix
      Returns:
      the number of columns.
    • order

      public Order order()
      Returns the matrix layout.
      Returns:
      the matrix layout.
    • memory

      public MemorySegment memory()
      Returns the memory storage of matrix.
      Returns:
      the memory storage of matrix.
    • ld

      public int ld()
      Returns the leading dimension.
      Returns:
      the leading dimension.
    • isSymmetric

      public boolean isSymmetric()
      Return true if the matrix is symmetric (uplo != null && diag == null).
      Returns:
      true if the matrix is symmetric.
    • withUplo

      public DenseMatrix withUplo(UPLO uplo)
      Sets the format of packed matrix.
      Parameters:
      uplo - the format of packed matrix.
      Returns:
      this matrix.
    • uplo

      public UPLO uplo()
      Gets the format of packed matrix.
      Returns:
      the format of packed matrix.
    • withDiag

      public DenseMatrix withDiag(Diag diag)
      Sets/unsets if the matrix is triangular.
      Parameters:
      diag - if not null, it specifies if the triangular matrix has unit diagonal elements.
      Returns:
      this matrix.
    • diag

      public Diag diag()
      Gets the flag if a triangular matrix has unit diagonal elements. Returns null if the matrix is not triangular.
      Returns:
      the flag if a triangular matrix has unit diagonal elements.
    • rowNames

      public String[] rowNames()
      Returns the row names.
      Returns:
      the row names.
    • withRowNames

      public DenseMatrix withRowNames(String[] names)
      Sets the row names.
      Parameters:
      names - the row names.
      Returns:
      this matrix.
    • colNames

      public String[] colNames()
      Returns the column names.
      Returns:
      the column names.
    • withColNames

      public DenseMatrix withColNames(String[] names)
      Sets the column names.
      Parameters:
      names - the column names.
      Returns:
      this matrix.
    • fill

      public abstract void fill(double value)
      Assigns the specified value to each element of the specified vector.
      Parameters:
      value - the value to be stored in all elements of the vector.
    • row

      public Vector row(int i)
      Returns the i-th row. Negative index -i means the i-th row from the end.
      Parameters:
      i - the row index.
      Returns:
      the row.
    • column

      public abstract Vector column(int j)
      Returns the j-th column. Negative index -j means the j-th row from the end.
      Parameters:
      j - the column index.
      Returns:
      the column.
    • rows

      public DenseMatrix rows(int... rows)
      Returns the matrix of selected rows. Negative index -i means the i-th row from the end.
      Parameters:
      rows - the row indices.
      Returns:
      the submatrix.
    • columns

      public DenseMatrix columns(int... cols)
      Returns the matrix of selected columns.
      Parameters:
      cols - the column indices.
      Returns:
      the submatrix.
    • rows

      public DenseMatrix rows(int from, int to)
      Returns the submatrix of selected rows.
      Parameters:
      from - the beginning row, inclusive.
      to - the ending row, exclusive.
      Returns:
      the submatrix.
    • columns

      public DenseMatrix columns(int from, int to)
      Returns the submatrix of selected columns.
      Parameters:
      from - the beginning column, inclusive.
      to - the ending column, exclusive.
      Returns:
      the submatrix.
    • submatrix

      public DenseMatrix submatrix(int i, int j, int k, int l)
      Returns the submatrix which top left at (i, j) and bottom right at (k, l).
      Parameters:
      i - the beginning row, inclusive.
      j - the beginning column, inclusive,
      k - the ending row, exclusive.
      l - the ending column, exclusive.
      Returns:
      the submatrix.
    • colSums

      public Vector colSums()
      Returns the sum of each column.
      Returns:
      the sum of each column.
    • rowSums

      public Vector rowSums()
      Returns the sum of each row.
      Returns:
      the sum of each row.
    • colMeans

      public Vector colMeans()
      Returns the mean of each column.
      Returns:
      the mean of each column.
    • rowMeans

      public Vector rowMeans()
      Returns the mean of each row.
      Returns:
      the mean of each row.
    • colSds

      public Vector colSds()
      Returns the standard deviations of each column.
      Returns:
      the standard deviations of each column.
    • standardize

      public DenseMatrix standardize()
      Standardizes the columns of matrix.
      Returns:
      a new matrix with zero mean and unit variance for each column.
    • standardize

      public DenseMatrix standardize(Vector center, Vector scale)
      Centers and scales the columns of matrix.
      Parameters:
      center - column center. If null, no centering.
      scale - column scale. If null, no scaling.
      Returns:
      a new normalized matrix.
    • add

      public void add(double alpha, DenseMatrix A, double beta, DenseMatrix B)
      Adds two matrices
          C = alpha * A + beta * B
      
      Parameters:
      alpha - the scalar alpha.
      A - the input matrix.
      beta - the scalar beta.
      B - the input matrix.
    • axpy

      public DenseMatrix axpy(double alpha, DenseMatrix x)
      Computes a constant alpha times a matrix x plus this matrix y. The result overwrites this matrix y.
      Parameters:
      alpha - If alpha = 0 this routine returns without any computation.
      x - Input matrix.
      Returns:
      this matrix.
    • add

      public DenseMatrix add(DenseMatrix B)
      Adds two matrices
          A += B
      
      Parameters:
      B - the input matrix.
      Returns:
      this matrix.
    • sub

      public DenseMatrix sub(DenseMatrix B)
      Subtracts two matrices
          A -= B
      
      Parameters:
      B - the input matrix.
      Returns:
      this matrix.
    • mv

      public void mv(Transpose trans, double alpha, Vector x, double beta, Vector y)
      Description copied from interface: Matrix
      Matrix-vector multiplication.
          y = alpha * A * x + beta * y
      
      Specified by:
      mv in interface Matrix
      Parameters:
      trans - normal, transpose, or conjugate transpose operation on the matrix.
      alpha - the scalar alpha.
      x - the input vector.
      beta - the scalar beta. When beta is supplied as zero, y need not be set on input.
      y - the input and output vector.
    • mm

      public static void mm(double alpha, Transpose transA, DenseMatrix A, Transpose transB, DenseMatrix B, double beta, DenseMatrix C)
      Matrix-matrix multiplication.
          C := alpha*A*B + beta*C
      
      Parameters:
      alpha - the scalar alpha.
      transA - normal, transpose, or conjugate transpose operation on the matrix A.
      A - the operand.
      transB - normal, transpose, or conjugate transpose operation on the matrix B.
      B - the operand.
      beta - the scalar beta.
      C - the operand.
    • mm

      public DenseMatrix mm(DenseMatrix B)
      Matrix multiplication A * B.
      Parameters:
      B - the operand.
      Returns:
      the multiplication.
    • tm

      public DenseMatrix tm(DenseMatrix B)
      Matrix multiplication A' * B.
      Parameters:
      B - the operand.
      Returns:
      the multiplication.
    • mt

      public DenseMatrix mt(DenseMatrix B)
      Matrix multiplication A * B'.
      Parameters:
      B - the operand.
      Returns:
      the multiplication.
    • ata

      public DenseMatrix ata()
      Returns A' * A.
      Returns:
      A' * A.
    • aat

      public DenseMatrix aat()
      Returns A * A'.
      Returns:
      A * A'.
    • ger

      public void ger(double alpha, Vector x, Vector y)
      Performs the rank-1 update operation.
          A := A + alpha*x*y'
      
      Parameters:
      alpha - the scalar alpha.
      x - the left vector.
      y - the right vector.
    • inverse

      public DenseMatrix inverse()
      Returns the inverse of matrix.
      Returns:
      the inverse of matrix.
    • lu

      public LU lu()
      LU decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix.
      Returns:
      LU decomposition.
    • cholesky

      public Cholesky cholesky()
      Cholesky decomposition for symmetric and positive definite matrix. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix.
      Returns:
      Cholesky decomposition.
      Throws:
      ArithmeticException - if the matrix is not positive definite.
    • qr

      public QR qr()
      QR Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix.
      Returns:
      QR decomposition.
    • svd

      public SVD svd()
      Singular Value Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. Returns a compact SVD of m-by-n matrix A:
      • m > n — Only the first n columns of U are computed, and S is n-by-n.
      • m = n — Equivalent to full SVD.
      • m < n — Only the first m columns of V are computed, and S is m-by-m.
      The compact decomposition removes extra rows or columns of zeros from the diagonal matrix of singular values, S, along with the columns in either U or V that multiply those zeros in the expression A = U*S*V'. Removing these zeros and columns can improve execution time and reduce storage requirements without compromising the accuracy of the decomposition.
      Returns:
      singular value decomposition.
    • svd

      public SVD svd(boolean vectors)
      Singular Value Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. Returns a compact SVD of m-by-n matrix A:
      • m > n — Only the first n columns of U are computed, and S is n-by-n.
      • m = n — Equivalent to full SVD.
      • m < n — Only the first m columns of V are computed, and S is m-by-m.
      The compact decomposition removes extra rows or columns of zeros from the diagonal matrix of singular values, S, along with the columns in either U or V that multiply those zeros in the expression A = U*S*V'. Removing these zeros and columns can improve execution time and reduce storage requirements without compromising the accuracy of the decomposition.
      Parameters:
      vectors - The flag whether computing the singular vectors.
      Returns:
      singular value decomposition.
    • eigen

      public EVD eigen()
      Right Eigenvalue Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. For a symmetric matrix, all eigenvalues are real values. Otherwise, the eigenvalues may be complex numbers.

      By default eigen does not always return the eigenvalues and eigenvectors in sorted order. Use the EVD.sort function to put the eigenvalues in descending order and reorder the corresponding eigenvectors.

      Returns:
      eign value decomposition.
    • eigen

      public EVD eigen(boolean vl, boolean vr)
      Eigenvalue Decomposition. The decomposition will overwrite this matrix. Makes a copy first if you want to keep the matrix. For a symmetric matrix, all eigenvalues are real values. Otherwise, the eigenvalues may be complex numbers.

      By default eigen does not always return the eigenvalues and eigenvectors in sorted order. Use the sort function to put the eigenvalues in descending order and reorder the corresponding eigenvectors.

      Parameters:
      vl - The flag if computing the left eigenvectors.
      vr - The flag if computing the right eigenvectors.
      Returns:
      eigen value decomposition.
    • of

      public static DenseMatrix of(double[][] A)
      Returns a matrix from a two-dimensional array.
      Parameters:
      A - the two-dimensional array.
      Returns:
      the matrix.
    • of

      public static DenseMatrix of(float[][] A)
      Returns a matrix from a two-dimensional array.
      Parameters:
      A - the two-dimensional array.
      Returns:
      the matrix.
    • zeros

      public static DenseMatrix zeros(ScalarType scalarType, int m, int n)
      Returns a zero matrix.
      Parameters:
      scalarType - the scalar type.
      m - the number of rows.
      n - the number of columns.
      Returns:
      a zero matrix.
    • zeros

      public DenseMatrix zeros(int m, int n)
      Returns a zero matrix of the same scalar type as this matrix.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      Returns:
      a zero matrix.
    • eye

      public static DenseMatrix eye(ScalarType scalarType, int n)
      Returns an identity matrix.
      Parameters:
      scalarType - the scalar type.
      n - the number of columns.
      Returns:
      an identity matrix.
    • eye

      public static DenseMatrix eye(ScalarType scalarType, int m, int n)
      Returns an identity matrix.
      Parameters:
      scalarType - the scalar type.
      m - the number of rows.
      n - the number of columns.
      Returns:
      an identity matrix.
    • eye

      public DenseMatrix eye(int n)
      Returns an identity matrix of the same scalar type as this matrix.
      Parameters:
      n - the number of columns.
      Returns:
      an identity matrix.
    • eye

      public DenseMatrix eye(int m, int n)
      Returns an identity matrix of the same scalar type as this matrix.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      Returns:
      an identity matrix.
    • diagflat

      public static DenseMatrix diagflat(double[] diag)
      Returns the diagonal matrix with the elements of given array.
      Parameters:
      diag - the diagonal elements.
      Returns:
      the diagonal matrix.
    • diagflat

      public static DenseMatrix diagflat(float[] diag)
      Returns the diagonal matrix with the elements of given array.
      Parameters:
      diag - the diagonal elements.
      Returns:
      the diagonal matrix.
    • randn

      public static DenseMatrix randn(ScalarType scalarType, int m, int n)
      Returns a random matrix of standard normal distribution.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      Returns:
      the random matrix.
    • rand

      public static DenseMatrix rand(ScalarType scalarType, int m, int n, Distribution distribution)
      Returns a random matrix with given distribution.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      distribution - the distribution of random numbers.
      Returns:
      the random matrix.
    • rand

      public static DenseMatrix rand(ScalarType scalarType, int m, int n)
      Returns a uniformly distributed random matrix in [0, 1).
      Parameters:
      m - the number of rows.
      n - the number of columns.
      Returns:
      the random matrix.
    • rand

      public static DenseMatrix rand(ScalarType scalarType, int m, int n, double lo, double hi)
      Returns a uniformly distributed random matrix in [lo, hi).
      Parameters:
      m - the number of rows.
      n - the number of columns.
      lo - the lower bound of uniform distribution.
      hi - the upper bound of uniform distribution.
      Returns:
      the random matrix.
    • toeplitz

      public static DenseMatrix toeplitz(double[] a)
      Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.
      Parameters:
      a - A[i, j] = a[i - j] for i >= j (or a[j - i] when j > i)
      Returns:
      the Toeplitz matrix.
    • toeplitz

      public static DenseMatrix toeplitz(float[] a)
      Returns a symmetric Toeplitz matrix in which each descending diagonal from left to right is constant.
      Parameters:
      a - A[i, j] = a[i - j] for i >= j (or a[j - i] when j > i)
      Returns:
      the Toeplitz matrix.
    • toeplitz

      public static DenseMatrix toeplitz(double[] kl, double[] ku)
      Returns a Toeplitz matrix in which each descending diagonal from left to right is constant.
      Parameters:
      kl - A[i, j] = kl[i - j] for i > j
      ku - A[i, j] = ku[j - i] for i <= j
      Returns:
      the Toeplitz matrix.
    • toeplitz

      public static DenseMatrix toeplitz(float[] kl, float[] ku)
      Returns a Toeplitz matrix in which each descending diagonal from left to right is constant.
      Parameters:
      kl - A[i, j] = kl[i - j] for i > j
      ku - A[i, j] = ku[j - i] for i <= j
      Returns:
      the Toeplitz matrix.