http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/Matrices.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/Matrices.java b/math/src/main/java/org/apache/mahout/math/Matrices.java deleted file mode 100644 index 5d8b5c5..0000000 --- a/math/src/main/java/org/apache/mahout/math/Matrices.java +++ /dev/null @@ -1,167 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import com.google.common.base.Preconditions; -import org.apache.mahout.common.RandomUtils; -import org.apache.mahout.math.function.DoubleFunction; -import org.apache.mahout.math.function.Functions; -import org.apache.mahout.math.function.IntIntFunction; - -import java.util.Random; - -public final class Matrices { - - /** - * Create a matrix view based on a function generator. - * <p> - * The generator needs to be idempotent, i.e. returning same value - * for each combination of (row, column) argument sent to generator's - * {@link IntIntFunction#apply(int, int)} call. - * - * @param rows Number of rows in a view - * @param columns Number of columns in a view. - * @param gf view generator - * @param denseLike type of matrix returne dby {@link org.apache.mahout.math.Matrix#like()}. - * @return new matrix view. - */ - public static Matrix functionalMatrixView(final int rows, - final int columns, - final IntIntFunction gf, - final boolean denseLike) { - return new FunctionalMatrixView(rows, columns, gf, denseLike); - } - - /** - * Shorter form of {@link Matrices#functionalMatrixView(int, int, - * org.apache.mahout.math.function.IntIntFunction, boolean)}. - */ - public static Matrix functionalMatrixView(final int rows, - final int columns, - final IntIntFunction gf) { - return new FunctionalMatrixView(rows, columns, gf); - } - - /** - * A read-only transposed view of a matrix argument. - * - * @param m original matrix - * @return transposed view of original matrix - */ - public static Matrix transposedView(final Matrix m) { - - Preconditions.checkArgument(!(m instanceof SparseColumnMatrix)); - - if (m instanceof TransposedMatrixView) { - return ((TransposedMatrixView) m).getDelegate(); - } else { - return new TransposedMatrixView(m); - } - } - - /** - * Random Gaussian matrix view. - * - * @param seed generator seed - */ - public static Matrix gaussianView(final int rows, - final int columns, - long seed) { - return functionalMatrixView(rows, columns, gaussianGenerator(seed), true); - } - - - /** - * Matrix view based on uniform [-1,1) distribution. - * - * @param seed generator seed - */ - public static Matrix symmetricUniformView(final int rows, - final int columns, - int seed) { - return functionalMatrixView(rows, columns, uniformSymmetricGenerator(seed), true); - } - - /** - * Matrix view based on uniform [0,1) distribution. - * - * @param seed generator seed - */ - public static Matrix uniformView(final int rows, - final int columns, - int seed) { - return functionalMatrixView(rows, columns, uniformGenerator(seed), true); - } - - /** - * Generator for a matrix populated by random Gauissian values (Gaussian matrix view) - * - * @param seed The seed for the matrix. - * @return Gaussian {@link IntIntFunction} generating matrix view with normal values - */ - public static IntIntFunction gaussianGenerator(final long seed) { - final Random rnd = RandomUtils.getRandom(seed); - return new IntIntFunction() { - @Override - public double apply(int first, int second) { - rnd.setSeed(seed ^ (((long) first << 32) | (second & 0xffffffffL))); - return rnd.nextGaussian(); - } - }; - } - - private static final double UNIFORM_DIVISOR = Math.pow(2.0, 64); - - /** - * Uniform [-1,1) matrix generator function. - * <p> - * WARNING: to keep things performant, it is stateful and so not thread-safe. - * You'd need to create a copy per thread (with same seed) if shared between threads. - * - * @param seed - random seed initializer - * @return Uniform {@link IntIntFunction} generator - */ - public static IntIntFunction uniformSymmetricGenerator(final int seed) { - return new IntIntFunction() { - private byte[] data = new byte[8]; - - @Override - public double apply(int row, int column) { - long d = ((long) row << Integer.SIZE) | (column & 0xffffffffL); - for (int i = 0; i < 8; i++, d >>>= 8) data[i] = (byte) d; - long hash = MurmurHash.hash64A(data, seed); - return hash / UNIFORM_DIVISOR; - } - }; - } - - /** - * Uniform [0,1) matrix generator function - * - * @param seed generator seed - */ - public static IntIntFunction uniformGenerator(final int seed) { - return Functions.chain(new DoubleFunction() { - @Override - public double apply(double x) { - return (x + 1.0) / 2.0; - } - }, uniformSymmetricGenerator(seed)); - } - -}
http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/Matrix.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/Matrix.java b/math/src/main/java/org/apache/mahout/math/Matrix.java deleted file mode 100644 index 57fab78..0000000 --- a/math/src/main/java/org/apache/mahout/math/Matrix.java +++ /dev/null @@ -1,413 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import org.apache.mahout.math.flavor.MatrixFlavor; -import org.apache.mahout.math.function.DoubleDoubleFunction; -import org.apache.mahout.math.function.DoubleFunction; -import org.apache.mahout.math.function.VectorFunction; - -import java.util.Map; - -/** The basic interface including numerous convenience functions */ -public interface Matrix extends Cloneable, VectorIterable { - - /** @return a formatted String suitable for output */ - String asFormatString(); - - /** - * Assign the value to all elements of the receiver - * - * @param value a double value - * @return the modified receiver - */ - Matrix assign(double value); - - /** - * Assign the values to the receiver - * - * @param values a double[] of values - * @return the modified receiver - * @throws CardinalityException if the cardinalities differ - */ - Matrix assign(double[][] values); - - /** - * Assign the other vector values to the receiver - * - * @param other a Matrix - * @return the modified receiver - * @throws CardinalityException if the cardinalities differ - */ - Matrix assign(Matrix other); - - /** - * Apply the function to each element of the receiver - * - * @param function a DoubleFunction to apply - * @return the modified receiver - */ - Matrix assign(DoubleFunction function); - - /** - * Apply the function to each element of the receiver and the corresponding element of the other argument - * - * @param other a Matrix containing the second arguments to the function - * @param function a DoubleDoubleFunction to apply - * @return the modified receiver - * @throws CardinalityException if the cardinalities differ - */ - Matrix assign(Matrix other, DoubleDoubleFunction function); - - /** - * Assign the other vector values to the column of the receiver - * - * @param column the int row to assign - * @param other a Vector - * @return the modified receiver - * @throws CardinalityException if the cardinalities differ - */ - Matrix assignColumn(int column, Vector other); - - /** - * Assign the other vector values to the row of the receiver - * - * @param row the int row to assign - * @param other a Vector - * @return the modified receiver - * @throws CardinalityException if the cardinalities differ - */ - Matrix assignRow(int row, Vector other); - - /** - * Collects the results of a function applied to each row of a matrix. - * @param f The function to be applied to each row. - * @return The vector of results. - */ - Vector aggregateRows(VectorFunction f); - - /** - * Collects the results of a function applied to each column of a matrix. - * @param f The function to be applied to each column. - * @return The vector of results. - */ - Vector aggregateColumns(VectorFunction f); - - /** - * Collects the results of a function applied to each element of a matrix and then - * aggregated. - * @param combiner A function that combines the results of the mapper. - * @param mapper A function to apply to each element. - * @return The result. - */ - double aggregate(DoubleDoubleFunction combiner, DoubleFunction mapper); - - /** - * @return The number of rows in the matrix. - */ - int columnSize(); - - /** - * @return Returns the number of rows in the matrix. - */ - int rowSize(); - - /** - * Return a copy of the recipient - * - * @return a new Matrix - */ - Matrix clone(); - - /** - * Returns matrix determinator using Laplace theorem - * - * @return a matrix determinator - */ - double determinant(); - - /** - * Return a new matrix containing the values of the recipient divided by the argument - * - * @param x a double value - * @return a new Matrix - */ - Matrix divide(double x); - - /** - * Return the value at the given indexes - * - * @param row an int row index - * @param column an int column index - * @return the double at the index - * @throws IndexException if the index is out of bounds - */ - double get(int row, int column); - - /** - * Return the value at the given indexes, without checking bounds - * - * @param row an int row index - * @param column an int column index - * @return the double at the index - */ - double getQuick(int row, int column); - - /** - * Return an empty matrix of the same underlying class as the receiver - * - * @return a Matrix - */ - Matrix like(); - - /** - * Returns an empty matrix of the same underlying class as the receiver and of the specified size. - * - * @param rows the int number of rows - * @param columns the int number of columns - */ - Matrix like(int rows, int columns); - - /** - * Return a new matrix containing the element by element difference of the recipient and the argument - * - * @param x a Matrix - * @return a new Matrix - * @throws CardinalityException if the cardinalities differ - */ - Matrix minus(Matrix x); - - /** - * Return a new matrix containing the sum of each value of the recipient and the argument - * - * @param x a double - * @return a new Matrix - */ - Matrix plus(double x); - - /** - * Return a new matrix containing the element by element sum of the recipient and the argument - * - * @param x a Matrix - * @return a new Matrix - * @throws CardinalityException if the cardinalities differ - */ - Matrix plus(Matrix x); - - /** - * Set the value at the given index - * - * @param row an int row index into the receiver - * @param column an int column index into the receiver - * @param value a double value to set - * @throws IndexException if the index is out of bounds - */ - void set(int row, int column, double value); - - void set(int row, double[] data); - - /** - * Set the value at the given index, without checking bounds - * - * @param row an int row index into the receiver - * @param column an int column index into the receiver - * @param value a double value to set - */ - void setQuick(int row, int column, double value); - - /** - * Return the number of values in the recipient - * - * @return an int[2] containing [row, column] count - */ - int[] getNumNondefaultElements(); - - /** - * Return a new matrix containing the product of each value of the recipient and the argument - * - * @param x a double argument - * @return a new Matrix - */ - Matrix times(double x); - - /** - * Return a new matrix containing the product of the recipient and the argument - * - * @param x a Matrix argument - * @return a new Matrix - * @throws CardinalityException if the cardinalities are incompatible - */ - Matrix times(Matrix x); - - /** - * Return a new matrix that is the transpose of the receiver - * - * @return the transpose - */ - Matrix transpose(); - - /** - * Return the sum of all the elements of the receiver - * - * @return a double - */ - double zSum(); - - /** - * Return a map of the current column label bindings of the receiver - * - * @return a {@code Map<String, Integer>} - */ - Map<String, Integer> getColumnLabelBindings(); - - /** - * Return a map of the current row label bindings of the receiver - * - * @return a {@code Map<String, Integer>} - */ - Map<String, Integer> getRowLabelBindings(); - - /** - * Sets a map of column label bindings in the receiver - * - * @param bindings a {@code Map<String, Integer>} of label bindings - */ - void setColumnLabelBindings(Map<String, Integer> bindings); - - /** - * Sets a map of row label bindings in the receiver - * - * @param bindings a {@code Map<String, Integer>} of label bindings - */ - void setRowLabelBindings(Map<String, Integer> bindings); - - /** - * Return the value at the given labels - * - * @param rowLabel a String row label - * @param columnLabel a String column label - * @return the double at the index - * - * @throws IndexException if the index is out of bounds - */ - double get(String rowLabel, String columnLabel); - - /** - * Set the value at the given index - * - * @param rowLabel a String row label - * @param columnLabel a String column label - * @param value a double value to set - * @throws IndexException if the index is out of bounds - */ - void set(String rowLabel, String columnLabel, double value); - - /** - * Set the value at the given index, updating the row and column label bindings - * - * @param rowLabel a String row label - * @param columnLabel a String column label - * @param row an int row index - * @param column an int column index - * @param value a double value - */ - void set(String rowLabel, String columnLabel, int row, int column, double value); - - /** - * Sets the row values at the given row label - * - * @param rowLabel a String row label - * @param rowData a double[] array of row data - */ - void set(String rowLabel, double[] rowData); - - /** - * Sets the row values at the given row index and updates the row labels - * - * @param rowLabel the String row label - * @param row an int the row index - * @param rowData a double[] array of row data - */ - void set(String rowLabel, int row, double[] rowData); - - /* - * Need stories for these but keeping them here for now. - * - */ - // void getNonZeros(IntArrayList jx, DoubleArrayList values); - // void foreachNonZero(IntDoubleFunction f); - // double aggregate(DoubleDoubleFunction aggregator, DoubleFunction map); - // double aggregate(Matrix other, DoubleDoubleFunction aggregator, - // DoubleDoubleFunction map); - // NewMatrix assign(Matrix y, DoubleDoubleFunction function, IntArrayList - // nonZeroIndexes); - - /** - * Return a view into part of a matrix. Changes to the view will change the - * original matrix. - * - * @param offset an int[2] offset into the receiver - * @param size the int[2] size of the desired result - * @return a matrix that shares storage with part of the original matrix. - * @throws CardinalityException if the length is greater than the cardinality of the receiver - * @throws IndexException if the offset is negative or the offset+length is outside of the receiver - */ - Matrix viewPart(int[] offset, int[] size); - - /** - * Return a view into part of a matrix. Changes to the view will change the - * original matrix. - * - * @param rowOffset The first row of the view - * @param rowsRequested The number of rows in the view - * @param columnOffset The first column in the view - * @param columnsRequested The number of columns in the view - * @return a matrix that shares storage with part of the original matrix. - * @throws CardinalityException if the length is greater than the cardinality of the receiver - * @throws IndexException if the offset is negative or the offset+length is outside of the - * receiver - */ - Matrix viewPart(int rowOffset, int rowsRequested, int columnOffset, int columnsRequested); - - /** - * Return a reference to a row. Changes to the view will change the original matrix. - * @param row The index of the row to return. - * @return A vector that shares storage with the original. - */ - Vector viewRow(int row); - - /** - * Return a reference to a column. Changes to the view will change the original matrix. - * @param column The index of the column to return. - * @return A vector that shares storage with the original. - */ - Vector viewColumn(int column); - - /** - * Returns a reference to the diagonal of a matrix. Changes to the view will change - * the original matrix. - * @return A vector that shares storage with the original matrix. - */ - Vector viewDiagonal(); - - /** - * Get matrix structural flavor (operations performance hints). This is optional operation, may - * throw {@link java.lang.UnsupportedOperationException}. - */ - MatrixFlavor getFlavor(); -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/MatrixSlice.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/MatrixSlice.java b/math/src/main/java/org/apache/mahout/math/MatrixSlice.java deleted file mode 100644 index 51378c1..0000000 --- a/math/src/main/java/org/apache/mahout/math/MatrixSlice.java +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -public class MatrixSlice extends DelegatingVector { - private int index; - - public MatrixSlice(Vector v, int index) { - super(v); - this.index = index; - } - - public Vector vector() { - return getVector(); - } - - public int index() { - return index; - } -} - http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java b/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java deleted file mode 100644 index 30d2afb..0000000 --- a/math/src/main/java/org/apache/mahout/math/MatrixTimesOps.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -/** - * Optional interface for optimized matrix multiplications. - * Some concrete Matrix implementations may mix this in. - */ -public interface MatrixTimesOps { - /** - * computes matrix product of (this * that) - */ - Matrix timesRight(Matrix that); - - /** - * Computes matrix product of (that * this) - */ - Matrix timesLeft(Matrix that); - -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/MatrixVectorView.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/MatrixVectorView.java b/math/src/main/java/org/apache/mahout/math/MatrixVectorView.java deleted file mode 100644 index 6ad44b5..0000000 --- a/math/src/main/java/org/apache/mahout/math/MatrixVectorView.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import java.util.Iterator; -import java.util.NoSuchElementException; - -/** - * Provides a virtual vector that is really a row or column or diagonal of a matrix. - */ -public class MatrixVectorView extends AbstractVector { - private Matrix matrix; - private int row; - private int column; - private int rowStride; - private int columnStride; - private boolean isDense = true; - - public MatrixVectorView(Matrix matrix, int row, int column, int rowStride, int columnStride, boolean isDense) { - this(matrix, row, column, rowStride, columnStride); - this.isDense = isDense; - } - - public MatrixVectorView(Matrix matrix, int row, int column, int rowStride, int columnStride) { - super(viewSize(matrix, row, column, rowStride, columnStride)); - if (row < 0 || row >= matrix.rowSize()) { - throw new IndexException(row, matrix.rowSize()); - } - if (column < 0 || column >= matrix.columnSize()) { - throw new IndexException(column, matrix.columnSize()); - } - - this.matrix = matrix; - this.row = row; - this.column = column; - this.rowStride = rowStride; - this.columnStride = columnStride; - } - - private static int viewSize(Matrix matrix, int row, int column, int rowStride, int columnStride) { - if (rowStride != 0 && columnStride != 0) { - int n1 = (matrix.numRows() - row) / rowStride; - int n2 = (matrix.numCols() - column) / columnStride; - return Math.min(n1, n2); - } else if (rowStride > 0) { - return (matrix.numRows() - row) / rowStride; - } else { - return (matrix.numCols() - column) / columnStride; - } - } - - /** - * @return true iff the {@link Vector} implementation should be considered - * dense -- that it explicitly represents every value - */ - @Override - public boolean isDense() { - return isDense; - } - - /** - * @return true iff {@link Vector} should be considered to be iterable in - * index order in an efficient way. In particular this implies that {@link #iterator()} and - * {@link #iterateNonZero()} return elements in ascending order by index. - */ - @Override - public boolean isSequentialAccess() { - return true; - } - - /** - * Iterates over all elements <p> - * NOTE: Implementations may choose to reuse the Element returned - * for performance reasons, so if you need a copy of it, you should call {@link #getElement(int)} for - * the given index - * - * @return An {@link java.util.Iterator} over all elements - */ - @Override - public Iterator<Element> iterator() { - final LocalElement r = new LocalElement(0); - return new Iterator<Element>() { - private int i; - - @Override - public boolean hasNext() { - return i < size(); - } - - @Override - public Element next() { - if (i >= size()) { - throw new NoSuchElementException(); - } - r.index = i++; - return r; - } - - @Override - public void remove() { - throw new UnsupportedOperationException("Can't remove from a view"); - } - }; - } - - /** - * Iterates over all non-zero elements. <p> - * NOTE: Implementations may choose to reuse the Element - * returned for performance reasons, so if you need a copy of it, you should call {@link - * #getElement(int)} for the given index - * - * @return An {@link java.util.Iterator} over all non-zero elements - */ - @Override - public Iterator<Element> iterateNonZero() { - - return new Iterator<Element>() { - class NonZeroElement implements Element { - int index; - - @Override - public double get() { - return getQuick(index); - } - - @Override - public int index() { - return index; - } - - @Override - public void set(double value) { - invalidateCachedLength(); - setQuick(index, value); - } - } - - private final NonZeroElement element = new NonZeroElement(); - private int index = -1; - private int lookAheadIndex = -1; - - @Override - public boolean hasNext() { - if (lookAheadIndex == index) { // User calls hasNext() after a next() - lookAhead(); - } // else user called hasNext() repeatedly. - return lookAheadIndex < size(); - } - - private void lookAhead() { - lookAheadIndex++; - while (lookAheadIndex < size() && getQuick(lookAheadIndex) == 0.0) { - lookAheadIndex++; - } - } - - @Override - public Element next() { - if (lookAheadIndex == index) { // If user called next() without checking hasNext(). - lookAhead(); - } - - index = lookAheadIndex; - - if (index >= size()) { // If the end is reached. - throw new NoSuchElementException(); - } - - element.index = index; - return element; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - - /** - * Return the value at the given index, without checking bounds - * - * @param index an int index - * @return the double at the index - */ - @Override - public double getQuick(int index) { - return matrix.getQuick(row + rowStride * index, column + columnStride * index); - } - - /** - * Return an empty vector of the same underlying class as the receiver - * - * @return a Vector - */ - @Override - public Vector like() { - return matrix.like(size(), 1).viewColumn(0); - } - - @Override - public Vector like(int cardinality) { - return matrix.like(cardinality, 1).viewColumn(0); - } - - /** - * Set the value at the given index, without checking bounds - * - * @param index an int index into the receiver - * @param value a double value to set - */ - @Override - public void setQuick(int index, double value) { - matrix.setQuick(row + rowStride * index, column + columnStride * index, value); - } - - /** - * Return the number of values in the recipient - * - * @return an int - */ - @Override - public int getNumNondefaultElements() { - return size(); - } - - @Override - public double getLookupCost() { - // TODO: what is a genuine value here? - return 1; - } - - @Override - public double getIteratorAdvanceCost() { - // TODO: what is a genuine value here? - return 1; - } - - @Override - public boolean isAddConstantTime() { - // TODO: what is a genuine value here? - return true; - } - - @Override - protected Matrix matrixLike(int rows, int columns) { - return matrix.like(rows, columns); - } - - @Override - public Vector clone() { - MatrixVectorView r = (MatrixVectorView) super.clone(); - r.matrix = matrix.clone(); - r.row = row; - r.column = column; - r.rowStride = rowStride; - r.columnStride = columnStride; - return r; - } - - /** - * Used internally by assign() to update multiple indices and values at once. - * Only really useful for sparse vectors (especially SequentialAccessSparseVector). - * <p> - * If someone ever adds a new type of sparse vectors, this method must merge (index, value) pairs into the vector. - * - * @param updates a mapping of indices to values to merge in the vector. - */ - @Override - public void mergeUpdates(OrderedIntDoubleMapping updates) { - int[] indices = updates.getIndices(); - double[] values = updates.getValues(); - for (int i = 0; i < updates.getNumMappings(); ++i) { - matrix.setQuick(row + rowStride * indices[i], column + columnStride * indices[i], values[i]); - } - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/MatrixView.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/MatrixView.java b/math/src/main/java/org/apache/mahout/math/MatrixView.java deleted file mode 100644 index 951515b..0000000 --- a/math/src/main/java/org/apache/mahout/math/MatrixView.java +++ /dev/null @@ -1,160 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import org.apache.mahout.math.flavor.MatrixFlavor; - -/** Implements subset view of a Matrix */ -public class MatrixView extends AbstractMatrix { - - private Matrix matrix; - - // the offset into the Matrix - private int[] offset; - - /** - * Construct a view of the matrix with given offset and cardinality - * - * @param matrix an underlying Matrix - * @param offset the int[2] offset into the underlying matrix - * @param size the int[2] size of the view - */ - public MatrixView(Matrix matrix, int[] offset, int[] size) { - super(size[ROW], size[COL]); - int rowOffset = offset[ROW]; - if (rowOffset < 0) { - throw new IndexException(rowOffset, rowSize()); - } - - int rowsRequested = size[ROW]; - if (rowOffset + rowsRequested > matrix.rowSize()) { - throw new IndexException(rowOffset + rowsRequested, matrix.rowSize()); - } - - int columnOffset = offset[COL]; - if (columnOffset < 0) { - throw new IndexException(columnOffset, columnSize()); - } - - int columnsRequested = size[COL]; - if (columnOffset + columnsRequested > matrix.columnSize()) { - throw new IndexException(columnOffset + columnsRequested, matrix.columnSize()); - } - this.matrix = matrix; - this.offset = offset; - } - - @Override - public Matrix clone() { - MatrixView clone = (MatrixView) super.clone(); - clone.matrix = matrix.clone(); - clone.offset = offset.clone(); - return clone; - } - - @Override - public double getQuick(int row, int column) { - return matrix.getQuick(offset[ROW] + row, offset[COL] + column); - } - - @Override - public Matrix like() { - return matrix.like(rowSize(), columnSize()); - } - - @Override - public Matrix like(int rows, int columns) { - return matrix.like(rows, columns); - } - - @Override - public void setQuick(int row, int column, double value) { - matrix.setQuick(offset[ROW] + row, offset[COL] + column, value); - } - - @Override - public int[] getNumNondefaultElements() { - return new int[]{rowSize(), columnSize()}; - - } - - @Override - public Matrix viewPart(int[] offset, int[] size) { - if (offset[ROW] < 0) { - throw new IndexException(offset[ROW], 0); - } - if (offset[ROW] + size[ROW] > rowSize()) { - throw new IndexException(offset[ROW] + size[ROW], rowSize()); - } - if (offset[COL] < 0) { - throw new IndexException(offset[COL], 0); - } - if (offset[COL] + size[COL] > columnSize()) { - throw new IndexException(offset[COL] + size[COL], columnSize()); - } - int[] origin = this.offset.clone(); - origin[ROW] += offset[ROW]; - origin[COL] += offset[COL]; - return new MatrixView(matrix, origin, size); - } - - @Override - public Matrix assignColumn(int column, Vector other) { - if (rowSize() != other.size()) { - throw new CardinalityException(rowSize(), other.size()); - } - for (int row = 0; row < rowSize(); row++) { - matrix.setQuick(row + offset[ROW], column + offset[COL], other - .getQuick(row)); - } - return this; - } - - @Override - public Matrix assignRow(int row, Vector other) { - if (columnSize() != other.size()) { - throw new CardinalityException(columnSize(), other.size()); - } - for (int col = 0; col < columnSize(); col++) { - matrix - .setQuick(row + offset[ROW], col + offset[COL], other.getQuick(col)); - } - return this; - } - - @Override - public Vector viewColumn(int column) { - if (column < 0 || column >= columnSize()) { - throw new IndexException(column, columnSize()); - } - return matrix.viewColumn(column + offset[COL]).viewPart(offset[ROW], rowSize()); - } - - @Override - public Vector viewRow(int row) { - if (row < 0 || row >= rowSize()) { - throw new IndexException(row, rowSize()); - } - return matrix.viewRow(row + offset[ROW]).viewPart(offset[COL], columnSize()); - } - - @Override - public MatrixFlavor getFlavor() { - return matrix.getFlavor(); - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/MurmurHash.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/MurmurHash.java b/math/src/main/java/org/apache/mahout/math/MurmurHash.java deleted file mode 100644 index 13f3a07..0000000 --- a/math/src/main/java/org/apache/mahout/math/MurmurHash.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import com.google.common.primitives.Ints; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -/** - * <p>This is a very fast, non-cryptographic hash suitable for general hash-based - * lookup. See http://murmurhash.googlepages.com/ for more details. - * </p> - * <p>The C version of MurmurHash 2.0 found at that site was ported - * to Java by Andrzej Bialecki (ab at getopt org).</p> - */ -public final class MurmurHash { - - private MurmurHash() {} - - /** - * Hashes an int. - * @param data The int to hash. - * @param seed The seed for the hash. - * @return The 32 bit hash of the bytes in question. - */ - public static int hash(int data, int seed) { - return hash(ByteBuffer.wrap(Ints.toByteArray(data)), seed); - } - - /** - * Hashes bytes in an array. - * @param data The bytes to hash. - * @param seed The seed for the hash. - * @return The 32 bit hash of the bytes in question. - */ - public static int hash(byte[] data, int seed) { - return hash(ByteBuffer.wrap(data), seed); - } - - /** - * Hashes bytes in part of an array. - * @param data The data to hash. - * @param offset Where to start munging. - * @param length How many bytes to process. - * @param seed The seed to start with. - * @return The 32-bit hash of the data in question. - */ - public static int hash(byte[] data, int offset, int length, int seed) { - return hash(ByteBuffer.wrap(data, offset, length), seed); - } - - /** - * Hashes the bytes in a buffer from the current position to the limit. - * @param buf The bytes to hash. - * @param seed The seed for the hash. - * @return The 32 bit murmur hash of the bytes in the buffer. - */ - public static int hash(ByteBuffer buf, int seed) { - // save byte order for later restoration - ByteOrder byteOrder = buf.order(); - buf.order(ByteOrder.LITTLE_ENDIAN); - - int m = 0x5bd1e995; - int r = 24; - - int h = seed ^ buf.remaining(); - - while (buf.remaining() >= 4) { - int k = buf.getInt(); - - k *= m; - k ^= k >>> r; - k *= m; - - h *= m; - h ^= k; - } - - if (buf.remaining() > 0) { - ByteBuffer finish = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN); - // for big-endian version, use this first: - // finish.position(4-buf.remaining()); - finish.put(buf).rewind(); - h ^= finish.getInt(); - h *= m; - } - - h ^= h >>> 13; - h *= m; - h ^= h >>> 15; - - buf.order(byteOrder); - return h; - } - - - public static long hash64A(byte[] data, int seed) { - return hash64A(ByteBuffer.wrap(data), seed); - } - - public static long hash64A(byte[] data, int offset, int length, int seed) { - return hash64A(ByteBuffer.wrap(data, offset, length), seed); - } - - public static long hash64A(ByteBuffer buf, int seed) { - ByteOrder byteOrder = buf.order(); - buf.order(ByteOrder.LITTLE_ENDIAN); - - long m = 0xc6a4a7935bd1e995L; - int r = 47; - - long h = seed ^ (buf.remaining() * m); - - while (buf.remaining() >= 8) { - long k = buf.getLong(); - - k *= m; - k ^= k >>> r; - k *= m; - - h ^= k; - h *= m; - } - - if (buf.remaining() > 0) { - ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN); - // for big-endian version, do this first: - // finish.position(8-buf.remaining()); - finish.put(buf).rewind(); - h ^= finish.getLong(); - h *= m; - } - - h ^= h >>> r; - h *= m; - h ^= h >>> r; - - buf.order(byteOrder); - return h; - } - -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/MurmurHash3.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/MurmurHash3.java b/math/src/main/java/org/apache/mahout/math/MurmurHash3.java deleted file mode 100644 index bd0bb6b..0000000 --- a/math/src/main/java/org/apache/mahout/math/MurmurHash3.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * This code is public domain. - * - * The MurmurHash3 algorithm was created by Austin Appleby and put into the public domain. - * See http://code.google.com/p/smhasher/ - * - * This java port was authored by - * Yonik Seeley and was placed into the public domain per - * https://github.com/yonik/java_util/blob/master/src/util/hash/MurmurHash3.java. - */ - -package org.apache.mahout.math; - -/** - * <p> - * This produces exactly the same hash values as the final C+ - + * version of MurmurHash3 and is thus suitable for producing the same hash values across - * platforms. - * <p> - * The 32 bit x86 version of this hash should be the fastest variant for relatively short keys like ids. - * <p> - * Note - The x86 and x64 versions do _not_ produce the same results, as the - * algorithms are optimized for their respective platforms. - * <p> - * See also http://github.com/yonik/java_util for future updates to this file. - */ -public final class MurmurHash3 { - - private MurmurHash3() {} - - /** Returns the MurmurHash3_x86_32 hash. */ - public static int murmurhash3x8632(byte[] data, int offset, int len, int seed) { - - int c1 = 0xcc9e2d51; - int c2 = 0x1b873593; - - int h1 = seed; - int roundedEnd = offset + (len & 0xfffffffc); // round down to 4 byte block - - for (int i = offset; i < roundedEnd; i += 4) { - // little endian load order - int k1 = (data[i] & 0xff) | ((data[i + 1] & 0xff) << 8) | ((data[i + 2] & 0xff) << 16) | (data[i + 3] << 24); - k1 *= c1; - k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15); - k1 *= c2; - - h1 ^= k1; - h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13); - h1 = h1 * 5 + 0xe6546b64; - } - - // tail - int k1 = 0; - - switch(len & 0x03) { - case 3: - k1 = (data[roundedEnd + 2] & 0xff) << 16; - // fallthrough - case 2: - k1 |= (data[roundedEnd + 1] & 0xff) << 8; - // fallthrough - case 1: - k1 |= data[roundedEnd] & 0xff; - k1 *= c1; - k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15); - k1 *= c2; - h1 ^= k1; - default: - } - - // finalization - h1 ^= len; - - // fmix(h1); - h1 ^= h1 >>> 16; - h1 *= 0x85ebca6b; - h1 ^= h1 >>> 13; - h1 *= 0xc2b2ae35; - h1 ^= h1 >>> 16; - - return h1; - } - -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/NamedVector.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/NamedVector.java b/math/src/main/java/org/apache/mahout/math/NamedVector.java deleted file mode 100644 index d4fa609..0000000 --- a/math/src/main/java/org/apache/mahout/math/NamedVector.java +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import org.apache.mahout.math.function.DoubleDoubleFunction; -import org.apache.mahout.math.function.DoubleFunction; - -public class NamedVector implements Vector { - - private Vector delegate; - private String name; - - public NamedVector() { - } - - public NamedVector(NamedVector other) { - this.delegate = other.getDelegate(); - this.name = other.getName(); - } - - public NamedVector(Vector delegate, String name) { - if (delegate == null || name == null) { - throw new IllegalArgumentException(); - } - this.delegate = delegate; - this.name = name; - } - - public String getName() { - return name; - } - - public Vector getDelegate() { - return delegate; - } - - @Override - public int hashCode() { - return delegate.hashCode(); - } - - /** - * To not break transitivity with other {@link Vector}s, this does not compare name. - */ - @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") - @Override - public boolean equals(Object other) { - return delegate.equals(other); - } - - @SuppressWarnings("CloneDoesntCallSuperClone") - @Override - public NamedVector clone() { - return new NamedVector(delegate.clone(), name); - } - - @Override - public Iterable<Element> all() { - return delegate.all(); - } - - @Override - public Iterable<Element> nonZeroes() { - return delegate.nonZeroes(); - } - - @Override - public String asFormatString() { - return toString(); - } - - @Override - public String toString() { - StringBuilder bldr = new StringBuilder(); - bldr.append(name).append(':').append(delegate.toString()); - return bldr.toString(); - } - - @Override - public Vector assign(double value) { - return delegate.assign(value); - } - - @Override - public Vector assign(double[] values) { - return delegate.assign(values); - } - - @Override - public Vector assign(Vector other) { - return delegate.assign(other); - } - - @Override - public Vector assign(DoubleFunction function) { - return delegate.assign(function); - } - - @Override - public Vector assign(Vector other, DoubleDoubleFunction function) { - return delegate.assign(other, function); - } - - @Override - public Vector assign(DoubleDoubleFunction f, double y) { - return delegate.assign(f, y); - } - - @Override - public int size() { - return delegate.size(); - } - - @Override - public boolean isDense() { - return delegate.isDense(); - } - - @Override - public boolean isSequentialAccess() { - return delegate.isSequentialAccess(); - } - - @Override - public Element getElement(int index) { - return delegate.getElement(index); - } - - /** - * Merge a set of (index, value) pairs into the vector. - * - * @param updates an ordered mapping of indices to values to be merged in. - */ - @Override - public void mergeUpdates(OrderedIntDoubleMapping updates) { - delegate.mergeUpdates(updates); - } - - @Override - public Vector divide(double x) { - return delegate.divide(x); - } - - @Override - public double dot(Vector x) { - return delegate.dot(x); - } - - @Override - public double get(int index) { - return delegate.get(index); - } - - @Override - public double getQuick(int index) { - return delegate.getQuick(index); - } - - @Override - public NamedVector like() { - return new NamedVector(delegate.like(), name); - } - - @Override - public Vector like(int cardinality) { - return new NamedVector(delegate.like(cardinality), name); - } - - @Override - public Vector minus(Vector x) { - return delegate.minus(x); - } - - @Override - public Vector normalize() { - return delegate.normalize(); - } - - @Override - public Vector normalize(double power) { - return delegate.normalize(power); - } - - @Override - public Vector logNormalize() { - return delegate.logNormalize(); - } - - @Override - public Vector logNormalize(double power) { - return delegate.logNormalize(power); - } - - @Override - public double norm(double power) { - return delegate.norm(power); - } - - @Override - public double maxValue() { - return delegate.maxValue(); - } - - @Override - public int maxValueIndex() { - return delegate.maxValueIndex(); - } - - @Override - public double minValue() { - return delegate.minValue(); - } - - @Override - public int minValueIndex() { - return delegate.minValueIndex(); - } - - @Override - public Vector plus(double x) { - return delegate.plus(x); - } - - @Override - public Vector plus(Vector x) { - return delegate.plus(x); - } - - @Override - public void set(int index, double value) { - delegate.set(index, value); - } - - @Override - public void setQuick(int index, double value) { - delegate.setQuick(index, value); - } - - @Override - public void incrementQuick(int index, double increment) { - delegate.incrementQuick(index, increment); - } - - @Override - public int getNumNonZeroElements() { - return delegate.getNumNonZeroElements(); - } - - @Override - public int getNumNondefaultElements() { - return delegate.getNumNondefaultElements(); - } - - @Override - public Vector times(double x) { - return delegate.times(x); - } - - @Override - public Vector times(Vector x) { - return delegate.times(x); - } - - @Override - public Vector viewPart(int offset, int length) { - return delegate.viewPart(offset, length); - } - - @Override - public double zSum() { - return delegate.zSum(); - } - - @Override - public Matrix cross(Vector other) { - return delegate.cross(other); - } - - @Override - public double aggregate(DoubleDoubleFunction aggregator, DoubleFunction map) { - return delegate.aggregate(aggregator, map); - } - - @Override - public double aggregate(Vector other, DoubleDoubleFunction aggregator, DoubleDoubleFunction combiner) { - return delegate.aggregate(other, aggregator, combiner); - } - - @Override - public double getLengthSquared() { - return delegate.getLengthSquared(); - } - - @Override - public double getDistanceSquared(Vector v) { - return delegate.getDistanceSquared(v); - } - - @Override - public double getLookupCost() { - return delegate.getLookupCost(); - } - - @Override - public double getIteratorAdvanceCost() { - return delegate.getIteratorAdvanceCost(); - } - - @Override - public boolean isAddConstantTime() { - return delegate.isAddConstantTime(); - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/OldQRDecomposition.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/OldQRDecomposition.java b/math/src/main/java/org/apache/mahout/math/OldQRDecomposition.java deleted file mode 100644 index e1552e4..0000000 --- a/math/src/main/java/org/apache/mahout/math/OldQRDecomposition.java +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * 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; - -import org.apache.mahout.math.function.Functions; - -import java.util.Locale; - - -/** - For an <tt>m x n</tt> matrix <tt>A</tt> with <tt>m >= n</tt>, the QR decomposition is an <tt>m x n</tt> - orthogonal matrix <tt>Q</tt> and an <tt>n x n</tt> upper triangular matrix <tt>R</tt> so that - <tt>A = Q*R</tt>. - <P> - The QR decompostion always exists, even if the matrix does not have - full rank, so the constructor will never fail. The primary use of the - QR decomposition is in the least squares solution of nonsquare systems - of simultaneous linear equations. This will fail if <tt>isFullRank()</tt> - returns <tt>false</tt>. - */ - -/** partially deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */ -public class OldQRDecomposition implements QR { - - /** Array for internal storage of decomposition. */ - private final Matrix qr; - - /** Row and column dimensions. */ - private final int originalRows; - private final int originalColumns; - - /** Array for internal storage of diagonal of R. */ - private final Vector rDiag; - - /** - * Constructs and returns a new QR decomposition object; computed by Householder reflections; The decomposed matrices - * can be retrieved via instance methods of the returned decomposition object. - * - * @param a A rectangular matrix. - * @throws IllegalArgumentException if {@code A.rows() < A.columns()} - */ - - public OldQRDecomposition(Matrix a) { - - // Initialize. - qr = a.clone(); - originalRows = a.numRows(); - originalColumns = a.numCols(); - rDiag = new DenseVector(originalColumns); - - // precompute and cache some views to avoid regenerating them time and again - Vector[] QRcolumnsPart = new Vector[originalColumns]; - for (int k = 0; k < originalColumns; k++) { - QRcolumnsPart[k] = qr.viewColumn(k).viewPart(k, originalRows - k); - } - - // Main loop. - for (int k = 0; k < originalColumns; k++) { - //DoubleMatrix1D QRcolk = QR.viewColumn(k).viewPart(k,m-k); - // Compute 2-norm of k-th column without under/overflow. - double nrm = 0; - //if (k<m) nrm = QRcolumnsPart[k].aggregate(hypot,F.identity); - - for (int i = k; i < originalRows; i++) { // fixes bug reported by [email protected] - nrm = Algebra.hypot(nrm, qr.getQuick(i, k)); - } - - - if (nrm != 0.0) { - // Form k-th Householder vector. - if (qr.getQuick(k, k) < 0) { - nrm = -nrm; - } - QRcolumnsPart[k].assign(Functions.div(nrm)); - /* - for (int i = k; i < m; i++) { - QR[i][k] /= nrm; - } - */ - - qr.setQuick(k, k, qr.getQuick(k, k) + 1); - - // Apply transformation to remaining columns. - for (int j = k + 1; j < originalColumns; j++) { - Vector QRcolj = qr.viewColumn(j).viewPart(k, originalRows - k); - double s = QRcolumnsPart[k].dot(QRcolj); - /* - // fixes bug reported by John Chambers - DoubleMatrix1D QRcolj = QR.viewColumn(j).viewPart(k,m-k); - double s = QRcolumnsPart[k].zDotProduct(QRcolumns[j]); - double s = 0.0; - for (int i = k; i < m; i++) { - s += QR[i][k]*QR[i][j]; - } - */ - s = -s / qr.getQuick(k, k); - //QRcolumnsPart[j].assign(QRcolumns[k], F.plusMult(s)); - - for (int i = k; i < originalRows; i++) { - qr.setQuick(i, j, qr.getQuick(i, j) + s * qr.getQuick(i, k)); - } - - } - } - rDiag.setQuick(k, -nrm); - } - } - - /** - * Generates and returns the (economy-sized) orthogonal factor <tt>Q</tt>. - * - * @return <tt>Q</tt> - */ - @Override - public Matrix getQ() { - int columns = Math.min(originalColumns, originalRows); - Matrix q = qr.like(originalRows, columns); - for (int k = columns - 1; k >= 0; k--) { - Vector QRcolk = qr.viewColumn(k).viewPart(k, originalRows - k); - q.set(k, k, 1); - for (int j = k; j < columns; j++) { - if (qr.get(k, k) != 0) { - Vector Qcolj = q.viewColumn(j).viewPart(k, originalRows - k); - double s = -QRcolk.dot(Qcolj) / qr.get(k, k); - Qcolj.assign(QRcolk, Functions.plusMult(s)); - } - } - } - return q; - } - - /** - * Returns the upper triangular factor, <tt>R</tt>. - * - * @return <tt>R</tt> - */ - @Override - public Matrix getR() { - int rows = Math.min(originalRows, originalColumns); - Matrix r = qr.like(rows, originalColumns); - for (int i = 0; i < rows; i++) { - for (int j = 0; j < originalColumns; j++) { - if (i < j) { - r.setQuick(i, j, qr.getQuick(i, j)); - } else if (i == j) { - r.setQuick(i, j, rDiag.getQuick(i)); - } else { - r.setQuick(i, j, 0); - } - } - } - return r; - } - - /** - * Returns whether the matrix <tt>A</tt> has full rank. - * - * @return true if <tt>R</tt>, and hence <tt>A</tt>, has full rank. - */ - @Override - public boolean hasFullRank() { - for (int j = 0; j < originalColumns; j++) { - if (rDiag.getQuick(j) == 0) { - return false; - } - } - return true; - } - - /** - * Least squares solution of <tt>A*X = B</tt>; <tt>returns X</tt>. - * - * @param B A matrix with as many rows as <tt>A</tt> and any number of columns. - * @return <tt>X</tt> that minimizes the two norm of <tt>Q*R*X - B</tt>. - * @throws IllegalArgumentException if <tt>B.rows() != A.rows()</tt>. - */ - @Override - public Matrix solve(Matrix B) { - if (B.numRows() != originalRows) { - throw new IllegalArgumentException("Matrix row dimensions must agree."); - } - - int columns = B.numCols(); - Matrix x = B.like(originalColumns, columns); - - // this can all be done a bit more efficiently if we don't actually - // form explicit versions of Q^T and R but this code isn't soo bad - // and it is much easier to understand - Matrix qt = getQ().transpose(); - Matrix y = qt.times(B); - - Matrix r = getR(); - for (int k = Math.min(originalColumns, originalRows) - 1; k >= 0; k--) { - // X[k,] = Y[k,] / R[k,k], note that X[k,] starts with 0 so += is same as = - x.viewRow(k).assign(y.viewRow(k), Functions.plusMult(1 / r.get(k, k))); - - // Y[0:(k-1),] -= R[0:(k-1),k] * X[k,] - Vector rColumn = r.viewColumn(k).viewPart(0, k); - for (int c = 0; c < columns; c++) { - y.viewColumn(c).viewPart(0, k).assign(rColumn, Functions.plusMult(-x.get(k, c))); - } - } - return x; - } - - /** - * Returns a rough string rendition of a QR. - */ - @Override - public String toString() { - return String.format(Locale.ENGLISH, "QR(%d,%d,fullRank=%s)", originalColumns, originalRows, hasFullRank()); - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/OrderedIntDoubleMapping.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/OrderedIntDoubleMapping.java b/math/src/main/java/org/apache/mahout/math/OrderedIntDoubleMapping.java deleted file mode 100644 index 7c6ad11..0000000 --- a/math/src/main/java/org/apache/mahout/math/OrderedIntDoubleMapping.java +++ /dev/null @@ -1,265 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import java.io.Serializable; - -public final class OrderedIntDoubleMapping implements Serializable, Cloneable { - - static final double DEFAULT_VALUE = 0.0; - - private int[] indices; - private double[] values; - private int numMappings; - - // If true, doesn't allow DEFAULT_VALUEs in the mapping (adding a zero discards it). Otherwise, a DEFAULT_VALUE is - // treated like any other value. - private boolean noDefault = true; - - OrderedIntDoubleMapping(boolean noDefault) { - this(); - this.noDefault = noDefault; - } - - OrderedIntDoubleMapping() { - // no-arg constructor for deserializer - this(11); - } - - OrderedIntDoubleMapping(int capacity) { - indices = new int[capacity]; - values = new double[capacity]; - numMappings = 0; - } - - OrderedIntDoubleMapping(int[] indices, double[] values, int numMappings) { - this.indices = indices; - this.values = values; - this.numMappings = numMappings; - } - - public int[] getIndices() { - return indices; - } - - public int indexAt(int offset) { - return indices[offset]; - } - - public void setIndexAt(int offset, int index) { - indices[offset] = index; - } - - public double[] getValues() { - return values; - } - - public void setValueAt(int offset, double value) { - values[offset] = value; - } - - - public int getNumMappings() { - return numMappings; - } - - private void growTo(int newCapacity) { - if (newCapacity > indices.length) { - int[] newIndices = new int[newCapacity]; - System.arraycopy(indices, 0, newIndices, 0, numMappings); - indices = newIndices; - double[] newValues = new double[newCapacity]; - System.arraycopy(values, 0, newValues, 0, numMappings); - values = newValues; - } - } - - private int find(int index) { - int low = 0; - int high = numMappings - 1; - while (low <= high) { - int mid = low + (high - low >>> 1); - int midVal = indices[mid]; - if (midVal < index) { - low = mid + 1; - } else if (midVal > index) { - high = mid - 1; - } else { - return mid; - } - } - return -(low + 1); - } - - public double get(int index) { - int offset = find(index); - return offset >= 0 ? values[offset] : DEFAULT_VALUE; - } - - public void set(int index, double value) { - if (numMappings == 0 || index > indices[numMappings - 1]) { - if (!noDefault || value != DEFAULT_VALUE) { - if (numMappings >= indices.length) { - growTo(Math.max((int) (1.2 * numMappings), numMappings + 1)); - } - indices[numMappings] = index; - values[numMappings] = value; - ++numMappings; - } - } else { - int offset = find(index); - if (offset >= 0) { - insertOrUpdateValueIfPresent(offset, value); - } else { - insertValueIfNotDefault(index, offset, value); - } - } - } - - /** - * Merges the updates in linear time by allocating new arrays and iterating through the existing indices and values - * and the updates' indices and values at the same time while selecting the minimum index to set at each step. - * @param updates another list of mappings to be merged in. - */ - public void merge(OrderedIntDoubleMapping updates) { - int[] updateIndices = updates.getIndices(); - double[] updateValues = updates.getValues(); - - int newNumMappings = numMappings + updates.getNumMappings(); - int newCapacity = Math.max((int) (1.2 * newNumMappings), newNumMappings + 1); - int[] newIndices = new int[newCapacity]; - double[] newValues = new double[newCapacity]; - - int k = 0; - int i = 0, j = 0; - for (; i < numMappings && j < updates.getNumMappings(); ++k) { - if (indices[i] < updateIndices[j]) { - newIndices[k] = indices[i]; - newValues[k] = values[i]; - ++i; - } else if (indices[i] > updateIndices[j]) { - newIndices[k] = updateIndices[j]; - newValues[k] = updateValues[j]; - ++j; - } else { - newIndices[k] = updateIndices[j]; - newValues[k] = updateValues[j]; - ++i; - ++j; - } - } - - for (; i < numMappings; ++i, ++k) { - newIndices[k] = indices[i]; - newValues[k] = values[i]; - } - for (; j < updates.getNumMappings(); ++j, ++k) { - newIndices[k] = updateIndices[j]; - newValues[k] = updateValues[j]; - } - - indices = newIndices; - values = newValues; - numMappings = k; - } - - @Override - public int hashCode() { - int result = 0; - for (int i = 0; i < numMappings; i++) { - result = 31 * result + indices[i]; - result = 31 * result + (int) Double.doubleToRawLongBits(values[i]); - } - return result; - } - - @Override - public boolean equals(Object o) { - if (o instanceof OrderedIntDoubleMapping) { - OrderedIntDoubleMapping other = (OrderedIntDoubleMapping) o; - if (numMappings == other.numMappings) { - for (int i = 0; i < numMappings; i++) { - if (indices[i] != other.indices[i] || values[i] != other.values[i]) { - return false; - } - } - return true; - } - } - return false; - } - - @Override - public String toString() { - StringBuilder result = new StringBuilder(10 * numMappings); - for (int i = 0; i < numMappings; i++) { - result.append('('); - result.append(indices[i]); - result.append(','); - result.append(values[i]); - result.append(')'); - } - return result.toString(); - } - - @SuppressWarnings("CloneDoesntCallSuperClone") - @Override - public OrderedIntDoubleMapping clone() { - return new OrderedIntDoubleMapping(indices.clone(), values.clone(), numMappings); - } - - public void increment(int index, double increment) { - int offset = find(index); - if (offset >= 0) { - double newValue = values[offset] + increment; - insertOrUpdateValueIfPresent(offset, newValue); - } else { - insertValueIfNotDefault(index, offset, increment); - } - } - - private void insertValueIfNotDefault(int index, int offset, double value) { - if (!noDefault || value != DEFAULT_VALUE) { - if (numMappings >= indices.length) { - growTo(Math.max((int) (1.2 * numMappings), numMappings + 1)); - } - int at = -offset - 1; - if (numMappings > at) { - for (int i = numMappings - 1, j = numMappings; i >= at; i--, j--) { - indices[j] = indices[i]; - values[j] = values[i]; - } - } - indices[at] = index; - values[at] = value; - numMappings++; - } - } - - private void insertOrUpdateValueIfPresent(int offset, double newValue) { - if (noDefault && newValue == DEFAULT_VALUE) { - for (int i = offset + 1, j = offset; i < numMappings; i++, j++) { - indices[j] = indices[i]; - values[j] = values[i]; - } - numMappings--; - } else { - values[offset] = newValue; - } - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/OrthonormalityVerifier.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/OrthonormalityVerifier.java b/math/src/main/java/org/apache/mahout/math/OrthonormalityVerifier.java deleted file mode 100644 index e8dd2b1..0000000 --- a/math/src/main/java/org/apache/mahout/math/OrthonormalityVerifier.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import com.google.common.collect.Lists; - -import java.util.List; - -public final class OrthonormalityVerifier { - - private OrthonormalityVerifier() { - } - - public static VectorIterable pairwiseInnerProducts(Iterable<MatrixSlice> basis) { - DenseMatrix out = null; - for (MatrixSlice slice1 : basis) { - List<Double> dots = Lists.newArrayList(); - for (MatrixSlice slice2 : basis) { - dots.add(slice1.vector().dot(slice2.vector())); - } - if (out == null) { - out = new DenseMatrix(dots.size(), dots.size()); - } - for (int i = 0; i < dots.size(); i++) { - out.set(slice1.index(), i, dots.get(i)); - } - } - return out; - } - -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/PermutedVectorView.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/PermutedVectorView.java b/math/src/main/java/org/apache/mahout/math/PermutedVectorView.java deleted file mode 100644 index e46f326..0000000 --- a/math/src/main/java/org/apache/mahout/math/PermutedVectorView.java +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.mahout.math; - -import java.util.Iterator; - -import com.google.common.collect.AbstractIterator; - -/** - * Provides a permuted view of a vector. - */ -public class PermutedVectorView extends AbstractVector { - private final Vector vector; // the vector containing the data - private final int[] pivot; // convert from external index to internal - private final int[] unpivot; // convert from internal index to external - - public PermutedVectorView(Vector vector, int[] pivot, int[] unpivot) { - super(vector.size()); - this.vector = vector; - this.pivot = pivot; - this.unpivot = unpivot; - } - - public PermutedVectorView(Vector vector, int[] pivot) { - this(vector, pivot, reversePivotPermutation(pivot)); - } - - private static int[] reversePivotPermutation(int[] pivot) { - int[] unpivot1 = new int[pivot.length]; - for (int i = 0; i < pivot.length; i++) { - unpivot1[pivot[i]] = i; - } - return unpivot1; - } - - /** - * Subclasses must override to return an appropriately sparse or dense result - * - * @param rows the row cardinality - * @param columns the column cardinality - * @return a Matrix - */ - @Override - protected Matrix matrixLike(int rows, int columns) { - if (vector.isDense()) { - return new DenseMatrix(rows, columns); - } else { - return new SparseRowMatrix(rows, columns); - } - } - - /** - * Used internally by assign() to update multiple indices and values at once. - * Only really useful for sparse vectors (especially SequentialAccessSparseVector). - * <p> - * If someone ever adds a new type of sparse vectors, this method must merge (index, value) pairs into the vector. - * - * @param updates a mapping of indices to values to merge in the vector. - */ - @Override - public void mergeUpdates(OrderedIntDoubleMapping updates) { - for (int i = 0; i < updates.getNumMappings(); ++i) { - updates.setIndexAt(i, pivot[updates.indexAt(i)]); - } - vector.mergeUpdates(updates); - } - - /** - * @return true iff this implementation should be considered dense -- that it explicitly - * represents every value - */ - @Override - public boolean isDense() { - return vector.isDense(); - } - - /** - * If the view is permuted, the elements cannot be accessed in the same order. - * - * @return true iff this implementation should be considered to be iterable in index order in an - * efficient way. In particular this implies that {@link #iterator()} and {@link - * #iterateNonZero()} return elements in ascending order by index. - */ - @Override - public boolean isSequentialAccess() { - return false; - } - - /** - * Iterates over all elements <p> * NOTE: Implementations may choose to reuse the Element - * returned for performance reasons, so if you need a copy of it, you should call {@link - * #getElement(int)} for the given index - * - * @return An {@link java.util.Iterator} over all elements - */ - @Override - public Iterator<Element> iterator() { - return new AbstractIterator<Element>() { - private final Iterator<Element> i = vector.all().iterator(); - - @Override - protected Vector.Element computeNext() { - if (i.hasNext()) { - final Element x = i.next(); - return new Element() { - private final int index = unpivot[x.index()]; - - @Override - public double get() { - return x.get(); - } - - @Override - public int index() { - return index; - } - - @Override - public void set(double value) { - x.set(value); - } - }; - } else { - return endOfData(); - } - } - }; - } - - /** - * Iterates over all non-zero elements. <p> NOTE: Implementations may choose to reuse the Element - * returned for performance reasons, so if you need a copy of it, you should call {@link - * #getElement(int)} for the given index - * - * @return An {@link java.util.Iterator} over all non-zero elements - */ - @Override - public Iterator<Element> iterateNonZero() { - return new AbstractIterator<Element>() { - private final Iterator<Element> i = vector.nonZeroes().iterator(); - - @Override - protected Vector.Element computeNext() { - if (i.hasNext()) { - final Element x = i.next(); - return new Element() { - private final int index = unpivot[x.index()]; - - @Override - public double get() { - return x.get(); - } - - @Override - public int index() { - return index; - } - - @Override - public void set(double value) { - x.set(value); - } - }; - } else { - return endOfData(); - } - } - }; - } - - /** - * Return the value at the given index, without checking bounds - * - * @param index an int index - * @return the double at the index - */ - @Override - public double getQuick(int index) { - return vector.getQuick(pivot[index]); - } - - /** - * Return an empty vector of the same underlying class as the receiver - * - * @return a Vector - */ - @Override - public Vector like() { - return vector.like(); - } - - @Override - public Vector like(int cardinality) { - return vector.like(cardinality); - } - - /** - * Set the value at the given index, without checking bounds - * - * @param index an int index into the receiver - * @param value a double value to set - */ - @Override - public void setQuick(int index, double value) { - vector.setQuick(pivot[index], value); - } - - /** Return the number of values in the recipient */ - @Override - public int getNumNondefaultElements() { - return vector.getNumNondefaultElements(); - } - - @Override - public int getNumNonZeroElements() { - // Return the number of nonzeros in the recipient, - // so potentially don't have to go through our iterator - return vector.getNumNonZeroElements(); - } - - @Override - public double getLookupCost() { - return vector.getLookupCost(); - } - - @Override - public double getIteratorAdvanceCost() { - return vector.getIteratorAdvanceCost(); - } - - @Override - public boolean isAddConstantTime() { - return vector.isAddConstantTime(); - } -} http://git-wip-us.apache.org/repos/asf/mahout/blob/e0573de3/math/src/main/java/org/apache/mahout/math/PersistentObject.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/PersistentObject.java b/math/src/main/java/org/apache/mahout/math/PersistentObject.java deleted file mode 100644 index f1d4293..0000000 --- a/math/src/main/java/org/apache/mahout/math/PersistentObject.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -/* -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; - -/** - * This empty class is the common root for all persistent capable classes. - * If this class inherits from <tt>java.lang.Object</tt> then all subclasses are serializable with - * the standard Java serialization mechanism. - * If this class inherits from <tt>com.objy.db.app.ooObj</tt> then all subclasses are - * <i>additionally</i> serializable with the Objectivity ODBMS persistance mechanism. - * Thus, by modifying the inheritance of this class the entire tree of subclasses can - * be switched to Objectivity compatibility (and back) with minimum effort. - */ -public abstract class PersistentObject implements java.io.Serializable, Cloneable { - - /** Not yet commented. */ - protected PersistentObject() { - } - - /** - * Returns a copy of the receiver. This default implementation does not nothing except making the otherwise - * <tt>protected</tt> clone method <tt>public</tt>. - * - * @return a copy of the receiver. - */ - @Override - public Object clone() { - try { - return super.clone(); - } catch (CloneNotSupportedException exc) { - throw new InternalError(); //should never happen since we are cloneable - } - } -}
