/*
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.DoubleMatrix1D;
import cern.colt.matrix.DoubleMatrix2D;
/**
Dense 1-d matrix (aka vector) holding double 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 DenseDoubleMatrix1D extends DoubleMatrix1D {
/**
* The elements of this matrix.
*/
protected double[] 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 DenseDoubleMatrix1D(double[] 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 DenseDoubleMatrix1D(int size) {
setUp(size);
this.elements = new double[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 DenseDoubleMatrix1D(int size, double[] 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 DoubleMatrix1D assign(double[] 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;
}
/**
* 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 DoubleMatrix1D assign(double value) {
int index = index(0);
int s = this.stride;
double[] elems = this.elements;
for (int i=size; --i >= 0; ) {
elems[index] = value;
index += s;
}
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 DoubleMatrix1D assign(cern.colt.function.DoubleFunction function) {
int s=stride;
int i=index(0);
double[] elems = this.elements;
if (elems==null) throw new InternalError();
// specialization for speed
if (function instanceof cern.jet.math.Mult) { // x[i] = mult*x[i]
double multiplicator = ((cern.jet.math.Mult)function).multiplicator;
if (multiplicator==1) return this;
for (int k=size; --k >= 0; ) {
elems[i] *= multiplicator;
i += s;
}
}
else { // 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 DoubleMatrix1D assign(DoubleMatrix1D source) {
// overriden for performance only
if (! (source instanceof DenseDoubleMatrix1D)) {
return super.assign(source);
}
DenseDoubleMatrix1D other = (DenseDoubleMatrix1D) 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)) {
DoubleMatrix1D c = other.copy();
if (! (c instanceof DenseDoubleMatrix1D)) { // should not happen
return super.assign(source);
}
other = (DenseDoubleMatrix1D) c;
}
final double[] elems = this.elements;
final double[] 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 DoubleDoubleFunction() {
public double apply(double x, double 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 DoubleMatrix1D assign(DoubleMatrix1D y, cern.colt.function.DoubleDoubleFunction function) {
// overriden for performance only
if (! (y instanceof DenseDoubleMatrix1D)) {
return super.assign(y,function);
}
DenseDoubleMatrix1D other = (DenseDoubleMatrix1D) y;
checkSize(y);
final double[] elems = this.elements;
final double[] otherElems = other.elements;
if (elems==null || otherElems==null) throw new InternalError();
int s = this.stride;
int ys = other.stride;
int index = index(0);
int otherIndex = other.index(0);
// specialized for speed
if (function==cern.jet.math.Functions.mult) { // x[i] = x[i] * y[i]
for (int k=size; --k >= 0; ) {
elems[index] *= otherElems[otherIndex];
index += s;
otherIndex += ys;
}
}
else if (function==cern.jet.math.Functions.div) { // x[i] = x[i] / y[i]
for (int k=size; --k >= 0; ) {
elems[index] /= otherElems[otherIndex];
index += s;
otherIndex += ys;
}
}
else if (function instanceof cern.jet.math.PlusMult) {
double multiplicator = ((cern.jet.math.PlusMult) function).multiplicator;
if (multiplicator == 0) { // x[i] = x[i] + 0*y[i]
return this;
}
else if (multiplicator == 1) { // x[i] = x[i] + y[i]
for (int k=size; --k >= 0; ) {
elems[index] += otherElems[otherIndex];
index += s;
otherIndex += ys;
}
}
else if (multiplicator == -1) { // x[i] = x[i] - y[i]
for (int k=size; --k >= 0; ) {
elems[index] -= otherElems[otherIndex];
index += s;
otherIndex += ys;
}
}
else { // the general case x[i] = x[i] + mult*y[i]
for (int k=size; --k >= 0; ) {
elems[index] += multiplicator*otherElems[otherIndex];
index += s;
otherIndex += ys;
}
}
}
else { // 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 number of cells having non-zero values, but at most maxCardinality; ignores tolerance.
*/
protected int cardinality(int maxCardinality) {
int cardinality = 0;
int index = index(0);
int s = this.stride;
double[] elems = this.elements;
int i=size;
while (--i >= 0 && cardinality < maxCardinality) {
if (elems[index] != 0) cardinality++;
index += s;
}
return cardinality;
}
/**
* 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 double 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(DoubleMatrix1D other) {
if (other instanceof SelectedDenseDoubleMatrix1D) {
SelectedDenseDoubleMatrix1D otherMatrix = (SelectedDenseDoubleMatrix1D) other;
return this.elements==otherMatrix.elements;
}
else if (other instanceof DenseDoubleMatrix1D) {
DenseDoubleMatrix1D otherMatrix = (DenseDoubleMatrix1D) 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 DenseDoubleMatrix1D the new matrix must also be of type DenseDoubleMatrix1D,
* if the receiver is an instance of type SparseDoubleMatrix1D the new matrix must also be of type SparseDoubleMatrix1D, 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 DoubleMatrix1D like(int size) {
return new DenseDoubleMatrix1D(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 DenseDoubleMatrix1D the new matrix must be of type DenseDoubleMatrix2D,
* if the receiver is an instance of type SparseDoubleMatrix1D the new matrix must be of type SparseDoubleMatrix2D, 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 DoubleMatrix2D like2D(int rows, int columns) {
return new DenseDoubleMatrix2D(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, double 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(DoubleMatrix1D other) {
// overriden for performance only
if (! (other instanceof DenseDoubleMatrix1D)) {
super.swap(other);
}
DenseDoubleMatrix1D y = (DenseDoubleMatrix1D) other;
if (y==this) return;
checkSize(y);
final double[] elems = this.elements;
final double[] 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; ) {
double 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(double[] 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 DoubleMatrix1D viewSelectionLike(int[] offsets) {
return new SelectedDenseDoubleMatrix1D(this.elements,offsets);
}
/**
* Returns the dot product of two vectors x and y, which is Sum(x[i]*y[i]).
* Where x == this.
* Operates on cells at indexes from .. Min(size(),y.size(),from+length)-1.
* @param y the second vector.
* @param from the first index to be considered.
* @param length the number of cells to be considered.
* @return the sum of products; zero if from<0 || length<0.
*/
public double zDotProduct(DoubleMatrix1D y, int from, int length) {
if (!(y instanceof DenseDoubleMatrix1D)) {
return super.zDotProduct(y, from, length);
}
DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D) y;
int tail = from + length;
if (from < 0 || length < 0) return 0;
if (size < tail) tail = size;
if (y.size < tail) tail = y.size;
int min = tail-from;
int i = index(from);
int j = yy.index(from);
int s = stride;
int ys = yy.stride;
final double[] elems = this.elements;
final double[] yElems = yy.elements;
if (elems==null || yElems==null) throw new InternalError();
double sum = 0;
/*
// unoptimized
for (int k = min; --k >= 0;) {
sum += elems[i] * yElems[j];
i += s;
j += ys;
}
*/
// optimized
// loop unrolling
i -= s;
j -= ys;
for (int k=min/4; --k >= 0; ) {
sum += elems[i += s] * yElems[j += ys] +
elems[i += s] * yElems[j += ys] +
elems[i += s] * yElems[j += ys] +
elems[i += s] * yElems[j += ys];
}
for (int k=min%4; --k >= 0; ) {
sum += elems[i += s] * yElems[j += ys];
}
return sum;
}
/**
* Returns the sum of all cells; Sum( x[i] ).
* @return the sum.
*/
public double zSum() {
double sum = 0;
int s=stride;
int i=index(0);
final double[] elems = this.elements;
if (elems==null) throw new InternalError();
for (int k=size; --k >= 0; ) {
sum += elems[i];
i += s;
}
return sum;
}
}
Ads help maintain this website.