JSci.maths.matrices.DoubleSparseMatrix Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.maths.matrices;
import JSci.GlobalSettings;
import JSci.maths.Mapping;
import JSci.maths.DimensionException;
import JSci.maths.vectors.AbstractDoubleVector;
import JSci.maths.vectors.DoubleVector;
import JSci.maths.vectors.DoubleSparseVector;
/**
* The DoubleSparseMatrix class provides an object for encapsulating sparse matrices.
* Uses compressed row storage (Yale sparse matrix format).
* @version 1.4
* @author Mark Hale
*/
public final class DoubleSparseMatrix extends AbstractDoubleMatrix {
/**
* Matrix elements.
*/
private double elements[];
/**
* Sparse indexing data.
* Contains the column positions of each element,
* e.g. colPos[n]
is the column position
* of the n
th element.
*/
private int colPos[];
/**
* Sparse indexing data.
* Contains the indices of the start of each row,
* e.g. rows[i]
is the index
* where the i
th row starts.
*/
private int rows[];
/**
* Amount by which to increase the capacity.
*/
private int capacityIncrement = 1;
/**
* Constructs an empty matrix.
* @param rowCount the number of rows
* @param colCount the number of columns
*/
public DoubleSparseMatrix(final int rowCount, final int colCount) {
super(rowCount, colCount);
elements=new double[0];
colPos=new int[0];
rows=new int[numRows+1];
}
public DoubleSparseMatrix(final int rowCount, final int colCount, int capacityIncrement) {
this(rowCount, colCount);
this.capacityIncrement = capacityIncrement;
}
/**
* Constructs a matrix from an array.
* @param array an assigned value
*/
public DoubleSparseMatrix(final double array[][]) {
super(array.length,array[0].length);
rows=new int[numRows+1];
int n=0;
for(int i=0;iGlobalSettings.ZERO_TOL)
n++;
}
}
elements=new double[n];
colPos=new int[n];
n=0;
for(int i=0;iGlobalSettings.ZERO_TOL) {
elements[n]=array[i][j];
colPos[n]=j;
n++;
}
}
}
rows[numRows]=n;
}
/**
* Compares two double sparse matrices for equality.
* @param m a double matrix
*/
public boolean equals(AbstractDoubleMatrix m, double tol) {
if(numRows==m.numRows && numCols==m.numCols) {
if(m instanceof DoubleSparseMatrix) {
return this.equals((DoubleSparseMatrix)m);
} else {
double sumSqr = 0;
for(int i=0;i=0 && i=0 && j=0 && i=0 && j GlobalSettings.ZERO_TOL) {
// expand
if(rows[numRows] == elements.length) {
// increase capacity
final double oldMatrix[]=elements;
final int oldColPos[]=colPos;
elements=new double[oldMatrix.length+capacityIncrement];
colPos=new int[oldColPos.length+capacityIncrement];
System.arraycopy(oldMatrix,0,elements,0,p);
System.arraycopy(oldColPos,0,colPos,0,p);
System.arraycopy(oldMatrix,p,elements,p+1,oldMatrix.length-p);
System.arraycopy(oldColPos,p,colPos,p+1,oldColPos.length-p);
} else {
System.arraycopy(elements,p,elements,p+1,rows[numRows]-p);
System.arraycopy(colPos,p,colPos,p+1,rows[numRows]-p);
}
elements[p]=x;
colPos[p]=j;
for(int k=i+1;k-norm.
*/
public double infNorm() {
double result=0.0,tmpResult;
for(int i=0;iresult)
result=tmpResult;
}
return result;
}
/**
* Returns the Frobenius (l2) norm.
*/
public double frobeniusNorm() {
double result=0.0;
for(int i=0;i