JIGL
v1.6

jigl.math
Class Matrix

java.lang.Object
  |
  +--jigl.math.Matrix
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
IdentityMatrix, ProjectionMatrix, RotationMatrix, ScaleMatrix, TranslationMatrix

public class Matrix
extends Object
implements Cloneable

This class allows a programmer to create real matrices with an arbitrary number of rows and columns.


Field Summary
 double[][] mem
          This member is the reference to the arrays where the entries of the matrix are stored.
 
Constructor Summary
Matrix()
          This is the default constructor.
Matrix(double[][] b)
          This constructor builds a matrix from the double array b.
Matrix(int m, int n)
          This constructor builds a zero matrix of size m-by-n.
Matrix(Matrix A)
          This constructor builds a matrix from the matrix A.
 
Method Summary
 Matrix add(Matrix A)
          This method creates a new matrix and stores the sum of this and A in it.
 Matrix assign(double[][] b)
          This method replaces the contents of the mem array with the data contained in b.
 Matrix assign(Matrix A)
          This method replaces the members of this with those of A.
 Object clone()
           
 void eigsrt(double[] lambda)
          Given the eigenvalues lambda[0..n-1] and eigenvectors contained in the matrix this as output from jacobi, this routine sorts the eigenvalues into descending order, and rearranges the columns the columns of this correspondingly.
 boolean equals(Matrix A)
          This method compares if two matrices are equal.
 Matrix gaussj(Matrix B)
          Linear equation solution by Gauss-Jordan elimination.
 double get(int i, int j)
          This method returns the (i, j) entry of the matrix.
 Vector getColumn(int j)
          This method returns column j of the matrix.
 Vector getRow(int i)
          This method returns row i of the matrix.
 int jacobi(double[] lambda, Matrix S)
          Computes all eigenvalues and eigenvectors of a real symmetric matrix this.
 Matrix mult(double a)
          This method returns of new matrix with each entry multiplied by a.
 Matrix mult(Matrix A)
          This method creates a new matrix and stores the product of this and A in it.
 Vector mult(Vector x)
          This method returns a new vector that is the result of this multplied with x.
 int nColumns()
          This method returns the number of columns in the matrix.
 int nRows()
          This method returns the number of rows in the matrix.
static double pythag(double a, double b)
          Computes (a² + b²)½ without destructive underflow or overflow.
private  void rotate(int i, int j, int k, int l, double s, double tau)
          The method performs a Jacobi rotation between entries (i, j) and (k, l).
 void set(int i, int j, double c)
          This method sets element (i, j) to c.
static double sign(double a, double b)
           
 Matrix sub(Matrix A)
          This method creates a new matrix and stores the difference of this and A in it.
static Vector svbksb(Matrix U, double[] w, Matrix V, Vector b)
          Solves A·X = B for a vector X, where is specified by the matrices U and V and the array of singular values w as returned by svdcmp.
 void svdcmp(Matrix U, double[] w, Matrix V)
          Given a matrix, this routine computes its singular value decomposition, UWVT.
private  void swap(int i, int j, int k, int l)
          This method exchanges entry (i, j) with entry (k, l).
 Matrix t()
          This method returns a new matrix containing the transpose of this.
 String toString()
          This method creates a string from the matrix.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

mem

public double[][] mem
This member is the reference to the arrays where the entries of the matrix are stored.

Constructor Detail

Matrix

public Matrix()
This is the default constructor. It does not allocate any memory.


Matrix

public Matrix(int m,
              int n)
This constructor builds a zero matrix of size m-by-n.


Matrix

public Matrix(double[][] b)
This constructor builds a matrix from the double array b.


Matrix

public Matrix(Matrix A)
This constructor builds a matrix from the matrix A.

Method Detail

nRows

public int nRows()
This method returns the number of rows in the matrix.


nColumns

public int nColumns()
This method returns the number of columns in the matrix.


get

public double get(int i,
                  int j)
           throws ArrayIndexOutOfBoundsException,
                  NullPointerException
This method returns the (i, j) entry of the matrix.

ArrayIndexOutOfBoundsException
NullPointerException

getRow

public Vector getRow(int i)
              throws ArrayIndexOutOfBoundsException,
                     NullPointerException
This method returns row i of the matrix.

ArrayIndexOutOfBoundsException
NullPointerException

getColumn

