/*
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.matrix.ObjectMatrix1D;
import cern.colt.matrix.ObjectMatrix2D;
/**
Dense 1-d matrix (aka vector) holding Object elements.
First see the package summary and javadoc tree view to get the broad picture.
Implementation:
Internally holds one single contigous one-dimensional array.
Note that this implementation is not synchronized.
Memory requirements:
memory [bytes] = 8*size().
Thus, a 1000000 matrix uses 8 MB.
Time complexity:
O(1) (i.e. constant time) for the basic operations
get, getQuick, set, setQuick and size,
@author wolfgang.hoschek@cern.ch
@version 1.0, 09/24/99
*/
public class DenseObjectMatrix1D extends ObjectMatrix1D {
/**
* The elements of this matrix.
*/
protected Object[] 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 DenseObjectMatrix1D(Object[] 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 DenseObjectMatrix1D(int size) {
setUp(size);
this.elements = new Object[size];
}
/**
* Constructs a matrix view with the given parameters.
* @param size the number of cells the matrix shall have.
* @param elements the cells.
* @param zero 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 DenseObjectMatrix1D(int size, Object[] elements, int zero, int stride) {
setUp(size,zero,stride);
this.elements = elements;
this.isNoView = false;
}
/**
* Sets all cells to the state specified by values.
* values is required to have the same number of cells as the receiver.
*
* 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 cells.
* @return this (for convenience only).
* @throws IllegalArgumentException if values.length != size().
*/
public ObjectMatrix1D assign(Object[] values) {
if (isNoView) {
if (values.length != size) throw new IllegalArgumentException("Must have same number of cells: length="+values.length+"size()="+size());
System.arraycopy(values, 0, this.elements, 0, values.length);
}
else {
super.assign(values);
}
return this;
}
/**
Assigns the result of a function to each cell; x[i] = function(x[i]).
(Iterates downwards from [size()-1] to [0]).
Example:
// change each cell to its sine
matrix = 0.5 1.5 2.5 3.5
matrix.assign(cern.jet.math.Functions.sin);
-->
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 ObjectMatrix1D assign(cern.colt.function.ObjectFunction function) {
int s=stride;
int i=index(0);
Object[] elems = this.elements;
if (elements==null) throw new InternalError();
// the general case x[i] = f(x[i])
for (int k=size; --k >= 0; ) {
elems[i] = function.apply(elems[i]);
i += s;
}
return this;
}
/**
* Replaces all cell values of the receiver with the values of another matrix.
* Both matrices must have the same size.
* 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 size() != other.size().
*/
public ObjectMatrix1D assign(ObjectMatrix1D source) {
// overriden for performance only
if (! (source instanceof DenseObjectMatrix1D)) {
return super.assign(source);
}
DenseObjectMatrix1D other = (DenseObjectMatrix1D) source;
if (other==this) return this;
checkSize(other);
if (isNoView && other.isNoView) { // quickest
System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
return this;
}
if (haveSharedCells(other)) {
ObjectMatrix1D c = other.copy();
if (! (c instanceof DenseObjectMatrix1D)) { // should not happen
return super.assign(source);
}
other = (DenseObjectMatrix1D) c;
}
final Object[] elems = this.elements;
final Object[] otherElems = other.elements;
if (elements==null || otherElems==null) throw new InternalError();
int s = this.stride;
int ys = other.stride;
int index = index(0);
int otherIndex = other.index(0);
for (int k=size; --k >= 0; ) {
elems[index] = otherElems[otherIndex];
index += s;
otherIndex += ys;
}
return this;
}
/**
Assigns the result of a function to each cell; x[i] = function(x[i],y[i]).
(Iterates downwards from [size()-1] to [0]).
Example:
// assign x[i] = x[i]y[i]
m1 = 0 1 2 3;
m2 = 0 2 4 6;
m1.assign(m2, cern.jet.math.Functions.pow);
-->
m1 == 1 1 16 729
// for non-standard functions there is no shortcut:
m1.assign(m2,
new ObjectObjectFunction() {
public Object apply(Object x, Object y) { return Math.pow(x,y); }
}
);
For further examples, see the package doc.
@param y the secondary matrix to operate on.
@param function a function object taking as first argument the current cell's value of this,
and as second argument the current cell's value of y,
@return this (for convenience only).
@throws IllegalArgumentException if size() != y.size().
@see cern.jet.math.Functions
*/
public ObjectMatrix1D assign(ObjectMatrix1D y, cern.colt.function.ObjectObjectFunction function) {
// overriden for performance only
if (! (y instanceof DenseObjectMatrix1D)) {
return super.assign(y,function);
}
DenseObjectMatrix1D other = (DenseObjectMatrix1D) y;
checkSize(y);
final Object[] elems = this.elements;
final Object[] otherElems = other.elements;
if (elements==null || otherElems==null) throw new InternalError();
int s = this.stride;
int ys = other.stride;
int index = index(0);
int otherIndex = other.index(0);
// the general case x[i] = f(x[i],y[i])
for (int k=size; --k >= 0; ) {
elems[index] = function.apply(elems[index], otherElems[otherIndex]);
index += s;
otherIndex += ys;
}
return this;
}
/**
* 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 Object getQuick(int index) {
//if (debug) if (index<0 || index>=size) checkIndex(index);
//return elements[index(index)];
// manually inlined:
return elements[zero + index*stride];
}
/**
* Returns true if both matrices share at least one identical cell.
*/
protected boolean haveSharedCellsRaw(ObjectMatrix1D other) {
if (other instanceof SelectedDenseObjectMatrix1D) {
SelectedDenseObjectMatrix1D otherMatrix = (SelectedDenseObjectMatrix1D) other;
return this.elements==otherMatrix.elements;
}
else if (other instanceof DenseObjectMatrix1D) {
DenseObjectMatrix1D otherMatrix = (DenseObjectMatrix1D) other;
return this.elements==otherMatrix.elements;
}
return false;
}
/**
* 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.
*/
protected int index(int rank) {
// overriden for manual inlining only
//return _offset(_rank(rank));
return zero + rank*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 DenseObjectMatrix1D the new matrix must also be of type DenseObjectMatrix1D,
* if the receiver is an instance of type SparseObjectMatrix1D the new matrix must also be of type SparseObjectMatrix1D, 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 ObjectMatrix1D like(int size) {
return new DenseObjectMatrix1D(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 DenseObjectMatrix1D the new matrix must be of type DenseObjectMatrix2D,
* if the receiver is an instance of type SparseObjectMatrix1D the new matrix must be of type SparseObjectMatrix2D, 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 ObjectMatrix2D like2D(int rows, int columns) {
return new DenseObjectMatrix2D(rows,columns);
}
/**
* 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 void setQuick(int index, Object value) {
//if (debug) if (index<0 || index>=size) checkIndex(index);
//elements[index(index)] = value;
// manually inlined:
elements[zero + index*stride] = value;
}
/**
Swaps each element this[i] with other[i].
@throws IllegalArgumentException if size() != other.size().
*/
public void swap(ObjectMatrix1D other) {
// overriden for performance only
if (! (other instanceof DenseObjectMatrix1D)) {
super.swap(other);
}
DenseObjectMatrix1D y = (DenseObjectMatrix1D) other;
if (y==this) return;
checkSize(y);
final Object[] elems = this.elements;
final Object[] otherElems = y.elements;
if (elements==null || otherElems==null) throw new InternalError();
int s = this.stride;
int ys = y.stride;
int index = index(0);
int otherIndex = y.index(0);
for (int k=size; --k >= 0; ) {
Object tmp = elems[index];
elems[index] = otherElems[otherIndex];
otherElems[otherIndex] = tmp;
index += s;
otherIndex += ys;
}
return;
}
/**
Fills the cell values into the specified 1-dimensional array.
The values are copied. So subsequent changes in values are not reflected in the matrix, and vice-versa.
After this call returns the array values has the form
for (int i=0; i < size(); i++) values[i] = get(i);
@throws IllegalArgumentException if values.length < size().
*/
public void toArray(Object[] values) {
if (values.length < size) throw new IllegalArgumentException("values too small");
if (this.isNoView) System.arraycopy(this.elements,0,values,0,this.elements.length);
else super.toArray(values);
}
/**
* Construct and returns a new selection view.
*
* @param offsets the offsets of the visible elements.
* @return a new view.
*/
protected ObjectMatrix1D viewSelectionLike(int[] offsets) {
return new SelectedDenseObjectMatrix1D(this.elements,offsets);
}
}
Ads help maintain this website.