You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2009/11/25 04:41:31 UTC
svn commit: r883974 [10/20] - in
/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix:
./ bench/ doublealgo/ impl/ linalg/ objectalgo/
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix1D.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix1D.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix1D.java Wed Nov 25 03:41:28 2009
@@ -37,10 +37,10 @@
*/
@Deprecated
public class DenseDoubleMatrix1D extends DoubleMatrix1D {
- /**
- * The elements of this matrix.
- */
- protected double[] elements;
+ /**
+ * 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 <tt>values</tt> are not reflected in the matrix, and vice-versa.
@@ -48,8 +48,8 @@
* @param values The values to be filled into the new matrix.
*/
public DenseDoubleMatrix1D(double[] values) {
- this(values.length);
- assign(values);
+ this(values.length);
+ assign(values);
}
/**
* Constructs a matrix with a given number of cells.
@@ -58,8 +58,8 @@
* @throws IllegalArgumentException if <tt>size<0</tt>.
*/
public DenseDoubleMatrix1D(int size) {
- setUp(size);
- this.elements = new double[size];
+ setUp(size);
+ this.elements = new double[size];
}
/**
* Constructs a matrix view with the given parameters.
@@ -70,9 +70,9 @@
* @throws IllegalArgumentException if <tt>size<0</tt>.
*/
protected DenseDoubleMatrix1D(int size, double[] elements, int zero, int stride) {
- setUp(size,zero,stride);
- this.elements = elements;
- this.isNoView = false;
+ setUp(size,zero,stride);
+ this.elements = elements;
+ this.isNoView = false;
}
/**
* Sets all cells to the state specified by <tt>values</tt>.
@@ -85,14 +85,14 @@
* @throws IllegalArgumentException if <tt>values.length != size()</tt>.
*/
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;
+ 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 <tt>value</tt>.
@@ -100,14 +100,14 @@
* @return <tt>this</tt> (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;
+ 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; <tt>x[i] = function(x[i])</tt>.
@@ -128,27 +128,27 @@
@see org.apache.mahout.jet.math.Functions
*/
public DoubleMatrix1D assign(org.apache.mahout.matrix.function.DoubleFunction function) {
- int s=stride;
- int i=index(0);
- double[] elems = this.elements;
- if (elems==null) throw new InternalError();
+ int s=stride;
+ int i=index(0);
+ double[] elems = this.elements;
+ if (elems==null) throw new InternalError();
- // specialization for speed
- if (function instanceof org.apache.mahout.jet.math.Mult) { // x[i] = mult*x[i]
- double multiplicator = ((org.apache.mahout.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;
+ // specialization for speed
+ if (function instanceof org.apache.mahout.jet.math.Mult) { // x[i] = mult*x[i]
+ double multiplicator = ((org.apache.mahout.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.
@@ -157,42 +157,42 @@
*
* @param source the source matrix to copy from (may be identical to the receiver).
* @return <tt>this</tt> (for convenience only).
- * @throws IllegalArgumentException if <tt>size() != other.size()</tt>.
+ * @throws IllegalArgumentException if <tt>size() != other.size()</tt>.
*/
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;
- }
+ // 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;
+ 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;
+ 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; <tt>x[i] = function(x[i],y[i])</tt>.
@@ -220,90 +220,90 @@
@param function a function object taking as first argument the current cell's value of <tt>this</tt>,
and as second argument the current cell's value of <tt>y</tt>,
@return <tt>this</tt> (for convenience only).
-@throws IllegalArgumentException if <tt>size() != y.size()</tt>.
+@throws IllegalArgumentException if <tt>size() != y.size()</tt>.
@see org.apache.mahout.jet.math.Functions
*/
public DoubleMatrix1D assign(DoubleMatrix1D y, org.apache.mahout.matrix.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;
+ // 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);
+ int index = index(0);
+ int otherIndex = other.index(0);
- // specialized for speed
- if (function== org.apache.mahout.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== org.apache.mahout.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 org.apache.mahout.jet.math.PlusMult) {
- double multiplicator = ((org.apache.mahout.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;
+ // specialized for speed
+ if (function== org.apache.mahout.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== org.apache.mahout.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 org.apache.mahout.jet.math.PlusMult) {
+ double multiplicator = ((org.apache.mahout.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;
+ 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 <tt>index</tt>.
@@ -316,24 +316,24 @@
* @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];
+ //if (debug) if (index<0 || index>=size) checkIndex(index);
+ //return elements[index(index)];
+ // manually inlined:
+ return elements[zero + index*stride];
}
/**
* Returns <tt>true</tt> 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;
+ 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.
@@ -342,9 +342,9 @@
* @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;
+ // overriden for manual inlining only
+ //return _offset(_rank(rank));
+ return zero + rank*stride;
}
/**
* Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified size.
@@ -356,7 +356,7 @@
* @return a new empty matrix of the same dynamic type.
*/
public DoubleMatrix1D like(int size) {
- return new DenseDoubleMatrix1D(size);
+ return new DenseDoubleMatrix1D(size);
}
/**
* Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the receiver.
@@ -368,7 +368,7 @@
* @return a new matrix of the corresponding dynamic type.
*/
public DoubleMatrix2D like2D(int rows, int columns) {
- return new DenseDoubleMatrix2D(rows,columns);
+ return new DenseDoubleMatrix2D(rows,columns);
}
/**
* Sets the matrix cell at coordinate <tt>index</tt> to the specified value.
@@ -381,40 +381,40 @@
* @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;
+ //if (debug) if (index<0 || index>=size) checkIndex(index);
+ //elements[index(index)] = value;
+ // manually inlined:
+ elements[zero + index*stride] = value;
}
/**
Swaps each element <tt>this[i]</tt> with <tt>other[i]</tt>.
@throws IllegalArgumentException if <tt>size() != other.size()</tt>.
*/
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;
+ // 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;
+ 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.
@@ -426,9 +426,9 @@
@throws IllegalArgumentException if <tt>values.length < size()</tt>.
*/
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);
+ 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.
@@ -437,7 +437,7 @@
* @return a new view.
*/
protected DoubleMatrix1D viewSelectionLike(int[] offsets) {
- return new SelectedDenseDoubleMatrix1D(this.elements,offsets);
+ return new SelectedDenseDoubleMatrix1D(this.elements,offsets);
}
/**
* Returns the dot product of two vectors x and y, which is <tt>Sum(x[i]*y[i])</tt>.
@@ -449,64 +449,64 @@
* @return the sum of products; zero if <tt>from<0 || length<0</tt>.
*/
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;
+ 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; <tt>Sum( x[i] )</tt>.
* @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;
+ 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;
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix2D.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix2D.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix2D.java Wed Nov 25 03:41:28 2009
@@ -35,17 +35,17 @@
Thus
<pre>
for (int row=0; row < rows; row++) {
- for (int column=0; column < columns; column++) {
- matrix.setQuick(row,column,someValue);
- }
+ for (int column=0; column < columns; column++) {
+ matrix.setQuick(row,column,someValue);
+ }
}
</pre>
is quicker than
<pre>
for (int column=0; column < columns; column++) {
- for (int row=0; row < rows; row++) {
- matrix.setQuick(row,column,someValue);
- }
+ for (int row=0; row < rows; row++) {
+ matrix.setQuick(row,column,someValue);
+ }
}
</pre>
@author wolfgang.hoschek@cern.ch
@@ -56,16 +56,16 @@
*/
@Deprecated
public class DenseDoubleMatrix2D extends DoubleMatrix2D {
- static final long serialVersionUID = 1020177651L;
- /**
- * The elements of this matrix.
- * elements are stored in row major, i.e.
- * index==row*columns + column
- * columnOf(index)==index%columns
- * rowOf(index)==index/columns
- * i.e. {row0 column0..m}, {row1 column0..m}, ..., {rown column0..m}
- */
- protected double[] elements;
+ static final long serialVersionUID = 1020177651L;
+ /**
+ * The elements of this matrix.
+ * elements are stored in row major, i.e.
+ * index==row*columns + column
+ * columnOf(index)==index%columns
+ * rowOf(index)==index/columns
+ * i.e. {row0 column0..m}, {row1 column0..m}, ..., {rown column0..m}
+ */
+ protected double[] elements;
/**
* Constructs a matrix with a copy of the given values.
* <tt>values</tt> is required to have the form <tt>values[row][column]</tt>
@@ -77,19 +77,19 @@
* @throws IllegalArgumentException if <tt>for any 1 <= row < values.length: values[row].length != values[row-1].length</tt>.
*/
public DenseDoubleMatrix2D(double[][] values) {
- this(values.length, values.length==0 ? 0: values[0].length);
- assign(values);
+ this(values.length, values.length==0 ? 0: values[0].length);
+ assign(values);
}
/**
* Constructs a matrix with a given number of rows and columns.
* All entries are initially <tt>0</tt>.
* @param rows the number of rows the matrix shall have.
* @param columns the number of columns the matrix shall have.
- * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt>.
+ * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt>.
*/
public DenseDoubleMatrix2D(int rows, int columns) {
- setUp(rows, columns);
- this.elements = new double[rows*columns];
+ setUp(rows, columns);
+ this.elements = new double[rows*columns];
}
/**
* Constructs a view with the given parameters.
@@ -100,12 +100,12 @@
* @param columnZero the position of the first element.
* @param rowStride the number of elements between two rows, i.e. <tt>index(i+1,j)-index(i,j)</tt>.
* @param columnStride the number of elements between two columns, i.e. <tt>index(i,j+1)-index(i,j)</tt>.
- * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt> or flip's are illegal.
+ * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt> or flip's are illegal.
*/
protected DenseDoubleMatrix2D(int rows, int columns, double[] elements, int rowZero, int columnZero, int rowStride, int columnStride) {
- setUp(rows,columns,rowZero,columnZero,rowStride,columnStride);
- this.elements = elements;
- this.isNoView = false;
+ setUp(rows,columns,rowZero,columnZero,rowStride,columnStride);
+ this.elements = elements;
+ this.isNoView = false;
}
/**
* Sets all cells to the state specified by <tt>values</tt>.
@@ -119,20 +119,20 @@
* @throws IllegalArgumentException if <tt>values.length != rows() || for any 0 <= row < rows(): values[row].length != columns()</tt>.
*/
public DoubleMatrix2D assign(double[][] values) {
- if (this.isNoView) {
- if (values.length != rows) throw new IllegalArgumentException("Must have same number of rows: rows="+values.length+"rows()="+rows());
- int i = columns*(rows-1);
- for (int row=rows; --row >= 0;) {
- double[] currentRow = values[row];
- if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns());
- System.arraycopy(currentRow, 0, this.elements, i, columns);
- i -= columns;
- }
- }
- else {
- super.assign(values);
- }
- return this;
+ if (this.isNoView) {
+ if (values.length != rows) throw new IllegalArgumentException("Must have same number of rows: rows="+values.length+"rows()="+rows());
+ int i = columns*(rows-1);
+ for (int row=rows; --row >= 0;) {
+ double[] currentRow = values[row];
+ if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns());
+ System.arraycopy(currentRow, 0, this.elements, i, columns);
+ i -= columns;
+ }
+ }
+ else {
+ super.assign(values);
+ }
+ return this;
}
/**
* Sets all cells to the state specified by <tt>value</tt>.
@@ -140,18 +140,18 @@
* @return <tt>this</tt> (for convenience only).
*/
public DoubleMatrix2D assign(double value) {
- final double[] elems = this.elements;
- int index = index(0,0);
- int cs = this.columnStride;
- int rs = this.rowStride;
- for (int row=rows; --row >= 0; ) {
- for (int i=index, column=columns; --column >= 0; ) {
- elems[i] = value;
- i += cs;
- }
- index += rs;
- }
- return this;
+ final double[] elems = this.elements;
+ int index = index(0,0);
+ int cs = this.columnStride;
+ int rs = this.rowStride;
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, column=columns; --column >= 0; ) {
+ elems[i] = value;
+ i += cs;
+ }
+ index += rs;
+ }
+ return this;
}
/**
Assigns the result of a function to each cell; <tt>x[row,col] = function(x[row,col])</tt>.
@@ -176,35 +176,35 @@
@see org.apache.mahout.jet.math.Functions
*/
public DoubleMatrix2D assign(org.apache.mahout.matrix.function.DoubleFunction function) {
- final double[] elems = this.elements;
- if (elems==null) throw new InternalError();
- int index = index(0,0);
- int cs = this.columnStride;
- int rs = this.rowStride;
-
- // specialization for speed
- if (function instanceof org.apache.mahout.jet.math.Mult) { // x[i] = mult*x[i]
- double multiplicator = ((org.apache.mahout.jet.math.Mult)function).multiplicator;
- if (multiplicator==1) return this;
- if (multiplicator==0) return assign(0);
- for (int row=rows; --row >= 0; ) { // the general case
- for (int i=index, column=columns; --column >= 0; ) {
- elems[i] *= multiplicator;
- i += cs;
- }
- index += rs;
- }
- }
- else { // the general case x[i] = f(x[i])
- for (int row=rows; --row >= 0; ) {
- for (int i=index, column=columns; --column >= 0; ) {
- elems[i] = function.apply(elems[i]);
- i += cs;
- }
- index += rs;
- }
- }
- return this;
+ final double[] elems = this.elements;
+ if (elems==null) throw new InternalError();
+ int index = index(0,0);
+ int cs = this.columnStride;
+ int rs = this.rowStride;
+
+ // specialization for speed
+ if (function instanceof org.apache.mahout.jet.math.Mult) { // x[i] = mult*x[i]
+ double multiplicator = ((org.apache.mahout.jet.math.Mult)function).multiplicator;
+ if (multiplicator==1) return this;
+ if (multiplicator==0) return assign(0);
+ for (int row=rows; --row >= 0; ) { // the general case
+ for (int i=index, column=columns; --column >= 0; ) {
+ elems[i] *= multiplicator;
+ i += cs;
+ }
+ index += rs;
+ }
+ }
+ else { // the general case x[i] = f(x[i])
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, column=columns; --column >= 0; ) {
+ elems[i] = function.apply(elems[i]);
+ i += cs;
+ }
+ index += rs;
+ }
+ }
+ return this;
}
/**
* Replaces all cell values of the receiver with the values of another matrix.
@@ -213,50 +213,50 @@
*
* @param source the source matrix to copy from (may be identical to the receiver).
* @return <tt>this</tt> (for convenience only).
- * @throws IllegalArgumentException if <tt>columns() != source.columns() || rows() != source.rows()</tt>
+ * @throws IllegalArgumentException if <tt>columns() != source.columns() || rows() != source.rows()</tt>
*/
public DoubleMatrix2D assign(DoubleMatrix2D source) {
- // overriden for performance only
- if (! (source instanceof DenseDoubleMatrix2D)) {
- return super.assign(source);
- }
- DenseDoubleMatrix2D other = (DenseDoubleMatrix2D) source;
- if (other==this) return this; // nothing to do
- checkShape(other);
-
- if (this.isNoView && other.isNoView) { // quickest
- System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
- return this;
- }
-
- if (haveSharedCells(other)) {
- DoubleMatrix2D c = other.copy();
- if (! (c instanceof DenseDoubleMatrix2D)) { // should not happen
- return super.assign(other);
- }
- other = (DenseDoubleMatrix2D) c;
- }
-
- final double[] elems = this.elements;
- final double[] otherElems = other.elements;
- if (elems==null || otherElems==null) throw new InternalError();
- int cs = this.columnStride;
- int ocs = other.columnStride;
- int rs = this.rowStride;
- int ors = other.rowStride;
-
- int otherIndex = other.index(0,0);
- int index = index(0,0);
- for (int row=rows; --row >= 0; ) {
- for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
- elems[i] = otherElems[j];
- i += cs;
- j += ocs;
- }
- index += rs;
- otherIndex += ors;
- }
- return this;
+ // overriden for performance only
+ if (! (source instanceof DenseDoubleMatrix2D)) {
+ return super.assign(source);
+ }
+ DenseDoubleMatrix2D other = (DenseDoubleMatrix2D) source;
+ if (other==this) return this; // nothing to do
+ checkShape(other);
+
+ if (this.isNoView && other.isNoView) { // quickest
+ System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
+ return this;
+ }
+
+ if (haveSharedCells(other)) {
+ DoubleMatrix2D c = other.copy();
+ if (! (c instanceof DenseDoubleMatrix2D)) { // should not happen
+ return super.assign(other);
+ }
+ other = (DenseDoubleMatrix2D) c;
+ }
+
+ final double[] elems = this.elements;
+ final double[] otherElems = other.elements;
+ if (elems==null || otherElems==null) throw new InternalError();
+ int cs = this.columnStride;
+ int ocs = other.columnStride;
+ int rs = this.rowStride;
+ int ors = other.rowStride;
+
+ int otherIndex = other.index(0,0);
+ int index = index(0,0);
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
+ elems[i] = otherElems[j];
+ i += cs;
+ j += ocs;
+ }
+ index += rs;
+ otherIndex += ors;
+ }
+ return this;
}
/**
Assigns the result of a function to each cell; <tt>x[row,col] = function(x[row,col],y[row,col])</tt>.
@@ -284,102 +284,102 @@
@param function a function object taking as first argument the current cell's value of <tt>this</tt>,
and as second argument the current cell's value of <tt>y</tt>,
@return <tt>this</tt> (for convenience only).
-@throws IllegalArgumentException if <tt>columns() != other.columns() || rows() != other.rows()</tt>
+@throws IllegalArgumentException if <tt>columns() != other.columns() || rows() != other.rows()</tt>
@see org.apache.mahout.jet.math.Functions
*/
public DoubleMatrix2D assign(DoubleMatrix2D y, org.apache.mahout.matrix.function.DoubleDoubleFunction function) {
- // overriden for performance only
- if (! (y instanceof DenseDoubleMatrix2D)) {
- return super.assign(y, function);
- }
- DenseDoubleMatrix2D other = (DenseDoubleMatrix2D) y;
- checkShape(y);
-
- final double[] elems = this.elements;
- final double[] otherElems = other.elements;
- if (elems==null || otherElems==null) throw new InternalError();
- int cs = this.columnStride;
- int ocs = other.columnStride;
- int rs = this.rowStride;
- int ors = other.rowStride;
-
- int otherIndex = other.index(0,0);
- int index = index(0,0);
-
- // specialized for speed
- if (function== org.apache.mahout.jet.math.Functions.mult) { // x[i] = x[i] * y[i]
- for (int row=rows; --row >= 0; ) {
- for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
- elems[i] *= otherElems[j];
- i += cs;
- j += ocs;
- }
- index += rs;
- otherIndex += ors;
- }
- }
- else if (function== org.apache.mahout.jet.math.Functions.div) { // x[i] = x[i] / y[i]
- for (int row=rows; --row >= 0; ) {
- for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
- elems[i] /= otherElems[j];
- i += cs;
- j += ocs;
- }
- index += rs;
- otherIndex += ors;
- }
- }
- else if (function instanceof org.apache.mahout.jet.math.PlusMult) {
- double multiplicator = ((org.apache.mahout.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 row=rows; --row >= 0; ) {
- for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
- elems[i] += otherElems[j];
- i += cs;
- j += ocs;
- }
- index += rs;
- otherIndex += ors;
- }
- }
- else if (multiplicator == -1) { // x[i] = x[i] - y[i]
- for (int row=rows; --row >= 0; ) {
- for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
- elems[i] -= otherElems[j];
- i += cs;
- j += ocs;
- }
- index += rs;
- otherIndex += ors;
- }
- }
- else { // the general case
- for (int row=rows; --row >= 0; ) { // x[i] = x[i] + mult*y[i]
- for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
- elems[i] += multiplicator*otherElems[j];
- i += cs;
- j += ocs;
- }
- index += rs;
- otherIndex += ors;
- }
- }
- }
- else { // the general case x[i] = f(x[i],y[i])
- for (int row=rows; --row >= 0; ) {
- for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
- elems[i] = function.apply(elems[i], otherElems[j]);
- i += cs;
- j += ocs;
- }
- index += rs;
- otherIndex += ors;
- }
- }
- return this;
+ // overriden for performance only
+ if (! (y instanceof DenseDoubleMatrix2D)) {
+ return super.assign(y, function);
+ }
+ DenseDoubleMatrix2D other = (DenseDoubleMatrix2D) y;
+ checkShape(y);
+
+ final double[] elems = this.elements;
+ final double[] otherElems = other.elements;
+ if (elems==null || otherElems==null) throw new InternalError();
+ int cs = this.columnStride;
+ int ocs = other.columnStride;
+ int rs = this.rowStride;
+ int ors = other.rowStride;
+
+ int otherIndex = other.index(0,0);
+ int index = index(0,0);
+
+ // specialized for speed
+ if (function== org.apache.mahout.jet.math.Functions.mult) { // x[i] = x[i] * y[i]
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
+ elems[i] *= otherElems[j];
+ i += cs;
+ j += ocs;
+ }
+ index += rs;
+ otherIndex += ors;
+ }
+ }
+ else if (function== org.apache.mahout.jet.math.Functions.div) { // x[i] = x[i] / y[i]
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
+ elems[i] /= otherElems[j];
+ i += cs;
+ j += ocs;
+ }
+ index += rs;
+ otherIndex += ors;
+ }
+ }
+ else if (function instanceof org.apache.mahout.jet.math.PlusMult) {
+ double multiplicator = ((org.apache.mahout.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 row=rows; --row >= 0; ) {
+ for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
+ elems[i] += otherElems[j];
+ i += cs;
+ j += ocs;
+ }
+ index += rs;
+ otherIndex += ors;
+ }
+ }
+ else if (multiplicator == -1) { // x[i] = x[i] - y[i]
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
+ elems[i] -= otherElems[j];
+ i += cs;
+ j += ocs;
+ }
+ index += rs;
+ otherIndex += ors;
+ }
+ }
+ else { // the general case
+ for (int row=rows; --row >= 0; ) { // x[i] = x[i] + mult*y[i]
+ for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
+ elems[i] += multiplicator*otherElems[j];
+ i += cs;
+ j += ocs;
+ }
+ index += rs;
+ otherIndex += ors;
+ }
+ }
+ }
+ else { // the general case x[i] = f(x[i],y[i])
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, j=otherIndex, column=columns; --column >= 0; ) {
+ elems[i] = function.apply(elems[i], otherElems[j]);
+ i += cs;
+ j += ocs;
+ }
+ index += rs;
+ otherIndex += ors;
+ }
+ }
+ return this;
}
/**
* Returns the matrix cell value at coordinate <tt>[row,column]</tt>.
@@ -393,10 +393,10 @@
* @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 elements[index(row,column)];
- //manually inlined:
- return elements[rowZero + row*rowStride + columnZero + column*columnStride];
+ //if (debug) if (column<0 || column>=columns || row<0 || row>=rows) throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
+ //return elements[index(row,column)];
+ //manually inlined:
+ return elements[rowZero + row*rowStride + columnZero + column*columnStride];
}
/**
* Returns <tt>true</tt> if both matrices share common cells.
@@ -408,15 +408,15 @@
* </ul>
*/
protected boolean haveSharedCellsRaw(DoubleMatrix2D other) {
- if (other instanceof SelectedDenseDoubleMatrix2D) {
- SelectedDenseDoubleMatrix2D otherMatrix = (SelectedDenseDoubleMatrix2D) other;
- return this.elements==otherMatrix.elements;
- }
- else if (other instanceof DenseDoubleMatrix2D) {
- DenseDoubleMatrix2D otherMatrix = (DenseDoubleMatrix2D) other;
- return this.elements==otherMatrix.elements;
- }
- return false;
+ if (other instanceof SelectedDenseDoubleMatrix2D) {
+ SelectedDenseDoubleMatrix2D otherMatrix = (SelectedDenseDoubleMatrix2D) other;
+ return this.elements==otherMatrix.elements;
+ }
+ else if (other instanceof DenseDoubleMatrix2D) {
+ DenseDoubleMatrix2D otherMatrix = (DenseDoubleMatrix2D) 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.
@@ -425,9 +425,9 @@
* @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;
+ // return super.index(row,column);
+ // manually inlined for speed:
+ return rowZero + row*rowStride + columnZero + column*columnStride;
}
/**
* Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified number of rows and columns.
@@ -440,7 +440,7 @@
* @return a new empty matrix of the same dynamic type.
*/
public DoubleMatrix2D like(int rows, int columns) {
- return new DenseDoubleMatrix2D(rows, columns);
+ return new DenseDoubleMatrix2D(rows, columns);
}
/**
* Construct and returns a new 1-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the receiver.
@@ -451,7 +451,7 @@
* @return a new matrix of the corresponding dynamic type.
*/
public DoubleMatrix1D like1D(int size) {
- return new DenseDoubleMatrix1D(size);
+ return new DenseDoubleMatrix1D(size);
}
/**
* Construct and returns a new 1-d matrix <i>of the corresponding dynamic type</i>, sharing the same cells.
@@ -464,7 +464,7 @@
* @return a new matrix of the corresponding dynamic type.
*/
protected DoubleMatrix1D like1D(int size, int zero, int stride) {
- return new DenseDoubleMatrix1D(size,this.elements,zero,stride);
+ return new DenseDoubleMatrix1D(size,this.elements,zero,stride);
}
/**
* Sets the matrix cell at coordinate <tt>[row,column]</tt> to the specified value.
@@ -478,10 +478,10 @@
* @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);
- //elements[index(row,column)] = value;
- //manually inlined:
- elements[rowZero + row*rowStride + columnZero + column*columnStride] = value;
+ //if (debug) if (column<0 || column>=columns || row<0 || row>=rows) throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
+ //elements[index(row,column)] = value;
+ //manually inlined:
+ elements[rowZero + row*rowStride + columnZero + column*columnStride] = value;
}
/**
* Construct and returns a new selection view.
@@ -491,7 +491,7 @@
* @return a new view.
*/
protected DoubleMatrix2D viewSelectionLike(int[] rowOffsets, int[] columnOffsets) {
- return new SelectedDenseDoubleMatrix2D(this.elements,rowOffsets,columnOffsets,0);
+ return new SelectedDenseDoubleMatrix2D(this.elements,rowOffsets,columnOffsets,0);
}
/**
8 neighbor stencil transformation. For efficient finite difference operations.
@@ -541,277 +541,277 @@
C.zAssign8Neighbors(B,g); // fast, even though it doesn't look like it
};
</pre>
-
+
@param B the matrix to hold the results.
@param function the function to be applied to the 9 cells.
@throws NullPointerException if <tt>function==null</tt>.
@throws IllegalArgumentException if <tt>rows() != B.rows() || columns() != B.columns()</tt>.
*/
public void zAssign8Neighbors(DoubleMatrix2D B, org.apache.mahout.matrix.function.Double9Function function) {
- // 1. using only 4-5 out of the 9 cells in "function" is *not* the limiting factor for performance.
+ // 1. using only 4-5 out of the 9 cells in "function" is *not* the limiting factor for performance.
- // 2. if the "function" would be hardwired into the innermost loop, a speedup of 1.5-2.0 would be seen
- // but then the multi-purpose interface is gone...
+ // 2. if the "function" would be hardwired into the innermost loop, a speedup of 1.5-2.0 would be seen
+ // but then the multi-purpose interface is gone...
- if (!(B instanceof DenseDoubleMatrix2D)) {
- super.zAssign8Neighbors(B, function);
- return;
- }
- if (function==null) throw new NullPointerException("function must not be null.");
- checkShape(B);
- int r = rows-1;
- int c = columns-1;
- if (rows<3 || columns<3) return; // nothing to do
-
- DenseDoubleMatrix2D BB = (DenseDoubleMatrix2D) B;
- int A_rs = rowStride;
- int B_rs = BB.rowStride;
- int A_cs = columnStride;
- int B_cs = BB.columnStride;
- double[] elems = this.elements;
- double[] B_elems = BB.elements;
- if (elems == null || B_elems==null) throw new InternalError();
-
- int A_index = index(1,1);
- int B_index = BB.index(1,1);
- for (int i=1; i<r; i++) {
- double a00, a01, a02;
- double a10, a11, a12;
- double a20, a21, a22;
-
- int B11 = B_index;
-
- int A02 = A_index - A_rs - A_cs;
- int A12 = A02 + A_rs;
- int A22 = A12 + A_rs;
-
- // in each step six cells can be remembered in registers - they don't need to be reread from slow memory
- a00=elems[A02]; A02+=A_cs; a01=elems[A02]; //A02+=A_cs;
- a10=elems[A12]; A12+=A_cs; a11=elems[A12]; //A12+=A_cs;
- a20=elems[A22]; A22+=A_cs; a21=elems[A22]; //A22+=A_cs;
-
- for (int j=1; j<c; j++) {
- //in each step 3 instead of 9 cells need to be read from memory.
- a02=elems[A02+=A_cs];
- a12=elems[A12+=A_cs];
- a22=elems[A22+=A_cs];
-
- B_elems[B11] = function.apply(
- a00, a01, a02,
- a10, a11, a12,
- a20, a21, a22);
- B11 += B_cs;
-
- // move remembered cells
- a00=a01; a01=a02;
- a10=a11; a11=a12;
- a20=a21; a21=a22;
- }
- A_index += A_rs;
- B_index += B_rs;
- }
+ if (!(B instanceof DenseDoubleMatrix2D)) {
+ super.zAssign8Neighbors(B, function);
+ return;
+ }
+ if (function==null) throw new NullPointerException("function must not be null.");
+ checkShape(B);
+ int r = rows-1;
+ int c = columns-1;
+ if (rows<3 || columns<3) return; // nothing to do
+
+ DenseDoubleMatrix2D BB = (DenseDoubleMatrix2D) B;
+ int A_rs = rowStride;
+ int B_rs = BB.rowStride;
+ int A_cs = columnStride;
+ int B_cs = BB.columnStride;
+ double[] elems = this.elements;
+ double[] B_elems = BB.elements;
+ if (elems == null || B_elems==null) throw new InternalError();
+
+ int A_index = index(1,1);
+ int B_index = BB.index(1,1);
+ for (int i=1; i<r; i++) {
+ double a00, a01, a02;
+ double a10, a11, a12;
+ double a20, a21, a22;
+
+ int B11 = B_index;
+
+ int A02 = A_index - A_rs - A_cs;
+ int A12 = A02 + A_rs;
+ int A22 = A12 + A_rs;
+
+ // in each step six cells can be remembered in registers - they don't need to be reread from slow memory
+ a00=elems[A02]; A02+=A_cs; a01=elems[A02]; //A02+=A_cs;
+ a10=elems[A12]; A12+=A_cs; a11=elems[A12]; //A12+=A_cs;
+ a20=elems[A22]; A22+=A_cs; a21=elems[A22]; //A22+=A_cs;
+
+ for (int j=1; j<c; j++) {
+ //in each step 3 instead of 9 cells need to be read from memory.
+ a02=elems[A02+=A_cs];
+ a12=elems[A12+=A_cs];
+ a22=elems[A22+=A_cs];
+
+ B_elems[B11] = function.apply(
+ a00, a01, a02,
+ a10, a11, a12,
+ a20, a21, a22);
+ B11 += B_cs;
+
+ // move remembered cells
+ a00=a01; a01=a02;
+ a10=a11; a11=a12;
+ a20=a21; a21=a22;
+ }
+ A_index += A_rs;
+ B_index += B_rs;
+ }
}
public DoubleMatrix1D zMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, boolean transposeA) {
- if (transposeA) return viewDice().zMult(y,z,alpha,beta,false);
- if (z==null) z = new DenseDoubleMatrix1D(this.rows);
- if (!(y instanceof DenseDoubleMatrix1D && z instanceof DenseDoubleMatrix1D)) return super.zMult(y,z,alpha,beta,transposeA);
-
- if (columns != y.size || rows > z.size)
- throw new IllegalArgumentException("Incompatible args: "+toStringShort()+", "+y.toStringShort()+", "+z.toStringShort());
-
- DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D) y;
- DenseDoubleMatrix1D zz = (DenseDoubleMatrix1D) z;
- final double[] AElems = this.elements;
- final double[] yElems = yy.elements;
- final double[] zElems = zz.elements;
- if (AElems==null || yElems==null || zElems==null) throw new InternalError();
- int As = this.columnStride;
- int ys = yy.stride;
- int zs = zz.stride;
-
- int indexA = index(0,0);
- int indexY = yy.index(0);
- int indexZ = zz.index(0);
-
- int cols = columns;
- for (int row=rows; --row >= 0; ) {
- double sum = 0;
-
- /*
- // not loop unrolled
- for (int i=indexA, j=indexY, column=columns; --column >= 0; ) {
- sum += AElems[i] * yElems[j];
- i += As;
- j += ys;
- }
- */
-
- // loop unrolled
- int i = indexA - As;
- int j = indexY - ys;
- for (int k=cols%4; --k >= 0; ) {
- sum += AElems[i += As] * yElems[j += ys];
- }
- for (int k=cols/4; --k >= 0; ) {
- sum += AElems[i += As] * yElems[j += ys] +
- AElems[i += As] * yElems[j += ys] +
- AElems[i += As] * yElems[j += ys] +
- AElems[i += As] * yElems[j += ys];
- }
-
- zElems[indexZ] = alpha*sum + beta*zElems[indexZ];
- indexA += this.rowStride;
- indexZ += zs;
- }
+ if (transposeA) return viewDice().zMult(y,z,alpha,beta,false);
+ if (z==null) z = new DenseDoubleMatrix1D(this.rows);
+ if (!(y instanceof DenseDoubleMatrix1D && z instanceof DenseDoubleMatrix1D)) return super.zMult(y,z,alpha,beta,transposeA);
+
+ if (columns != y.size || rows > z.size)
+ throw new IllegalArgumentException("Incompatible args: "+toStringShort()+", "+y.toStringShort()+", "+z.toStringShort());
+
+ DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D) y;
+ DenseDoubleMatrix1D zz = (DenseDoubleMatrix1D) z;
+ final double[] AElems = this.elements;
+ final double[] yElems = yy.elements;
+ final double[] zElems = zz.elements;
+ if (AElems==null || yElems==null || zElems==null) throw new InternalError();
+ int As = this.columnStride;
+ int ys = yy.stride;
+ int zs = zz.stride;
+
+ int indexA = index(0,0);
+ int indexY = yy.index(0);
+ int indexZ = zz.index(0);
+
+ int cols = columns;
+ for (int row=rows; --row >= 0; ) {
+ double sum = 0;
+
+ /*
+ // not loop unrolled
+ for (int i=indexA, j=indexY, column=columns; --column >= 0; ) {
+ sum += AElems[i] * yElems[j];
+ i += As;
+ j += ys;
+ }
+ */
+
+ // loop unrolled
+ int i = indexA - As;
+ int j = indexY - ys;
+ for (int k=cols%4; --k >= 0; ) {
+ sum += AElems[i += As] * yElems[j += ys];
+ }
+ for (int k=cols/4; --k >= 0; ) {
+ sum += AElems[i += As] * yElems[j += ys] +
+ AElems[i += As] * yElems[j += ys] +
+ AElems[i += As] * yElems[j += ys] +
+ AElems[i += As] * yElems[j += ys];
+ }
+
+ zElems[indexZ] = alpha*sum + beta*zElems[indexZ];
+ indexA += this.rowStride;
+ indexZ += zs;
+ }
- return z;
+ return z;
}
public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, double alpha, double beta, boolean transposeA, boolean transposeB) {
- // overriden for performance only
- if (transposeA) return viewDice().zMult(B,C,alpha,beta,false,transposeB);
- if (B instanceof SparseDoubleMatrix2D || B instanceof RCDoubleMatrix2D) {
- // exploit quick sparse mult
- // A*B = (B' * A')'
- if (C==null) {
- return B.zMult(this, null, alpha,beta,!transposeB,true).viewDice();
- }
- else {
- B.zMult(this, C.viewDice(), alpha,beta,!transposeB,true);
- return C;
- }
- /*
- final RCDoubleMatrix2D transB = new RCDoubleMatrix2D(B.columns,B.rows);
- B.forEachNonZero(
- new org.apache.mahout.matrix.function.IntIntDoubleFunction() {
- public double apply(int i, int j, double value) {
- transB.setQuick(j,i,value);
- return value;
- }
- }
- );
-
- return transB.zMult(this.viewDice(),C.viewDice()).viewDice();
- */
- }
- if (transposeB) return this.zMult(B.viewDice(),C,alpha,beta,transposeA,false);
-
- int m = rows;
- int n = columns;
- int p = B.columns;
- if (C==null) C = new DenseDoubleMatrix2D(m,p);
- if (!(C instanceof DenseDoubleMatrix2D)) return super.zMult(B,C,alpha,beta,transposeA,transposeB);
- if (B.rows != n)
- throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+toStringShort()+", "+B.toStringShort());
- if (C.rows != m || C.columns != p)
- throw new IllegalArgumentException("Incompatibel result matrix: "+toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
- if (this == C || B == C)
- throw new IllegalArgumentException("Matrices must not be identical");
-
- DenseDoubleMatrix2D BB = (DenseDoubleMatrix2D) B;
- DenseDoubleMatrix2D CC = (DenseDoubleMatrix2D) C;
- final double[] AElems = this.elements;
- final double[] BElems = BB.elements;
- final double[] CElems = CC.elements;
- if (AElems==null || BElems==null || CElems==null) throw new InternalError();
-
- int cA = this.columnStride;
- int cB = BB.columnStride;
- int cC = CC.columnStride;
-
- int rA = this.rowStride;
- int rB = BB.rowStride;
- int rC = CC.rowStride;
-
- /*
- A is blocked to hide memory latency
- xxxxxxx B
- xxxxxxx
- xxxxxxx
- A
- xxx xxxxxxx C
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
- xxx xxxxxxx
- --- -------
- xxx xxxxxxx
- */
- final int BLOCK_SIZE = 30000; // * 8 == Level 2 cache in bytes
- //if (n+p == 0) return C;
- //int m_optimal = (BLOCK_SIZE - n*p) / (n+p);
- int m_optimal = (BLOCK_SIZE - n) / (n+1);
- if (m_optimal <= 0) m_optimal = 1;
- int blocks = m/m_optimal;
- int rr = 0;
- if (m%m_optimal != 0) blocks++;
- for (; --blocks >= 0; ) {
- int jB = BB.index(0,0);
- int indexA = index(rr,0);
- int jC = CC.index(rr,0);
- rr += m_optimal;
- if (blocks==0) m_optimal += m - rr;
-
- for (int j = p; --j >= 0; ) {
- int iA = indexA;
- int iC = jC;
- for (int i = m_optimal; --i >= 0; ) {
- int kA = iA;
- int kB = jB;
- double s = 0;
-
- /*
- // not unrolled:
- for (int k = n; --k >= 0; ) {
- //s += getQuick(i,k) * B.getQuick(k,j);
- s += AElems[kA] * BElems[kB];
- kB += rB;
- kA += cA;
- }
- */
-
- // loop unrolled
- kA -= cA;
- kB -= rB;
-
- for (int k=n%4; --k >= 0; ) {
- s += AElems[kA += cA] * BElems[kB += rB];
- }
- for (int k=n/4; --k >= 0; ) {
- s += AElems[kA += cA] * BElems[kB += rB] +
- AElems[kA += cA] * BElems[kB += rB] +
- AElems[kA += cA] * BElems[kB += rB] +
- AElems[kA += cA] * BElems[kB += rB];
- }
-
- CElems[iC] = alpha*s + beta*CElems[iC];
- iA += rA;
- iC += rC;
- }
- jB += cB;
- jC += cC;
- }
- }
- return C;
+ // overriden for performance only
+ if (transposeA) return viewDice().zMult(B,C,alpha,beta,false,transposeB);
+ if (B instanceof SparseDoubleMatrix2D || B instanceof RCDoubleMatrix2D) {
+ // exploit quick sparse mult
+ // A*B = (B' * A')'
+ if (C==null) {
+ return B.zMult(this, null, alpha,beta,!transposeB,true).viewDice();
+ }
+ else {
+ B.zMult(this, C.viewDice(), alpha,beta,!transposeB,true);
+ return C;
+ }
+ /*
+ final RCDoubleMatrix2D transB = new RCDoubleMatrix2D(B.columns,B.rows);
+ B.forEachNonZero(
+ new org.apache.mahout.matrix.function.IntIntDoubleFunction() {
+ public double apply(int i, int j, double value) {
+ transB.setQuick(j,i,value);
+ return value;
+ }
+ }
+ );
+
+ return transB.zMult(this.viewDice(),C.viewDice()).viewDice();
+ */
+ }
+ if (transposeB) return this.zMult(B.viewDice(),C,alpha,beta,transposeA,false);
+
+ int m = rows;
+ int n = columns;
+ int p = B.columns;
+ if (C==null) C = new DenseDoubleMatrix2D(m,p);
+ if (!(C instanceof DenseDoubleMatrix2D)) return super.zMult(B,C,alpha,beta,transposeA,transposeB);
+ if (B.rows != n)
+ throw new IllegalArgumentException("Matrix2D inner dimensions must agree:"+toStringShort()+", "+B.toStringShort());
+ if (C.rows != m || C.columns != p)
+ throw new IllegalArgumentException("Incompatibel result matrix: "+toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
+ if (this == C || B == C)
+ throw new IllegalArgumentException("Matrices must not be identical");
+
+ DenseDoubleMatrix2D BB = (DenseDoubleMatrix2D) B;
+ DenseDoubleMatrix2D CC = (DenseDoubleMatrix2D) C;
+ final double[] AElems = this.elements;
+ final double[] BElems = BB.elements;
+ final double[] CElems = CC.elements;
+ if (AElems==null || BElems==null || CElems==null) throw new InternalError();
+
+ int cA = this.columnStride;
+ int cB = BB.columnStride;
+ int cC = CC.columnStride;
+
+ int rA = this.rowStride;
+ int rB = BB.rowStride;
+ int rC = CC.rowStride;
+
+ /*
+ A is blocked to hide memory latency
+ xxxxxxx B
+ xxxxxxx
+ xxxxxxx
+ A
+ xxx xxxxxxx C
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+ xxx xxxxxxx
+ --- -------
+ xxx xxxxxxx
+ */
+ final int BLOCK_SIZE = 30000; // * 8 == Level 2 cache in bytes
+ //if (n+p == 0) return C;
+ //int m_optimal = (BLOCK_SIZE - n*p) / (n+p);
+ int m_optimal = (BLOCK_SIZE - n) / (n+1);
+ if (m_optimal <= 0) m_optimal = 1;
+ int blocks = m/m_optimal;
+ int rr = 0;
+ if (m%m_optimal != 0) blocks++;
+ for (; --blocks >= 0; ) {
+ int jB = BB.index(0,0);
+ int indexA = index(rr,0);
+ int jC = CC.index(rr,0);
+ rr += m_optimal;
+ if (blocks==0) m_optimal += m - rr;
+
+ for (int j = p; --j >= 0; ) {
+ int iA = indexA;
+ int iC = jC;
+ for (int i = m_optimal; --i >= 0; ) {
+ int kA = iA;
+ int kB = jB;
+ double s = 0;
+
+ /*
+ // not unrolled:
+ for (int k = n; --k >= 0; ) {
+ //s += getQuick(i,k) * B.getQuick(k,j);
+ s += AElems[kA] * BElems[kB];
+ kB += rB;
+ kA += cA;
+ }
+ */
+
+ // loop unrolled
+ kA -= cA;
+ kB -= rB;
+
+ for (int k=n%4; --k >= 0; ) {
+ s += AElems[kA += cA] * BElems[kB += rB];
+ }
+ for (int k=n/4; --k >= 0; ) {
+ s += AElems[kA += cA] * BElems[kB += rB] +
+ AElems[kA += cA] * BElems[kB += rB] +
+ AElems[kA += cA] * BElems[kB += rB] +
+ AElems[kA += cA] * BElems[kB += rB];
+ }
+
+ CElems[iC] = alpha*s + beta*CElems[iC];
+ iA += rA;
+ iC += rC;
+ }
+ jB += cB;
+ jC += cC;
+ }
+ }
+ return C;
}
/**
* Returns the sum of all cells; <tt>Sum( x[i,j] )</tt>.
* @return the sum.
*/
public double zSum() {
- double sum = 0;
- final double[] elems = this.elements;
- if (elems==null) throw new InternalError();
- int index = index(0,0);
- int cs = this.columnStride;
- int rs = this.rowStride;
- for (int row=rows; --row >= 0; ) {
- for (int i=index, column=columns; --column >= 0; ) {
- sum += elems[i];
- i += cs;
- }
- index += rs;
- }
- return sum;
+ double sum = 0;
+ final double[] elems = this.elements;
+ if (elems==null) throw new InternalError();
+ int index = index(0,0);
+ int cs = this.columnStride;
+ int rs = this.rowStride;
+ for (int row=rows; --row >= 0; ) {
+ for (int i=index, column=columns; --column >= 0; ) {
+ sum += elems[i];
+ i += cs;
+ }
+ index += rs;
+ }
+ return sum;
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix3D.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix3D.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/DenseDoubleMatrix3D.java Wed Nov 25 03:41:28 2009
@@ -34,21 +34,21 @@
Thus
<pre>
for (int slice=0; slice < slices; slice++) {
- for (int row=0; row < rows; row++) {
- for (int column=0; column < columns; column++) {
- matrix.setQuick(slice,row,column,someValue);
- }
- }
+ for (int row=0; row < rows; row++) {
+ for (int column=0; column < columns; column++) {
+ matrix.setQuick(slice,row,column,someValue);
+ }
+ }
}
</pre>
is quicker than
<pre>
for (int column=0; column < columns; column++) {
- for (int row=0; row < rows; row++) {
- for (int slice=0; slice < slices; slice++) {
- matrix.setQuick(slice,row,column,someValue);
- }
- }
+ for (int row=0; row < rows; row++) {
+ for (int slice=0; slice < slices; slice++) {
+ matrix.setQuick(slice,row,column,someValue);
+ }
+ }
}
</pre>
@author wolfgang.hoschek@cern.ch
@@ -59,15 +59,15 @@
*/
@Deprecated
public class DenseDoubleMatrix3D extends DoubleMatrix3D {
- /**
- * The elements of this matrix.
- * elements are stored in slice major, then row major, then column major, in order of significance, i.e.
- * index==slice*sliceStride+ row*rowStride + column*columnStride
- * i.e. {slice0 row0..m}, {slice1 row0..m}, ..., {sliceN row0..m}
- * with each row storead as
- * {row0 column0..m}, {row1 column0..m}, ..., {rown column0..m}
- */
- protected double[] elements;
+ /**
+ * The elements of this matrix.
+ * elements are stored in slice major, then row major, then column major, in order of significance, i.e.
+ * index==slice*sliceStride+ row*rowStride + column*columnStride
+ * i.e. {slice0 row0..m}, {slice1 row0..m}, ..., {sliceN row0..m}
+ * with each row storead as
+ * {row0 column0..m}, {row1 column0..m}, ..., {rown column0..m}
+ */
+ protected double[] elements;
/**
* Constructs a matrix with a copy of the given values.
* <tt>values</tt> is required to have the form <tt>values[slice][row][column]</tt>
@@ -80,8 +80,8 @@
* @throws IllegalArgumentException if <tt>for any 1 <= row < values[0].length: values[slice][row].length != values[slice][row-1].length</tt>.
*/
public DenseDoubleMatrix3D(double[][][] values) {
- this(values.length, (values.length==0 ? 0: values[0].length), (values.length==0 ? 0: values[0].length==0 ? 0 : values[0][0].length));
- assign(values);
+ this(values.length, (values.length==0 ? 0: values[0].length), (values.length==0 ? 0: values[0].length==0 ? 0 : values[0][0].length));
+ assign(values);
}
/**
* Constructs a matrix with a given number of slices, rows and columns.
@@ -89,12 +89,12 @@
* @param slices the number of slices the matrix shall have.
* @param rows the number of rows the matrix shall have.
* @param columns the number of columns the matrix shall have.
- * @throws IllegalArgumentException if <tt>(double)slices*columns*rows > Integer.MAX_VALUE</tt>.
- * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+ * @throws IllegalArgumentException if <tt>(double)slices*columns*rows > Integer.MAX_VALUE</tt>.
+ * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
*/
public DenseDoubleMatrix3D(int slices, int rows, int columns) {
- setUp(slices,rows, columns);
- this.elements = new double[slices*rows*columns];
+ setUp(slices,rows, columns);
+ this.elements = new double[slices*rows*columns];
}
/**
* Constructs a view with the given parameters.
@@ -108,13 +108,13 @@
* @param sliceStride the number of elements between two slices, i.e. <tt>index(k+1,i,j)-index(k,i,j)</tt>.
* @param rowStride the number of elements between two rows, i.e. <tt>index(k,i+1,j)-index(k,i,j)</tt>.
* @param columnnStride the number of elements between two columns, i.e. <tt>index(k,i,j+1)-index(k,i,j)</tt>.
- * @throws IllegalArgumentException if <tt>(double)slices*columns*rows > Integer.MAX_VALUE</tt>.
- * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+ * @throws IllegalArgumentException if <tt>(double)slices*columns*rows > Integer.MAX_VALUE</tt>.
+ * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
*/
protected DenseDoubleMatrix3D(int slices, int rows, int columns, double[] elements, int sliceZero, int rowZero, int columnZero, int sliceStride, int rowStride, int columnStride) {
- setUp(slices,rows,columns,sliceZero,rowZero,columnZero,sliceStride,rowStride,columnStride);
- this.elements = elements;
- this.isNoView = false;
+ setUp(slices,rows,columns,sliceZero,rowZero,columnZero,sliceStride,rowStride,columnStride);
+ this.elements = elements;
+ this.isNoView = false;
}
/**
* Sets all cells to the state specified by <tt>values</tt>.
@@ -129,24 +129,24 @@
* @throws IllegalArgumentException if <tt>for any 0 <= column < columns(): values[slice][row].length != columns()</tt>.
*/
public DoubleMatrix3D assign(double[][][] values) {
- if (this.isNoView) {
- if (values.length != slices) throw new IllegalArgumentException("Must have same number of slices: slices="+values.length+"slices()="+slices());
- int i=slices*rows*columns - columns;
- for (int slice=slices; --slice >= 0;) {
- double[][] currentSlice = values[slice];
- if (currentSlice.length != rows) throw new IllegalArgumentException("Must have same number of rows in every slice: rows="+currentSlice.length+"rows()="+rows());
- for (int row=rows; --row >= 0;) {
- double[] currentRow = currentSlice[row];
- if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns());
- System.arraycopy(currentRow, 0, this.elements, i, columns);
- i -= columns;
- }
- }
- }
- else {
- super.assign(values);
- }
- return this;
+ if (this.isNoView) {
+ if (values.length != slices) throw new IllegalArgumentException("Must have same number of slices: slices="+values.length+"slices()="+slices());
+ int i=slices*rows*columns - columns;
+ for (int slice=slices; --slice >= 0;) {
+ double[][] currentSlice = values[slice];
+ if (currentSlice.length != rows) throw new IllegalArgumentException("Must have same number of rows in every slice: rows="+currentSlice.length+"rows()="+rows());
+ for (int row=rows; --row >= 0;) {
+ double[] currentRow = currentSlice[row];
+ if (currentRow.length != columns) throw new IllegalArgumentException("Must have same number of columns in every row: columns="+currentRow.length+"columns()="+columns());
+ System.arraycopy(currentRow, 0, this.elements, i, columns);
+ i -= columns;
+ }
+ }
+ }
+ else {
+ super.assign(values);
+ }
+ return this;
}
/**
* Replaces all cell values of the receiver with the values of another matrix.
@@ -155,29 +155,29 @@
*
* @param source the source matrix to copy from (may be identical to the receiver).
* @return <tt>this</tt> (for convenience only).
- * @throws IllegalArgumentException if <tt>slices() != source.slices() || rows() != source.rows() || columns() != source.columns()</tt>
+ * @throws IllegalArgumentException if <tt>slices() != source.slices() || rows() != source.rows() || columns() != source.columns()</tt>
*/
public DoubleMatrix3D assign(DoubleMatrix3D source) {
- // overriden for performance only
- if (! (source instanceof DenseDoubleMatrix3D)) {
- return super.assign(source);
- }
- DenseDoubleMatrix3D other = (DenseDoubleMatrix3D) source;
- if (other==this) return this;
- checkShape(other);
- if (haveSharedCells(other)) {
- DoubleMatrix3D c = other.copy();
- if (! (c instanceof DenseDoubleMatrix3D)) { // should not happen
- return super.assign(source);
- }
- other = (DenseDoubleMatrix3D) c;
- }
-
- if (this.isNoView && other.isNoView) { // quickest
- System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
- return this;
- }
- return super.assign(other);
+ // overriden for performance only
+ if (! (source instanceof DenseDoubleMatrix3D)) {
+ return super.assign(source);
+ }
+ DenseDoubleMatrix3D other = (DenseDoubleMatrix3D) source;
+ if (other==this) return this;
+ checkShape(other);
+ if (haveSharedCells(other)) {
+ DoubleMatrix3D c = other.copy();
+ if (! (c instanceof DenseDoubleMatrix3D)) { // should not happen
+ return super.assign(source);
+ }
+ other = (DenseDoubleMatrix3D) c;
+ }
+
+ if (this.isNoView && other.isNoView) { // quickest
+ System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
+ return this;
+ }
+ return super.assign(other);
}
/**
* Returns the matrix cell value at coordinate <tt>[slice,row,column]</tt>.
@@ -192,10 +192,10 @@
* @return the value at the specified coordinate.
*/
public double getQuick(int slice, int row, int column) {
- //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
- //return elements[index(slice,row,column)];
- //manually inlined:
- return elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride];
+ //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
+ //return elements[index(slice,row,column)];
+ //manually inlined:
+ return elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride];
}
/**
* Returns <tt>true</tt> if both matrices share common cells.
@@ -207,15 +207,15 @@
* </ul>
*/
protected boolean haveSharedCellsRaw(DoubleMatrix3D other) {
- if (other instanceof SelectedDenseDoubleMatrix3D) {
- SelectedDenseDoubleMatrix3D otherMatrix = (SelectedDenseDoubleMatrix3D) other;
- return this.elements==otherMatrix.elements;
- }
- else if (other instanceof DenseDoubleMatrix3D) {
- DenseDoubleMatrix3D otherMatrix = (DenseDoubleMatrix3D) other;
- return this.elements==otherMatrix.elements;
- }
- return false;
+ if (other instanceof SelectedDenseDoubleMatrix3D) {
+ SelectedDenseDoubleMatrix3D otherMatrix = (SelectedDenseDoubleMatrix3D) other;
+ return this.elements==otherMatrix.elements;
+ }
+ else if (other instanceof DenseDoubleMatrix3D) {
+ DenseDoubleMatrix3D otherMatrix = (DenseDoubleMatrix3D) 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.
@@ -225,9 +225,9 @@
* @param column the index of the third-coordinate.
*/
protected int index(int slice, int row, int column) {
- //return _sliceOffset(_sliceRank(slice)) + _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
- //manually inlined:
- return sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride;
+ //return _sliceOffset(_sliceRank(slice)) + _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
+ //manually inlined:
+ return sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride;
}
/**
* Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified number of slices, rows and columns.
@@ -241,7 +241,7 @@
* @return a new empty matrix of the same dynamic type.
*/
public DoubleMatrix3D like(int slices, int rows, int columns) {
- return new DenseDoubleMatrix3D(slices,rows,columns);
+ return new DenseDoubleMatrix3D(slices,rows,columns);
}
/**
* Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, sharing the same cells.
@@ -257,7 +257,7 @@
* @return a new matrix of the corresponding dynamic type.
*/
protected DoubleMatrix2D like2D(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) {
- return new DenseDoubleMatrix2D(rows,columns,this.elements,rowZero,columnZero,rowStride,columnStride);
+ return new DenseDoubleMatrix2D(rows,columns,this.elements,rowZero,columnZero,rowStride,columnStride);
}
/**
* Sets the matrix cell at coordinate <tt>[slice,row,column]</tt> to the specified value.
@@ -272,10 +272,10 @@
* @param value the value to be filled into the specified cell.
*/
public void setQuick(int slice, int row, int column, double value) {
- //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
- //elements[index(slice,row,column)] = value;
- //manually inlined:
- elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride] = value;
+ //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
+ //elements[index(slice,row,column)] = value;
+ //manually inlined:
+ elements[sliceZero + slice*sliceStride + rowZero + row*rowStride + columnZero + column*columnStride] = value;
}
/**
* Construct and returns a new selection view.
@@ -286,7 +286,7 @@
* @return a new view.
*/
protected DoubleMatrix3D viewSelectionLike(int[] sliceOffsets, int[] rowOffsets, int[] columnOffsets) {
- return new SelectedDenseDoubleMatrix3D(this.elements,sliceOffsets,rowOffsets,columnOffsets,0);
+ return new SelectedDenseDoubleMatrix3D(this.elements,sliceOffsets,rowOffsets,columnOffsets,0);
}
/**
27 neighbor stencil transformation. For efficient finite difference operations.
@@ -340,121 +340,121 @@
};
A.zAssign27Neighbors(B,f);
</pre>
-
+
@param B the matrix to hold the results.
@param function the function to be applied to the 27 cells.
@throws NullPointerException if <tt>function==null</tt>.
@throws IllegalArgumentException if <tt>rows() != B.rows() || columns() != B.columns() || slices() != B.slices() </tt>.
*/
public void zAssign27Neighbors(DoubleMatrix3D B, org.apache.mahout.matrix.function.Double27Function function) {
- // overridden for performance only
- if (!(B instanceof DenseDoubleMatrix3D)) {
- super.zAssign27Neighbors(B, function);
- return;
- }
- if (function==null) throw new NullPointerException("function must not be null.");
- checkShape(B);
- int r = rows-1;
- int c = columns-1;
- if (rows<3 || columns<3 || slices<3) return; // nothing to do
-
- DenseDoubleMatrix3D BB = (DenseDoubleMatrix3D) B;
- int A_ss = sliceStride;
- int A_rs = rowStride;
- int B_rs = BB.rowStride;
- int A_cs = columnStride;
- int B_cs = BB.columnStride;
- double[] elems = this.elements;
- double[] B_elems = BB.elements;
- if (elems == null || B_elems==null) throw new InternalError();
-
- for (int k=1; k<slices-1; k++) {
- int A_index = index(k,1,1);
- int B_index = BB.index(k,1,1);
-
- for (int i=1; i<r; i++) {
- int A002 = A_index - A_ss - A_rs - A_cs;
- int A012 = A002 + A_rs;
- int A022 = A012 + A_rs;
-
- int A102 = A002 + A_ss;
- int A112 = A102 + A_rs;
- int A122 = A112 + A_rs;
-
- int A202 = A102 + A_ss;
- int A212 = A202 + A_rs;
- int A222 = A212 + A_rs;
-
- double a000, a001, a002;
- double a010, a011, a012;
- double a020, a021, a022;
-
- double a100, a101, a102;
- double a110, a111, a112;
- double a120, a121, a122;
-
- double a200, a201, a202;
- double a210, a211, a212;
- double a220, a221, a222;
-
- a000=elems[A002]; A002+=A_cs; a001=elems[A002];
- a010=elems[A012]; A012+=A_cs; a011=elems[A012];
- a020=elems[A022]; A022+=A_cs; a021=elems[A022];
-
- a100=elems[A102]; A102+=A_cs; a101=elems[A102];
- a110=elems[A112]; A112+=A_cs; a111=elems[A112];
- a120=elems[A122]; A122+=A_cs; a121=elems[A122];
-
- a200=elems[A202]; A202+=A_cs; a201=elems[A202];
- a210=elems[A212]; A212+=A_cs; a211=elems[A212];
- a220=elems[A222]; A222+=A_cs; a221=elems[A222];
-
- int B11 = B_index;
- for (int j=1; j<c; j++) {
- // in each step 18 cells can be remembered in registers - they don't need to be reread from slow memory
- // in each step 9 instead of 27 cells need to be read from memory.
- a002=elems[A002+=A_cs];
- a012=elems[A012+=A_cs];
- a022=elems[A022+=A_cs];
-
- a102=elems[A102+=A_cs];
- a112=elems[A112+=A_cs];
- a122=elems[A122+=A_cs];
-
- a202=elems[A202+=A_cs];
- a212=elems[A212+=A_cs];
- a222=elems[A222+=A_cs];
-
- B_elems[B11] = function.apply(
- a000, a001, a002,
- a010, a011, a012,
- a020, a021, a022,
-
- a100, a101, a102,
- a110, a111, a112,
- a120, a121, a122,
-
- a200, a201, a202,
- a210, a211, a212,
- a220, a221, a222);
- B11 += B_cs;
-
- // move remembered cells
- a000=a001; a001=a002;
- a010=a011; a011=a012;
- a020=a021; a021=a022;
-
- a100=a101; a101=a102;
- a110=a111; a111=a112;
- a120=a121; a121=a122;
-
- a200=a201; a201=a202;
- a210=a211; a211=a212;
- a220=a221; a221=a222;
- }
- A_index += A_rs;
- B_index += B_rs;
- }
- }
+ // overridden for performance only
+ if (!(B instanceof DenseDoubleMatrix3D)) {
+ super.zAssign27Neighbors(B, function);
+ return;
+ }
+ if (function==null) throw new NullPointerException("function must not be null.");
+ checkShape(B);
+ int r = rows-1;
+ int c = columns-1;
+ if (rows<3 || columns<3 || slices<3) return; // nothing to do
+
+ DenseDoubleMatrix3D BB = (DenseDoubleMatrix3D) B;
+ int A_ss = sliceStride;
+ int A_rs = rowStride;
+ int B_rs = BB.rowStride;
+ int A_cs = columnStride;
+ int B_cs = BB.columnStride;
+ double[] elems = this.elements;
+ double[] B_elems = BB.elements;
+ if (elems == null || B_elems==null) throw new InternalError();
+
+ for (int k=1; k<slices-1; k++) {
+ int A_index = index(k,1,1);
+ int B_index = BB.index(k,1,1);
+
+ for (int i=1; i<r; i++) {
+ int A002 = A_index - A_ss - A_rs - A_cs;
+ int A012 = A002 + A_rs;
+ int A022 = A012 + A_rs;
+
+ int A102 = A002 + A_ss;
+ int A112 = A102 + A_rs;
+ int A122 = A112 + A_rs;
+
+ int A202 = A102 + A_ss;
+ int A212 = A202 + A_rs;
+ int A222 = A212 + A_rs;
+
+ double a000, a001, a002;
+ double a010, a011, a012;
+ double a020, a021, a022;
+
+ double a100, a101, a102;
+ double a110, a111, a112;
+ double a120, a121, a122;
+
+ double a200, a201, a202;
+ double a210, a211, a212;
+ double a220, a221, a222;
+
+ a000=elems[A002]; A002+=A_cs; a001=elems[A002];
+ a010=elems[A012]; A012+=A_cs; a011=elems[A012];
+ a020=elems[A022]; A022+=A_cs; a021=elems[A022];
+
+ a100=elems[A102]; A102+=A_cs; a101=elems[A102];
+ a110=elems[A112]; A112+=A_cs; a111=elems[A112];
+ a120=elems[A122]; A122+=A_cs; a121=elems[A122];
+
+ a200=elems[A202]; A202+=A_cs; a201=elems[A202];
+ a210=elems[A212]; A212+=A_cs; a211=elems[A212];
+ a220=elems[A222]; A222+=A_cs; a221=elems[A222];
+
+ int B11 = B_index;
+ for (int j=1; j<c; j++) {
+ // in each step 18 cells can be remembered in registers - they don't need to be reread from slow memory
+ // in each step 9 instead of 27 cells need to be read from memory.
+ a002=elems[A002+=A_cs];
+ a012=elems[A012+=A_cs];
+ a022=elems[A022+=A_cs];
+
+ a102=elems[A102+=A_cs];
+ a112=elems[A112+=A_cs];
+ a122=elems[A122+=A_cs];
+
+ a202=elems[A202+=A_cs];
+ a212=elems[A212+=A_cs];
+ a222=elems[A222+=A_cs];
+
+ B_elems[B11] = function.apply(
+ a000, a001, a002,
+ a010, a011, a012,
+ a020, a021, a022,
+
+ a100, a101, a102,
+ a110, a111, a112,
+ a120, a121, a122,
+
+ a200, a201, a202,
+ a210, a211, a212,
+ a220, a221, a222);
+ B11 += B_cs;
+
+ // move remembered cells
+ a000=a001; a001=a002;
+ a010=a011; a011=a012;
+ a020=a021; a021=a022;
+
+ a100=a101; a101=a102;
+ a110=a111; a111=a112;
+ a120=a121; a121=a122;
+
+ a200=a201; a201=a202;
+ a210=a211; a211=a212;
+ a220=a221; a221=a222;
+ }
+ A_index += A_rs;
+ B_index += B_rs;
+ }
+ }
}
}