edu.nps.moves.deadreckoning.utils
Class Matrix

java.lang.Object
  extended by edu.nps.moves.deadreckoning.utils.Matrix

public class Matrix
extends java.lang.Object

A class that performs some basic Matrix manipulations, stopping short of Eigenvectors, Single Value Decomposition, LU, and other more advaced manipulations.

  1. Multiplication
  2. Inverse
  3. Add
  4. Subtract
  5. Transpose
  6. Row swap
  7. Sub determinate (2x2)
  8. Author:
    Sheldon L. Snyder

    Constructor Summary
    Matrix(int dimension)
              create a square matrix initialized to the identity
    Matrix(int rows, int cols)
              create a matrix of any dimensions initialized to all zeroes.
    Matrix(Matrix M)
              Creates a matrix of a matrix...a copy
     
    Method Summary
     Matrix add(Matrix M2)
              Adds two matrices together
    static void add(Matrix r1, double a)
              Adds a constant to each element of this matrix
    static Matrix add(Matrix M1, Matrix M2)
              Static method to add any two matrices
     double cell(int i, int j)
              return the value in this matrix located at the ith row and jth column
     int cols()
              Get the number of columns in this matrix
    static double det(double d1, double d2, double d3, double d4)
              Solves the determinate of this 2x2 matrix
    static Matrix inversMat3x3(Matrix in)
              Given a 3 x 3 matrix and using Determinats to solve for inverse
     Matrix mult(double a)
              scales a matrix, but does not destroy the content of the original
     Matrix mult(Matrix M2)
              Multiplies 2 matrixes together
    static Matrix mult(Matrix M1, Matrix M2)
              multiplies two matrixes together None Destructive
     void multSelf(double a)
              Scalar multiply in place
    static double[] multVec(Matrix A, double[] x)
              Performs Ax multiplication
     void print()
              Prints the content of a matrix to standard out
     void replace(Matrix M)
              copy each cell from M to this.data
     int rows()
              Gets the number of rows in this matrix
     void setCell(int i, int j, double value)
              set the value of the cell at the ith row and jth column to value
     Matrix subtract(Matrix M2)
              Subtracts a matrix from this
    static Matrix subtract(Matrix M1, Matrix M2)
              static Subtraces M2 from M1 5 = 8 - 3
    static Matrix transpose(Matrix in)
              Makes a transpose of the input matrix
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    Matrix

    public Matrix(int dimension)
    create a square matrix initialized to the identity

    Parameters:
    dimension - - size to make a square matrix

    Matrix

    public Matrix(int rows,
                  int cols)
    create a matrix of any dimensions initialized to all zeroes.

    Parameters:
    rows -
    cols -

    Matrix

    public Matrix(Matrix M)
           throws MatrixException
    Creates a matrix of a matrix...a copy

    Parameters:
    M - - matrix to copy
    Throws:
    MatrixException
    Method Detail

    replace

    public void replace(Matrix M)
                 throws MatrixException
    copy each cell from M to this.data

    Replaces the value of this...destructive copy

    to just get a copy of a matrix
    Matrix mm33 is a matrix initialized to some value
    Matrix inv = Matrix.inverseMxM(mm33);

    This makes a copy of mm33 into inv. mm33 is not altered in this process.

    Parameters:
    M - - matrix to copy
    Throws:
    MatrixException

    cols

    public int cols()
    Get the number of columns in this matrix

    Returns:
    the number of columns in this matrix

    rows

    public int rows()
    Gets the number of rows in this matrix

    Returns:
    the number rows in this matrix

    cell

    public double cell(int i,
                       int j)
                throws MatrixException
    return the value in this matrix located at the ith row and jth column

    Should have made this getCell, but I got too far along to make all the changes....

    Parameters:
    i - - row
    j - - column
    Returns:
    - the value at row i and column j
    Throws:
    MatrixException

    setCell

    public void setCell(int i,
                        int j,
                        double value)
                 throws MatrixException
    set the value of the cell at the ith row and jth column to value

    Parameters:
    i - - row
    j - - column
    value - - the double to put in the cell (i,j)
    Throws:
    MatrixException

    add

    public Matrix add(Matrix M2)
               throws MatrixException
    Adds two matrices together

    non-destructive

    Parameters:
    M2 - - what to add to this
    Returns:
    this matrix with M2 added to itthis + M2 of same only if both are same dim
    Throws:
    MatrixException

    add

    public static void add(Matrix r1,
                           double a)
                    throws java.lang.Exception
    Adds a constant to each element of this matrix

    Parameters:
    r1 - - the matrix to receve the addition
    a - - the vlaue to ad to each cell of the matrix
    Throws:
    java.lang.Exception

    add

    public static Matrix add(Matrix M1,
                             Matrix M2)
                      throws MatrixException
    Static method to add any two matrices

    Parameters:
    M1 -
    M2 -
    Returns:
    adds m1 and m2 only if of the same size
    Throws:
    MatrixException

    subtract

    public Matrix subtract(Matrix M2)
                    throws MatrixException
    Subtracts a matrix from this

    non-destructive

    Parameters:
    M2 -
    Returns:
    this - M2
    Throws:
    MatrixException

    subtract

    public static Matrix subtract(Matrix M1,
                                  Matrix M2)
                           throws MatrixException
    static Subtraces M2 from M1 5 = 8 - 3

    Parameters:
    M1 - - subtraced from matrix (the 8 in the above)
    M2 - - what is subtracte (the 3 in the above)
    Returns:
    - the result of subrtaction (the 5 in the above)
    Throws:
    MatrixException

    mult

    public Matrix mult(double a)
                throws MatrixException
    scales a matrix, but does not destroy the content of the original

    Non-destructive multiply

    Parameters:
    a - - the scalar
    Returns:
    this multipled by a
    Throws:
    MatrixException

    transpose

    public static Matrix transpose(Matrix in)
                            throws MatrixException
    Makes a transpose of the input matrix

    rows become columns
    Row 1 is now column 1
    Row 2 is now column 2
    Row n is now column n

    Parameters:
    in - - input matrix
    Returns:
    - the transpose of the input
    Throws:
    MatrixException

    multVec

    public static double[] multVec(Matrix A,
                                   double[] x)
                            throws MatrixException
    Performs Ax multiplication

    Parameters:
    A -
    x -
    Returns:
    the resulting array
    Throws:
    MatrixException

    multSelf

    public void multSelf(double a)
    Scalar multiply in place

    Destructive multiply

    Parameters:
    a -

    mult

    public Matrix mult(Matrix M2)
                throws MatrixException
    Multiplies 2 matrixes together

    3x1 * 1x3 = 3x3

    None Destructive

    Parameters:
    M2 -
    Returns:
    a Matrix of this * M2
    Throws:
    MatrixException

    mult

    public static Matrix mult(Matrix M1,
                              Matrix M2)
                       throws MatrixException
    multiplies two matrixes together None Destructive

    Parameters:
    M1 - left hand side
    M2 - right hand side
    Returns:
    a Matrix of M1 * M2
    Throws:
    MatrixException

    print

    public void print()
               throws MatrixException
    Prints the content of a matrix to standard out

    Throws:
    MatrixException

    inversMat3x3

    public static Matrix inversMat3x3(Matrix in)
                               throws MatrixException
    Given a 3 x 3 matrix and using Determinats to solve for inverse

    Parameters:
    in - - input 3x3 matrix
    Returns:
    - the inverse of this matrix
    Throws:
    MatrixException

    det

    public static double det(double d1,
                             double d2,
                             double d3,
                             double d4)
    Solves the determinate of this 2x2 matrix

    Parameters:
    d1 - - a11
    d2 - - a12
    d3 - - a21
    d4 - - a22
    Returns:
    the determinate of this 2x2