scpsolver.util
Class SparseMatrix

java.lang.Object
  extended by scpsolver.util.SparseMatrix
All Implemented Interfaces:
Matrix

public class SparseMatrix
extends java.lang.Object
implements Matrix

Implementation of a sparse matrix. The actual values of the matrix are saved in a SparseVector.

Author:
planatsc

Constructor Summary
SparseMatrix(double[][] q)
          Creates a new matrix with elements specified in q
SparseMatrix(int rows, int columns)
          Creates a new empty SparseMatrix
SparseMatrix(int rows, int columns, int initrowdim)
           
SparseMatrix(SparseMatrix m)
          Copy constructor for the SparseMatrix class
 
Method Summary
 Matrix add(Matrix B)
          Adds two matrices
 SparseMatrix clone()
          Returns a deep copy of the matrix
 boolean equals(java.lang.Object o)
          Compares two matrices.
 boolean equals(SparseMatrix A)
          Compares two SparseMatrices
 double get(int row, int column)
          Returns a given element of the matrix
 int getColNum()
           
 NonSparseMatrix getNonSparseMatrix()
           
 NonZeroElementIterator getNonZeroElementIterator()
           
 int getNumberOfNonZeroElements()
           
 int getNumberOfNonZeroRows()
           
 int getRowNum()
           
 double getSparsity()
           
 void grow(int size, int direction)
          Grows the matrix with zero-elements
 void grow(Matrix M, int direction)
          Grows the matrix on one side with another matrix
 Matrix plus(Matrix m)
          Adds a matrix to the matrix
static SparseMatrix readMTX(java.lang.String filename)
           
 void set(double[][] q)
          Sets all the entries of the matrix to the values supplied by the array
 void set(int row, int column, double value)
          Sets a single entry of the matrix to the given value
 SparseMatrix submatrix(int from, int to)
          Returns a quadratic submatrix with range [from, to)
 Matrix times(Matrix multiplier)
          Multiplies a matrix with another matrix
 Matrix transpose()
          Calculates the transpose matrix
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SparseMatrix

public SparseMatrix(int rows,
                    int columns)
Creates a new empty SparseMatrix

Parameters:
rows - The number of rows of the new matrix
columns - The number of columns of the new matrix

SparseMatrix

public SparseMatrix(int rows,
                    int columns,
                    int initrowdim)

SparseMatrix

public SparseMatrix(SparseMatrix m)
Copy constructor for the SparseMatrix class

Parameters:
m - The copied matrix

SparseMatrix

public SparseMatrix(double[][] q)
Creates a new matrix with elements specified in q

Parameters:
q - The elements of the new matrix
Method Detail

readMTX

public static SparseMatrix readMTX(java.lang.String filename)

clone

public SparseMatrix clone()
Returns a deep copy of the matrix

Overrides:
clone in class java.lang.Object

set

public void set(double[][] q)
Sets all the entries of the matrix to the values supplied by the array

Parameters:
q - The new entries of the matrix

get

public double get(int row,
                  int column)
Returns a given element of the matrix

Specified by:
get in interface Matrix
Parameters:
row - Specifies the row of the given element
column - Specifies the column of the given element

set

public void set(int row,
                int column,
                double value)
Sets a single entry of the matrix to the given value

Specified by:
set in interface Matrix
Parameters:
row - Specifies the row of the altered element
column - Specifies the column of the altered element
value - The new value of the entry

getRowNum

public int getRowNum()
Specified by:
getRowNum in interface Matrix

getColNum

public int getColNum()
Specified by:
getColNum in interface Matrix

transpose

public Matrix transpose()
Calculates the transpose matrix

Specified by:
transpose in interface Matrix
Returns:
The transposed matrix

times

public Matrix times(Matrix multiplier)
Multiplies a matrix with another matrix

Specified by:
times in interface Matrix
Parameters:
multiplier - The matrix with which this matrix should be multiplied
Returns:
this*multiplier

plus

public Matrix plus(Matrix m)
Adds a matrix to the matrix

Specified by:
plus in interface Matrix
Parameters:
m - The matrix which should be added to this matrix
Returns:
this+m

equals

public boolean equals(java.lang.Object o)
Compares two matrices. Notice: if the Object o isn't a probable SparseMatrix, it can't be compared yet.

Overrides:
equals in class java.lang.Object
Parameters:
o - The matrix which should be compared
Returns:
True, if this matrix and o provide the same values, false otherwise.

equals

public boolean equals(SparseMatrix A)
Compares two SparseMatrices

Parameters:
A - The matrix to be compared to
Returns:
True, if the second matrix provides the same values, false otherwise. Note that the matrices don't need to be identical in the data-arrays, so it's okay when one matrix explicitly safes a zero argument and the other doesn't.

submatrix

public SparseMatrix submatrix(int from,
                              int to)
Returns a quadratic submatrix with range [from, to)

Parameters:
from - First row and column to be copied
to - First row and column not to be copied anymore
Returns:
The submatrix

add

public Matrix add(Matrix B)
Adds two matrices

Parameters:
B - The added matrix
Returns:
This+B

grow

public void grow(int size,
                 int direction)
Grows the matrix with zero-elements

Parameters:
size - The new size of the matrix
direction - Determines where the new zero-elements going to be located. Enumeration according to the Cartesian coordinate system. So, if you want to grow the matrix on the right and on the bottom side, direction is 2.

grow

public void grow(Matrix M,
                 int direction)
Grows the matrix on one side with another matrix


getNumberOfNonZeroElements

public int getNumberOfNonZeroElements()

getSparsity

public double getSparsity()

getNonZeroElementIterator

public NonZeroElementIterator getNonZeroElementIterator()
Specified by:
getNonZeroElementIterator in interface Matrix

getNumberOfNonZeroRows

public int getNumberOfNonZeroRows()

getNonSparseMatrix

public NonSparseMatrix getNonSparseMatrix()