Class MatrixD

  • All Implemented Interfaces:
    Matrix, java.io.Serializable, java.lang.Cloneable

    public class MatrixD
    extends AbstractMatrix
    Jama = Java Matrix class.

    The Java Matrix Class provides the fundamental operations of numerical linear algebra. Various constructors create Matrices from two dimensional arrays of double precision floating point numbers. Various "gets" and "sets" provide access to sub-matrices and matrix elements. Several methods implement basic matrix arithmetic, including matrix addition and multiplication, matrix norms, and element-by-element array operations. Methods for reading and printing matrices are also included. All the operations in this version of the Matrix Class involve real matrices. Complex matrices may be handled in a future version.

    Five fundamental matrix decompositions, which consist of pairs or triples of matrices, permutation vectors, and the like, produce results in five decomposition classes. These decompositions are accessed by the Matrix class to compute solutions of simultaneous linear equations, determinants, inverses and other matrix functions. The five decompositions are:

    • Cholesky Decomposition of symmetric, positive definite matrices.
    • LU Decomposition of rectangular matrices.
    • QR Decomposition of rectangular matrices.
    • Singular Value Decomposition of rectangular matrices.
    • Eigenvalue Decomposition of both symmetric and nonsymmetric square matrices.
    Example of use:
    Solve a linear system A x = b and compute the residual norm, ||b - A x||.
     
     double[][] vals = { { 1., 2., 3 }, { 4., 5., 6. }, { 7., 8., 10. } };
     Matrix A = new Matrix(vals);
     Matrix b = Matrix.random(3, 1);
     Matrix x = A.solve(b);
     Matrix r = A.times(x).minus(b);
     double rnorm = r.normInf();
     
    Version:
    5 August 1998
    Author:
    The MathWorks, Inc. and the National Institute of Standards and Technology.
    See Also:
    Serialized Form
    • Constructor Detail

      • MatrixD

        public MatrixD​(double[] vals,
                       int m)
        Construct a matrix from a one-dimensional packed array
        Parameters:
        vals - One-dimensional array of doubles
        m - Number of rows.
        Throws:
        java.lang.IllegalArgumentException - Array length must be a multiple of m.
      • MatrixD

        public MatrixD​(double[] vals,
                       int m,
                       boolean rowMajor)
        Construct a matrix from a one-dimensional packed array
        Parameters:
        vals - One-dimensional array of doubles
        m - Number of rows.
        rowMajor - true: data is stored row-wise (C/C++), false: data is stored column-wise (Fortran)
        Throws:
        java.lang.IllegalArgumentException - Array length must be a multiple of m.
      • MatrixD

        public MatrixD​(double[][] A)
        Construct a matrix from a 2-D array.
        Parameters:
        A - Two-dimensional array of doubles.
        Throws:
        java.lang.IllegalArgumentException - All rows must have the same length
      • MatrixD

        public MatrixD​(double[][] A,
                       int m,
                       int n)
        Construct a matrix quickly without checking arguments.
        Parameters:
        A - Two-dimensional array of doubles.
        m - Number of rows.
        n - Number of columns.
      • MatrixD

        public MatrixD​(int m,
                       int n)
        Construct an m-by-n matrix of zeros.
        Parameters:
        m - Number of rows.
        n - Number of columns.
      • MatrixD

        public MatrixD​(int m,
                       int n,
                       double s)
        Construct an m-by-n constant matrix.
        Parameters:
        m - Number of rows.
        n - Number of columns.
        s - Fill the matrix with this scalar value.
    • Method Detail

      • apply1DFunction

        public void apply1DFunction​(Function1D func)
        apply user specified function to each matrix element
        Parameters:
        func - user-supplied function
      • arrayLeftDivide

        public MatrixD arrayLeftDivide​(MatrixD B)
        Element-by-element left division, C = A.\B
        Parameters:
        B - another matrix
        Returns:
        A.\B
      • arrayLeftDivideEquals

        public MatrixD arrayLeftDivideEquals​(MatrixD B)
        Element-by-element left division in place, A = A.\B
        Parameters:
        B - another matrix
        Returns:
        A.\B
      • arrayRightDivide

        public MatrixD arrayRightDivide​(MatrixD B)
        Element-by-element right division, C = A./B
        Parameters:
        B - another matrix
        Returns:
        A./B
      • arrayRightDivideEquals

        public MatrixD arrayRightDivideEquals​(MatrixD B)
        Element-by-element right division in place, A = A./B
        Parameters:
        B - another matrix
        Returns:
        A./B
      • arrayTimes

        public MatrixD arrayTimes​(MatrixD B)
        Element-by-element multiplication, C = A.*B
        Parameters:
        B - another matrix
        Returns:
        A.*B
      • arrayTimesEquals

        public MatrixD arrayTimesEquals​(MatrixD B)
        Element-by-element multiplication in place, A = A.*B
        Parameters:
        B - another matrix
        Returns:
        A.*B
      • clone

        public java.lang.Object clone()
        Clone the Matrix object.
        Overrides:
        clone in class java.lang.Object
      • cond

        public double cond()
        Matrix condition (2 norm)
        Returns:
        ratio of largest to smallest singular value.
      • copy

        public MatrixD copy()
        Make a deep copy of a matrix
        Returns:
        copy of matrix
      • det

        public double det()
        Matrix determinant
        Returns:
        determinant
      • get

        public double get​(int i,
                          int j)
        Get a single element.
        Parameters:
        i - Row index.
        j - Column index
        Returns:
        A(i,j)
      • getArray

        public double[][] getArray()
        Access the internal two-dimensional array.
        Returns:
        Pointer to the two-dimensional array of matrix elements.
      • getArrayCopy

        public double[][] getArrayCopy()
        Copy the internal two-dimensional array.
        Returns:
        Two-dimensional array copy of matrix elements.
      • getColumnPackedCopy

        public double[] getColumnPackedCopy()
        Make a one-dimensional column packed copy of the internal array.
        Returns:
        Matrix elements packed in a one-dimensional array by columns.
      • getMatrix

        public MatrixD getMatrix​(int i0,
                                 int i1,
                                 int j0,
                                 int j1)
        Get a sub-matrix.
        Parameters:
        i0 - Initial row index
        i1 - Final row index
        j0 - Initial column index
        j1 - Final column index
        Returns:
        A(i0:i1,j0:j1)
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - sub-matrix indices
      • getMatrix

        public MatrixD getMatrix​(int i0,
                                 int i1,
                                 int[] c)
        Get a sub-matrix.
        Parameters:
        i0 - Initial row index
        i1 - Final row index
        c - Array of column indices.
        Returns:
        A(i0:i1,c(:))
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • getMatrix

        public MatrixD getMatrix​(int[] r,
                                 int j0,
                                 int j1)
        Get a sub-matrix.
        Parameters:
        r - Array of row indices.
        j0 - Initial column index
        j1 - Final column index
        Returns:
        A(r(:),j0:j1)
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • getMatrix

        public MatrixD getMatrix​(int[] r,
                                 int[] c)
        Get a sub-matrix.
        Parameters:
        r - Array of row indices.
        c - Array of column indices.
        Returns:
        A(r(:),c(:))
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • getRowPackedCopy

        public double[] getRowPackedCopy()
        Make a one-dimensional row Major copy of the internal array.
        Returns:
        Matrix elements packed in a one-dimensional array by rows.
      • inverse

        public MatrixD inverse()
        Matrix inverse or pseudo-inverse
        Returns:
        inverse(A) if A is square, pseudo-inverse otherwise.
      • minus

        public MatrixD minus​(MatrixD B)
        C = A - B
        Parameters:
        B - another matrix
        Returns:
        A - B
      • minusEquals

        public MatrixD minusEquals​(MatrixD B)
        A = A - B
        Parameters:
        B - another matrix
        Returns:
        A - B
      • plus

        public MatrixD plus​(MatrixD B)
        C = A + B
        Parameters:
        B - another matrix
        Returns:
        A + B
      • plusEquals

        public MatrixD plusEquals​(MatrixD B)
        A = A + B
        Parameters:
        B - another matrix
        Returns:
        A + B
      • print

        public void print​(int w,
                          int d)
        Print the matrix to stdout. Line the elements up in columns with a Fortran-like 'Fw.d' style format.
        Parameters:
        w - Column width.
        d - Number of digits after the decimal.
      • print

        public void print​(java.text.NumberFormat format,
                          int width)
        Print the matrix to stdout. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.
        Parameters:
        format - A Formatting object for individual elements.
        width - Field width for each column.
        See Also:
        DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
      • print

        public void print​(java.io.PrintWriter output,
                          int w,
                          int d)
        Print the matrix to the output stream. Line the elements up in columns with a Fortran-like 'Fw.d' style format.
        Parameters:
        output - Output stream.
        w - Column width.
        d - Number of digits after the decimal.
      • print

        public void print​(java.io.PrintWriter output,
                          java.text.NumberFormat format,
                          int width)
        Print the matrix to the output stream. Line the elements up in columns. Use the format object, and right justify within columns of width characters. Note that is the matrix is to be read back in, you probably will want to use a NumberFormat that is set to US Locale.
        Parameters:
        output - the output stream.
        format - A formatting object to format the matrix elements
        width - Column width.
        See Also:
        DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
      • pseudoInverse

        public MatrixD pseudoInverse​(double condition)
        Matrix inversion using the SVD pseudo inverse
        Parameters:
        condition - condition number
        Returns:
        inverse matrix
      • rank

        public int rank()
        Matrix rank
        Returns:
        effective numerical rank, obtained from SVD.
      • set

        public void set​(int i,
                        int j,
                        double s)
        Set a single element.
        Parameters:
        i - Row index.
        j - Column index.
        s - A(i,j).
      • setMatrix

        public void setMatrix​(int i0,
                              int i1,
                              int j0,
                              int j1,
                              MatrixD X)
        Set a submatrix.
        Parameters:
        i0 - Initial row index
        i1 - Final row index
        j0 - Initial column index
        j1 - Final column index
        X - A(i0:i1,j0:j1)
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • setMatrix

        public void setMatrix​(int i0,
                              int i1,
                              int[] c,
                              MatrixD X)
        Set a submatrix.
        Parameters:
        i0 - Initial row index
        i1 - Final row index
        c - Array of column indices.
        X - A(i0:i1,c(:))
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • setMatrix

        public void setMatrix​(int[] r,
                              int j0,
                              int j1,
                              MatrixD X)
        Set a submatrix.
        Parameters:
        r - Array of row indices.
        j0 - Initial column index
        j1 - Final column index
        X - A(r(:),j0:j1)
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • setMatrix

        public void setMatrix​(int[] r,
                              int[] c,
                              MatrixD X)
        Set a submatrix.
        Parameters:
        r - Array of row indices.
        c - Array of column indices.
        X - A(r(:),c(:))
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - Submatrix indices
      • solve

        public MatrixD solve​(MatrixD B)
        Solve A*X = B
        Parameters:
        B - right hand side
        Returns:
        solution if A is square, least squares solution otherwise
      • solveTranspose

        public MatrixD solveTranspose​(MatrixD B)
        Solve X*A = B, which is also A'*X' = B'
        Parameters:
        B - right hand side
        Returns:
        solution if A is square, least squares solution otherwise.
      • squareElements

        public void squareElements()
        Square individual matrix elements
      • times

        public MatrixD times​(double s)
        Multiply a matrix by a scalar, C = s*A
        Parameters:
        s - scalar
        Returns:
        s*A
      • times

        public MatrixD times​(MatrixD B)
        Linear algebraic matrix multiplication, A * B
        Parameters:
        B - another matrix
        Returns:
        Matrix product, A * B
        Throws:
        java.lang.IllegalArgumentException - Matrix inner dimensions must agree.
      • timesEquals

        public MatrixD timesEquals​(double s)
        Multiply a matrix by a scalar in place, A = s*A
        Parameters:
        s - scalar
        Returns:
        replace A by s*A
      • trace

        public double trace()
        Matrix trace.
        Returns:
        sum of the diagonal elements.
      • transpose

        public MatrixD transpose()
        Matrix transpose.
        Returns:
        A^{T}
      • uminus

        public MatrixD uminus()
        Unary minus
        Returns:
        -A
      • read

        public static MatrixD read​(java.io.BufferedReader input)
                            throws java.io.IOException
        Read a matrix from a stream. The format is the same the print method, so printed matrices can be read back in (provided they were printed using US Locale). Elements are separated by whitespace, all the elements for each row appear on a single line, the last row is followed by a blank line.
        Parameters:
        input - the input stream.
        Returns:
        matrix object that has been recovered from file
        Throws:
        java.io.IOException - in case of troubles ;-)