Class DenseMatrix

  • All Implemented Interfaces:
    java.lang.Iterable<MatrixEntry>, Matrix

    public class DenseMatrix
    extends AbstractMatrix
    Dense matrix. It is a good all-round matrix structure, with fast access and efficient algebraic operations. The matrix

    a11 a12 a13 a14
    a21 a22 a23 a24
    a31 a32 a33 a34
    a41 a42 a43 a44

    is stored column major in a single array, as follows:

    a11 a21 a31 a41 a12 a22 a32 a42 a13 a23 a33 a43 a14 a24 a34 a44

    • Constructor Detail

      • DenseMatrix

        public DenseMatrix​(MatrixVectorReader r)
                    throws java.io.IOException
        Constructor for DenseMatrix
        Parameters:
        r - Reader to get the matrix from
        Throws:
        java.io.IOException
      • DenseMatrix

        public DenseMatrix​(int numRows,
                           int numColumns)
        Constructor for DenseMatrix
        Parameters:
        numRows - Number of rows
        numColumns - Number of columns
      • DenseMatrix

        public DenseMatrix​(Matrix A)
        Constructor for DenseMatrix
        Parameters:
        A - Matrix to copy. A deep copy is made
      • DenseMatrix

        public DenseMatrix​(Matrix A,
                           boolean deep)
        Constructor for DenseMatrix
        Parameters:
        A - Matrix to copy contents from
        deep - If true, A is copied, else a shallow copy is made and the matrices share underlying storage. For this, A must be a dense matrix
      • DenseMatrix

        public DenseMatrix​(Vector x,
                           boolean deep)
        Constructor for DenseMatrix. Builds the matrix from a vector
        Parameters:
        x - Vector to copy from. This will form this matrix' single column
        deep - If true, x is copied, if false, the internal storage of this matrix is the same as that of the vector. In that case, x must be a DenseVector
      • DenseMatrix

        public DenseMatrix​(Vector x)
        Constructor for DenseMatrix. Builds the matrix from a vector
        Parameters:
        x - The vector which forms this matrix' single column. It is copied, not referenced
      • DenseMatrix

        public DenseMatrix​(Vector[] x)
        Constructor for DenseMatrix. Builds the matrix from vectors. Each vector will correspond to a column of the matrix
        Parameters:
        x - Vectors which forms the columns of this matrix. Every vector must have the same size
      • DenseMatrix

        public DenseMatrix​(double[][] values)
        Constructor for DenseMatrix. Copies from the passed array
        Parameters:
        values - Arrays to copy from. Every sub-array must have the same size
    • Method Detail

      • multAdd

        public Matrix multAdd​(double alpha,
                              Matrix B,
                              Matrix C)
        Description copied from interface: Matrix
        C = alpha*A*B + C
        Specified by:
        multAdd in interface Matrix
        Overrides:
        multAdd in class AbstractMatrix
        B - Matrix such that B.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
        C - Matrix such that C.numRows() == A.numRows() and B.numColumns() == C.numColumns()
        Returns:
        C
      • transAmultAdd

        public Matrix transAmultAdd​(double alpha,
                                    Matrix B,
                                    Matrix C)
        Description copied from interface: Matrix
        C = alpha*AT*B + C
        Specified by:
        transAmultAdd in interface Matrix
        Overrides:
        transAmultAdd in class AbstractMatrix
        B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
        C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
        Returns:
        C
      • transBmultAdd

        public Matrix transBmultAdd​(double alpha,
                                    Matrix B,
                                    Matrix C)
        Description copied from interface: Matrix
        C = alpha*A*BT + C
        Specified by:
        transBmultAdd in interface Matrix
        Overrides:
        transBmultAdd in class AbstractMatrix
        B - Matrix such that B.numRows() == A.numRows() and B.numColumns() == C.numColumns()
        C - Matrix such that C.numRows() == A.numColumns() and B.numColumns() == C.numColumns()
        Returns:
        C
      • transABmultAdd

        public Matrix transABmultAdd​(double alpha,
                                     Matrix B,
                                     Matrix C)
        Description copied from interface: Matrix
        C = alpha*AT*BT + C
        Specified by:
        transABmultAdd in interface Matrix
        Overrides:
        transABmultAdd in class AbstractMatrix
        B - Matrix such that B.numColumns() == A.numRows() and B.numRows() == C.numColumns()
        C - Matrix such that C.numRows() == A.numColumns() and B.numRows() == C.numColumns()
        Returns:
        C
      • rank1

        public Matrix rank1​(double alpha,
                            Vector x,
                            Vector y)
        Description copied from interface: Matrix
        A = alpha*x*yT + A. The matrix must be square, and the vectors of the same length
        Specified by:
        rank1 in interface Matrix
        Overrides:
        rank1 in class AbstractMatrix
        Returns:
        A
      • multAdd

        public Vector multAdd​(double alpha,
                              Vector x,
                              Vector y)
        Description copied from interface: Matrix
        y = alpha*A*x + y
        Specified by:
        multAdd in interface Matrix
        Overrides:
        multAdd in class AbstractMatrix
        x - Vector of size A.numColumns()
        y - Vector of size A.numRows()
        Returns:
        y
      • solve

        public Matrix solve​(Matrix B,
                            Matrix X)
        Description copied from interface: Matrix
        X = A\B. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
        Specified by:
        solve in interface Matrix
        Overrides:
        solve in class AbstractMatrix
        Parameters:
        B - Matrix with the same number of rows as A, and the same number of columns as X
        X - Matrix with a number of rows equal A.numColumns(), and the same number of columns as B
        Returns:
        X
      • solve

        public Vector solve​(Vector b,
                            Vector x)
        Description copied from interface: Matrix
        x = A\b. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
        Specified by:
        solve in interface Matrix
        Overrides:
        solve in class AbstractMatrix
        Parameters:
        b - Vector of size A.numRows()
        x - Vector of size A.numColumns()
        Returns:
        x
      • transSolve

        public Matrix transSolve​(Matrix B,
                                 Matrix X)
        Description copied from interface: Matrix
        X = AT\B. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated transpose solver
        Specified by:
        transSolve in interface Matrix
        Overrides:
        transSolve in class AbstractMatrix
        Parameters:
        B - Matrix with a number of rows equal A.numColumns(), and the same number of columns as X
        X - Matrix with the same number of rows as A, and the same number of columns as B
        Returns:
        X
      • transSolve

        public Vector transSolve​(Vector b,
                                 Vector x)
        Description copied from interface: Matrix
        x = AT\b. Not all matrices support this operation, those that do not throw UnsupportedOperationException. Note that it is often more efficient to use a matrix decomposition and its associated solver
        Specified by:
        transSolve in interface Matrix
        Overrides:
        transSolve in class AbstractMatrix
        Parameters:
        b - Vector of size A.numColumns()
        x - Vector of size A.numRows()
        Returns:
        x
      • getData

        public double[] getData()
        Returns the matrix contents. Ordering depends on the underlying storage assumptions
      • add

        public void add​(int row,
                        int column,
                        double value)
        Description copied from interface: Matrix
        A(row,column) += value
        Specified by:
        add in interface Matrix
        Overrides:
        add in class AbstractMatrix
      • set

        public void set​(int row,
                        int column,
                        double value)
        Description copied from interface: Matrix
        A(row,column) = value
        Specified by:
        set in interface Matrix
        Overrides:
        set in class AbstractMatrix
      • get

        public double get​(int row,
                          int column)
        Description copied from interface: Matrix
        Returns A(row,column)
        Specified by:
        get in interface Matrix
        Overrides:
        get in class AbstractMatrix
      • zero

        public Matrix zero()
        Description copied from interface: Matrix
        Zeros all the entries in the matrix, while preserving any underlying structure. Useful for general, unstructured matrices.
        Specified by:
        zero in interface Matrix
        Overrides:
        zero in class AbstractMatrix
        Returns:
        A