Class Matrix

  • Direct Known Subclasses:
    Vector

    public class Matrix
    extends Object
    This class implements matrices and their operations.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int columns  
      protected int rows  
      protected double[][] value  
    • Constructor Summary

      Constructors 
      Constructor Description
      Matrix​(double[][] data)
      Creates a new Matrix from array of double.
      Matrix​(int n, int m)
      Creates a new Matrix with n rows and m columns.
      Matrix​(Matrix M)
      Creates a new copy of a Matrix from a given matrix M.
    • Field Detail

      • rows

        protected int rows
      • columns

        protected int columns
      • value

        protected double[][] value
    • Constructor Detail

      • Matrix

        public Matrix​(double[][] data)
        Creates a new Matrix from array of double. This does not copy the array but simply references the same memory range.
        Parameters:
        data - Array of double. Rows of the matrix will be set to the first dimension of data.
      • Matrix

        public Matrix​(Matrix M)
        Creates a new copy of a Matrix from a given matrix M.
        Parameters:
        M - Matrix to be copied from.
      • Matrix

        public Matrix​(int n,
                      int m)
        Creates a new Matrix with n rows and m columns.
        Parameters:
        n - Number of rows.
        m - Number of columns.
    • Method Detail

      • get

        public double get​(int i,
                          int j)
        Read an entry in a matrix. Returns Double.NaN if specified position is not within the matrix.
        Parameters:
        i - Row (1..n)
        j - Column (1..m)
        Returns:
        Entry at i,j or Double.NaN if specified position is not within the matrix
      • set

        public boolean set​(int i,
                           int j,
                           double value)
        Writes an entry in a matrix. Returns false if specified position is not within the matrix.
        Parameters:
        i - Row (1..n)
        j - Column (1..m)
        value - Value to write.
        Returns:
        false if specified position is not within the matrix.
      • getRowCount

        public int getRowCount()
        Reads number of rows in a matrix.
        Returns:
        Number of rows.
      • getColumnCount

        public int getColumnCount()
        Reads number of columns in a matrix.
        Returns:
        Number of columns.
      • add

        public boolean add​(Matrix left,
                           Matrix right)
        Adds two matrices and stores the result in this instance. Returns false if the dimensions of the involved matrices do not fit.
        Parameters:
        left - First input matrix
        right - Second input matrix
        Returns:
        false if dimensions of the involved matrices do not fit.
      • multiply

        public boolean multiply​(Matrix left,
                                Matrix right)
        Multiplies two matrices and stores the result in this instance. Returns false dimensions of the involved matrices do not fit.
        Parameters:
        left - First input matrix
        right - Second input matrix
        Returns:
        false if the number of columns of left != number of rows of right or if the number of columns of right != number of columns of this or if the number of rows of left != number of rows of this or.
      • transpose

        public boolean transpose​(Matrix original)
      • transpose

        public void transpose()
      • clone

        public Matrix clone()
        Creates and returns a copy of this object.
        Returns:
        A clone of this instance.
      • copy

        public boolean copy​(Matrix M)
        Copies the content of matrix M into this matrix.
        Parameters:
        M - The matrix to be copied from.
        Returns:
        true if successful, false if the two matrices have different dimensions.
      • isEqual

        public boolean isEqual​(Matrix M)
        Compares this object against the specified object.
        Parameters:
        M - The matrix to compare with.
        Returns:
        true if every value of this matrix is the same as the value of the compared matrix.
      • rot3x

        public static boolean rot3x​(Matrix A,
                                    double angle)
      • rot3x

        public static Matrix rot3x​(double angle)
      • print

        public void print​(PrintStream out)
        Prints a matrix onto a print stream.
        Parameters:
        out - Print stream