Class Matrix

  • Direct Known Subclasses:
    ColumnVector, RowVector, SquareMatrix

    public class Matrix
    extends Object
    From: Java Number Cruncher The Java Programmer's Guide to Numerical Computation by Ronald Mak The matrix class.
    • Field Detail

      • nRows

        protected int nRows
        number of rows
      • nCols

        protected int nCols
        number of columns
      • values

        protected double[][] values
        2-d array of values
    • Constructor Detail

      • Matrix

        protected Matrix()
        Default constructor.
      • Matrix

        public Matrix​(int rowCount,
                      int colCount)
        Constructor.
        Parameters:
        rowCount - the number of rows
        colCount - the number of columns
      • Matrix

        public Matrix​(double[][] values)
        Constructor.
        Parameters:
        values - the 2-d array of values
    • Method Detail

      • rowCount

        public int rowCount()
        Get the row count.
        Returns:
        the row count
      • columnCount

        public int columnCount()
        Get the column count.
        Returns:
        the column count
      • at

        public double at​(int r,
                         int c)
                  throws MatrixException
        Get the value of element [r,c] in the matrix.
        Parameters:
        r - the row index
        c - the column index
        Returns:
        the value
        Throws:
        numbercruncher.MatrixException - for an invalid index
        MatrixException
      • getRow

        public RowVector getRow​(int r)
                         throws MatrixException
        Get a row of this matrix.
        Parameters:
        r - the row index
        Returns:
        the row as a row vector
        Throws:
        numbercruncher.MatrixException - for an invalid index
        MatrixException
      • getColumn

        public ColumnVector getColumn​(int c)
                               throws MatrixException
        Get a column of this matrix.
        Parameters:
        c - the column index
        Returns:
        the column as a column vector
        Throws:
        numbercruncher.MatrixException - for an invalid index
        MatrixException
      • values

        public double[][] values()
        Copy the values of this matrix.
        Returns:
        the values
      • copyValues2D

        public double[][] copyValues2D()
        Copy the values of this matrix.
        Returns:
        the copied values
      • set

        public void set​(int r,
                        int c,
                        double value)
                 throws MatrixException
        Set the value of element [r,c].
        Parameters:
        r - the row index
        c - the column index
        value - the value
        Throws:
        numbercruncher.MatrixException - for an invalid index
        MatrixException
      • set

        protected void set​(double[][] values)
        Set this matrix from a 2-d array of values. If the rows do not have the same length, then the matrix column count is the length of the shortest row.
        Parameters:
        values - the 2-d array of values
      • setRow

        public void setRow​(RowVector rv,
                           int r)
                    throws MatrixException
        Set a row of this matrix from a row vector.
        Parameters:
        rv - the row vector
        r - the row index
        Throws:
        numbercruncher.MatrixException - for an invalid index or an invalid vector size
        MatrixException
      • setColumn

        public void setColumn​(ColumnVector cv,
                              int c)
                       throws MatrixException
        Set a column of this matrix from a column vector.
        Parameters:
        cv - the column vector
        c - the column index
        Throws:
        numbercruncher.MatrixException - for an invalid index or an invalid vector size
        MatrixException
      • transpose

        public Matrix transpose()
        Return the transpose of this matrix.
        Returns:
        the transposed matrix
      • add

        public Matrix add​(Matrix m)
                   throws MatrixException
        Add another matrix to this matrix.
        Parameters:
        m - the matrix addend
        Returns:
        the sum matrix
        Throws:
        numbercruncher.MatrixException - for invalid size
        MatrixException
      • subtract

        public Matrix subtract​(Matrix m)
                        throws MatrixException
        Subtract another matrix from this matrix.
        Parameters:
        m - the matrix subrrahend
        Returns:
        the difference matrix
        Throws:
        numbercruncher.MatrixException - for invalid size
        MatrixException
      • multiply

        public Matrix multiply​(double k)
        Multiply this matrix by a constant.
        Parameters:
        k - the constant
        Returns:
        the product matrix
      • multiply

        public Matrix multiply​(Matrix m)
                        throws MatrixException
        Multiply this matrix by another matrix.
        Parameters:
        m - the matrix multiplier
        Returns:
        the product matrix
        Throws:
        numbercruncher.MatrixException - for invalid size
        MatrixException
      • multiply

        public ColumnVector multiply​(ColumnVector cv)
                              throws MatrixException
        Multiply this matrix by a column vector: this*cv
        Parameters:
        cv - the column vector
        Returns:
        the product column vector
        Throws:
        numbercruncher.MatrixException - for invalid size
        MatrixException
      • multiply

        public RowVector multiply​(RowVector rv)
                           throws MatrixException
        Multiply a row vector by this matrix: rv*this
        Parameters:
        rv - the row vector
        Returns:
        the product row vector
        Throws:
        numbercruncher.MatrixException - for invalid size
        MatrixException