public Vector getColumn(int j)
                 throws ArrayIndexOutOfBoundsException,
                        NullPointerException
This method returns column j of the matrix.

ArrayIndexOutOfBoundsException
NullPointerException

set

public void set(int i,
                int j,
                double c)
         throws ArrayIndexOutOfBoundsException,
                NullPointerException
This method sets element (i, j) to c.

ArrayIndexOutOfBoundsException
NullPointerException

assign

public Matrix assign(double[][] b)
              throws ArrayStoreException
This method replaces the contents of the mem array with the data contained in b. Each sub-array of b must be of the same length.

ArrayStoreException

assign

public Matrix assign(Matrix A)
This method replaces the members of this with those of A.


equals

public boolean equals(Matrix A)
               throws ArithmeticException
This method compares if two matrices are equal. They are equal if and only if every element of this is equal to evey element of A.

ArithmeticException

add

public Matrix add(Matrix A)
           throws ArithmeticException
This method creates a new matrix and stores the sum of this and A in it.

The matrices must be the same size.

ArithmeticException

sub

public Matrix sub(Matrix A)
           throws ArithmeticException
This method creates a new matrix and stores the difference of this and A in it.

The matrices must be the same size.

ArithmeticException

mult

public Matrix mult(Matrix A)
            throws ArithmeticException
This method creates a new matrix and stores the product of this and A in it.

The number of columns in this must equal the number of rows of A.

ArithmeticException

mult

public Matrix mult(double a)
This method returns of new matrix with each entry multiplied by a.


mult

public Vector mult(Vector x)
            throws ArithmeticException
This method returns a new vector that is the result of this multplied with x.

The number of columns of this must be equal to the dimension of x.

ArithmeticException

t

public Matrix t()
         throws ArithmeticException
This method returns a new matrix containing the transpose of this.

The matrix must be a square matrix.

ArithmeticException

swap

private void swap(int i,
                  int j,
                  int k,
                  int l)
This method exchanges entry (i, j) with entry (k, l).

It is called by gaussj.


gaussj

public Matrix gaussj(Matrix B)
              throws ArithmeticException
Linear equation solution by Gauss-Jordan elimination. this references the input matrix. B is an n-by-m matrix containing the m right-hand side vectors. On output, a matrix is returned that is the inverse of this, and the values of B are replaced by the corresponding set of solution vectors.

ArithmeticException
See Also:
Gauss-Jordan elimination Algorithm

rotate

private void rotate(int i,
                    int j,
                    int k,
                    int l,
                    double s,
                    double tau)
The method performs a Jacobi rotation between entries (i, j) and (k, l).


eigsrt

public void eigsrt(double[] lambda)
Given the eigenvalues lambda[0..n-1] and eigenvectors contained in the matrix this as output from jacobi, this routine sorts the eigenvalues into descending order, and rearranges the columns the columns of this correspondingly. The method is straight insertion.


jacobi

public int jacobi(double[] lambda,
                  Matrix S)
           throws ArithmeticException,
                  ArrayStoreException
Computes all eigenvalues and eigenvectors of a real symmetric matrix this. lamdba contains the eigenvalues of this. S is a matrix whose columns contain, on output, the normalized eigenvectors of this.

ArithmeticException
ArrayStoreException

pythag

public static double pythag(double a,
                            double b)
Computes (a² + b²)½ without destructive underflow or overflow.


sign

public static double sign(double a,
                          double b)

svbksb

public static Vector svbksb(Matrix U,
                            double[] w,
                            Matrix V,
                            Vector b)
                     throws ArithmeticException
Solves A·X = B for a vector X, where is specified by the matrices U and V and the array of singular values w as returned by svdcmp. b is the input right-hand side. No input quantities are destroyed, so the routine may be called sequentially with different b's.

ArithmeticException

svdcmp

public void svdcmp(Matrix U,
                   double[] w,
                   Matrix V)
            throws ArithmeticException,
                   ArrayStoreException
Given a matrix, this routine computes its singular value decomposition, UWVT. The diagonal matrix of singular values W is output as the array w.

ArithmeticException
ArrayStoreException

toString

public String toString()
This method creates a string from the matrix.

Overrides:
toString in class Object

clone

public Object clone()
Overrides:
clone in class Object

JIGL
v1.6

Submit a bug or feature
Revised in May 2002 by Weiming Liu