All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cern.colt.matrix.tlong.impl.SparseLongMatrix1D Maven / Gradle / Ivy

Go to download

Parallel Colt is a multithreaded version of Colt - a library for high performance scientific computing in Java. It contains efficient algorithms for data analysis, linear algebra, multi-dimensional arrays, Fourier transforms, statistics and histogramming.

The newest version!
/*
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.tlong.impl;

import cern.colt.map.tlong.AbstractLongLongMap;
import cern.colt.map.tlong.OpenLongLongHashMap;
import cern.colt.matrix.tlong.LongMatrix1D;
import cern.colt.matrix.tlong.LongMatrix2D;
import cern.colt.matrix.tlong.LongMatrix3D;

/**
 * Sparse hashed 1-d matrix (aka vector) holding long 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.tlong.OpenIntLongHashMap}, which is a compact and * performant hashing technique. *

* Memory requirements: *

* Cells that *

    *
  • are never set to non-zero values do not use any memory. *
  • switch from zero to non-zero state do use memory. *
  • switch back from non-zero to zero state also do use memory. However, * their memory is automatically reclaimed from time to time. It can also * manually be reclaimed by calling {@link #trimToSize()}. *
*

* 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 1000000 matrix with minLoadFactor=0.25 and maxLoadFactor=0.5 and * 1000000 non-zero cells consumes between 25 MB and 50 MB. The same 1000000 * 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 DenseLongMatrix1D}. However, constant factors are considerably larger. * * @author [email protected] * @version 1.0, 09/24/99 * * @author Piotr Wendykier ([email protected]) * @version 1.1, 08/22/2007 */ public class SparseLongMatrix1D extends LongMatrix1D { /** * */ private static final long serialVersionUID = 1L; /* * The elements of the matrix. */ protected AbstractLongLongMap elements; /** * Constructs a matrix with a copy of the given values. 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. */ public SparseLongMatrix1D(long[] values) { this(values.length); assign(values); } /** * Constructs a matrix with a given number of cells. All entries are * initially 0. * * @param size * the number of cells the matrix shall have. * @throws IllegalArgumentException * if size<0. */ public SparseLongMatrix1D(int size) { this(size, size / 1000, 0.2, 0.5); } /** * Constructs a matrix with a given number of parameters. All entries are * initially 0. For details related to memory usage see * {@link cern.colt.map.tlong.OpenIntLongHashMap}. * * @param size * the number of cells 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 size<0. */ public SparseLongMatrix1D(int size, int initialCapacity, double minLoadFactor, double maxLoadFactor) { setUp(size); this.elements = new OpenLongLongHashMap(initialCapacity, minLoadFactor, maxLoadFactor); } /** * Constructs a matrix view with a given number of parameters. * * @param size * the number of cells the matrix shall have. * @param elements * the cells. * @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). * @throws IllegalArgumentException * if size<0. */ protected SparseLongMatrix1D(int size, AbstractLongLongMap elements, int offset, int stride) { setUp(size, offset, stride); 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 LongMatrix1D assign(long value) { // overriden for performance only if (this.isNoView && value == 0) this.elements.clear(); else super.assign(value); return this; } /** * Returns the number of cells having non-zero values. */ public int cardinality() { if (this.isNoView) return this.elements.size(); else return super.cardinality(); } /** * Returns the elements of this matrix. * * @return the elements */ public AbstractLongLongMap elements() { return elements; } /** * 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 minCapacity * the desired minimum number of non-zero cells. */ public void ensureCapacity(int minCapacity) { this.elements.ensureCapacity(minCapacity); } /** * Returns the matrix cell value at coordinate index. * *

* 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): index<0 || index>=size(). * * @param index * the index of the cell. * @return the value of the specified cell. */ public synchronized long getQuick(int index) { // if (debug) if (index<0 || index>=size) checkIndex(index); // return this.elements.get(index(index)); // manually inlined: return elements.get((long) zero + (long) index * (long) stride); } /** * Returns the position of the element with the given relative rank within * the (virtual or non-virtual) internal 1-dimensional array. You may want * to override this method for performance. * * @param rank * the rank of the element. */ public long index(int rank) { // overriden for manual inlining only // return _offset(_rank(rank)); return (long) zero + (long) rank * (long) stride; } /** * Construct and returns a new empty matrix of the same dynamic type * as the receiver, having the specified size. For example, if the receiver * is an instance of type DenseLongMatrix1D the new matrix must * also be of type DenseLongMatrix1D, if the receiver is an * instance of type SparseLongMatrix1D the new matrix must also be * of type SparseLongMatrix1D, etc. In general, the new matrix * should have internal parametrization as similar as possible. * * @param size * the number of cell the matrix shall have. * @return a new empty matrix of the same dynamic type. */ public LongMatrix1D like(int size) { return new SparseLongMatrix1D(size); } /** * Construct and returns a new 2-d matrix of the corresponding dynamic * type, entirelly independent of the receiver. For example, if the * receiver is an instance of type DenseLongMatrix1D the new matrix * must be of type DenseLongMatrix2D, if the receiver is an * instance of type SparseLongMatrix1D the new matrix must be of * type SparseLongMatrix2D, etc. * * @param rows * the number of rows the matrix shall have. * @param columns * the number of columns the matrix shall have. * @return a new matrix of the corresponding dynamic type. */ public LongMatrix2D like2D(int rows, int columns) { return new SparseLongMatrix2D(rows, columns); } public LongMatrix2D reshape(int rows, int columns) { if (rows * columns != size) { throw new IllegalArgumentException("rows*columns != size"); } LongMatrix2D M = new SparseLongMatrix2D(rows, columns); int idx = 0; for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { long elem = getQuick(idx++); if (elem != 0) { M.setQuick(r, c, elem); } } } return M; } public LongMatrix3D reshape(int slices, int rows, int columns) { if (slices * rows * columns != size) { throw new IllegalArgumentException("slices*rows*columns != size"); } LongMatrix3D M = new SparseLongMatrix3D(slices, rows, columns); int idx = 0; for (int s = 0; s < slices; s++) { for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { long elem = getQuick(idx++); if (elem != 0) { M.setQuick(s, r, c, elem); } } } } return M; } /** * Sets the matrix cell at coordinate index 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): index<0 || index>=size(). * * @param index * the index of the cell. * @param value * the value to be filled into the specified cell. */ public synchronized void setQuick(int index, long value) { // if (debug) if (index<0 || index>=size) checkIndex(index); // int i = index(index); // manually inlined: long i = (long) zero + (long) index * (long) stride; if (value == 0) this.elements.removeKey(i); else this.elements.put(i, value); } public String toString() { StringBuilder builder = new StringBuilder(); builder.append("1 x ").append(size).append(" sparse matrix, nnz = ").append(cardinality()).append('\n'); for (int i = 0; i < size; i++) { long elem = getQuick(i); if (elem != 0) { builder.append('(').append(i).append(')').append('\t').append(elem).append('\n'); } } return builder.toString(); } public void trimToSize() { this.elements.trimToSize(); } /** * Returns true if both matrices share at least one identical cell. */ protected boolean haveSharedCellsRaw(LongMatrix1D other) { if (other instanceof SelectedSparseLongMatrix1D) { SelectedSparseLongMatrix1D otherMatrix = (SelectedSparseLongMatrix1D) other; return this.elements == otherMatrix.elements; } else if (other instanceof SparseLongMatrix1D) { SparseLongMatrix1D otherMatrix = (SparseLongMatrix1D) other; return this.elements == otherMatrix.elements; } return false; } /** * Construct and returns a new selection view. * * @param offsets * the offsets of the visible elements. * @return a new view. */ protected LongMatrix1D viewSelectionLike(int[] offsets) { return new SelectedSparseLongMatrix1D(this.elements, offsets); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy