/*
Copyright (C) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
is hereby granted without fee, provided that the above copyright notice appear in all copies and 
that both that copyright notice and this permission notice appear in supporting documentation. 
CERN makes no representations about the suitability of this software for any purpose. 
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.impl;

import cern.colt.map.AbstractIntDoubleMap;
import cern.colt.map.OpenIntDoubleHashMap;
import cern.colt.matrix.DoubleMatrix1D;
import cern.colt.matrix.DoubleMatrix2D;
/**
Sparse hashed 2-d matrix holding double elements.
First see the package summary and javadoc tree view to get the broad picture.

Implementation:

Note that this implementation is not synchronized. Uses a {@link cern.colt.map.OpenIntDoubleHashMap}, which is a compact and performant hashing technique.

Memory requirements:

Cells that

worst case: memory [bytes] = (1/minLoadFactor) * nonZeros * 13.
best case: memory [bytes] = (1/maxLoadFactor) * nonZeros * 13.
Where nonZeros = cardinality() is the number of non-zero cells. Thus, a 1000 x 1000 matrix with minLoadFactor=0.25 and maxLoadFactor=0.5 and 1000000 non-zero cells consumes between 25 MB and 50 MB. The same 1000 x 1000 matrix with 1000 non-zero cells consumes between 25 and 50 KB.

Time complexity:

This class offers expected time complexity O(1) (i.e. constant time) for the basic operations get, getQuick, set, setQuick and size assuming the hash function disperses the elements properly among the buckets. Otherwise, pathological cases, although highly improbable, can occur, degrading performance to O(N) in the worst case. As such this sparse class is expected to have no worse time complexity than its dense counterpart {@link DenseDoubleMatrix2D}. However, constant factors are considerably larger.

Cells are internally addressed in row-major. Performance sensitive applications can exploit this fact. Setting values in a loop row-by-row is quicker than column-by-column, because fewer hash collisions occur. Thus

	for (int row=0; row < rows; row++) {
		for (int column=0; column < columns; column++) {
			matrix.setQuick(row,column,someValue);
		}
	}
is quicker than
	for (int column=0; column < columns; column++) {
		for (int row=0; row < rows; row++) {
			matrix.setQuick(row,column,someValue);
		}
	}
@see cern.colt.map @see cern.colt.map.OpenIntDoubleHashMap @author wolfgang.hoschek@cern.ch @version 1.0, 09/24/99 */ public class SparseDoubleMatrix2D extends DoubleMatrix2D { /* * The elements of the matrix. */ protected AbstractIntDoubleMap elements; protected int dummy; /** * Constructs a matrix with a copy of the given values. * values is required to have the form values[row][column] * and have exactly the same number of columns in every row. *

* The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa. * * @param values The values to be filled into the new matrix. * @throws IllegalArgumentException if for any 1 <= row < values.length: values[row].length != values[row-1].length. */ public SparseDoubleMatrix2D(double[][] values) { this(values.length, values.length==0 ? 0: values[0].length); assign(values); } /** * Constructs a matrix with a given number of rows and columns and default memory usage. * All entries are initially 0. * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @throws IllegalArgumentException if rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE. */ public SparseDoubleMatrix2D(int rows, int columns) { this(rows,columns,rows*(columns/1000),0.2,0.5); } /** * Constructs a matrix with a given number of rows and columns using memory as specified. * All entries are initially 0. * For details related to memory usage see {@link cern.colt.map.OpenIntDoubleHashMap}. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @param initialCapacity the initial capacity of the hash map. * If not known, set initialCapacity=0 or small. * @param minLoadFactor the minimum load factor of the hash map. * @param maxLoadFactor the maximum load factor of the hash map. * @throws IllegalArgumentException if initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) || (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >= maxLoadFactor). * @throws IllegalArgumentException if rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE. */ public SparseDoubleMatrix2D(int rows, int columns, int initialCapacity, double minLoadFactor, double maxLoadFactor) { setUp(rows,columns); this.elements = new OpenIntDoubleHashMap(initialCapacity, minLoadFactor, maxLoadFactor); } /** * Constructs a view with the given parameters. * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @param elements the cells. * @param rowZero the position of the first element. * @param columnZero the position of the first element. * @param rowStride the number of elements between two rows, i.e. index(i+1,j)-index(i,j). * @param columnStride the number of elements between two columns, i.e. index(i,j+1)-index(i,j). * @throws IllegalArgumentException if rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE or flip's are illegal. */ protected SparseDoubleMatrix2D(int rows, int columns, AbstractIntDoubleMap elements, int rowZero, int columnZero, int rowStride, int columnStride) { setUp(rows,columns,rowZero,columnZero,rowStride,columnStride); this.elements = elements; this.isNoView = false; } /** * Sets all cells to the state specified by value. * @param value the value to be filled into the cells. * @return this (for convenience only). */ public DoubleMatrix2D assign(double value) { // overriden for performance only if (this.isNoView && value==0) this.elements.clear(); else super.assign(value); return this; } /** Assigns the result of a function to each cell; x[row,col] = function(x[row,col]).

Example:

matrix = 2 x 2 matrix
0.5 1.5      
2.5 3.5

// change each cell to its sine
matrix.assign(cern.jet.math.Functions.sin);
-->
2 x 2 matrix
0.479426  0.997495 
0.598472 -0.350783
For further examples, see the package doc. @param function a function object taking as argument the current cell's value. @return this (for convenience only). @see cern.jet.math.Functions */ public DoubleMatrix2D assign(cern.colt.function.DoubleFunction function) { if (this.isNoView && function instanceof cern.jet.math.Mult) { // x[i] = mult*x[i] this.elements.assign(function); } else { super.assign(function); } return this; } /** * Replaces all cell values of the receiver with the values of another matrix. * Both matrices must have the same number of rows and columns. * If both matrices share the same cells (as is the case if they are views derived from the same matrix) and intersect in an ambiguous way, then replaces as if using an intermediate auxiliary deep copy of other. * * @param source the source matrix to copy from (may be identical to the receiver). * @return this (for convenience only). * @throws IllegalArgumentException if columns() != source.columns() || rows() != source.rows() */ public DoubleMatrix2D assign(DoubleMatrix2D source) { // overriden for performance only if (! (source instanceof SparseDoubleMatrix2D)) { return super.assign(source); } SparseDoubleMatrix2D other = (SparseDoubleMatrix2D) source; if (other==this) return this; // nothing to do checkShape(other); if (this.isNoView && other.isNoView) { // quickest this.elements.assign(other.elements); return this; } return super.assign(source); } public DoubleMatrix2D assign(final DoubleMatrix2D y, cern.colt.function.DoubleDoubleFunction function) { if (!this.isNoView) return super.assign(y,function); checkShape(y); if (function instanceof cern.jet.math.PlusMult) { // x[i] = x[i] + alpha*y[i] final double alpha = ((cern.jet.math.PlusMult) function).multiplicator; if (alpha==0) return this; // nothing to do y.forEachNonZero( new cern.colt.function.IntIntDoubleFunction() { public double apply(int i, int j, double value) { setQuick(i,j,getQuick(i,j) + alpha*value); return value; } } ); return this; } if (function==cern.jet.math.Functions.mult) { // x[i] = x[i] * y[i] this.elements.forEachPair( new cern.colt.function.IntDoubleProcedure() { public boolean apply(int key, double value) { int i = key/columns; int j = key%columns; double r = value * y.getQuick(i,j); if (r!=value) elements.put(key,r); return true; } } ); } if (function==cern.jet.math.Functions.div) { // x[i] = x[i] / y[i] this.elements.forEachPair( new cern.colt.function.IntDoubleProcedure() { public boolean apply(int key, double value) { int i = key/columns; int j = key%columns; double r = value / y.getQuick(i,j); if (r!=value) elements.put(key,r); return true; } } ); } return super.assign(y,function); } /** * Returns the number of cells having non-zero values. */ public int cardinality() { if (this.isNoView) return this.elements.size(); else return super.cardinality(); } /** * Ensures that the receiver can hold at least the specified number of non-zero cells without needing to allocate new internal memory. * If necessary, allocates new internal memory and increases the capacity of the receiver. *

* This method never need be called; it is for performance tuning only. * Calling this method before tt>set()ing a large number of non-zero values boosts performance, * because the receiver will grow only once instead of potentially many times and hash collisions get less probable. * * @param minNonZeros the desired minimum number of non-zero cells. */ public void ensureCapacity(int minCapacity) { this.elements.ensureCapacity(minCapacity); } public DoubleMatrix2D forEachNonZero(final cern.colt.function.IntIntDoubleFunction function) { if (this.isNoView) { this.elements.forEachPair( new cern.colt.function.IntDoubleProcedure() { public boolean apply(int key, double value) { int i = key/columns; int j = key%columns; double r = function.apply(i,j,value); if (r!=value) elements.put(key,r); return true; } } ); } else { super.forEachNonZero(function); } return this; } /** * Returns the matrix cell value at coordinate [row,column]. * *

Provided with invalid parameters this method may return invalid objects without throwing any exception. * You should only use this method when you are absolutely sure that the coordinate is within bounds. * Precondition (unchecked): 0 <= column < columns() && 0 <= row < rows(). * * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @return the value at the specified coordinate. */ public double getQuick(int row, int column) { //if (debug) if (column<0 || column>=columns || row<0 || row>=rows) throw new IndexOutOfBoundsException("row:"+row+", column:"+column); //return this.elements.get(index(row,column)); //manually inlined: return this.elements.get(rowZero + row*rowStride + columnZero + column*columnStride); } /** * Returns true if both matrices share common cells. * More formally, returns true if at least one of the following conditions is met *

*/ protected boolean haveSharedCellsRaw(DoubleMatrix2D other) { if (other instanceof SelectedSparseDoubleMatrix2D) { SelectedSparseDoubleMatrix2D otherMatrix = (SelectedSparseDoubleMatrix2D) other; return this.elements==otherMatrix.elements; } else if (other instanceof SparseDoubleMatrix2D) { SparseDoubleMatrix2D otherMatrix = (SparseDoubleMatrix2D) other; return this.elements==otherMatrix.elements; } return false; } /** * Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array. * * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. */ protected int index(int row, int column) { // return super.index(row,column); // manually inlined for speed: return rowZero + row*rowStride + columnZero + column*columnStride; } /** * Construct and returns a new empty matrix of the same dynamic type as the receiver, having the specified number of rows and columns. * For example, if the receiver is an instance of type DenseDoubleMatrix2D the new matrix must also be of type DenseDoubleMatrix2D, * if the receiver is an instance of type SparseDoubleMatrix2D the new matrix must also be of type SparseDoubleMatrix2D, etc. * In general, the new matrix should have internal parametrization as similar as possible. * * @param rows the number of rows the matrix shall have. * @param columns the number of columns the matrix shall have. * @return a new empty matrix of the same dynamic type. */ public DoubleMatrix2D like(int rows, int columns) { return new SparseDoubleMatrix2D(rows, columns); } /** * Construct and returns a new 1-d matrix of the corresponding dynamic type, entirelly independent of the receiver. * For example, if the receiver is an instance of type DenseDoubleMatrix2D the new matrix must be of type DenseDoubleMatrix1D, * if the receiver is an instance of type SparseDoubleMatrix2D the new matrix must be of type SparseDoubleMatrix1D, etc. * * @param size the number of cells the matrix shall have. * @return a new matrix of the corresponding dynamic type. */ public DoubleMatrix1D like1D(int size) { return new SparseDoubleMatrix1D(size); } /** * Construct and returns a new 1-d matrix of the corresponding dynamic type, sharing the same cells. * For example, if the receiver is an instance of type DenseDoubleMatrix2D the new matrix must be of type DenseDoubleMatrix1D, * if the receiver is an instance of type SparseDoubleMatrix2D the new matrix must be of type SparseDoubleMatrix1D, etc. * * @param size the number of cells the matrix shall have. * @param offset the index of the first element. * @param stride the number of indexes between any two elements, i.e. index(i+1)-index(i). * @return a new matrix of the corresponding dynamic type. */ protected DoubleMatrix1D like1D(int size, int offset, int stride) { return new SparseDoubleMatrix1D(size,this.elements,offset,stride); } /** * Sets the matrix cell at coordinate [row,column] to the specified value. * *

Provided with invalid parameters this method may access illegal indexes without throwing any exception. * You should only use this method when you are absolutely sure that the coordinate is within bounds. * Precondition (unchecked): 0 <= column < columns() && 0 <= row < rows(). * * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @param value the value to be filled into the specified cell. */ public void setQuick(int row, int column, double value) { //if (debug) if (column<0 || column>=columns || row<0 || row>=rows) throw new IndexOutOfBoundsException("row:"+row+", column:"+column); //int index = index(row,column); //manually inlined: int index = rowZero + row*rowStride + columnZero + column*columnStride; //if (value == 0 || Math.abs(value) < TOLERANCE) if (value == 0) this.elements.removeKey(index); else this.elements.put(index, value); } /** * Releases any superfluous memory created by explicitly putting zero values into cells formerly having non-zero values; * An application can use this operation to minimize the * storage of the receiver. *

* Background: *

* Cells that

* A sequence like set(r,c,5); set(r,c,0); * sets a cell to non-zero state and later back to zero state. * Such as sequence generates obsolete memory that is automatically reclaimed from time to time or can manually be reclaimed by calling trimToSize(). * Putting zeros into cells already containing zeros does not generate obsolete memory since no memory was allocated to them in the first place. */ public void trimToSize() { this.elements.trimToSize(); } /** * Construct and returns a new selection view. * * @param rowOffsets the offsets of the visible elements. * @param columnOffsets the offsets of the visible elements. * @return a new view. */ protected DoubleMatrix2D viewSelectionLike(int[] rowOffsets, int[] columnOffsets) { return new SelectedSparseDoubleMatrix2D(this.elements,rowOffsets,columnOffsets,0); } public DoubleMatrix1D zMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, final boolean transposeA) { int m = rows; int n = columns; if (transposeA) { m = columns; n = rows; } boolean ignore = (z==null); if (z==null) z = new DenseDoubleMatrix1D(m); if (!(this.isNoView && y instanceof DenseDoubleMatrix1D && z instanceof DenseDoubleMatrix1D)) { return super.zMult(y,z,alpha,beta,transposeA); } if (n != y.size() || m > z.size()) throw new IllegalArgumentException("Incompatible args: "+ ((transposeA ? viewDice() : this).toStringShort()) +", "+y.toStringShort()+", "+z.toStringShort()); if (!ignore) z.assign(cern.jet.math.Functions.mult(beta/alpha)); DenseDoubleMatrix1D zz = (DenseDoubleMatrix1D) z; final double[] zElements = zz.elements; final int zStride = zz.stride; final int zi = z.index(0); DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D) y; final double[] yElements = yy.elements; final int yStride = yy.stride; final int yi = y.index(0); if (yElements==null || zElements==null) throw new InternalError(); this.elements.forEachPair( new cern.colt.function.IntDoubleProcedure() { public boolean apply(int key, double value) { int i = key/columns; int j = key%columns; if (transposeA) { int tmp=i; i=j; j=tmp; } zElements[zi + zStride*i] += value * yElements[yi + yStride*j]; //System.out.println("["+i+","+j+"]-->"+value); return true; } } ); /* forEachNonZero( new cern.colt.function.IntIntDoubleFunction() { public double apply(int i, int j, double value) { if (transposeA) { int tmp=i; i=j; j=tmp; } zElements[zi + zStride*i] += value * yElements[yi + yStride*j]; //z.setQuick(row,z.getQuick(row) + value * y.getQuick(column)); //System.out.println("["+i+","+j+"]-->"+value); return value; } } ); */ if (alpha!=1) z.assign(cern.jet.math.Functions.mult(alpha)); return z; } public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, final double alpha, double beta, final boolean transposeA, boolean transposeB) { if (!(this.isNoView)) { return super.zMult(B,C,alpha,beta,transposeA,transposeB); } if (transposeB) B = B.viewDice(); int m = rows; int n = columns; if (transposeA) { m = columns; n = rows; } int p = B.columns; boolean ignore = (C==null); if (C==null) C = new DenseDoubleMatrix2D(m,p); if (B.rows != n) throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort()); if (C.rows != m || C.columns != p) throw new IllegalArgumentException("Incompatibel result matrix: "+toStringShort()+", "+ (transposeB ? B.viewDice() : B).toStringShort()+", "+C.toStringShort()); if (this == C || B == C) throw new IllegalArgumentException("Matrices must not be identical"); if (!ignore) C.assign(cern.jet.math.Functions.mult(beta)); // cache views final DoubleMatrix1D[] Brows = new DoubleMatrix1D[n]; for (int i=n; --i>=0; ) Brows[i] = B.viewRow(i); final DoubleMatrix1D[] Crows = new DoubleMatrix1D[m]; for (int i=m; --i>=0; ) Crows[i] = C.viewRow(i); final cern.jet.math.PlusMult fun = cern.jet.math.PlusMult.plusMult(0); this.elements.forEachPair( new cern.colt.function.IntDoubleProcedure() { public boolean apply(int key, double value) { int i = key/columns; int j = key%columns; fun.multiplicator = value*alpha; if (!transposeA) Crows[i].assign(Brows[j],fun); else Crows[j].assign(Brows[i],fun); return true; } } ); return C; } }

Ads help maintain this website.