org.jscience.physics.fluids.dynamics.util
Class Matrix

java.lang.Object
  extended by org.jscience.physics.fluids.dynamics.util.Matrix

public class Matrix
extends java.lang.Object

Sparse Matrix, simetric or not. This class is in charge of the data and methods related to matrix operations.

The disperse storage is a technique that allows storing large matrices in memory by using much less RAM. The method consists in working only with the non null elements: the matrices used in finite elements, finite differences, and other numerical methods have many elements as nulls.

The Matrix is stored in three vectors: elem, ipos and jpos. They are public to allow other classes to implement optimized algorithms.

For more information about sparse matrices consult the program manual.

Revised for ADFC v2.0


Field Summary
 double[] elem
          Contains the value of the elements not null.
 int[] ipos
          ipos[n] indicates in which position of elem and jpos starts the row n-esima of the matrix.
 int[] jpos
          elem[n] indicates in which column it is found the element not null n-esimo.
 boolean simetric
          Indicates if the matrix is stored using its simetry.
 
Constructor Summary
Matrix(double[] as, int[] aipos, int[] ajpos, boolean sim)
          constructor.
Matrix(Matrix matrix, boolean swallow)
          copy constructor.
 
Method Summary
 void applyFactor(double factor)
          multiply all the elements by a real number.
 void assignElement(int i, int j, double value)
          assigns the value of a element.
 void checkRigidityMatrix()
          checks that the matrix of rigidity is more or less correct in structure.
 int getRows()
          returns the number of rows.
 double[] multipliy(double[] vector)
          multiply this matrix by a vector.
 double[] multiply(double[] result, double[] vector)
          multiply the matrix by a vector.
 double[] multiply(double[] result, double[] vector, int rowSize)
          multiply simetric/asimetric matrix by vector.
 double[] multiply(double[] vector, int rowSize)
          multiply simetric/asimetric matrix by vector.
 void printMatrix()
          show matrix in the console.
 double readElement(int i, int j)
          returns the value of a element.
 void saveToFile(java.lang.String name, java.lang.String title)
          Saves the content of the matrix to a file.
 double scalarProductOfMatrixProduct(double[] a, double[] b)
          this function carries out the product a(Mb)
 void sumElement(int i, int j, double value)
          sums the specified value in the position (i, j).
 void sumMatrix(Matrix m, double factor)
          sums element by element the content of other matrix, which is supposed with identical Ipos y Jpos.
 void sumParallel(int i, int j, double value, Matrix sister, double value2)
          sums in the position (i, j) the given value, and at the same position of the sister matrix, sum value2.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

elem

public double[] elem
Contains the value of the elements not null. Therefore, elem[n] is the value of element not null n-esimo.


jpos

public int[] jpos
elem[n] indicates in which column it is found the element not null n-esimo.


ipos

public int[] ipos
ipos[n] indicates in which position of elem and jpos starts the row n-esima of the matrix.


simetric

public boolean simetric
Indicates if the matrix is stored using its simetry. If so, it corresponds with the lower triangule.

Constructor Detail

Matrix

public Matrix(double[] as,
              int[] aipos,
              int[] ajpos,
              boolean sim)
constructor. If as is null, means that the constructor should reserve the memory for as (so in this case, a new empty matrix).

Parameters:
as - elements in compact format.
aipos - vector Ipos (NVPN)
ajpos - vector Jpos (NNVI)
sim - simetric or not

Matrix

public Matrix(Matrix matrix,
              boolean swallow)
copy constructor. Constructs a new object duplicating the contents of the original matrix. If especifica swallow, the copy of ipos and jpos is not by value, but by reference. This way, we can have two identical matrices without duplicating RAM in those vectors. Elem is always duplicated, to allow modifications without lateral effects.

Parameters:
matrix - matrix to copy.
swallow - copy swallow (no duplication of RAM for the structure ipos/jpos)
Method Detail

applyFactor

public void applyFactor(double factor)
multiply all the elements by a real number.

Parameters:
factor - real number.

assignElement

public void assignElement(int i,
                          int j,
                          double value)
assigns the value of a element.

Parameters:
i - row
j - column
value - new value

checkRigidityMatrix

public void checkRigidityMatrix()
checks that the matrix of rigidity is more or less correct in structure.


printMatrix

public void printMatrix()
show matrix in the console.


getRows

public int getRows()
returns the number of rows.

Returns:
number of rows.

readElement

public double readElement(int i,
                          int j)
returns the value of a element.

Parameters:
i - row
j - column
Returns:
value del element

multiply

public double[] multiply(double[] result,
                         double[] vector)
multiply the matrix by a vector.

Parameters:
result - where to store the solution. It can be null.
vector - vector to multiply.
Returns:
vector result.

multipliy

public double[] multipliy(double[] vector)
multiply this matrix by a vector.

Parameters:
vector - vector to multiply.
Returns:
vector result.

multiply

public double[] multiply(double[] vector,
                         int rowSize)
multiply simetric/asimetric matrix by vector. Updated to a faster version, which demands that the first element of a row is the diagonal.

Parameters:
vector - column.
rowSize - size of the row (and of the solution too)
Returns:
vector column solution.

multiply

public double[] multiply(double[] result,
                         double[] vector,
                         int rowSize)
multiply simetric/asimetric matrix by vector. Updated to a faster version, which demands that the first element of a row is the diagonal.

Parameters:
result - where to store the result.
vector - column.
rowSize - size of the row (and of the solution too)
Returns:
vector column solution.

scalarProductOfMatrixProduct

public double scalarProductOfMatrixProduct(double[] a,
                                           double[] b)
this function carries out the product a(Mb)

Parameters:
a - vector
b - vector
Returns:
scalar product of vectors a and b.

sumElement

public void sumElement(int i,
                       int j,
                       double value)
sums the specified value in the position (i, j).

Parameters:
i - row of the position to be summed.
j - column of the position to be summed.
value - value to be summed in the specified position.

sumParallel

public void sumParallel(int i,
                        int j,
                        double value,
                        Matrix sister,
                        double value2)
sums in the position (i, j) the given value, and at the same position of the sister matrix, sum value2. This method es useful in cases of dos consecutive sums in identical positions and with matrices of equal estructure. For example: the mass matrix and the matrix of rigidity, the matrices of divergence X and Y... For efficiency, it is not verified that the structure is identical. That has to be done by the programmer.

Parameters:
i - row of the position to be summed.
j - column of the position to be summed.
value - value to be summed in the given position of this.
sister - the other matrix in which we are summing in parallel.
value2 - value to be summed in the given position of sister.

sumMatrix

public void sumMatrix(Matrix m,
                      double factor)
sums element by element the content of other matrix, which is supposed with identical Ipos y Jpos.

Parameters:
m - matrix to be summed to this
factor - factor to be applied to the term. For example, -1 differentiates.

saveToFile

public void saveToFile(java.lang.String name,
                       java.lang.String title)
Saves the content of the matrix to a file. Mostly for debugging.

Parameters:
name - path to the file in which to save the matrix.
title - string as title in the first line of the file