You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by gs...@apache.org on 2009/12/18 00:22:41 UTC
svn commit: r891983 [35/47] - in /lucene/mahout/trunk: ./ core/
core/src/main/java/org/apache/mahout/cf/taste/hadoop/item/
core/src/main/java/org/apache/mahout/clustering/
core/src/main/java/org/apache/mahout/clustering/canopy/
core/src/main/java/org/a...
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix1D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,225 @@
+/*
+Copyright 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 org.apache.mahout.math.matrix.impl;
+
+/**
+ Abstract base class for 1-d matrices (aka <i>vectors</i>) holding objects or primitive data types such as <code>int</code>, <code>double</code>, etc.
+ First see the <a href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the broad picture.
+ <p>
+ <b>Note that this implementation is not synchronized.</b>
+
+ @author wolfgang.hoschek@cern.ch
+ @version 1.0, 09/24/99
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public abstract class AbstractMatrix1D extends AbstractMatrix {
+
+ /** the number of cells this matrix (view) has */
+ protected int size;
+
+
+ /** the index of the first element */
+ protected int zero;
+
+ /** the number of indexes between any two elements, i.e. <tt>index(i+1) - index(i)</tt>. */
+ protected int stride;
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected AbstractMatrix1D() {
+ }
+
+ /**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param absRank the absolute rank of the element.
+ * @return the position.
+ */
+ protected int _offset(int absRank) {
+ return absRank;
+ }
+
+ /**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+ protected int _rank(int rank) {
+ return zero + rank * stride;
+ //return zero + ((rank+flipMask)^flipMask);
+ //return zero + rank*flip; // slower
+ }
+
+ /**
+ * Sanity check for operations requiring an index to be within bounds.
+ *
+ * @throws IndexOutOfBoundsException if <tt>index < 0 || index >= size()</tt>.
+ */
+ protected void checkIndex(int index) {
+ if (index < 0 || index >= size) {
+ throw new IndexOutOfBoundsException("Attempted to access " + toStringShort() + " at index=" + index);
+ }
+ }
+
+ /**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < size())</tt> for any i=0..indexes.length()-1.
+ */
+ protected void checkIndexes(int[] indexes) {
+ for (int i = indexes.length; --i >= 0;) {
+ int index = indexes[i];
+ if (index < 0 || index >= size) {
+ checkIndex(index);
+ }
+ }
+ }
+
+ /**
+ * Checks whether the receiver contains the given range and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>index<0 || index+width>size()</tt>.
+ */
+ protected void checkRange(int index, int width) {
+ if (index < 0 || index + width > size) {
+ throw new IndexOutOfBoundsException("index: " + index + ", width: " + width + ", size=" + size);
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring two matrices with the same size.
+ *
+ * @throws IllegalArgumentException if <tt>size() != B.size()</tt>.
+ */
+ protected void checkSize(double[] B) {
+ if (size != B.length) {
+ throw new IllegalArgumentException("Incompatible sizes: " + toStringShort() + " and " + B.length);
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring two matrices with the same size.
+ *
+ * @throws IllegalArgumentException if <tt>size() != B.size()</tt>.
+ */
+ public void checkSize(AbstractMatrix1D B) {
+ if (size != B.size) {
+ throw new IllegalArgumentException("Incompatible sizes: " + toStringShort() + " and " + B.toStringShort());
+ }
+ }
+
+ /**
+ * 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) {
+ return _offset(_rank(rank));
+ }
+
+ /**
+ * Sets up a matrix with a given number of cells.
+ *
+ * @param size the number of cells the matrix shall have.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+ protected void setUp(int size) {
+ setUp(size, 0, 1);
+ }
+
+ /**
+ * Sets up a matrix with the given parameters.
+ *
+ * @param size the number of elements the matrix shall have.
+ * @param zero the index of the first element.
+ * @param stride the number of indexes between any two elements, i.e. <tt>index(i+1)-index(i)</tt>.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+ protected void setUp(int size, int zero, int stride) {
+ if (size < 0) {
+ throw new IllegalArgumentException("negative size");
+ }
+
+ this.size = size;
+ this.zero = zero;
+ this.stride = stride;
+ this.isNoView = true;
+ }
+
+ /** Returns the number of cells. */
+ @Override
+ public int size() {
+ return size;
+ }
+
+ /**
+ * Returns the stride of the given dimension (axis, rank).
+ *
+ * @return the stride in the given dimension.
+ * @throws IllegalArgumentException if <tt>dimension != 0</tt>.
+ */
+ protected int stride(int dimension) {
+ if (dimension != 0) {
+ throw new IllegalArgumentException("invalid dimension: " + dimension + "used to access" + toStringShort());
+ }
+ return this.stride;
+ }
+
+ /** Returns a string representation of the receiver's shape. */
+ public String toStringShort() {
+ return AbstractFormatter.shape(this);
+ }
+
+ /**
+ * Self modifying version of viewFlip(). What used to be index <tt>0</tt> is now index <tt>size()-1</tt>, ..., what
+ * used to be index <tt>size()-1</tt> is now index <tt>0</tt>.
+ */
+ protected AbstractMatrix1D vFlip() {
+ if (size > 0) {
+ this.zero += (this.size - 1) * this.stride;
+ this.stride = -this.stride;
+ this.isNoView = false;
+ }
+ return this;
+ }
+
+ /**
+ * Self modifying version of viewPart().
+ *
+ * @throws IndexOutOfBoundsException if <tt>index<0 || index+width>size()</tt>.
+ */
+ protected AbstractMatrix1D vPart(int index, int width) {
+ checkRange(index, width);
+ this.zero += this.stride * index;
+ this.size = width;
+ this.isNoView = false;
+ return this;
+ }
+
+ /**
+ * Self modifying version of viewStrides().
+ *
+ * @throws IndexOutOfBoundsException if <tt>stride <= 0</tt>.
+ */
+ protected AbstractMatrix1D vStrides(int stride) {
+ if (stride <= 0) {
+ throw new IndexOutOfBoundsException("illegal stride: " + stride);
+ }
+ this.stride *= stride;
+ if (this.size != 0) {
+ this.size = (this.size - 1) / stride + 1;
+ }
+ this.isNoView = false;
+ return this;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix2D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix2D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix2D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,322 @@
+/*
+Copyright 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 org.apache.mahout.math.matrix.impl;
+
+/**
+ Abstract base class for 2-d matrices holding objects or primitive data types such as <code>int</code>, <code>double</code>, etc.
+ First see the <a href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the broad picture.
+ <p>
+ <b>Note that this implementation is not synchronized.</b>
+
+ @author wolfgang.hoschek@cern.ch
+ @version 1.0, 09/24/99
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public abstract class AbstractMatrix2D extends AbstractMatrix {
+
+ /** the number of colums and rows this matrix (view) has */
+ protected int columns, rows;
+
+ /** the number of elements between two rows, i.e. <tt>index(i+1,j,k) - index(i,j,k)</tt>. */
+ protected int rowStride;
+
+ /** the number of elements between two columns, i.e. <tt>index(i,j+1,k) - index(i,j,k)</tt>. */
+ protected int columnStride;
+
+ /** the index of the first element */
+ protected int rowZero, columnZero;
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected AbstractMatrix2D() {
+ }
+
+ /**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param absRank the absolute rank of the element.
+ * @return the position.
+ */
+ protected int _columnOffset(int absRank) {
+ return absRank;
+ }
+
+ /**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+ protected int _columnRank(int rank) {
+ return columnZero + rank * columnStride;
+ //return columnZero + ((rank+columnFlipMask)^columnFlipMask);
+ //return columnZero + rank*columnFlip; // slower
+ }
+
+ /**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param absRank the absolute rank of the element.
+ * @return the position.
+ */
+ protected int _rowOffset(int absRank) {
+ return absRank;
+ }
+
+ /**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+ protected int _rowRank(int rank) {
+ return rowZero + rank * rowStride;
+ //return rowZero + ((rank+rowFlipMask)^rowFlipMask);
+ //return rowZero + rank*rowFlip; // slower
+ }
+
+ /**
+ * Checks whether the receiver contains the given box and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>column<0 || width<0 || column+width>columns() || row<0 || height<0 ||
+ * row+height>rows()</tt>
+ */
+ protected void checkBox(int row, int column, int height, int width) {
+ if (column < 0 || width < 0 || column + width > columns || row < 0 || height < 0 || row + height > rows) {
+ throw new IndexOutOfBoundsException(
+ toStringShort() + ", column:" + column + ", row:" + row + " ,width:" + width + ", height:" + height);
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring a column index to be within bounds.
+ *
+ * @throws IndexOutOfBoundsException if <tt>column < 0 || column >= columns()</tt>.
+ */
+ protected void checkColumn(int column) {
+ if (column < 0 || column >= columns) {
+ throw new IndexOutOfBoundsException("Attempted to access " + toStringShort() + " at column=" + column);
+ }
+ }
+
+ /**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < columns())</tt> for any i=0..indexes.length()-1.
+ */
+ protected void checkColumnIndexes(int[] indexes) {
+ for (int i = indexes.length; --i >= 0;) {
+ int index = indexes[i];
+ if (index < 0 || index >= columns) {
+ checkColumn(index);
+ }
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring a row index to be within bounds.
+ *
+ * @throws IndexOutOfBoundsException if <tt>row < 0 || row >= rows()</tt>.
+ */
+ protected void checkRow(int row) {
+ if (row < 0 || row >= rows) {
+ throw new IndexOutOfBoundsException("Attempted to access " + toStringShort() + " at row=" + row);
+ }
+ }
+
+ /**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < rows())</tt> for any i=0..indexes.length()-1.
+ */
+ protected void checkRowIndexes(int[] indexes) {
+ for (int i = indexes.length; --i >= 0;) {
+ int index = indexes[i];
+ if (index < 0 || index >= rows) {
+ checkRow(index);
+ }
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring two matrices with the same number of columns and rows.
+ *
+ * @throws IllegalArgumentException if <tt>columns() != B.columns() || rows() != B.rows()</tt>.
+ */
+ public void checkShape(AbstractMatrix2D B) {
+ if (columns != B.columns || rows != B.rows) {
+ throw new IllegalArgumentException("Incompatible dimensions: " + toStringShort() + " and " + B.toStringShort());
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring matrices with the same number of columns and rows.
+ *
+ * @throws IllegalArgumentException if <tt>columns() != B.columns() || rows() != B.rows() || columns() != C.columns()
+ * || rows() != C.rows()</tt>.
+ */
+ public void checkShape(AbstractMatrix2D B, AbstractMatrix2D C) {
+ if (columns != B.columns || rows != B.rows || columns != C.columns || rows != C.rows) {
+ throw new IllegalArgumentException(
+ "Incompatible dimensions: " + toStringShort() + ", " + B.toStringShort() + ", " + C.toStringShort());
+ }
+ }
+
+ /** Returns the number of columns. */
+ public int columns() {
+ return columns;
+ }
+
+ /**
+ * Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array.
+ *
+ * @param row the index of the row-coordinate.
+ * @param column the index of the column-coordinate.
+ */
+ protected int index(int row, int column) {
+ return _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
+ }
+
+ /** Returns the number of rows. */
+ public int rows() {
+ return rows;
+ }
+
+ /**
+ * Sets up a matrix with a given number of rows and columns.
+ *
+ * @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>.
+ */
+ protected void setUp(int rows, int columns) {
+ setUp(rows, columns, 0, 0, columns, 1);
+ }
+
+ /**
+ * Sets up a matrix with a given number of rows and columns and the given strides.
+ *
+ * @param rows the number of rows the matrix shall have.
+ * @param columns the number of columns the matrix shall have.
+ * @param rowZero the position of the first element.
+ * @param columnZero the position of the first element.
+ * @param rowStride the number of elements between two rows, i.e. <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.
+ */
+ protected void setUp(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) {
+ if (rows < 0 || columns < 0) {
+ throw new IllegalArgumentException("negative size");
+ }
+ this.rows = rows;
+ this.columns = columns;
+
+ this.rowZero = rowZero;
+ this.columnZero = columnZero;
+
+ this.rowStride = rowStride;
+ this.columnStride = columnStride;
+
+ this.isNoView = true;
+ if ((double) columns * rows > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("matrix too large");
+ }
+ }
+
+ /** Returns the number of cells which is <tt>rows()*columns()</tt>. */
+ @Override
+ public int size() {
+ return rows * columns;
+ }
+
+ /** Returns a string representation of the receiver's shape. */
+ public String toStringShort() {
+ return AbstractFormatter.shape(this);
+ }
+
+ /** Self modifying version of viewColumnFlip(). */
+ protected AbstractMatrix2D vColumnFlip() {
+ if (columns > 0) {
+ columnZero += (columns - 1) * columnStride;
+ columnStride = -columnStride;
+ this.isNoView = false;
+ }
+ return this;
+ }
+
+ /** Self modifying version of viewDice(). */
+ protected AbstractMatrix2D vDice() {
+ // swap;
+ int tmp = rows;
+ rows = columns;
+ columns = tmp;
+ tmp = rowStride;
+ rowStride = columnStride;
+ columnStride = tmp;
+ tmp = rowZero;
+ rowZero = columnZero;
+ columnZero = tmp;
+
+ // flips stay unaffected
+
+ this.isNoView = false;
+ return this;
+ }
+
+ /**
+ * Self modifying version of viewPart().
+ *
+ * @throws IndexOutOfBoundsException if <tt>column<0 || width<0 || column+width>columns() || row<0 || height<0 ||
+ * row+height>rows()</tt>
+ */
+ protected AbstractMatrix2D vPart(int row, int column, int height, int width) {
+ checkBox(row, column, height, width);
+ this.rowZero += this.rowStride * row;
+ this.columnZero += this.columnStride * column;
+ this.rows = height;
+ this.columns = width;
+ this.isNoView = false;
+ return this;
+ }
+
+ /** Self modifying version of viewRowFlip(). */
+ protected AbstractMatrix2D vRowFlip() {
+ if (rows > 0) {
+ rowZero += (rows - 1) * rowStride;
+ rowStride = -rowStride;
+ this.isNoView = false;
+ }
+ return this;
+ }
+
+ /**
+ * Self modifying version of viewStrides().
+ *
+ * @throws IndexOutOfBoundsException if <tt>rowStride<=0 || columnStride<=0</tt>.
+ */
+ protected AbstractMatrix2D vStrides(int rowStride, int columnStride) {
+ if (rowStride <= 0 || columnStride <= 0) {
+ throw new IndexOutOfBoundsException("illegal strides: " + rowStride + ", " + columnStride);
+ }
+ this.rowStride *= rowStride;
+ this.columnStride *= columnStride;
+ if (this.rows != 0) {
+ this.rows = (this.rows - 1) / rowStride + 1;
+ }
+ if (this.columns != 0) {
+ this.columns = (this.columns - 1) / columnStride + 1;
+ }
+ this.isNoView = false;
+ return this;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix2D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix3D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix3D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix3D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,440 @@
+/*
+Copyright 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 org.apache.mahout.math.matrix.impl;
+
+/**
+ Abstract base class for 3-d matrices holding objects or primitive data types such as <code>int</code>, <code>double</code>, etc.
+ First see the <a href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the broad picture.
+ <p>
+ <b>Note that this implementation is not synchronized.</b>
+
+ @author wolfgang.hoschek@cern.ch
+ @version 1.0, 09/24/99
+ */
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public abstract class AbstractMatrix3D extends AbstractMatrix {
+
+ /** the number of slices this matrix (view) has */
+ protected int slices;
+
+ /** the number of rows this matrix (view) has */
+ protected int rows;
+
+ /** the number of columns this matrix (view) has */
+ protected int columns;
+
+
+ /** the number of elements between two slices, i.e. <tt>index(k+1,i,j) - index(k,i,j)</tt>. */
+ protected int sliceStride;
+
+ /** the number of elements between two rows, i.e. <tt>index(k,i+1,j) - index(k,i,j)</tt>. */
+ protected int rowStride;
+
+ /** the number of elements between two columns, i.e. <tt>index(k,i,j+1) - index(k,i,j)</tt>. */
+ protected int columnStride;
+
+ /** the index of the first element */
+ protected int sliceZero, rowZero, columnZero;
+
+ // this.isNoView implies: offset==0, sliceStride==rows*slices, rowStride==columns, columnStride==1
+
+ /** Makes this class non instantiable, but still let's others inherit from it. */
+ protected AbstractMatrix3D() {
+ }
+
+ /**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param absRank the absolute rank of the element.
+ * @return the position.
+ */
+ protected int _columnOffset(int absRank) {
+ return absRank;
+ }
+
+ /**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+ protected int _columnRank(int rank) {
+ return columnZero + rank * columnStride;
+ }
+
+ /**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param absRank the absolute rank of the element.
+ * @return the position.
+ */
+ protected int _rowOffset(int absRank) {
+ return absRank;
+ }
+
+ /**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+ protected int _rowRank(int rank) {
+ return rowZero + rank * rowStride;
+ }
+
+ /**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param absRank the absolute rank of the element.
+ * @return the position.
+ */
+ protected int _sliceOffset(int absRank) {
+ return absRank;
+ }
+
+ /**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+ protected int _sliceRank(int rank) {
+ return sliceZero + rank * sliceStride;
+ }
+
+ /**
+ * Checks whether the receiver contains the given box and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>row<0 || height<0 || row+height>rows || slice<0 || depth<0 ||
+ * slice+depth>slices || column<0 || width<0 || column+width>columns</tt>
+ */
+ protected void checkBox(int slice, int row, int column, int depth, int height, int width) {
+ if (slice < 0 || depth < 0 || slice + depth > slices || row < 0 || height < 0 || row + height > rows ||
+ column < 0 || width < 0 || column + width > columns) {
+ throw new IndexOutOfBoundsException(
+ toStringShort() + ", slice:" + slice + ", row:" + row + " ,column:" + column + ", depth:" + depth +
+ " ,height:" + height + ", width:" + width);
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring a column index to be within bounds.
+ *
+ * @throws IndexOutOfBoundsException if <tt>column < 0 || column >= columns()</tt>.
+ */
+ protected void checkColumn(int column) {
+ if (column < 0 || column >= columns) {
+ throw new IndexOutOfBoundsException("Attempted to access " + toStringShort() + " at column=" + column);
+ }
+ }
+
+ /**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < columns())</tt> for any i=0..indexes.length()-1.
+ */
+ protected void checkColumnIndexes(int[] indexes) {
+ for (int i = indexes.length; --i >= 0;) {
+ int index = indexes[i];
+ if (index < 0 || index >= columns) {
+ checkColumn(index);
+ }
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring a row index to be within bounds.
+ *
+ * @throws IndexOutOfBoundsException if <tt>row < 0 || row >= rows()</tt>.
+ */
+ protected void checkRow(int row) {
+ if (row < 0 || row >= rows) {
+ throw new IndexOutOfBoundsException("Attempted to access " + toStringShort() + " at row=" + row);
+ }
+ }
+
+ /**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < rows())</tt> for any i=0..indexes.length()-1.
+ */
+ protected void checkRowIndexes(int[] indexes) {
+ for (int i = indexes.length; --i >= 0;) {
+ int index = indexes[i];
+ if (index < 0 || index >= rows) {
+ checkRow(index);
+ }
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring two matrices with the same number of slices, rows and columns.
+ *
+ * @throws IllegalArgumentException if <tt>slices() != B.slices() || rows() != B.rows() || columns() !=
+ * B.columns()</tt>.
+ */
+ public void checkShape(AbstractMatrix3D B) {
+ if (slices != B.slices || rows != B.rows || columns != B.columns) {
+ throw new IllegalArgumentException("Incompatible dimensions: " + toStringShort() + " and " + B.toStringShort());
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring matrices with the same number of slices, rows and columns.
+ *
+ * @throws IllegalArgumentException if <tt>slices() != B.slices() || rows() != B.rows() || columns() != B.columns() ||
+ * slices() != C.slices() || rows() != C.rows() || columns() != C.columns()</tt>.
+ */
+ public void checkShape(AbstractMatrix3D B, AbstractMatrix3D C) {
+ if (slices != B.slices || rows != B.rows || columns != B.columns || slices != C.slices || rows != C.rows ||
+ columns != C.columns) {
+ throw new IllegalArgumentException(
+ "Incompatible dimensions: " + toStringShort() + ", " + B.toStringShort() + ", " + C.toStringShort());
+ }
+ }
+
+ /**
+ * Sanity check for operations requiring a slice index to be within bounds.
+ *
+ * @throws IndexOutOfBoundsException if <tt>slice < 0 || slice >= slices()</tt>.
+ */
+ protected void checkSlice(int slice) {
+ if (slice < 0 || slice >= slices) {
+ throw new IndexOutOfBoundsException("Attempted to access " + toStringShort() + " at slice=" + slice);
+ }
+ }
+
+ /**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ *
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < slices())</tt> for any i=0..indexes.length()-1.
+ */
+ protected void checkSliceIndexes(int[] indexes) {
+ for (int i = indexes.length; --i >= 0;) {
+ int index = indexes[i];
+ if (index < 0 || index >= slices) {
+ checkSlice(index);
+ }
+ }
+ }
+
+ /** Returns the number of columns. */
+ public int columns() {
+ return columns;
+ }
+
+ /**
+ * Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array.
+ *
+ * @param slice the index of the slice-coordinate.
+ * @param row the index of the row-coordinate.
+ * @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));
+ }
+
+ /** Returns the number of rows. */
+ public int rows() {
+ return rows;
+ }
+
+ /**
+ * Sets up a matrix with a given number of slices and rows.
+ *
+ * @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)rows*slices > Integer.MAX_VALUE</tt>.
+ * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+ */
+ protected void setUp(int slices, int rows, int columns) {
+ setUp(slices, rows, columns, 0, 0, 0, rows * columns, columns, 1);
+ }
+
+ /**
+ * Sets up a matrix with a given number of slices and rows and the given strides.
+ *
+ * @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.
+ * @param sliceZero the position of the first element.
+ * @param rowZero the position of the first element.
+ * @param columnZero the position of the first element.
+ * @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 columnStride 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*rows*columnss > Integer.MAX_VALUE</tt>.
+ * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+ */
+ protected void setUp(int slices, int rows, int columns, int sliceZero, int rowZero, int columnZero, int sliceStride,
+ int rowStride, int columnStride) {
+ if (slices < 0 || rows < 0 || columns < 0) {
+ throw new IllegalArgumentException("negative size");
+ }
+ if ((double) slices * rows * columns > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("matrix too large");
+ }
+
+ this.slices = slices;
+ this.rows = rows;
+ this.columns = columns;
+
+ this.sliceZero = sliceZero;
+ this.rowZero = rowZero;
+ this.columnZero = columnZero;
+
+ this.sliceStride = sliceStride;
+ this.rowStride = rowStride;
+ this.columnStride = columnStride;
+
+ this.isNoView = true;
+ }
+
+ protected int[] shape() {
+ int[] shape = new int[3];
+ shape[0] = slices;
+ shape[1] = rows;
+ shape[2] = columns;
+ return shape;
+ }
+
+ /** Returns the number of cells which is <tt>slices()*rows()*columns()</tt>. */
+ @Override
+ public int size() {
+ return slices * rows * columns;
+ }
+
+ /** Returns the number of slices. */
+ public int slices() {
+ return slices;
+ }
+
+ /** Returns a string representation of the receiver's shape. */
+ public String toStringShort() {
+ return AbstractFormatter.shape(this);
+ }
+
+ /** Self modifying version of viewColumnFlip(). */
+ protected AbstractMatrix3D vColumnFlip() {
+ if (columns > 0) {
+ columnZero += (columns - 1) * columnStride;
+ columnStride = -columnStride;
+ this.isNoView = false;
+ }
+ return this;
+ }
+
+ /**
+ * Self modifying version of viewDice().
+ *
+ * @throws IllegalArgumentException if some of the parameters are equal or not in range 0..2.
+ */
+ protected AbstractMatrix3D vDice(int axis0, int axis1, int axis2) {
+ int d = 3;
+ if (axis0 < 0 || axis0 >= d || axis1 < 0 || axis1 >= d || axis2 < 0 || axis2 >= d ||
+ axis0 == axis1 || axis0 == axis2 || axis1 == axis2) {
+ throw new IllegalArgumentException("Illegal Axes: " + axis0 + ", " + axis1 + ", " + axis2);
+ }
+
+ // swap shape
+ int[] shape = shape();
+
+ this.slices = shape[axis0];
+ this.rows = shape[axis1];
+ this.columns = shape[axis2];
+
+ // swap strides
+ int[] strides = new int[3];
+ strides[0] = this.sliceStride;
+ strides[1] = this.rowStride;
+ strides[2] = this.columnStride;
+
+ this.sliceStride = strides[axis0];
+ this.rowStride = strides[axis1];
+ this.columnStride = strides[axis2];
+
+ this.isNoView = false;
+ return this;
+ }
+
+ /**
+ * Self modifying version of viewPart().
+ *
+ * @throws IndexOutOfBoundsException if <tt>slice<0 || depth<0 || slice+depth>slices() || row<0 || height<0 ||
+ * row+height>rows() || column<0 || width<0 || column+width>columns()</tt>
+ */
+ protected AbstractMatrix3D vPart(int slice, int row, int column, int depth, int height, int width) {
+ checkBox(slice, row, column, depth, height, width);
+
+ this.sliceZero += this.sliceStride * slice;
+ this.rowZero += this.rowStride * row;
+ this.columnZero += this.columnStride * column;
+
+ this.slices = depth;
+ this.rows = height;
+ this.columns = width;
+
+ this.isNoView = false;
+ return this;
+ }
+
+ /** Self modifying version of viewRowFlip(). */
+ protected AbstractMatrix3D vRowFlip() {
+ if (rows > 0) {
+ rowZero += (rows - 1) * rowStride;
+ rowStride = -rowStride;
+ this.isNoView = false;
+ }
+ return this;
+ }
+
+ /** Self modifying version of viewSliceFlip(). */
+ protected AbstractMatrix3D vSliceFlip() {
+ if (slices > 0) {
+ sliceZero += (slices - 1) * sliceStride;
+ sliceStride = -sliceStride;
+ this.isNoView = false;
+ }
+ return this;
+ }
+
+ /**
+ * Self modifying version of viewStrides().
+ *
+ * @throws IndexOutOfBoundsException if <tt>sliceStride<=0 || rowStride<=0 || columnStride<=0</tt>.
+ */
+ protected AbstractMatrix3D vStrides(int sliceStride, int rowStride, int columnStride) {
+ if (sliceStride <= 0 || rowStride <= 0 || columnStride <= 0) {
+ throw new IndexOutOfBoundsException("illegal strides: " + sliceStride + ", " + rowStride + ", " + columnStride);
+ }
+
+ this.sliceStride *= sliceStride;
+ this.rowStride *= rowStride;
+ this.columnStride *= columnStride;
+
+ if (this.slices != 0) {
+ this.slices = (this.slices - 1) / sliceStride + 1;
+ }
+ if (this.rows != 0) {
+ this.rows = (this.rows - 1) / rowStride + 1;
+ }
+ if (this.columns != 0) {
+ this.columns = (this.columns - 1) / columnStride + 1;
+ }
+
+ this.isNoView = false;
+ return this;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/AbstractMatrix3D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DelegateDoubleMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DelegateDoubleMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DelegateDoubleMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DelegateDoubleMatrix1D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,100 @@
+/*
+Copyright � 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 org.apache.mahout.math.matrix.impl;
+
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/**
+ * 1-d matrix holding <tt>double</tt> elements; either a view wrapping another 2-d matrix and therefore delegating calls
+ * to it.
+ *
+ * @author wolfgang.hoschek@cern.ch
+ * @version 1.0, 09/24/99
+ */
+class DelegateDoubleMatrix1D extends WrapperDoubleMatrix1D {
+ /*
+ * The elements of the matrix.
+ */
+ private DoubleMatrix2D content;
+ /*
+ * The row this view is bound to.
+ */
+ private int row;
+
+ DelegateDoubleMatrix1D(DoubleMatrix2D newContent, int row) {
+ super(null);
+ if (row < 0 || row >= newContent.rows()) {
+ throw new IllegalArgumentException();
+ }
+ setUp(newContent.columns());
+ this.row = row;
+ this.content = newContent;
+ }
+
+ /**
+ * Returns the matrix cell value at coordinate <tt>index</tt>.
+ *
+ * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. <b>You
+ * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+ * (unchecked): <tt>index<0 || index>=size()</tt>.
+ *
+ * @param index the index of the cell.
+ * @return the value of the specified cell.
+ */
+ @Override
+ public double getQuick(int index) {
+ return content.getQuick(row, index);
+ }
+
+ /**
+ * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified
+ * size. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must also be
+ * of type <tt>DenseDoubleMatrix1D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new
+ * matrix must also be of type <tt>SparseDoubleMatrix1D</tt>, 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.
+ */
+ @Override
+ public DoubleMatrix1D like(int size) {
+ return content.like1D(size);
+ }
+
+ /**
+ * Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the
+ * receiver. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must be
+ * of type <tt>DenseDoubleMatrix2D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new
+ * matrix must be of type <tt>SparseDoubleMatrix2D</tt>, 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.
+ */
+ @Override
+ public DoubleMatrix2D like2D(int rows, int columns) {
+ return content.like(rows, columns);
+ }
+
+ /**
+ * Sets the matrix cell at coordinate <tt>index</tt> to the specified value.
+ *
+ * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception. <b>You
+ * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+ * (unchecked): <tt>index<0 || index>=size()</tt>.
+ *
+ * @param index the index of the cell.
+ * @param value the value to be filled into the specified cell.
+ */
+ @Override
+ public void setQuick(int index, double value) {
+ content.setQuick(row, index, value);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DelegateDoubleMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseDoubleMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseDoubleMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseDoubleMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseDoubleMatrix1D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,547 @@
+/*
+Copyright � 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 org.apache.mahout.math.matrix.impl;
+
+import org.apache.mahout.math.function.DoubleFunction;
+import org.apache.mahout.math.jet.math.Mult;
+import org.apache.mahout.math.jet.math.PlusMult;
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
+@Deprecated
+public class DenseDoubleMatrix1D extends DoubleMatrix1D {
+
+ /** The elements of this matrix. */
+ protected final 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.
+ *
+ * @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 <tt>0</tt>.
+ *
+ * @param size the number of cells the matrix shall have.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+ 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. <tt>index(i+1)-index(i)</tt>.
+ * @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;
+ }
+
+ /**
+ * Sets all cells to the state specified by <tt>values</tt>. <tt>values</tt> is required to have the same number of
+ * cells as the receiver. <p> The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the
+ * matrix, and vice-versa.
+ *
+ * @param values the values to be filled into the cells.
+ * @return <tt>this</tt> (for convenience only).
+ * @throws IllegalArgumentException if <tt>values.length != size()</tt>.
+ */
+ @Override
+ 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 <tt>value</tt>.
+ *
+ * @param value the value to be filled into the cells.
+ * @return <tt>this</tt> (for convenience only).
+ */
+ @Override
+ 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; <tt>x[i] = function(x[i])</tt>. (Iterates downwards from
+ * <tt>[size()-1]</tt> to <tt>[0]</tt>). <p> <b>Example:</b>
+ * <pre>
+ * // change each cell to its sine
+ * matrix = 0.5 1.5 2.5 3.5
+ * matrix.assign(Functions.sin);
+ * -->
+ * matrix == 0.479426 0.997495 0.598472 -0.350783
+ * </pre>
+ * For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>.
+ *
+ * @param function a function object taking as argument the current cell's value.
+ * @return <tt>this</tt> (for convenience only).
+ * @see org.apache.mahout.math.jet.math.Functions
+ */
+ @Override
+ public DoubleMatrix1D assign(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 Mult) { // x[i] = mult*x[i]
+ double multiplicator = ((Mult) function).getMultiplicator();
+ 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 <i>as if</i> using an intermediate auxiliary deep copy of <tt>other</tt>.
+ *
+ * @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>.
+ */
+ @Override
+ 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;
+ }
+
+ double[] elems = this.elements;
+ 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; <tt>x[i] = function(x[i],y[i])</tt>. (Iterates downwards from
+ * <tt>[size()-1]</tt> to <tt>[0]</tt>). <p> <b>Example:</b>
+ * <pre>
+ * // assign x[i] = x[i]<sup>y[i]</sup>
+ * m1 = 0 1 2 3;
+ * m2 = 0 2 4 6;
+ * m1.assign(m2, org.apache.mahout.math.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); }
+ * }
+ * );
+ * </pre>
+ * For further examples, see the <a href="package-summary.html#FunctionObjects">package doc</a>.
+ *
+ * @param y the secondary matrix to operate on.
+ * @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>.
+ * @see org.apache.mahout.math.jet.math.Functions
+ */
+ @Override
+ public DoubleMatrix1D assign(DoubleMatrix1D y, org.apache.mahout.math.function.DoubleDoubleFunction function) {
+ // overriden for performance only
+ if (!(y instanceof DenseDoubleMatrix1D)) {
+ return super.assign(y, function);
+ }
+ DenseDoubleMatrix1D other = (DenseDoubleMatrix1D) y;
+ checkSize(y);
+ double[] elems = this.elements;
+ 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 == org.apache.mahout.math.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.math.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 PlusMult) {
+ double multiplicator = ((PlusMult) function).getMultiplicator();
+ 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. */
+ @Override
+ 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 <tt>index</tt>.
+ *
+ * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. <b>You
+ * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+ * (unchecked): <tt>index<0 || index>=size()</tt>.
+ *
+ * @param index the index of the cell.
+ * @return the value of the specified cell.
+ */
+ @Override
+ 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 <tt>true</tt> if both matrices share at least one identical cell. */
+ @Override
+ 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.
+ */
+ @Override
+ 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 <i>of the same dynamic type</i> as the receiver, having the specified
+ * size. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must also be
+ * of type <tt>DenseDoubleMatrix1D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new
+ * matrix must also be of type <tt>SparseDoubleMatrix1D</tt>, 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.
+ */
+ @Override
+ public DoubleMatrix1D like(int 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. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must be
+ * of type <tt>DenseDoubleMatrix2D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new
+ * matrix must be of type <tt>SparseDoubleMatrix2D</tt>, 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.
+ */
+ @Override
+ public DoubleMatrix2D like2D(int rows, int columns) {
+ return new DenseDoubleMatrix2D(rows, columns);
+ }
+
+ /**
+ * Sets the matrix cell at coordinate <tt>index</tt> to the specified value.
+ *
+ * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception. <b>You
+ * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+ * (unchecked): <tt>index<0 || index>=size()</tt>.
+ *
+ * @param index the index of the cell.
+ * @param value the value to be filled into the specified cell.
+ */
+ @Override
+ 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 <tt>this[i]</tt> with <tt>other[i]</tt>.
+ *
+ * @throws IllegalArgumentException if <tt>size() != other.size()</tt>.
+ */
+ @Override
+ 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);
+
+ double[] elems = this.elements;
+ 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;
+ }
+ }
+
+ /**
+ * Fills the cell values into the specified 1-dimensional array. The values are copied. So subsequent changes in
+ * <tt>values</tt> are not reflected in the matrix, and vice-versa. After this call returns the array <tt>values</tt>
+ * has the form <br> <tt>for (int i=0; i < size(); i++) values[i] = get(i);</tt>
+ *
+ * @throws IllegalArgumentException if <tt>values.length < size()</tt>.
+ */
+ @Override
+ 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.
+ */
+ @Override
+ protected DoubleMatrix1D viewSelectionLike(int[] 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>. Where <tt>x == this</tt>.
+ * Operates on cells at indexes <tt>from .. Min(size(),y.size(),from+length)-1</tt>.
+ *
+ * @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 <tt>from<0 || length<0</tt>.
+ */
+ @Override
+ 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;
+ double[] elems = this.elements;
+ double[] yElems = yy.elements;
+ if (elems == null || yElems == null) {
+ throw new InternalError();
+ }
+
+ /*
+ // unoptimized
+ for (int k = min; --k >= 0;) {
+ sum += elems[i] * yElems[j];
+ i += s;
+ j += ys;
+ }
+ */
+
+ // optimized
+ // loop unrolling
+ i -= s;
+ j -= ys;
+ double sum = 0;
+ 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.
+ */
+ @Override
+ public double zSum() {
+ int s = stride;
+ int i = index(0);
+ double[] elems = this.elements;
+ if (elems == null) {
+ throw new InternalError();
+ }
+ double sum = 0;
+ for (int k = size; --k >= 0;) {
+ sum += elems[i];
+ i += s;
+ }
+ return sum;
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseDoubleMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native