Utils
Class Matrix

java.lang.Object
  extended by Utils.Matrix

public class Matrix
extends java.lang.Object

Class implementing several operations on double matrices.


Constructor Summary
Matrix(double[][] a)
          Builds a matrix from a bi-dimensional array.
Matrix(int n)
          Builds a square matrix containing 0 values.
Matrix(int n, int m)
          Builds a matrix containing 0 values.
Matrix(Matrix matrix)
          Copy constructor
 
Method Summary
 Matrix clone()
          Returns a copy of this matrix.
 double condition()
          Computes the condition number of this matrix.
 double determinant()
          Computes the determinant of this matrix.
 Matrix gauss()
          Performs the Gauss algorithm (without switching rows)
 Matrix gaussPivot()
          Performs the Gauss algorithm (using the maximum pivot technique)
 Matrix gaussSeidel()
          Returns the Gauss-Seidel iteration matrix.
 double get(int i, int j)
          Returns the value of a matrix element.
 int getColumns()
          Returns the width of the matrix.
 double getMinValue()
          Returns the minimum value in this matrix.
 int getRows()
          Returns the height of the matrix.
static double[][] getSubMatrix(double[][] matrix, boolean width, boolean height)
          Extracts a submatrix from a bi-dimensional double array.
static Matrix identity(int n)
          Creates an identity matrix.
 double infinityNorm()
          Computes the infinity-norm of this matrix.
 Matrix invert()
          Computes the inverse matrix.
static double[][] invert(double[][] m)
          Inverts a double bi-dimensional array.
 Matrix jacobi()
          Returns the Jacobi iteration matrix.
 Matrix multiply(double value)
          Multiplies all elements of this matrix to a given real value.
 Matrix multiply(Matrix matrix)
          Multiplies the matrix to a given matrix.
 double oneNorm()
          Computes the 1-norm of this matrix.
 void set(int i, int j, double v)
          Modifies a matrix element.
 double[] solve(boolean pivot)
          Solves a linear problem based on a complete matrix.
 double[] solve(double[] b, boolean pivot)
          Solves a linear problem based on this matrix and a given array using the Gauss algorithm.
 Matrix sum(Matrix matrix)
          Sums this matrix to a given one.
 void switchRows(int i, int j)
          Switches two rows.
 double[][] toDouble()
          Returns a copy of this matrix as a bi-dimensional double array.
 float[][] toFloat()
          Returns a copy of this matrix as a bi-dimensional float array.
 java.lang.String toString()
          Returns a string representation of this matrix.
 java.lang.String toString(int i, int j)
          Returns a string representation of a matrix element.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Matrix

public Matrix(int n)
Builds a square matrix containing 0 values.

Parameters:
n - Number of rows/columns of the matrix.

Matrix

public Matrix(int n,
              int m)
Builds a matrix containing 0 values.

Parameters:
n - Number of rows of the matrix.
m - Number of columns of the matrix.

Matrix

public Matrix(double[][] a)
Builds a matrix from a bi-dimensional array.

Parameters:
a - The bi-dimensional array whose values are to be copied into the matrix.

Matrix

public Matrix(Matrix matrix)
Copy constructor

Parameters:
matrix - The matrix to be copied.
Method Detail

getRows

public int getRows()
Returns the height of the matrix.

Returns:
The number of rows in this matrix.

getColumns

public int getColumns()
Returns the width of the matrix.

Returns:
The number of columns in this matrix.

get

public double get(int i,
                  int j)
Returns the value of a matrix element.

Parameters:
i - Row index.
j - Column index.
Returns:
Value at the given position.

set

public void set(int i,
                int j,
                double v)
Modifies a matrix element.

Parameters:
i - Row index.
j - Column index.
v - Value to insert at the given position.

identity

public static Matrix identity(int n)
Creates an identity matrix.

Parameters:
n - Number of rows/columns of the matrix.
Returns:
The identity matrix whose values are 1 on the main diagonal and 0 otherwise.

sum

