Class Matrix


  • public class Matrix
    extends java.lang.Object
    A simple class defining a mathematical matrix.
    • Constructor Summary

      Constructors 
      Constructor Description
      Matrix​(int cols, int rows)
      Create a new Matrix of the specified number of rows and columns
      Matrix​(Matrix m)
      Create a new Matrix that's a clone of the specified matrix
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Matrix add​(Matrix m)
      Return a new matrix that is the sum of this matrix and the specified matrix.
      Matrix appendHorizontal​(Matrix m)
      Append the specified matrix to the current matrix by extending the number of columns in the current matrix
      Matrix appendVertical​(Matrix m)
      Append the specified matrix to the current matrix by extending the number of columns in the current matrix
      Matrix div​(Matrix m)
      Return a new matrix that is the result of this matrix divided by the specified matrix.
      Matrix exchangeRows​(int row1, int row2)
      Return a duplicate of the current matrix with the specified rows swapped
      double get​(int col, int row)
      Get the element at the specified row and column.
      int getCols()
      Return the number of columns in this matrix
      int getRows()
      Return the number of rows in this matrix
      static Matrix identity​(int ord)
      Return a new Identity Matrix of the specified size
      Matrix inverse()
      Return the inverse of the current matrix
      static void main​(java.lang.String[] args)  
      Matrix mul​(double fac)
      Return a new matrix that is the product of this matrix and the specified factor.
      Matrix mul​(Matrix m)
      Return a new matrix that is the product of this matrix and the specified matrix.
      Matrix pivot​(int col, int row)
      Pivot the current matrix around the specified row and column
      void set​(int col, int row, double val)
      Set the element at the specified row and column to the specified value
      Matrix subMatrix​(int col, int row, int numcols, int numrows)
      Return a subset of this matrix consisiting of just the specified columns and rows
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Matrix

        public Matrix​(int cols,
                      int rows)
        Create a new Matrix of the specified number of rows and columns
      • Matrix

        public Matrix​(Matrix m)
        Create a new Matrix that's a clone of the specified matrix
    • Method Detail

      • set

        public void set​(int col,
                        int row,
                        double val)
        Set the element at the specified row and column to the specified value
        Parameters:
        col - the column to set the value in, from 0 to getCols()-1
        row - the row to set the value in, from 0 to getRows()-1
        val - the value to set.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the matrix doesn't contain the specified row or column
      • get

        public double get​(int col,
                          int row)
        Get the element at the specified row and column.
        Parameters:
        col - the column to set the value in, from 0 to getCols()-1
        row - the row to set the value in, from 0 to getRows()-1
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the matrix doesn't contain the specified row or column
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • exchangeRows

        public Matrix exchangeRows​(int row1,
                                   int row2)
        Return a duplicate of the current matrix with the specified rows swapped
      • identity

        public static Matrix identity​(int ord)
        Return a new Identity Matrix of the specified size
        Parameters:
        ord - the number of rows and columns in the matrix
      • add

        public Matrix add​(Matrix m)
        Return a new matrix that is the sum of this matrix and the specified matrix. The current matrix is unmodified
      • getRows

        public int getRows()
        Return the number of rows in this matrix
      • getCols

        public int getCols()
        Return the number of columns in this matrix
      • mul

        public Matrix mul​(double fac)
        Return a new matrix that is the product of this matrix and the specified factor. The current matrix is unmodified
      • mul

        public Matrix mul​(Matrix m)
        Return a new matrix that is the product of this matrix and the specified matrix. The current matrix is unmodified
        Throws:
        java.lang.IllegalArgumentException - if the two matrices are incompatible sizes.
      • div

        public Matrix div​(Matrix m)
        Return a new matrix that is the result of this matrix divided by the specified matrix. This is the same as multiplying by the inverse of the specified matrix. The current matrix is unmodified.
        Throws:
        java.lang.IllegalArgumentException - if the two matrices are incompatible sizes.
        java.lang.ArithmeticException - if the second matrix has no inverse.
      • appendHorizontal

        public Matrix appendHorizontal​(Matrix m)
        Append the specified matrix to the current matrix by extending the number of columns in the current matrix
      • appendVertical

        public Matrix appendVertical​(Matrix m)
        Append the specified matrix to the current matrix by extending the number of columns in the current matrix
      • subMatrix

        public Matrix subMatrix​(int col,
                                int row,
                                int numcols,
                                int numrows)
        Return a subset of this matrix consisiting of just the specified columns and rows
      • inverse

        public Matrix inverse()
                       throws java.lang.ArithmeticException
        Return the inverse of the current matrix
        Throws:
        java.lang.ArithmeticException - if there is no inverse
      • pivot

        public Matrix pivot​(int col,
                            int row)
        Pivot the current matrix around the specified row and column
      • main

        public static void main​(java.lang.String[] args)