Class BigMatrix

java.lang.Object
smile.math.matrix.IMatrix
smile.math.matrix.BigMatrix
All Implemented Interfaces:
Serializable, Cloneable

public class BigMatrix extends IMatrix
Big dense matrix of double precision values for more than 2 billion elements.
See Also:
  • Constructor Details

    • BigMatrix

      public BigMatrix(int m, int n)
      Constructor of zero matrix.
      Parameters:
      m - the number of rows.
      n - the number of columns.
    • BigMatrix

      public BigMatrix(int m, int n, double a)
      Constructor. Fills the matrix with given value.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      a - the initial value.
    • BigMatrix

      public BigMatrix(int m, int n, int ld, org.bytedeco.javacpp.DoublePointer A)
      Constructor.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      ld - the leading dimension.
      A - the matrix storage.
  • Method Details

    • of

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

      public static BigMatrix column(double[] A)
      Returns a column vector/matrix.
      Parameters:
      A - the column vector.
      Returns:
      the column vector/matrix.
    • column

      public static BigMatrix column(double[] A, int offset, int length)
      Returns a column vector/matrix.
      Parameters:
      A - the column vector.
      offset - the offset of the subarray to be used; must be non-negative and no larger than array.length.
      length - the length of the subarray to be used; must be non-negative and no larger than array.length - offset.
      Returns:
      the column vector/matrix.
    • row

      public static BigMatrix row(double[] A)
      Returns a row vector/matrix.
      Parameters:
      A - the row vector.
      Returns:
      the row vector/matrix.
    • row

      public static BigMatrix row(double[] A, int offset, int length)
      Returns a row vector/matrix.
      Parameters:
      A - the row vector.
      offset - the offset of the subarray to be used; must be non-negative and no larger than array.length.
      length - the length of the subarray to be used; must be non-negative and no larger than array.length - offset.
      Returns:
      the row vector/matrix.
    • rand

      public static BigMatrix rand(int m, int n, Distribution distribution)
      Returns a random matrix.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      distribution - the distribution of random number.
      Returns:
      the matrix.
    • randn

      public static BigMatrix randn(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 matrix.
    • rand

      public static BigMatrix rand(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 BigMatrix rand(int m, int n, double lo, double hi)
      Returns a random matrix of uniform distribution.
      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 matrix.
    • eye

      public static BigMatrix eye(int n)
      Returns an identity matrix.
      Parameters:
      n - the number of rows/columns.
      Returns:
      the matrix.
    • eye

      public static BigMatrix eye(int m, int n)
      Returns an m-by-n identity matrix.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      Returns:
      the matrix.
    • diag

      public static BigMatrix diag(int n, double diag)
      Returns a square diagonal matrix.
      Parameters:
      n - the number of rows/columns.
      diag - the diagonal value.
      Returns:
      the matrix.
    • diag

      public static BigMatrix diag(int m, int n, double diag)
      Returns an m-by-n diagonal matrix.
      Parameters:
      m - the number of rows.
      n - the number of columns.
      diag - the diagonal value.
      Returns:
      the matrix.
    • diag

      public static BigMatrix diag(double[] diag)
      Returns a square diagonal matrix.
      Parameters:
      diag - the diagonal elements.
      Returns:
      the matrix.
    • diag

      public static BigMatrix diag(org.bytedeco.javacpp.DoublePointer diag)
      Returns a square diagonal matrix.
      Parameters:
      diag - the diagonal elements.
      Returns:
      the matrix.
    • toeplitz

      public static BigMatrix 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 matrix.
    • toeplitz

      public static BigMatrix 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 matrix.
    • nrow

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

      public int ncol()
      Description copied from class: IMatrix
      Returns the number of columns.
      Specified by:
      ncol in class IMatrix
      Returns:
      the number of columns.
    • size

      public long size()
      Description copied from class: IMatrix
      Returns the number of stored matrix elements. For conventional matrix, it is simply nrow * ncol. But it is usually much less for band, packed or sparse matrix.
      Specified by:
      size in class IMatrix
      Returns:
      the number of stored matrix elements.
    • layout

      public Layout layout()
      Returns the matrix layout.
      Returns:
      the matrix layout.
    • 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.
    • uplo

      public BigMatrix uplo(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.
    • triangular

      public BigMatrix triangular(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.
    • triangular

      public Diag triangular()
      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.
    • clone

      public BigMatrix clone()
      Returns a deep copy of matrix.
      Overrides:
      clone in class Object
    • toArray

      public double[][] toArray()
      Return the two-dimensional array of matrix.
      Returns:
      the two-dimensional array of matrix.
    • set

      public BigMatrix set(BigMatrix b)
      Sets the matrix value. If the matrices have the same layout, this matrix will share the underlying storage with b.
      Parameters:
      b - the right hand side of assignment.
      Returns:
      this matrix.
    • index

      protected long index(int i, int j)
      Returns the linearized index of matrix element.
      Parameters:
      i - the row index.
      j - the column index.
      Returns:
      the linearized index.
    • get

      public double get(int i, int j)
      Description copied from class: IMatrix
      Returns A[i,j].
      Overrides:
      get in class IMatrix
      Parameters:
      i - the row index.
      j - the column index.
      Returns:
      the matrix cell value.
    • set

      public void set(int i, int j, double x)
      Description copied from class: IMatrix
      Sets A[i,j] = x.
      Overrides:
      set in class IMatrix
      Parameters:
      i - the row index.
      j - the column index.
      x - the matrix cell value.
    • get

      public BigMatrix get(int[] rows, int[] cols)
      Returns the matrix of selected rows and columns. Negative index -i means the i-th row/column from the end.
      Parameters:
      rows - the row indices.
      cols - the column indices.
      Returns:
      the submatrix.
    • row

      public double[] 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.
    • col

      public double[] col(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.
    • row

      public BigMatrix row(int... rows)
      Returns the matrix of selected rows.
      Parameters:
      rows - the row indices.
      Returns:
      the submatrix.
    • col

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

      public BigMatrix submatrix(int i, int j, int k, int l)
      Returns the submatrix which top left at (i, j) and bottom right at (k, l). The content of the submatrix will be that of this matrix. Changes to this matrix's content will be visible in the submatrix, and vice versa.
      Parameters:
      i - the beginning row, inclusive.
      j - the beginning column, inclusive,
      k - the ending row, inclusive.
      l - the ending column, inclusive.
      Returns:
      the submatrix.
    • fill

      public void fill(double x)
      Fill the matrix with a value.
      Parameters:
      x - the value.
    • transpose

      public BigMatrix transpose()
      Returns the transpose of matrix. The transpose shares the storage with this matrix. Changes to this matrix's content will be visible in the transpose, and vice versa.
      Returns:
      the transpose of matrix.
    • transpose

      public BigMatrix transpose(boolean share)
      Returns the transpose of matrix.
      Parameters:
      share - if true, the transpose shares the storage with this matrix. Changes to this matrix's content will be visible in the transpose, and vice versa.
      Returns:
      the transpose of matrix.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • equals

      public boolean equals(BigMatrix o, double epsilon)
      Returns true if two matrices equal in given precision.
      Parameters:
      o - the other matrix.
      epsilon - a number close to zero.
      Returns:
      true if two matrices equal in given precision.
    • add

      public double add(int i, int j, double b)
      A[i,j] += b
      Parameters:
      i - the row index.
      j - the column index.
      b - the operand.
      Returns:
      the updated cell value.
    • sub

      public double sub(int i, int j, double b)
      A[i,j] -= b
      Parameters:
      i - the row index.
      j - the column index.
      b - the operand.
      Returns:
      the updated cell value.
    • mul

      public double mul(int i, int j, double b)
      A[i,j] *= b
      Parameters:
      i - the row index.
      j - the column index.
      b - the operand.
      Returns:
      the updated cell value.
    • div

      public double div(int i, int j, double b)
      A[i,j] /= b
      Parameters:
      i - the row index.
      j - the column index.
      b - the operand.
      Returns:
      the updated cell value.
    • addDiag

      public BigMatrix addDiag(double b)
      A[i, i] += b
      Parameters:
      b - the operand.
      Returns:
      this matrix.
    • addDiag

      public BigMatrix addDiag(double[] b)
      A[i, i] += b[i]
      Parameters:
      b - the operand.
      Returns:
      this matrix.
    • add

      public BigMatrix add(double b)
      A += b
      Parameters:
      b - the operand.
      Returns:
      this matrix.
    • sub

      public BigMatrix sub(double b)
      A -= b
      Parameters:
      b - the operand.
      Returns:
      this matrix.
    • mul

      public BigMatrix mul(double b)
      A *= b
      Parameters:
      b - the operand.
      Returns:
      this matrix.
    • div

      public BigMatrix div(double b)
      A /= b
      Parameters:
      b - the operand.
      Returns:
      this matrix.
    • add

      public BigMatrix add(BigMatrix B)
      Element-wise addition A += B
      Parameters:
      B - the operand.
      Returns:
      this matrix.
    • sub

      public BigMatrix sub(BigMatrix B)
      Element-wise subtraction A -= B
      Parameters:
      B - the operand.
      Returns:
      this matrix.
    • mul

      public BigMatrix mul(BigMatrix B)
      Element-wise multiplication A *= B
      Parameters:
      B - the operand.
      Returns:
      this matrix.
    • div

      public BigMatrix div(BigMatrix B)
      Element-wise division A /= B
      Parameters:
      B - the operand.
      Returns:
      this matrix.
    • add

      public BigMatrix add(double beta, BigMatrix B)
      Element-wise addition A += beta * B
      Parameters:
      beta - the scalar alpha.
      B - the operand.
      Returns:
      this matrix.
    • add

      public BigMatrix add(double alpha, BigMatrix A, double beta, BigMatrix B)
      Element-wise addition C = alpha * A + beta * B
      Parameters:
      alpha - the scalar alpha.
      A - the operand.
      beta - the scalar beta.
      B - the operand.
      Returns:
      this matrix.
    • add

      public BigMatrix add(double alpha, double beta, BigMatrix B)
      Element-wise addition A = alpha * A + beta * B
      Parameters:
      alpha - the scalar alpha.
      beta - the scalar beta.
      B - the operand.
      Returns:
      this matrix.
    • add2

      public BigMatrix add2(double alpha, double beta, BigMatrix B)
      Element-wise addition A = alpha * A + beta * B^2
      Parameters:
      alpha - the scalar alpha.
      beta - the scalar beta.
      B - the operand.
      Returns:
      this matrix.
    • add

      public BigMatrix add(double alpha, double[] x, double[] y)
      Rank-1 update A += alpha * x * y'
      Parameters:
      alpha - the scalar alpha.
      x - the column vector.
      y - the row vector.
      Returns:
      this matrix.
    • replaceNaN

      public BigMatrix replaceNaN(double x)
      Replaces NaN's with given value.
      Parameters:
      x - a real number.
      Returns:
      this matrix.
    • sum

      public double sum()
      Returns the sum of all elements.
      Returns:
      the sum of all elements.
    • norm1

      public double norm1()
      L1 matrix norm that is the maximum of column sums.
      Returns:
      L1 matrix norm.
    • norm2

      public double norm2()
      L2 matrix norm that is the maximum singular value.
      Returns:
      L2 matrix norm.
    • norm

      public double norm()
      L2 matrix norm that is the maximum singular value.
      Returns:
      L2 matrix norm.
    • normInf

      public double normInf()
      L matrix norm that is the maximum of row sums.
      Returns:
      L matrix norm.
    • normFro

      public double normFro()
      Frobenius matrix norm that is the square root of sum of squares of all elements.
      Returns:
      Frobenius matrix norm.
    • xAx

      public double xAx(double[] x)
      Returns the quadratic form x' * A * x. The left upper submatrix of A is used in the computation based on the size of x.
      Parameters:
      x - the vector.
      Returns:
      the quadratic form.
    • rowSums

      public double[] rowSums()
      Returns the sum of each row.
      Returns:
      the sum of each row.
    • rowMeans

      public double[] rowMeans()
      Returns the mean of each row.
      Returns:
      the mean of each row.
    • rowSds

      public double[] rowSds()
      Returns the standard deviations of each row.
      Returns:
      the standard deviations of each row.
    • colSums

      public double[] colSums()
      Returns the sum of each column.
      Returns:
      the sum of each column.
    • colMeans

      public double[] colMeans()
      Returns the mean of each column.
      Returns:
      the mean of each column.
    • colSds

      public double[] colSds()
      Returns the standard deviations of each column.
      Returns:
      the standard deviations of each column.
    • standardize

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

      public BigMatrix scale(double[] center, double[] 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 matrix with zero mean and unit variance for each column.
    • inverse

      public BigMatrix inverse()
      Returns the inverse of matrix.
      Returns:
      the inverse of matrix.
    • mv

      public void mv(Transpose trans, double alpha, double[] x, double beta, double[] y)
      Description copied from class: IMatrix
      Matrix-vector multiplication.
      
           y = alpha * op(A) * x + beta * y
       
      where op is the transpose operation.
      Specified by:
      mv in class IMatrix
      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.
    • mv

      public void mv(double[] work, int inputOffset, int outputOffset)
      Description copied from class: IMatrix
      Matrix-vector multiplication A * x.
      Specified by:
      mv in class IMatrix
      Parameters:
      work - the workspace for both input and output vector.
      inputOffset - the offset of input vector in workspace.
      outputOffset - the offset of output vector in workspace.
    • tv

      public void tv(double[] work, int inputOffset, int outputOffset)
      Description copied from class: IMatrix
      Matrix-vector multiplication A' * x.
      Specified by:
      tv in class IMatrix
      Parameters:
      work - the workspace for both input and output vector.
      inputOffset - the offset of input vector in workspace.
      outputOffset - the offset of output vector in workspace.
    • mm

      public BigMatrix mm(Transpose transA, BigMatrix A, Transpose transB, BigMatrix B)
      Matrix-matrix multiplication.
      
           C := A*B
       
      Parameters:
      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.
      Returns:
      this matrix.
    • mm

      public BigMatrix mm(Transpose transA, BigMatrix A, Transpose transB, BigMatrix B, double alpha, double beta)
      Matrix-matrix multiplication.
      
           C := alpha*A*B + beta*C
       
      Parameters:
      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.
      alpha - the scalar alpha.
      beta - the scalar beta.
      Returns:
      this matrix.
    • ata

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

      public BigMatrix aat()
      Returns A * A'.
      Returns:
      A * A'.
    • adb

      public static BigMatrix adb(Transpose transA, BigMatrix A, double[] D, Transpose transB, BigMatrix B)
      Returns A * D * B, where D is a diagonal matrix.
      Parameters:
      transA - normal, transpose, or conjugate transpose operation on the matrix A.
      A - the operand.
      D - the diagonal matrix.
      transB - normal, transpose, or conjugate transpose operation on the matrix B.
      B - the operand.
      Returns:
      the multiplication.
    • mm

      public BigMatrix mm(BigMatrix B)
      Returns matrix multiplication A * B.
      Parameters:
      B - the operand.
      Returns:
      the multiplication.
    • mt

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

      public BigMatrix tm(BigMatrix B)
      Returns matrix multiplication A' * B.
      Parameters:
      B - the operand.
      Returns:
      the multiplication.
    • tt

      public BigMatrix tt(BigMatrix B)
      Returns matrix multiplication A' * B'.
      Parameters:
      B - the operand.
      Returns:
      the multiplication.
    • lu

      public BigMatrix.LU lu()
      LU decomposition.
      Returns:
      LU decomposition.
    • lu

      public BigMatrix.LU lu(boolean overwrite)
      LU decomposition.
      Parameters:
      overwrite - The flag if the decomposition overwrites this matrix.
      Returns:
      LU decomposition.
    • cholesky

      public BigMatrix.Cholesky cholesky()
      Cholesky decomposition for symmetric and positive definite matrix.
      Returns:
      Cholesky decomposition.
      Throws:
      ArithmeticException - if the matrix is not positive definite.
    • cholesky

      public BigMatrix.Cholesky cholesky(boolean overwrite)
      Cholesky decomposition for symmetric and positive definite matrix.
      Parameters:
      overwrite - The flag if the decomposition overwrites this matrix.
      Returns:
      Cholesky decomposition.
      Throws:
      ArithmeticException - if the matrix is not positive definite.
    • qr

      public BigMatrix.QR qr()
      QR Decomposition.
      Returns:
      QR decomposition.
    • qr

      public BigMatrix.QR qr(boolean overwrite)
      QR Decomposition.
      Parameters:
      overwrite - The flag if the decomposition overwrites this matrix.
      Returns:
      QR decomposition.
    • svd

      public BigMatrix.SVD svd()
      Singular Value Decomposition. 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 BigMatrix.SVD svd(boolean vectors, boolean overwrite)
      Singular Value Decomposition. 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 if computing the singular vectors.
      overwrite - The flag if the decomposition overwrites this matrix.
      Returns:
      singular value decomposition.
    • eigen

      public BigMatrix.EVD eigen()
      Eigenvalue Decomposition. 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 BigMatrix.EVD eigen(boolean vl, boolean vr, boolean overwrite)
      Eigenvalue Decomposition. 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.
      overwrite - The flag if the decomposition overwrites this matrix.
      Returns:
      eigen value decomposition.