public Matrix sum(Matrix matrix)
Sums this matrix to a given one.

Parameters:
matrix - The matrix to be added.
Returns:
A new matrix obtained as the sum of the two matrices, or null if the two matrices have different sizes.

multiply

public Matrix multiply(double value)
Multiplies all elements of this matrix to a given real value.

Parameters:
value - The value matrix to be multiplied to matrix elements.
Returns:
The new product matrix.

multiply

public Matrix multiply(Matrix matrix)
Multiplies the matrix to a given matrix.

Parameters:
matrix - The matrix to be added.
Returns:
A new matrix obtained as the sum of the two matrices, or null if the two matrices have incompatible sizes.

switchRows

public void switchRows(int i,
                       int j)
Switches two rows.

Parameters:
i - The first row.
j - The second row.

gauss

public Matrix gauss()
Performs the Gauss algorithm (without switching rows)

Returns:
The triangular Gauss matrix (obtained without row-switching)

gaussPivot

public Matrix gaussPivot()
Performs the Gauss algorithm (using the maximum pivot technique)

Returns:
The triangular Gauss matrix.

solve

public double[] solve(double[] b,
                      boolean pivot)
Solves a linear problem based on this matrix and a given array using the Gauss algorithm.

Parameters:
b - The values array.
pivot - If true uses the pivot-based Gauss algorithm.
Returns:
An array containing the solution of the linear problem.

solve

public double[] solve(boolean pivot)
Solves a linear problem based on a complete matrix.

Parameters:
pivot - If true uses the pivot-based Gauss algorithm.
Returns:
An array containing the solution of the linear problem.

invert

public Matrix invert()
Computes the inverse matrix.

Returns:
The inverse matrix.

invert

public static double[][] invert(double[][] m)
Inverts a double bi-dimensional array.

Parameters:
m - Bi-dimensional array to invert.
Returns:
The inverse matrix as a bi-dimensional array.

oneNorm

public double oneNorm()
Computes the 1-norm of this matrix.

Returns:
The 1-norm of this matrix.

infinityNorm

public double infinityNorm()
Computes the infinity-norm of this matrix.

Returns:
The infinity-norm of this matrix.

condition

public double condition()
Computes the condition number of this matrix.

Returns:
The condition number of this matrix.

determinant

public double determinant()
Computes the determinant of this matrix.

Returns:
The determinant of this matrix.

jacobi

public Matrix jacobi()
Returns the Jacobi iteration matrix.

Returns:
The iteration matrix computed using the Jacobi algorithm.

gaussSeidel

public Matrix gaussSeidel()
Returns the Gauss-Seidel iteration matrix.

Returns:
The iteration matrix computed using the Gauss-Seidel algorithm.

toFloat

public float[][] toFloat()
Returns a copy of this matrix as a bi-dimensional float array.

Returns:
A copy of this matrix as a bi-dimensional float array.

toDouble

public double[][] toDouble()
Returns a copy of this matrix as a bi-dimensional double array.

Returns:
A copy of this matrix as a bi-dimensional double array.

clone

public Matrix clone()
Returns a copy of this matrix.

Overrides:
clone in class java.lang.Object
Returns:
A copy of this matrix.

toString

public java.lang.String toString()
Returns a string representation of this matrix.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this matrix.

toString

public java.lang.String toString(int i,
                                 int j)
Returns a string representation of a matrix element.

Parameters:
i - Row index.
j - Column index.
Returns:
A string representation of a matrix element.

getMinValue

public double getMinValue()
Returns the minimum value in this matrix.

Returns:
The minimum value in this matrix.

getSubMatrix

public static double[][] getSubMatrix(double[][] matrix,
                                      boolean width,
                                      boolean height)
Extracts a submatrix from a bi-dimensional double array.

Parameters:
matrix - The bi-dimensional array where the submatrix is extracted from.
width - Selects either the left (true) or the right (false) part of the matrix.
height - Selects either the lower (true) or the upper (false) part of the matrix.
Returns:
The extracted submatrix.