http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/FunctionMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/FunctionMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/FunctionMatrixStorage.java deleted file mode 100644 index 18cc108..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/FunctionMatrixStorage.java +++ /dev/null @@ -1,190 +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; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IntIntDoubleToVoidFunction; -import org.apache.ignite.ml.math.functions.IntIntToDoubleFunction; - -/** - * Read-only or read-write function-based matrix storage. - */ -public class FunctionMatrixStorage implements MatrixStorage { - /** */ - private int rows; - /** */ - private int cols; - - /** */ - private IntIntToDoubleFunction getFunc; - /** */ - private IntIntDoubleToVoidFunction setFunc; - - /** - * - */ - public FunctionMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param getFunc Function that returns value corresponding to given row and column index. - * @param setFunc Set function. If {@code null} - this will be a read-only matrix. - */ - public FunctionMatrixStorage(int rows, int cols, IntIntToDoubleFunction getFunc, - IntIntDoubleToVoidFunction setFunc) { - assert rows > 0; - assert cols > 0; - assert getFunc != null; - - this.rows = rows; - this.cols = cols; - this.getFunc = getFunc; - this.setFunc = setFunc; - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param getFunc Function that returns value corresponding to given row and column index. - */ - public FunctionMatrixStorage(int rows, int cols, IntIntToDoubleFunction getFunc) { - this(rows, cols, getFunc, null); - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return getFunc.apply(x, y); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - if (setFunc != null) - setFunc.apply(x, y, v); - else - throw new UnsupportedOperationException("Cannot set into read-only matrix."); - } - - /** - * @return Getter function. - */ - public IntIntToDoubleFunction getFunction() { - return getFunc; - } - - /** - * @return Setter function. - */ - public IntIntDoubleToVoidFunction setFunction() { - return setFunc; - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(setFunc); - out.writeObject(getFunc); - out.writeInt(rows); - out.writeInt(cols); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - setFunc = (IntIntDoubleToVoidFunction)in.readObject(); - getFunc = (IntIntToDoubleFunction)in.readObject(); - rows = in.readInt(); - cols = in.readInt(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return StorageConstants.UNKNOWN_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return StorageConstants.RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - FunctionMatrixStorage that = (FunctionMatrixStorage)o; - - return rows == that.rows && cols == that.cols - && (getFunc != null ? getFunc.equals(that.getFunc) : that.getFunc == null) - && (setFunc != null ? setFunc.equals(that.setFunc) : that.setFunc == null); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = rows; - - res = 31 * res + cols; - res = 31 * res + (getFunc != null ? getFunc.hashCode() : 0); - res = 31 * res + (setFunc != null ? setFunc.hashCode() : 0); - - return res; - } -}
http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/PivotedMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/PivotedMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/PivotedMatrixStorage.java deleted file mode 100644 index 387b347..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/PivotedMatrixStorage.java +++ /dev/null @@ -1,266 +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.MatrixStorage; - -/** - * Pivoted (index mapped) view over another matrix storage implementation. - */ -public class PivotedMatrixStorage implements MatrixStorage { - /** Matrix storage. */ - private MatrixStorage sto; - - /** */ - private int[] rowPivot; - /** */ - private int[] colPivot; - /** */ - private int[] rowUnpivot; - /** */ - private int[] colUnpivot; - - /** - * - */ - public PivotedMatrixStorage() { - // No-op. - } - - /** - * @param sto Matrix storage. - * @param rowPivot Pivot array for rows. - * @param colPivot Pivot array for columns. - */ - public PivotedMatrixStorage(MatrixStorage sto, int[] rowPivot, int[] colPivot) { - assert sto != null; - assert rowPivot != null; - assert colPivot != null; - - this.sto = sto; - this.rowPivot = rowPivot; - this.colPivot = colPivot; - - rowUnpivot = invert(rowPivot); - colUnpivot = invert(colPivot); - } - - /** - * - */ - public int[] rowPivot() { - return rowPivot; - } - - /** - * - */ - public int[] columnPivot() { - return colPivot; - } - - /** - * - */ - public int[] rowUnpivot() { - return rowUnpivot; - } - - /** - * - */ - public int[] columnUnpivot() { - return colUnpivot; - } - - /** - * @param sto Matrix storage. - * @param pivot Pivot array. - */ - public PivotedMatrixStorage(MatrixStorage sto, int[] pivot) { - this(sto, pivot, pivot == null ? null : java.util.Arrays.copyOf(pivot, pivot.length)); - } - - /** - * @param sto Matrix storage. - */ - public PivotedMatrixStorage(MatrixStorage sto) { - this(sto, sto == null ? null : identityPivot(sto.rowSize()), sto == null ? null : identityPivot(sto.columnSize())); - } - - /** - * @param i First row index to swap. - * @param j Second row index to swap. - */ - public void swapRows(int i, int j) { - if (i != j) { - int tmp = rowPivot[i]; - - rowPivot[i] = rowPivot[j]; - rowPivot[j] = tmp; - - rowUnpivot[rowPivot[i]] = i; - rowUnpivot[rowPivot[j]] = j; - } - } - - /** - * @param i First column index to swap. - * @param j Second column index to swap. - */ - public void swapColumns(int i, int j) { - if (i != j) { - int tmp = colPivot[i]; - - colPivot[i] = colPivot[j]; - colPivot[j] = tmp; - - colUnpivot[colPivot[i]] = i; - colUnpivot[colPivot[j]] = j; - } - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return sto.get(rowPivot[x], colPivot[y]); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - sto.set(rowPivot[x], colPivot[y], v); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return sto.columnSize(); - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return sto.rowSize(); - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return sto.storageMode(); - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return sto.accessMode(); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(sto); - out.writeObject(rowPivot); - out.writeObject(colPivot); - out.writeObject(rowUnpivot); - out.writeObject(colUnpivot); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - sto = (MatrixStorage)in.readObject(); - rowPivot = (int[])in.readObject(); - colPivot = (int[])in.readObject(); - rowUnpivot = (int[])in.readObject(); - colUnpivot = (int[])in.readObject(); - } - - /** {@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 false; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + sto.hashCode(); - res = res * 37 + Arrays.hashCode(rowPivot); - res = res * 37 + Arrays.hashCode(rowUnpivot); - res = res * 37 + Arrays.hashCode(colPivot); - res = res * 37 + Arrays.hashCode(colUnpivot); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null || getClass() != obj.getClass()) - return false; - - PivotedMatrixStorage that = (PivotedMatrixStorage)obj; - - return Arrays.equals(rowPivot, that.rowPivot) && Arrays.equals(rowUnpivot, that.rowUnpivot) - && Arrays.equals(colPivot, that.colPivot) && Arrays.equals(colUnpivot, that.colUnpivot) - && (sto != null ? sto.equals(that.sto) : that.sto == null); - } - - /** - * @param n Pivot array length. - */ - private static int[] identityPivot(int n) { - int[] pivot = new int[n]; - - for (int i = 0; i < n; i++) - pivot[i] = i; - - return pivot; - } - - /** - * @param pivot Pivot array to be inverted. - */ - private static int[] invert(int[] pivot) { - int[] x = new int[pivot.length]; - - for (int i = 0; i < pivot.length; i++) - x[pivot[i]] = i; - - return x; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/RandomMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/RandomMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/RandomMatrixStorage.java deleted file mode 100644 index 56bd871..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/RandomMatrixStorage.java +++ /dev/null @@ -1,187 +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.nio.ByteBuffer; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.MurmurHash; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; - -/** - * {@link MatrixStorage} implementation with random values in the matrix elements. - */ -public class RandomMatrixStorage implements MatrixStorage { - /** */ - private static final int PRIME1 = 104047; - /** */ - private static final int PRIME2 = 101377; - /** */ - private static final int PRIME3 = 64661; - /** */ - private static final long SCALE = 1L << 32; - - /** Random generation seed. */ - private int seed; - - /** Amount of rows in the matrix. */ - private int rows; - /** Amount of columns in the matrix. */ - private int cols; - - /** Whether fast hash is used, in {@link #get(int, int)}. */ - private boolean fastHash; - - /** - * For externalization. - */ - public RandomMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param fastHash Whether fast hash is used. - */ - public RandomMatrixStorage(int rows, int cols, boolean fastHash) { - assert rows > 0; - assert cols > 0; - - this.rows = rows; - this.cols = cols; - this.fastHash = fastHash; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - if (!fastHash) { - ByteBuffer buf = ByteBuffer.allocate(8); - - buf.putInt(x); - buf.putInt(y); - buf.flip(); - - return (MurmurHash.hash64A(buf, seed) & (SCALE - 1)) / (double)SCALE; - } - else - // This isn't a fantastic random number generator, but it is just fine for random projections. - return ((((x * PRIME1) + y * PRIME2 + x * y * PRIME3) & 8) * 0.25) - 1; - } - - /** - * - */ - public boolean isFastHash() { - return fastHash; - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - throw new UnsupportedOperationException("Random matrix storage is a read-only storage."); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeInt(seed); - out.writeBoolean(fastHash); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - seed = in.readInt(); - fastHash = in.readBoolean(); - } - - /** {@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 boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return StorageConstants.UNKNOWN_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return StorageConstants.RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + Boolean.hashCode(fastHash); - res = res * 37 + seed; - res = res * 37 + cols; - res = res * 37 + rows; - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - RandomMatrixStorage that = (RandomMatrixStorage)o; - - return rows == that.rows && cols == that.cols && seed == that.seed && fastHash == that.fastHash; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorage.java deleted file mode 100644 index 9b4a189..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorage.java +++ /dev/null @@ -1,330 +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 it.unimi.dsi.fastutil.ints.Int2DoubleOpenHashMap; -import it.unimi.dsi.fastutil.ints.Int2DoubleRBTreeMap; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Map; -import java.util.Set; -import java.util.UUID; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.CacheAtomicityMode; -import org.apache.ignite.cache.CacheMode; -import org.apache.ignite.cache.CachePeekMode; -import org.apache.ignite.cache.CacheWriteSynchronizationMode; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.DistributedStorage; -import org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey; -import org.apache.ignite.ml.math.distributed.keys.impl.SparseMatrixKey; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; - -/** - * {@link MatrixStorage} implementation for {@link SparseDistributedMatrix}. - */ -public class SparseDistributedMatrixStorage extends CacheUtils implements MatrixStorage, StorageConstants, DistributedStorage<RowColMatrixKey> { - /** Cache name used for all instances of {@link SparseDistributedMatrixStorage}. */ - private static final String CACHE_NAME = "ML_SPARSE_MATRICES_CONTAINER"; - - /** Amount of rows in the matrix. */ - private int rows; - - /** Amount of columns in the matrix. */ - private int cols; - - /** Row or column based storage mode. */ - private int stoMode; - - /** Random or sequential access mode. */ - private int acsMode; - - /** Matrix uuid. */ - private UUID uuid; - - /** Actual distributed storage. */ - private IgniteCache< - RowColMatrixKey /* Row or column index with matrix uuid. */, - Map<Integer, Double> /* Map-based row or column. */ - > cache = null; - - /** - * - */ - public SparseDistributedMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param stoMode Row or column based storage mode. - * @param acsMode Random or sequential access mode. - */ - public SparseDistributedMatrixStorage(int rows, int cols, int stoMode, int acsMode) { - assert rows > 0; - assert cols > 0; - assertAccessMode(acsMode); - assertStorageMode(stoMode); - - this.rows = rows; - this.cols = cols; - this.stoMode = stoMode; - this.acsMode = acsMode; - - cache = newCache(); - - uuid = UUID.randomUUID(); - } - - /** - * Create new ML cache if needed. - */ - private IgniteCache<RowColMatrixKey, Map<Integer, Double>> newCache() { - CacheConfiguration<RowColMatrixKey, Map<Integer, Double>> cfg = new CacheConfiguration<>(); - - // Write to primary. - cfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.PRIMARY_SYNC); - - // Atomic transactions only. - cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); - - // No eviction. - cfg.setEvictionPolicy(null); - - // No copying of values. - cfg.setCopyOnRead(false); - - // Cache is partitioned. - cfg.setCacheMode(CacheMode.PARTITIONED); - - // TODO: Possibly we should add a fix of https://issues.apache.org/jira/browse/IGNITE-6862 here commented below. - // cfg.setReadFromBackup(false); - - // Random cache name. - cfg.setName(CACHE_NAME); - - return Ignition.localIgnite().getOrCreateCache(cfg); - } - - /** - * - */ - public IgniteCache<RowColMatrixKey, Map<Integer, Double>> cache() { - return cache; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return acsMode; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - if (stoMode == ROW_STORAGE_MODE) - return matrixGet(x, y); - else - return matrixGet(y, x); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - if (stoMode == ROW_STORAGE_MODE) - matrixSet(x, y, v); - else - matrixSet(y, x, v); - } - - /** - * Distributed matrix get. - * - * @param a Row or column index. - * @param b Row or column index. - * @return Matrix value at (a, b) index. - */ - private double matrixGet(int a, int b) { - // Remote get from the primary node (where given row or column is stored locally). - return ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, a)).call(() -> { - IgniteCache<RowColMatrixKey, Map<Integer, Double>> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - // Local get. - Map<Integer, Double> map = cache.localPeek(getCacheKey(a), CachePeekMode.PRIMARY); - - if (map == null) - map = cache.get(getCacheKey(a)); - - return (map == null || !map.containsKey(b)) ? 0.0 : map.get(b); - }); - } - - /** - * Distributed matrix set. - * - * @param a Row or column index. - * @param b Row or column index. - * @param v New value to set. - */ - private void matrixSet(int a, int b, double v) { - // Remote set on the primary node (where given row or column is stored locally). - ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, a)).run(() -> { - IgniteCache<RowColMatrixKey, Map<Integer, Double>> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - // Local get. - Map<Integer, Double> map = cache.localPeek(getCacheKey(a), CachePeekMode.PRIMARY); - - if (map == null) { - map = cache.get(getCacheKey(a)); //Remote entry get. - - if (map == null) - map = acsMode == SEQUENTIAL_ACCESS_MODE ? new Int2DoubleRBTreeMap() : new Int2DoubleOpenHashMap(); - } - - if (v != 0.0) - map.put(b, v); - else if (map.containsKey(b)) - map.remove(b); - - // Local put. - cache.put(getCacheKey(a), map); - }); - } - - /** Build cache key for row/column. */ - public RowColMatrixKey getCacheKey(int idx) { - return new SparseMatrixKey(idx, uuid, idx); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return stoMode; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeInt(acsMode); - out.writeInt(stoMode); - out.writeObject(uuid); - out.writeUTF(cache.getName()); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - acsMode = in.readInt(); - stoMode = in.readInt(); - uuid = (UUID)in.readObject(); - cache = ignite().getOrCreateCache(in.readUTF()); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return acsMode == SEQUENTIAL_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return acsMode == RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** Delete all data from cache. */ - @Override public void destroy() { - Set<RowColMatrixKey> keyset = IntStream.range(0, rows).mapToObj(this::getCacheKey).collect(Collectors.toSet()); - - cache.clearAll(keyset); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + cols; - res = res * 37 + rows; - res = res * 37 + acsMode; - res = res * 37 + stoMode; - res = res * 37 + uuid.hashCode(); - res = res * 37 + cache.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null || getClass() != obj.getClass()) - return false; - - SparseDistributedMatrixStorage that = (SparseDistributedMatrixStorage)obj; - - return rows == that.rows && cols == that.cols && acsMode == that.acsMode && stoMode == that.stoMode - && uuid.equals(that.uuid) && (cache != null ? cache.equals(that.cache) : that.cache == null); - } - - /** */ - public UUID getUUID() { - return uuid; - } - - /** {@inheritDoc} */ - @Override public Set<RowColMatrixKey> getAllKeys() { - int range = stoMode == ROW_STORAGE_MODE ? rows : cols; - - return IntStream.range(0, range).mapToObj(i -> new SparseMatrixKey(i, getUUID(), i)).collect(Collectors.toSet()); - } - - /** {@inheritDoc} */ - @Override public String cacheName() { - return CACHE_NAME; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/CacheVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/CacheVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/CacheVectorStorage.java deleted file mode 100644 index c4bb995..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/CacheVectorStorage.java +++ /dev/null @@ -1,175 +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.vector; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.distributed.ValueMapper; -import org.apache.ignite.ml.math.distributed.VectorKeyMapper; - -/** - * Vector storage based on existing cache and index and value mapping functions. - */ -public class CacheVectorStorage<K, V> implements VectorStorage { - /** Storage size. */ - private int size; - - /** Key mapper. */ - private VectorKeyMapper<K> keyMapper; - /** Value mapper. */ - private ValueMapper<V> valMapper; - - /** Underlying ignite cache. */ - private IgniteCache<K, V> cache; - - /** - * - */ - public CacheVectorStorage() { - // No-op. - } - - /** - * @param size Vector size. - * @param cache Ignite cache. - * @param keyMapper {@link VectorKeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain value for given cache key. - */ - public CacheVectorStorage(int size, IgniteCache<K, V> cache, VectorKeyMapper<K> keyMapper, - ValueMapper<V> valMapper) { - assert size > 0; - assert cache != null; - assert keyMapper != null; - assert valMapper != null; - - this.size = size; - this.cache = cache; - this.keyMapper = keyMapper; - this.valMapper = valMapper; - } - - /** - * @return Ignite cache. - */ - public IgniteCache<K, V> cache() { - return cache; - } - - /** - * @return Key mapper to validate cache keys. - */ - public VectorKeyMapper<K> keyMapper() { - return keyMapper; - } - - /** - * @return Value mapper to obtain vector element values. - */ - public ValueMapper<V> valueMapper() { - return valMapper; - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - return valMapper.toDouble(cache.get(keyMapper.apply(i))); - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - cache.put(keyMapper.apply(i), valMapper.fromDouble(v)); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(size); - out.writeObject(keyMapper); - out.writeObject(valMapper); - out.writeUTF(cache.getName()); - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - size = in.readInt(); - keyMapper = (VectorKeyMapper<K>)in.readObject(); - valMapper = (ValueMapper<V>)in.readObject(); - cache = Ignition.localIgnite().getOrCreateCache(in.readUTF()); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + size(); - res = res * 37 + keyMapper.hashCode(); - res = res * 37 + valMapper.hashCode(); - res = res * 37 + cache.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null || getClass() != obj.getClass()) - return false; - - CacheVectorStorage that = (CacheVectorStorage)obj; - - return size == that.size - && (keyMapper != null ? keyMapper.getClass().equals(that.keyMapper.getClass()) : that.keyMapper == null) - && (valMapper != null ? valMapper.getClass().equals(that.valMapper.getClass()) : that.valMapper == null) - && (cache != null ? cache.equals(that.cache) : that.cache == null); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/ConstantVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/ConstantVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/ConstantVectorStorage.java deleted file mode 100644 index 0423bc5..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/ConstantVectorStorage.java +++ /dev/null @@ -1,134 +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.vector; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; - -/** - * Constant read-only vector storage. - */ -public class ConstantVectorStorage implements VectorStorage { - /** */ - private int size; - /** */ - private double val; - - /** - * - */ - public ConstantVectorStorage() { - // No-op. - } - - /** - * @param size Vector size. - * @param val Value to set for vector elements. - */ - public ConstantVectorStorage(int size, double val) { - assert size > 0; - - this.size = size; - this.val = val; - } - - /** - * @return Constant stored in Vector elements. - */ - public double constant() { - return val; - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - return val; - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - throw new UnsupportedOperationException("Can't set value into constant vector."); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(size); - out.writeDouble(val); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - size = in.readInt(); - val = in.readDouble(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + size; - res = res * 37 + Double.hashCode(val); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - ConstantVectorStorage that = (ConstantVectorStorage)o; - - return size == that.size && val == that.val; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/FunctionVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/FunctionVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/FunctionVectorStorage.java deleted file mode 100644 index aabe3b1..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/FunctionVectorStorage.java +++ /dev/null @@ -1,143 +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.vector; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.functions.IntDoubleToVoidFunction; - -/** - * Read-only or read-write function-based vector storage. - */ -public class FunctionVectorStorage implements VectorStorage { - /** */ - private IgniteFunction<Integer, Double> getFunc; - /** */ - private IntDoubleToVoidFunction setFunc; - /** */ - private int size; - - /** - * - */ - public FunctionVectorStorage() { - // No-op. - } - - /** - * Creates read-only or read-write storage. - * - * @param size Cardinality of this vector storage. - * @param getFunc Get function. - * @param setFunc Optional set function ({@code null} for read-only storage). - */ - public FunctionVectorStorage(int size, IgniteFunction<Integer, Double> getFunc, IntDoubleToVoidFunction setFunc) { - assert size > 0; - assert getFunc != null; // At least get function is required. - - this.size = size; - this.getFunc = getFunc; - this.setFunc = setFunc; - } - - /** - * @return Getter function. - */ - public IgniteFunction<Integer, Double> getFunction() { - return getFunc; - } - - /** - * @return Setter function. - */ - public IntDoubleToVoidFunction setFunction() { - return setFunc; - } - - /** - * Creates read-only storage. - * - * @param size Cardinality of this vector storage. - * @param getFunc Get function. - */ - public FunctionVectorStorage(int size, IgniteFunction<Integer, Double> getFunc) { - this(size, getFunc, null); - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - return getFunc.apply(i); - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - if (setFunc != null) - setFunc.accept(i, v); - else - throw new UnsupportedOperationException("Cannot set into read-only vector."); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(setFunc); - out.writeObject(getFunc); - out.writeInt(size); - } - - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - setFunc = (IntDoubleToVoidFunction)in.readObject(); - getFunc = (IgniteFunction<Integer, Double>)in.readObject(); - size = in.readInt(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/PivotedVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/PivotedVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/PivotedVectorStorage.java deleted file mode 100644 index 1c798e4..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/PivotedVectorStorage.java +++ /dev/null @@ -1,176 +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.vector; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Arrays; -import org.apache.ignite.ml.math.VectorStorage; - -/** - * Pivoted (index mapped) view over another vector storage implementation. - */ -public class PivotedVectorStorage implements VectorStorage { - /** */ - private VectorStorage sto; - - /** */ - private int[] pivot; - /** */ - private int[] unpivot; - - /** - * @param pivot Pivot array. - */ - private static int[] reverse(int[] pivot) { - int[] res = new int[pivot.length]; - - for (int i = 0; i < pivot.length; i++) - res[pivot[i]] = i; - - return res; - } - - /** - * @return Pivot array for this vector view. - */ - public int[] pivot() { - return pivot; - } - - /** - * @return Unpivot array for this vector view. - */ - public int[] unpivot() { - return unpivot; - } - - /** - * @param sto Backing vector storage. - * @param pivot Mapping from external index to internal. - * @param unpivot Mapping from internal index to external. - */ - public PivotedVectorStorage(VectorStorage sto, int[] pivot, int[] unpivot) { - assert sto != null; - assert pivot != null; - assert unpivot != null; - - this.sto = sto; - this.pivot = pivot; - this.unpivot = unpivot; - } - - /** - * @param sto Backing vector storage. - * @param pivot Mapping from external index to internal. - */ - public PivotedVectorStorage(VectorStorage sto, int[] pivot) { - this(sto, pivot, reverse(pivot)); - } - - /** - * - */ - public PivotedVectorStorage() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public int size() { - return sto.size(); - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - return sto.get(pivot[i]); - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - sto.set(pivot[i], v); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(sto); - out.writeObject(pivot); - out.writeObject(unpivot); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - sto = (VectorStorage)in.readObject(); - pivot = (int[])in.readObject(); - unpivot = (int[])in.readObject(); - } - - /** {@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(); - } - - /** {@inheritDoc} */ - @Override public double[] data() { - return isArrayBased() ? sto.data() : null; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - PivotedVectorStorage that = (PivotedVectorStorage)o; - - return (sto != null ? sto.equals(that.sto) : that.sto == null) && Arrays.equals(pivot, that.pivot) - && Arrays.equals(unpivot, that.unpivot); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = sto != null ? sto.hashCode() : 0; - - res = 31 * res + Arrays.hashCode(pivot); - res = 31 * res + Arrays.hashCode(unpivot); - - return res; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/RandomVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/RandomVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/RandomVectorStorage.java deleted file mode 100644 index be1ad91..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/RandomVectorStorage.java +++ /dev/null @@ -1,152 +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.vector; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.nio.ByteBuffer; -import java.util.Random; -import org.apache.ignite.ml.math.MurmurHash; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; - -/** - * {@link VectorStorage} implementation with random values in the vector elements. - */ -public class RandomVectorStorage implements VectorStorage { - /** */ - private static final long SCALE = 1L << 32; - /** */ - private static final int PRIME = 104047; - - /** Random generation seed. */ - private int seed; - - /** Vector size. */ - private int size; - - /** Whether fast hash is used, in {@link #get(int)}. */ - private boolean fastHash; - - /** */ - public RandomVectorStorage() { - // No-op. - } - - /** - * @param size Size of the storage. - * @param fastHash Whether or not to use fast hashing or Murmur hashing. - */ - public RandomVectorStorage(int size, boolean fastHash) { - assert size > 0; - - this.size = size; - this.fastHash = fastHash; - - seed = new Random().nextInt(); - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - if (!fastHash) { - ByteBuffer buf = ByteBuffer.allocate(4); - - buf.putInt(i); - buf.flip(); - - return (MurmurHash.hash64A(buf, seed) & (SCALE - 1)) / (double)SCALE; - } - else - // This isn't a fantastic random number generator, but it is just fine for random projections. - return (((i * PRIME) & 8) * 0.25) - 1; - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - throw new UnsupportedOperationException("Random vector storage is a read-only storage."); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(size); - out.writeInt(seed); - out.writeBoolean(fastHash); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - size = in.readInt(); - seed = in.readInt(); - fastHash = in.readBoolean(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + Boolean.hashCode(fastHash); - res = res * 37 + seed; - res = res * 37 + size; - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - RandomVectorStorage that = (RandomVectorStorage)o; - - return size == that.size && seed == that.seed && fastHash == that.fastHash; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorDelegateStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorDelegateStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorDelegateStorage.java deleted file mode 100644 index ac86e16..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorDelegateStorage.java +++ /dev/null @@ -1,145 +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.vector; - -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.VectorStorage; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; - -/** - * Single value view storage over another vector. - */ -public class SingleElementVectorDelegateStorage implements VectorStorage { - /** */ - private int idx; - /** */ - private Vector vec; - - /** - * - */ - public SingleElementVectorDelegateStorage() { - // No-op. - } - - /** - * @param vec Parent vector. - * @param idx Element index. - */ - public SingleElementVectorDelegateStorage(Vector vec, int idx) { - assert vec != null; - assert idx >= 0; - - this.vec = vec; - this.idx = idx; - } - - /** - * @return Index of the element in the parent vector. - */ - public int index() { - return idx; - } - - /** - * @return Parent vector. - */ - public Vector delegate() { - return vec; - } - - /** {@inheritDoc} */ - @Override public int size() { - return vec.size(); - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - return i == idx ? vec.get(i) : 0.0; - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - if (i == idx) - vec.set(i, v); - else - throw new UnsupportedOperationException("Can't set element outside of index: " + idx); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(vec); - out.writeInt(idx); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - vec = (Vector)in.readObject(); - idx = in.readInt(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - SingleElementVectorDelegateStorage that = (SingleElementVectorDelegateStorage)o; - - return idx == that.idx && (vec != null ? vec.equals(that.vec) : that.vec == null); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = idx; - - res = 31 * res + (vec != null ? vec.hashCode() : 0); - - return res; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorStorage.java deleted file mode 100644 index 488e158..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorStorage.java +++ /dev/null @@ -1,145 +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.vector; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; - -/** - * Vector storage holding a single non-zero value at some index. - */ -public class SingleElementVectorStorage implements VectorStorage { - /** */ - private int idx; - /** */ - private double val; - /** */ - private int size; - - /** - * - */ - public SingleElementVectorStorage() { - // No-op. - } - - /** - * @param size Parent vector size. - * @param idx Element index in the parent vector. - * @param val Value of the element. - */ - public SingleElementVectorStorage(int size, int idx, double val) { - assert size > 0; - assert idx >= 0; - - this.size = size; - this.idx = idx; - this.val = val; - } - - /** - * @return Index of the element in the parent vector. - */ - public int index() { - return idx; - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public double get(int i) { - return i == idx ? val : 0.0; - } - - /** {@inheritDoc} */ - @Override public void set(int i, double v) { - if (i == idx) - val = v; - else - throw new UnsupportedOperationException("Can't set element outside of index: " + idx); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(size); - out.writeInt(idx); - out.writeDouble(val); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - size = in.readInt(); - idx = in.readInt(); - val = in.readDouble(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - SingleElementVectorStorage that = (SingleElementVectorStorage)o; - - return idx == that.idx && Double.compare(that.val, val) == 0 && size == that.size; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = idx; - long temp = Double.doubleToLongBits(val); - - res = 31 * res + (int)(temp ^ (temp >>> 32)); - res = 31 * res + size; - - return res; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorage.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorage.java deleted file mode 100644 index 0496d05..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorage.java +++ /dev/null @@ -1,281 +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.vector; - -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Set; -import java.util.UUID; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.CacheAtomicityMode; -import org.apache.ignite.cache.CacheMode; -import org.apache.ignite.cache.CacheWriteSynchronizationMode; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.DistributedStorage; -import org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey; -import org.apache.ignite.ml.math.distributed.keys.impl.SparseMatrixKey; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector; - -/** - * {@link VectorStorage} implementation for {@link SparseDistributedVector}. - */ -public class SparseDistributedVectorStorage extends CacheUtils implements VectorStorage, StorageConstants, DistributedStorage<RowColMatrixKey> { - /** Cache name used for all instances of {@link SparseDistributedVectorStorage}. */ - private static final String CACHE_NAME = "ML_SPARSE_VECTORS_CONTAINER"; - - /** Amount of elements in the vector. */ - private int size; - - /** Random or sequential access mode. */ - private int acsMode; - - /** Matrix uuid. */ - private UUID uuid; - - /** Actual distributed storage. */ - private IgniteCache<RowColMatrixKey, Double> cache = null; - - /** - * - */ - public SparseDistributedVectorStorage() { - // No-op. - } - - /** - * @param size Amount of elements in the vector. - * @param acsMode Random or sequential access mode. - */ - public SparseDistributedVectorStorage(int size, int acsMode) { - - assert size > 0; - assertAccessMode(acsMode); - - this.size = size; - this.acsMode = acsMode; - - cache = newCache(); - - uuid = UUID.randomUUID(); - } - - /** - * Create new ML cache if needed. - */ - private IgniteCache<RowColMatrixKey, Double> newCache() { - CacheConfiguration<RowColMatrixKey, Double> cfg = new CacheConfiguration<>(); - - // Write to primary. - cfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.PRIMARY_SYNC); - - // Atomic transactions only. - cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC); - - // No eviction. - cfg.setEvictionPolicy(null); - - // No copying of values. - cfg.setCopyOnRead(false); - - // Cache is partitioned. - cfg.setCacheMode(CacheMode.PARTITIONED); - - // Random cache name. - cfg.setName(CACHE_NAME); - - return Ignition.localIgnite().getOrCreateCache(cfg); - } - - /** - * Gets cache - * - * @return cache - */ - public IgniteCache<RowColMatrixKey, Double> cache() { - return cache; - } - - /** - * Gets access mode - * - * @return code of access mode - */ - public int accessMode() { - return acsMode; - } - - /** - * Gets vector element by element index - * - * @param i Vector element index. - * @return vector element - */ - @Override public double get(int i) { - // Remote get from the primary node (where given row or column is stored locally). - return ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getCacheKey(i))).call(() -> { - IgniteCache<RowColMatrixKey, Double> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - Double res = cache.get(getCacheKey(i)); - if (res == null) - return 0.0; - return res; - }); - } - - /** - * Sets vector element by index - * - * @param i Vector element index. - * @param v Value to set at given index. - */ - @Override public void set(int i, double v) { - // Remote set on the primary node (where given row or column is stored locally). - ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getCacheKey(i))).run(() -> { - IgniteCache<RowColMatrixKey, Double> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - RowColMatrixKey cacheKey = getCacheKey(i); - - if (v != 0.0) - cache.put(cacheKey, v); - else if (cache.containsKey(cacheKey)) // remove zero elements - cache.remove(cacheKey); - - }); - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(size); - out.writeInt(acsMode); - out.writeObject(uuid); - out.writeUTF(cache.getName()); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - size = in.readInt(); - acsMode = in.readInt(); - uuid = (UUID)in.readObject(); - cache = ignite().getOrCreateCache(in.readUTF()); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return acsMode == SEQUENTIAL_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return false; - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return acsMode == RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return true; - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** Delete all data from cache. */ - @Override public void destroy() { - Set<RowColMatrixKey> keyset = IntStream.range(0, size).mapToObj(this::getCacheKey).collect(Collectors.toSet()); - cache.clearAll(keyset); - } - - /** - * Builds cache key for vector element. - * - * @param idx Index. - * @return RowColMatrixKey. - */ - public RowColMatrixKey getCacheKey(int idx) { - return new SparseMatrixKey(idx, uuid, null); - } - - /** {@inheritDoc} */ - @Override public Set<RowColMatrixKey> getAllKeys() { - int range = size; - - return IntStream.range(0, range).mapToObj(i -> new SparseMatrixKey(i, getUUID(), null)).collect(Collectors.toSet()); - } - - /** {@inheritDoc} */ - @Override public String cacheName() { - return CACHE_NAME; - } - - /** */ - public UUID getUUID() { - return uuid; - } - - /** {@inheritDoc} */ - @Override public double[] data() { - double[] res = new double[this.size]; - - for (int i = 0; i < this.size; i++) - res[i] = this.get(i); - - return res; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + size; - res = res * 37 + acsMode; - res = res * 37 + uuid.hashCode(); - res = res * 37 + cache.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null || getClass() != obj.getClass()) - return false; - - SparseDistributedVectorStorage that = (SparseDistributedVectorStorage)obj; - - return size == that.size && acsMode == that.acsMode - && uuid.equals(that.uuid) && (cache != null ? cache.equals(that.cache) : that.cache == null); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/AbstractReadOnlyVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/AbstractReadOnlyVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/AbstractReadOnlyVector.java deleted file mode 100644 index 1de334f..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/AbstractReadOnlyVector.java +++ /dev/null @@ -1,131 +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.vector; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.functions.IgniteBiFunction; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.functions.IgniteIntDoubleToDoubleBiFunction; -import org.apache.ignite.ml.math.impls.matrix.FunctionMatrix; - -/** - * This class provides a helper implementation of the read-only implementation of {@link Vector} - * 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 AbstractReadOnlyVector extends AbstractVector { - /** */ - public AbstractReadOnlyVector() { - // No-op. - } - - /** - * @param sto Storage. - */ - public AbstractReadOnlyVector(VectorStorage sto) { - super(true, sto); - } - - /** {@inheritDoc} */ - @Override public Matrix cross(Vector vec) { - return new FunctionMatrix(size(), vec.size(), - (row, col) -> vec.get(col) * get(row)); - } - - /** {@inheritDoc} */ - @Override public Matrix toMatrix(boolean rowLike) { - return new FunctionMatrix(rowLike ? 1 : size(), rowLike ? size() : 1, - (row, col) -> rowLike ? get(col) : get(row)); - } - - /** {@inheritDoc} */ - @Override public Matrix toMatrixPlusOne(boolean rowLike, double zeroVal) { - return new FunctionMatrix(rowLike ? 1 : size() + 1, rowLike ? size() + 1 : 1, (row, col) -> { - if (row == 0 && col == 0) - return zeroVal; - - return rowLike ? get(col - 1) : get(row - 1); - }); - } - - /** {@inheritDoc} */ - @Override public Vector copy() { - return this; // This exploits read-only feature of this type vector. - } - - /** {@inheritDoc} */ - @Override public Vector logNormalize() { - return logNormalize(2.0, Math.sqrt(getLengthSquared())); - } - - /** {@inheritDoc} */ - @Override public Vector logNormalize(double power) { - return logNormalize(power, kNorm(power)); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteDoubleFunction<Double> fun) { - return new FunctionVector(size(), (i) -> fun.apply(get(i))); - } - - /** {@inheritDoc} */ - @Override public Vector map(Vector vec, IgniteBiFunction<Double, Double, Double> fun) { - checkCardinality(vec); - - return new FunctionVector(size(), (i) -> fun.apply(get(i), vec.get(i))); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteBiFunction<Double, Double, Double> fun, double y) { - return new FunctionVector(size(), (i) -> fun.apply(get(i), y)); - } - - /** {@inheritDoc} */ - @Override public Vector divide(double x) { - if (x == 1.0) - return this; - - return new FunctionVector(size(), (i) -> get(i) / x); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - return x == 0 ? new ConstantVector(size(), 0) : new FunctionVector(size(), (i) -> get(i) * x); - } - - /** - * @param power Power. - * @param normLen Normalized length. - * @return logNormalized value. - */ - private Vector logNormalize(double power, double normLen) { - assert !(Double.isInfinite(power) || power <= 1.0); - - double denominator = normLen * Math.log(power); - - return new FunctionVector(size(), (idx) -> Math.log1p(get(idx)) / denominator); - } - - /** {@inheritDoc} */ - @Override public void compute(int idx, IgniteIntDoubleToDoubleBiFunction f) { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/CacheVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/CacheVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/CacheVector.java deleted file mode 100644 index 676f271..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/CacheVector.java +++ /dev/null @@ -1,140 +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.vector; - -import org.apache.ignite.IgniteCache; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.ValueMapper; -import org.apache.ignite.ml.math.distributed.VectorKeyMapper; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -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.impls.storage.vector.CacheVectorStorage; - -/** - * Vector based on existing cache and index and value mapping functions. - */ -public class CacheVector<K, V> extends AbstractVector { - /** - * - */ - public CacheVector() { - // No-op. - } - - /** - * Creates new vector over existing cache. - * - * @param size Vector size. - * @param cache Ignite cache. - * @param keyFunc {@link VectorKeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain value for given cache key. - */ - public CacheVector( - int size, - IgniteCache<K, V> cache, - VectorKeyMapper<K> keyFunc, - ValueMapper<V> valMapper) { - setStorage(new CacheVectorStorage<>(size, cache, keyFunc, valMapper)); - } - - /** - * @param mapper Mapping function. - */ - private Vector mapOverCache(IgniteFunction<Double, Double> mapper) { - CacheVectorStorage<K, V> sto = storage(); - - CacheUtils.map(sto.cache().getName(), sto.keyMapper(), sto.valueMapper(), mapper); - - return this; - } - - /** {@inheritDoc} */ - @Override public double minValue() { - CacheVectorStorage<K, V> sto = storage(); - - return CacheUtils.min(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - CacheVectorStorage<K, V> sto = storage(); - - return CacheUtils.max(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteDoubleFunction<Double> fun) { - return mapOverCache(fun::apply); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteBiFunction<Double, Double, Double> fun, double y) { - // TODO: IGNITE-5723, provide cache-optimized implementation. - return super.map(fun, y); - } - - /** {@inheritDoc} */ - @Override public double sum() { - CacheVectorStorage<K, V> sto = storage(); - - return CacheUtils.sum(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public Vector assign(double val) { - return mapOverCache((Double d) -> val); - } - - /** {@inheritDoc} */ - @Override public Vector plus(double x) { - return mapOverCache((Double d) -> d + x); - } - - /** {@inheritDoc} */ - @Override public Vector divide(double x) { - return mapOverCache((Double d) -> d / x); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - return mapOverCache((Double d) -> d * x); - } - - /** - * - * - */ - @SuppressWarnings({"unchecked"}) - private CacheVectorStorage<K, V> storage() { - return (CacheVectorStorage<K, V>)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/ConstantVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/ConstantVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/ConstantVector.java deleted file mode 100644 index 5038f13..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/ConstantVector.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.vector; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.VectorStorage; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.storage.vector.ConstantVectorStorage; - -/** - * Constant value, read-only vector. - */ -public class ConstantVector extends AbstractReadOnlyVector { - /** - * - */ - public ConstantVector() { - // No-op. - } - - /** - * @param size Vector size. - * @param val Value of the constant. - */ - public ConstantVector(int size, double val) { - super(new ConstantVectorStorage(size, val)); - } - - /** - * - * - */ - private ConstantVectorStorage storage() { - return (ConstantVectorStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Vector copy() { - ConstantVectorStorage sto = storage(); - - return new ConstantVector(sto.size(), sto.constant()); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - return new ConstantVector(crd, storage().constant()); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - ConstantVector that = (ConstantVector)o; - - VectorStorage sto = getStorage(); - - return (sto != null ? sto.equals(that.getStorage()) : that.getStorage() == null); - } -}
