http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/VectorUtils.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/VectorUtils.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/VectorUtils.java deleted file mode 100644 index 3f72fd3..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/VectorUtils.java +++ /dev/null @@ -1,195 +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.ignite.ml.math; - -import java.util.Arrays; -import java.util.Map; -import java.util.Objects; -import org.apache.ignite.internal.util.typedef.internal.A; -import org.apache.ignite.ml.math.functions.IgniteBiFunction; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.impls.vector.MapWrapperVector; -import org.apache.ignite.ml.math.impls.vector.SparseLocalVector; - -/** - * Some utils for {@link Vector}. - */ -public class VectorUtils { - /** Create new vector like given vector initialized by zeroes. */ - public static Vector zeroesLike(Vector v) { - return v.like(v.size()).assign(0.0); - } - - /** Create new */ - public static DenseLocalOnHeapVector zeroes(int n) { - return (DenseLocalOnHeapVector)new DenseLocalOnHeapVector(n).assign(0.0); - } - - /** */ - public static Vector fromMap(Map<Integer, Double> val, boolean cp) { - return new MapWrapperVector(val); - } - - /** - * Turn number into a local Vector of given size with one-hot encoding. - * - * @param num Number to turn into vector. - * @param vecSize Vector size of output vector. - * @return One-hot encoded number. - */ - public static Vector num2Vec(int num, int vecSize) { - return num2Vec(num, vecSize, false); - } - - /** - * Turn number into Vector of given size with one-hot encoding. - * - * @param num Number to turn into vector. - * @param vecSize Vector size of output vector. - * @param isDistributed Flag indicating if distributed vector should be created. - * @return One-hot encoded number. - */ - public static Vector num2Vec(int num, int vecSize, boolean isDistributed) { - Vector res = new DenseLocalOnHeapVector(vecSize); - return res.setX(num, 1); - } - - /** - * Turn Vector into number by looking at index of maximal element in vector. - * - * @param vec Vector to be turned into number. - * @return Number. - */ - public static double vec2Num(Vector vec) { - int max = 0; - double maxVal = Double.NEGATIVE_INFINITY; - - for (int i = 0; i < vec.size(); i++) { - double curVal = vec.getX(i); - if (curVal > maxVal) { - max = i; - maxVal = curVal; - } - } - - return max; - } - - /** - * Performs in-place vector multiplication. - * - * @param vec1 Operand to be changed and first multiplication operand. - * @param vec2 Second multiplication operand. - * @return Updated first operand. - */ - public static Vector elementWiseTimes(Vector vec1, Vector vec2) { - vec1.map(vec2, (a, b) -> a * b); - - return vec1; - } - - /** - * Performs in-place vector subtraction. - * - * @param vec1 Operand to be changed and subtracted from. - * @param vec2 Operand to subtract. - * @return Updated first operand. - */ - public static Vector elementWiseMinus(Vector vec1, Vector vec2) { - vec1.map(vec2, (a, b) -> a - b); - - return vec1; - } - - /** - * Zip two vectors with given binary function - * (i.e. apply binary function to both vector elementwise and construct vector from results). - * - * Example zipWith({0, 2, 4}, {1, 3, 5}, plus) = {0 + 1, 2 + 3, 4 + 5}. - * Length of result is length of shortest of vectors. - * - * @param v1 First vector. - * @param v2 Second vector. - * @param f Function to zip with. - * @return Result of zipping. - */ - public static Vector zipWith(Vector v1, Vector v2, IgniteBiFunction<Double, Double, Double> f) { - int size = Math.min(v1.size(), v2.size()); - - Vector res = v1.like(size); - - for (int row = 0; row < size; row++) - res.setX(row, f.apply(v1.getX(row), v2.getX(row))); - - return res; - } - - /** - * Get copy of part of given length of given vector starting from given offset. - * - * @param v Vector to copy part from. - * @param off Offset. - * @param len Length. - * @return Copy of part of given length of given vector starting from given offset. - */ - public static Vector copyPart(Vector v, int off, int len) { - assert off >= 0; - assert len <= v.size(); - - Vector res = v.like(len); - - for (int i = 0; i < len; i++) - res.setX(i, v.getX(off + i)); - - return res; - } - - /** - * Creates dense local on heap vector based on array of doubles. - * - * @param values Values. - */ - public static Vector of(double ... values) { - A.notNull(values, "values"); - - return new DenseLocalOnHeapVector(values); - } - - /** - * Creates vector based on array of Doubles. If array contains null-elements then - * method returns sparse local on head vector. In other case method returns - * dense local on heap vector. - * - * @param values Values. - */ - public static Vector of(Double[] values) { - A.notNull(values, "values"); - - Vector answer = null; - if (Arrays.stream(values).anyMatch(Objects::isNull)) - answer = new SparseLocalVector(values.length, StorageConstants.RANDOM_ACCESS_MODE); - else - answer = new DenseLocalOnHeapVector(values.length); - - for (int i = 0; i < values.length; i++) - if (values[i] != null) - answer.set(i, values[i]); - - return answer; - } -}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/DistanceMeasure.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/DistanceMeasure.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/DistanceMeasure.java index 7ea8fb3..50324c1 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/DistanceMeasure.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/DistanceMeasure.java @@ -17,7 +17,7 @@ package org.apache.ignite.ml.math.distances; import java.io.Externalizable; -import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.math.exceptions.CardinalityException; /** http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/EuclideanDistance.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/EuclideanDistance.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/EuclideanDistance.java index 64ea285..935e19b 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/EuclideanDistance.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/EuclideanDistance.java @@ -19,7 +19,7 @@ package org.apache.ignite.ml.math.distances; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; -import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.math.exceptions.CardinalityException; import org.apache.ignite.ml.math.util.MatrixUtil; @@ -61,9 +61,6 @@ public class EuclideanDistance implements DistanceMeasure { if (this == obj) return true; - if (obj == null || getClass() != obj.getClass()) - return false; - - return true; + return obj != null && getClass() == obj.getClass(); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/HammingDistance.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/HammingDistance.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/HammingDistance.java index cb99074..f08ff7b 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/HammingDistance.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/HammingDistance.java @@ -19,7 +19,7 @@ package org.apache.ignite.ml.math.distances; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; -import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.math.exceptions.CardinalityException; import org.apache.ignite.ml.math.functions.Functions; import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; @@ -62,9 +62,6 @@ public class HammingDistance implements DistanceMeasure { if (this == obj) return true; - if (obj == null || getClass() != obj.getClass()) - return false; - - return true; + return obj != null && getClass() == obj.getClass(); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/ManhattanDistance.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/ManhattanDistance.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/ManhattanDistance.java index 9ea36b3..d298e3c 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/ManhattanDistance.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/distances/ManhattanDistance.java @@ -19,7 +19,7 @@ package org.apache.ignite.ml.math.distances; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; -import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.primitives.vector.Vector; import org.apache.ignite.ml.math.exceptions.CardinalityException; import org.apache.ignite.ml.math.util.MatrixUtil; @@ -56,9 +56,6 @@ public class ManhattanDistance implements DistanceMeasure { if (this == obj) return true; - if (obj == null || getClass() != obj.getClass()) - return false; - - return true; + return obj != null && getClass() == obj.getClass(); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/exceptions/preprocessing/UnknownStringValue.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/exceptions/preprocessing/UnknownStringValue.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/exceptions/preprocessing/UnknownStringValue.java index f2312a1..2fc6cee 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/exceptions/preprocessing/UnknownStringValue.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/exceptions/preprocessing/UnknownStringValue.java @@ -27,9 +27,9 @@ public class UnknownStringValue extends IgniteException { private static final long serialVersionUID = 0L; /** - * @param unknownString String value that caused this exception. + * @param unknownStr String value that caused this exception. */ - public UnknownStringValue(String unknownString) { - super("This String value is unknown for StringEncoder: " + unknownString); + public UnknownStringValue(String unknownStr) { + super("This String value is unknown for StringEncoder: " + unknownStr); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteBiFunction.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteBiFunction.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteBiFunction.java index 560be4b..b44d77b 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteBiFunction.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteBiFunction.java @@ -28,7 +28,7 @@ import java.util.function.BiFunction; */ public interface IgniteBiFunction<T, U, R> extends BiFunction<T, U, R>, Serializable { /** */ - default <V> IgniteBiFunction<T, U, V> andThen(IgniteFunction<? super R, ? extends V> after) { + public default <V> IgniteBiFunction<T, U, V> andThen(IgniteFunction<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t, U u) -> after.apply(apply(t, u)); } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableDoubleToDoubleFunction.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableDoubleToDoubleFunction.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableDoubleToDoubleFunction.java index e97ee41..985b2d5 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableDoubleToDoubleFunction.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableDoubleToDoubleFunction.java @@ -27,5 +27,5 @@ public interface IgniteDifferentiableDoubleToDoubleFunction extends IgniteDouble * @param pnt Point to calculate differential at. * @return Function differential at a given point. */ - double differential(double pnt); + public double differential(double pnt); } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableVectorToDoubleFunction.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableVectorToDoubleFunction.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableVectorToDoubleFunction.java index 3132c63..8f55525 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableVectorToDoubleFunction.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteDifferentiableVectorToDoubleFunction.java @@ -17,7 +17,7 @@ package org.apache.ignite.ml.math.functions; -import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.primitives.vector.Vector; /** * Interface for differentiable functions from vector to double. @@ -29,5 +29,5 @@ public interface IgniteDifferentiableVectorToDoubleFunction extends IgniteFuncti * @param pnt Point to calculate differential at. * @return Function differential at a given point. */ - Vector differential(Vector pnt); + public Vector differential(Vector pnt); } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriConsumer.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriConsumer.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriConsumer.java index af304c9..2954016 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriConsumer.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriConsumer.java @@ -36,5 +36,5 @@ public interface IgniteTriConsumer<A, B, C> extends Serializable { * @param second Second parameter. * @param third Third parameter. */ - void accept(A first, B second, C third); + public void accept(A first, B second, C third); } http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriFunction.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriFunction.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriFunction.java index 4d8fd20..e1f7ae5 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriFunction.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/functions/IgniteTriFunction.java @@ -25,10 +25,10 @@ import java.util.function.Function; @FunctionalInterface public interface IgniteTriFunction<A, B, C, R> extends Serializable { /** */ - R apply(A a, B b, C c); + public R apply(A a, B b, C c); /** */ - default <V> IgniteTriFunction<A, B, C, V> andThen(Function<? super R, ? extends V> after) { + public default <V> IgniteTriFunction<A, B, C, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (A a, B b, C c) -> after.apply(apply(a, b, c)); http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/AbstractMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/AbstractMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/AbstractMatrix.java deleted file mode 100644 index 06fb7a2..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/AbstractMatrix.java +++ /dev/null @@ -1,980 +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.ignite.ml.math.impls.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Random; -import java.util.Spliterator; -import java.util.function.Consumer; -import org.apache.ignite.lang.IgniteUuid; -import org.apache.ignite.ml.math.Blas; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.exceptions.ColumnIndexException; -import org.apache.ignite.ml.math.exceptions.RowIndexException; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.functions.IgniteBiFunction; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.functions.IgniteTriFunction; -import org.apache.ignite.ml.math.functions.IntIntToDoubleFunction; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.impls.vector.MatrixVectorView; -import org.apache.ignite.ml.math.util.MatrixUtil; - -/** - * This class provides a helper implementation of the {@link Matrix} - * interface to minimize the effort required to implement it. - * Subclasses may override some of the implemented methods if a more - * specific or optimized implementation is desirable. - */ -public abstract class AbstractMatrix implements Matrix { - // Stochastic sparsity analysis. - /** */ - private static final double Z95 = 1.959964; - /** */ - private static final double Z80 = 1.281552; - /** */ - private static final int MAX_SAMPLES = 500; - /** */ - private static final int MIN_SAMPLES = 15; - - /** Cached minimum element. */ - private Element minElm; - /** Cached maximum element. */ - private Element maxElm = null; - - /** Matrix storage implementation. */ - private MatrixStorage sto; - - /** Meta attributes storage. */ - private Map<String, Object> meta = new HashMap<>(); - - /** Matrix's GUID. */ - private IgniteUuid guid = IgniteUuid.randomUuid(); - - /** - * @param sto Backing {@link MatrixStorage}. - */ - public AbstractMatrix(MatrixStorage sto) { - this.sto = sto; - } - - /** - * - */ - public AbstractMatrix() { - // No-op. - } - - /** - * @param sto Backing {@link MatrixStorage}. - */ - protected void setStorage(MatrixStorage sto) { - assert sto != null; - - this.sto = sto; - } - - /** - * @param row Row index in the matrix. - * @param col Column index in the matrix. - * @param v Value to set. - */ - protected void storageSet(int row, int col, double v) { - sto.set(row, col, v); - - // Reset cached values. - minElm = maxElm = null; - } - - /** - * @param row Row index in the matrix. - * @param col Column index in the matrix. - */ - protected double storageGet(int row, int col) { - return sto.get(row, col); - } - - /** {@inheritDoc} */ - @Override public Element maxElement() { - if (maxElm == null) { - double max = Double.NEGATIVE_INFINITY; - int row = 0, col = 0; - - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) { - double d = storageGet(x, y); - - if (d > max) { - max = d; - row = x; - col = y; - } - } - - maxElm = mkElement(row, col); - } - - return maxElm; - } - - /** {@inheritDoc} */ - @Override public Element minElement() { - if (minElm == null) { - double min = Double.MAX_VALUE; - int row = 0, col = 0; - - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) { - double d = storageGet(x, y); - - if (d < min) { - min = d; - row = x; - col = y; - } - } - - minElm = mkElement(row, col); - } - - return minElm; - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - return maxElement().get(); - } - - /** {@inheritDoc} */ - @Override public double minValue() { - return minElement().get(); - } - - /** - * @param row Row index in the matrix. - * @param col Column index in the matrix. - */ - private Element mkElement(int row, int col) { - return new Element() { - /** {@inheritDoc} */ - @Override public double get() { - return storageGet(row, col); - } - - /** {@inheritDoc} */ - @Override public int row() { - return row; - } - - /** {@inheritDoc} */ - @Override public int column() { - return col; - } - - /** {@inheritDoc} */ - @Override public void set(double d) { - storageSet(row, col, d); - } - }; - } - - /** {@inheritDoc} */ - @Override public Element getElement(int row, int col) { - return mkElement(row, col); - } - - /** {@inheritDoc} */ - @Override public Matrix swapRows(int row1, int row2) { - checkRowIndex(row1); - checkRowIndex(row2); - - int cols = columnSize(); - - for (int y = 0; y < cols; y++) { - double v = getX(row1, y); - - setX(row1, y, getX(row2, y)); - setX(row2, y, v); - } - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix swapColumns(int col1, int col2) { - checkColumnIndex(col1); - checkColumnIndex(col2); - - int rows = rowSize(); - - for (int x = 0; x < rows; x++) { - double v = getX(x, col1); - - setX(x, col1, getX(x, col2)); - setX(x, col2, v); - } - - return this; - } - - /** {@inheritDoc} */ - @Override public MatrixStorage getStorage() { - return sto; - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return sto.isSequentialAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return sto.isDense(); - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return sto.isRandomAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return sto.isDistributed(); - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return sto.isArrayBased(); - } - - /** - * Check row index bounds. - * - * @param row Row index. - */ - void checkRowIndex(int row) { - if (row < 0 || row >= rowSize()) - throw new RowIndexException(row); - } - - /** - * Check column index bounds. - * - * @param col Column index. - */ - void checkColumnIndex(int col) { - if (col < 0 || col >= columnSize()) - throw new ColumnIndexException(col); - } - - /** - * Check column and row index bounds. - * - * @param row Row index. - * @param col Column index. - */ - private void checkIndex(int row, int col) { - checkRowIndex(row); - checkColumnIndex(col); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(sto); - out.writeObject(meta); - out.writeObject(guid); - } - - /** {@inheritDoc} */ - @Override public Map<String, Object> getMetaStorage() { - return meta; - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - sto = (MatrixStorage)in.readObject(); - meta = (Map<String, Object>)in.readObject(); - guid = (IgniteUuid)in.readObject(); - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double val) { - if (sto.isArrayBased()) - Arrays.fill(sto.data(), val); - else { - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - storageSet(x, y, val); - } - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix assign(IntIntToDoubleFunction fun) { - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - storageSet(x, y, fun.apply(x, y)); - - return this; - } - - /** */ - private void checkCardinality(Matrix mtx) { - checkCardinality(mtx.rowSize(), mtx.columnSize()); - } - - /** */ - private void checkCardinality(int rows, int cols) { - if (rows != rowSize()) - throw new CardinalityException(rowSize(), rows); - - if (cols != columnSize()) - throw new CardinalityException(columnSize(), cols); - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double[][] vals) { - checkCardinality(vals.length, vals[0].length); - - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - storageSet(x, y, vals[x][y]); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix assign(Matrix mtx) { - checkCardinality(mtx); - - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - storageSet(x, y, mtx.getX(x, y)); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix map(IgniteDoubleFunction<Double> fun) { - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - storageSet(x, y, fun.apply(storageGet(x, y))); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix map(Matrix mtx, IgniteBiFunction<Double, Double, Double> fun) { - checkCardinality(mtx); - - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - storageSet(x, y, fun.apply(storageGet(x, y), mtx.getX(x, y))); - - return this; - } - - /** {@inheritDoc} */ - @Override public Spliterator<Double> allSpliterator() { - return new Spliterator<Double>() { - /** {@inheritDoc} */ - @Override public boolean tryAdvance(Consumer<? super Double> act) { - int rLen = rowSize(); - int cLen = columnSize(); - - for (int i = 0; i < rLen; i++) - for (int j = 0; j < cLen; j++) - act.accept(storageGet(i, j)); - - return true; - } - - /** {@inheritDoc} */ - @Override public Spliterator<Double> trySplit() { - return null; // No Splitting. - } - - /** {@inheritDoc} */ - @Override public long estimateSize() { - return rowSize() * columnSize(); - } - - /** {@inheritDoc} */ - @Override public int characteristics() { - return ORDERED | SIZED; - } - }; - } - - /** {@inheritDoc} */ - @Override public int nonZeroElements() { - int cnt = 0; - - for (int i = 0; i < rowSize(); i++) - for (int j = 0; j < rowSize(); j++) - if (get(i, j) != 0.0) - cnt++; - - return cnt; - } - - /** {@inheritDoc} */ - @Override public Spliterator<Double> nonZeroSpliterator() { - return new Spliterator<Double>() { - /** {@inheritDoc} */ - @Override public boolean tryAdvance(Consumer<? super Double> act) { - int rLen = rowSize(); - int cLen = columnSize(); - - for (int i = 0; i < rLen; i++) - for (int j = 0; j < cLen; j++) { - double val = storageGet(i, j); - - if (val != 0.0) - act.accept(val); - } - return true; - } - - /** {@inheritDoc} */ - @Override public Spliterator<Double> trySplit() { - return null; // No Splitting. - } - - /** {@inheritDoc} */ - @Override public long estimateSize() { - return nonZeroElements(); - } - - /** {@inheritDoc} */ - @Override public int characteristics() { - return ORDERED | SIZED; - } - }; - } - - /** {@inheritDoc} */ - @Override public Matrix assignColumn(int col, Vector vec) { - checkColumnIndex(col); - - int rows = rowSize(); - - for (int x = 0; x < rows; x++) - storageSet(x, col, vec.getX(x)); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix assignRow(int row, Vector vec) { - checkRowIndex(row); - - int cols = columnSize(); - - if (cols != vec.size()) - throw new CardinalityException(cols, vec.size()); - - // TODO: IGNITE-5777, use Blas for this. - for (int y = 0; y < cols; y++) - storageSet(row, y, vec.getX(y)); - - return this; - } - - /** {@inheritDoc} */ - @Override public Vector foldRows(IgniteFunction<Vector, Double> fun) { - int rows = rowSize(); - - Vector vec = likeVector(rows); - - for (int i = 0; i < rows; i++) - vec.setX(i, fun.apply(viewRow(i))); - - return vec; - } - - /** {@inheritDoc} */ - @Override public Vector foldColumns(IgniteFunction<Vector, Double> fun) { - int cols = columnSize(); - - Vector vec = likeVector(cols); - - for (int i = 0; i < cols; i++) - vec.setX(i, fun.apply(viewColumn(i))); - - return vec; - } - - /** {@inheritDoc} */ - @Override public <T> T foldMap(IgniteBiFunction<T, Double, T> foldFun, IgniteDoubleFunction<Double> mapFun, - T zeroVal) { - T res = zeroVal; - - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - res = foldFun.apply(res, mapFun.apply(storageGet(x, y))); - - return res; - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return sto.columnSize(); - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return sto.rowSize(); - } - - /** */ - protected Matrix likeIdentity() { - int n = rowSize(); - Matrix res = like(n, n); - - for (int i = 0; i < n; i++) - res.setX(i, i, 1.0); - - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix divide(double d) { - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - setX(x, y, getX(x, y) / d); - - return this; - } - - /** {@inheritDoc} */ - @Override public double get(int row, int col) { - checkIndex(row, col); - - return storageGet(row, col); - } - - /** {@inheritDoc} */ - @Override public double getX(int row, int col) { - return storageGet(row, col); - } - - /** {@inheritDoc} */ - @Override public Matrix minus(Matrix mtx) { - int rows = rowSize(); - int cols = columnSize(); - - checkCardinality(rows, cols); - - Matrix res = like(rows, cols); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - res.setX(x, y, getX(x, y) - mtx.getX(x, y)); - - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix plus(double x) { - Matrix cp = copy(); - - cp.map(Functions.plus(x)); - - return cp; - } - - /** {@inheritDoc} */ - @Override public Matrix plus(Matrix mtx) { - int rows = rowSize(); - int cols = columnSize(); - - checkCardinality(rows, cols); - - Matrix res = like(rows, cols); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - res.setX(x, y, getX(x, y) + mtx.getX(x, y)); - - return res; - - } - - /** {@inheritDoc} */ - @Override public IgniteUuid guid() { - return guid; - } - - /** {@inheritDoc} */ - @Override public Matrix set(int row, int col, double val) { - checkIndex(row, col); - - storageSet(row, col, val); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix setRow(int row, double[] data) { - checkRowIndex(row); - - int cols = columnSize(); - - if (cols != data.length) - throw new CardinalityException(cols, data.length); - // TODO: IGNITE-5777, use Blas for this. - for (int y = 0; y < cols; y++) - setX(row, y, data[y]); - - return this; - } - - /** {@inheritDoc} */ - @Override public Vector getRow(int row) { - checkRowIndex(row); - - Vector res = new DenseLocalOnHeapVector(columnSize()); - - for (int i = 0; i < columnSize(); i++) - res.setX(i, getX(row, i)); - - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix setColumn(int col, double[] data) { - checkColumnIndex(col); - - int rows = rowSize(); - - if (rows != data.length) - throw new CardinalityException(rows, data.length); - - for (int x = 0; x < rows; x++) - setX(x, col, data[x]); - - return this; - } - - /** {@inheritDoc} */ - @Override public Vector getCol(int col) { - checkColumnIndex(col); - - Vector res; - - if (isDistributed()) - res = MatrixUtil.likeVector(this, rowSize()); - else - res = new DenseLocalOnHeapVector(rowSize()); - - for (int i = 0; i < rowSize(); i++) - res.setX(i, getX(i, col)); - - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix setX(int row, int col, double val) { - storageSet(row, col, val); - - return this; - } - - /** {@inheritDoc} */ - @Override public double maxAbsRowSumNorm() { - double max = 0.0; - - int rows = rowSize(); - int cols = columnSize(); - - for (int x = 0; x < rows; x++) { - double sum = 0; - - for (int y = 0; y < cols; y++) - sum += Math.abs(getX(x, y)); - - if (sum > max) - max = sum; - } - - return max; - } - - /** {@inheritDoc} */ - @Override public Matrix times(double x) { - Matrix cp = copy(); - - cp.map(Functions.mult(x)); - - return cp; - } - - /** {@inheritDoc} */ - @Override public Vector times(Vector vec) { - int cols = columnSize(); - - if (cols != vec.size()) - throw new CardinalityException(cols, vec.size()); - - int rows = rowSize(); - - Vector res = likeVector(rows); - - Blas.gemv(1, this, vec, 0, res); - - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix times(Matrix mtx) { - int cols = columnSize(); - - if (cols != mtx.rowSize()) - throw new CardinalityException(cols, mtx.rowSize()); - - Matrix res = like(rowSize(), mtx.columnSize()); - - Blas.gemm(1, this, mtx, 0, res); - - return res; - } - - /** {@inheritDoc} */ - @Override public double sum() { - int rows = rowSize(); - int cols = columnSize(); - - double sum = 0.0; - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - sum += getX(x, y); - - return sum; - } - - /** {@inheritDoc} */ - @Override public Matrix transpose() { - int rows = rowSize(); - int cols = columnSize(); - - Matrix mtx = like(cols, rows); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < cols; y++) - mtx.setX(y, x, getX(x, y)); - - return mtx; - } - - /** {@inheritDoc} */ - @Override public boolean density(double threshold) { - assert threshold >= 0.0 && threshold <= 1.0; - - int n = MIN_SAMPLES; - int rows = rowSize(); - int cols = columnSize(); - - double mean = 0.0; - double pq = threshold * (1 - threshold); - - Random rnd = new Random(); - - for (int i = 0; i < MIN_SAMPLES; i++) - if (getX(rnd.nextInt(rows), rnd.nextInt(cols)) != 0.0) - mean++; - - mean /= MIN_SAMPLES; - - double iv = Z80 * Math.sqrt(pq / n); - - if (mean < threshold - iv) - return false; // Sparse. - else if (mean > threshold + iv) - return true; // Dense. - - while (n < MAX_SAMPLES) { - // Determine upper bound we may need for 'n' to likely relinquish the uncertainty. - // Here, we use confidence interval formula but solved for 'n'. - double ivX = Math.max(Math.abs(threshold - mean), 1e-11); - - double stdErr = ivX / Z80; - double nX = Math.min(Math.max((int)Math.ceil(pq / (stdErr * stdErr)), n), MAX_SAMPLES) - n; - - if (nX < 1.0) // IMPL NOTE this can happen with threshold 1.0 - nX = 1.0; - - double meanNext = 0.0; - - for (int i = 0; i < nX; i++) - if (getX(rnd.nextInt(rows), rnd.nextInt(cols)) != 0.0) - meanNext++; - - mean = (n * mean + meanNext) / (n + nX); - - n += nX; - - // Are we good now? - iv = Z80 * Math.sqrt(pq / n); - - if (mean < threshold - iv) - return false; // Sparse. - else if (mean > threshold + iv) - return true; // Dense. - } - - return mean > threshold; // Dense if mean > threshold. - } - - /** {@inheritDoc} */ - @Override public Matrix viewPart(int[] off, int[] size) { - return new MatrixView(this, off[0], off[1], size[0], size[1]); - } - - /** {@inheritDoc} */ - @Override public Matrix viewPart(int rowOff, int rows, int colOff, int cols) { - return viewPart(new int[] {rowOff, colOff}, new int[] {rows, cols}); - } - - /** {@inheritDoc} */ - @Override public Vector viewRow(int row) { - return new MatrixVectorView(this, row, 0, 0, 1); - } - - /** {@inheritDoc} */ - @Override public Vector viewColumn(int col) { - return new MatrixVectorView(this, 0, col, 1, 0); - } - - /** {@inheritDoc} */ - @Override public Vector viewDiagonal() { - return new MatrixVectorView(this, 0, 0, 1, 1); - } - - /** {@inheritDoc} */ - @Override public void destroy() { - getStorage().destroy(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - Matrix cp = like(rowSize(), columnSize()); - - cp.assign(this); - - return cp; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + guid.hashCode(); - res = res * 37 + sto.hashCode(); - res = res * 37 + meta.hashCode(); - - return res; - } - - /** - * {@inheritDoc} - * - * We ignore guid's for comparisons. - */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - AbstractMatrix that = (AbstractMatrix)o; - - MatrixStorage sto = getStorage(); - - return (sto != null ? sto.equals(that.getStorage()) : that.getStorage() == null); - } - - /** {@inheritDoc} */ - @Override public void compute(int row, int col, IgniteTriFunction<Integer, Integer, Double, Double> f) { - setX(row, col, f.apply(row, col, getX(row, col))); - } - - /** - * Return max amount of columns in 2d array. - * - * TODO: why this in this class, mb some util class? - * - * @param data Data. - */ - protected int getMaxAmountOfColumns(double[][] data) { - int maxAmountOfCols = 0; - - for (int i = 0; i < data.length; i++) - maxAmountOfCols = Math.max(maxAmountOfCols, data[i].length); - - return maxAmountOfCols; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "Matrix [rows=" + rowSize() + ", cols=" + columnSize() + "]"; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOffHeapMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOffHeapMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOffHeapMatrix.java deleted file mode 100644 index 08c9142..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOffHeapMatrix.java +++ /dev/null @@ -1,114 +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.ignite.ml.math.impls.matrix; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.impls.storage.matrix.DenseOffHeapMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector; - -/** - * Dense local off-heap implementation of the {@link Matrix} interface. - */ -public class DenseLocalOffHeapMatrix extends AbstractMatrix { - /** */ - public DenseLocalOffHeapMatrix() { - // No-op. - } - - /** - * @param data Backing data array. - */ - public DenseLocalOffHeapMatrix(double[][] data) { - assert data != null; - - setStorage(new DenseOffHeapMatrixStorage(data)); - } - - /** - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - */ - public DenseLocalOffHeapMatrix(int rows, int cols) { - assert rows > 0; - assert cols > 0; - - setStorage(new DenseOffHeapMatrixStorage(rows, cols)); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - DenseLocalOffHeapMatrix cp = new DenseLocalOffHeapMatrix(getStorage().rowSize(), getStorage().columnSize()); - - cp.assign(this); - - return cp; - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return new DenseLocalOffHeapMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new DenseLocalOffHeapVector(crd); - } - - /** {@inheritDoc} */ - @Override protected Matrix likeIdentity() { - int n = rowSize(); - Matrix res = like(n, n); - - // IMPL NOTE as opposed to on-heap matrices this one isn't initialized with zeroes - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - res.setX(i, j, i == j ? 1.0 : 0.0); - - return res; - } - - /** - * TODO: IGNITE-5535, WIP, currently it`s tmp naive impl. - */ - @Override public Matrix times(Matrix mtx) { - int cols = columnSize(); - - if (cols != mtx.rowSize()) - throw new CardinalityException(cols, mtx.rowSize()); - - int rows = rowSize(); - - int mtxCols = mtx.columnSize(); - - Matrix res = like(rows, mtxCols); - - for (int x = 0; x < rows; x++) - for (int y = 0; y < mtxCols; y++) { - double sum = 0.0; - - for (int k = 0; k < cols; k++) - sum += getX(x, k) * mtx.getX(k, y); - - res.setX(x, y, sum); - } - - return res; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOnHeapMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOnHeapMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOnHeapMatrix.java deleted file mode 100644 index 2376cbd..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DenseLocalOnHeapMatrix.java +++ /dev/null @@ -1,133 +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.ignite.ml.math.impls.matrix; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.OrderedMatrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.storage.matrix.ArrayMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; - -/** - * Basic implementation for matrix. - * - * This is a trivial implementation for matrix assuming dense logic, local on-heap JVM storage - * based on <code>double[][]</code> array. It is only suitable for data sets where - * local, non-distributed execution is satisfactory and on-heap JVM storage is enough - * to keep the entire data set. - */ -public class DenseLocalOnHeapMatrix extends AbstractMatrix implements OrderedMatrix { - /** - * - */ - public DenseLocalOnHeapMatrix() { - // No-op. - } - - /** - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - */ - public DenseLocalOnHeapMatrix(int rows, int cols) { - this(rows, cols, StorageConstants.ROW_STORAGE_MODE); - } - - /** - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - * @param acsMode Storage order (row or column-based). - */ - public DenseLocalOnHeapMatrix(int rows, int cols, int acsMode) { - assert rows > 0; - assert cols > 0; - - setStorage(new ArrayMatrixStorage(rows, cols, acsMode)); - } - - /** - * @param mtx Backing data array. - * @param acsMode Access mode. - */ - public DenseLocalOnHeapMatrix(double[][] mtx, int acsMode) { - assert mtx != null; - - setStorage(new ArrayMatrixStorage(mtx, acsMode)); - } - - /** - * @param mtx Backing data array. - */ - public DenseLocalOnHeapMatrix(double[][] mtx) { - this(mtx, StorageConstants.ROW_STORAGE_MODE); - } - - /** - * @param mtx Backing data array. - * @param acsMode Access mode. - */ - public DenseLocalOnHeapMatrix(double[] mtx, int rows, int acsMode) { - assert mtx != null; - - setStorage(new ArrayMatrixStorage(mtx, rows, acsMode)); - } - - /** - * Build new matrix from flat raw array. - */ - public DenseLocalOnHeapMatrix(double[] mtx, int rows) { - this(mtx, StorageConstants.ROW_STORAGE_MODE, rows); - } - - /** */ - private DenseLocalOnHeapMatrix(DenseLocalOnHeapMatrix orig) { - this(orig, orig.accessMode()); - } - - /** - * @param orig Original matrix. - * @param acsMode Access mode. - */ - private DenseLocalOnHeapMatrix(DenseLocalOnHeapMatrix orig, int acsMode) { - assert orig != null; - - setStorage(new ArrayMatrixStorage(orig.rowSize(), orig.columnSize(), acsMode)); - - assign(orig); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new DenseLocalOnHeapMatrix(this, accessMode()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return new DenseLocalOnHeapMatrix(rows, cols, getStorage() != null ? accessMode() : StorageConstants.ROW_STORAGE_MODE); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new DenseLocalOnHeapVector(crd); - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return getStorage().accessMode(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixView.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixView.java deleted file mode 100644 index 89b031e..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixView.java +++ /dev/null @@ -1,84 +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.ignite.ml.math.impls.matrix; - -import java.io.Externalizable; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.storage.matrix.MatrixDelegateStorage; - -/** - * Implements the rectangular view into the parent {@link Matrix}. - */ -public class MatrixView extends AbstractMatrix { - /** - * Constructor for {@link Externalizable} interface. - */ - public MatrixView() { - // No-op. - } - - /** - * @param parent Backing parent {@link Matrix}. - * @param rowOff Row offset to parent matrix. - * @param colOff Column offset to parent matrix. - * @param rows Amount of rows in the view. - * @param cols Amount of columns in the view. - */ - public MatrixView(Matrix parent, int rowOff, int colOff, int rows, int cols) { - this(parent == null ? null : parent.getStorage(), rowOff, colOff, rows, cols); - } - - /** - * @param sto Backing parent {@link MatrixStorage}. - * @param rowOff Row offset to parent storage. - * @param colOff Column offset to parent storage. - * @param rows Amount of rows in the view. - * @param cols Amount of columns in the view. - */ - public MatrixView(MatrixStorage sto, int rowOff, int colOff, int rows, int cols) { - super(new MatrixDelegateStorage(sto, rowOff, colOff, rows, cols)); - } - - /** - * - * - */ - private MatrixDelegateStorage storage() { - return (MatrixDelegateStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - MatrixDelegateStorage sto = storage(); - - return new MatrixView(sto.delegate(), sto.rowOffset(), sto.columnOffset(), sto.rowSize(), sto.columnSize()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseLocalOnHeapMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseLocalOnHeapMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseLocalOnHeapMatrix.java deleted file mode 100644 index d0a5937..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseLocalOnHeapMatrix.java +++ /dev/null @@ -1,99 +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.ignite.ml.math.impls.matrix; - -import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; -import it.unimi.dsi.fastutil.ints.IntIterator; -import it.unimi.dsi.fastutil.ints.IntSet; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.functions.IgniteTriFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.SparseLocalOnHeapMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.SparseLocalVector; - -/** - * Sparse local onheap matrix with {@link SparseLocalVector} as rows. - */ -public class SparseLocalOnHeapMatrix extends AbstractMatrix implements StorageConstants { - /** - * - */ - public SparseLocalOnHeapMatrix() { - // No-op. - } - - /** - * Construct new {@link SparseLocalOnHeapMatrix}. - * - * By default storage sets in row optimized mode and in random access mode. - */ - public SparseLocalOnHeapMatrix(int rows, int cols) { - setStorage(mkStorage(rows, cols)); - } - - /** - * Create new {@link SparseLocalOnHeapMatrixStorage}. - */ - private MatrixStorage mkStorage(int rows, int cols) { - return new SparseLocalOnHeapMatrixStorage(rows, cols, StorageConstants.RANDOM_ACCESS_MODE, StorageConstants.ROW_STORAGE_MODE); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return new SparseLocalOnHeapMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new SparseLocalVector(crd, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** {@inheritDoc} */ - @Override public int nonZeroElements() { - int res = 0; - IntIterator rowIter = indexesMap().keySet().iterator(); - - while (rowIter.hasNext()) { - int row = rowIter.nextInt(); - res += indexesMap().get(row).size(); - } - - return res; - } - - /** */ - public Int2ObjectArrayMap<IntSet> indexesMap() { - return ((SparseLocalOnHeapMatrixStorage)getStorage()).indexesMap(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - Matrix cp = like(rowSize(), columnSize()); - - cp.assign(this); - - return cp; - } - - /** {@inheritDoc} */ - @Override public void compute(int row, int col, IgniteTriFunction<Integer, Integer, Double, Double> f) { - ((SparseLocalOnHeapMatrixStorage)getStorage()).compute(row, col, f); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/package-info.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/package-info.java deleted file mode 100644 index 9eabf80..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/package-info.java +++ /dev/null @@ -1,22 +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 description. --> - * Contains several matrix implementations. - */ -package org.apache.ignite.ml.math.impls.matrix; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/package-info.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/package-info.java deleted file mode 100644 index d531014..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/package-info.java +++ /dev/null @@ -1,22 +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 description. --> - * Contains specific implementations for core algebra. - */ -package org.apache.ignite.ml.math.impls; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/ArrayMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/ArrayMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/ArrayMatrixStorage.java deleted file mode 100644 index da865d6..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/ArrayMatrixStorage.java +++ /dev/null @@ -1,225 +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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Arrays; -import org.apache.ignite.ml.math.Blas; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.functions.IgniteIntIntToIntBiFunction; -import org.apache.ignite.ml.math.util.MatrixUtil; - -/** - * Array based {@link MatrixStorage} implementation. - */ -public class ArrayMatrixStorage implements MatrixStorage { - /** Backing data array. */ - private double[] data; - /** Amount of rows in the matrix. */ - private int rows; - /** Amount of columns in the matrix. */ - private int cols; - /** Mode specifying if this matrix is row-major or column-major. */ - private int stoMode; - /** Index mapper */ - private IgniteIntIntToIntBiFunction idxMapper; - - /** - * - */ - public ArrayMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public ArrayMatrixStorage(int rows, int cols) { - this(rows, cols, StorageConstants.ROW_STORAGE_MODE); - } - - /** */ - public ArrayMatrixStorage(int rows, int cols, int stoMode) { - assert rows > 0; - assert cols > 0; - - this.data = new double[rows * cols]; - this.rows = rows; - this.cols = cols; - idxMapper = indexMapper(stoMode); - this.stoMode = stoMode; - } - - /** - * @param data Backing data array. - */ - public ArrayMatrixStorage(double[][] data, int stoMode) { - this(MatrixUtil.flatten(data, stoMode), data.length, stoMode); - } - - /** - * @param data Backing data array. - */ - public ArrayMatrixStorage(double[][] data) { - this(MatrixUtil.flatten(data, StorageConstants.ROW_STORAGE_MODE), data.length); - } - - /** - * @param data Backing data array. - */ - public ArrayMatrixStorage(double[] data, int rows, int stoMode) { - assert data != null; - assert data.length % rows == 0; - - this.data = data; - this.rows = rows; - this.cols = data.length / rows; - idxMapper = indexMapper(stoMode); - this.stoMode = stoMode; - - assert rows > 0; - assert cols > 0; - } - - /** - * @param data Backing data array. - */ - public ArrayMatrixStorage(double[] data, int rows) { - this(data, rows, StorageConstants.ROW_STORAGE_MODE); - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return data[idxMapper.apply(x, y)]; - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - data[idxMapper.apply(x, y)] = v; - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return true; - } - - /** {@inheritDoc} */ - @Override public double[] data() { - return data; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return stoMode; - } - - /** - * Get the index mapper for given access mode. - * - * NB: inverted for {@link Blas}. - * - * @param stoMode Access mode. - */ - private IgniteIntIntToIntBiFunction indexMapper(int stoMode) { - return stoMode == StorageConstants.COLUMN_STORAGE_MODE ? (r, c) -> r * cols + c : - (r, c) -> c * rows + r; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeInt(stoMode); - - out.writeObject(data); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - stoMode = in.readInt(); - idxMapper = indexMapper(stoMode); - - data = (double[])in.readObject(); - } - - /** Get the access mode of this storage. */ - public int accessMode() { - return stoMode; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res += res * 37 + rows; - res += res * 37 + cols; - res += res * 37 + stoMode; - res += res * 37 + Arrays.hashCode(data); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - ArrayMatrixStorage that = (ArrayMatrixStorage)o; - - return stoMode == that.stoMode && Arrays.equals(data, that.data); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DenseOffHeapMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DenseOffHeapMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DenseOffHeapMatrixStorage.java deleted file mode 100644 index 37801d5..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DenseOffHeapMatrixStorage.java +++ /dev/null @@ -1,217 +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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.internal.util.GridUnsafe; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; - -/** - * Local, dense off-heap matrix storage. - */ -public class DenseOffHeapMatrixStorage implements MatrixStorage { - /** */ - private int rows; - /** */ - private int cols; - /** */ - private transient long ptr; - //TODO: IGNITE-5535, temp solution. - /** */ - private int ptrInitHash; - - /** */ - public DenseOffHeapMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public DenseOffHeapMatrixStorage(int rows, int cols) { - assert rows > 0; - assert cols > 0; - - this.rows = rows; - this.cols = cols; - - allocateMemory(rows, cols); - } - - /** - * @param data Backing data array. - */ - public DenseOffHeapMatrixStorage(double[][] data) { - assert data != null; - assert data[0] != null; - - this.rows = data.length; - this.cols = data[0].length; - - assert rows > 0; - assert cols > 0; - - allocateMemory(rows, cols); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - set(i, j, data[i][j]); - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return GridUnsafe.getDouble(pointerOffset(x, y)); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - GridUnsafe.putDouble(pointerOffset(x, y), v); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return StorageConstants.ROW_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return StorageConstants.RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public double[] data() { - return null; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeInt(ptrInitHash); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - out.writeDouble(get(i, j)); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - - allocateMemory(rows, cols); - - ptrInitHash = in.readInt(); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - set(i, j, in.readDouble()); - } - - /** {@inheritDoc} */ - @Override public void destroy() { - GridUnsafe.freeMemory(ptr); - } - - /** */ - private long pointerOffset(int x, int y) { - return ptr + x * cols * Double.BYTES + y * Double.BYTES; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - return obj != null && - getClass().equals(obj.getClass()) && - (rows == ((DenseOffHeapMatrixStorage)obj).rows) && - (cols == ((DenseOffHeapMatrixStorage)obj).cols) && - (rows == 0 || cols == 0 || ptr == ((DenseOffHeapMatrixStorage)obj).ptr || isMemoryEquals((DenseOffHeapMatrixStorage)obj)); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + rows; - res = res * 37 + cols; - res = res * 37 + ptrInitHash; - - return res; - } - - /** */ - private boolean isMemoryEquals(DenseOffHeapMatrixStorage otherStorage) { - boolean res = true; - - for (int i = 0; i < otherStorage.rows; i++) { - for (int j = 0; j < otherStorage.cols; j++) { - if (Double.compare(get(i, j), otherStorage.get(i, j)) != 0) { - res = false; - break; - } - } - } - - return res; - } - - /** */ - private void allocateMemory(int rows, int cols) { - ptr = GridUnsafe.allocateMemory((long)rows * cols * Double.BYTES); - - ptrInitHash = Long.hashCode(ptr); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MapWrapperStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MapWrapperStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MapWrapperStorage.java deleted file mode 100644 index 3a02e44..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MapWrapperStorage.java +++ /dev/null @@ -1,120 +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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Map; -import java.util.Set; -import org.apache.ignite.internal.util.GridArgumentCheck; -import org.apache.ignite.ml.math.VectorStorage; - -/** - * Storage for wrapping given map. - */ -public class MapWrapperStorage implements VectorStorage { - /** Underlying map. */ - private Map<Integer, Double> data; - - /** Vector size. */ - private int size; - - /** - * Construct a wrapper around given map. - * - * @param map Map to wrap. - */ - public MapWrapperStorage(Map<Integer, Double> map) { - data = map; - - Set<Integer> keys = map.keySet(); - - GridArgumentCheck.notEmpty(keys, "map"); - - Integer min = keys.stream().mapToInt(Integer::valueOf).min().getAsInt(); - Integer max = keys.stream().mapToInt(Integer::valueOf).max().getAsInt(); - - assert min >= 0; - - size = (max - min) + 1; - } - - /** - * No-op constructor for serialization. - */ - public MapWrapperStorage() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - return data.getOrDefault(i, 0.0); - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - if (v != 0.0) - data.put(i, v); - else if (data.containsKey(i)) - data.remove(i); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(data); - out.writeInt(size); - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - data = (Map<Integer, Double>)in.readObject(); - size = in.readInt(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MatrixDelegateStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MatrixDelegateStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MatrixDelegateStorage.java deleted file mode 100644 index 7dc37cd..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/MatrixDelegateStorage.java +++ /dev/null @@ -1,215 +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.ignite.ml.math.impls.storage.matrix; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.MatrixStorage; - -/** - * {@link MatrixStorage} implementation that delegates to parent matrix. - */ -public class MatrixDelegateStorage implements MatrixStorage { - /** Parent matrix storage. */ - private MatrixStorage dlg; - - /** Row offset in the parent matrix. */ - private int rowOff; - /** Column offset in the parent matrix. */ - private int colOff; - - /** Amount of rows in the matrix. */ - private int rows; - /** Amount of columns in the matrix. */ - private int cols; - - /** - * - */ - public MatrixDelegateStorage() { - // No-op. - } - - /** - * @param dlg Backing parent storage. - * @param rowOff Row offset to parent matrix. - * @param colOff Column offset to parent matrix. - * @param rows Amount of rows in the view. - * @param cols Amount of columns in the view. - */ - public MatrixDelegateStorage(MatrixStorage dlg, int rowOff, int colOff, int rows, int cols) { - assert dlg != null; - assert rowOff >= 0; - assert colOff >= 0; - assert rows > 0; - assert cols > 0; - - this.dlg = dlg; - - this.rowOff = rowOff; - this.colOff = colOff; - - this.rows = rows; - this.cols = cols; - } - - /** - * - */ - public MatrixStorage delegate() { - return dlg; - } - - /** - * - */ - public int rowOffset() { - return rowOff; - } - - /** - * - */ - public int columnOffset() { - return colOff; - } - - /** - * - */ - public int rowsLength() { - return rows; - } - - /** - * - */ - public int columnsLength() { - return cols; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return dlg.get(rowOff + x, colOff + y); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - dlg.set(rowOff + x, colOff + y, v); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return dlg.storageMode(); - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return dlg.accessMode(); - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return dlg.isArrayBased() && rowOff == 0 && colOff == 0; - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return dlg.isSequentialAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return dlg.isDense(); - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return dlg.isRandomAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return dlg.isDistributed(); - } - - /** {@inheritDoc} */ - @Override public double[] data() { - return dlg.data(); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(dlg); - - out.writeInt(rowOff); - out.writeInt(colOff); - - out.writeInt(rows); - out.writeInt(cols); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - dlg = (MatrixStorage)in.readObject(); - - rowOff = in.readInt(); - colOff = in.readInt(); - - rows = in.readInt(); - cols = in.readInt(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + rows; - res = res * 37 + cols; - res = res * 37 + rowOff; - res = res * 37 + colOff; - res = res * 37 + dlg.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - MatrixDelegateStorage that = (MatrixDelegateStorage)o; - - return rows == that.rows && cols == that.cols && rowOff == that.rowOff && colOff == that.colOff && - (dlg != null ? dlg.equals(that.dlg) : that.dlg == null); - } -}
