From 098d2832b3f4af23c72bc25f43e4ab8a95f2f416 Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Wed, 11 Apr 2018 21:40:27 +0300 Subject: [PATCH 1/5] IGNITE-7829: Added example --- .../examples/ml/knn/KNNRegressionExample.java | 310 ++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java new file mode 100644 index 0000000000000..c4e829ba8e5f1 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/ml/knn/KNNRegressionExample.java @@ -0,0 +1,310 @@ +/* + * 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.examples.ml.knn; + +import java.util.Arrays; +import java.util.UUID; +import javax.cache.Cache; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.Ignition; +import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction; +import org.apache.ignite.cache.query.QueryCursor; +import org.apache.ignite.cache.query.ScanQuery; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.ml.dataset.impl.cache.CacheBasedDatasetBuilder; +import org.apache.ignite.ml.knn.classification.KNNClassificationTrainer; +import org.apache.ignite.ml.knn.classification.KNNStrategy; +import org.apache.ignite.ml.knn.regression.KNNRegressionModel; +import org.apache.ignite.ml.knn.regression.KNNRegressionTrainer; +import org.apache.ignite.ml.math.distances.ManhattanDistance; +import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; +import org.apache.ignite.thread.IgniteThread; + +/** + * Run kNN regression trainer over distributed dataset. + * + * @see KNNClassificationTrainer + */ +public class KNNRegressionExample { + /** Run example. */ + public static void main(String[] args) throws InterruptedException { + System.out.println(); + System.out.println(">>> kNN regression algorithm over cached dataset usage example started."); + // Start ignite grid. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(">>> Ignite grid started."); + + IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), + KNNRegressionExample.class.getSimpleName(), () -> { + IgniteCache dataCache = getTestCache(ignite); + + KNNRegressionTrainer trainer = new KNNRegressionTrainer(); + + KNNRegressionModel knnMdl = (KNNRegressionModel)trainer.fit( + new CacheBasedDatasetBuilder<>(ignite, dataCache), + (k, v) -> Arrays.copyOfRange(v, 1, v.length), + (k, v) -> v[0] + ).withK(5) + .withDistanceMeasure(new ManhattanDistance()) + .withStrategy(KNNStrategy.WEIGHTED); + + int totalAmount = 0; + // Calculate mean squared error (MSE) + double mse = 0.0; + // Calculate mean absolute error (MAE) + double mae = 0.0; + + try (QueryCursor> observations = dataCache.query(new ScanQuery<>())) { + for (Cache.Entry observation : observations) { + double[] val = observation.getValue(); + double[] inputs = Arrays.copyOfRange(val, 1, val.length); + double groundTruth = val[0]; + + double prediction = knnMdl.apply(new DenseLocalOnHeapVector(inputs)); + + mse += Math.pow(prediction - groundTruth, 2.0); + mae += Math.abs(prediction - groundTruth); + + totalAmount++; + } + + mse = mse / totalAmount; + System.out.println("\n>>> Mean squared error (MSE) " + mse); + + mae = mae / totalAmount; + System.out.println("\n>>> Mean absolute error (MAE) " + mae); + } + }); + + igniteThread.start(); + igniteThread.join(); + } + } + + /** + * Fills cache with data and returns it. + * + * @param ignite Ignite instance. + * @return Filled Ignite Cache. + */ + private static IgniteCache getTestCache(Ignite ignite) { + CacheConfiguration cacheConfiguration = new CacheConfiguration<>(); + cacheConfiguration.setName("TEST_" + UUID.randomUUID()); + cacheConfiguration.setAffinity(new RendezvousAffinityFunction(false, 10)); + + IgniteCache cache = ignite.createCache(cacheConfiguration); + + for (int i = 0; i < data.length; i++) + cache.put(i, data[i]); + + return cache; + } + + /** The Iris dataset. */ + private static final double[][] data = { + {199, 125, 256, 6000, 256, 16, 128}, + {253, 29, 8000, 32000, 32, 8, 32}, + {132, 29, 8000, 16000, 32, 8, 16}, + {290, 26, 8000, 32000, 64, 8, 32}, + {381, 23, 16000, 32000, 64, 16, 32}, + {749, 23, 16000, 64000, 64, 16, 32}, + {1238, 23, 32000, 64000, 128, 32, 64}, + {23, 400, 1000, 3000, 0, 1, 2}, + {24, 400, 512, 3500, 4, 1, 6}, + {70, 60, 2000, 8000, 65, 1, 8}, + {117, 50, 4000, 16000, 65, 1, 8}, + {15, 350, 64, 64, 0, 1, 4}, + {64, 200, 512, 16000, 0, 4, 32}, + {23, 167, 524, 2000, 8, 4, 15}, + {29, 143, 512, 5000, 0, 7, 32}, + {22, 143, 1000, 2000, 0, 5, 16}, + {124, 110, 5000, 5000, 142, 8, 64}, + {35, 143, 1500, 6300, 0, 5, 32}, + {39, 143, 3100, 6200, 0, 5, 20}, + {40, 143, 2300, 6200, 0, 6, 64}, + {45, 110, 3100, 6200, 0, 6, 64}, + {28, 320, 128, 6000, 0, 1, 12}, + {21, 320, 512, 2000, 4, 1, 3}, + {28, 320, 256, 6000, 0, 1, 6}, + {22, 320, 256, 3000, 4, 1, 3}, + {28, 320, 512, 5000, 4, 1, 5}, + {27, 320, 256, 5000, 4, 1, 6}, + {102, 25, 1310, 2620, 131, 12, 24}, + {74, 50, 2620, 10480, 30, 12, 24}, + {138, 56, 5240, 20970, 30, 12, 24}, + {136, 64, 5240, 20970, 30, 12, 24}, + {23, 50, 500, 2000, 8, 1, 4}, + {29, 50, 1000, 4000, 8, 1, 5}, + {44, 50, 2000, 8000, 8, 1, 5}, + {30, 50, 1000, 4000, 8, 3, 5}, + {41, 50, 1000, 8000, 8, 3, 5}, + {74, 50, 2000, 16000, 8, 3, 5}, + {54, 133, 1000, 12000, 9, 3, 12}, + {41, 133, 1000, 8000, 9, 3, 12}, + {18, 810, 512, 512, 8, 1, 1}, + {28, 810, 1000, 5000, 0, 1, 1}, + {36, 320, 512, 8000, 4, 1, 5}, + {38, 200, 512, 8000, 8, 1, 8}, + {34, 700, 384, 8000, 0, 1, 1}, + {19, 700, 256, 2000, 0, 1, 1}, + {72, 140, 1000, 16000, 16, 1, 3}, + {36, 200, 1000, 8000, 0, 1, 2}, + {30, 110, 1000, 4000, 16, 1, 2}, + {56, 110, 1000, 12000, 16, 1, 2}, + {42, 220, 1000, 8000, 16, 1, 2}, + {34, 800, 256, 8000, 0, 1, 4}, + {19, 125, 512, 1000, 0, 8, 20}, + {75, 75, 2000, 8000, 64, 1, 38}, + {113, 75, 2000, 16000, 64, 1, 38}, + {157, 75, 2000, 16000, 128, 1, 38}, + {18, 90, 256, 1000, 0, 3, 10}, + {20, 105, 256, 2000, 0, 3, 10}, + {28, 105, 1000, 4000, 0, 3, 24}, + {33, 105, 2000, 4000, 8, 3, 19}, + {47, 75, 2000, 8000, 8, 3, 24}, + {54, 75, 3000, 8000, 8, 3, 48}, + {20, 175, 256, 2000, 0, 3, 24}, + {23, 300, 768, 3000, 0, 6, 24}, + {25, 300, 768, 3000, 6, 6, 24}, + {52, 300, 768, 12000, 6, 6, 24}, + {27, 300, 768, 4500, 0, 1, 24}, + {50, 300, 384, 12000, 6, 1, 24}, + {18, 300, 192, 768, 6, 6, 24}, + {53, 180, 768, 12000, 6, 1, 31}, + {23, 330, 1000, 3000, 0, 2, 4}, + {30, 300, 1000, 4000, 8, 3, 64}, + {73, 300, 1000, 16000, 8, 2, 112}, + {20, 330, 1000, 2000, 0, 1, 2}, + {25, 330, 1000, 4000, 0, 3, 6}, + {28, 140, 2000, 4000, 0, 3, 6}, + {29, 140, 2000, 4000, 0, 4, 8}, + {32, 140, 2000, 4000, 8, 1, 20}, + {175, 140, 2000, 32000, 32, 1, 20}, + {57, 140, 2000, 8000, 32, 1, 54}, + {181, 140, 2000, 32000, 32, 1, 54}, + {32, 140, 2000, 4000, 8, 1, 20}, + {82, 57, 4000, 16000, 1, 6, 12}, + {171, 57, 4000, 24000, 64, 12, 16}, + {361, 26, 16000, 32000, 64, 16, 24}, + {350, 26, 16000, 32000, 64, 8, 24}, + {220, 26, 8000, 32000, 0, 8, 24}, + {113, 26, 8000, 16000, 0, 8, 16}, + {15, 480, 96, 512, 0, 1, 1}, + {21, 203, 1000, 2000, 0, 1, 5}, + {35, 115, 512, 6000, 16, 1, 6}, + {18, 1100, 512, 1500, 0, 1, 1}, + {20, 1100, 768, 2000, 0, 1, 1}, + {20, 600, 768, 2000, 0, 1, 1}, + {28, 400, 2000, 4000, 0, 1, 1}, + {45, 400, 4000, 8000, 0, 1, 1}, + {18, 900, 1000, 1000, 0, 1, 2}, + {17, 900, 512, 1000, 0, 1, 2}, + {26, 900, 1000, 4000, 4, 1, 2}, + {28, 900, 1000, 4000, 8, 1, 2}, + {28, 900, 2000, 4000, 0, 3, 6}, + {31, 225, 2000, 4000, 8, 3, 6}, + {42, 180, 2000, 8000, 8, 1, 6}, + {76, 185, 2000, 16000, 16, 1, 6}, + {76, 180, 2000, 16000, 16, 1, 6}, + {26, 225, 1000, 4000, 2, 3, 6}, + {59, 25, 2000, 12000, 8, 1, 4}, + {65, 25, 2000, 12000, 16, 3, 5}, + {101, 17, 4000, 16000, 8, 6, 12}, + {116, 17, 4000, 16000, 32, 6, 12}, + {18, 1500, 768, 1000, 0, 0, 0}, + {20, 1500, 768, 2000, 0, 0, 0}, + {20, 800, 768, 2000, 0, 0, 0}, + {30, 50, 2000, 4000, 0, 3, 6}, + {44, 50, 2000, 8000, 8, 3, 6}, + {82, 50, 2000, 16000, 24, 1, 6}, + {128, 50, 8000, 16000, 48, 1, 10}, + {37, 100, 1000, 8000, 0, 2, 6}, + {46, 100, 1000, 8000, 24, 2, 6}, + {46, 100, 1000, 8000, 24, 3, 6}, + {80, 50, 2000, 16000, 12, 3, 16}, + {88, 50, 2000, 16000, 24, 6, 16}, + {33, 150, 512, 4000, 0, 8, 128}, + {46, 115, 2000, 8000, 16, 1, 3}, + {29, 115, 2000, 4000, 2, 1, 5}, + {53, 92, 2000, 8000, 32, 1, 6}, + {41, 92, 2000, 8000, 4, 1, 6}, + {86, 75, 4000, 16000, 16, 1, 6}, + {95, 60, 4000, 16000, 32, 1, 6}, + {107, 60, 2000, 16000, 64, 5, 8}, + {117, 60, 4000, 16000, 64, 5, 8}, + {119, 50, 4000, 16000, 64, 5, 10}, + {120, 72, 4000, 16000, 64, 8, 16}, + {48, 72, 2000, 8000, 16, 6, 8}, + {126, 40, 8000, 16000, 32, 8, 16}, + {266, 40, 8000, 32000, 64, 8, 24}, + {270, 35, 8000, 32000, 64, 8, 24}, + {426, 38, 16000, 32000, 128, 16, 32}, + {151, 48, 4000, 24000, 32, 8, 24}, + {267, 38, 8000, 32000, 64, 8, 24}, + {603, 30, 16000, 32000, 256, 16, 24}, + {19, 112, 1000, 1000, 0, 1, 4}, + {21, 84, 1000, 2000, 0, 1, 6}, + {26, 56, 1000, 4000, 0, 1, 6}, + {35, 56, 2000, 6000, 0, 1, 8}, + {41, 56, 2000, 8000, 0, 1, 8}, + {47, 56, 4000, 8000, 0, 1, 8}, + {62, 56, 4000, 12000, 0, 1, 8}, + {78, 56, 4000, 16000, 0, 1, 8}, + {80, 38, 4000, 8000, 32, 16, 32}, + {142, 38, 8000, 16000, 64, 4, 8}, + {281, 38, 8000, 24000, 160, 4, 8}, + {190, 38, 4000, 16000, 128, 16, 32}, + {21, 200, 1000, 2000, 0, 1, 2}, + {25, 200, 1000, 4000, 0, 1, 4}, + {67, 200, 2000, 8000, 64, 1, 5}, + {24, 250, 512, 4000, 0, 1, 7}, + {24, 250, 512, 4000, 0, 4, 7}, + {64, 250, 1000, 16000, 1, 1, 8}, + {25, 160, 512, 4000, 2, 1, 5}, + {20, 160, 512, 2000, 2, 3, 8}, + {29, 160, 1000, 4000, 8, 1, 14}, + {43, 160, 1000, 8000, 16, 1, 14}, + {53, 160, 2000, 8000, 32, 1, 13}, + {19, 240, 512, 1000, 8, 1, 3}, + {22, 240, 512, 2000, 8, 1, 5}, + {31, 105, 2000, 4000, 8, 3, 8}, + {41, 105, 2000, 6000, 16, 6, 16}, + {47, 105, 2000, 8000, 16, 4, 14}, + {99, 52, 4000, 16000, 32, 4, 12}, + {67, 70, 4000, 12000, 8, 6, 8}, + {81, 59, 4000, 12000, 32, 6, 12}, + {149, 59, 8000, 16000, 64, 12, 24}, + {183, 26, 8000, 24000, 32, 8, 16}, + {275, 26, 8000, 32000, 64, 12, 16}, + {382, 26, 8000, 32000, 128, 24, 32}, + {56, 116, 2000, 8000, 32, 5, 28}, + {182, 50, 2000, 32000, 24, 6, 26}, + {227, 50, 2000, 32000, 48, 26, 52}, + {341, 50, 2000, 32000, 112, 52, 104}, + {360, 50, 4000, 32000, 112, 52, 104}, + {919, 30, 8000, 64000, 96, 12, 176}, + {978, 30, 8000, 64000, 128, 12, 176}, + {24, 180, 262, 4000, 0, 1, 3}, + {37, 124, 1000, 8000, 0, 1, 8}, + {50, 98, 1000, 8000, 32, 2, 8}, + {41, 125, 2000, 8000, 0, 2, 14}, + {47, 480, 512, 8000, 32, 0, 0}, + {25, 480, 1000, 4000, 0, 0, 0} + }; +} From 861102f115fdd2458aa30ff098395100885bae33 Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Tue, 8 May 2018 13:56:46 +0300 Subject: [PATCH 2/5] Removed old algebra --- .../CholeskyDecompositionExample.java | 80 -- .../EigenDecompositionExample.java | 69 -- .../LUDecompositionExample.java | 83 -- .../QRDecompositionExample.java | 82 -- .../SingularValueDecompositionExample.java | 70 -- .../ml/math/decompositions/package-info.java | 22 - .../ml/math/matrix/CacheMatrixExample.java | 102 -- .../matrix/MatrixCustomStorageExample.java | 14 - .../ml/math/matrix/MatrixExample.java | 14 - .../ml/math/matrix/OffHeapMatrixExample.java | 14 - .../SparseDistributedMatrixExample.java | 70 -- .../ml/math/matrix/SparseMatrixExample.java | 15 - .../ml/math/vector/CacheVectorExample.java | 109 -- .../org/apache/ignite/ml/math/Algebra.java | 590 ----------- .../java/org/apache/ignite/ml/math/Blas.java | 8 +- .../org/apache/ignite/ml/math/Constants.java | 59 -- .../ignite/ml/math/IdentityValueMapper.java | 56 -- .../org/apache/ignite/ml/math/MathUtils.java | 32 - .../org/apache/ignite/ml/math/Matrix.java | 15 - .../apache/ignite/ml/math/VectorUtils.java | 3 +- .../decompositions/CholeskyDecomposition.java | 309 ------ .../decompositions/DecompositionSupport.java | 105 -- .../decompositions/EigenDecomposition.java | 936 ------------------ .../math/decompositions/LUDecomposition.java | 383 ------- .../ml/math/decompositions/QRDSolver.java | 197 ---- .../math/decompositions/QRDecomposition.java | 212 ---- .../SingularValueDecomposition.java | 623 ------------ .../ml/math/decompositions/package-info.java | 22 - .../ml/math/distributed/CacheUtils.java | 734 -------------- .../math/distributed/DistributedStorage.java | 35 - .../ml/math/distributed/MatrixKeyMapper.java | 33 - .../ml/math/distributed/ValueMapper.java | 37 - .../ml/math/distributed/VectorKeyMapper.java | 32 - .../keys/DataStructureCacheKey.java | 35 - .../math/distributed/keys/MatrixBlockKey.java | 38 - .../distributed/keys/RowColMatrixKey.java | 30 - .../math/distributed/keys/VectorBlockKey.java | 34 - .../distributed/keys/impl/MatrixBlockKey.java | 166 ---- .../keys/impl/SparseMatrixKey.java | 119 --- .../distributed/keys/impl/VectorBlockKey.java | 152 --- .../distributed/keys/impl/package-info.java | 22 - .../math/distributed/keys/package-info.java | 22 - .../ml/math/distributed/package-info.java | 22 - .../ml/math/impls/matrix/AbstractMatrix.java | 24 - .../ml/math/impls/matrix/CacheMatrix.java | 158 --- .../ml/math/impls/matrix/DiagonalMatrix.java | 101 -- .../ml/math/impls/matrix/FunctionMatrix.java | 95 -- .../math/impls/matrix/MatrixBlockEntry.java | 50 - .../math/impls/matrix/PivotedMatrixView.java | 241 ----- .../ml/math/impls/matrix/RandomMatrix.java | 97 -- .../matrix/SparseBlockDistributedMatrix.java | 314 ------ .../impls/matrix/SparseDistributedMatrix.java | 290 ------ .../impls/matrix/TransposedMatrixView.java | 84 -- .../storage/matrix/BlockMatrixStorage.java | 434 -------- .../storage/matrix/BlockVectorStorage.java | 368 ------- .../storage/matrix/CacheMatrixStorage.java | 196 ---- .../storage/matrix/DiagonalMatrixStorage.java | 158 --- .../storage/matrix/FunctionMatrixStorage.java | 190 ---- .../storage/matrix/PivotedMatrixStorage.java | 266 ----- .../storage/matrix/RandomMatrixStorage.java | 187 ---- .../SparseDistributedMatrixStorage.java | 330 ------ .../storage/vector/CacheVectorStorage.java | 175 ---- .../storage/vector/ConstantVectorStorage.java | 134 --- .../storage/vector/FunctionVectorStorage.java | 143 --- .../storage/vector/PivotedVectorStorage.java | 176 ---- .../storage/vector/RandomVectorStorage.java | 152 --- .../SingleElementVectorDelegateStorage.java | 145 --- .../vector/SingleElementVectorStorage.java | 145 --- .../SparseDistributedVectorStorage.java | 281 ------ .../impls/vector/AbstractReadOnlyVector.java | 131 --- .../ml/math/impls/vector/CacheVector.java | 140 --- .../ml/math/impls/vector/ConstantVector.java | 84 -- .../ml/math/impls/vector/FunctionVector.java | 112 --- .../math/impls/vector/PivotedVectorView.java | 163 --- .../ml/math/impls/vector/RandomVector.java | 130 --- .../impls/vector/SingleElementVector.java | 102 -- .../impls/vector/SingleElementVectorView.java | 97 -- .../vector/SparseBlockDistributedVector.java | 135 --- .../impls/vector/SparseDistributedVector.java | 147 --- .../math/impls/vector/VectorBlockEntry.java | 47 - .../ignite/ml/math/util/MatrixUtil.java | 17 +- .../ignite/ml/nn/ReplicatedVectorMatrix.java | 14 - .../ignite/ml/structures/LabeledDataset.java | 4 - .../ml/math/MathImplDistributedTestSuite.java | 47 - .../ml/math/MathImplLocalTestSuite.java | 37 - .../ignite/ml/math/MathImplMainTestSuite.java | 1 - .../CholeskyDecompositionTest.java | 160 --- .../EigenDecompositionTest.java | 193 ---- .../decompositions/LUDecompositionTest.java | 252 ----- .../ml/math/decompositions/QRDSolverTest.java | 87 -- .../decompositions/QRDecompositionTest.java | 141 --- .../SingularValueDecompositionTest.java | 122 --- .../ml/math/impls/matrix/CacheMatrixTest.java | 371 ------- .../math/impls/matrix/DiagonalMatrixTest.java | 209 ---- .../matrix/FunctionMatrixConstructorTest.java | 113 --- .../matrix/MatrixImplementationFixtures.java | 135 +-- .../matrix/MatrixImplementationsTest.java | 196 +--- .../PivotedMatrixViewConstructorTest.java | 129 --- .../SparseDistributedBlockMatrixTest.java | 408 -------- .../matrix/SparseDistributedMatrixTest.java | 316 ------ .../matrix/TransposedMatrixViewTest.java | 87 -- .../SparseDistributedMatrixStorageTest.java | 126 --- .../SparseDistributedVectorStorageTest.java | 123 --- .../ml/math/impls/vector/CacheVectorTest.java | 430 -------- .../vector/ConstantVectorConstructorTest.java | 52 - .../vector/FunctionVectorConstructorTest.java | 121 --- .../PivotedVectorViewConstructorTest.java | 211 ---- .../vector/RandomVectorConstructorTest.java | 145 --- .../SingleElementVectorConstructorTest.java | 159 --- ...ingleElementVectorViewConstructorTest.java | 137 --- .../SparseBlockDistributedVectorTest.java | 186 ---- .../vector/SparseDistributedVectorTest.java | 191 ---- .../vector/VectorImplementationsFixtures.java | 203 ---- .../vector/VectorImplementationsTest.java | 861 ---------------- .../math/impls/vector/VectorIterableTest.java | 376 ------- .../math/impls/vector/VectorToMatrixTest.java | 50 +- ...rseBlockDistributedMatrixMulBenchmark.java | 32 - ...eSparseDistributedMatrixMul2Benchmark.java | 74 -- ...teSparseDistributedMatrixMulBenchmark.java | 49 - 119 files changed, 29 insertions(+), 18563 deletions(-) delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/CholeskyDecompositionExample.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/EigenDecompositionExample.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/LUDecompositionExample.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/QRDecompositionExample.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/SingularValueDecompositionExample.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/package-info.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/CacheMatrixExample.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java delete mode 100644 examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/Algebra.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/Constants.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/IdentityValueMapper.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/MathUtils.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/CholeskyDecomposition.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/DecompositionSupport.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/EigenDecomposition.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/LUDecomposition.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDSolver.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDecomposition.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/SingularValueDecomposition.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/package-info.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/CacheUtils.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/DistributedStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/MatrixKeyMapper.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/ValueMapper.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/VectorKeyMapper.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/DataStructureCacheKey.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/MatrixBlockKey.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/RowColMatrixKey.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/VectorBlockKey.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/MatrixBlockKey.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/SparseMatrixKey.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/VectorBlockKey.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/package-info.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/package-info.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/package-info.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/FunctionMatrixStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/PivotedMatrixStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/RandomMatrixStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/CacheVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/ConstantVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/FunctionVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/PivotedVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/RandomVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorDelegateStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SingleElementVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorage.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/AbstractReadOnlyVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/CacheVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/ConstantVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java delete mode 100644 modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrixTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrixTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrixConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixViewConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrixTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixViewTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorageTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/RandomVectorConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorViewConstructorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVectorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVectorTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorIterableTest.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseBlockDistributedMatrixMulBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMul2Benchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMulBenchmark.java diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/CholeskyDecompositionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/CholeskyDecompositionExample.java deleted file mode 100644 index ebac2b18e26b0..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/CholeskyDecompositionExample.java +++ /dev/null @@ -1,80 +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.examples.ml.math.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Tracer; -import org.apache.ignite.ml.math.decompositions.CholeskyDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; - -/** - * Example of using {@link CholeskyDecomposition}. - */ -public class CholeskyDecompositionExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - public static void main(String[] args) { - System.out.println(">>> Cholesky decomposition example started."); - // Let's compute a Cholesky decomposition of Hermitian matrix m: - // m = l l^{*}, where - // l is a lower triangular matrix - // l^{*} is its conjugate transpose - - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }); - System.out.println("\n>>> Matrix m for decomposition: "); - Tracer.showAscii(m); - - // This decomposition is useful when dealing with systems of linear equations of the form - // m x = b where m is a Hermitian matrix. - // For such systems Cholesky decomposition provides - // more effective method of solving compared to LU decomposition. - // Suppose we want to solve system - // m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs - // as a matrix of the form - // (b1, b2, ..., bm) - // to the method Cholesky::solve which returns solutions in the form - // (sol1, sol2, ..., solm) - CholeskyDecomposition dec = new CholeskyDecomposition(m); - System.out.println("\n>>> Made decomposition m = l * l^{*}."); - System.out.println(">>> Matrix l is "); - Tracer.showAscii(dec.getL()); - System.out.println(">>> Matrix l^{*} is "); - Tracer.showAscii(dec.getLT()); - - Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { - {4.0, -6.0, 7.0}, - {1.0, 1.0, 1.0} - }).transpose(); - System.out.println("\n>>> Solving systems of linear equations of the form m x = b for various bs represented by columns of matrix"); - Tracer.showAscii(bs); - Matrix sol = dec.solve(bs); - - System.out.println("\n>>> List of solutions: "); - for (int i = 0; i < sol.columnSize(); i++) - Tracer.showAscii(sol.viewColumn(i)); - - System.out.println("\n>>> Cholesky decomposition example completed."); - } -} diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/EigenDecompositionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/EigenDecompositionExample.java deleted file mode 100644 index cda37f48d55d0..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/EigenDecompositionExample.java +++ /dev/null @@ -1,69 +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.examples.ml.math.decompositions; - -import org.apache.ignite.ml.math.Tracer; -import org.apache.ignite.ml.math.decompositions.EigenDecomposition; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; - -/** - * Example of using {@link EigenDecomposition}. - */ -public class EigenDecompositionExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - public static void main(String[] args) { - System.out.println(">>> Eigen decomposition example started."); - - // Let's compute EigenDecomposition for some square (n x n) matrix m with real eigenvalues: - // m = v d v^{-1}, where d is diagonal matrix having eigenvalues of m on diagonal - // and v is matrix where i-th column is eigenvector for i-th eigenvalue (i from 0 to n - 1) - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d, 0.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d} - }); - System.out.println("\n>>> Matrix m for decomposition: "); - Tracer.showAscii(m); - - EigenDecomposition dec = new EigenDecomposition(m); - System.out.println("\n>>> Made decomposition."); - System.out.println(">>> Matrix getV is "); - Tracer.showAscii(dec.getV()); - System.out.println(">>> Matrix getD is "); - Tracer.showAscii(dec.getD()); - - // From this decomposition we, for example, can easily compute determinant of matrix m - // det (m) = det (v d v^{-1}) = - // det(v) det (d) det(v^{-1}) = - // det(v) det(v)^{-1} det(d) = - // det (d) = - // product of diagonal elements of d = - // product of eigenvalues - double det = dec.getRealEigenValues().foldMap(Functions.MULT, Functions.IDENTITY, 1.0); - System.out.println("\n>>> Determinant is " + det); - - System.out.println("\n>>> Eigen decomposition example completed."); - } - -} diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/LUDecompositionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/LUDecompositionExample.java deleted file mode 100644 index a815047af7ad5..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/LUDecompositionExample.java +++ /dev/null @@ -1,83 +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.examples.ml.math.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Tracer; -import org.apache.ignite.ml.math.decompositions.LUDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; - -/** - * Example of using {@link LUDecomposition}. - */ -public class LUDecompositionExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - public static void main(String[] args) { - System.out.println(">>> LU decomposition example started."); - // Let's compute a LU decomposition for some (n x n) matrix m: - // m = p l u, where - // p is an (n x n) is a row-permutation matrix - // l is a (n x n) lower triangular matrix - // u is a (n x n) upper triangular matrix - - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 1.0d, -1.0d}, - {1.0d, -2.0d, 3.0d}, - {2.0d, 3.0d, 1.0d} - }); - System.out.println("\n>>> Matrix m for decomposition: "); - Tracer.showAscii(m); - - // This decomposition is useful when dealing with systems of linear equations. - // (see https://en.wikipedia.org/wiki/LU_decomposition) - // suppose we want to solve system - // m x = b for various bs. Then after we computed LU decomposition, we can feed various bs - // as a matrix of the form - // (b1, b2, ..., bm) - // to the method LUDecomposition::solve which returns solutions in the form - // (sol1, sol2, ..., solm) - - LUDecomposition dec = new LUDecomposition(m); - System.out.println("\n>>> Made decomposition."); - System.out.println(">>> Matrix getL is "); - Tracer.showAscii(dec.getL()); - System.out.println(">>> Matrix getU is "); - Tracer.showAscii(dec.getU()); - System.out.println(">>> Matrix getP is "); - Tracer.showAscii(dec.getP()); - - Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { - {4.0, -6.0, 7.0}, - {1.0, 1.0, 1.0} - }); - System.out.println("\n>>> Matrix to solve: "); - Tracer.showAscii(bs); - - Matrix sol = dec.solve(bs.transpose()); - - System.out.println("\n>>> List of solutions: "); - for (int i = 0; i < sol.columnSize(); i++) - Tracer.showAscii(sol.viewColumn(i)); - - System.out.println("\n>>> LU decomposition example completed."); - } -} diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/QRDecompositionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/QRDecompositionExample.java deleted file mode 100644 index bed99d14c789d..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/QRDecompositionExample.java +++ /dev/null @@ -1,82 +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.examples.ml.math.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Tracer; -import org.apache.ignite.ml.math.decompositions.QRDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; - -/** - * Example of using {@link QRDecomposition}. - */ -public class QRDecompositionExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - public static void main(String[] args) { - System.out.println(">>> QR decomposition example started."); - Matrix m = new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }); - - System.out.println("\n>>> Input matrix:"); - Tracer.showAscii(m); - - QRDecomposition dec = new QRDecomposition(m); - System.out.println("\n>>> Value for full rank in decomposition: [" + dec.hasFullRank() + "]."); - - Matrix q = dec.getQ(); - Matrix r = dec.getR(); - - System.out.println("\n>>> Orthogonal matrix Q:"); - Tracer.showAscii(q); - System.out.println("\n>>> Upper triangular matrix R:"); - Tracer.showAscii(r); - - Matrix qSafeCp = safeCopy(q); - - Matrix identity = qSafeCp.times(qSafeCp.transpose()); - - System.out.println("\n>>> Identity matrix obtained from Q:"); - Tracer.showAscii(identity); - - Matrix recomposed = qSafeCp.times(r); - - System.out.println("\n>>> Recomposed input matrix:"); - Tracer.showAscii(recomposed); - - Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10)); - - System.out.println("\n>>> Solved matrix:"); - Tracer.showAscii(sol); - - dec.destroy(); - - System.out.println("\n>>> QR decomposition example completed."); - } - - /** */ - private static Matrix safeCopy(Matrix orig) { - return new DenseLocalOnHeapMatrix(orig.rowSize(), orig.columnSize()).assign(orig); - } -} diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/SingularValueDecompositionExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/SingularValueDecompositionExample.java deleted file mode 100644 index 81406ae43db69..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/SingularValueDecompositionExample.java +++ /dev/null @@ -1,70 +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.examples.ml.math.decompositions; - -import org.apache.ignite.ml.math.Tracer; -import org.apache.ignite.ml.math.decompositions.SingularValueDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; - -/** - * Example of using {@link SingularValueDecomposition}. - */ -public class SingularValueDecompositionExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - public static void main(String[] args) { - System.out.println(">>> Singular value decomposition (SVD) example started."); - - // Let's compute a SVD of (l x k) matrix m. This decomposition can be thought as extension of EigenDecomposition to - // rectangular matrices. The factorization we get is following: - // m = u * s * v^{*}, where - // u is a real or complex unitary matrix - // s is a rectangular diagonal matrix with non-negative real numbers on diagonal (this numbers are singular values of m) - // v is a real or complex unitary matrix - // If m is real then u and v are also real. - // Complex case is not supported for the moment. - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d, 0.0d, 2.0d}, - {0.0d, 0.0d, 3.0d, 0.0d, 0.0d}, - {0.0d, 0.0d, 0.0d, 0.0d, 0.0d}, - {0.0d, 2.0d, 0.0d, 0.0d, 0.0d} - }); - System.out.println("\n>>> Matrix m for decomposition: "); - Tracer.showAscii(m); - - SingularValueDecomposition dec = new SingularValueDecomposition(m); - System.out.println("\n>>> Made decomposition m = u * s * v^{*}."); - System.out.println(">>> Matrix u is "); - Tracer.showAscii(dec.getU()); - System.out.println(">>> Matrix s is "); - Tracer.showAscii(dec.getS()); - System.out.println(">>> Matrix v is "); - Tracer.showAscii(dec.getV()); - - // This decomposition can in particular help with solving problem of finding x minimizing 2-norm of m x such - // that 2-norm of x is 1. It appears that it is the right singular vector corresponding to minimal singular - // value, which is always last. - System.out.println("\n>>> Vector x minimizing 2-norm of m x such that 2 norm of x is 1: "); - Tracer.showAscii(dec.getV().viewColumn(dec.getSingularValues().length - 1)); - - System.out.println("\n>>> Singular value decomposition (SVD) example completed."); - } -} diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/package-info.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/package-info.java deleted file mode 100644 index 644f8ba4454de..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/decompositions/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. - */ - -/** - * - * Core algebra decomposition examples. - */ -package org.apache.ignite.examples.ml.math.decompositions; diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/CacheMatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/CacheMatrixExample.java deleted file mode 100644 index a7cbaab8710bf..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/CacheMatrixExample.java +++ /dev/null @@ -1,102 +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.examples.ml.math.matrix; - -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.examples.ml.math.vector.CacheVectorExample; -import org.apache.ignite.ml.math.IdentityValueMapper; -import org.apache.ignite.ml.math.Tracer; -import org.apache.ignite.ml.math.distributed.MatrixKeyMapper; -import org.apache.ignite.ml.math.distributed.ValueMapper; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.impls.matrix.CacheMatrix; - -/** - * Example that demonstrates how to use {@link CacheMatrix}. - * - * Basically CacheMatrix is view over existing data in cache. So we have {@link MatrixKeyMapper} and {@link ValueMapper} - * for this purpose. A {@link MatrixKeyMapper} allows us to map matrix indices to cache keys. And a {@link ValueMapper} - * allows us map cache object to matrix elements - doubles. - * - * In this example we use simple flat mapping for keys and {@link IdentityValueMapper} for cache objects - * because they are Doubles. - * - * @see CacheVectorExample - */ -public class CacheMatrixExample { - /** */ private static final String CACHE_NAME = CacheMatrixExample.class.getSimpleName(); - /** */ private static final int ROWS = 3; - /** */ private static final int COLS = 3; - - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - public static void main(String[] args) { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> CacheMatrix example started."); - - CacheConfiguration cfg = new CacheConfiguration<>(); - - cfg.setName(CACHE_NAME); - - try (IgniteCache cache = ignite.getOrCreateCache(cfg)) { - double[][] testValues = {{1.0, 0.0, 0.0}, {1.0, 1.0, 0.0}, {1.0, 1.0, 1.0}}; - - ValueMapper valMapper = new IdentityValueMapper(); - - // Map matrix element indices to cache keys. - MatrixKeyMapper keyMapper = new MatrixKeyMapper() { - @Override public Integer apply(int x, int y) { - return x * COLS + y; - } - - @Override public boolean isValid(Integer integer) { - return integer >= 0 && integer < COLS * ROWS; - } - }; - - // Create cache matrix. - CacheMatrix cacheMatrix = new CacheMatrix<>(ROWS, COLS, cache, keyMapper, valMapper); - - cacheMatrix.assign(testValues); - - Tracer.showAscii(cacheMatrix); - - // Find all positive elements. - Integer nonZeroes = cacheMatrix.foldMap((o, aDouble) -> { - if (aDouble > 0) - return o + 1; - return o; - }, Functions.IDENTITY, 0); - - System.out.println("Quantity of non zeroes elements is " + nonZeroes.intValue()); - - System.out.println(">>>"); - System.out.println(">>> Finished executing Ignite \"CacheMatrix\" example."); - System.out.println(">>> Lower triangular matrix 3x3 have only 6 positive elements."); - System.out.println(">>>"); - } - } - } -} diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java index 3b4f27dee543e..95bf5ccea7a25 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java @@ -63,20 +63,6 @@ public static void main(String[] args) { System.out.println(">>> Matrix product: "); MatrixExampleUtil.print(mult); - System.out.println("\n>>> Calculating matrices determinants."); - double det1 = m1.determinant(); - double det2 = m2.determinant(); - double detMult = mult.determinant(); - boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d; - - System.out.println(">>> First matrix determinant: [" + det1 + "]."); - System.out.println(">>> Second matrix determinant: [" + det2 + "]."); - System.out.println(">>> Matrix product determinant: [" + detMult - + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "]."); - - System.out.println("Determinant of product matrix [" + detMult - + "] should be equal to product of determinants [" + (det1 * det2) + "]."); - System.out.println("\n>>> Matrix API usage example completed."); } diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java index 755f36c92f21e..3e5e6ff985aab 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java @@ -60,20 +60,6 @@ public static void main(String[] args) { System.out.println(">>> Matrix product: "); MatrixExampleUtil.print(mult); - System.out.println("\n>>> Calculating matrices determinants."); - double det1 = m1.determinant(); - double det2 = m2.determinant(); - double detMult = mult.determinant(); - boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d; - - System.out.println(">>> First matrix determinant: [" + det1 + "]."); - System.out.println(">>> Second matrix determinant: [" + det2 + "]."); - System.out.println(">>> Matrix product determinant: [" + detMult - + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "]."); - - System.out.println("Determinant of product matrix [" + detMult - + "] should be equal to product of determinants [" + (det1 * det2) + "]."); - System.out.println("\n>>> Basic Matrix API usage example completed."); } } diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java index db017941ccb94..8ad1d1d4aa2e4 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java @@ -65,20 +65,6 @@ public static void main(String[] args) { System.out.println(">>> Matrix product: "); MatrixExampleUtil.print(mult); - System.out.println("\n>>> Calculating matrices determinants."); - double det1 = m1.determinant(); - double det2 = m2.determinant(); - double detMult = mult.determinant(); - boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d; - - System.out.println(">>> First matrix determinant: [" + det1 + "]."); - System.out.println(">>> Second matrix determinant: [" + det2 + "]."); - System.out.println(">>> Matrix product determinant: [" + detMult - + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "]."); - - System.out.println("Determinant of product matrix [" + detMult - + "] should be equal to product of determinants [" + (det1 * det2) + "]."); - System.out.println("\n>>> Off-heap matrix API usage example completed."); } } diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java deleted file mode 100644 index c73a8a0d28f62..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java +++ /dev/null @@ -1,70 +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.examples.ml.math.matrix; - -import org.apache.ignite.Ignite; -import org.apache.ignite.Ignition; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.impls.matrix.CacheMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; -import org.apache.ignite.thread.IgniteThread; - -/** - * This example shows how to create and use {@link SparseDistributedMatrix} API. - * - * Unlike the {@link CacheMatrix} the {@link SparseDistributedMatrix} creates it's own cache. - */ -public class SparseDistributedMatrixExample { - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - public static void main(String[] args) throws InterruptedException { - System.out.println(); - System.out.println(">>> Sparse distributed matrix API usage example started."); - // Start ignite grid. - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(">>> Ignite grid started."); - // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread - // because we create ignite cache internally. - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> { - - double[][] testValues = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}}; - - System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread."); - // Create SparseDistributedMatrix, new cache will be created automagically. - SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(testValues.length, testValues[0].length, - StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - distributedMatrix.assign(testValues); - - System.out.println("Sum of all matrix elements is " + distributedMatrix.sum()); - - System.out.println(">>> Destroy SparseDistributedMatrix after using."); - // Destroy internal cache. - distributedMatrix.destroy(); - }); - - igniteThread.start(); - - igniteThread.join(); - } - } -} diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java index a03688d604161..25962983f1c22 100644 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java @@ -64,21 +64,6 @@ public static void main(String[] args) { MatrixExampleUtil.print(m2); System.out.println(">>> Matrix product: "); MatrixExampleUtil.print(mult); - - System.out.println("\n>>> Calculating matrices determinants."); - double det1 = m1.determinant(); - double det2 = m2.determinant(); - double detMult = mult.determinant(); - boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d; - - System.out.println(">>> First matrix determinant: [" + det1 + "]."); - System.out.println(">>> Second matrix determinant: [" + det2 + "]."); - System.out.println(">>> Matrix product determinant: [" + detMult - + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "]."); - - System.out.println("Determinant of product matrix [" + detMult - + "] should be equal to product of determinants [" + (det1 * det2) + "]."); - System.out.println("\n>>> Sparse matrix API usage example completed."); } } diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java deleted file mode 100644 index 4253ac1a69a7a..0000000000000 --- a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java +++ /dev/null @@ -1,109 +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.examples.ml.math.vector; - -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.ml.math.IdentityValueMapper; -import org.apache.ignite.ml.math.distributed.ValueMapper; -import org.apache.ignite.ml.math.distributed.VectorKeyMapper; -import org.apache.ignite.ml.math.impls.vector.CacheVector; - -/** - * This example shows how to use {@link CacheVector} API. - *

- * Basically CacheVector is a view over existing data in cache. So we have {@link VectorKeyMapper} and - * {@link ValueMapper} for this purpose. A {@link VectorKeyMapper} allows us to map vector indices to cache keys. - * And a {@link ValueMapper} allows us map cache object to vector elements - doubles.

- *

- * In this example we use simple flat mapping for keys and {@link IdentityValueMapper} for cache - * objects because they are Doubles.

- */ -public class CacheVectorExample { - /** */ - private static final String CACHE_NAME = CacheVectorExample.class.getSimpleName(); - - /** */ - private static final int CARDINALITY = 10; - - /** - * Executes example. - * - * @param args Command line arguments, none required. - */ - @SuppressWarnings("unchecked") - public static void main(String[] args) { - try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { - System.out.println(); - System.out.println(">>> CacheVector example started."); - - CacheConfiguration cfg = new CacheConfiguration<>(); - - cfg.setName(CACHE_NAME); - - try (IgniteCache cache = ignite.getOrCreateCache(cfg)) { - double[] testValues1 = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; - double[] testValues2 = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; - - ValueMapper valMapper = new IdentityValueMapper(); - - // Map vector element index to cache keys. - VectorKeyMapper keyMapper1 = new VectorKeyMapper() { - @Override public Integer apply(int i) { - return i; - } - - @Override public boolean isValid(Integer integer) { - return integer >= 0 && CARDINALITY > integer; - } - }; - - // Map vector element index to cache keys with shift. - VectorKeyMapper keyMapper2 = new VectorKeyMapper() { - @Override public Integer apply(int i) { - return i + CARDINALITY; - } - - @Override public boolean isValid(Integer integer) { - return integer >= 0 && CARDINALITY > integer; - } - }; - - // Create two cache vectors over one cache. - CacheVector cacheVector1 = new CacheVector(CARDINALITY, cache, keyMapper1, valMapper); - System.out.println(">>> First cache vector created."); - - CacheVector cacheVector2 = new CacheVector(CARDINALITY, cache, keyMapper2, valMapper); - System.out.println(">>> Second cache vector created."); - - cacheVector1.assign(testValues1); - cacheVector2.assign(testValues2); - - // Dot product for orthogonal vectors is 0.0. - assert cacheVector1.dot(cacheVector2) == 0.0; - - System.out.println(">>>"); - System.out.println(">>> Finished executing Ignite \"CacheVector\" example."); - System.out.println(">>> Dot product is 0.0 for orthogonal vectors."); - System.out.println(">>>"); - } - } - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/Algebra.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/Algebra.java deleted file mode 100644 index c54e390d20d56..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/Algebra.java +++ /dev/null @@ -1,590 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -Copyright 1999 CERN - European Organization for Nuclear Research. -Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose -is hereby granted without fee, provided that the above copyright notice appear in all copies and -that both that copyright notice and this permission notice appear in supporting documentation. -CERN makes no representations about the suitability of this software for any purpose. -It is provided "as is" without expressed or implied warranty. -*/ - -package org.apache.ignite.ml.math; - -/** - * Miscellaneous arithmetic and algebra functions. - * Lifted from Apache Mahout. - */ -public class Algebra extends Constants { - /** */ - private static final double[] STIRLING_CORRECTION = { - 0.0, - 8.106146679532726e-02, 4.134069595540929e-02, - 2.767792568499834e-02, 2.079067210376509e-02, - 1.664469118982119e-02, 1.387612882307075e-02, - 1.189670994589177e-02, 1.041126526197209e-02, - 9.255462182712733e-03, 8.330563433362871e-03, - 7.573675487951841e-03, 6.942840107209530e-03, - 6.408994188004207e-03, 5.951370112758848e-03, - 5.554733551962801e-03, 5.207655919609640e-03, - 4.901395948434738e-03, 4.629153749334029e-03, - 4.385560249232324e-03, 4.166319691996922e-03, - 3.967954218640860e-03, 3.787618068444430e-03, - 3.622960224683090e-03, 3.472021382978770e-03, - 3.333155636728090e-03, 3.204970228055040e-03, - 3.086278682608780e-03, 2.976063983550410e-03, - 2.873449362352470e-03, 2.777674929752690e-03, - }; - - /** */ - private static final double[] LOG_FACTORIALS = { - 0.00000000000000000, 0.00000000000000000, 0.69314718055994531, - 1.79175946922805500, 3.17805383034794562, 4.78749174278204599, - 6.57925121201010100, 8.52516136106541430, 10.60460290274525023, - 12.80182748008146961, 15.10441257307551530, 17.50230784587388584, - 19.98721449566188615, 22.55216385312342289, 25.19122118273868150, - 27.89927138384089157, 30.67186010608067280, 33.50507345013688888, - 36.39544520803305358, 39.33988418719949404, 42.33561646075348503, - 45.38013889847690803, 48.47118135183522388, 51.60667556776437357, - 54.78472939811231919, 58.00360522298051994, 61.26170176100200198, - 64.55753862700633106, 67.88974313718153498, 71.25703896716800901 - }; - - /** */ - private static final long[] LONG_FACTORIALS = { - 1L, - 1L, - 2L, - 6L, - 24L, - 120L, - 720L, - 5040L, - 40320L, - 362880L, - 3628800L, - 39916800L, - 479001600L, - 6227020800L, - 87178291200L, - 1307674368000L, - 20922789888000L, - 355687428096000L, - 6402373705728000L, - 121645100408832000L, - 2432902008176640000L - }; - - /** */ - private static final double[] DOUBLE_FACTORIALS = { - 5.109094217170944E19, - 1.1240007277776077E21, - 2.585201673888498E22, - 6.204484017332394E23, - 1.5511210043330984E25, - 4.032914611266057E26, - 1.0888869450418352E28, - 3.048883446117138E29, - 8.841761993739701E30, - 2.652528598121911E32, - 8.222838654177924E33, - 2.6313083693369355E35, - 8.68331761881189E36, - 2.952327990396041E38, - 1.0333147966386144E40, - 3.719933267899013E41, - 1.3763753091226346E43, - 5.23022617466601E44, - 2.0397882081197447E46, - 8.15915283247898E47, - 3.34525266131638E49, - 1.4050061177528801E51, - 6.041526306337384E52, - 2.6582715747884495E54, - 1.196222208654802E56, - 5.502622159812089E57, - 2.5862324151116827E59, - 1.2413915592536068E61, - 6.082818640342679E62, - 3.0414093201713376E64, - 1.5511187532873816E66, - 8.06581751709439E67, - 4.274883284060024E69, - 2.308436973392413E71, - 1.2696403353658264E73, - 7.109985878048632E74, - 4.052691950487723E76, - 2.350561331282879E78, - 1.386831185456898E80, - 8.32098711274139E81, - 5.075802138772246E83, - 3.146997326038794E85, - 1.9826083154044396E87, - 1.2688693218588414E89, - 8.247650592082472E90, - 5.443449390774432E92, - 3.6471110918188705E94, - 2.48003554243683E96, - 1.7112245242814127E98, - 1.1978571669969892E100, - 8.504785885678624E101, - 6.123445837688612E103, - 4.470115461512686E105, - 3.307885441519387E107, - 2.4809140811395404E109, - 1.8854947016660506E111, - 1.451830920282859E113, - 1.1324281178206295E115, - 8.94618213078298E116, - 7.15694570462638E118, - 5.797126020747369E120, - 4.7536433370128435E122, - 3.94552396972066E124, - 3.314240134565354E126, - 2.8171041143805494E128, - 2.4227095383672744E130, - 2.107757298379527E132, - 1.854826422573984E134, - 1.6507955160908465E136, - 1.4857159644817605E138, - 1.3520015276784033E140, - 1.2438414054641305E142, - 1.156772507081641E144, - 1.0873661566567426E146, - 1.0329978488239061E148, - 9.916779348709491E149, - 9.619275968248216E151, - 9.426890448883248E153, - 9.332621544394415E155, - 9.332621544394418E157, - 9.42594775983836E159, - 9.614466715035125E161, - 9.902900716486178E163, - 1.0299016745145631E166, - 1.0813967582402912E168, - 1.1462805637347086E170, - 1.2265202031961373E172, - 1.324641819451829E174, - 1.4438595832024942E176, - 1.5882455415227423E178, - 1.7629525510902457E180, - 1.974506857221075E182, - 2.2311927486598138E184, - 2.543559733472186E186, - 2.925093693493014E188, - 3.393108684451899E190, - 3.96993716080872E192, - 4.6845258497542896E194, - 5.574585761207606E196, - 6.689502913449135E198, - 8.094298525273444E200, - 9.875044200833601E202, - 1.2146304367025332E205, - 1.506141741511141E207, - 1.882677176888926E209, - 2.3721732428800483E211, - 3.0126600184576624E213, - 3.856204823625808E215, - 4.974504222477287E217, - 6.466855489220473E219, - 8.471580690878813E221, - 1.1182486511960037E224, - 1.4872707060906847E226, - 1.99294274616152E228, - 2.690472707318049E230, - 3.6590428819525483E232, - 5.0128887482749884E234, - 6.917786472619482E236, - 9.615723196941089E238, - 1.3462012475717523E241, - 1.8981437590761713E243, - 2.6953641378881633E245, - 3.8543707171800694E247, - 5.550293832739308E249, - 8.047926057471989E251, - 1.1749972043909107E254, - 1.72724589045464E256, - 2.5563239178728637E258, - 3.8089226376305687E260, - 5.7133839564458575E262, - 8.627209774233244E264, - 1.3113358856834527E267, - 2.0063439050956838E269, - 3.0897696138473515E271, - 4.789142901463393E273, - 7.471062926282892E275, - 1.1729568794264134E278, - 1.8532718694937346E280, - 2.946702272495036E282, - 4.714723635992061E284, - 7.590705053947223E286, - 1.2296942187394494E289, - 2.0044015765453032E291, - 3.287218585534299E293, - 5.423910666131583E295, - 9.003691705778434E297, - 1.5036165148649983E300, - 2.5260757449731988E302, - 4.2690680090047056E304, - 7.257415615308004E306 - }; - - /** - * Efficiently returns the binomial coefficient, often also referred to as - * "n over k" or "n choose k". The binomial coefficient is defined as - * {@code (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )}. - *
  • {@code k<0}: {@code 0}.
  • - *
  • {@code k==0}: {@code 1}.
  • - *
  • {@code k==1}: {@code n}.
  • - *
  • else: {@code (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k)}.
  • - *
- * - * @param n Size of set. - * @param k Size of subset. - * @return Binomial coefficient. - */ - public static double binomial(double n, long k) { - if (k < 0) - return 0; - - if (k == 0) - return 1; - - if (k == 1) - return n; - - // binomial(n,k) = (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ) - double a = n - k + 1; - double b = 1; - double binomial = 1; - - for (long i = k; i-- > 0; ) - binomial *= (a++) / (b++); - - return binomial; - } - - /** - * Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k". - * The binomial coefficient is defined as - *
  • {@code k<0}: {@code 0}.
  • {@code k==0 || k==n}: {@code 1}.
  • {@code k==1 || k==n-1}: - * {@code n}.
  • else: {@code (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )}.
- * - * @param n Size of set. - * @param k Size of subset. - * @return Binomial coefficient. - */ - public static double binomial(long n, long k) { - if (k < 0) - return 0; - - if (k == 0 || k == n) - return 1; - - if (k == 1 || k == n - 1) - return n; - - if (n > k) { - int max = LONG_FACTORIALS.length + DOUBLE_FACTORIALS.length; - - if (n < max) { - double nFac = factorial((int)n); - double kFac = factorial((int)k); - double nMinusKFac = factorial((int)(n - k)); - double nk = nMinusKFac * kFac; - - if (nk != Double.POSITIVE_INFINITY) // No numeric overflow? - return nFac / nk; - } - - if (k > n / 2) - k = n - k; - } - - // binomial(n,k) = (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ) - long a = n - k + 1; - long b = 1; - double binomial = 1; - - for (long i = k; i-- > 0; ) - binomial *= (double)a++ / (b++); - - return binomial; - } - - /** - * Returns the smallest long >= value. - *
Examples: {@code 1.0 -> 1, 1.2 -> 2, 1.9 -> 2}. This - * method is safer than using (long) Math.ceil(value), because of possible rounding error.
- * - * @param val Value for ceil. - * @return Ceil of the given value. - */ - public static long ceil(double val) { - return Math.round(Math.ceil(val)); - } - - /** - * Evaluates the series of Chebyshev polynomials Ti at argument x/2. The series is given by - *
-     * N-1
-     * - '
-     * y  =   >   coef[i] T (x/2)
-     * -            i
-     * i=0
-     * 
- * Coefficients are stored in reverse order, i.e. the zero order term is last in the array. Note N is the number of - * coefficients, not the order. - *

- * If coefficients are for the interval a to b, x must have been transformed to x - * -< 2(2x - b - a)/(b-a) before entering the routine. This maps x from (a, b) to (-1, 1), over which the - * Chebyshev polynomials are defined.

- *

- * If the coefficients are for the inverted interval, in which (a, b) is - * mapped to (1/b, 1/a), the transformation required is {@code x -> 2(2ab/x - b - a)/(b-a)}. If b is infinity, this - * becomes {@code x -> 4a/x - 1}.

- *

- * SPEED: - *

- * Taking advantage of the recurrence properties of the Chebyshev - * polynomials, the routine requires one more addition per loop than evaluating a nested polynomial of the same - * degree. - * - * @param x Argument to the polynomial. - * @param coef Coefficients of the polynomial. - * @param N Number of coefficients. - */ - public static double chbevl(double x, double[] coef, int N) { - int p = 0; - - double b0 = coef[p++]; - double b1 = 0.0; - int i = N - 1; - - double b2; - - do { - b2 = b1; - b1 = b0; - b0 = x * b1 - b2 + coef[p++]; - } - while (--i > 0); - - return 0.5 * (b0 - b2); - } - - /** - * Instantly returns the factorial {@code k!}. - * - * @param k must hold {@code k >= 0}. - */ - private static double factorial(int k) { - if (k < 0) - throw new IllegalArgumentException(); - - int len1 = LONG_FACTORIALS.length; - - if (k < len1) - return LONG_FACTORIALS[k]; - - int len2 = DOUBLE_FACTORIALS.length; - - return (k < len1 + len2) ? DOUBLE_FACTORIALS[k - len1] : Double.POSITIVE_INFINITY; - } - - /** - * Returns the largest long <= value. - *
Examples: {@code 1.0 -> 1, 1.2 -> 1, 1.9 -> 1
2.0 -> 2, 2.2 -> 2, 2.9 -> 2}
- * This method is safer than using (long) Math.floor(value), because of possible rounding error. - */ - public static long floor(double val) { - return Math.round(Math.floor(val)); - } - - /** - * Returns {@code logbasevalue}. - */ - public static double log(double base, double val) { - return Math.log(val) / Math.log(base); - } - - /** - * Returns {@code log10value}. - */ - public static double log10(double val) { - // 1.0 / Math.log(10) == 0.43429448190325176 - return Math.log(val) * 0.43429448190325176; - } - - /** - * Returns {@code log2value}. - */ - public static double log2(double val) { - // 1.0 / Math.log(2) == 1.4426950408889634 - return Math.log(val) * 1.4426950408889634; - } - - /** - * Returns {@code log(k!)}. Tries to avoid overflows. For {@code k<30} simply looks up a table in O(1). - * For {@code k>=30} uses Stirling's approximation. - * - * @param k must hold {@code k >= 0}. - */ - public static double logFactorial(int k) { - if (k >= 30) { - double r = 1.0 / k; - double rr = r * r; - double C7 = -5.95238095238095238e-04; - double C5 = 7.93650793650793651e-04; - double C3 = -2.77777777777777778e-03; - double C1 = 8.33333333333333333e-02; - double C0 = 9.18938533204672742e-01; - - return (k + 0.5) * Math.log(k) - k + C0 + r * (C1 + rr * (C3 + rr * (C5 + rr * C7))); - } - else - return LOG_FACTORIALS[k]; - } - - /** - * Instantly returns the factorial {@code k!}. - * - * @param k must hold {@code k >= 0 && k < 21} - */ - public static long longFactorial(int k) { - if (k < 0) - throw new IllegalArgumentException("Negative k"); - - if (k < LONG_FACTORIALS.length) - return LONG_FACTORIALS[k]; - - throw new IllegalArgumentException("Overflow"); - } - - /** - * Returns the StirlingCorrection. - *

- * Correction term of the Stirling approximation for {@code log(k!)} (series in - * 1/k, or table values for small k) with int parameter k. {@code log k! = (k + 1/2)log(k + 1) - (k + 1) + - * (1/2)log(2Pi) + STIRLING_CORRECTION(k + 1) log k! = (k + 1/2)log(k) - k + (1/2)log(2Pi) + - * STIRLING_CORRECTION(k) }

- */ - public static double stirlingCorrection(int k) { - if (k > 30) { - double r = 1.0 / k; - double rr = r * r; - double C7 = -5.95238095238095238e-04; - double C5 = 7.93650793650793651e-04; - double C3 = -2.77777777777777778e-03; - double C1 = 8.33333333333333333e-02; - - return r * (C1 + rr * (C3 + rr * (C5 + rr * C7))); - } - else - return STIRLING_CORRECTION[k]; - } - - /** - * Evaluates the given polynomial of degree {@code N} at {@code x}, assuming coefficient of N is 1.0. Otherwise same - * as {@link #evalPoly(double, double[], int)}. - *
-     * 2          N
-     * y  =  C  + C x + C x  +...+ C x
-     * 0    1     2          N
-     * 
- * where
-     * C  = 1
-     * N
-     * 
- * and hence is omitted from the array. - *

- * Coefficients are stored in reverse order:

- *
-     * coef[0] = C  , ..., coef[N-1] = C  .
-     * N-1                   0
-     * 
- * Calling arguments are otherwise the same as {@link #evalPoly(double, double[], int)}. - *

- * In the interest of speed, there are no checks for out of bounds arithmetic. - * - * @param x Argument to the polynomial. - * @param coef Coefficients of the polynomial. - * @param n Degree of the polynomial. - */ - public static double evalPoly1(double x, double[] coef, int n) { - double res = x + coef[0]; - - for (int i = 1; i < n; i++) - res = res * x + coef[i]; - - return res; - } - - /** - * Evaluates the given polynomial of degree {@code N} at {@code x}. - *

-     * 2          N
-     * y  =  C  + C x + C x  +...+ C x
-     * 0    1     2          N
-     * 
- *

- * Coefficients are stored in reverse order:

- *
-     * coef[0] = C  , ..., coef[N] = C  .
-     * N                   0
-     * 
- *

- * In the interest of speed, there are no checks for out of bounds arithmetic.

- * - * @param x Argument to the polynomial. - * @param coef Coefficients of the polynomial. - * @param n Degree of the polynomial. - */ - public static double evalPoly(double x, double[] coef, int n) { - double res = coef[0]; - - for (int i = 1; i <= n; i++) - res = res * x + coef[i]; - - return res; - } - - /** - * Gets sqrt(a^2 + b^2) without under/overflow. - * - * @param a First side value. - * @param b Second side value. - * @return Hypotenuse value. - */ - public static double hypot(double a, double b) { - double r; - - if (Math.abs(a) > Math.abs(b)) { - r = b / a; - r = Math.abs(a) * Math.sqrt(1 + r * r); - } - else if (b != 0) { - r = a / b; - r = Math.abs(b) * Math.sqrt(1 + r * r); - } - else - r = 0.0; - - return r; - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/Blas.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/Blas.java index 99297252a7a04..bc603c8db1fd7 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/Blas.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/Blas.java @@ -25,10 +25,7 @@ import org.apache.ignite.ml.math.exceptions.NonSquareMatrixException; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrix; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.vector.CacheVector; import org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; import org.apache.ignite.ml.math.impls.vector.SparseLocalOffHeapVector; @@ -271,8 +268,7 @@ else if (alpha == 0.0) * Currently we support only local onheap matrices for BLAS. */ private static void checkMatrixType(Matrix a, String op) { - if (a instanceof DenseLocalOffHeapMatrix || a instanceof SparseDistributedMatrix - || a instanceof SparseBlockDistributedMatrix) + if (a instanceof DenseLocalOffHeapMatrix) throw new IllegalArgumentException("Operation doesn't support for matrix [class=" + a.getClass().getName() + ", operation=" + op + "]."); } @@ -281,7 +277,7 @@ private static void checkMatrixType(Matrix a, String op) { * Currently we support only local onheap vectors for BLAS. */ private static void checkVectorType(Vector a, String op) { - if (a instanceof DenseLocalOffHeapVector || a instanceof SparseLocalOffHeapVector || a instanceof CacheVector) + if (a instanceof DenseLocalOffHeapVector || a instanceof SparseLocalOffHeapVector) throw new IllegalArgumentException("Operation doesn't support for vector [class=" + a.getClass().getName() + ", operation=" + op + "]."); } diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/Constants.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/Constants.java deleted file mode 100644 index f9b6c8854f7ef..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/Constants.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* -Copyright 1999 CERN - European Organization for Nuclear Research. -Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose -is hereby granted without fee, provided that the above copyright notice appear in all copies and -that both that copyright notice and this permission notice appear in supporting documentation. -CERN makes no representations about the suitability of this software for any purpose. -It is provided "as is" without expressed or implied warranty. -*/ - -package org.apache.ignite.ml.math; - -/** - * Math constants. Lifted from Apache Mahout. - */ -public class Constants { - /** Constant for {@code 2**-53}. */ - public static final double MACHEP = 1.11022302462515654042E-16; - - /** Constant for {@code log(2**1024)}. */ - public static final double MAXLOG = 7.09782712893383996732E2; - - /** Constant for {@code log(2**-1022)}. */ - public static final double MINLOG = -7.451332191019412076235E2; - - /** Constant for gamma function. */ - public static final double MAXGAM = 171.624376956302725; - - /** Constant for {@code 1/(sqrt(2*pi))}. */ - public static final double SQTPI = 2.50662827463100050242E0; - - /** Constant for {@code sqrt(2)/2}. */ - public static final double SQRTH = 7.07106781186547524401E-1; - - /** Constant for {@code log(Pi)}. */ - public static final double LOGPI = 1.14472988584940017414; - - /** Constant for big value. */ - public static final double BIG = 4.503599627370496e15; - - /** Constant for inverse of big value. */ - public static final double BIGINV = 2.22044604925031308085e-16; -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/IdentityValueMapper.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/IdentityValueMapper.java deleted file mode 100644 index 615006efa2b39..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/IdentityValueMapper.java +++ /dev/null @@ -1,56 +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 org.apache.ignite.ml.math.distributed.ValueMapper; - -/** - * Identity value mapper. - */ -public class IdentityValueMapper implements ValueMapper { - /** */ - private static final long serialVersionUID = -8010078306142216389L; - - /** {@inheritDoc} */ - @Override public Double fromDouble(double v) { - return v; - } - - /** {@inheritDoc} */ - @Override public double toDouble(Double v) { - assert v != null; - - return v; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - return Long.hashCode(serialVersionUID); - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - return true; - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/MathUtils.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/MathUtils.java deleted file mode 100644 index c7b33c3c3626a..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/MathUtils.java +++ /dev/null @@ -1,32 +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 org.apache.ignite.ml.math.exceptions.NullArgumentException; - -/** - * Miscellaneous utility functions. - */ -public final class MathUtils { - /** */ - public static void checkNotNull(Object o) - throws NullArgumentException { - if (o == null) - throw new NullArgumentException(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/Matrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/Matrix.java index 9d1ece443b536..0e85659ed2f33 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/Matrix.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/Matrix.java @@ -279,21 +279,6 @@ interface Element { */ public int rowSize(); - /** - * Returns matrix determinant using Laplace theorem. - * - * @return A determinant for this matrix. - * @throws CardinalityException Thrown if matrix is not square. - */ - public double determinant(); - - /** - * Returns the inverse matrix of this matrix - * - * @return Inverse of this matrix - */ - public Matrix inverse(); - /** * Divides each value in this matrix by the argument. * 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 index 2f51245815db5..85c61bb7cdb43 100644 --- 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 @@ -21,7 +21,6 @@ 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.SparseDistributedVector; /** * Some utils for {@link Vector}. @@ -62,7 +61,7 @@ public static Vector num2Vec(int num, int vecSize) { * @return One-hot encoded number. */ public static Vector num2Vec(int num, int vecSize, boolean isDistributed) { - Vector res = isDistributed ? new SparseDistributedVector(vecSize) : new DenseLocalOnHeapVector(vecSize); + Vector res = new DenseLocalOnHeapVector(vecSize); return res.setX(num, 1); } diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/CholeskyDecomposition.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/CholeskyDecomposition.java deleted file mode 100644 index d1a3f512f5a45..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/CholeskyDecomposition.java +++ /dev/null @@ -1,309 +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.decompositions; - -import org.apache.ignite.ml.math.Destroyable; -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.exceptions.NonPositiveDefiniteMatrixException; -import org.apache.ignite.ml.math.exceptions.NonSymmetricMatrixException; -import org.apache.ignite.ml.math.util.MatrixUtil; - -import static org.apache.ignite.ml.math.util.MatrixUtil.like; -import static org.apache.ignite.ml.math.util.MatrixUtil.likeVector; - -/** - * Calculates the Cholesky decomposition of a matrix. - *

- * This class inspired by class from Apache Common Math with similar name.

- * - * @see MathWorld - * @see Wikipedia - */ -public class CholeskyDecomposition implements Destroyable { - /** - * Default threshold above which off-diagonal elements are considered too different - * and matrix not symmetric. - */ - public static final double DFLT_REL_SYMMETRY_THRESHOLD = 1.0e-15; - - /** - * Default threshold below which diagonal elements are considered null - * and matrix not positive definite. - */ - public static final double DFLT_ABS_POSITIVITY_THRESHOLD = 1.0e-10; - - /** Row-oriented storage for LT matrix data. */ - private double[][] lTData; - /** Cached value of L. */ - private Matrix cachedL; - /** Cached value of LT. */ - private Matrix cachedLT; - /** Origin matrix */ - private Matrix origin; - - /** - * Calculates the Cholesky decomposition of the given matrix. - *

- * Calling this constructor is equivalent to call {@link #CholeskyDecomposition(Matrix, double, double)} with the - * thresholds set to the default values {@link #DFLT_REL_SYMMETRY_THRESHOLD} and - * {@link #DFLT_ABS_POSITIVITY_THRESHOLD}.

- * - * @param mtx the matrix to decompose. - * @throws CardinalityException if matrix is not square. - * @see #CholeskyDecomposition(Matrix, double, double) - * @see #DFLT_REL_SYMMETRY_THRESHOLD - * @see #DFLT_ABS_POSITIVITY_THRESHOLD - */ - public CholeskyDecomposition(final Matrix mtx) { - this(mtx, DFLT_REL_SYMMETRY_THRESHOLD, DFLT_ABS_POSITIVITY_THRESHOLD); - } - - /** - * Calculates the Cholesky decomposition of the given matrix. - * - * @param mtx the matrix to decompose. - * @param relSymmetryThreshold threshold above which off-diagonal elements are considered too different and matrix - * not symmetric - * @param absPositivityThreshold threshold below which diagonal elements are considered null and matrix not positive - * definite - * @see #CholeskyDecomposition(Matrix) - * @see #DFLT_REL_SYMMETRY_THRESHOLD - * @see #DFLT_ABS_POSITIVITY_THRESHOLD - */ - public CholeskyDecomposition(final Matrix mtx, final double relSymmetryThreshold, - final double absPositivityThreshold) { - assert mtx != null; - - if (mtx.columnSize() != mtx.rowSize()) - throw new CardinalityException(mtx.rowSize(), mtx.columnSize()); - - origin = mtx; - - final int order = mtx.rowSize(); - - lTData = toDoubleArr(mtx); - cachedL = null; - cachedLT = null; - - // Check the matrix before transformation. - for (int i = 0; i < order; ++i) { - final double[] lI = lTData[i]; - - // Check off-diagonal elements (and reset them to 0). - for (int j = i + 1; j < order; ++j) { - final double[] lJ = lTData[j]; - - final double lIJ = lI[j]; - final double lJI = lJ[i]; - - final double maxDelta = relSymmetryThreshold * Math.max(Math.abs(lIJ), Math.abs(lJI)); - - if (Math.abs(lIJ - lJI) > maxDelta) - throw new NonSymmetricMatrixException(i, j, relSymmetryThreshold); - - lJ[i] = 0; - } - } - - // Transform the matrix. - for (int i = 0; i < order; ++i) { - final double[] ltI = lTData[i]; - - // Check diagonal element. - if (ltI[i] <= absPositivityThreshold) - throw new NonPositiveDefiniteMatrixException(ltI[i], i, absPositivityThreshold); - - ltI[i] = Math.sqrt(ltI[i]); - final double inverse = 1.0 / ltI[i]; - - for (int q = order - 1; q > i; --q) { - ltI[q] *= inverse; - final double[] ltQ = lTData[q]; - - for (int p = q; p < order; ++p) - ltQ[p] -= ltI[q] * ltI[p]; - } - } - } - - /** {@inheritDoc} */ - @Override public void destroy() { - if (cachedL != null) - cachedL.destroy(); - if (cachedLT != null) - cachedLT.destroy(); - } - - /** - * Returns the matrix L of the decomposition. - *

L is an lower-triangular matrix

- * - * @return the L matrix - */ - public Matrix getL() { - if (cachedL == null) - cachedL = getLT().transpose(); - - return cachedL; - } - - /** - * Returns the transpose of the matrix L of the decomposition. - *

LT is an upper-triangular matrix

- * - * @return the transpose of the matrix L of the decomposition - */ - public Matrix getLT() { - - if (cachedLT == null) { - Matrix like = like(origin, origin.rowSize(), origin.columnSize()); - like.assign(lTData); - - cachedLT = like; - } - - // return the cached matrix - return cachedLT; - } - - /** - * Return the determinant of the matrix - * - * @return determinant of the matrix - */ - public double getDeterminant() { - double determinant = 1.0; - - for (int i = 0; i < lTData.length; ++i) { - double lTii = lTData[i][i]; - determinant *= lTii * lTii; - } - - return determinant; - } - - /** - * Solve the linear equation A × X = B for matrices A. - * - * @param b right-hand side of the equation A × X = B - * @return a vector X that minimizes the two norm of A × X - B - * @throws CardinalityException if the vectors dimensions do not match - */ - public Vector solve(final Vector b) { - final int m = lTData.length; - - if (b.size() != m) - throw new CardinalityException(b.size(), m); - - final double[] x = b.getStorage().data(); - - // Solve LY = b - for (int j = 0; j < m; j++) { - final double[] lJ = lTData[j]; - - x[j] /= lJ[j]; - - final double xJ = x[j]; - - for (int i = j + 1; i < m; i++) - x[i] -= xJ * lJ[i]; - } - - // Solve LTX = Y - for (int j = m - 1; j >= 0; j--) { - x[j] /= lTData[j][j]; - - final double xJ = x[j]; - - for (int i = 0; i < j; i++) - x[i] -= xJ * lTData[i][j]; - } - - return likeVector(origin, m).assign(x); - } - - /** - * Solve the linear equation A × X = B for matrices A. - * - * @param b right-hand side of the equation A × X = B - * @return a matrix X that minimizes the two norm of A × X - B - * @throws CardinalityException if the matrices dimensions do not match - */ - public Matrix solve(final Matrix b) { - final int m = lTData.length; - - if (b.rowSize() != m) - throw new CardinalityException(b.rowSize(), m); - - final int nColB = b.columnSize(); - final double[][] x = MatrixUtil.unflatten(b.getStorage().data(), b.columnSize(), b.getStorage().storageMode()); - - // Solve LY = b - for (int j = 0; j < m; j++) { - final double[] lJ = lTData[j]; - final double lJJ = lJ[j]; - final double[] xJ = x[j]; - - for (int k = 0; k < nColB; ++k) - xJ[k] /= lJJ; - - for (int i = j + 1; i < m; i++) { - final double[] xI = x[i]; - final double lJI = lJ[i]; - - for (int k = 0; k < nColB; ++k) - xI[k] -= xJ[k] * lJI; - } - } - - // Solve LTX = Y - for (int j = m - 1; j >= 0; j--) { - final double lJJ = lTData[j][j]; - final double[] xJ = x[j]; - - for (int k = 0; k < nColB; ++k) - xJ[k] /= lJJ; - - for (int i = 0; i < j; i++) { - final double[] xI = x[i]; - final double lIJ = lTData[i][j]; - - for (int k = 0; k < nColB; ++k) - xI[k] -= xJ[k] * lIJ; - } - } - - return like(origin, m, b.columnSize()).assign(x); - } - - /** */ - private double[][] toDoubleArr(Matrix mtx) { - if (mtx.isArrayBased()) - return MatrixUtil.unflatten(mtx.getStorage().data(), mtx.columnSize(), mtx.getStorage().storageMode()); - - double[][] res = new double[mtx.rowSize()][mtx.columnSize()]; - - for (int row = 0; row < mtx.rowSize(); row++) - for (int col = 0; col < mtx.columnSize(); col++) - res[row][col] = mtx.get(row, col); - - return res; - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/DecompositionSupport.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/DecompositionSupport.java deleted file mode 100644 index 20d6e79c76dda..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/DecompositionSupport.java +++ /dev/null @@ -1,105 +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.decompositions; - -import org.apache.ignite.ml.math.Destroyable; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.matrix.CacheMatrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.matrix.RandomMatrix; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; - -/** - * Helper methods to support decomposition of matrix types having some functionality limited. - */ -public abstract class DecompositionSupport implements Destroyable { - /** - * Create the like matrix with read-only matrices support. - * - * @param matrix Matrix for like. - * @return Like matrix. - */ - protected Matrix like(Matrix matrix) { - if (isCopyLikeSupport(matrix)) - return new DenseLocalOnHeapMatrix(matrix.rowSize(), matrix.columnSize()); - else - return matrix.like(matrix.rowSize(), matrix.columnSize()); - } - - /** - * Create the like matrix with specified size with read-only matrices support. - * - * @param matrix Matrix for like. - * @return Like matrix. - */ - protected Matrix like(Matrix matrix, int rows, int cols) { - if (isCopyLikeSupport(matrix)) - return new DenseLocalOnHeapMatrix(rows, cols); - else - return matrix.like(rows, cols); - } - - /** - * Create the like vector with read-only matrices support. - * - * @param matrix Matrix for like. - * @param crd Cardinality of the vector. - * @return Like vector. - */ - protected Vector likeVector(Matrix matrix, int crd) { - if (isCopyLikeSupport(matrix)) - return new DenseLocalOnHeapVector(crd); - else - return matrix.likeVector(crd); - } - - /** - * Create the like vector with read-only matrices support. - * - * @param matrix Matrix for like. - * @return Like vector. - */ - protected Vector likeVector(Matrix matrix) { - return likeVector(matrix, matrix.rowSize()); - } - - /** - * Create the copy of matrix with read-only matrices support. - * - * @param matrix Matrix for copy. - * @return Copy. - */ - protected Matrix copy(Matrix matrix) { - if (isCopyLikeSupport(matrix)) { - DenseLocalOnHeapMatrix cp = new DenseLocalOnHeapMatrix(matrix.rowSize(), matrix.columnSize()); - - cp.assign(matrix); - - return cp; - } - else - return matrix.copy(); - } - - /** */ - private boolean isCopyLikeSupport(Matrix matrix) { - return matrix instanceof RandomMatrix || matrix instanceof PivotedMatrixView || matrix instanceof CacheMatrix; - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/EigenDecomposition.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/EigenDecomposition.java deleted file mode 100644 index a5c92e68f8d41..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/EigenDecomposition.java +++ /dev/null @@ -1,936 +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.decompositions; - -import org.apache.ignite.ml.math.Destroyable; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.functions.Functions; - -import static org.apache.ignite.ml.math.util.MatrixUtil.like; -import static org.apache.ignite.ml.math.util.MatrixUtil.likeVector; - -/** - * This class provides EigenDecomposition of given matrix. The class is based on - * class with similar name from Apache Mahout library. - * - * @see MathWorld - */ -public class EigenDecomposition implements Destroyable { - /** Row and column dimension (square matrix). */ - private final int n; - - /** Array for internal storage of eigen vectors. */ - private final Matrix v; - - /** Array for internal storage of eigenvalues. */ - private final Vector d; - /** Array for internal storage of eigenvalues. */ - private final Vector e; - - /** - * @param matrix Matrix to decompose. - */ - public EigenDecomposition(Matrix matrix) { - this(matrix, isSymmetric(matrix)); - } - - /** - * @param matrix Matrix to decompose. - * @param isSymmetric {@code true} if matrix passes symmetry check, {@code false otherwise}. - */ - public EigenDecomposition(Matrix matrix, boolean isSymmetric) { - n = matrix.columnSize(); - - d = likeVector(matrix); - e = likeVector(matrix); - v = like(matrix); - - if (isSymmetric) { - v.assign(matrix); - - // Tridiagonalize. - tred2(); - - // Diagonalize. - tql2(); - - } - else - // Reduce to Hessenberg form. - // Reduce Hessenberg to real Schur form. - hqr2(orthes(matrix)); - } - - /** - * Return the eigen vector matrix. - * - * @return V - */ - public Matrix getV() { - return like(v).assign(v); - } - - /** - * Return the real parts of the eigenvalues - */ - public Vector getRealEigenValues() { - return d; - } - - /** - * Return the imaginary parts of the eigenvalues. - * - * @return Vector of imaginary parts. - */ - public Vector getImagEigenvalues() { - return e; - } - - /** - * Return the block diagonal eigenvalue matrix - * - * @return D - */ - public Matrix getD() { - Matrix res = like(v, d.size(), d.size()); - res.assign(0); - res.viewDiagonal().assign(d); - for (int i = 0; i < n; i++) { - double v = e.getX(i); - if (v > 0) - res.setX(i, i + 1, v); - else if (v < 0) - res.setX(i, i - 1, v); - } - return res; - } - - /** - * Destroys decomposition components and other internal components of decomposition. - */ - @Override public void destroy() { - e.destroy(); - v.destroy(); - d.destroy(); - } - - /** */ - private void tred2() { - // This is derived from the Algol procedures tred2 by - // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for - // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding - // Fortran subroutine in EISPACK. - - d.assign(v.viewColumn(n - 1)); - - // Householder reduction to tridiagonal form. - - for (int i = n - 1; i > 0; i--) { - - // Scale to avoid under/overflow. - double scale = d.viewPart(0, i).kNorm(1); - double h = 0.0; - - if (scale == 0.0) { - e.setX(i, d.getX(i - 1)); - for (int j = 0; j < i; j++) { - d.setX(j, v.getX(i - 1, j)); - v.setX(i, j, 0.0); - v.setX(j, i, 0.0); - } - } - else { - - // Generate Householder vector. - - for (int k = 0; k < i; k++) { - d.setX(k, d.getX(k) / scale); - h += d.getX(k) * d.getX(k); - } - - double f = d.getX(i - 1); - double g = Math.sqrt(h); - - if (f > 0) - g = -g; - - e.setX(i, scale * g); - h -= f * g; - d.setX(i - 1, f - g); - - for (int j = 0; j < i; j++) - e.setX(j, 0.0); - - // Apply similarity transformation to remaining columns. - - for (int j = 0; j < i; j++) { - f = d.getX(j); - v.setX(j, i, f); - g = e.getX(j) + v.getX(j, j) * f; - - for (int k = j + 1; k <= i - 1; k++) { - g += v.getX(k, j) * d.getX(k); - e.setX(k, e.getX(k) + v.getX(k, j) * f); - } - - e.setX(j, g); - } - - f = 0.0; - - for (int j = 0; j < i; j++) { - e.setX(j, e.getX(j) / h); - f += e.getX(j) * d.getX(j); - } - - double hh = f / (h + h); - - for (int j = 0; j < i; j++) - e.setX(j, e.getX(j) - hh * d.getX(j)); - - for (int j = 0; j < i; j++) { - f = d.getX(j); - g = e.getX(j); - - for (int k = j; k <= i - 1; k++) - v.setX(k, j, v.getX(k, j) - (f * e.getX(k) + g * d.getX(k))); - - d.setX(j, v.getX(i - 1, j)); - v.setX(i, j, 0.0); - } - } - - d.setX(i, h); - } - } - - /** */ - private Matrix orthes(Matrix matrix) { - // Working storage for nonsymmetric algorithm. - Vector ort = likeVector(matrix); - Matrix hessenBerg = like(matrix).assign(matrix); - - // This is derived from the Algol procedures orthes and ortran, - // by Martin and Wilkinson, Handbook for Auto. Comp., - // Vol.ii-Linear Algebra, and the corresponding - // Fortran subroutines in EISPACK. - - int low = 0; - int high = n - 1; - - for (int m = low + 1; m <= high - 1; m++) { - - // Scale column. - - Vector hCol = hessenBerg.viewColumn(m - 1).viewPart(m, high - m + 1); - double scale = hCol.kNorm(1); - - if (scale != 0.0) { - // Compute Householder transformation. - ort.viewPart(m, high - m + 1).map(hCol, Functions.plusMult(1 / scale)); - double h = ort.viewPart(m, high - m + 1).getLengthSquared(); - - double g = Math.sqrt(h); - - if (ort.getX(m) > 0) - g = -g; - - h -= ort.getX(m) * g; - ort.setX(m, ort.getX(m) - g); - - // Apply Householder similarity transformation - // H = (I-u*u'/h)*H*(I-u*u')/h) - - Vector ortPiece = ort.viewPart(m, high - m + 1); - - for (int j = m; j < n; j++) { - double f = ortPiece.dot(hessenBerg.viewColumn(j).viewPart(m, high - m + 1)) / h; - hessenBerg.viewColumn(j).viewPart(m, high - m + 1).map(ortPiece, Functions.plusMult(-f)); - } - - for (int i = 0; i <= high; i++) { - double f = ortPiece.dot(hessenBerg.viewRow(i).viewPart(m, high - m + 1)) / h; - hessenBerg.viewRow(i).viewPart(m, high - m + 1).map(ortPiece, Functions.plusMult(-f)); - } - - ort.setX(m, scale * ort.getX(m)); - hessenBerg.setX(m, m - 1, scale * g); - } - } - - // Accumulate transformations (Algol's ortran). - - v.assign(0); - v.viewDiagonal().assign(1); - - for (int m = high - 1; m >= low + 1; m--) { - if (hessenBerg.getX(m, m - 1) != 0.0) { - ort.viewPart(m + 1, high - m).assign(hessenBerg.viewColumn(m - 1).viewPart(m + 1, high - m)); - - for (int j = m; j <= high; j++) { - double g = ort.viewPart(m, high - m + 1).dot(v.viewColumn(j).viewPart(m, high - m + 1)); - - // Double division avoids possible underflow - g = g / ort.getX(m) / hessenBerg.getX(m, m - 1); - v.viewColumn(j).viewPart(m, high - m + 1).map(ort.viewPart(m, high - m + 1), Functions.plusMult(g)); - } - } - } - - return hessenBerg; - } - - /** - * Symmetric tridiagonal QL algorithm. - */ - private void tql2() { - // This is derived from the Algol procedures tql2, by - // Bowdler, Martin, Reinsch, and Wilkinson, Handbook for - // Auto. Comp., Vol.ii-Linear Algebra, and the corresponding - // Fortran subroutine in EISPACK. - - e.viewPart(0, n - 1).assign(e.viewPart(1, n - 1)); - e.setX(n - 1, 0.0); - - double f = 0.0; - double tst1 = 0.0; - double eps = Math.pow(2.0, -52.0); - - for (int l = 0; l < n; l++) { - // Find small subdiagonal element. - - tst1 = Math.max(tst1, Math.abs(d.getX(l)) + Math.abs(e.getX(l))); - int m = l; - - while (m < n) { - if (Math.abs(e.getX(m)) <= eps * tst1) - break; - - m++; - } - - // If m == l, d.getX(l) is an eigenvalue, - // otherwise, iterate. - - if (m > l) { - do { - // Compute implicit shift - - double g = d.getX(l); - double p = (d.getX(l + 1) - g) / (2.0 * e.getX(l)); - double r = Math.hypot(p, 1.0); - - if (p < 0) - r = -r; - - d.setX(l, e.getX(l) / (p + r)); - d.setX(l + 1, e.getX(l) * (p + r)); - double dl1 = d.getX(l + 1); - double h = g - d.getX(l); - - for (int i = l + 2; i < n; i++) - d.setX(i, d.getX(i) - h); - - f += h; - - // Implicit QL transformation. - - p = d.getX(m); - double c = 1.0; - double c2 = c; - double c3 = c; - double el1 = e.getX(l + 1); - double s = 0.0; - double s2 = 0.0; - - for (int i = m - 1; i >= l; i--) { - c3 = c2; - c2 = c; - s2 = s; - g = c * e.getX(i); - h = c * p; - r = Math.hypot(p, e.getX(i)); - e.setX(i + 1, s * r); - s = e.getX(i) / r; - c = p / r; - p = c * d.getX(i) - s * g; - d.setX(i + 1, h + s * (c * g + s * d.getX(i))); - - // Accumulate transformation. - - for (int k = 0; k < n; k++) { - h = v.getX(k, i + 1); - v.setX(k, i + 1, s * v.getX(k, i) + c * h); - v.setX(k, i, c * v.getX(k, i) - s * h); - } - } - - p = -s * s2 * c3 * el1 * e.getX(l) / dl1; - e.setX(l, s * p); - d.setX(l, c * p); - - // Check for convergence. - - } - while (Math.abs(e.getX(l)) > eps * tst1); - } - - d.setX(l, d.getX(l) + f); - e.setX(l, 0.0); - } - - // Sort eigenvalues and corresponding vectors. - - for (int i = 0; i < n - 1; i++) { - int k = i; - double p = d.getX(i); - - for (int j = i + 1; j < n; j++) - if (d.getX(j) > p) { - k = j; - p = d.getX(j); - } - - if (k != i) { - d.setX(k, d.getX(i)); - d.setX(i, p); - - for (int j = 0; j < n; j++) { - p = v.getX(j, i); - v.setX(j, i, v.getX(j, k)); - v.setX(j, k, p); - } - } - } - } - - /** */ - private void hqr2(Matrix h) { - // This is derived from the Algol procedure hqr2, - // by Martin and Wilkinson, Handbook for Auto. Comp., - // Vol.ii-Linear Algebra, and the corresponding - // Fortran subroutine in EISPACK. - - // Initialize - - int nn = this.n; - int n = nn - 1; - int low = 0; - int high = nn - 1; - double eps = Math.pow(2.0, -52.0); - double exshift = 0.0; - double p = 0; - double q = 0; - double r = 0; - double s = 0; - double z = 0; - double w; - double x; - double y; - - // Store roots isolated by balanc and compute matrix norm - - double norm = h.foldMap(Functions.PLUS, Functions.ABS, 0.0d); - - // Outer loop over eigenvalue index - - int iter = 0; - while (n >= low) { - // Look for single small sub-diagonal element - int l = n; - - while (l > low) { - s = Math.abs(h.getX(l - 1, l - 1)) + Math.abs(h.getX(l, l)); - - if (s == 0.0) - s = norm; - - if (Math.abs(h.getX(l, l - 1)) < eps * s) - break; - - l--; - } - - // Check for convergence - - if (l == n) { - // One root found - h.setX(n, n, h.getX(n, n) + exshift); - d.setX(n, h.getX(n, n)); - e.setX(n, 0.0); - n--; - iter = 0; - } - else if (l == n - 1) { - // Two roots found - w = h.getX(n, n - 1) * h.getX(n - 1, n); - p = (h.getX(n - 1, n - 1) - h.getX(n, n)) / 2.0; - q = p * p + w; - z = Math.sqrt(Math.abs(q)); - h.setX(n, n, h.getX(n, n) + exshift); - h.setX(n - 1, n - 1, h.getX(n - 1, n - 1) + exshift); - x = h.getX(n, n); - - // Real pair - if (q >= 0) { - if (p >= 0) - z = p + z; - else - z = p - z; - - d.setX(n - 1, x + z); - d.setX(n, d.getX(n - 1)); - - if (z != 0.0) - d.setX(n, x - w / z); - - e.setX(n - 1, 0.0); - e.setX(n, 0.0); - x = h.getX(n, n - 1); - s = Math.abs(x) + Math.abs(z); - p = x / s; - q = z / s; - r = Math.sqrt(p * p + q * q); - p /= r; - q /= r; - - // Row modification - - for (int j = n - 1; j < nn; j++) { - z = h.getX(n - 1, j); - h.setX(n - 1, j, q * z + p * h.getX(n, j)); - h.setX(n, j, q * h.getX(n, j) - p * z); - } - - // Column modification - - for (int i = 0; i <= n; i++) { - z = h.getX(i, n - 1); - h.setX(i, n - 1, q * z + p * h.getX(i, n)); - h.setX(i, n, q * h.getX(i, n) - p * z); - } - - // Accumulate transformations - - for (int i = low; i <= high; i++) { - z = v.getX(i, n - 1); - v.setX(i, n - 1, q * z + p * v.getX(i, n)); - v.setX(i, n, q * v.getX(i, n) - p * z); - } - - // Complex pair - - } - else { - d.setX(n - 1, x + p); - d.setX(n, x + p); - e.setX(n - 1, z); - e.setX(n, -z); - } - - n -= 2; - iter = 0; - - // No convergence yet - - } - else { - // Form shift - x = h.getX(n, n); - y = 0.0; - w = 0.0; - - if (l < n) { - y = h.getX(n - 1, n - 1); - w = h.getX(n, n - 1) * h.getX(n - 1, n); - } - - // Wilkinson's original ad hoc shift - - if (iter == 10) { - exshift += x; - - for (int i = low; i <= n; i++) - h.setX(i, i, x); - - s = Math.abs(h.getX(n, n - 1)) + Math.abs(h.getX(n - 1, n - 2)); - x = y = 0.75 * s; - w = -0.4375 * s * s; - } - - // MATLAB's new ad hoc shift - - if (iter == 30) { - s = (y - x) / 2.0; - s = s * s + w; - - if (s > 0) { - s = Math.sqrt(s); - - if (y < x) - s = -s; - - s = x - w / ((y - x) / 2.0 + s); - - for (int i = low; i <= n; i++) - h.setX(i, i, h.getX(i, i) - s); - - exshift += s; - x = y = w = 0.964; - } - } - - iter++; // (Could check iteration count here.) - - // Look for two consecutive small sub-diagonal elements - - int m = n - 2; - - while (m >= l) { - z = h.getX(m, m); - r = x - z; - s = y - z; - p = (r * s - w) / h.getX(m + 1, m) + h.getX(m, m + 1); - q = h.getX(m + 1, m + 1) - z - r - s; - r = h.getX(m + 2, m + 1); - s = Math.abs(p) + Math.abs(q) + Math.abs(r); - p /= s; - q /= s; - r /= s; - - if (m == l) - break; - - double hmag = Math.abs(h.getX(m - 1, m - 1)) + Math.abs(h.getX(m + 1, m + 1)); - double threshold = eps * Math.abs(p) * (Math.abs(z) + hmag); - - if (Math.abs(h.getX(m, m - 1)) * (Math.abs(q) + Math.abs(r)) < threshold) - break; - - m--; - } - - for (int i = m + 2; i <= n; i++) { - h.setX(i, i - 2, 0.0); - - if (i > m + 2) - h.setX(i, i - 3, 0.0); - } - - // Double QR step involving rows l:n and columns m:n - - for (int k = m; k <= n - 1; k++) { - boolean notlast = k != n - 1; - - if (k != m) { - p = h.getX(k, k - 1); - q = h.getX(k + 1, k - 1); - r = notlast ? h.getX(k + 2, k - 1) : 0.0; - x = Math.abs(p) + Math.abs(q) + Math.abs(r); - if (x != 0.0) { - p /= x; - q /= x; - r /= x; - } - } - - if (x == 0.0) - break; - - s = Math.sqrt(p * p + q * q + r * r); - - if (p < 0) - s = -s; - - if (s != 0) { - if (k != m) - h.setX(k, k - 1, -s * x); - else if (l != m) - h.setX(k, k - 1, -h.getX(k, k - 1)); - - p += s; - x = p / s; - y = q / s; - z = r / s; - q /= p; - r /= p; - - // Row modification - - for (int j = k; j < nn; j++) { - p = h.getX(k, j) + q * h.getX(k + 1, j); - - if (notlast) { - p += r * h.getX(k + 2, j); - h.setX(k + 2, j, h.getX(k + 2, j) - p * z); - } - - h.setX(k, j, h.getX(k, j) - p * x); - h.setX(k + 1, j, h.getX(k + 1, j) - p * y); - } - - // Column modification - - for (int i = 0; i <= Math.min(n, k + 3); i++) { - p = x * h.getX(i, k) + y * h.getX(i, k + 1); - - if (notlast) { - p += z * h.getX(i, k + 2); - h.setX(i, k + 2, h.getX(i, k + 2) - p * r); - } - - h.setX(i, k, h.getX(i, k) - p); - h.setX(i, k + 1, h.getX(i, k + 1) - p * q); - } - - // Accumulate transformations - - for (int i = low; i <= high; i++) { - p = x * v.getX(i, k) + y * v.getX(i, k + 1); - - if (notlast) { - p += z * v.getX(i, k + 2); - v.setX(i, k + 2, v.getX(i, k + 2) - p * r); - } - - v.setX(i, k, v.getX(i, k) - p); - v.setX(i, k + 1, v.getX(i, k + 1) - p * q); - } - } // (s != 0) - } // k loop - } // check convergence - } // while (n >= low) - - // Back substitute to find vectors of upper triangular form - - if (norm == 0.0) - return; - - for (n = nn - 1; n >= 0; n--) { - p = d.getX(n); - q = e.getX(n); - - // Real vector - - double t; - - if (q == 0) { - int l = n; - h.setX(n, n, 1.0); - - for (int i = n - 1; i >= 0; i--) { - w = h.getX(i, i) - p; - r = 0.0; - - for (int j = l; j <= n; j++) - r += h.getX(i, j) * h.getX(j, n); - - if (e.getX(i) < 0.0) { - z = w; - s = r; - } - else { - l = i; - - if (e.getX(i) == 0.0) { - if (w == 0.0) - h.setX(i, n, -r / (eps * norm)); - else - h.setX(i, n, -r / w); - - // Solve real equations - - } - else { - x = h.getX(i, i + 1); - y = h.getX(i + 1, i); - q = (d.getX(i) - p) * (d.getX(i) - p) + e.getX(i) * e.getX(i); - t = (x * s - z * r) / q; - h.setX(i, n, t); - - if (Math.abs(x) > Math.abs(z)) - h.setX(i + 1, n, (-r - w * t) / x); - else - h.setX(i + 1, n, (-s - y * t) / z); - } - - // Overflow control - - t = Math.abs(h.getX(i, n)); - - if (eps * t * t > 1) { - for (int j = i; j <= n; j++) - h.setX(j, n, h.getX(j, n) / t); - } - } - } - - // Complex vector - - } - else if (q < 0) { - int l = n - 1; - - // Last vector component imaginary so matrix is triangular - - if (Math.abs(h.getX(n, n - 1)) > Math.abs(h.getX(n - 1, n))) { - h.setX(n - 1, n - 1, q / h.getX(n, n - 1)); - h.setX(n - 1, n, -(h.getX(n, n) - p) / h.getX(n, n - 1)); - } - else { - cdiv(0.0, -h.getX(n - 1, n), h.getX(n - 1, n - 1) - p, q); - h.setX(n - 1, n - 1, cdivr); - h.setX(n - 1, n, cdivi); - } - - h.setX(n, n - 1, 0.0); - h.setX(n, n, 1.0); - - for (int i = n - 2; i >= 0; i--) { - double ra = 0.0; - double sa = 0.0; - - for (int j = l; j <= n; j++) { - ra += h.getX(i, j) * h.getX(j, n - 1); - sa += h.getX(i, j) * h.getX(j, n); - } - - w = h.getX(i, i) - p; - - if (e.getX(i) < 0.0) { - z = w; - r = ra; - s = sa; - } - else { - l = i; - - if (e.getX(i) == 0) { - cdiv(-ra, -sa, w, q); - h.setX(i, n - 1, cdivr); - h.setX(i, n, cdivi); - } - else { - - // Solve complex equations - - x = h.getX(i, i + 1); - y = h.getX(i + 1, i); - - double vr = (d.getX(i) - p) * (d.getX(i) - p) + e.getX(i) * e.getX(i) - q * q; - double vi = (d.getX(i) - p) * 2.0 * q; - - if (vr == 0.0 && vi == 0.0) { - double hmag = Math.abs(x) + Math.abs(y); - vr = eps * norm * (Math.abs(w) + Math.abs(q) + hmag + Math.abs(z)); - } - - cdiv(x * r - z * ra + q * sa, x * s - z * sa - q * ra, vr, vi); - - h.setX(i, n - 1, cdivr); - h.setX(i, n, cdivi); - - if (Math.abs(x) > (Math.abs(z) + Math.abs(q))) { - h.setX(i + 1, n - 1, (-ra - w * h.getX(i, n - 1) + q * h.getX(i, n)) / x); - h.setX(i + 1, n, (-sa - w * h.getX(i, n) - q * h.getX(i, n - 1)) / x); - } - else { - cdiv(-r - y * h.getX(i, n - 1), -s - y * h.getX(i, n), z, q); - - h.setX(i + 1, n - 1, cdivr); - h.setX(i + 1, n, cdivi); - } - } - - // Overflow control - - t = Math.max(Math.abs(h.getX(i, n - 1)), Math.abs(h.getX(i, n))); - - if (eps * t * t > 1) - for (int j = i; j <= n; j++) { - h.setX(j, n - 1, h.getX(j, n - 1) / t); - h.setX(j, n, h.getX(j, n) / t); - } - } - } - } - } - - // Vectors of isolated roots - - for (int i = 0; i < nn; i++) - if (i < low || i > high) { - for (int j = i; j < nn; j++) - v.setX(i, j, h.getX(i, j)); - } - - // Back transformation to get eigen vectors of original matrix - - for (int j = nn - 1; j >= low; j--) - for (int i = low; i <= high; i++) { - z = 0.0; - - for (int k = low; k <= Math.min(j, high); k++) - z += v.getX(i, k) * h.getX(k, j); - - v.setX(i, j, z); - } - } - - /** */ - private static boolean isSymmetric(Matrix matrix) { - int cols = matrix.columnSize(); - int rows = matrix.rowSize(); - - if (cols != rows) - return false; - - for (int i = 0; i < cols; i++) - for (int j = 0; j < rows; j++) { - if (matrix.getX(i, j) != matrix.get(j, i)) - return false; - } - - return true; - } - - /** Complex scalar division - real part. */ - private double cdivr; - /** Complex scalar division - imaginary part. */ - private double cdivi; - - /** */ - private void cdiv(double xr, double xi, double yr, double yi) { - double r; - double d; - - if (Math.abs(yr) > Math.abs(yi)) { - r = yi / yr; - d = yr + r * yi; - cdivr = (xr + r * xi) / d; - cdivi = (xi - r * xr) / d; - } - else { - r = yr / yi; - d = yi + r * yr; - cdivr = (r * xr + xi) / d; - cdivi = (r * xi - xr) / d; - } - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/LUDecomposition.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/LUDecomposition.java deleted file mode 100644 index 8b79d9bdf1e94..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/LUDecomposition.java +++ /dev/null @@ -1,383 +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.decompositions; - -import org.apache.ignite.ml.math.Destroyable; -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.exceptions.SingularMatrixException; - -import static org.apache.ignite.ml.math.util.MatrixUtil.copy; -import static org.apache.ignite.ml.math.util.MatrixUtil.like; -import static org.apache.ignite.ml.math.util.MatrixUtil.likeVector; - -/** - * Calculates the LU-decomposition of a square matrix. - *

- * This class is inspired by class from Apache Common Math with similar name.

- * - * @see MathWorld - * @see Wikipedia - * - *

TODO: IGNITE-5828, Maybe we should make this class (and other decompositions) Externalizable.

- */ -public class LUDecomposition implements Destroyable { - /** Default bound to determine effective singularity in LU decomposition. */ - private static final double DEFAULT_TOO_SMALL = 1e-11; - - /** Pivot permutation associated with LU decomposition. */ - private final Vector pivot; - - /** Parity of the permutation associated with the LU decomposition. */ - private boolean even; - /** Singularity indicator. */ - private boolean singular; - - /** Cached value of L. */ - private Matrix cachedL; - /** Cached value of U. */ - private Matrix cachedU; - /** Cached value of P. */ - private Matrix cachedP; - - /** Original matrix. */ - private Matrix matrix; - - /** Entries of LU decomposition. */ - private Matrix lu; - - /** - * Calculates the LU-decomposition of the given matrix. - * This constructor uses 1e-11 as default value for the singularity - * threshold. - * - * @param matrix Matrix to decompose. - * @throws CardinalityException if matrix is not square. - */ - public LUDecomposition(Matrix matrix) { - this(matrix, DEFAULT_TOO_SMALL); - } - - /** - * Calculates the LUP-decomposition of the given matrix. - * - * @param matrix Matrix to decompose. - * @param singularityThreshold threshold (based on partial row norm). - * @throws CardinalityException if matrix is not square. - */ - public LUDecomposition(Matrix matrix, double singularityThreshold) { - assert matrix != null; - - int rows = matrix.rowSize(); - int cols = matrix.columnSize(); - - if (rows != cols) - throw new CardinalityException(rows, cols); - - this.matrix = matrix; - - lu = copy(matrix); - - pivot = likeVector(matrix); - - for (int i = 0; i < pivot.size(); i++) - pivot.setX(i, i); - - even = true; - singular = false; - - cachedL = null; - cachedU = null; - cachedP = null; - - for (int col = 0; col < cols; col++) { - - //upper - for (int row = 0; row < col; row++) { - Vector luRow = lu.viewRow(row); - double sum = luRow.get(col); - - for (int i = 0; i < row; i++) - sum -= luRow.getX(i) * lu.getX(i, col); - - luRow.setX(col, sum); - } - - // permutation row - int max = col; - - double largest = Double.NEGATIVE_INFINITY; - - // lower - for (int row = col; row < rows; row++) { - Vector luRow = lu.viewRow(row); - double sum = luRow.getX(col); - - for (int i = 0; i < col; i++) - sum -= luRow.getX(i) * lu.getX(i, col); - - luRow.setX(col, sum); - - if (Math.abs(sum) > largest) { - largest = Math.abs(sum); - max = row; - } - } - - // Singularity check - if (Math.abs(lu.getX(max, col)) < singularityThreshold) { - singular = true; - return; - } - - // Pivot if necessary - if (max != col) { - double tmp; - Vector luMax = lu.viewRow(max); - Vector luCol = lu.viewRow(col); - - for (int i = 0; i < cols; i++) { - tmp = luMax.getX(i); - luMax.setX(i, luCol.getX(i)); - luCol.setX(i, tmp); - } - - int temp = (int)pivot.getX(max); - pivot.setX(max, pivot.getX(col)); - pivot.setX(col, temp); - - even = !even; - } - - // Divide the lower elements by the "winning" diagonal elt. - final double luDiag = lu.getX(col, col); - - for (int row = col + 1; row < cols; row++) { - double val = lu.getX(row, col) / luDiag; - lu.setX(row, col, val); - } - } - } - - /** - * Destroys decomposition components and other internal components of decomposition. - */ - @Override public void destroy() { - if (cachedL != null) - cachedL.destroy(); - if (cachedU != null) - cachedU.destroy(); - if (cachedP != null) - cachedP.destroy(); - lu.destroy(); - } - - /** - * Returns the matrix L of the decomposition. - *

L is a lower-triangular matrix

- * - * @return the L matrix (or null if decomposed matrix is singular). - */ - public Matrix getL() { - if ((cachedL == null) && !singular) { - final int m = pivot.size(); - - cachedL = like(matrix); - cachedL.assign(0.0); - - for (int i = 0; i < m; ++i) { - for (int j = 0; j < i; ++j) - cachedL.setX(i, j, lu.getX(i, j)); - - cachedL.setX(i, i, 1.0); - } - } - - return cachedL; - } - - /** - * Returns the matrix U of the decomposition. - *

U is an upper-triangular matrix

- * - * @return the U matrix (or null if decomposed matrix is singular). - */ - public Matrix getU() { - if ((cachedU == null) && !singular) { - final int m = pivot.size(); - - cachedU = like(matrix); - cachedU.assign(0.0); - - for (int i = 0; i < m; ++i) - for (int j = i; j < m; ++j) - cachedU.setX(i, j, lu.getX(i, j)); - } - - return cachedU; - } - - /** - * Returns the P rows permutation matrix. - *

P is a sparse matrix with exactly one element set to 1.0 in - * each row and each column, all other elements being set to 0.0.

- *

The positions of the 1 elements are given by the {@link #getPivot() - * pivot permutation vector}.

- * - * @return the P rows permutation matrix (or null if decomposed matrix is singular). - * @see #getPivot() - */ - public Matrix getP() { - if ((cachedP == null) && !singular) { - final int m = pivot.size(); - - cachedP = like(matrix); - cachedP.assign(0.0); - - for (int i = 0; i < m; ++i) - cachedP.setX(i, (int)pivot.get(i), 1.0); - } - - return cachedP; - } - - /** - * Returns the pivot permutation vector. - * - * @return the pivot permutation vector. - * @see #getP() - */ - public Vector getPivot() { - return pivot.copy(); - } - - /** - * Return the determinant of the matrix. - * - * @return determinant of the matrix. - */ - public double determinant() { - if (singular) - return 0; - - final int m = pivot.size(); - double determinant = even ? 1 : -1; - - for (int i = 0; i < m; i++) - determinant *= lu.getX(i, i); - - return determinant; - } - - /** - * @param b Vector to solve using this decomposition. - * @return Solution vector. - */ - public Vector solve(Vector b) { - final int m = pivot.size(); - - if (b.size() != m) - throw new CardinalityException(b.size(), m); - - if (singular) - throw new SingularMatrixException(); - - final double[] bp = new double[m]; - - // Apply permutations to b - for (int row = 0; row < m; row++) - bp[row] = b.get((int)pivot.get(row)); - - // Solve LY = b - for (int col = 0; col < m; col++) { - final double bpCol = bp[col]; - - for (int i = col + 1; i < m; i++) - bp[i] -= bpCol * lu.get(i, col); - } - - // Solve UX = Y - for (int col = m - 1; col >= 0; col--) { - bp[col] /= lu.get(col, col); - final double bpCol = bp[col]; - - for (int i = 0; i < col; i++) - bp[i] -= bpCol * lu.get(i, col); - } - - return b.like(m).assign(bp); - } - - /** - * @param b Matrix to solve using this decomposition. - * @return Solution matrix. - */ - public Matrix solve(Matrix b) { - final int m = pivot.size(); - - if (b.rowSize() != m) - throw new CardinalityException(b.rowSize(), m); - - if (singular) - throw new SingularMatrixException(); - - final int nColB = b.columnSize(); - - // Apply permutations to b - final double[][] bp = new double[m][nColB]; - for (int row = 0; row < m; row++) { - final double[] bpRow = bp[row]; - final int pRow = (int)pivot.get(row); - - for (int col = 0; col < nColB; col++) - bpRow[col] = b.get(pRow, col); - } - - // Solve LY = b - for (int col = 0; col < m; col++) { - final double[] bpCol = bp[col]; - for (int i = col + 1; i < m; i++) { - final double[] bpI = bp[i]; - final double luICol = lu.get(i, col); - - for (int j = 0; j < nColB; j++) - bpI[j] -= bpCol[j] * luICol; - } - } - - // Solve UX = Y - for (int col = m - 1; col >= 0; col--) { - final double[] bpCol = bp[col]; - final double luDiag = lu.getX(col, col); - - for (int j = 0; j < nColB; j++) - bpCol[j] /= luDiag; - - for (int i = 0; i < col; i++) { - final double[] bpI = bp[i]; - final double luICol = lu.get(i, col); - - for (int j = 0; j < nColB; j++) - bpI[j] -= bpCol[j] * luICol; - } - } - - return b.like(b.rowSize(), b.columnSize()).assign(bp); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDSolver.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDSolver.java deleted file mode 100644 index bb591ee7261d3..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDSolver.java +++ /dev/null @@ -1,197 +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.decompositions; - -import java.io.Serializable; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.NoDataException; -import org.apache.ignite.ml.math.exceptions.NullArgumentException; -import org.apache.ignite.ml.math.exceptions.SingularMatrixException; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.util.MatrixUtil; - -import static org.apache.ignite.ml.math.util.MatrixUtil.like; - -/** - * For an {@code m x n} matrix {@code A} with {@code m >= n}, the QR decomposition - * is an {@code m x n} orthogonal matrix {@code Q} and an {@code n x n} upper - * triangular matrix {@code R} so that {@code A = Q*R}. - */ -public class QRDSolver implements Serializable { - /** */ - private final Matrix q; - - /** */ - private final Matrix r; - - /** - * Constructs a new QR decomposition solver object. - * - * @param q An orthogonal matrix. - * @param r An upper triangular matrix - */ - public QRDSolver(Matrix q, Matrix r) { - this.q = q; - this.r = r; - } - - /** - * Least squares solution of {@code A*X = B}; {@code returns X}. - * - * @param mtx A matrix with as many rows as {@code A} and any number of cols. - * @return {@code X<} that minimizes the two norm of {@code Q*R*X - B}. - * @throws IllegalArgumentException if {@code B.rows() != A.rows()}. - */ - public Matrix solve(Matrix mtx) { - if (mtx.rowSize() != q.rowSize()) - throw new IllegalArgumentException("Matrix row dimensions must agree."); - - int cols = mtx.columnSize(); - Matrix x = like(r, r.columnSize(), cols); - - Matrix qt = q.transpose(); - Matrix y = qt.times(mtx); - - for (int k = Math.min(r.columnSize(), q.rowSize()) - 1; k >= 0; k--) { - // X[k,] = Y[k,] / R[k,k], note that X[k,] starts with 0 so += is same as = - x.viewRow(k).map(y.viewRow(k), Functions.plusMult(1 / r.get(k, k))); - - if (k == 0) - continue; - - // Y[0:(k-1),] -= R[0:(k-1),k] * X[k,] - Vector rCol = r.viewColumn(k).viewPart(0, k); - - for (int c = 0; c < cols; c++) - y.viewColumn(c).viewPart(0, k).map(rCol, Functions.plusMult(-x.get(k, c))); - } - - return x; - } - - /** - * Least squares solution of {@code A*X = B}; {@code returns X}. - * - * @param vec A vector with as many rows as {@code A}. - * @return {@code X<} that minimizes the two norm of {@code Q*R*X - B}. - * @throws IllegalArgumentException if {@code B.rows() != A.rows()}. - */ - public Vector solve(Vector vec) { - if (vec == null) - throw new NullArgumentException(); - if (vec.size() == 0) - throw new NoDataException(); - // TODO: IGNITE-5826, Should we copy here? - - Matrix res = solve(vec.likeMatrix(vec.size(), 1).assignColumn(0, vec)); - - return vec.like(res.rowSize()).assign(res.viewColumn(0)); - } - - /** - *

Compute the "hat" matrix. - *

- *

The hat matrix is defined in terms of the design matrix X - * by X(XTX)-1XT - *

- *

The implementation here uses the QR decomposition to compute the - * hat matrix as Q IpQT where Ip is the - * p-dimensional identity matrix augmented by 0's. This computational - * formula is from "The Hat Matrix in Regression and ANOVA", - * David C. Hoaglin and Roy E. Welsch, - * The American Statistician, Vol. 32, No. 1 (Feb., 1978), pp. 17-22. - *

- *

Data for the model must have been successfully loaded using one of - * the {@code newSampleData} methods before invoking this method; otherwise - * a {@code NullPointerException} will be thrown.

- * - * @return the hat matrix - * @throws NullPointerException unless method {@code newSampleData} has been called beforehand. - */ - public Matrix calculateHat() { - // Create augmented identity matrix - // No try-catch or advertised NotStrictlyPositiveException - NPE above if n < 3 - Matrix augI = MatrixUtil.like(q, q.columnSize(), q.columnSize()); - - int n = augI.columnSize(); - int p = r.columnSize(); - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - if (i == j && i < p) - augI.setX(i, j, 1d); - else - augI.setX(i, j, 0d); - - // Compute and return Hat matrix - // No DME advertised - args valid if we get here - return q.times(augI).times(q.transpose()); - } - - /** - *

Calculates the variance-covariance matrix of the regression parameters. - *

- *

Var(b) = (XTX)-1 - *

- *

Uses QR decomposition to reduce (XTX)-1 - * to (RTR)-1, with only the top p rows of - * R included, where p = the length of the beta vector.

- * - *

Data for the model must have been successfully loaded using one of - * the {@code newSampleData} methods before invoking this method; otherwise - * a {@code NullPointerException} will be thrown.

- * - * @param p Size of the beta variance-covariance matrix - * @return The beta variance-covariance matrix - * @throws SingularMatrixException if the design matrix is singular - * @throws NullPointerException if the data for the model have not been loaded - */ - public Matrix calculateBetaVariance(int p) { - Matrix rAug = MatrixUtil.copy(r.viewPart(0, p, 0, p)); - Matrix rInv = rAug.inverse(); - - return rInv.times(rInv.transpose()); - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - - QRDSolver solver = (QRDSolver)o; - - return q.equals(solver.q) && r.equals(solver.r); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = q.hashCode(); - res = 31 * res + r.hashCode(); - return res; - } - - /** - * Returns a rough string rendition of a QRD solver. - */ - @Override public String toString() { - return String.format("QRD Solver(%d x %d)", q.rowSize(), r.columnSize()); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDecomposition.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDecomposition.java deleted file mode 100644 index c0696831ef513..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/QRDecomposition.java +++ /dev/null @@ -1,212 +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.decompositions; - -import org.apache.ignite.ml.math.Destroyable; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.SingularMatrixException; -import org.apache.ignite.ml.math.functions.Functions; - -import static org.apache.ignite.ml.math.util.MatrixUtil.copy; -import static org.apache.ignite.ml.math.util.MatrixUtil.like; - -/** - * For an {@code m x n} matrix {@code A} with {@code m >= n}, the QR decomposition - * is an {@code m x n} orthogonal matrix {@code Q} and an {@code n x n} upper - * triangular matrix {@code R} so that {@code A = Q*R}. - */ -public class QRDecomposition implements Destroyable { - /** */ - private final Matrix q; - /** */ - private final Matrix r; - - /** */ - private final Matrix mType; - /** */ - private final boolean fullRank; - - /** */ - private final int rows; - /** */ - private final int cols; - - /** - * @param v Value to be checked for being an ordinary double. - */ - private void checkDouble(double v) { - if (Double.isInfinite(v) || Double.isNaN(v)) - throw new ArithmeticException("Invalid intermediate result"); - } - - /** - * Constructs a new QR decomposition object computed by Householder reflections. - * Threshold for singularity check used in this case is 0. - * - * @param mtx A rectangular matrix. - */ - public QRDecomposition(Matrix mtx) { - this(mtx, 0.0); - } - - /** - * Constructs a new QR decomposition object computed by Householder reflections. - * - * @param mtx A rectangular matrix. - * @param threshold Value used for detecting singularity of {@code R} matrix in decomposition. - */ - public QRDecomposition(Matrix mtx, double threshold) { - assert mtx != null; - - rows = mtx.rowSize(); - - int min = Math.min(mtx.rowSize(), mtx.columnSize()); - - cols = mtx.columnSize(); - - mType = like(mtx, 1, 1); - - Matrix qTmp = copy(mtx); - - boolean fullRank = true; - - r = like(mtx, min, cols); - - for (int i = 0; i < min; i++) { - Vector qi = qTmp.viewColumn(i); - - double alpha = qi.kNorm(2); - - if (Math.abs(alpha) > Double.MIN_VALUE) - qi.map(Functions.div(alpha)); - else { - checkDouble(alpha); - - fullRank = false; - } - - r.set(i, i, alpha); - - for (int j = i + 1; j < cols; j++) { - Vector qj = qTmp.viewColumn(j); - - double norm = qj.kNorm(2); - - if (Math.abs(norm) > Double.MIN_VALUE) { - double beta = qi.dot(qj); - - r.set(i, j, beta); - - if (j < min) - qj.map(qi, Functions.plusMult(-beta)); - } - else - checkDouble(norm); - } - } - - if (cols > min) - q = qTmp.viewPart(0, rows, 0, min).copy(); - else - q = qTmp; - - verifyNonSingularR(threshold); - - this.fullRank = fullRank; - } - - /** {@inheritDoc} */ - @Override public void destroy() { - q.destroy(); - r.destroy(); - mType.destroy(); - } - - /** - * Gets orthogonal factor {@code Q}. - */ - public Matrix getQ() { - return q; - } - - /** - * Gets triangular factor {@code R}. - */ - public Matrix getR() { - return r; - } - - /** - * Returns whether the matrix {@code A} has full rank. - * - * @return true if {@code R}, and hence {@code A} , has full rank. - */ - public boolean hasFullRank() { - return fullRank; - } - - /** - * Least squares solution of {@code A*X = B}; {@code returns X}. - * - * @param mtx A matrix with as many rows as {@code A} and any number of cols. - * @return {@code X<} that minimizes the two norm of {@code Q*R*X - B}. - * @throws IllegalArgumentException if {@code B.rows() != A.rows()}. - */ - public Matrix solve(Matrix mtx) { - return new QRDSolver(q, r).solve(mtx); - } - - /** - * Least squares solution of {@code A*X = B}; {@code returns X}. - * - * @param vec A vector with as many rows as {@code A}. - * @return {@code X<} that minimizes the two norm of {@code Q*R*X - B}. - * @throws IllegalArgumentException if {@code B.rows() != A.rows()}. - */ - public Vector solve(Vector vec) { - return new QRDSolver(q, r).solve(vec); - } - - /** - * Returns a rough string rendition of a QR. - */ - @Override public String toString() { - return String.format("QR(%d x %d, fullRank=%s)", rows, cols, hasFullRank()); - } - - /** - * Check singularity. - * - * @param min Singularity threshold. - * @throws SingularMatrixException if the matrix is singular and {@code raise} is {@code true}. - */ - private void verifyNonSingularR(double min) { - // TODO: IGNITE-5828, Not a very fast approach for distributed matrices. would be nice if we could independently - // check parts on different nodes for singularity and do fold with 'or'. - - final int len = r.columnSize() > r.rowSize() ? r.rowSize() : r.columnSize(); - for (int i = 0; i < len; i++) { - final double d = r.getX(i, i); - if (Math.abs(d) <= min) - throw new SingularMatrixException("Number is too small (%f, while " + - "threshold is %f). Index of diagonal element is (%d, %d)", d, min, i, i); - - } - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/SingularValueDecomposition.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/SingularValueDecomposition.java deleted file mode 100644 index 68aeb6d7eae4f..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/SingularValueDecomposition.java +++ /dev/null @@ -1,623 +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.decompositions; - -import org.apache.ignite.ml.math.Algebra; -import org.apache.ignite.ml.math.Destroyable; -import org.apache.ignite.ml.math.Matrix; - -import static org.apache.ignite.ml.math.util.MatrixUtil.like; - -/** - * Compute a singular value decomposition (SVD) of {@code (l x k)} matrix {@code m}. - *

This decomposition can be thought - * as an extension of {@link EigenDecomposition} to rectangular matrices. The factorization we get is following:

- *

{@code m = u * s * v^{*}}, where

- *
  • {@code u} is a real or complex unitary matrix.
  • - *
  • {@code s} is a rectangular diagonal matrix with non-negative real numbers on diagonal - * (these numbers are singular values of {@code m}).
  • - *
  • {@code v} is a real or complex unitary matrix.
- *

If {@code m} is real then {@code u} and {@code v} are also real.

- *

See also: Wikipedia article on SVD.

- *

Note: complex case is currently not supported.

- */ -public class SingularValueDecomposition implements Destroyable { - // U and V. - /** */ - private final double[][] u; - /** */ - private final double[][] v; - - /** Singular values. */ - private final double[] s; - - /** Row dimension. */ - private final int m; - /** Column dimension. */ - private final int n; - - /** */ - private Matrix arg; - - /** */ - private boolean transpositionNeeded; - - /** - * Singular value decomposition object. - * - * @param arg A rectangular matrix. - */ - public SingularValueDecomposition(Matrix arg) { - assert arg != null; - - this.arg = arg; - - if (arg.rowSize() < arg.columnSize()) - transpositionNeeded = true; - - double[][] a; - - if (transpositionNeeded) { - // Use the transpose matrix. - m = arg.columnSize(); - n = arg.rowSize(); - - a = new double[m][n]; - - for (int i = 0; i < m; i++) - for (int j = 0; j < n; j++) - a[i][j] = arg.get(j, i); - } - else { - m = arg.rowSize(); - n = arg.columnSize(); - - a = new double[m][n]; - - for (int i = 0; i < m; i++) - for (int j = 0; j < n; j++) - a[i][j] = arg.get(i, j); - } - - int nu = Math.min(m, n); - - s = new double[Math.min(m + 1, n)]; - u = new double[m][nu]; - v = new double[n][n]; - - double[] e = new double[n]; - double[] work = new double[m]; - - int nct = Math.min(m - 1, n); - int nrt = Math.max(0, Math.min(n - 2, m)); - - for (int k = 0; k < Math.max(nct, nrt); k++) { - if (k < nct) { - // Compute the transformation for the k-th column and - // place the k-th diagonal in s[k]. Compute 2-norm of k-th - // column without under/overflow. - s[k] = 0; - - for (int i = k; i < m; i++) - s[k] = Algebra.hypot(s[k], a[i][k]); - - if (s[k] != 0.0) { - if (a[k][k] < 0.0) - s[k] = -s[k]; - - for (int i = k; i < m; i++) - a[i][k] /= s[k]; - - a[k][k] += 1.0; - } - - s[k] = -s[k]; - } - - for (int j = k + 1; j < n; j++) { - if (k < nct && s[k] != 0.0) { - // Apply the transformation. - double t = 0; - - for (int i = k; i < m; i++) - t += a[i][k] * a[i][j]; - - t = -t / a[k][k]; - - for (int i = k; i < m; i++) - a[i][j] += t * a[i][k]; - } - - // Place the k-th row of A into e for the - // subsequent calculation of the row transformation. - e[j] = a[k][j]; - } - - if (k < nct) - // Place the transformation in U for subsequent back - // multiplication. - for (int i = k; i < m; i++) - u[i][k] = a[i][k]; - - if (k < nrt) { - // Compute the k-th row transformation and place the - // k-th super-diagonal in e[k]. - // Compute 2-norm without under/overflow. - e[k] = 0; - - for (int i = k + 1; i < n; i++) - e[k] = Algebra.hypot(e[k], e[i]); - - if (e[k] != 0.0) { - if (e[k + 1] < 0.0) - e[k] = -e[k]; - - for (int i = k + 1; i < n; i++) - e[i] /= e[k]; - - e[k + 1] += 1.0; - } - - e[k] = -e[k]; - - if (k + 1 < m && e[k] != 0.0) { - // Apply the transformation. - for (int i = k + 1; i < m; i++) - work[i] = 0.0; - - for (int j = k + 1; j < n; j++) - for (int i = k + 1; i < m; i++) - work[i] += e[j] * a[i][j]; - - for (int j = k + 1; j < n; j++) { - double t = -e[j] / e[k + 1]; - - for (int i = k + 1; i < m; i++) - a[i][j] += t * work[i]; - } - } - - // Place the transformation in V for subsequent - // back multiplication. - for (int i = k + 1; i < n; i++) - v[i][k] = e[i]; - } - } - - // Set up the final bi-diagonal matrix or order p. - int p = Math.min(n, m + 1); - - if (nct < n) - s[nct] = a[nct][nct]; - - if (m < p) - s[p - 1] = 0.0; - - if (nrt + 1 < p) - e[nrt] = a[nrt][p - 1]; - - e[p - 1] = 0.0; - - // Generate U. - for (int j = nct; j < nu; j++) { - for (int i = 0; i < m; i++) - u[i][j] = 0.0; - - u[j][j] = 1.0; - } - - for (int k = nct - 1; k >= 0; k--) { - if (s[k] != 0.0) { - for (int j = k + 1; j < nu; j++) { - double t = 0; - - for (int i = k; i < m; i++) - t += u[i][k] * u[i][j]; - - t = -t / u[k][k]; - - for (int i = k; i < m; i++) - u[i][j] += t * u[i][k]; - } - - for (int i = k; i < m; i++) - u[i][k] = -u[i][k]; - - u[k][k] = 1.0 + u[k][k]; - - for (int i = 0; i < k - 1; i++) - u[i][k] = 0.0; - } - else { - for (int i = 0; i < m; i++) - u[i][k] = 0.0; - - u[k][k] = 1.0; - } - } - - // Generate V. - for (int k = n - 1; k >= 0; k--) { - if (k < nrt && e[k] != 0.0) { - for (int j = k + 1; j < nu; j++) { - double t = 0; - - for (int i = k + 1; i < n; i++) - t += v[i][k] * v[i][j]; - - t = -t / v[k + 1][k]; - - for (int i = k + 1; i < n; i++) - v[i][j] += t * v[i][k]; - } - } - - for (int i = 0; i < n; i++) - v[i][k] = 0.0; - - v[k][k] = 1.0; - } - - // Main iteration loop for the singular values. - int pp = p - 1; - int iter = 0; - - double eps = Math.pow(2.0, -52.0); - double tiny = Math.pow(2.0, -966.0); - - while (p > 0) { - int k; - - for (k = p - 2; k >= -1; k--) { - if (k == -1) - break; - - if (Math.abs(e[k]) <= tiny + eps * (Math.abs(s[k]) + Math.abs(s[k + 1]))) { - e[k] = 0.0; - - break; - } - } - - int kase; - - if (k == p - 2) - kase = 4; - else { - int ks; - - for (ks = p - 1; ks >= k; ks--) { - if (ks == k) - break; - - double t = - (ks != p ? Math.abs(e[ks]) : 0.) + - (ks != k + 1 ? Math.abs(e[ks - 1]) : 0.); - - if (Math.abs(s[ks]) <= tiny + eps * t) { - s[ks] = 0.0; - - break; - } - } - - if (ks == k) - kase = 3; - else if (ks == p - 1) - kase = 1; - else { - kase = 2; - - k = ks; - } - } - - k++; - - // Perform the task indicated by kase. - switch (kase) { - // Deflate negligible s(p). - case 1: { - double f = e[p - 2]; - - e[p - 2] = 0.0; - - for (int j = p - 2; j >= k; j--) { - double t = Algebra.hypot(s[j], f); - double cs = s[j] / t; - double sn = f / t; - - s[j] = t; - - if (j != k) { - f = -sn * e[j - 1]; - e[j - 1] = cs * e[j - 1]; - } - - for (int i = 0; i < n; i++) { - t = cs * v[i][j] + sn * v[i][p - 1]; - - v[i][p - 1] = -sn * v[i][j] + cs * v[i][p - 1]; - v[i][j] = t; - } - } - } - - break; - - // Split at negligible s(k). - case 2: { - double f = e[k - 1]; - e[k - 1] = 0.0; - - for (int j = k; j < p; j++) { - double t = Algebra.hypot(s[j], f); - double cs = s[j] / t; - double sn = f / t; - - s[j] = t; - f = -sn * e[j]; - e[j] = cs * e[j]; - - for (int i = 0; i < m; i++) { - t = cs * u[i][j] + sn * u[i][k - 1]; - - u[i][k - 1] = -sn * u[i][j] + cs * u[i][k - 1]; - u[i][j] = t; - } - } - } - - break; - - // Perform one qr step. - case 3: { - // Calculate the shift. - double scale = Math.max(Math.max(Math.max(Math.max( - Math.abs(s[p - 1]), Math.abs(s[p - 2])), Math.abs(e[p - 2])), - Math.abs(s[k])), Math.abs(e[k])); - - double sp = s[p - 1] / scale; - double spm1 = s[p - 2] / scale; - double epm1 = e[p - 2] / scale; - double sk = s[k] / scale; - double ek = e[k] / scale; - double b = ((spm1 + sp) * (spm1 - sp) + epm1 * epm1) / 2.0; - double c = sp * epm1 * sp * epm1; - double shift = 0.0; - - if (b != 0.0 || c != 0.0) { - shift = Math.sqrt(b * b + c); - - if (b < 0.0) - shift = -shift; - - shift = c / (b + shift); - } - - double f = (sk + sp) * (sk - sp) + shift; - double g = sk * ek; - - // Chase zeros. - for (int j = k; j < p - 1; j++) { - double t = Algebra.hypot(f, g); - double cs = f / t; - double sn = g / t; - - if (j != k) - e[j - 1] = t; - - f = cs * s[j] + sn * e[j]; - e[j] = cs * e[j] - sn * s[j]; - g = sn * s[j + 1]; - s[j + 1] = cs * s[j + 1]; - - for (int i = 0; i < n; i++) { - t = cs * v[i][j] + sn * v[i][j + 1]; - - v[i][j + 1] = -sn * v[i][j] + cs * v[i][j + 1]; - v[i][j] = t; - } - - t = Algebra.hypot(f, g); - cs = f / t; - sn = g / t; - s[j] = t; - f = cs * e[j] + sn * s[j + 1]; - s[j + 1] = -sn * e[j] + cs * s[j + 1]; - g = sn * e[j + 1]; - e[j + 1] = cs * e[j + 1]; - - if (j < m - 1) - for (int i = 0; i < m; i++) { - t = cs * u[i][j] + sn * u[i][j + 1]; - - u[i][j + 1] = -sn * u[i][j] + cs * u[i][j + 1]; - u[i][j] = t; - } - } - - e[p - 2] = f; - iter = iter + 1; - } - - break; - - // Convergence. - case 4: { - // Make the singular values positive. - if (s[k] <= 0.0) { - s[k] = s[k] < 0.0 ? -s[k] : 0.0; - - for (int i = 0; i <= pp; i++) - v[i][k] = -v[i][k]; - } - - // Order the singular values. - while (k < pp) { - if (s[k] >= s[k + 1]) - break; - - double t = s[k]; - - s[k] = s[k + 1]; - s[k + 1] = t; - - if (k < n - 1) - for (int i = 0; i < n; i++) { - t = v[i][k + 1]; - - v[i][k + 1] = v[i][k]; - v[i][k] = t; - } - - if (k < m - 1) - for (int i = 0; i < m; i++) { - t = u[i][k + 1]; - - u[i][k + 1] = u[i][k]; - u[i][k] = t; - } - - k++; - } - - iter = 0; - p--; - } - - break; - - default: - throw new IllegalStateException(); - } - } - } - - /** - * Gets the two norm condition number, which is {@code max(S) / min(S)} . - */ - public double cond() { - return s[0] / s[Math.min(m, n) - 1]; - } - - /** - * @return the diagonal matrix of singular values. - */ - public Matrix getS() { - double[][] s = new double[n][n]; - - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) - s[i][j] = 0.0; - - s[i][i] = this.s[i]; - } - - return like(arg, n, n).assign(s); - } - - /** - * Gets the diagonal of {@code S}, which is a one-dimensional array of - * singular values. - * - * @return diagonal of {@code S}. - */ - public double[] getSingularValues() { - return s; - } - - /** - * Gets the left singular vectors {@code U}. - * - * @return {@code U} - */ - public Matrix getU() { - if (transpositionNeeded) - return like(arg, v.length, v.length).assign(v); - else { - int numCols = Math.min(m + 1, n); - - Matrix r = like(arg, m, numCols); - - for (int i = 0; i < m; i++) - for (int j = 0; j < numCols; j++) - r.set(i, j, u[i][j]); - - return r; - } - } - - /** - * Gets the right singular vectors {@code V}. - * - * @return {@code V} - */ - public Matrix getV() { - if (transpositionNeeded) { - int numCols = Math.min(m + 1, n); - - Matrix r = like(arg, m, numCols); - - for (int i = 0; i < m; i++) - for (int j = 0; j < numCols; j++) - r.set(i, j, u[i][j]); - - return r; - } - else - return like(arg, v.length, v.length).assign(v); - } - - /** - * Gets the two norm, which is {@code max(S)}. - */ - public double norm2() { - return s[0]; - } - - /** - * Gets effective numerical matrix rank. - */ - public int rank() { - double eps = Math.pow(2.0, -52.0); - double tol = Math.max(m, n) * s[0] * eps; - int r = 0; - - for (double value : s) - if (value > tol) - r++; - - return r; - } - - /** - * Gets [n × n] covariance matrix. - * - * @param minSingularVal Value below which singular values are ignored. - */ - Matrix getCovariance(double minSingularVal) { - Matrix j = like(arg, s.length, s.length); - Matrix vMat = like(arg, v.length, v.length).assign(v); - - for (int i = 0; i < s.length; i++) - j.set(i, i, s[i] >= minSingularVal ? 1 / (s[i] * s[i]) : 0.0); - - return vMat.times(j).times(vMat.transpose()); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/package-info.java deleted file mode 100644 index d317ccd19f3dc..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/decompositions/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. - */ - -/** - * - * Contains matrix decompositions for distributed code algebra. - */ -package org.apache.ignite.ml.math.decompositions; diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/CacheUtils.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/CacheUtils.java deleted file mode 100644 index 3256f8a1174eb..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/CacheUtils.java +++ /dev/null @@ -1,734 +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.distributed; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.BinaryOperator; -import java.util.stream.Stream; -import javax.cache.Cache; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.affinity.Affinity; -import org.apache.ignite.cache.query.ScanQuery; -import org.apache.ignite.cluster.ClusterGroup; -import org.apache.ignite.cluster.ClusterNode; -import org.apache.ignite.internal.processors.cache.CacheEntryImpl; -import org.apache.ignite.internal.util.typedef.internal.A; -import org.apache.ignite.lang.IgniteBiTuple; -import org.apache.ignite.lang.IgniteCallable; -import org.apache.ignite.lang.IgnitePredicate; -import org.apache.ignite.lang.IgniteRunnable; -import org.apache.ignite.ml.math.KeyMapper; -import org.apache.ignite.ml.math.distributed.keys.DataStructureCacheKey; -import org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey; -import org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey; -import org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IgniteBiFunction; -import org.apache.ignite.ml.math.functions.IgniteBinaryOperator; -import org.apache.ignite.ml.math.functions.IgniteConsumer; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.functions.IgniteSupplier; -import org.apache.ignite.ml.math.functions.IgniteTriFunction; -import org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry; -import org.apache.ignite.ml.math.impls.vector.VectorBlockEntry; - -/** - * Distribution-related misc. support. - * - * TODO: IGNITE-5102, fix sparse key filters. - */ -public class CacheUtils { - /** - * Cache entry support. - * - * @param - * @param - */ - public static class CacheEntry { - /** */ - private Cache.Entry entry; - /** */ - private IgniteCache cache; - - /** - * @param entry Original cache entry. - * @param cache Cache instance. - */ - CacheEntry(Cache.Entry entry, IgniteCache cache) { - this.entry = entry; - this.cache = cache; - } - - /** - * - */ - public Cache.Entry entry() { - return entry; - } - - /** - * - */ - public IgniteCache cache() { - return cache; - } - } - - /** - * Gets local Ignite instance. - */ - public static Ignite ignite() { - return Ignition.localIgnite(); - } - - /** - * @param cacheName Cache name. - * @param k Key into the cache. - * @param Key type. - * @return Cluster group for given key. - */ - protected static ClusterGroup getClusterGroupForGivenKey(String cacheName, K k) { - return ignite().cluster().forNode(ignite().affinity(cacheName).mapKeyToNode(k)); - } - - /** - * @param cacheName Cache name. - * @param keyMapper {@link KeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain double value for given cache key. - * @param Cache key object type. - * @param Cache value object type. - * @return Sum of the values obtained for valid keys. - */ - public static double sum(String cacheName, KeyMapper keyMapper, ValueMapper valMapper) { - Collection subSums = fold(cacheName, (CacheEntry ce, Double acc) -> { - if (keyMapper.isValid(ce.entry().getKey())) { - double v = valMapper.toDouble(ce.entry().getValue()); - - return acc == null ? v : acc + v; - } - else - return acc; - }); - - return sum(subSums); - } - - /** - * @param matrixUuid Matrix UUID. - * @return Sum obtained using sparse logic. - */ - @SuppressWarnings("unchecked") - public static double sparseSum(UUID matrixUuid, String cacheName) { - A.notNull(matrixUuid, "matrixUuid"); - A.notNull(cacheName, "cacheName"); - - Collection subSums = fold(cacheName, (CacheEntry ce, Double acc) -> { - V v = ce.entry().getValue(); - - double sum; - - if (v instanceof Map) { - Map map = (Map)v; - - sum = sum(map.values()); - } - else if (v instanceof MatrixBlockEntry) { - MatrixBlockEntry be = (MatrixBlockEntry)v; - - sum = be.sum(); - } - else - throw new UnsupportedOperationException(); - - return acc == null ? sum : acc + sum; - }, sparseKeyFilter(matrixUuid)); - - return sum(subSums); - } - - /** - * @param c {@link Collection} of double values to sum. - * @return Sum of the values. - */ - private static double sum(Collection c) { - // Fix for IGNITE-6762, some collections could store null values. - return c.stream().filter(Objects::nonNull).mapToDouble(Double::doubleValue).sum(); - } - - /** - * @param cacheName Cache name. - * @param keyMapper {@link KeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain double value for given cache key. - * @param Cache key object type. - * @param Cache value object type. - * @return Minimum value for valid keys. - */ - public static double min(String cacheName, KeyMapper keyMapper, ValueMapper valMapper) { - Collection mins = fold(cacheName, (CacheEntry ce, Double acc) -> { - if (keyMapper.isValid(ce.entry().getKey())) { - double v = valMapper.toDouble(ce.entry().getValue()); - - if (acc == null) - return v; - else - return Math.min(acc, v); - } - else - return acc; - }); - - return Collections.min(mins); - } - - /** - * @param matrixUuid Matrix UUID. - * @return Minimum value obtained using sparse logic. - */ - @SuppressWarnings("unchecked") - public static double sparseMin(UUID matrixUuid, String cacheName) { - A.notNull(matrixUuid, "matrixUuid"); - A.notNull(cacheName, "cacheName"); - - Collection mins = fold(cacheName, (CacheEntry ce, Double acc) -> { - V v = ce.entry().getValue(); - - double min; - - if (v instanceof Map) { - Map map = (Map)v; - - min = Collections.min(map.values()); - } - else if (v instanceof MatrixBlockEntry) { - MatrixBlockEntry be = (MatrixBlockEntry)v; - - min = be.minValue(); - } - else - throw new UnsupportedOperationException(); - - if (acc == null) - return min; - else - return Math.min(acc, min); - - }, sparseKeyFilter(matrixUuid)); - - return Collections.min(mins); - } - - /** - * @param matrixUuid Matrix UUID. - * @return Maximum value obtained using sparse logic. - */ - @SuppressWarnings("unchecked") - public static double sparseMax(UUID matrixUuid, String cacheName) { - A.notNull(matrixUuid, "matrixUuid"); - A.notNull(cacheName, "cacheName"); - - Collection maxes = fold(cacheName, (CacheEntry ce, Double acc) -> { - V v = ce.entry().getValue(); - - double max; - - if (v instanceof Map) { - Map map = (Map)v; - - max = Collections.max(map.values()); - } - else if (v instanceof MatrixBlockEntry) { - MatrixBlockEntry be = (MatrixBlockEntry)v; - - max = be.maxValue(); - } - else - throw new UnsupportedOperationException(); - - if (acc == null) - return max; - else - return Math.max(acc, max); - - }, sparseKeyFilter(matrixUuid)); - - return Collections.max(maxes); - } - - /** - * @param cacheName Cache name. - * @param keyMapper {@link KeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain double value for given cache key. - * @param Cache key object type. - * @param Cache value object type. - * @return Maximum value for valid keys. - */ - public static double max(String cacheName, KeyMapper keyMapper, ValueMapper valMapper) { - Collection maxes = fold(cacheName, (CacheEntry ce, Double acc) -> { - if (keyMapper.isValid(ce.entry().getKey())) { - double v = valMapper.toDouble(ce.entry().getValue()); - - if (acc == null) - return v; - else - return Math.max(acc, v); - } - else - return acc; - }); - - return Collections.max(maxes); - } - - /** - * @param cacheName Cache name. - * @param keyMapper {@link KeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain double value for given cache key. - * @param mapper Mapping {@link IgniteFunction}. - * @param Cache key object type. - * @param Cache value object type. - */ - public static void map(String cacheName, KeyMapper keyMapper, ValueMapper valMapper, - IgniteFunction mapper) { - foreach(cacheName, (CacheEntry ce) -> { - K k = ce.entry().getKey(); - - if (keyMapper.isValid(k)) - // Actual assignment. - ce.cache().put(k, valMapper.fromDouble(mapper.apply(valMapper.toDouble(ce.entry().getValue())))); - }); - } - - /** - * @param matrixUuid Matrix UUID. - * @param mapper Mapping {@link IgniteFunction}. - */ - @SuppressWarnings("unchecked") - public static void sparseMap(UUID matrixUuid, IgniteDoubleFunction mapper, String cacheName) { - A.notNull(matrixUuid, "matrixUuid"); - A.notNull(cacheName, "cacheName"); - A.notNull(mapper, "mapper"); - - foreach(cacheName, (CacheEntry ce) -> { - K k = ce.entry().getKey(); - - V v = ce.entry().getValue(); - - if (v instanceof Map) { - Map map = (Map)v; - - for (Map.Entry e : (map.entrySet())) - e.setValue(mapper.apply(e.getValue())); - - } - else if (v instanceof MatrixBlockEntry) { - MatrixBlockEntry be = (MatrixBlockEntry)v; - - be.map(mapper); - } - else - throw new UnsupportedOperationException(); - - ce.cache().put(k, v); - }, sparseKeyFilter(matrixUuid)); - } - - /** - * Filter for distributed matrix keys. - * - * @param matrixUuid Matrix uuid. - */ - private static IgnitePredicate sparseKeyFilter(UUID matrixUuid) { - return key -> { - if (key instanceof DataStructureCacheKey) - return ((DataStructureCacheKey)key).dataStructureId().equals(matrixUuid); - else if (key instanceof IgniteBiTuple) - return ((IgniteBiTuple)key).get2().equals(matrixUuid); - else if (key instanceof MatrixBlockKey) - return ((MatrixBlockKey)key).dataStructureId().equals(matrixUuid); - else if (key instanceof RowColMatrixKey) - return ((RowColMatrixKey)key).dataStructureId().equals(matrixUuid); - else if (key instanceof VectorBlockKey) - return ((VectorBlockKey)key).dataStructureId().equals(matrixUuid); - else - throw new UnsupportedOperationException(); - }; - } - - /** - * @param cacheName Cache name. - * @param fun An operation that accepts a cache entry and processes it. - * @param Cache key object type. - * @param Cache value object type. - */ - private static void foreach(String cacheName, IgniteConsumer> fun) { - foreach(cacheName, fun, null); - } - - /** - * @param cacheName Cache name. - * @param fun An operation that accepts a cache entry and processes it. - * @param keyFilter Cache keys filter. - * @param Cache key object type. - * @param Cache value object type. - */ - protected static void foreach(String cacheName, IgniteConsumer> fun, - IgnitePredicate keyFilter) { - bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - IgniteCache cache = ignite.getOrCreateCache(cacheName); - - int partsCnt = ignite.affinity(cacheName).partitions(); - - // Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong. - Affinity affinity = ignite.affinity(cacheName); - ClusterNode locNode = ignite.cluster().localNode(); - - // Iterate over all partitions. Some of them will be stored on that local node. - for (int part = 0; part < partsCnt; part++) { - int p = part; - - // Iterate over given partition. - // Query returns an empty cursor if this partition is not stored on this node. - for (Cache.Entry entry : cache.query(new ScanQuery(part, - (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) - fun.accept(new CacheEntry<>(entry, cache)); - } - }); - } - - /** - * @param cacheName Cache name. - * @param fun An operation that accepts a cache entry and processes it. - * @param ignite Ignite. - * @param keysGen Keys generator. - * @param Cache key object type. - * @param Cache value object type. - */ - public static void update(String cacheName, Ignite ignite, - IgniteBiFunction, Stream>> fun, IgniteSupplier> keysGen) { - bcast(cacheName, ignite, () -> { - Ignite ig = Ignition.localIgnite(); - IgniteCache cache = ig.getOrCreateCache(cacheName); - - Affinity affinity = ig.affinity(cacheName); - ClusterNode locNode = ig.cluster().localNode(); - - Collection ks = affinity.mapKeysToNodes(keysGen.get()).get(locNode); - - if (ks == null) - return; - - Map m = new ConcurrentHashMap<>(); - - ks.parallelStream().forEach(k -> { - V v = cache.localPeek(k); - if (v != null) - (fun.apply(ignite, new CacheEntryImpl<>(k, v))).forEach(ent -> m.put(ent.getKey(), ent.getValue())); - }); - - cache.putAll(m); - }); - } - - /** - * @param cacheName Cache name. - * @param fun An operation that accepts a cache entry and processes it. - * @param ignite Ignite. - * @param keysGen Keys generator. - * @param Cache key object type. - * @param Cache value object type. - */ - public static void update(String cacheName, Ignite ignite, IgniteConsumer> fun, - IgniteSupplier> keysGen) { - bcast(cacheName, ignite, () -> { - Ignite ig = Ignition.localIgnite(); - IgniteCache cache = ig.getOrCreateCache(cacheName); - - Affinity affinity = ig.affinity(cacheName); - ClusterNode locNode = ig.cluster().localNode(); - - Collection ks = affinity.mapKeysToNodes(keysGen.get()).get(locNode); - - if (ks == null) - return; - - Map m = new ConcurrentHashMap<>(); - - for (K k : ks) { - V v = cache.localPeek(k); - fun.accept(new CacheEntryImpl<>(k, v)); - m.put(k, v); - } - - cache.putAll(m); - }); - } - - /** - * Currently fold supports only commutative operations. - * - * @param cacheName Cache name. - * @param folder Fold function operating over cache entries. - * @param Cache key object type. - * @param Cache value object type. - * @param Fold result type. - * @return Fold operation result. - */ - public static Collection fold(String cacheName, IgniteBiFunction, A, A> folder) { - return fold(cacheName, folder, null); - } - - /** - * Currently fold supports only commutative operations. - * - * @param cacheName Cache name. - * @param folder Fold function operating over cache entries. - * @param Cache key object type. - * @param Cache value object type. - * @param Fold result type. - * @return Fold operation result. - */ - public static Collection fold(String cacheName, IgniteBiFunction, A, A> folder, - IgnitePredicate keyFilter) { - return bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - IgniteCache cache = ignite.getOrCreateCache(cacheName); - - int partsCnt = ignite.affinity(cacheName).partitions(); - - // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong. - Affinity affinity = ignite.affinity(cacheName); - ClusterNode locNode = ignite.cluster().localNode(); - - A a = null; - - // Iterate over all partitions. Some of them will be stored on that local node. - for (int part = 0; part < partsCnt; part++) { - int p = part; - - // Iterate over given partition. - // Query returns an empty cursor if this partition is not stored on this node. - for (Cache.Entry entry : cache.query(new ScanQuery(part, - (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) - a = folder.apply(new CacheEntry<>(entry, cache), a); - } - - return a; - }); - } - - /** - * Distributed version of fold operation. - * - * @param cacheName Cache name. - * @param folder Folder. - * @param keyFilter Key filter. - * @param accumulator Accumulator. - * @param zeroValSupp Zero value supplier. - */ - public static A distributedFold(String cacheName, IgniteBiFunction, A, A> folder, - IgnitePredicate keyFilter, BinaryOperator accumulator, IgniteSupplier zeroValSupp) { - return sparseFold(cacheName, folder, keyFilter, accumulator, zeroValSupp, null, null, 0, - false); - } - - /** - * Sparse version of fold. This method also applicable to sparse zeroes. - * - * @param cacheName Cache name. - * @param folder Folder. - * @param keyFilter Key filter. - * @param accumulator Accumulator. - * @param zeroValSupp Zero value supplier. - * @param defVal Default value. - * @param defKey Default key. - * @param defValCnt Def value count. - * @param isNilpotent Is nilpotent. - */ - private static A sparseFold(String cacheName, IgniteBiFunction, A, A> folder, - IgnitePredicate keyFilter, BinaryOperator accumulator, IgniteSupplier zeroValSupp, V defVal, K defKey, - long defValCnt, boolean isNilpotent) { - - A defRes = zeroValSupp.get(); - - if (!isNilpotent) - for (int i = 0; i < defValCnt; i++) - defRes = folder.apply(new CacheEntryImpl<>(defKey, defVal), defRes); - - Collection totalRes = bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - IgniteCache cache = ignite.getOrCreateCache(cacheName); - - int partsCnt = ignite.affinity(cacheName).partitions(); - - // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong. - Affinity affinity = ignite.affinity(cacheName); - ClusterNode locNode = ignite.cluster().localNode(); - - A a = zeroValSupp.get(); - - // Iterate over all partitions. Some of them will be stored on that local node. - for (int part = 0; part < partsCnt; part++) { - int p = part; - - // Iterate over given partition. - // Query returns an empty cursor if this partition is not stored on this node. - for (Cache.Entry entry : cache.query(new ScanQuery(part, - (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) - a = folder.apply(entry, a); - } - - return a; - }); - return totalRes.stream().reduce(defRes, accumulator); - } - - /** - * Distributed version of fold operation. This method also applicable to sparse zeroes. - * - * @param cacheName Cache name. - * @param ignite ignite - * @param acc Accumulator - * @param supp supplier - * @param entriesGen entries generator - * @param comb combiner - * @param zeroValSupp Zero value supplier. - * @return aggregated result - */ - public static A reduce(String cacheName, Ignite ignite, - IgniteTriFunction, A, A> acc, - IgniteSupplier supp, - IgniteSupplier>> entriesGen, IgniteBinaryOperator comb, - IgniteSupplier zeroValSupp) { - - A defRes = zeroValSupp.get(); - - Collection totalRes = bcast(cacheName, ignite, () -> { - // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong. - A a = zeroValSupp.get(); - - W w = supp.get(); - - for (Cache.Entry kvEntry : entriesGen.get()) - a = acc.apply(w, kvEntry, a); - - return a; - }); - - return totalRes.stream().reduce(defRes, comb); - } - - /** - * Distributed version of fold operation. - * - * @param cacheName Cache name. - * @param acc Accumulator - * @param supp supplier - * @param entriesGen entries generator - * @param comb combiner - * @param zeroValSupp Zero value supplier - * @return aggregated result - */ - public static A reduce(String cacheName, - IgniteTriFunction, A, A> acc, - IgniteSupplier supp, - IgniteSupplier>> entriesGen, - IgniteBinaryOperator comb, - IgniteSupplier zeroValSupp) { - return reduce(cacheName, Ignition.localIgnite(), acc, supp, entriesGen, comb, zeroValSupp); - } - - /** - * @param cacheName Cache name. - * @param run {@link Runnable} to broadcast to cache nodes for given cache name. - */ - public static void bcast(String cacheName, Ignite ignite, IgniteRunnable run) { - ignite.compute(ignite.cluster().forDataNodes(cacheName)).broadcast(run); - } - - /** - * Broadcast runnable to data nodes of given cache. - * - * @param cacheName Cache name. - * @param run Runnable. - */ - public static void bcast(String cacheName, IgniteRunnable run) { - bcast(cacheName, ignite(), run); - } - - /** - * @param cacheName Cache name. - * @param call {@link IgniteCallable} to broadcast to cache nodes for given cache name. - * @param Type returned by the callable. - */ - public static Collection bcast(String cacheName, IgniteCallable call) { - return bcast(cacheName, ignite(), call); - } - - /** - * Broadcast callable to data nodes of given cache. - * - * @param cacheName Cache name. - * @param ignite Ignite instance. - * @param call Callable to broadcast. - * @param Type of callable result. - * @return Results of callable from each node. - */ - public static Collection bcast(String cacheName, Ignite ignite, IgniteCallable call) { - return ignite.compute(ignite.cluster().forDataNodes(cacheName)).broadcast(call); - } - - /** - * @param vectorUuid Matrix UUID. - * @param mapper Mapping {@link IgniteFunction}. - */ - @SuppressWarnings("unchecked") - public static void sparseMapForVector(UUID vectorUuid, IgniteDoubleFunction mapper, String cacheName) { - A.notNull(vectorUuid, "vectorUuid"); - A.notNull(cacheName, "cacheName"); - A.notNull(mapper, "mapper"); - - foreach(cacheName, (CacheEntry ce) -> { - K k = ce.entry().getKey(); - - V v = ce.entry().getValue(); - - if (v instanceof VectorBlockEntry) { - VectorBlockEntry entry = (VectorBlockEntry)v; - - for (int i = 0; i < entry.size(); i++) - entry.set(i, (Double)mapper.apply(entry.get(i))); - - ce.cache().put(k, (V)entry); - } - else { - V mappingRes = mapper.apply((Double)v); - ce.cache().put(k, mappingRes); - } - }, sparseKeyFilter(vectorUuid)); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/DistributedStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/DistributedStorage.java deleted file mode 100644 index 7b58d1d44112f..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/DistributedStorage.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.ml.math.distributed; - -import java.util.Set; - -/** - * Extension for any distributed storage. - */ -public interface DistributedStorage { - /** - * Build a keyset for this storage. - */ - public Set getAllKeys(); - - /** - * @return The name of cache used in this storage. - */ - public String cacheName(); -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/MatrixKeyMapper.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/MatrixKeyMapper.java deleted file mode 100644 index 2a93328009b8e..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/MatrixKeyMapper.java +++ /dev/null @@ -1,33 +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.distributed; - -import org.apache.ignite.ml.math.KeyMapper; -import org.apache.ignite.ml.math.Matrix; - -/** - * Maps {@link Matrix} row and column index to cache key. - */ -public interface MatrixKeyMapper extends KeyMapper { - /** - * @param x Matrix row index. - * @param y Matrix column index. - * @return Cache key for given row and column. - */ - public K apply(int x, int y); -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/ValueMapper.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/ValueMapper.java deleted file mode 100644 index c94cfb655ce93..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/ValueMapper.java +++ /dev/null @@ -1,37 +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.distributed; - -import java.io.Serializable; - -/** - * Utility mapper that can be used to map arbitrary values types to and from double. - */ -public interface ValueMapper extends Serializable { - /** - * @param v Value to map from double. - * @return Mapped value. - */ - public V fromDouble(double v); - - /** - * @param v Value to map to double. - * @return Mapped value. - */ - public double toDouble(V v); -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/VectorKeyMapper.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/VectorKeyMapper.java deleted file mode 100644 index de08d7b4f05e7..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/VectorKeyMapper.java +++ /dev/null @@ -1,32 +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.distributed; - -import org.apache.ignite.ml.math.KeyMapper; -import org.apache.ignite.ml.math.Vector; - -/** - * Maps {@link Vector} element index to cache key. - */ -public interface VectorKeyMapper extends KeyMapper { - /** - * @param i Vector element index. - * @return Cache key for given element index. - */ - public K apply(int i); -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/DataStructureCacheKey.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/DataStructureCacheKey.java deleted file mode 100644 index d99ea4881776a..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/DataStructureCacheKey.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.ml.math.distributed.keys; - -import java.util.UUID; - -/** - * Base matrix cache key. - */ -public interface DataStructureCacheKey { - /** - * @return matrix id. - */ - public UUID dataStructureId(); - - /** - * @return affinity key. - */ - public Object affinityKey(); -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/MatrixBlockKey.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/MatrixBlockKey.java deleted file mode 100644 index 9c7656871a94a..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/MatrixBlockKey.java +++ /dev/null @@ -1,38 +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.distributed.keys; - -import org.apache.ignite.internal.util.lang.IgnitePair; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; - -/** - * Cache key for blocks in {@link SparseBlockDistributedMatrix}. - * - * TODO: check if using {@link IgnitePair} will be better for block id. - */ -public interface MatrixBlockKey extends DataStructureCacheKey { - /** - * @return block row id. - */ - public long blockRowId(); - - /** - * @return block col id. - */ - public long blockColId(); -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/RowColMatrixKey.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/RowColMatrixKey.java deleted file mode 100644 index 05d8027e0b3ad..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/RowColMatrixKey.java +++ /dev/null @@ -1,30 +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.distributed.keys; - -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; - -/** - * Cache key for {@link SparseDistributedMatrix}. - */ -public interface RowColMatrixKey extends DataStructureCacheKey { - /** - * Return index value(blockId, Row/Col index, etc.) - */ - public int index(); -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/VectorBlockKey.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/VectorBlockKey.java deleted file mode 100644 index 32af965b8eba0..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/VectorBlockKey.java +++ /dev/null @@ -1,34 +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.distributed.keys; - -import org.apache.ignite.internal.util.lang.IgnitePair; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector; - -/** - * Cache key for blocks in {@link SparseBlockDistributedVector}. - * - * TODO: check if using {@link IgnitePair} will be better for block id. - */ -public interface VectorBlockKey extends DataStructureCacheKey { - /** - * @return block id. - */ - public long blockId(); - -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/MatrixBlockKey.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/MatrixBlockKey.java deleted file mode 100644 index 6e09499a37edd..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/MatrixBlockKey.java +++ /dev/null @@ -1,166 +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.distributed.keys.impl; - -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.UUID; -import org.apache.ignite.binary.BinaryObjectException; -import org.apache.ignite.binary.BinaryRawReader; -import org.apache.ignite.binary.BinaryRawWriter; -import org.apache.ignite.binary.BinaryReader; -import org.apache.ignite.binary.BinaryWriter; -import org.apache.ignite.binary.Binarylizable; -import org.apache.ignite.internal.util.typedef.F; -import org.apache.ignite.internal.util.typedef.internal.S; -import org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; -import org.jetbrains.annotations.Nullable; - -/** - * Key implementation for {@link MatrixBlockEntry} using for {@link SparseBlockDistributedMatrix}. - */ -public class MatrixBlockKey implements org.apache.ignite.ml.math.distributed.keys.MatrixBlockKey, - Externalizable, Binarylizable { - /** */ - private static final long serialVersionUID = 0L; - - /** Block row ID */ - private long blockIdRow; - - /** Block col ID */ - private long blockIdCol; - - /** Matrix ID */ - private UUID matrixUuid; - - /** Block affinity key. */ - private UUID affinityKey; - - /** - * Empty constructor required for {@link Externalizable}. - */ - public MatrixBlockKey() { - // No-op. - } - - /** - * Construct matrix block key. - * - * @param matrixUuid Matrix uuid. - * @param affinityKey Affinity key. - */ - public MatrixBlockKey(long rowId, long colId, UUID matrixUuid, @Nullable UUID affinityKey) { - assert rowId >= 0; - assert colId >= 0; - assert matrixUuid != null; - - this.blockIdRow = rowId; - this.blockIdCol = colId; - this.matrixUuid = matrixUuid; - this.affinityKey = affinityKey; - } - - /** {@inheritDoc} */ - @Override public long blockRowId() { - return blockIdRow; - } - - /** {@inheritDoc} */ - @Override public long blockColId() { - return blockIdCol; - } - - /** {@inheritDoc} */ - @Override public UUID dataStructureId() { - return matrixUuid; - } - - /** {@inheritDoc} */ - @Override public UUID affinityKey() { - return affinityKey; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(matrixUuid); - out.writeObject(affinityKey); - out.writeLong(blockIdRow); - out.writeLong(blockIdCol); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - matrixUuid = (UUID)in.readObject(); - affinityKey = (UUID)in.readObject(); - blockIdRow = in.readLong(); - blockIdCol = in.readLong(); - } - - /** {@inheritDoc} */ - @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException { - BinaryRawWriter out = writer.rawWriter(); - - out.writeUuid(matrixUuid); - out.writeUuid(affinityKey); - out.writeLong(blockIdRow); - out.writeLong(blockIdCol); - } - - /** {@inheritDoc} */ - @Override public void readBinary(BinaryReader reader) throws BinaryObjectException { - BinaryRawReader in = reader.rawReader(); - - matrixUuid = in.readUuid(); - affinityKey = in.readUuid(); - blockIdRow = in.readLong(); - blockIdCol = in.readLong(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 37; - - res += res * 37 + blockIdCol; - res += res * 37 + blockIdRow; - res += res * 37 + matrixUuid.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (obj == this) - return true; - - if (obj == null || obj.getClass() != getClass()) - return false; - - MatrixBlockKey that = (MatrixBlockKey)obj; - - return blockIdRow == that.blockIdRow && blockIdCol == that.blockIdCol && matrixUuid.equals(that.matrixUuid) - && F.eq(affinityKey, that.affinityKey); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(MatrixBlockKey.class, this); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/SparseMatrixKey.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/SparseMatrixKey.java deleted file mode 100644 index 3669d19048fa9..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/SparseMatrixKey.java +++ /dev/null @@ -1,119 +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.distributed.keys.impl; - -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.UUID; -import org.apache.ignite.cache.affinity.AffinityKeyMapped; -import org.apache.ignite.internal.util.typedef.F; -import org.apache.ignite.internal.util.typedef.internal.S; -import org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; - - -/** - * Key implementation for {@link SparseDistributedMatrix}. - */ -public class SparseMatrixKey implements RowColMatrixKey, Externalizable { - /** */ - private int idx; - - /** */ - private UUID matrixId; - - /** */ - @AffinityKeyMapped - private Object affinityKey; - - /** - * Default constructor (required by Externalizable). - */ - public SparseMatrixKey() { - - } - - /** - * Build Key. - */ - public SparseMatrixKey(int idx, UUID matrixId, Object affinityKey) { - assert idx >= 0 : "Index must be positive."; - assert matrixId != null : "Matrix id can`t be null."; - - this.idx = idx; - this.matrixId = matrixId; - this.affinityKey = affinityKey; - } - - /** {@inheritDoc} */ - @Override public int index() { - return idx; - } - - /** {@inheritDoc} */ - @Override public UUID dataStructureId() { - return matrixId; - } - - /** {@inheritDoc} */ - @Override public Object affinityKey() { - return affinityKey; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(matrixId); - out.writeObject(affinityKey); - out.writeInt(idx); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - matrixId = (UUID)in.readObject(); - affinityKey = in.readObject(); - idx = in.readInt(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = idx; - res = 31 * res + (matrixId != null ? matrixId.hashCode() : 0); - res = 31 * res + (affinityKey != null ? affinityKey.hashCode() : 0); - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (obj == this) - return true; - - if (obj == null || obj.getClass() != getClass()) - return false; - - SparseMatrixKey that = (SparseMatrixKey)obj; - - return idx == that.idx && matrixId.equals(that.matrixId) && F.eq(affinityKey, that.affinityKey); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(SparseMatrixKey.class, this); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/VectorBlockKey.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/VectorBlockKey.java deleted file mode 100644 index 718897fda6f3b..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/VectorBlockKey.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.distributed.keys.impl; - -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.UUID; -import org.apache.ignite.binary.BinaryObjectException; -import org.apache.ignite.binary.BinaryRawReader; -import org.apache.ignite.binary.BinaryRawWriter; -import org.apache.ignite.binary.BinaryReader; -import org.apache.ignite.binary.BinaryWriter; -import org.apache.ignite.binary.Binarylizable; -import org.apache.ignite.internal.util.typedef.F; -import org.apache.ignite.internal.util.typedef.internal.S; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector; -import org.apache.ignite.ml.math.impls.vector.VectorBlockEntry; -import org.jetbrains.annotations.Nullable; - -/** - * Key implementation for {@link VectorBlockEntry} using for {@link SparseBlockDistributedVector}. - */ -public class VectorBlockKey implements org.apache.ignite.ml.math.distributed.keys.VectorBlockKey, Externalizable, Binarylizable { - /** */ - private static final long serialVersionUID = 0L; - - /** Block row ID */ - private long blockId; - - /** Vector ID */ - private UUID vectorUuid; - - /** Block affinity key. */ - private UUID affinityKey; - - /** - * Empty constructor required for {@link Externalizable}. - */ - public VectorBlockKey() { - // No-op. - } - - /** - * Construct vector block key. - * - * @param vectorUuid Vector uuid. - * @param affinityKey Affinity key. - */ - public VectorBlockKey(long blockId, UUID vectorUuid, @Nullable UUID affinityKey) { - assert blockId >= 0; - assert vectorUuid != null; - - this.blockId = blockId; - this.vectorUuid = vectorUuid; - this.affinityKey = affinityKey; - } - - /** {@inheritDoc} */ - @Override public long blockId() { - return blockId; - } - - /** {@inheritDoc} */ - @Override public UUID dataStructureId() { - return vectorUuid; - } - - /** {@inheritDoc} */ - @Override public UUID affinityKey() { - return affinityKey; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(vectorUuid); - out.writeObject(affinityKey); - out.writeLong(blockId); - - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - vectorUuid = (UUID)in.readObject(); - affinityKey = (UUID)in.readObject(); - blockId = in.readLong(); - - } - - /** {@inheritDoc} */ - @Override public void writeBinary(BinaryWriter writer) throws BinaryObjectException { - BinaryRawWriter out = writer.rawWriter(); - - out.writeUuid(vectorUuid); - out.writeUuid(affinityKey); - out.writeLong(blockId); - } - - /** {@inheritDoc} */ - @Override public void readBinary(BinaryReader reader) throws BinaryObjectException { - BinaryRawReader in = reader.rawReader(); - - vectorUuid = in.readUuid(); - affinityKey = in.readUuid(); - blockId = in.readLong(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 37; - - res += res * 37 + blockId; - res += res * 37 + vectorUuid.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (obj == this) - return true; - - if (obj == null || obj.getClass() != getClass()) - return false; - - VectorBlockKey that = (VectorBlockKey)obj; - - return blockId == that.blockId && vectorUuid.equals(that.vectorUuid) - && F.eq(affinityKey, that.affinityKey); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return S.toString(VectorBlockKey.class, this); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/package-info.java deleted file mode 100644 index 3a68ee27df4d5..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/impl/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. - */ - -/** - * * - * Contains matrix cache key implementations. - */ -package org.apache.ignite.ml.math.distributed.keys.impl; \ No newline at end of file diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/package-info.java deleted file mode 100644 index 8954c6e0971b4..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/keys/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. - */ - -/** - * * - * Contains matrix cache keys. - */ -package org.apache.ignite.ml.math.distributed.keys; \ No newline at end of file diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/package-info.java deleted file mode 100644 index ad7399b302677..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/distributed/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. - */ - -/** - * - * Contains classes for distribution support. - */ -package org.apache.ignite.ml.math.distributed; \ No newline at end of file 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 index 5b6c988688280..06fb7a28ebaa6 100644 --- 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 @@ -31,7 +31,6 @@ 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.decompositions.LUDecomposition; import org.apache.ignite.ml.math.exceptions.CardinalityException; import org.apache.ignite.ml.math.exceptions.ColumnIndexException; import org.apache.ignite.ml.math.exceptions.RowIndexException; @@ -581,29 +580,6 @@ private void checkCardinality(int rows, int cols) { return sto.rowSize(); } - /** {@inheritDoc} */ - @Override public double determinant() { - //TODO: IGNITE-5799, This decomposition should be cached - LUDecomposition dec = new LUDecomposition(this); - double res = dec.determinant(); - dec.destroy(); - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix inverse() { - if (rowSize() != columnSize()) - throw new CardinalityException(rowSize(), columnSize()); - - //TODO: IGNITE-5799, This decomposition should be cached - LUDecomposition dec = new LUDecomposition(this); - - Matrix res = dec.solve(likeIdentity()); - dec.destroy(); - - return res; - } - /** */ protected Matrix likeIdentity() { int n = rowSize(); diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java deleted file mode 100644 index 1f832bc73e0d6..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrix.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.ml.math.impls.matrix; - -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.MatrixKeyMapper; -import org.apache.ignite.ml.math.distributed.ValueMapper; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.CacheMatrixStorage; - -/** - * Matrix based on existing cache and key and value mapping functions. - */ -public class CacheMatrix extends AbstractMatrix { - /** - * - */ - public CacheMatrix() { - // No-op. - } - - /** - * Creates new matrix over existing cache. - * - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - * @param cache Ignite cache. - * @param keyMapper {@link MatrixKeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain value for given cache key. - */ - public CacheMatrix( - int rows, - int cols, - IgniteCache cache, - MatrixKeyMapper keyMapper, - ValueMapper valMapper) { - assert rows > 0; - assert cols > 0; - assert cache != null; - assert keyMapper != null; - assert valMapper != null; - - setStorage(new CacheMatrixStorage<>(rows, cols, cache, keyMapper, valMapper)); - } - - /** - * - */ - @SuppressWarnings({"unchecked"}) - private CacheMatrixStorage storage() { - return (CacheMatrixStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Matrix divide(double d) { - return mapOverValues(v -> v / d); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Matrix plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply to. - */ - @Override public Matrix times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Matrix map(IgniteDoubleFunction fun) { - return mapOverValues(fun::apply); - } - - /** {@inheritDoc} */ - @Override public double sum() { - CacheMatrixStorage sto = storage(); - - return CacheUtils.sum(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - CacheMatrixStorage sto = storage(); - - return CacheUtils.max(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public double minValue() { - CacheMatrixStorage sto = storage(); - - return CacheUtils.min(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** - * @param mapper Mapping function. - * @return Matrix with mapped values. - */ - private Matrix mapOverValues(IgniteFunction mapper) { - CacheMatrixStorage sto = storage(); - - CacheUtils.map(sto.cache().getName(), sto.keyMapper(), sto.valueMapper(), mapper); - - return this; - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java deleted file mode 100644 index bd9a4a1643027..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrix.java +++ /dev/null @@ -1,101 +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.impls.storage.matrix.DiagonalMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.ConstantVector; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.impls.vector.SingleElementVectorView; - -/** - * Implementation of diagonal view of the {@link Matrix}. - * - *

See also: Wikipedia article.

- */ -public class DiagonalMatrix extends AbstractMatrix { - /** - * - */ - public DiagonalMatrix() { - // No-op. - } - - /** - * @param diagonal Backing {@link Vector}. - */ - public DiagonalMatrix(Vector diagonal) { - super(new DiagonalMatrixStorage(diagonal)); - } - - /** - * @param mtx Backing {@link Matrix}. - */ - public DiagonalMatrix(Matrix mtx) { - super(new DiagonalMatrixStorage(mtx == null ? null : mtx.viewDiagonal())); - } - - /** - * @param vals Backing array of values at diagonal. - */ - public DiagonalMatrix(double[] vals) { - super(new DiagonalMatrixStorage(new DenseLocalOnHeapVector(vals))); - } - - /** - * - * - */ - private DiagonalMatrixStorage storage() { - return (DiagonalMatrixStorage)getStorage(); - } - - /** - * @param size Size of diagonal. - * @param val Constant value at diagonal. - */ - public DiagonalMatrix(int size, double val) { - super(new DiagonalMatrixStorage(new ConstantVector(size, val))); - } - - /** {@inheritDoc} */ - @Override public Vector viewRow(int row) { - return new SingleElementVectorView(storage().diagonal(), row); - } - - /** {@inheritDoc} */ - @Override public Vector viewColumn(int col) { - return new SingleElementVectorView(storage().diagonal(), col); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new DiagonalMatrix(storage().diagonal()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return storage().diagonal().likeMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return storage().diagonal().like(crd); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java deleted file mode 100644 index 020d50a938149..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrix.java +++ /dev/null @@ -1,95 +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.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IntIntDoubleToVoidFunction; -import org.apache.ignite.ml.math.functions.IntIntToDoubleFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.FunctionMatrixStorage; - -/** - * Implementation of {@link Matrix} that maps row and column index to {@link java.util.function} interfaces. - */ -public class FunctionMatrix extends AbstractMatrix { - /** - * - */ - public FunctionMatrix() { - // No-op. - } - - /** - * Creates read-write or read-only function matrix. - * - * @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 FunctionMatrix(int rows, int cols, IntIntToDoubleFunction getFunc, IntIntDoubleToVoidFunction setFunc) { - assert rows > 0; - assert cols > 0; - assert getFunc != null; - - setStorage(new FunctionMatrixStorage(rows, cols, getFunc, setFunc)); - } - - /** - * Creates read-only function matrix. - * - * @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 FunctionMatrix(int rows, int cols, IntIntToDoubleFunction getFunc) { - assert rows > 0; - assert cols > 0; - assert getFunc != null; - - setStorage(new FunctionMatrixStorage(rows, cols, getFunc)); - } - - /** - * - * - */ - private FunctionMatrixStorage storage() { - return (FunctionMatrixStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - FunctionMatrixStorage sto = storage(); - - return new FunctionMatrix(sto.rowSize(), sto.columnSize(), sto.getFunction(), sto.setFunction()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - FunctionMatrixStorage sto = storage(); - - return new FunctionMatrix(rows, cols, sto.getFunction(), sto.setFunction()); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java deleted file mode 100644 index a2d13a12ca25c..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/MatrixBlockEntry.java +++ /dev/null @@ -1,50 +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; - -/** - * Block for {@link SparseBlockDistributedMatrix}. - */ -public final class MatrixBlockEntry extends SparseLocalOnHeapMatrix { - /** Max block size. */ - public static final int MAX_BLOCK_SIZE = 32; - - /** */ - public MatrixBlockEntry() { - // No-op. - } - - /** */ - public MatrixBlockEntry(int row, int col) { - super(row, col); - - assert col <= MAX_BLOCK_SIZE; - assert row <= MAX_BLOCK_SIZE; - } - - /** */ - public MatrixBlockEntry(Matrix mtx) { - assert mtx.columnSize() <= MAX_BLOCK_SIZE; - assert mtx.rowSize() <= MAX_BLOCK_SIZE; - - setStorage(mtx.getStorage()); - } - -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java deleted file mode 100644 index 361bee5640476..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixView.java +++ /dev/null @@ -1,241 +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 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.IndexException; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.storage.matrix.PivotedMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.PivotedVectorView; - -/** - * Pivoted (index mapped) view over another matrix implementation. - */ -public class PivotedMatrixView extends AbstractMatrix { - /** Pivoted matrix. */ - private Matrix mtx; - - /** - * - */ - public PivotedMatrixView() { - // No-op. - } - - /** - * @param mtx Parent matrix. - * @param rowPivot Pivot array for rows. - * @param colPivot Pivot array for columns. - */ - public PivotedMatrixView(Matrix mtx, int[] rowPivot, int[] colPivot) { - super(new PivotedMatrixStorage(mtx == null ? null : mtx.getStorage(), rowPivot, colPivot)); - - this.mtx = mtx; - } - - /** - * @param mtx Parent matrix. - */ - public PivotedMatrixView(Matrix mtx) { - super(new PivotedMatrixStorage(mtx == null ? null : mtx.getStorage())); - - this.mtx = mtx; - } - - /** - * @param mtx Parent matrix. - * @param pivot Pivot array for rows and columns. - */ - public PivotedMatrixView(Matrix mtx, int[] pivot) { - super(new PivotedMatrixStorage(mtx == null ? null : mtx.getStorage(), pivot)); - - this.mtx = mtx; - } - - /** - * Swaps indexes {@code i} and {@code j} for both both row and column. - * - * @param i First index to swap. - * @param j Second index to swap. - */ - public Matrix swap(int i, int j) { - swapRows(i, j); - swapColumns(i, j); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix swapRows(int i, int j) { - if (i < 0 || i >= storage().rowPivot().length) - throw new IndexException(i); - if (j < 0 || j >= storage().rowPivot().length) - throw new IndexException(j); - - storage().swapRows(i, j); - - return this; - } - - /** {@inheritDoc} */ - @Override public Matrix swapColumns(int i, int j) { - if (i < 0 || i >= storage().columnPivot().length) - throw new IndexException(i); - if (j < 0 || j >= storage().columnPivot().length) - throw new IndexException(j); - - storage().swapColumns(i, j); - - return this; - } - - /** {@inheritDoc} */ - @Override public Vector viewRow(int row) { - return new PivotedVectorView( - mtx.viewRow(storage().rowPivot()[row]), - storage().columnPivot(), - storage().columnUnpivot() - ); - } - - /** {@inheritDoc} */ - @Override public Vector viewColumn(int col) { - return new PivotedVectorView( - mtx.viewColumn(storage().columnPivot()[col]), - storage().rowPivot(), - storage().rowUnpivot() - ); - } - - /** - * @return Parent matrix. - */ - public Matrix getBaseMatrix() { - return mtx; - } - - /** - * @return Pivot array for rows. - */ - public int[] rowPivot() { - return storage().rowPivot(); - } - - /** - * @return Pivot array for columns. - */ - public int[] columnPivot() { - return storage().columnPivot(); - } - - /** - * @param i Index for row pivot. - * @return Row pivot for given index. - */ - public int rowPivot(int i) { - return storage().rowPivot()[i]; - } - - /** - * @param i Index for column pivot. - * @return Column pivot for given index. - */ - public int columnPivot(int i) { - return storage().columnPivot()[i]; - } - - /** - * @param i Index for row unpivot. - * @return Row unpivot for given index. - */ - public int rowUnpivot(int i) { - return storage().rowUnpivot()[i]; - } - - /** - * @param i Index for column unpivot. - * @return Column unpivot for given index. - */ - public int columnUnpivot(int i) { - return storage().columnUnpivot()[i]; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeObject(mtx); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - mtx = (Matrix)in.readObject(); - } - - /** */ - private PivotedMatrixStorage storage() { - return (PivotedMatrixStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new PivotedMatrixView(mtx, storage().rowPivot(), storage().columnPivot()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + mtx.hashCode(); - res = res * 37 + getStorage().hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - PivotedMatrixView that = (PivotedMatrixView)o; - - MatrixStorage sto = storage(); - - return mtx.equals(that.mtx) && sto.equals(that.storage()); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java deleted file mode 100644 index ece4ca964065e..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrix.java +++ /dev/null @@ -1,97 +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 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.impls.storage.matrix.RandomMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.RandomVector; - -/** - * Implementation of {@link Matrix} with random values in the elements. - */ -public class RandomMatrix extends AbstractMatrix { - /** Whether fast hash is used, see {@link RandomMatrixStorage}. */ - private boolean fastHash; - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param fastHash Whether fast hash is used. - */ - private MatrixStorage mkStorage(int rows, int cols, boolean fastHash) { - this.fastHash = fastHash; - - return new RandomMatrixStorage(rows, cols, fastHash); - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param fastHash Whether fast hash is used. - */ - public RandomMatrix(int rows, int cols, boolean fastHash) { - setStorage(mkStorage(rows, cols, fastHash)); - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public RandomMatrix(int rows, int cols) { - this(rows, cols, true); - } - - /** */ - public RandomMatrix() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new RandomMatrix(rowSize(), columnSize(), fastHash); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return new RandomMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new RandomVector(crd); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeBoolean(fastHash); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - fastHash = in.readBoolean(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java deleted file mode 100644 index d387d21a3e3f1..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseBlockDistributedMatrix.java +++ /dev/null @@ -1,314 +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.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.affinity.Affinity; -import org.apache.ignite.cluster.ClusterNode; -import org.apache.ignite.internal.util.lang.IgnitePair; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey; -import org.apache.ignite.ml.math.distributed.keys.impl.VectorBlockKey; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage; -import org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector; -import org.apache.ignite.ml.math.impls.vector.VectorBlockEntry; - -/** - * Sparse block distributed matrix. This matrix represented by blocks 32x32 {@link MatrixBlockEntry}. - * - * Using separate cache with keys {@link MatrixBlockKey} and values {@link MatrixBlockEntry}. - */ -public class SparseBlockDistributedMatrix extends AbstractMatrix implements StorageConstants { - /** - * - */ - public SparseBlockDistributedMatrix() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public SparseBlockDistributedMatrix(int rows, int cols) { - assert rows > 0; - assert cols > 0; - - setStorage(new BlockMatrixStorage(rows, cols)); - } - - /** - * @param data Data to fill the matrix - */ - public SparseBlockDistributedMatrix(double[][] data) { - assert data.length > 0; - - setStorage(new BlockMatrixStorage(data.length, getMaxAmountOfColumns(data))); - - for (int i = 0; i < data.length; i++) - for (int j = 0; j < data[i].length; j++) - storage().set(i, j, data[i][j]); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Matrix divide(double d) { - return mapOverValues(v -> v / d); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Matrix plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Matrix times(double x) { - return mapOverValues(v -> v * x); - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings({"unchecked"}) - @Override public Matrix times(final Matrix mtx) { - if (mtx == null) - throw new IllegalArgumentException("The matrix should be not null."); - - if (columnSize() != mtx.rowSize()) - throw new CardinalityException(columnSize(), mtx.rowSize()); - - SparseBlockDistributedMatrix matrixA = this; - SparseBlockDistributedMatrix matrixB = (SparseBlockDistributedMatrix)mtx; - - String cacheName = this.storage().cacheName(); - SparseBlockDistributedMatrix matrixC = new SparseBlockDistributedMatrix(matrixA.rowSize(), matrixB.columnSize()); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity affinity = ignite.affinity(cacheName); - - IgniteCache cache = ignite.getOrCreateCache(cacheName); - ClusterNode locNode = ignite.cluster().localNode(); - - BlockMatrixStorage storageC = matrixC.storage(); - - Map> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection locKeys = keysCToNodes.get(locNode); - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - long newBlockIdRow = key.blockRowId(); - long newBlockIdCol = key.blockColId(); - - IgnitePair newBlockId = new IgnitePair<>(newBlockIdRow, newBlockIdCol); - - MatrixBlockEntry blockC = null; - - List aRow = matrixA.storage().getRowForBlock(newBlockId); - List bCol = matrixB.storage().getColForBlock(newBlockId); - - for (int i = 0; i < aRow.size(); i++) { - MatrixBlockEntry blockA = aRow.get(i); - MatrixBlockEntry blockB = bCol.get(i); - - MatrixBlockEntry tmpBlock = new MatrixBlockEntry(blockA.times(blockB)); - - blockC = blockC == null ? tmpBlock : new MatrixBlockEntry(blockC.plus(tmpBlock)); - } - - cache.put(storageC.getCacheKey(newBlockIdRow, newBlockIdCol), blockC); - }); - }); - - return matrixC; - } - - /** - * {@inheritDoc} - */ - @SuppressWarnings({"unchecked"}) - @Override public Vector times(final Vector vec) { - if (vec == null) - throw new IllegalArgumentException("The vector should be not null."); - - if (columnSize() != vec.size()) - throw new CardinalityException(columnSize(), vec.size()); - - SparseBlockDistributedMatrix matrixA = this; - SparseBlockDistributedVector vectorB = (SparseBlockDistributedVector)vec; - - String cacheName = this.storage().cacheName(); - SparseBlockDistributedVector vectorC = new SparseBlockDistributedVector(matrixA.rowSize()); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity affinity = ignite.affinity(cacheName); - - IgniteCache cache = ignite.getOrCreateCache(cacheName); - ClusterNode locNode = ignite.cluster().localNode(); - - BlockVectorStorage storageC = vectorC.storage(); - - Map> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection locKeys = keysCToNodes.get(locNode); - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - long newBlockId = key.blockId(); - - IgnitePair newBlockIdForMtx = new IgnitePair<>(newBlockId, 0L); - - VectorBlockEntry blockC = null; - - List aRow = matrixA.storage().getRowForBlock(newBlockIdForMtx); - List bCol = vectorB.storage().getColForBlock(newBlockId); - - for (int i = 0; i < aRow.size(); i++) { - MatrixBlockEntry blockA = aRow.get(i); - VectorBlockEntry blockB = bCol.get(i); - - VectorBlockEntry tmpBlock = new VectorBlockEntry(blockA.times(blockB)); - - blockC = blockC == null ? tmpBlock : new VectorBlockEntry(blockC.plus(tmpBlock)); - } - - cache.put(storageC.getCacheKey(newBlockId), blockC); - }); - }); - return vectorC; - } - - /** {@inheritDoc} */ - @Override public Vector getCol(int col) { - checkColumnIndex(col); - - Vector res = new SparseBlockDistributedVector(rowSize()); - - for (int i = 0; i < rowSize(); i++) - res.setX(i, getX(i, col)); - return res; - } - - /** {@inheritDoc} */ - @Override public Vector getRow(int row) { - checkRowIndex(row); - - Vector res = new SparseBlockDistributedVector(columnSize()); - - for (int i = 0; i < columnSize(); i++) - res.setX(i, getX(row, i)); - return res; - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Matrix map(IgniteDoubleFunction fun) { - return mapOverValues(fun); - } - - /** {@inheritDoc} */ - @Override public double sum() { - return CacheUtils.sparseSum(getUUID(), this.storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - return CacheUtils.sparseMax(getUUID(), this.storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double minValue() { - return CacheUtils.sparseMin(getUUID(), this.storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - Matrix cp = like(rowSize(), columnSize()); - - cp.assign(this); - - return cp; - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - return new SparseBlockDistributedMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new SparseBlockDistributedVector(crd); - } - - /** */ - private UUID getUUID() { - return ((BlockMatrixStorage)getStorage()).getUUID(); - } - - /** - * @param mapper Mapping function. - * @return Matrix with mapped values. - */ - private Matrix mapOverValues(IgniteDoubleFunction mapper) { - CacheUtils.sparseMap(getUUID(), mapper, this.storage().cacheName()); - - return this; - } - - /** - * - */ - private BlockMatrixStorage storage() { - return (BlockMatrixStorage)getStorage(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java deleted file mode 100644 index 026138b1b8d71..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrix.java +++ /dev/null @@ -1,290 +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.util.Collection; -import java.util.Map; -import java.util.UUID; -import org.apache.ignite.Ignite; -import org.apache.ignite.Ignition; -import org.apache.ignite.cache.affinity.Affinity; -import org.apache.ignite.cluster.ClusterNode; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage; -import org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector; - -/** - * Sparse distributed matrix implementation based on data grid. - *

- * Unlike {@link CacheMatrix} that is based on existing cache, this implementation creates distributed - * cache internally and doesn't rely on pre-existing cache.

- *

- * You also need to call {@link #destroy()} to remove the underlying cache when you no longer need this - * matrix.

- *

- * This class is not intended for fast calculations (for example, matrix multiplication). If better performance - * is needed, {@link SparseBlockDistributedMatrix} should be used instead.

- *

- * Currently fold supports only commutative operations.

- */ -public class SparseDistributedMatrix extends AbstractMatrix implements StorageConstants { - /** - * - */ - public SparseDistributedMatrix() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - * @param stoMode Matrix storage mode. - * @param acsMode Matrix elements access mode. - */ - public SparseDistributedMatrix(int rows, int cols, int stoMode, int acsMode) { - assert rows > 0; - assert cols > 0; - assertAccessMode(acsMode); - assertStorageMode(stoMode); - - setStorage(new SparseDistributedMatrixStorage(rows, cols, stoMode, acsMode)); - - } - - /** - * @param data Data to fill the matrix - */ - public SparseDistributedMatrix(double[][] data) { - assert data.length > 0; - setStorage(new SparseDistributedMatrixStorage(data.length, getMaxAmountOfColumns(data), StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE)); - - for (int i = 0; i < data.length; i++) - for (int j = 0; j < data[i].length; j++) - storage().set(i, j, data[i][j]); - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public SparseDistributedMatrix(int rows, int cols) { - this(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** */ - private SparseDistributedMatrixStorage storage() { - return (SparseDistributedMatrixStorage)getStorage(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Matrix divide(double d) { - return mapOverValues(v -> v / d); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Matrix plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Matrix times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Matrix times(Matrix mtx) { - if (mtx == null) - throw new IllegalArgumentException("The matrix should be not null."); - - if (columnSize() != mtx.rowSize()) - throw new CardinalityException(columnSize(), mtx.rowSize()); - - SparseDistributedMatrix matrixA = this; - SparseDistributedMatrix matrixB = (SparseDistributedMatrix)mtx; - - String cacheName = storage().cacheName(); - SparseDistributedMatrix matrixC = new SparseDistributedMatrix(matrixA.rowSize(), matrixB.columnSize() - , getStorage().storageMode(), getStorage().isRandomAccess() ? RANDOM_ACCESS_MODE : SEQUENTIAL_ACCESS_MODE); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity affinity = ignite.affinity(cacheName); - - ClusterNode locNode = ignite.cluster().localNode(); - - SparseDistributedMatrixStorage storageC = matrixC.storage(); - - Map> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection locKeys = keysCToNodes.get(locNode); - - boolean isRowMode = storageC.storageMode() == ROW_STORAGE_MODE; - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - int idx = key.index(); - - if (isRowMode) { - Vector Aik = matrixA.getRow(idx); - - for (int i = 0; i < matrixB.columnSize(); i++) { - Vector Bkj = matrixB.getCol(i); - matrixC.set(idx, i, Aik.times(Bkj).sum()); - } - } - else { - Vector Bkj = matrixB.getCol(idx); - - for (int i = 0; i < matrixA.rowSize(); i++) { - Vector Aik = matrixA.getRow(i); - matrixC.set(idx, i, Aik.times(Bkj).sum()); - } - } - }); - }); - - return matrixC; - } - - /** {@inheritDoc} */ - @Override public Vector times(Vector vec) { - if (vec == null) - throw new IllegalArgumentException("The vector should be not null."); - - if (columnSize() != vec.size()) - throw new CardinalityException(columnSize(), vec.size()); - - SparseDistributedMatrix matrixA = this; - SparseDistributedVector vectorB = (SparseDistributedVector)vec; - - String cacheName = storage().cacheName(); - int rows = this.rowSize(); - - SparseDistributedVector vectorC = (SparseDistributedVector)likeVector(rows); - - CacheUtils.bcast(cacheName, () -> { - Ignite ignite = Ignition.localIgnite(); - Affinity affinity = ignite.affinity(cacheName); - - ClusterNode locNode = ignite.cluster().localNode(); - - SparseDistributedVectorStorage storageC = vectorC.storage(); - - Map> keysCToNodes = affinity.mapKeysToNodes(storageC.getAllKeys()); - Collection locKeys = keysCToNodes.get(locNode); - - if (locKeys == null) - return; - - // compute Cij locally on each node - // TODO: IGNITE:5114, exec in parallel - locKeys.forEach(key -> { - int idx = key.index(); - Vector Aik = matrixA.getRow(idx); - vectorC.set(idx, Aik.times(vectorB).sum()); - }); - }); - - return vectorC; - } - - /** {@inheritDoc} */ - @Override public Matrix assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Matrix map(IgniteDoubleFunction fun) { - return mapOverValues(fun); - } - - /** - * @param mapper Mapping function. - * @return Matrix with mapped values. - */ - private Matrix mapOverValues(IgniteDoubleFunction mapper) { - CacheUtils.sparseMap(getUUID(), mapper, storage().cacheName()); - - return this; - } - - /** {@inheritDoc} */ - @Override public double sum() { - return CacheUtils.sparseSum(getUUID(), storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - return CacheUtils.sparseMax(getUUID(), storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public double minValue() { - return CacheUtils.sparseMin(getUUID(), storage().cacheName()); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - Matrix cp = like(rowSize(), columnSize()); - - cp.assign(this); - - return cp; - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - if (storage() == null) - return new SparseDistributedMatrix(rows, cols); - else - return new SparseDistributedMatrix(rows, cols, storage().storageMode(), storage().accessMode()); - - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - return new SparseDistributedVector(crd, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** */ - public UUID getUUID() { - return ((SparseDistributedMatrixStorage)getStorage()).getUUID(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.java deleted file mode 100644 index 309570b75094a..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixView.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 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 transposed view of the parent {@link Matrix}. - */ -public class TransposedMatrixView extends AbstractMatrix { - /** */ - public TransposedMatrixView() { - //No-op. - } - - /** - * @param mtx Parent matrix. - */ - public TransposedMatrixView(Matrix mtx) { - this(mtx == null ? null : mtx.getStorage()); - } - - /** */ - private TransposedMatrixView(MatrixStorage sto) { - super(new MatrixDelegateStorage(sto, 0, 0, - sto == null ? 0 : sto.rowSize(), sto == null ? 0 : sto.columnSize())); - } - - /** {@inheritDoc} */ - @Override protected void storageSet(int row, int col, double v) { - super.storageSet(col, row, v); - } - - /** {@inheritDoc} */ - @Override protected double storageGet(int row, int col) { - return super.storageGet(col, row); - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return getStorage().columnSize(); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return getStorage().rowSize(); - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - MatrixDelegateStorage sto = (MatrixDelegateStorage)getStorage(); - - return new TransposedMatrixView(sto.delegate()); - } - - /** {@inheritDoc} */ - @Override public Matrix like(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector likeVector(int crd) { - throw new UnsupportedOperationException(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java deleted file mode 100644 index e73ef22f543a5..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockMatrixStorage.java +++ /dev/null @@ -1,434 +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.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.UUID; -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.internal.util.lang.IgnitePair; -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.impl.MatrixBlockKey; -import org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; - -import static org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry.MAX_BLOCK_SIZE; - -/** - * Storage for {@link SparseBlockDistributedMatrix}. - */ -public class BlockMatrixStorage extends CacheUtils implements MatrixStorage, StorageConstants, DistributedStorage { - /** Cache name used for all instances of {@link BlockMatrixStorage}. */ - private static final String CACHE_NAME = "ML_BLOCK_SPARSE_MATRICES_CONTAINER"; - - /** */ - private int blocksInCol; - - /** */ - private int blocksInRow; - - /** Amount of rows in the matrix. */ - private int rows; - - /** Amount of columns in the matrix. */ - private int cols; - - /** Matrix uuid. */ - private UUID uuid; - - /** Block size about 8 KB of data. */ - private int maxBlockEdge = MAX_BLOCK_SIZE; - - /** Actual distributed storage. */ - private IgniteCache< - MatrixBlockKey /* Matrix block number with uuid. */, - MatrixBlockEntry /* Block of matrix, local sparse matrix. */ - > cache = null; - - /** */ - public BlockMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in the matrix. - * @param cols Amount of columns in the matrix. - */ - public BlockMatrixStorage(int rows, int cols) { - assert rows > 0; - assert cols > 0; - - this.rows = rows; - this.cols = cols; - - this.blocksInRow = rows % maxBlockEdge == 0 ? rows / maxBlockEdge : rows / maxBlockEdge + 1; - this.blocksInCol = cols % maxBlockEdge == 0 ? cols / maxBlockEdge : cols / maxBlockEdge + 1; - - cache = newCache(); - - uuid = UUID.randomUUID(); - } - - /** */ - public IgniteCache cache() { - return cache; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return matrixGet(x, y); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - matrixSet(x, y, v); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return rows; - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return UNKNOWN_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeInt(blocksInRow); - out.writeInt(blocksInCol); - out.writeObject(uuid); - out.writeUTF(cache.getName()); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - blocksInRow = in.readInt(); - blocksInCol = in.readInt(); - uuid = (UUID)in.readObject(); - cache = ignite().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; - } - - /** Delete all data from cache. */ - @Override public void destroy() { - cache.clearAll(getAllKeys()); - } - - /** - * Get storage UUID. - * - * @return storage UUID. - */ - public UUID getUUID() { - return uuid; - } - - /** - * Build the cache key for the given blocks id. - * - * NB: NOT cell indices. - */ - public MatrixBlockKey getCacheKey(long blockIdRow, long blockIdCol) { - return new MatrixBlockKey(blockIdRow, blockIdCol, uuid, getAffinityKey(blockIdRow, blockIdCol)); - } - - /** - * Build the cache key for the given blocks id. - * - * NB: NOT cell indices. - */ - private MatrixBlockKey getCacheKey(IgnitePair blockId) { - return new MatrixBlockKey(blockId.get1(), blockId.get2(), uuid, getAffinityKey(blockId.get1(), blockId.get2())); - } - - /** {@inheritDoc} */ - @Override public Set getAllKeys() { - int maxRowIdx = rows - 1; - int maxColIdx = cols - 1; - IgnitePair maxBlockId = getBlockId(maxRowIdx, maxColIdx); - - Set keyset = new HashSet<>(); - - for (int i = 0; i <= maxBlockId.get1(); i++) - for (int j = 0; j <= maxBlockId.get2(); j++) - keyset.add(getCacheKey(i, j)); - - return keyset; - } - - /** {@inheritDoc} */ - @Override public String cacheName() { - return CACHE_NAME; - } - - /** - * Get rows for current block. - * - * @param blockId block id. - * @return The list of block entries. - */ - public List getRowForBlock(IgnitePair blockId) { - List res = new LinkedList<>(); - - for (int i = 0; i < blocksInCol; i++) - res.add(getEntryById(new IgnitePair<>(blockId.get1(), (long)i))); - - return res; - } - - /** - * Get cols for current block. - * - * @param blockId block id. - * @return The list of block entries. - */ - public List getColForBlock(IgnitePair blockId) { - List res = new LinkedList<>(); - - for (int i = 0; i < blocksInRow; i++) - res.add(getEntryById(new IgnitePair<>((long)i, blockId.get2()))); - - return res; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = blocksInCol; - - res = 31 * res + blocksInRow; - res = 31 * res + rows; - res = 31 * res + cols; - res = 31 * res + uuid.hashCode(); - res = 31 * res + maxBlockEdge; - res = 31 * res + cache.getName().hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - - BlockMatrixStorage that = (BlockMatrixStorage)o; - - return blocksInCol == that.blocksInCol && blocksInRow == that.blocksInRow && rows == that.rows - && cols == that.cols && maxBlockEdge == that.maxBlockEdge && uuid.equals(that.uuid) - && cache.getName().equals(that.cache.getName()); - - } - - /** - * Returns cached or new BlockEntry by given blockId. - * - * @param blockId blockId - * @return BlockEntry - */ - private MatrixBlockEntry getEntryById(IgnitePair blockId) { - MatrixBlockKey key = getCacheKey(blockId.get1(), blockId.get2()); - - MatrixBlockEntry entry = cache.localPeek(key, CachePeekMode.PRIMARY); - entry = entry != null ? entry : cache.get(key); - - if (entry == null) - entry = getEmptyBlockEntry(blockId); - - return entry; - } - - /** - * Builds empty BlockEntry with sizes based on blockId and BlockMatrixStorage fields' values. - * - * @param blockId blockId - * @return Empty BlockEntry - */ - private MatrixBlockEntry getEmptyBlockEntry(IgnitePair blockId) { - MatrixBlockEntry entry; - int rowMod = rows % maxBlockEdge; - int colMod = cols % maxBlockEdge; - - int rowSize; - - if (rowMod == 0) - rowSize = maxBlockEdge; - else - rowSize = blockId.get1() != (blocksInRow - 1) ? maxBlockEdge : rowMod; - - int colSize; - - if (colMod == 0) - colSize = maxBlockEdge; - else - colSize = blockId.get2() != (blocksInCol - 1) ? maxBlockEdge : colMod; - - entry = new MatrixBlockEntry(rowSize, colSize); - return entry; - } - - /** - * TODO: IGNITE-5646, WIP - * - * Get affinity key for the given id. - */ - private UUID getAffinityKey(long blockIdRow, long blockIdCol) { - return null; - } - - /** - * 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) { - IgnitePair blockId = getBlockId(a, b); - // Remote set on the primary node (where given row or column is stored locally). - ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, blockId)).run(() -> { - IgniteCache cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - MatrixBlockKey key = getCacheKey(blockId.get1(), blockId.get2()); - - // Local get. - MatrixBlockEntry block = getEntryById(blockId); - - block.set(a % block.rowSize(), b % block.columnSize(), v); - - // Local put. - cache.put(key, block); - }); - } - - /** - * Calculates blockId for given cell's coordinates. - * - * @param x x1 attribute in (x1,x2) coordinates - * @param y x2 attribute in (x1, x2) coordinates - * @return blockId as an IgnitePair - */ - private IgnitePair getBlockId(int x, int y) { - return new IgnitePair<>((long)x / maxBlockEdge, (long)y / maxBlockEdge); - } - - /** - * 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, getBlockId(a, b))).call(() -> { - IgniteCache cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - MatrixBlockKey key = getCacheKey(getBlockId(a, b)); - - // Local get. - MatrixBlockEntry block = cache.localPeek(key, CachePeekMode.PRIMARY); - - if (block == null) - block = cache.get(key); - - return block == null ? 0.0 : block.get(a % block.rowSize(), b % block.columnSize()); - }); - } - - /** - * Create new ML cache if needed. - */ - private IgniteCache newCache() { - CacheConfiguration 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); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java deleted file mode 100644 index 6400b4d005e98..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/BlockVectorStorage.java +++ /dev/null @@ -1,368 +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.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.UUID; -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.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.impl.VectorBlockKey; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector; -import org.apache.ignite.ml.math.impls.vector.VectorBlockEntry; -import org.jetbrains.annotations.NotNull; - -import static org.apache.ignite.ml.math.impls.matrix.MatrixBlockEntry.MAX_BLOCK_SIZE; - -/** - * Storage for {@link SparseBlockDistributedVector}. - */ -public class BlockVectorStorage extends CacheUtils implements VectorStorage, StorageConstants, DistributedStorage { - /** Cache name used for all instances of {@link BlockVectorStorage}. */ - private static final String CACHE_NAME = "ML_BLOCK_SPARSE_MATRICES_CONTAINER"; - - /** */ - private int blocks; - - /** Amount of columns in the vector. */ - private int size; - - /** Matrix uuid. */ - private UUID uuid; - - /** Block size about 8 KB of data. */ - private int maxBlockEdge = MAX_BLOCK_SIZE; - - /** Actual distributed storage. */ - private IgniteCache< - VectorBlockKey /* Matrix block number with uuid. */, - VectorBlockEntry /* Block of matrix, local sparse matrix. */ - > cache = null; - - /** - * - */ - public BlockVectorStorage() { - // No-op. - } - - /** - * @param size Amount of columns in the vector. - */ - public BlockVectorStorage(int size) { - - assert size > 0; - - this.size = size; - this.blocks = size % maxBlockEdge == 0 ? size / maxBlockEdge : size / maxBlockEdge + 1; - - cache = newCache(); - uuid = UUID.randomUUID(); - } - - /** */ - public IgniteCache cache() { - return cache; - } - - /** {@inheritDoc} */ - @Override public double get(int x) { - return matrixGet(x); - } - - /** {@inheritDoc} */ - @Override public void set(int x, double v) { - matrixSet(x, v); - } - - /** {@inheritDoc} */ - @Override public int size() { - return size; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(size); - out.writeInt(blocks); - out.writeObject(uuid); - out.writeUTF(cache.getName()); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - size = in.readInt(); - blocks = in.readInt(); - uuid = (UUID)in.readObject(); - - cache = ignite().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; - } - - /** Delete all data from cache. */ - @Override public void destroy() { - cache.clearAll(getAllKeys()); - } - - /** - * Get storage UUID. - * - * @return storage UUID. - */ - public UUID getUUID() { - return uuid; - } - - /** - * Build the cache key for the given blocks id. - * - * NB: NOT cell indices. - */ - public VectorBlockKey getCacheKey(long blockId) { - return new VectorBlockKey(blockId, uuid, getAffinityKey(blockId)); - } - - /** {@inheritDoc} */ - @Override public Set getAllKeys() { - int maxIdx = size - 1; - long maxBlockId = getBlockId(maxIdx); - - Set keyset = new HashSet<>(); - - for (int i = 0; i <= maxBlockId; i++) - keyset.add(getCacheKey(i)); - - return keyset; - } - - /** {@inheritDoc} */ - @Override public String cacheName() { - return CACHE_NAME; - } - - /** - * Get column for current block. - * - * @param blockId block id. - * @return The list of block entries. - */ - public List getColForBlock(long blockId) { - List res = new LinkedList<>(); - - for (int i = 0; i < blocks; i++) - res.add(getEntryById(i)); - - return res; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + size; - 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; - - BlockVectorStorage that = (BlockVectorStorage)obj; - - return size == that.size && uuid.equals(that.uuid) - && (cache != null ? cache.equals(that.cache) : that.cache == null); - } - - /** - * - */ - private VectorBlockEntry getEntryById(long blockId) { - VectorBlockKey key = getCacheKey(blockId); - - VectorBlockEntry entry = cache.localPeek(key, CachePeekMode.PRIMARY); - entry = entry != null ? entry : cache.get(key); - - if (entry == null) - entry = getEmptyBlockEntry(blockId); - - return entry; - } - - /** - * Get empty block entry by the given block id. - */ - @NotNull - private VectorBlockEntry getEmptyBlockEntry(long blockId) { - VectorBlockEntry entry; - int colMod = size % maxBlockEdge; - - int colSize; - - if (colMod == 0) - colSize = maxBlockEdge; - else - colSize = blockId != (blocks - 1) ? maxBlockEdge : colMod; - - entry = new VectorBlockEntry(colSize); - return entry; - } - - /** - * TODO: IGNITE-5646, WIP - * - * Get affinity key for the given id. - */ - private UUID getAffinityKey(long blockId) { - return null; - } - - /** - * Distributed matrix set. - * - * @param idx Row or column index. - * @param v New value to set. - */ - private void matrixSet(int idx, double v) { - long blockId = getBlockId(idx); - // Remote set on the primary node (where given row or column is stored locally). - ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, blockId)).run(() -> { - IgniteCache cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - VectorBlockKey key = getCacheKey(blockId); - - // Local get. - VectorBlockEntry block = getEntryById(blockId); - - block.set(idx % block.size(), v); - - // Local put. - cache.put(key, block); - }); - } - - /** */ - private long getBlockId(int x) { - return (long)x / maxBlockEdge; - } - - /** - * Distributed vector get. - * - * @param idx index. - * @return Vector value at (idx) index. - */ - private double matrixGet(int idx) { - // Remote get from the primary node (where given row or column is stored locally). - return ignite().compute(getClusterGroupForGivenKey(CACHE_NAME, getBlockId(idx))).call(() -> { - IgniteCache cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - VectorBlockKey key = getCacheKey(getBlockId(idx)); - - // Local get. - VectorBlockEntry block = cache.localPeek(key, CachePeekMode.PRIMARY); - - if (block == null) - block = cache.get(key); - - return block == null ? 0.0 : block.get(idx % block.size()); - }); - } - - /** - * Create new ML cache if needed. - */ - private IgniteCache newCache() { - CacheConfiguration 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); - } - - /** - * Avoid this method for large vectors - * - * @return data presented as array - */ - @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; - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java deleted file mode 100644 index fbad957156df4..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/CacheMatrixStorage.java +++ /dev/null @@ -1,196 +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.IgniteCache; -import org.apache.ignite.Ignition; -import org.apache.ignite.ml.math.MatrixStorage; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.distributed.MatrixKeyMapper; -import org.apache.ignite.ml.math.distributed.ValueMapper; - -/** - * Matrix storage based on arbitrary cache and key and value mapping functions. - */ -public class CacheMatrixStorage implements MatrixStorage { - /** */ - private int rows; - /** */ - private int cols; - /** */ - private IgniteCache cache; - /** */ - private MatrixKeyMapper keyMapper; - /** */ - private ValueMapper valMapper; - - /** - * - */ - public CacheMatrixStorage() { - // No-op. - } - - /** - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - * @param cache Ignite cache. - * @param keyMapper {@link MatrixKeyMapper} to validate cache key. - * @param valMapper {@link ValueMapper} to obtain value for given cache key. - */ - public CacheMatrixStorage(int rows, int cols, IgniteCache cache, MatrixKeyMapper keyMapper, - ValueMapper valMapper) { - assert rows > 0; - assert cols > 0; - assert cache != null; - assert keyMapper != null; - assert valMapper != null; - - this.rows = rows; - this.cols = cols; - this.cache = cache; - this.keyMapper = keyMapper; - this.valMapper = valMapper; - } - - /** - * @return Ignite cache. - */ - public IgniteCache cache() { - return cache; - } - - /** - * @return Key mapper. - */ - public MatrixKeyMapper keyMapper() { - return keyMapper; - } - - /** - * @return Value mapper. - */ - public ValueMapper valueMapper() { - return valMapper; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return valMapper.toDouble(cache.get(keyMapper.apply(x, y))); - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - cache.put(keyMapper.apply(x, y), valMapper.fromDouble(v)); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return cols; - } - - /** {@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 void writeExternal(ObjectOutput out) throws IOException { - out.writeInt(rows); - out.writeInt(cols); - out.writeUTF(cache.getName()); - out.writeObject(keyMapper); - out.writeObject(valMapper); - } - - /** {@inheritDoc} */ - @SuppressWarnings({"unchecked"}) - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - rows = in.readInt(); - cols = in.readInt(); - cache = Ignition.localIgnite().getOrCreateCache(in.readUTF()); - keyMapper = (MatrixKeyMapper)in.readObject(); - valMapper = (ValueMapper)in.readObject(); - } - - /** {@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 + rows; - res = res * 37 + cols; - res = res * 37 + cache.hashCode(); - res = res * 37 + keyMapper.hashCode(); - res = res * 37 + valMapper.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - CacheMatrixStorage that = (CacheMatrixStorage)o; - - return (cache != null ? cache.equals(that.cache) : that.cache == null) && - (keyMapper != null ? keyMapper.equals(that.keyMapper) : that.keyMapper == null) && - (valMapper != null ? valMapper.equals(that.valMapper) : that.valMapper == null); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java deleted file mode 100644 index a0f102a4ec3cd..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/storage/matrix/DiagonalMatrixStorage.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.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.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; - -/** - * {@link MatrixStorage} implementation for diagonal Matrix view. - */ -public class DiagonalMatrixStorage implements MatrixStorage { - /** Backing vector for matrix diagonal. */ - private Vector diagonal; - - /** - * - */ - public DiagonalMatrixStorage() { - // No-op. - } - - /** - * @param diagonal Backing {@link Vector} for matrix diagonal. - */ - public DiagonalMatrixStorage(Vector diagonal) { - assert diagonal != null; - - this.diagonal = diagonal; - } - - /** - * - */ - public Vector diagonal() { - return diagonal; - } - - /** {@inheritDoc} */ - @Override public double get(int x, int y) { - return x == y ? diagonal.get(x) : 0.0; - } - - /** {@inheritDoc} */ - @Override public void set(int x, int y, double v) { - if (x == y) - diagonal.set(x, v); - else - throw new UnsupportedOperationException("Can't set off-diagonal element."); - } - - /** {@inheritDoc} */ - @Override public int columnSize() { - return diagonal.size(); - } - - /** {@inheritDoc} */ - @Override public int rowSize() { - return diagonal.size(); - } - - /** {@inheritDoc} */ - @Override public int storageMode() { - return StorageConstants.UNKNOWN_STORAGE_MODE; - } - - /** {@inheritDoc} */ - @Override public int accessMode() { - return StorageConstants.RANDOM_ACCESS_MODE; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - out.writeObject(diagonal); - } - - /** {@inheritDoc} */ - @Override public double[] data() { - int size = diagonal.size(); - double[] res = new double[size * size]; - - for (int i = 0; i < size; i++) - res[i * size + i % size] = diagonal.getX(i); - - return res; - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - diagonal = (Vector)in.readObject(); - } - - /** {@inheritDoc} */ - @Override public boolean isSequentialAccess() { - return diagonal.isSequentialAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDense() { - return diagonal.isDense(); - } - - /** {@inheritDoc} */ - @Override public boolean isRandomAccess() { - return diagonal.isRandomAccess(); - } - - /** {@inheritDoc} */ - @Override public boolean isDistributed() { - return diagonal.isDistributed(); - } - - /** {@inheritDoc} */ - @Override public boolean isArrayBased() { - return false; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + diagonal.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - DiagonalMatrixStorage that = (DiagonalMatrixStorage)o; - - return diagonal.equals(that.diagonal); - } -} 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 18cc108ace44f..0000000000000 --- 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; - } -} 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 387b347faffba..0000000000000 --- 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; - } -} 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 56bd87103540b..0000000000000 --- 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; - } -} 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 9b4a18943b80a..0000000000000 --- 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 { - /** 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 /* 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> newCache() { - CacheConfiguration> 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> 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> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - // Local get. - Map 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> cache = Ignition.localIgnite().getOrCreateCache(CACHE_NAME); - - // Local get. - Map 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 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 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; - } -} 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 c4bb995be1f88..0000000000000 --- 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 implements VectorStorage { - /** Storage size. */ - private int size; - - /** Key mapper. */ - private VectorKeyMapper keyMapper; - /** Value mapper. */ - private ValueMapper valMapper; - - /** Underlying ignite cache. */ - private IgniteCache 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 cache, VectorKeyMapper keyMapper, - ValueMapper 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 cache() { - return cache; - } - - /** - * @return Key mapper to validate cache keys. - */ - public VectorKeyMapper keyMapper() { - return keyMapper; - } - - /** - * @return Value mapper to obtain vector element values. - */ - public ValueMapper 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)in.readObject(); - valMapper = (ValueMapper)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); - } -} 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 0423bc5f725d6..0000000000000 --- 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; - } -} 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 aabe3b1c131d1..0000000000000 --- 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 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 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 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 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)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; - } -} 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 1c798e4a38582..0000000000000 --- 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; - } -} 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 be1ad91ca48b8..0000000000000 --- 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; - } -} 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 ac86e16b69a60..0000000000000 --- 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; - } -} 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 488e158658b2c..0000000000000 --- 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; - } -} 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 0496d05468d09..0000000000000 --- 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 { - /** 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 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 newCache() { - CacheConfiguration 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 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 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 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 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 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); - } -} 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 1de334f33cea7..0000000000000 --- 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 fun) { - return new FunctionVector(size(), (i) -> fun.apply(get(i))); - } - - /** {@inheritDoc} */ - @Override public Vector map(Vector vec, IgniteBiFunction fun) { - checkCardinality(vec); - - return new FunctionVector(size(), (i) -> fun.apply(get(i), vec.get(i))); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteBiFunction 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(); - } -} 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 676f271169585..0000000000000 --- 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 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 cache, - VectorKeyMapper keyFunc, - ValueMapper valMapper) { - setStorage(new CacheVectorStorage<>(size, cache, keyFunc, valMapper)); - } - - /** - * @param mapper Mapping function. - */ - private Vector mapOverCache(IgniteFunction mapper) { - CacheVectorStorage sto = storage(); - - CacheUtils.map(sto.cache().getName(), sto.keyMapper(), sto.valueMapper(), mapper); - - return this; - } - - /** {@inheritDoc} */ - @Override public double minValue() { - CacheVectorStorage sto = storage(); - - return CacheUtils.min(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public double maxValue() { - CacheVectorStorage sto = storage(); - - return CacheUtils.max(sto.cache().getName(), sto.keyMapper(), sto.valueMapper()); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteDoubleFunction fun) { - return mapOverCache(fun::apply); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteBiFunction fun, double y) { - // TODO: IGNITE-5723, provide cache-optimized implementation. - return super.map(fun, y); - } - - /** {@inheritDoc} */ - @Override public double sum() { - CacheVectorStorage 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 storage() { - return (CacheVectorStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } -} 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 5038f1334844d..0000000000000 --- 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); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java deleted file mode 100644 index a2ffd904e4b7d..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java +++ /dev/null @@ -1,112 +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 java.util.Map; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.functions.IntDoubleToVoidFunction; -import org.apache.ignite.ml.math.impls.storage.vector.FunctionVectorStorage; - -/** - * Implementation of {@link Vector} that maps vector element index to {@link java.util.function} interfaces. - */ -public class FunctionVector extends AbstractVector { - /** - * - */ - public FunctionVector() { - // No-op. - } - - /** - * Creates read-write or read-only function vector. - * - * @param size Vector size. - * @param getFunc Function that returns value corresponding to given element index. - * @param setFunc Set function. If {@code null} - this will be a read-only vector. - */ - public FunctionVector(int size, IgniteFunction getFunc, IntDoubleToVoidFunction setFunc) { - setStorage(new FunctionVectorStorage(size, getFunc, setFunc)); - } - - /** - * Creates read-only function vector. - * - * @param size Vector size. - * @param getFunc Function that returns value corresponding to given element index. - */ - public FunctionVector(int size, IgniteFunction getFunc) { - setStorage(new FunctionVectorStorage(size, getFunc)); - } - - /** - * @param args Arguments for vector constructor. - */ - public FunctionVector(Map args) { - assert args != null; - - if (args.containsKey("size") && args.containsKey("getFunc") && args.containsKey("setFunc")) { - @SuppressWarnings("unchecked") - IgniteFunction getFunc = (IgniteFunction)args.get("getFunc"); - IntDoubleToVoidFunction setFunc = (IntDoubleToVoidFunction)args.get("setFunc"); - int size = (int)args.get("size"); - - setStorage(new FunctionVectorStorage(size, getFunc, setFunc)); - } - else if (args.containsKey("size") && args.containsKey("getFunc")) { - @SuppressWarnings("unchecked") - IgniteFunction getFunc = (IgniteFunction)args.get("getFunc"); - int size = (int)args.get("size"); - - setStorage(new FunctionVectorStorage(size, getFunc)); - } - else - throw new UnsupportedOperationException("Invalid constructor argument(s)."); - } - - /** - * - * - */ - private FunctionVectorStorage storage() { - return (FunctionVectorStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - FunctionVectorStorage sto = storage(); - - return new FunctionVector(crd, sto.getFunction(), sto.setFunction()); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - if (x == 0.0) - return like(size()).assign(0); - else - return super.times(x); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java deleted file mode 100644 index 365b5ebf0e5ff..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java +++ /dev/null @@ -1,163 +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 java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.impls.storage.vector.PivotedVectorStorage; - -/** - * Pivoted (index mapped) view over another vector. - */ -public class PivotedVectorView extends AbstractVector { - /** */ - private Vector vec; - - /** - * @param vec Parent vector. - * @param pivot Mapping from external index to internal. - * @param unpivot Mapping from internal index to external. - */ - public PivotedVectorView(Vector vec, int[] pivot, int[] unpivot) { - setStorage(new PivotedVectorStorage(vec.getStorage(), pivot, unpivot)); - - checkCardinality(pivot); - checkCardinality(unpivot); - - this.vec = vec; - } - - /** - * @param vec Parent vector. - * @param pivot Mapping from external index to internal. - */ - public PivotedVectorView(Vector vec, int[] pivot) { - setStorage(new PivotedVectorStorage(vec.getStorage(), pivot)); - - checkCardinality(pivot); - - this.vec = vec; - } - - /** */ - private PivotedVectorStorage storage() { - return (PivotedVectorStorage)getStorage(); - } - - /** - * - */ - public PivotedVectorView() { - // No-op. - } - - /** - * @return Parent vector. - */ - public Vector getBaseVector() { - return vec; - } - - /** - * @param i Index to pivot. - * @return Mapping from external index to internal for given index. - */ - public int pivot(int i) { - return storage().pivot()[i]; - } - - /** - * @param i Index to unpivot. - * @return Mapping from internal index to external for given index. - */ - public int unpivot(int i) { - return storage().unpivot()[i]; - } - - /** - * @param idx Index of vector element. - * @return Vector element at given index. - */ - protected Vector.Element makeElement(int idx) { - checkIndex(idx); - - // External index. - int exIdx = storage().pivot()[idx]; - - return new Vector.Element() { - /** {@inheritDoc} */ - @Override public double get() { - return storageGet(idx); - } - - /** {@inheritDoc} */ - @Override public int index() { - return exIdx; - } - - /** {@inheritDoc} */ - @Override public void set(double val) { - storageSet(idx, val); - } - }; - } - - /** {@inheritDoc} */ - @Override public Vector copy() { - PivotedVectorStorage sto = storage(); - - return new PivotedVectorView(vec, sto.pivot(), sto.unpivot()); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return vec.likeMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - if (x == 0.0) - return copy().map(Functions.mult(x)); - else - return super.times(x); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeObject(vec); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - vec = (Vector)in.readObject(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java deleted file mode 100644 index 633773e469e75..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java +++ /dev/null @@ -1,130 +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 java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Map; -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.matrix.RandomMatrix; -import org.apache.ignite.ml.math.impls.storage.vector.RandomVectorStorage; - -/** - * Random vector. Each value is taken from {-1,0,1} with roughly equal probability. Note - * that by default, the value is determined by a relatively simple hash of the index. - */ -public class RandomVector extends AbstractReadOnlyVector { - /** */ - private boolean fastHash; - - /** - * @param size Vector cardinality. - * @param fastHash Whether or not to use fast hashing or Murmur hashing. - */ - private VectorStorage mkStorage(int size, boolean fastHash) { - this.fastHash = fastHash; - - return new RandomVectorStorage(size, fastHash); - } - - /** - * @param size Vector cardinality. - * @param fastHash Whether or not to use fast hashing or Murmur hashing. - */ - public RandomVector(int size, boolean fastHash) { - setStorage(mkStorage(size, fastHash)); - } - - /** - * @param size Vector cardinality. - */ - public RandomVector(int size) { - this(size, true); - } - - /** - * @param args Parameters to create new vector instance. - */ - public RandomVector(Map args) { - assert args != null; - - if (args.containsKey("size") && args.containsKey("fastHash")) - setStorage(mkStorage((int)args.get("size"), (boolean)args.get("fastHash"))); - else if (args.containsKey("size")) - setStorage(mkStorage((int)args.get("size"), true)); - else - throw new UnsupportedOperationException("Invalid constructor argument(s)."); - } - - /** */ - public RandomVector() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - return new RandomVector(crd, fastHash); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return new RandomMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeBoolean(fastHash); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - fastHash = in.readBoolean(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + Boolean.hashCode(fastHash); - res = res * 37 + getStorage().hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - RandomVector that = (RandomVector)o; - VectorStorage sto = getStorage(); - - return fastHash == that.fastHash && (sto != null ? sto.equals(that.getStorage()) : that.getStorage() == null); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java deleted file mode 100644 index a5dc64b6c895f..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java +++ /dev/null @@ -1,102 +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 java.util.Map; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.storage.vector.SingleElementVectorStorage; - -/** - * Read-write vector holding a single non-zero value at some index. - */ -public class SingleElementVector extends AbstractVector { - /** - * - */ - public SingleElementVector() { - // No-op - } - - /** - * @param size Parent vector size. - * @param idx Index of the parent vector element. - * @param val Value of the vector element. - */ - public SingleElementVector(int size, int idx, double val) { - super(new SingleElementVectorStorage(size, idx, val)); - } - - /** - * @param args Parameters to create new vector instance. - */ - public SingleElementVector(Map args) { - assert args != null; - - if (args.containsKey("size") && args.containsKey("index") && args.containsKey("value")) { - int size = (int)args.get("size"); - int idx = (int)args.get("index"); - double val = (double)args.get("value"); - - setStorage(new SingleElementVectorStorage(size, idx, val)); - } - else - throw new UnsupportedOperationException("Invalid constructor argument(s)."); - } - - /** - * - * - */ - private SingleElementVectorStorage storage() { - return (SingleElementVectorStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Element minElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public Element maxElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public double sum() { - return getX(storage().index()); - } - - /** {@inheritDoc} */ - @Override public int nonZeroElements() { - return isZero(get(storage().index())) ? 0 : 1; - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - int idx = storage().index(); - - return new SingleElementVector(crd, idx, getX(idx)); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } -} \ No newline at end of file diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java deleted file mode 100644 index c2c648b8008e9..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java +++ /dev/null @@ -1,97 +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.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.impls.storage.vector.SingleElementVectorDelegateStorage; - -/** - * Single value vector view over another vector. - */ -public class SingleElementVectorView extends AbstractVector { - /** - * - */ - public SingleElementVectorView() { - // No-op. - } - - /** - * @param vec Parent vector. - * @param idx Index of the parent vector element. - */ - public SingleElementVectorView(Vector vec, int idx) { - super(new SingleElementVectorDelegateStorage(vec, idx)); - } - - /** - * - * - */ - private SingleElementVectorDelegateStorage storage() { - return (SingleElementVectorDelegateStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Vector.Element minElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public Vector.Element maxElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public double sum() { - return getX(storage().index()); - } - - /** {@inheritDoc} */ - @Override public int nonZeroElements() { - return isZero(getX(storage().index())) ? 0 : 1; - } - - /** {@inheritDoc} */ - @Override public Vector copy() { - SingleElementVectorDelegateStorage sto = storage(); - - return new SingleElementVectorView(sto.delegate(), sto.index()); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - if (x == 0.0) - return copy().map(Functions.mult(x)); - else - return super.times(x); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java deleted file mode 100644 index 535d51a11e0bb..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java +++ /dev/null @@ -1,135 +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 java.util.UUID; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; -import org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage; - -/** - * Sparse distributed vector implementation based on data grid. - *

- * Unlike {@link CacheVector} that is based on existing cache, this implementation creates distributed - * cache internally and doesn't rely on pre-existing cache.

- *

- * You also need to call {@link #destroy()} to remove the underlying cache when you no longer need this - * vector.

- *

- * Currently fold supports only commutative operations.

- */ -public class SparseBlockDistributedVector extends AbstractVector implements StorageConstants { - /** - * - */ - public SparseBlockDistributedVector() { - // No-op. - } - - /** - * @param size Vector size - */ - public SparseBlockDistributedVector(int size) { - - assert size > 0; - setStorage(new BlockVectorStorage(size)); - } - - /** - * @param data Data to fill storage - */ - public SparseBlockDistributedVector(double[] data) { - setStorage(new BlockVectorStorage(data.length)); - for (int i = 0; i < data.length; i++) { - double val = data[i]; - if (val != 0.0) - storage().set(i, val); - } - } - - /** */ - public BlockVectorStorage storage() { - return (BlockVectorStorage)getStorage(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Vector divide(double d) { - return mapOverValues(v -> v / d); - } - - /** {@inheritDoc} */ - @Override public Vector like(int size) { - return new SparseBlockDistributedVector(size); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return new SparseBlockDistributedMatrix(rows, cols); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Vector plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Vector times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Vector assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteDoubleFunction fun) { - return mapOverValues(fun); - } - - /** - * @param mapper Mapping function. - * @return Vector with mapped values. - */ - private Vector mapOverValues(IgniteDoubleFunction mapper) { - CacheUtils.sparseMapForVector(getUUID(), mapper, storage().cacheName()); - - return this; - } - - /** */ - public UUID getUUID() { - return ((BlockVectorStorage)getStorage()).getUUID(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java deleted file mode 100644 index 3e7f8a1a9dc3a..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java +++ /dev/null @@ -1,147 +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 java.util.UUID; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.CacheUtils; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; -import org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage; - -/** - * Sparse distributed vector implementation based on data grid. - *

- * Unlike {@link CacheVector} that is based on existing cache, this implementation creates distributed - * cache internally and doesn't rely on pre-existing cache.

- *

- * You also need to call {@link #destroy()} to remove the underlying cache when you no longer need this - * vector.

- *

- * Currently fold supports only commutative operations.

- */ -public class SparseDistributedVector extends AbstractVector implements StorageConstants { - /** - * - */ - public SparseDistributedVector() { - // No-op. - } - - /** - * @param size Vector size. - * @param acsMode Vector elements access mode.. - */ - public SparseDistributedVector(int size, int acsMode) { - - assert size > 0; - assertAccessMode(acsMode); - - setStorage(new SparseDistributedVectorStorage(size, acsMode)); - } - - /** - * @param size Size. - */ - public SparseDistributedVector(int size) { - this(size, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** - * @param data Data. - */ - public SparseDistributedVector(double[] data) { - setStorage(new SparseDistributedVectorStorage(data.length, StorageConstants.RANDOM_ACCESS_MODE)); - - for (int i = 0; i < data.length; i++) { - double val = data[i]; - - if (val != 0.0) - storage().set(i, val); - } - } - - /** */ - public SparseDistributedVectorStorage storage() { - return (SparseDistributedVectorStorage)getStorage(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Vector divide(double d) { - return mapOverValues(v -> v / d); - } - - /** {@inheritDoc} */ - @Override public Vector like(int size) { - return new SparseDistributedVector(size, storage().accessMode()); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return new SparseDistributedMatrix(rows, cols); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Vector plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Vector times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Vector assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteDoubleFunction fun) { - return mapOverValues(fun); - } - - /** - * @param mapper Mapping function. - * @return Vector with mapped values. - */ - private Vector mapOverValues(IgniteDoubleFunction mapper) { - CacheUtils.sparseMapForVector(getUUID(), mapper, storage().cacheName()); - - return this; - } - - /** */ - public UUID getUUID() { - return ((SparseDistributedVectorStorage)getStorage()).getUUID(); - } -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.java deleted file mode 100644 index 999e41229538a..0000000000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.java +++ /dev/null @@ -1,47 +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.Vector; - -/** - * Block for {@link SparseBlockDistributedVector}. - */ -public final class VectorBlockEntry extends SparseLocalVector { - /** Max block size. */ - public static final int MAX_BLOCK_SIZE = 32; - - /** */ - public VectorBlockEntry() { - // No-op. - } - - /** */ - public VectorBlockEntry(int size) { - super(size, RANDOM_ACCESS_MODE); - assert size <= MAX_BLOCK_SIZE; - } - - /** */ - public VectorBlockEntry(Vector v) { - assert v.size() <= MAX_BLOCK_SIZE; - - setStorage(v.getStorage()); - } - -} diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java index 9f14bc7a50920..e737aabc6279a 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java @@ -24,13 +24,8 @@ import org.apache.ignite.ml.math.Vector; import org.apache.ignite.ml.math.functions.IgniteBiFunction; import org.apache.ignite.ml.math.functions.IgniteTriFunction; -import org.apache.ignite.ml.math.impls.matrix.CacheMatrix; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; import org.apache.ignite.ml.math.impls.matrix.MatrixView; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.matrix.RandomMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; @@ -93,15 +88,6 @@ public static Vector likeVector(Matrix matrix, int crd) { return matrix.likeVector(crd); } - /** - * Check if a given matrix is distributed. - * - * @param matrix Matrix for like. - */ - private static boolean isDistributed(Matrix matrix) { - return matrix instanceof SparseDistributedMatrix || matrix instanceof SparseBlockDistributedMatrix; - } - /** * Create the like vector with read-only matrices support. * @@ -143,8 +129,7 @@ public static DenseLocalOnHeapMatrix asDense(SparseLocalOnHeapMatrix m, int acsM /** */ private static boolean isCopyLikeSupport(Matrix matrix) { - return matrix instanceof RandomMatrix || matrix instanceof MatrixView || matrix instanceof CacheMatrix || - matrix instanceof PivotedMatrixView; + return matrix instanceof MatrixView; } /** */ diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java index 559206d880466..7d0106eb6e31e 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java @@ -316,20 +316,6 @@ private void assign(IgniteBiConsumer replicantAssigner, return asCol ? vector.size() : replicationCnt; } - /** {@inheritDoc} */ - @Override public double determinant() { - // If matrix is not square throw exception. - checkCardinality(vector.size(), replicationCnt); - - // If matrix is 1x1 then determinant is its single element otherwise there are linear dependence and determinant is 0. - return vector.size() > 0 ? 0 : vector.get(1); - } - - /** {@inheritDoc} */ - @Override public Matrix inverse() { - throw new UnsupportedOperationException(); - } - /** {@inheritDoc} */ @Override public Matrix divide(double x) { return new ReplicatedVectorMatrix(vector.divide(x), replicationCnt, asCol); diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java index 3239116a24d14..69262ab8476ed 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java @@ -22,7 +22,6 @@ import org.apache.ignite.ml.math.exceptions.NoDataException; import org.apache.ignite.ml.math.exceptions.knn.NoLabelVectorException; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector; /** * Class for set of labeled vectors. @@ -200,9 +199,6 @@ public void setLabel(int idx, double lb) { /** */ public static Vector emptyVector(int size, boolean isDistributed) { - if(isDistributed) - return new SparseDistributedVector(size); - else return new DenseLocalOnHeapVector(size); } diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.java deleted file mode 100644 index 2968924f06895..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.java +++ /dev/null @@ -1,47 +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 org.apache.ignite.ml.math.impls.matrix.CacheMatrixTest; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedBlockMatrixTest; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrixTest; -import org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorageTest; -import org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorageTest; -import org.apache.ignite.ml.math.impls.vector.CacheVectorTest; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVectorTest; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVectorTest; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -/** - * Test suite for all distributed tests located in org.apache.ignite.ml.math.impls.* package. - */ -@RunWith(Suite.class) -@Suite.SuiteClasses({ - CacheVectorTest.class, - CacheMatrixTest.class, - SparseDistributedMatrixStorageTest.class, - SparseDistributedMatrixTest.class, - SparseDistributedBlockMatrixTest.class, - SparseDistributedVectorStorageTest.class, - SparseDistributedVectorTest.class, - SparseBlockDistributedVectorTest.class -}) -public class MathImplDistributedTestSuite { - // No-op. -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java index 926d8726efcee..ee9d41804acc5 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java @@ -17,24 +17,14 @@ package org.apache.ignite.ml.math; -import org.apache.ignite.ml.math.decompositions.CholeskyDecompositionTest; -import org.apache.ignite.ml.math.decompositions.EigenDecompositionTest; -import org.apache.ignite.ml.math.decompositions.LUDecompositionTest; -import org.apache.ignite.ml.math.decompositions.QRDSolverTest; -import org.apache.ignite.ml.math.decompositions.QRDecompositionTest; -import org.apache.ignite.ml.math.decompositions.SingularValueDecompositionTest; import org.apache.ignite.ml.math.distances.DistanceTest; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrixConstructorTest; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrixConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.DiagonalMatrixTest; -import org.apache.ignite.ml.math.impls.matrix.FunctionMatrixConstructorTest; import org.apache.ignite.ml.math.impls.matrix.MatrixAttributeTest; import org.apache.ignite.ml.math.impls.matrix.MatrixImplementationsTest; import org.apache.ignite.ml.math.impls.matrix.MatrixViewConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixViewConstructorTest; import org.apache.ignite.ml.math.impls.matrix.RandomMatrixConstructorTest; import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrixConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.TransposedMatrixViewTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixArrayStorageTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixOffHeapStorageTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixStorageImplementationTest; @@ -43,21 +33,13 @@ import org.apache.ignite.ml.math.impls.storage.vector.VectorArrayStorageTest; import org.apache.ignite.ml.math.impls.storage.vector.VectorOffheapStorageTest; import org.apache.ignite.ml.math.impls.vector.AbstractVectorTest; -import org.apache.ignite.ml.math.impls.vector.ConstantVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.DelegatingVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVectorConstructorTest; -import org.apache.ignite.ml.math.impls.vector.FunctionVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.MatrixVectorViewTest; -import org.apache.ignite.ml.math.impls.vector.PivotedVectorViewConstructorTest; -import org.apache.ignite.ml.math.impls.vector.RandomVectorConstructorTest; -import org.apache.ignite.ml.math.impls.vector.SingleElementVectorConstructorTest; -import org.apache.ignite.ml.math.impls.vector.SingleElementVectorViewConstructorTest; import org.apache.ignite.ml.math.impls.vector.SparseLocalVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.VectorAttributesTest; import org.apache.ignite.ml.math.impls.vector.VectorFoldMapTest; -import org.apache.ignite.ml.math.impls.vector.VectorImplementationsTest; -import org.apache.ignite.ml.math.impls.vector.VectorIterableTest; import org.apache.ignite.ml.math.impls.vector.VectorNormTest; import org.apache.ignite.ml.math.impls.vector.VectorToMatrixTest; import org.apache.ignite.ml.math.impls.vector.VectorViewTest; @@ -74,20 +56,12 @@ DenseLocalOnHeapVectorConstructorTest.class, DenseLocalOffHeapVectorConstructorTest.class, SparseLocalVectorConstructorTest.class, - RandomVectorConstructorTest.class, - ConstantVectorConstructorTest.class, - FunctionVectorConstructorTest.class, - SingleElementVectorConstructorTest.class, - PivotedVectorViewConstructorTest.class, - SingleElementVectorViewConstructorTest.class, DelegatingVectorConstructorTest.class, // Various vectors tests. AbstractVectorTest.class, - VectorImplementationsTest.class, VectorViewTest.class, MatrixVectorViewTest.class, // Vector particular features tests. - VectorIterableTest.class, VectorAttributesTest.class, VectorToMatrixTest.class, VectorNormTest.class, @@ -105,22 +79,11 @@ DenseLocalOnHeapMatrixConstructorTest.class, DenseLocalOffHeapMatrixConstructorTest.class, RandomMatrixConstructorTest.class, - FunctionMatrixConstructorTest.class, MatrixViewConstructorTest.class, - PivotedMatrixViewConstructorTest.class, SparseLocalOnHeapMatrixConstructorTest.class, // Matrix tests. MatrixImplementationsTest.class, - DiagonalMatrixTest.class, MatrixAttributeTest.class, - TransposedMatrixViewTest.class, - // Decompositions. - LUDecompositionTest.class, - EigenDecompositionTest.class, - CholeskyDecompositionTest.class, - QRDecompositionTest.class, - SingularValueDecompositionTest.class, - QRDSolverTest.class, DistanceTest.class, LSQROnHeapTest.class }) diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java index 974b7bbc3b53d..cd6ae986eb193 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java @@ -26,7 +26,6 @@ @RunWith(Suite.class) @Suite.SuiteClasses({ MathImplLocalTestSuite.class, - MathImplDistributedTestSuite.class, TracerTest.class, BlasTest.class }) diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java deleted file mode 100644 index cc726a81e9547..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.ml.math.decompositions; - -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.exceptions.NonPositiveDefiniteMatrixException; -import org.apache.ignite.ml.math.exceptions.NonSymmetricMatrixException; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** */ -public class CholeskyDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - basicTest(new PivotedMatrixView(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }))); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new CholeskyDecomposition(null); - } - - /** */ - @Test(expected = CardinalityException.class) - public void wrongMatrixSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - @Test(expected = NonSymmetricMatrixException.class) - public void nonSymmetricMatrixTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 10.0d}, - {-1.0d, 2.0d, -1.0d}, - {-10.0d, -1.0d, 2.0d} - })); - } - - /** */ - @Test(expected = NonPositiveDefiniteMatrixException.class) - public void nonAbsPositiveMatrixTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 0.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveWrongVectorSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapVector(2)); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveWrongMatrixSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - private void basicTest(Matrix m) { - // This decomposition is useful when dealing with systems of linear equations of the form - // m x = b where m is a Hermitian matrix. - // For such systems Cholesky decomposition provides - // more effective method of solving compared to LU decomposition. - // Suppose we want to solve system - // m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs - // as a matrix of the form - // (b1, b2, ..., bm) - // to the method Cholesky::solve which returns solutions in the form - // (sol1, sol2, ..., solm) - CholeskyDecomposition dec = new CholeskyDecomposition(m); - assertEquals("Unexpected value for decomposition determinant.", - 4d, dec.getDeterminant(), 0d); - - Matrix l = dec.getL(); - Matrix lt = dec.getLT(); - - assertNotNull("Matrix l is expected to be not null.", l); - assertNotNull("Matrix lt is expected to be not null.", lt); - - for (int row = 0; row < l.rowSize(); row++) - for (int col = 0; col < l.columnSize(); col++) - assertEquals("Unexpected value transposed matrix at (" + row + "," + col + ").", - l.get(row, col), lt.get(col, row), 0d); - - Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { - {4.0, -6.0, 7.0}, - {1.0, 1.0, 1.0} - }).transpose(); - Matrix sol = dec.solve(bs); - - assertNotNull("Solution matrix is expected to be not null.", sol); - assertEquals("Solution rows are not as expected.", bs.rowSize(), sol.rowSize()); - assertEquals("Solution columns are not as expected.", bs.columnSize(), sol.columnSize()); - - for (int i = 0; i < sol.columnSize(); i++) - assertNotNull("Solution matrix column is expected to be not null at index " + i, sol.viewColumn(i)); - - Vector b = new DenseLocalOnHeapVector(new double[] {4.0, -6.0, 7.0}); - Vector solVec = dec.solve(b); - - for (int idx = 0; idx < b.size(); idx++) - assertEquals("Unexpected value solution vector at " + idx, - b.get(idx), solVec.get(idx), 0d); - - dec.destroy(); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java deleted file mode 100644 index 76aca0ba6d48e..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java +++ /dev/null @@ -1,193 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Tests for {@link EigenDecomposition} - */ -public class EigenDecompositionTest { - /** */ - private static final double EPSILON = 1e-11; - - /** */ - @Test - public void testMatrixWithRealEigenvalues() { - test(new double[][] { - {1.0d, 0.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d, 0.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d}}, - new double[] {1, 2, 2, 1}); - } - - /** */ - @Test - public void testSymmetricMatrix() { - EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d, 1.0d}, - {0.0d, 1.0d, 0.0d, 1.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d}})); - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertNotNull("Matrix d is expected to be not null.", d); - assertNotNull("Matrix v is expected to be not null.", v); - - assertEquals("Unexpected rows in d matrix.", 4, d.rowSize()); - assertEquals("Unexpected cols in d matrix.", 4, d.columnSize()); - - assertEquals("Unexpected rows in v matrix.", 4, v.rowSize()); - assertEquals("Unexpected cols in v matrix.", 4, v.columnSize()); - - assertIsDiagonalNonZero(d); - - decomposition.destroy(); - } - - /** */ - @Test - public void testNonSquareMatrix() { - EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d}, - {0.0d, 0.0d, 2.0d}, - {1.0d, 1.0d, 0.0d}})); - // TODO: IGNITE-5828, find out why decomposition of 3X4 matrix throws row index exception - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertNotNull("Matrix d is expected to be not null.", d); - assertNotNull("Matrix v is expected to be not null.", v); - - assertEquals("Unexpected rows in d matrix.", 4, d.rowSize()); - assertEquals("Unexpected cols in d matrix.", 4, d.columnSize()); - - assertEquals("Unexpected rows in v matrix.", 4, v.rowSize()); - assertEquals("Unexpected cols in v matrix.", 3, v.columnSize()); - - assertIsDiagonal(d, true); - - decomposition.destroy(); - } - - /** */ - private void test(double[][] mRaw, double[] expRealEigenValues) { - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(mRaw); - EigenDecomposition decomposition = new EigenDecomposition(m); - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertIsDiagonalNonZero(d); - - // check that d's diagonal consists of eigenvalues of m. - assertDiagonalConsistsOfEigenvalues(m, d, v); - - // m = v d v^{-1} is equivalent to - // m v = v d - assertMatricesAreEqual(m.times(v), v.times(d)); - - assertEigenvalues(decomposition, expRealEigenValues); - - decomposition.destroy(); - } - - /** */ - private void assertEigenvalues(EigenDecomposition decomposition, double[] expRealEigenValues) { - Vector real = decomposition.getRealEigenValues(); - Vector imag = decomposition.getImagEigenvalues(); - - assertEquals("Real values size differs from expected.", expRealEigenValues.length, real.size()); - assertEquals("Imag values size differs from expected.", expRealEigenValues.length, imag.size()); - - for (int idx = 0; idx < expRealEigenValues.length; idx++) { - assertEquals("Real eigen value differs from expected at " + idx, - expRealEigenValues[idx], real.get(idx), 0d); - - assertEquals("Imag eigen value differs from expected at " + idx, - 0d, imag.get(idx), 0d); - } - - } - - /** */ - private void assertDiagonalConsistsOfEigenvalues(DenseLocalOnHeapMatrix m, Matrix d, Matrix v) { - int n = m.columnSize(); - for (int i = 0; i < n; i++) { - Vector eigenVector = v.viewColumn(i); - double eigenVal = d.getX(i, i); - assertVectorsAreEqual(m.times(eigenVector), eigenVector.times(eigenVal)); - } - - } - - /** */ - private void assertMatricesAreEqual(Matrix exp, Matrix actual) { - assertTrue("The row sizes of matrices are not equal", exp.rowSize() == actual.rowSize()); - assertTrue("The col sizes of matrices are not equal", exp.columnSize() == actual.columnSize()); - - // Since matrix is square, we need only one dimension - int n = exp.columnSize(); - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - assertEquals("Values should be equal", exp.getX(i, j), actual.getX(i, j), EPSILON); - } - - /** */ - private void assertVectorsAreEqual(Vector exp, Vector actual) { - assertTrue("Vectors sizes are not equal", exp.size() == actual.size()); - - // Since matrix is square, we need only one dimension - int n = exp.size(); - - for (int i = 0; i < n; i++) - assertEquals("Values should be equal", exp.getX(i), actual.getX(i), EPSILON); - } - - /** */ - private void assertIsDiagonalNonZero(Matrix m) { - assertIsDiagonal(m, false); - } - - /** */ - private void assertIsDiagonal(Matrix m, boolean zeroesAllowed) { - // Since matrix is square, we need only one dimension - int n = m.columnSize(); - - assertEquals("Diagonal matrix is not square", n, m.rowSize()); - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - assertTrue("Matrix is not diagonal, violation at (" + i + "," + j + ")", - ((i == j) && (zeroesAllowed || m.getX(i, j) != 0)) - || ((i != j) && m.getX(i, j) == 0)); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java deleted file mode 100644 index 8e8b92022b494..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java +++ /dev/null @@ -1,252 +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.decompositions; - -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.exceptions.SingularMatrixException; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Tests for {@link LUDecomposition}. - */ -public class LUDecompositionTest { - /** */ - private Matrix testL; - /** */ - private Matrix testU; - /** */ - private Matrix testP; - /** */ - private Matrix testMatrix; - /** */ - private int[] rawPivot; - - /** */ - @Before - public void setUp() { - double[][] rawMatrix = new double[][] { - {2.0d, 1.0d, 1.0d, 0.0d}, - {4.0d, 3.0d, 3.0d, 1.0d}, - {8.0d, 7.0d, 9.0d, 5.0d}, - {6.0d, 7.0d, 9.0d, 8.0d}}; - double[][] rawL = { - {1.0d, 0.0d, 0.0d, 0.0d}, - {3.0d / 4.0d, 1.0d, 0.0d, 0.0d}, - {1.0d / 2.0d, -2.0d / 7.0d, 1.0d, 0.0d}, - {1.0d / 4.0d, -3.0d / 7.0d, 1.0d / 3.0d, 1.0d}}; - double[][] rawU = { - {8.0d, 7.0d, 9.0d, 5.0d}, - {0.0d, 7.0d / 4.0d, 9.0d / 4.0d, 17.0d / 4.0d}, - {0.0d, 0.0d, -6.0d / 7.0d, -2.0d / 7.0d}, - {0.0d, 0.0d, 0.0d, 2.0d / 3.0d}}; - double[][] rawP = new double[][] { - {0, 0, 1.0d, 0}, - {0, 0, 0, 1.0d}, - {0, 1.0d, 0, 0}, - {1.0d, 0, 0, 0}}; - - rawPivot = new int[] {3, 4, 2, 1}; - - testMatrix = new DenseLocalOnHeapMatrix(rawMatrix); - testL = new DenseLocalOnHeapMatrix(rawL); - testU = new DenseLocalOnHeapMatrix(rawU); - testP = new DenseLocalOnHeapMatrix(rawP); - } - - /** */ - @Test - public void getL() throws Exception { - Matrix luDecompositionL = new LUDecomposition(testMatrix).getL(); - - assertEquals("Unexpected row size.", testL.rowSize(), luDecompositionL.rowSize()); - assertEquals("Unexpected column size.", testL.columnSize(), luDecompositionL.columnSize()); - - for (int i = 0; i < testL.rowSize(); i++) - for (int j = 0; j < testL.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testL.getX(i, j), luDecompositionL.getX(i, j), 0.0000001d); - - luDecompositionL.destroy(); - } - - /** */ - @Test - public void getU() throws Exception { - Matrix luDecompositionU = new LUDecomposition(testMatrix).getU(); - - assertEquals("Unexpected row size.", testU.rowSize(), luDecompositionU.rowSize()); - assertEquals("Unexpected column size.", testU.columnSize(), luDecompositionU.columnSize()); - - for (int i = 0; i < testU.rowSize(); i++) - for (int j = 0; j < testU.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testU.getX(i, j), luDecompositionU.getX(i, j), 0.0000001d); - - luDecompositionU.destroy(); - } - - /** */ - @Test - public void getP() throws Exception { - Matrix luDecompositionP = new LUDecomposition(testMatrix).getP(); - - assertEquals("Unexpected row size.", testP.rowSize(), luDecompositionP.rowSize()); - assertEquals("Unexpected column size.", testP.columnSize(), luDecompositionP.columnSize()); - - for (int i = 0; i < testP.rowSize(); i++) - for (int j = 0; j < testP.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testP.getX(i, j), luDecompositionP.getX(i, j), 0.0000001d); - - luDecompositionP.destroy(); - } - - /** */ - @Test - public void getPivot() throws Exception { - Vector pivot = new LUDecomposition(testMatrix).getPivot(); - - assertEquals("Unexpected pivot size.", rawPivot.length, pivot.size()); - - for (int i = 0; i < testU.rowSize(); i++) - assertEquals("Unexpected value at " + i, rawPivot[i], (int)pivot.get(i) + 1); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - LUDecomposition dec = new LUDecomposition(new PivotedMatrixView(testMatrix)); - Matrix luDecompositionL = dec.getL(); - - assertEquals("Unexpected L row size.", testL.rowSize(), luDecompositionL.rowSize()); - assertEquals("Unexpected L column size.", testL.columnSize(), luDecompositionL.columnSize()); - - for (int i = 0; i < testL.rowSize(); i++) - for (int j = 0; j < testL.columnSize(); j++) - assertEquals("Unexpected L value at (" + i + "," + j + ").", - testL.getX(i, j), luDecompositionL.getX(i, j), 0.0000001d); - - Matrix luDecompositionU = dec.getU(); - - assertEquals("Unexpected U row size.", testU.rowSize(), luDecompositionU.rowSize()); - assertEquals("Unexpected U column size.", testU.columnSize(), luDecompositionU.columnSize()); - - for (int i = 0; i < testU.rowSize(); i++) - for (int j = 0; j < testU.columnSize(); j++) - assertEquals("Unexpected U value at (" + i + "," + j + ").", - testU.getX(i, j), luDecompositionU.getX(i, j), 0.0000001d); - - Matrix luDecompositionP = dec.getP(); - - assertEquals("Unexpected P row size.", testP.rowSize(), luDecompositionP.rowSize()); - assertEquals("Unexpected P column size.", testP.columnSize(), luDecompositionP.columnSize()); - - for (int i = 0; i < testP.rowSize(); i++) - for (int j = 0; j < testP.columnSize(); j++) - assertEquals("Unexpected P value at (" + i + "," + j + ").", - testP.getX(i, j), luDecompositionP.getX(i, j), 0.0000001d); - - dec.destroy(); - } - - /** */ - @Test - public void singularDeterminant() throws Exception { - assertEquals("Unexpected determinant for singular matrix decomposition.", - 0d, new LUDecomposition(new DenseLocalOnHeapMatrix(2, 2)).determinant(), 0d); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveVecWrongSize() throws Exception { - new LUDecomposition(testMatrix).solve(new DenseLocalOnHeapVector(testMatrix.rowSize() + 1)); - } - - /** */ - @Test(expected = SingularMatrixException.class) - public void solveVecSingularMatrix() throws Exception { - new LUDecomposition(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())) - .solve(new DenseLocalOnHeapVector(testMatrix.rowSize())); - } - - /** */ - @Test - public void solveVec() throws Exception { - Vector sol = new LUDecomposition(new PivotedMatrixView(testMatrix)) - .solve(new DenseLocalOnHeapVector(testMatrix.rowSize())); - - assertEquals("Wrong solution vector size.", testMatrix.rowSize(), sol.size()); - - for (int i = 0; i < sol.size(); i++) - assertEquals("Unexpected value at index " + i, 0d, sol.getX(i), 0.0000001d); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveMtxWrongSize() throws Exception { - new LUDecomposition(testMatrix).solve( - new DenseLocalOnHeapMatrix(testMatrix.rowSize() + 1, testMatrix.rowSize())); - } - - /** */ - @Test(expected = SingularMatrixException.class) - public void solveMtxSingularMatrix() throws Exception { - new LUDecomposition(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())) - .solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())); - } - - /** */ - @Test - public void solveMtx() throws Exception { - Matrix sol = new LUDecomposition(new PivotedMatrixView(testMatrix)) - .solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())); - - assertEquals("Wrong solution matrix row size.", testMatrix.rowSize(), sol.rowSize()); - - assertEquals("Wrong solution matrix column size.", testMatrix.rowSize(), sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected P value at (" + row + "," + col + ").", - 0d, sol.getX(row, col), 0.0000001d); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new LUDecomposition(null); - } - - /** */ - @Test(expected = CardinalityException.class) - public void nonSquareMatrixTest() { - new LUDecomposition(new DenseLocalOnHeapMatrix(2, 3)); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java deleted file mode 100644 index d3e8e76055ca5..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java +++ /dev/null @@ -1,87 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** */ -public class QRDSolverTest { - /** */ - @Test - public void basicTest() { - Matrix m = new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }); - - QRDecomposition dec = new QRDecomposition(m); - assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank()); - - Matrix q = dec.getQ(); - Matrix r = dec.getR(); - - assertNotNull("Matrix q is expected to be not null.", q); - assertNotNull("Matrix r is expected to be not null.", r); - - Matrix qSafeCp = safeCopy(q); - - Matrix expIdentity = qSafeCp.times(qSafeCp.transpose()); - - final double delta = 0.0001; - - for (int row = 0; row < expIdentity.rowSize(); row++) - for (int col = 0; col < expIdentity.columnSize(); col++) - assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", - row == col ? 1d : 0d, expIdentity.get(col, row), delta); - - for (int row = 0; row < r.rowSize(); row++) - for (int col = 0; col < row - 1; col++) - assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", - 0d, r.get(row, col), delta); - - Matrix recomposed = qSafeCp.times(r); - - for (int row = 0; row < m.rowSize(); row++) - for (int col = 0; col < m.columnSize(); col++) - assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", - m.get(row, col), recomposed.get(row, col), delta); - - Matrix sol = new QRDSolver(q, r).solve(new DenseLocalOnHeapMatrix(3, 10)); - assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize()); - assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", - 0d, sol.get(row, col), delta); - - dec.destroy(); - } - - /** */ - private Matrix safeCopy(Matrix orig) { - return new DenseLocalOnHeapMatrix(orig.rowSize(), orig.columnSize()).assign(orig); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java deleted file mode 100644 index a3b083fe074c9..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java +++ /dev/null @@ -1,141 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** */ -public class QRDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - basicTest(new PivotedMatrixView(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }))); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new QRDecomposition(null); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - public void solveWrongMatrixSizeTest() { - new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - private void basicTest(Matrix m) { - QRDecomposition dec = new QRDecomposition(m); - assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank()); - - Matrix q = dec.getQ(); - Matrix r = dec.getR(); - - assertNotNull("Matrix q is expected to be not null.", q); - assertNotNull("Matrix r is expected to be not null.", r); - - Matrix qSafeCp = safeCopy(q); - - Matrix expIdentity = qSafeCp.times(qSafeCp.transpose()); - - final double delta = 0.0001; - - for (int row = 0; row < expIdentity.rowSize(); row++) - for (int col = 0; col < expIdentity.columnSize(); col++) - assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", - row == col ? 1d : 0d, expIdentity.get(col, row), delta); - - for (int row = 0; row < r.rowSize(); row++) - for (int col = 0; col < row - 1; col++) - assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", - 0d, r.get(row, col), delta); - - Matrix recomposed = qSafeCp.times(r); - - for (int row = 0; row < m.rowSize(); row++) - for (int col = 0; col < m.columnSize(); col++) - assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", - m.get(row, col), recomposed.get(row, col), delta); - - Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10)); - assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize()); - assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", - 0d, sol.get(row, col), delta); - - dec.destroy(); - - QRDecomposition dec1 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d}, - {-1.0d, 2.0d}, - {0.0d, -1.0d} - })); - - assertTrue("Unexpected value for full rank in decomposition " + dec1, dec1.hasFullRank()); - - dec1.destroy(); - - QRDecomposition dec2 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d, 0.0d}, - {0.0d, -1.0d, 2.0d, 0.0d} - })); - - assertTrue("Unexpected value for full rank in decomposition " + dec2, dec2.hasFullRank()); - - dec2.destroy(); - } - - /** */ - private Matrix safeCopy(Matrix orig) { - return new DenseLocalOnHeapMatrix(orig.rowSize(), orig.columnSize()).assign(orig); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java deleted file mode 100644 index 00e83d85716a9..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java +++ /dev/null @@ -1,122 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** */ -public class SingularValueDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - basicTest(new PivotedMatrixView(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }))); - } - - /** */ - @Test - public void rowsLessThanColumnsTest() { - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d} - }); - - SingularValueDecomposition dec = new SingularValueDecomposition(m); - assertEquals("Unexpected value for singular values size.", - 2, dec.getSingularValues().length); - - Matrix s = dec.getS(); - Matrix u = dec.getU(); - Matrix v = dec.getV(); - Matrix covariance = dec.getCovariance(0.5); - - assertNotNull("Matrix s is expected to be not null.", s); - assertNotNull("Matrix u is expected to be not null.", u); - assertNotNull("Matrix v is expected to be not null.", v); - assertNotNull("Covariance matrix is expected to be not null.", covariance); - - dec.destroy(); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new SingularValueDecomposition(null); - } - - /** */ - private void basicTest(Matrix m) { - SingularValueDecomposition dec = new SingularValueDecomposition(m); - assertEquals("Unexpected value for singular values size.", - 3, dec.getSingularValues().length); - - Matrix s = dec.getS(); - Matrix u = dec.getU(); - Matrix v = dec.getV(); - Matrix covariance = dec.getCovariance(0.5); - - assertNotNull("Matrix s is expected to be not null.", s); - assertNotNull("Matrix u is expected to be not null.", u); - assertNotNull("Matrix v is expected to be not null.", v); - assertNotNull("Covariance matrix is expected to be not null.", covariance); - - assertTrue("Decomposition cond is expected to be positive.", dec.cond() > 0); - assertTrue("Decomposition norm2 is expected to be positive.", dec.norm2() > 0); - assertEquals("Decomposition rank differs from expected.", 3, dec.rank()); - assertEquals("Decomposition singular values size differs from expected.", - 3, dec.getSingularValues().length); - - Matrix recomposed = (u.times(s).times(v.transpose())); - - for (int row = 0; row < m.rowSize(); row++) - for (int col = 0; col < m.columnSize(); col++) - assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", - m.get(row, col), recomposed.get(row, col), 0.001); - - for (int row = 0; row < covariance.rowSize(); row++) - for (int col = row + 1; col < covariance.columnSize(); col++) - assertEquals("Unexpected covariance matrix value at (" + row + "," + col + ").", - covariance.get(row, col), covariance.get(col, row), 0.001); - - dec.destroy(); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrixTest.java deleted file mode 100644 index c6f6f867570fd..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrixTest.java +++ /dev/null @@ -1,371 +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.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.ExternalizeTest; -import org.apache.ignite.ml.math.IdentityValueMapper; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.distributed.MatrixKeyMapper; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -/** - * Tests for {@link CacheMatrix}. - */ -@GridCommonTest(group = "Distributed Models") -public class CacheMatrixTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - /** Cache name. */ - private static final String CACHE_NAME = "test-cache"; - /** */ - private static final String UNEXPECTED_ATTRIBUTE_VALUE = "Unexpected attribute value."; - /** Grid instance. */ - private Ignite ignite; - /** Matrix rows */ - private final int rows = MathTestConstants.STORAGE_SIZE; - /** Matrix cols */ - private final int cols = MathTestConstants.STORAGE_SIZE; - - /** - * Default constructor. - */ - public CacheMatrixTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * {@inheritDoc} - */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - ignite.destroyCache(CACHE_NAME); - } - - /** */ - public void testGetSet() throws Exception { - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - double v = Math.random(); - cacheMatrix.set(i, j, v); - - assert Double.compare(v, cacheMatrix.get(i, j)) == 0; - assert Double.compare(v, cache.get(keyMapper.apply(i, j))) == 0; - } - } - } - - /** */ - public void testCopy() throws Exception { - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - fillMatrix(cacheMatrix); - - try { - cacheMatrix.copy(); - - fail("UnsupportedOperationException expected"); - } - catch (UnsupportedOperationException e) { - // No-op. - } - } - - /** */ - public void testLike() throws Exception { - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - try { - cacheMatrix.like(rows, cols); - - fail("UnsupportedOperationException expected"); - } - catch (UnsupportedOperationException e) { - // No-op. - } - } - - /** */ - public void testLikeVector() throws Exception { - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - try { - cacheMatrix.likeVector(cols); - - fail("UnsupportedOperationException expected"); - } - catch (UnsupportedOperationException e) { - // No-op. - } - } - - /** */ - public void testPlus() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - double plusVal = 2; - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - initMatrix(cacheMatrix); - - cacheMatrix.plus(plusVal); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - assertEquals(plusVal, cacheMatrix.get(i, j)); - } - - /** */ - public void testDivide() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - double initVal = 1; - double divVal = 2; - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - initMatrix(cacheMatrix); - cacheMatrix.assign(initVal); - cacheMatrix.divide(divVal); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - assertTrue(Double.compare(cacheMatrix.get(i, j), initVal / divVal) == 0); - } - - /** */ - public void testTimes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - double initVal = 1; - double timVal = 2; - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - initMatrix(cacheMatrix); - cacheMatrix.assign(initVal); - cacheMatrix.times(timVal); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - assertTrue(Double.compare(cacheMatrix.get(i, j), initVal * timVal) == 0); - } - - /** */ - public void testSum() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - double sum; - - initMatrix(cacheMatrix); - sum = cacheMatrix.sum(); - - assertTrue(Double.compare(sum, 0d) == 0); - - cacheMatrix.assign(1d); - sum = cacheMatrix.sum(); - - assertTrue(Double.compare(sum, rows * cols) == 0); - } - - /** */ - public void testAssignSingleValue() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - double initVal = 1; - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - initMatrix(cacheMatrix); - - cacheMatrix.assign(initVal); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - assertEquals(initVal, cacheMatrix.get(i, j)); - } - - /** */ - public void testAssignArray() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - double[][] initVal = new double[rows][cols]; - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - initVal[i][j] = Math.random(); - - cacheMatrix.assign(initVal); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - assertEquals(initVal[i][j], cacheMatrix.get(i, j)); - } - - /** */ - public void testAttributes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, cacheMatrix.isSequentialAccess()); - assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, cacheMatrix.isDense()); - assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, cacheMatrix.isArrayBased()); - assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, cacheMatrix.isRandomAccess()); - assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, cacheMatrix.isDistributed()); - } - - /** */ - public void testExternalization() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - final CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - ExternalizeTest> externalizeTest = new ExternalizeTest>() { - /** {@inheritDoc} */ - @Override public void externalizeTest() { - super.externalizeTest(cacheMatrix); - } - }; - - externalizeTest.externalizeTest(); - } - - /** */ - public void testMinMax() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - final CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - cacheMatrix.set(i, j, i * rows + j); - - assertEquals(0.0, cacheMatrix.minValue(), 0.0); - assertEquals(rows * cols - 1, cacheMatrix.maxValue(), 0.0); - } - - /** */ - public void testMap() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - MatrixKeyMapper keyMapper = getKeyMapper(rows, cols); - IgniteCache cache = getCache(); - final CacheMatrix cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - initMatrix(cacheMatrix); - - cacheMatrix.map(value -> value + 10); - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - assertEquals(10.0, cacheMatrix.getX(i, j), 0.0); - } - - /** */ - private IgniteCache getCache() { - assert ignite != null; - - CacheConfiguration cfg = new CacheConfiguration(); - cfg.setName(CACHE_NAME); - - IgniteCache cache = ignite.getOrCreateCache(CACHE_NAME); - - assert cache != null; - return cache; - } - - /** */ - private MatrixKeyMapper getKeyMapper(final int rows, final int cols) { - return new MatrixKeyMapperForTests(rows, cols); - } - - /** - * Init the given matrix by random values. - */ - private void fillMatrix(Matrix m) { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - m.set(i, j, Math.random()); - } - - /** - * Init the given matrix by zeros. - */ - private void initMatrix(Matrix m) { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - m.set(i, j, 0d); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrixTest.java deleted file mode 100644 index a00403f11cc2b..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrixTest.java +++ /dev/null @@ -1,209 +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.ExternalizeTest; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * Tests for {@link DiagonalMatrix}. - */ -public class DiagonalMatrixTest extends ExternalizeTest { - /** */ - public static final String UNEXPECTED_VALUE = "Unexpected value"; - - /** */ - private DiagonalMatrix testMatrix; - - /** */ - @Before - public void setup() { - DenseLocalOnHeapMatrix parent = new DenseLocalOnHeapMatrix(MathTestConstants.STORAGE_SIZE, MathTestConstants.STORAGE_SIZE); - fillMatrix(parent); - testMatrix = new DiagonalMatrix(parent); - } - - /** {@inheritDoc} */ - @Override public void externalizeTest() { - externalizeTest(testMatrix); - } - - /** */ - @Test - public void testSetGetBasic() { - double testVal = 42; - for (int i = 0; i < MathTestConstants.STORAGE_SIZE; i++) { - testMatrix.set(i, i, testVal); - - assertEquals(UNEXPECTED_VALUE + " at (" + i + "," + i + ")", testMatrix.get(i, i), testVal, 0d); - } - - //noinspection EqualsWithItself - assertTrue("Matrix is expected to be equal to self.", testMatrix.equals(testMatrix)); - //noinspection ObjectEqualsNull - assertFalse("Matrix is expected to be not equal to null.", testMatrix.equals(null)); - } - - /** */ - @Test - public void testSetGet() { - verifyDiagonal(testMatrix); - - final int size = MathTestConstants.STORAGE_SIZE; - - for (Matrix m : new Matrix[] { - new DenseLocalOnHeapMatrix(size + 1, size), - new DenseLocalOnHeapMatrix(size, size + 1)}) { - fillMatrix(m); - - verifyDiagonal(new DiagonalMatrix(m)); - } - - final double[] data = new double[size]; - - for (int i = 0; i < size; i++) - data[i] = 1 + i; - - final Matrix m = new DiagonalMatrix(new DenseLocalOnHeapVector(data)); - - assertEquals("Rows in matrix constructed from vector", size, m.rowSize()); - assertEquals("Cols in matrix constructed from vector", size, m.columnSize()); - - for (int i = 0; i < size; i++) - assertEquals(UNEXPECTED_VALUE + " at vector index " + i, data[i], m.get(i, i), 0d); - - verifyDiagonal(m); - - final Matrix m1 = new DiagonalMatrix(data); - - assertEquals("Rows in matrix constructed from array", size, m1.rowSize()); - assertEquals("Cols in matrix constructed from array", size, m1.columnSize()); - - for (int i = 0; i < size; i++) - assertEquals(UNEXPECTED_VALUE + " at array index " + i, data[i], m1.get(i, i), 0d); - - verifyDiagonal(m1); - } - - /** */ - @Test - public void testConstant() { - final int size = MathTestConstants.STORAGE_SIZE; - - for (double val : new double[] {-1.0, 0.0, 1.0}) { - Matrix m = new DiagonalMatrix(size, val); - - assertEquals("Rows in matrix", size, m.rowSize()); - assertEquals("Cols in matrix", size, m.columnSize()); - - for (int i = 0; i < size; i++) - assertEquals(UNEXPECTED_VALUE + " at index " + i, val, m.get(i, i), 0d); - - verifyDiagonal(m, true); - } - } - - /** */ - @Test - public void testAttributes() { - assertTrue(UNEXPECTED_VALUE, testMatrix.rowSize() == MathTestConstants.STORAGE_SIZE); - assertTrue(UNEXPECTED_VALUE, testMatrix.columnSize() == MathTestConstants.STORAGE_SIZE); - - assertFalse(UNEXPECTED_VALUE, testMatrix.isArrayBased()); - assertTrue(UNEXPECTED_VALUE, testMatrix.isDense()); - assertFalse(UNEXPECTED_VALUE, testMatrix.isDistributed()); - - assertEquals(UNEXPECTED_VALUE, testMatrix.isRandomAccess(), !testMatrix.isSequentialAccess()); - assertTrue(UNEXPECTED_VALUE, testMatrix.isRandomAccess()); - } - - /** */ - @Test - public void testNullParams() { - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new DiagonalMatrix((Matrix)null), "Null Matrix parameter"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new DiagonalMatrix((Vector)null), "Null Vector parameter"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new DiagonalMatrix((double[])null), "Null double[] parameter"); - } - - /** */ - private void verifyDiagonal(Matrix m, boolean readonly) { - final int rows = m.rowSize(), cols = m.columnSize(); - - final String sizeDetails = "rows" + "X" + "cols " + rows + "X" + cols; - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) { - final String details = " at (" + i + "," + j + "), " + sizeDetails; - - final boolean diagonal = i == j; - - final double old = m.get(i, j); - - if (!diagonal) - assertEquals(UNEXPECTED_VALUE + details, 0, old, 0d); - - final double exp = diagonal && !readonly ? old + 1 : old; - - boolean expECaught = false; - - try { - m.set(i, j, exp); - } - catch (UnsupportedOperationException uoe) { - if (diagonal && !readonly) - throw uoe; - - expECaught = true; - } - - if ((!diagonal || readonly) && !expECaught) - fail("Expected exception was not caught " + details); - - assertEquals(UNEXPECTED_VALUE + details, exp, m.get(i, j), 0d); - } - } - - /** */ - private void verifyDiagonal(Matrix m) { - verifyDiagonal(m, false); - } - - /** */ - private void fillMatrix(Matrix m) { - final int rows = m.rowSize(), cols = m.columnSize(); - - boolean negative = false; - - for (int i = 0; i < rows; i++) - for (int j = 0; j < cols; j++) - m.set(i, j, (negative = !negative) ? -(i * cols + j + 1) : i * cols + j + 1); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrixConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrixConstructorTest.java deleted file mode 100644 index 25de7d386060a..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrixConstructorTest.java +++ /dev/null @@ -1,113 +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.exceptions.UnsupportedOperationException; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** */ -public class FunctionMatrixConstructorTest { - /** */ - @Test - public void invalidArgsTest() { - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new FunctionMatrix(0, 1, (i, j) -> 0.0), - "Invalid row parameter."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new FunctionMatrix(1, 0, (i, j) -> 0.0), - "Invalid col parameter."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new FunctionMatrix(1, 1, null), - "Invalid func parameter."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new FunctionMatrix(0, 1, (i, j) -> 0.0, null), - "Invalid row parameter, with setter func."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new FunctionMatrix(1, 0, (i, j) -> 0.0, null), - "Invalid col parameter, with setter func."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new FunctionMatrix(1, 1, null, null), - "Invalid func parameter, with setter func."); - } - - /** */ - @Test - public void basicTest() { - for (int rows : new int[] {1, 2, 3}) - for (int cols : new int[] {1, 2, 3}) - basicTest(rows, cols); - - Matrix m = new FunctionMatrix(1, 1, (i, j) -> 1d); - //noinspection EqualsWithItself - assertTrue("Matrix is expected to be equal to self.", m.equals(m)); - //noinspection ObjectEqualsNull - assertFalse("Matrix is expected to be not equal to null.", m.equals(null)); - } - - /** */ - private void basicTest(int rows, int cols) { - double[][] data = new double[rows][cols]; - - for (int row = 0; row < rows; row++) - for (int col = 0; col < cols; col++) - data[row][col] = row * cols + row; - - Matrix mReadOnly = new FunctionMatrix(rows, cols, (i, j) -> data[i][j]); - - assertEquals("Rows in matrix.", rows, mReadOnly.rowSize()); - assertEquals("Cols in matrix.", cols, mReadOnly.columnSize()); - - for (int row = 0; row < rows; row++) - for (int col = 0; col < cols; col++) { - assertEquals("Unexpected value at " + row + "x" + col, data[row][col], mReadOnly.get(row, col), 0d); - - boolean expECaught = false; - - try { - mReadOnly.set(row, col, 0.0); - } - catch (UnsupportedOperationException uoe) { - expECaught = true; - } - - assertTrue("Expected exception wasn't thrown at " + row + "x" + col, expECaught); - } - - Matrix m = new FunctionMatrix(rows, cols, (i, j) -> data[i][j], (i, j, val) -> data[i][j] = val); - - assertEquals("Rows in matrix, with setter function.", rows, m.rowSize()); - assertEquals("Cols in matrix, with setter function.", cols, m.columnSize()); - - for (int row = 0; row < rows; row++) - for (int col = 0; col < cols; col++) { - assertEquals("Unexpected value at " + row + "x" + col, data[row][col], m.get(row, col), 0d); - - m.set(row, col, -data[row][col]); - } - - for (int row = 0; row < rows; row++) - for (int col = 0; col < cols; col++) - assertEquals("Unexpected value set at " + row + "x" + col, -(row * cols + row), m.get(row, col), 0d); - - assertTrue("Incorrect copy for empty matrix.", m.copy().equals(m)); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationFixtures.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationFixtures.java index 52a0077d4ae1e..e0800ccd2a60e 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationFixtures.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationFixtures.java @@ -17,9 +17,6 @@ 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.Iterator; import java.util.List; @@ -28,7 +25,6 @@ import java.util.function.Function; import java.util.function.Supplier; import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.storage.matrix.FunctionMatrixStorage; import org.jetbrains.annotations.NotNull; /** */ @@ -37,13 +33,8 @@ class MatrixImplementationFixtures { private static final List>> suppliers = Arrays.asList( (Supplier>)DenseLocalOnHeapMatrixFixture::new, (Supplier>)DenseLocalOffHeapMatrixFixture::new, - (Supplier>)RandomMatrixFixture::new, (Supplier>)SparseLocalOnHeapMatrixFixture::new, - (Supplier>)PivotedMatrixViewFixture::new, - (Supplier>)MatrixViewFixture::new, - (Supplier>)FunctionMatrixFixture::new, - (Supplier>)DiagonalMatrixFixture::new, - (Supplier>)TransposedMatrixViewFixture::new + (Supplier>)MatrixViewFixture::new ); /** */ @@ -75,14 +66,6 @@ private static class DenseLocalOffHeapMatrixFixture extends MatrixSizeIterator { } } - /** */ - private static class RandomMatrixFixture extends MatrixSizeIterator { - /** */ - RandomMatrixFixture() { - super(RandomMatrix::new, "RandomMatrix"); - } - } - /** */ private static class SparseLocalOnHeapMatrixFixture extends MatrixSizeIterator { /** */ @@ -91,14 +74,6 @@ private static class SparseLocalOnHeapMatrixFixture extends MatrixSizeIterator { } } - /** */ - private static class PivotedMatrixViewFixture extends WrapperMatrixIterator { - /** */ - PivotedMatrixViewFixture() { - super(PivotedMatrixView::new, "PivotedMatrixView over DenseLocalOnHeapMatrix"); - } - } - /** */ private static class MatrixViewFixture extends WrapperMatrixIterator { /** */ @@ -108,52 +83,6 @@ private static class MatrixViewFixture extends WrapperMatrixIterator { } } - /** */ - private static class FunctionMatrixFixture extends WrapperMatrixIterator { - /** */ - FunctionMatrixFixture() { - super(FunctionMatrixForTest::new, "FunctionMatrix wrapping DenseLocalOnHeapMatrix"); - } - } - - /** */ - private static class DiagonalMatrixFixture extends DiagonalIterator { - /** */ - DiagonalMatrixFixture() { - super(DenseLocalOnHeapMatrix::new, "DiagonalMatrix over DenseLocalOnHeapMatrix"); - } - - /** {@inheritDoc} */ - @NotNull - @Override public Iterator iterator() { - return new Iterator() { - /** {@inheritDoc} */ - @Override public boolean hasNext() { - return hasNextSize(getSizeIdx()); - } - - /** {@inheritDoc} */ - @Override public Matrix next() { - assert getSize(getSizeIdx()) == 1 : "Only size 1 allowed for diagonal matrix fixture."; - - Matrix matrix = getConstructor().apply(getSize(getSizeIdx()), getSize(getSizeIdx())); - - nextIdx(); - - return new DiagonalMatrix(matrix); - } - }; - } - } - - /** */ - private static class TransposedMatrixViewFixture extends WrapperMatrixIterator { - /** */ - TransposedMatrixViewFixture() { - super(TransposedMatrixView::new, "TransposedMatrixView over DenseLocalOnHeapMatrix"); - } - } - /** */ private static abstract class DiagonalIterator implements Iterable { /** */ @@ -316,66 +245,4 @@ void nextIdx() { sizeIdx++; } } - - /** Subclass tweaked for serialization */ - private static class FunctionMatrixForTest extends FunctionMatrix { - /** */ - Matrix underlying; - - /** */ - public FunctionMatrixForTest() { - // No-op. - } - - /** */ - FunctionMatrixForTest(Matrix underlying) { - super(underlying.rowSize(), underlying.columnSize(), underlying::get, underlying::set); - - this.underlying = underlying; - } - - /** {@inheritDoc} */ - @Override public Matrix copy() { - return new FunctionMatrixForTest(underlying); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeObject(underlying); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - underlying = (Matrix)in.readObject(); - - setStorage(new FunctionMatrixStorage(underlying.rowSize(), underlying.columnSize(), - underlying::get, underlying::set)); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + underlying.hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - FunctionMatrixForTest that = (FunctionMatrixForTest)o; - - return underlying != null ? underlying.equals(that.underlying) : that.underlying == null; - } - } } diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationsTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationsTest.java index dc8ff12a0d077..00f37ba35c0ba 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationsTest.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationsTest.java @@ -33,7 +33,6 @@ import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; import org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.impls.vector.RandomVector; import org.apache.ignite.ml.math.impls.vector.SparseLocalVector; import org.junit.Test; @@ -101,8 +100,7 @@ public void testCopy() { Matrix cp = m.copy(); assertTrue("Incorrect copy for empty matrix " + desc, cp.equals(m)); - if (!readOnly(m)) - fillMatrix(m); + fillMatrix(m); cp = m.copy(); @@ -112,11 +110,11 @@ public void testCopy() { /** */ @Test - public void testHaveLikeVector() throws InstantiationException, IllegalAccessException { + public void testHaveLikeVector() { for (Class key : likeVectorTypesMap().keySet()) { Class val = likeVectorTypesMap().get(key); - if (val == null && !ignore(key)) + if (val == null) System.out.println("Missing test for implementation of likeMatrix for " + key.getSimpleName()); } } @@ -209,9 +207,6 @@ public void testAssignFunction() { @Test public void testPlus() { consumeSampleMatrix((m, desc) -> { - if (readOnly(m)) - return; - double[][] data = fillAndReturn(m); double plusVal = Math.random(); @@ -265,9 +260,6 @@ public void testMinusMatrix() { @Test public void testTimes() { consumeSampleMatrix((m, desc) -> { - if (readOnly(m)) - return; - double[][] data = fillAndReturn(m); double timeVal = Math.random(); @@ -388,99 +380,6 @@ public void testTranspose() { }); } - /** */ - @Test - public void testDeterminant() { - consumeSampleMatrix((m, desc) -> { - if (m.rowSize() != m.columnSize()) - return; - - if (ignore(m.getClass())) - return; - - double[][] doubles = fillIntAndReturn(m); - - if (m.rowSize() == 1) { - assertEquals("Unexpected value " + desc, m.determinant(), doubles[0][0], 0d); - - return; - } - - if (m.rowSize() == 2) { - double det = doubles[0][0] * doubles[1][1] - doubles[0][1] * doubles[1][0]; - assertEquals("Unexpected value " + desc, m.determinant(), det, 0d); - - return; - } - - if (m.rowSize() > 512) - return; // IMPL NOTE if row size >= 30000 it takes unacceptably long for normal test run. - - Matrix diagMtx = m.like(m.rowSize(), m.columnSize()); - - diagMtx.assign(0); - for (int i = 0; i < m.rowSize(); i++) - diagMtx.set(i, i, m.get(i, i)); - - double det = 1; - - for (int i = 0; i < diagMtx.rowSize(); i++) - det *= diagMtx.get(i, i); - - try { - assertEquals("Unexpected value " + desc, det, diagMtx.determinant(), DEFAULT_DELTA); - } - catch (Exception e) { - System.out.println(desc); - throw e; - } - }); - } - - /** */ - @Test - public void testInverse() { - consumeSampleMatrix((m, desc) -> { - if (m.rowSize() != m.columnSize()) - return; - - if (ignore(m.getClass())) - return; - - if (m.rowSize() > 256) - return; // IMPL NOTE this is for quicker test run. - - fillNonSingularMatrix(m); - - assertTrue("Unexpected zero determinant " + desc, Math.abs(m.determinant()) > 0d); - - Matrix inverse = m.inverse(); - - Matrix mult = m.times(inverse); - - final double delta = 0.001d; - - assertEquals("Unexpected determinant " + desc, 1d, mult.determinant(), delta); - - assertEquals("Unexpected top left value " + desc, 1d, mult.get(0, 0), delta); - - if (m.rowSize() == 1) - return; - - assertEquals("Unexpected center value " + desc, - 1d, mult.get(m.rowSize() / 2, m.rowSize() / 2), delta); - - assertEquals("Unexpected bottom right value " + desc, - 1d, mult.get(m.rowSize() - 1, m.rowSize() - 1), delta); - - assertEquals("Unexpected top right value " + desc, - 0d, mult.get(0, m.rowSize() - 1), delta); - - assertEquals("Unexpected bottom left value " + desc, - 0d, mult.get(m.rowSize() - 1, 0), delta); - }); - } - /** */ @Test public void testMap() { @@ -525,8 +424,7 @@ public void testMapMatrix() { @Test public void testViewRow() { consumeSampleMatrix((m, desc) -> { - if (!readOnly(m)) - fillMatrix(m); + fillMatrix(m); for (int i = 0; i < m.rowSize(); i++) { Vector vector = m.viewRow(i); @@ -543,8 +441,7 @@ public void testViewRow() { @Test public void testViewCol() { consumeSampleMatrix((m, desc) -> { - if (!readOnly(m)) - fillMatrix(m); + fillMatrix(m); for (int i = 0; i < m.columnSize(); i++) { Vector vector = m.viewColumn(i); @@ -655,8 +552,7 @@ public void testMin() { @Test public void testGetElement() { consumeSampleMatrix((m, desc) -> { - if (!(readOnly(m))) - fillMatrix(m); + fillMatrix(m); for (int i = 0; i < m.rowSize(); i++) for (int j = 0; j < m.columnSize(); j++) { @@ -679,17 +575,7 @@ public void testGetElement() { e.set(newVal); } catch (UnsupportedOperationException uoe) { - if (!(readOnly(m))) - throw uoe; - - expECaught = true; - } - - if (readOnly(m)) { - if (!expECaught) - fail("Expected exception was not caught for " + details); - - continue; + throw uoe; } assertEquals("Unexpected value set for " + details, newVal, m.get(i, j), 0d); @@ -701,8 +587,7 @@ public void testGetElement() { @Test public void testGetX() { consumeSampleMatrix((m, desc) -> { - if (!(readOnly(m))) - fillMatrix(m); + fillMatrix(m); for (int i = 0; i < m.rowSize(); i++) for (int j = 0; j < m.columnSize(); j++) @@ -727,9 +612,6 @@ public void testGuid() { @Test public void testSwapRows() { consumeSampleMatrix((m, desc) -> { - if (readOnly(m)) - return; - double[][] doubles = fillAndReturn(m); final int swap_i = m.rowSize() == 1 ? 0 : 1; @@ -756,9 +638,6 @@ public void testSwapRows() { @Test public void testSwapColumns() { consumeSampleMatrix((m, desc) -> { - if (readOnly(m)) - return; - double[][] doubles = fillAndReturn(m); final int swap_i = m.columnSize() == 1 ? 0 : 1; @@ -861,8 +740,7 @@ public void testViewPart() { @Test public void testDensity() { consumeSampleMatrix((m, desc) -> { - if (!readOnly(m)) - fillMatrix(m); + fillMatrix(m); assertTrue("Unexpected density with threshold 0 for " + desc, m.density(0.0)); @@ -874,8 +752,7 @@ public void testDensity() { @Test public void testMaxAbsRowSumNorm() { consumeSampleMatrix((m, desc) -> { - if (!readOnly(m)) - fillMatrix(m); + fillMatrix(m); assertEquals("Unexpected value for " + desc, maxAbsRowSumNorm(m), m.maxAbsRowSumNorm(), 0d); @@ -930,10 +807,9 @@ public void testAssignColumn() { @Test public void testGetRowCol() { consumeSampleMatrix((m, desc) -> { - if (!(m instanceof RandomMatrix)) - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - m.setX(i, j, i + j); + for (int i = 0; i < m.rowSize(); i++) + for (int j = 0; j < m.columnSize(); j++) + m.setX(i, j, i + j); for (int i = 0; i < m.rowSize(); i++) assertNotNull("Unexpected value for " + desc + " at row " + i, m.getRow(i)); @@ -1023,28 +899,16 @@ private void testInvalidCardinality(Supplier supplier, String desc) { fail("Expected exception was not caught for " + desc); } - /** */ - private boolean readOnly(Matrix m) { - return m instanceof RandomMatrix; - } - /** */ private double[][] fillIntAndReturn(Matrix m) { double[][] data = new double[m.rowSize()][m.columnSize()]; - if (readOnly(m)) { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - data[i][j] = m.get(i, j); + for (int i = 0; i < m.rowSize(); i++) + for (int j = 0; j < m.columnSize(); j++) + data[i][j] = i * m.rowSize() + j + 1; - } - else { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - data[i][j] = i * m.rowSize() + j + 1; + m.assign(data); - m.assign(data); - } return data; } @@ -1052,19 +916,11 @@ private double[][] fillIntAndReturn(Matrix m) { private double[][] fillAndReturn(Matrix m) { double[][] data = new double[m.rowSize()][m.columnSize()]; - if (readOnly(m)) { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - data[i][j] = m.get(i, j); - - } - else { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - data[i][j] = -0.5d + Math.random(); + for (int i = 0; i < m.rowSize(); i++) + for (int j = 0; j < m.columnSize(); j++) + data[i][j] = -0.5d + Math.random(); - m.assign(data); - } + m.assign(data); return data; } @@ -1088,8 +944,7 @@ private void fillMatrix(Matrix m) { /** Ignore test for given matrix type. */ private boolean ignore(Class clazz) { - List> ignoredClasses = Arrays.asList(RandomMatrix.class, PivotedMatrixView.class, - MatrixView.class, FunctionMatrix.class, TransposedMatrixView.class); + List> ignoredClasses = Arrays.asList(MatrixView.class); for (Class ignoredClass : ignoredClasses) if (ignoredClass.isAssignableFrom(clazz)) @@ -1112,10 +967,7 @@ private static Map, Class> likeVectorT return new LinkedHashMap, Class>() {{ put(DenseLocalOnHeapMatrix.class, DenseLocalOnHeapVector.class); put(DenseLocalOffHeapMatrix.class, DenseLocalOffHeapVector.class); - put(RandomMatrix.class, RandomVector.class); put(SparseLocalOnHeapMatrix.class, SparseLocalVector.class); - put(DenseLocalOnHeapMatrix.class, DenseLocalOnHeapVector.class); - put(DiagonalMatrix.class, DenseLocalOnHeapVector.class); // IMPL NOTE per fixture // IMPL NOTE check for presence of all implementations here will be done in testHaveLikeMatrix via Fixture }}; } @@ -1125,11 +977,7 @@ private static Map, Class> likeTypesMa return new LinkedHashMap, Class>() {{ put(DenseLocalOnHeapMatrix.class, DenseLocalOnHeapMatrix.class); put(DenseLocalOffHeapMatrix.class, DenseLocalOffHeapMatrix.class); - put(RandomMatrix.class, RandomMatrix.class); put(SparseLocalOnHeapMatrix.class, SparseLocalOnHeapMatrix.class); - put(DenseLocalOnHeapMatrix.class, DenseLocalOnHeapMatrix.class); - put(DiagonalMatrix.class, DenseLocalOnHeapMatrix.class); // IMPL NOTE per fixture - put(FunctionMatrix.class, FunctionMatrix.class); // IMPL NOTE check for presence of all implementations here will be done in testHaveLikeMatrix via Fixture }}; } diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixViewConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixViewConstructorTest.java deleted file mode 100644 index 87bf3addeb58b..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixViewConstructorTest.java +++ /dev/null @@ -1,129 +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.util.Arrays; -import org.apache.ignite.ml.math.Matrix; -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** */ -public class PivotedMatrixViewConstructorTest { - /** */ - @Test - public void invalidArgsTest() { - Matrix m = new DenseLocalOnHeapMatrix(1, 1); - - int[] pivot = new int[] {0}; - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new PivotedMatrixView(null), - "Null parent matrix."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new PivotedMatrixView(null, pivot), - "Null parent matrix, with pivot."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new PivotedMatrixView(m, null), - "Null pivot."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new PivotedMatrixView(m, null, pivot), - "Null row pivot."); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new PivotedMatrixView(m, pivot, null), - "Null col pivot."); - } - - /** */ - @Test - public void basicTest() { - Matrix m = new DenseLocalOnHeapMatrix(2, 2); - - int[] pivot = new int[] {0, 1}; - - PivotedMatrixView view = new PivotedMatrixView(m, pivot); - - assertEquals("Rows in view.", m.rowSize(), view.rowSize()); - assertEquals("Cols in view.", m.columnSize(), view.columnSize()); - - assertTrue("Row pivot array in view.", Arrays.equals(pivot, view.rowPivot())); - assertTrue("Col pivot array in view.", Arrays.equals(pivot, view.columnPivot())); - - Assert.assertEquals("Base matrix in view.", m, view.getBaseMatrix()); - - assertEquals("Row pivot value in view.", 0, view.rowPivot(0)); - assertEquals("Col pivot value in view.", 0, view.columnPivot(0)); - - assertEquals("Row unpivot value in view.", 0, view.rowUnpivot(0)); - assertEquals("Col unpivot value in view.", 0, view.columnUnpivot(0)); - - Matrix swap = view.swap(1, 1); - - for (int row = 0; row < view.rowSize(); row++) - for (int col = 0; col < view.columnSize(); col++) - assertEquals("Unexpected swap value set at (" + row + "," + col + ").", - view.get(row, col), swap.get(row, col), 0d); - - //noinspection EqualsWithItself - assertTrue("View is expected to be equal to self.", view.equals(view)); - //noinspection ObjectEqualsNull - assertFalse("View is expected to be not equal to null.", view.equals(null)); - } - - /** */ - @Test - public void pivotTest() { - int[] pivot = new int[] {2, 1, 0, 3}; - - for (Matrix m : new Matrix[] { - new DenseLocalOnHeapMatrix(3, 3), - new DenseLocalOnHeapMatrix(3, 4), new DenseLocalOnHeapMatrix(4, 3)}) - pivotTest(m, pivot); - } - - /** */ - private void pivotTest(Matrix parent, int[] pivot) { - for (int row = 0; row < parent.rowSize(); row++) - for (int col = 0; col < parent.columnSize(); col++) - parent.set(row, col, row * parent.columnSize() + col + 1); - - Matrix view = new PivotedMatrixView(parent, pivot); - - int rows = parent.rowSize(); - int cols = parent.columnSize(); - - assertEquals("Rows in view.", rows, view.rowSize()); - assertEquals("Cols in view.", cols, view.columnSize()); - - for (int row = 0; row < rows; row++) - for (int col = 0; col < cols; col++) - assertEquals("Unexpected value at " + row + "x" + col, - parent.get(pivot[row], pivot[col]), view.get(row, col), 0d); - - int min = rows < cols ? rows : cols; - - for (int idx = 0; idx < min; idx++) - view.set(idx, idx, 0d); - - for (int idx = 0; idx < min; idx++) - assertEquals("Unexpected value set at " + idx, - 0d, parent.get(pivot[idx], pivot[idx]), 0d); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java deleted file mode 100644 index 20dda2aaa0a75..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java +++ /dev/null @@ -1,408 +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.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Collection; -import java.util.Set; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.DistributedStorage; -import org.apache.ignite.ml.math.distributed.keys.impl.MatrixBlockKey; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.ml.math.impls.storage.matrix.BlockMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVector; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -import static org.apache.ignite.ml.math.impls.MathTestConstants.UNEXPECTED_VAL; - -/** - * Tests for {@link SparseBlockDistributedMatrix}. - */ -@GridCommonTest(group = "Distributed Models") -public class SparseDistributedBlockMatrixTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - - /** Precision. */ - private static final double PRECISION = 0.0; - - /** Grid instance. */ - private Ignite ignite; - - /** Matrix rows */ - private final int rows = MathTestConstants.STORAGE_SIZE; - - /** Matrix cols */ - private final int cols = MathTestConstants.STORAGE_SIZE; - - /** Matrix for tests */ - private SparseBlockDistributedMatrix cacheMatrix; - - /** - * Default constructor. - */ - public SparseDistributedBlockMatrixTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * {@inheritDoc} - */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - if (cacheMatrix != null) { - cacheMatrix.destroy(); - cacheMatrix = null; - } - } - - /** */ - public void testGetSet() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - double v = Math.random(); - cacheMatrix.set(i, j, v); - - assertEquals("Unexpected value for matrix element[" + i + " " + j + "]", v, cacheMatrix.get(i, j), PRECISION); - } - } - } - - /** */ - public void testExternalize() throws IOException, ClassNotFoundException { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - - cacheMatrix.set(1, 1, 1.0); - - ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objOutputStream = new ObjectOutputStream(byteArrOutputStream); - - objOutputStream.writeObject(cacheMatrix); - - ByteArrayInputStream byteArrInputStream = new ByteArrayInputStream(byteArrOutputStream.toByteArray()); - ObjectInputStream objInputStream = new ObjectInputStream(byteArrInputStream); - - SparseBlockDistributedMatrix objRestored = (SparseBlockDistributedMatrix)objInputStream.readObject(); - - assertTrue(MathTestConstants.VAL_NOT_EQUALS, cacheMatrix.equals(objRestored)); - assertEquals(MathTestConstants.VAL_NOT_EQUALS, objRestored.get(1, 1), 1.0, PRECISION); - } - - /** Test simple math. */ - public void testMath() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - initMtx(cacheMatrix); - - cacheMatrix.assign(2.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 2.0, cacheMatrix.get(i, j), PRECISION); - - cacheMatrix.plus(3.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 5.0, cacheMatrix.get(i, j), PRECISION); - - cacheMatrix.times(2.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 10.0, cacheMatrix.get(i, j), PRECISION); - - cacheMatrix.divide(10.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 1.0, cacheMatrix.get(i, j), PRECISION); - - assertEquals(UNEXPECTED_VAL, cacheMatrix.rowSize() * cacheMatrix.columnSize(), cacheMatrix.sum(), PRECISION); - } - - /** */ - public void testMinMax() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - cacheMatrix.set(i, j, i * cols + j + 1); - - assertEquals(UNEXPECTED_VAL, 1.0, cacheMatrix.minValue(), PRECISION); - assertEquals(UNEXPECTED_VAL, rows * cols, cacheMatrix.maxValue(), PRECISION); - - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - cacheMatrix.set(i, j, -1.0 * (i * cols + j + 1)); - - assertEquals(UNEXPECTED_VAL, -rows * cols, cacheMatrix.minValue(), PRECISION); - assertEquals(UNEXPECTED_VAL, -1.0, cacheMatrix.maxValue(), PRECISION); - - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - cacheMatrix.set(i, j, i * cols + j); - - assertEquals(UNEXPECTED_VAL, 0.0, cacheMatrix.minValue(), PRECISION); - assertEquals(UNEXPECTED_VAL, rows * cols - 1.0, cacheMatrix.maxValue(), PRECISION); - } - - /** */ - public void testMap() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - initMtx(cacheMatrix); - - cacheMatrix.map(i -> 100.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 100.0, cacheMatrix.get(i, j), PRECISION); - } - - /** */ - public void testCopy() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - - cacheMatrix.set(rows - 1, cols - 1, 1); - - Matrix newMatrix = cacheMatrix.copy(); - assert newMatrix.columnSize() == cols; - assert newMatrix.rowSize() == rows; - assert newMatrix.get(rows - 1, cols - 1) == 1; - - } - - /** Test cache behaviour for matrix with different blocks */ - public void testCacheBehaviour() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - cacheBehaviorLogic(rows); - } - - /** Test cache behaviour for matrix with homogeneous blocks */ - public void testCacheBehaviourWithHomogeneousBlocks() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - int size = MatrixBlockEntry.MAX_BLOCK_SIZE * 3; - cacheBehaviorLogic(size); - } - - /** */ - private void cacheBehaviorLogic(int size) { - SparseBlockDistributedMatrix cacheMatrix1 = new SparseBlockDistributedMatrix(size, size); - SparseBlockDistributedMatrix cacheMatrix2 = new SparseBlockDistributedMatrix(size, size); - - initMtx(cacheMatrix1); - initMtx(cacheMatrix2); - - Collection cacheNames = ignite.cacheNames(); - - assert cacheNames.contains(((DistributedStorage)cacheMatrix1.getStorage()).cacheName()); - - IgniteCache cache = ignite.getOrCreateCache(((DistributedStorage)cacheMatrix1.getStorage()).cacheName()); - - Set keySet1 = buildKeySet(cacheMatrix1); - Set keySet2 = buildKeySet(cacheMatrix2); - - assert cache.containsKeys(keySet1); - assert cache.containsKeys(keySet2); - - cacheMatrix2.destroy(); - - assert cache.containsKeys(keySet1); - assert !cache.containsKeys(keySet2); - - cacheMatrix1.destroy(); - - assert !cache.containsKeys(keySet1); - } - - /** */ - public void testLike() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - - assertNotNull(cacheMatrix.like(1, 1)); - } - - /** */ - public void testLikeVector() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseBlockDistributedMatrix(rows, cols); - - Vector v = cacheMatrix.likeVector(1); - assert v.size() == 1; - assert v instanceof SparseBlockDistributedVector; - - } - - /** - * Simple test for two square matrices. - */ - public void testSquareMatrixTimes() { - squareMatrixTimesLogic(rows); - } - - /** - * Simple test for two square matrices with size which is proportional to MAX_BLOCK_SIZE constant - */ - public void testSquareMatrixTimesWithHomogeneousBlocks() { - int size = MatrixBlockEntry.MAX_BLOCK_SIZE * 3; - - squareMatrixTimesLogic(size); - } - - /** Build two square matrices, multiply them and check main diagonal elements */ - private void squareMatrixTimesLogic(int size) { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - Matrix cacheMatrix1 = new SparseBlockDistributedMatrix(size, size); - Matrix cacheMatrix2 = new SparseBlockDistributedMatrix(size, size); - - for (int i = 0; i < size; i++) { - cacheMatrix1.setX(i, i, i); - cacheMatrix2.setX(i, i, i); - } - - Matrix res = cacheMatrix1.times(cacheMatrix2); - - for (int i = 0; i < size; i++) - for (int j = 0; j < size; j++) - if (i == j) - assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, i * i, res.get(i, j), PRECISION); - else - assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, 0, res.get(i, j), PRECISION); - } - - /** - * - */ - public void testNonSquareMatrixTimes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - int size = MatrixBlockEntry.MAX_BLOCK_SIZE + 1; - int size2 = MatrixBlockEntry.MAX_BLOCK_SIZE * 2 + 1; - - Matrix cacheMatrix1 = new SparseBlockDistributedMatrix(size2, size); - Matrix cacheMatrix2 = new SparseBlockDistributedMatrix(size, size2); - - for (int i = 0; i < size; i++) { - cacheMatrix1.setX(i, i, i); - cacheMatrix2.setX(i, i, i); - } - - Matrix res = cacheMatrix1.times(cacheMatrix2); - - for (int i = 0; i < size; i++) - for (int j = 0; j < size; j++) - if (i == j) - assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, i * i, res.get(i, j), PRECISION); - else - assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, 0, res.get(i, j), PRECISION); - } - - /** - * - */ - public void testNonSquareMatrixTimes2() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - int size = MatrixBlockEntry.MAX_BLOCK_SIZE + 1; - int size2 = MatrixBlockEntry.MAX_BLOCK_SIZE * 2 + 1; - - Matrix cacheMatrix1 = new SparseBlockDistributedMatrix(size, size2); - Matrix cacheMatrix2 = new SparseBlockDistributedMatrix(size2, size); - - for (int i = 0; i < size; i++) { - cacheMatrix1.setX(i, i, i); - cacheMatrix2.setX(i, i, i); - } - - Matrix res = cacheMatrix1.times(cacheMatrix2); - - for (int i = 0; i < size; i++) - for (int j = 0; j < size; j++) - if (i == j) - assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, i * i, res.get(i, j), PRECISION); - else - assertEquals(UNEXPECTED_VAL + " for " + i + ":" + j, 0, res.get(i, j), PRECISION); - } - - /** */ - public void testMatrixVectorTimes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - SparseBlockDistributedMatrix a = new SparseBlockDistributedMatrix(new double[][] {{2.0, 4.0, 0.0}, {-2.0, 1.0, 3.0}, {-1.0, 0.0, 1.0}}); - SparseBlockDistributedVector b = new SparseBlockDistributedVector(new double[] {1.0, 2.0, -1.0}); - SparseBlockDistributedVector res = new SparseBlockDistributedVector(new double[] {10, -3.0, -2.0}); - - Vector calculatedRes = a.times(b); - - for (int i = 0; i < calculatedRes.size(); i++) - assertEquals(UNEXPECTED_VAL + " for " + i, res.get(i), calculatedRes.get(i), PRECISION); - } - - /** */ - private void initMtx(Matrix m) { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - m.set(i, j, 1.0); - } - - /** Build key set for SparseBlockDistributedMatrix. */ - private Set buildKeySet(SparseBlockDistributedMatrix m) { - - BlockMatrixStorage storage = (BlockMatrixStorage)m.getStorage(); - - return storage.getAllKeys(); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrixTest.java deleted file mode 100644 index 65f345358c837..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedMatrixTest.java +++ /dev/null @@ -1,316 +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.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Collection; -import java.util.Map; -import java.util.Set; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.DistributedStorage; -import org.apache.ignite.ml.math.distributed.keys.RowColMatrixKey; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -import static org.apache.ignite.ml.math.impls.MathTestConstants.UNEXPECTED_VAL; - -/** - * Tests for {@link SparseDistributedMatrix}. - */ -@GridCommonTest(group = "Distributed Models") -public class SparseDistributedMatrixTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - - /** Precision. */ - private static final double PRECISION = 0.0; - - /** */ - private static final int MATRIX_SIZE = 10; - - /** Grid instance. */ - private Ignite ignite; - - /** Matrix rows */ - private final int rows = MathTestConstants.STORAGE_SIZE; - - /** Matrix cols */ - private final int cols = MathTestConstants.STORAGE_SIZE; - - /** Matrix for tests */ - private SparseDistributedMatrix cacheMatrix; - - /** - * Default constructor. - */ - public SparseDistributedMatrixTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * {@inheritDoc} - */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - if (cacheMatrix != null) { - cacheMatrix.destroy(); - cacheMatrix = null; - } - } - - /** */ - public void testGetSet() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - double v = Math.random(); - cacheMatrix.set(i, j, v); - - assertEquals("Unexpected value for matrix element[" + i + " " + j + "]", v, cacheMatrix.get(i, j), PRECISION); - } - } - } - - /** */ - public void testExternalize() throws IOException, ClassNotFoundException { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - cacheMatrix.set(1, 1, 1.0); - - ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objOutputStream = new ObjectOutputStream(byteArrOutputStream); - - objOutputStream.writeObject(cacheMatrix); - - ByteArrayInputStream byteArrInputStream = new ByteArrayInputStream(byteArrOutputStream.toByteArray()); - ObjectInputStream objInputStream = new ObjectInputStream(byteArrInputStream); - - SparseDistributedMatrix objRestored = (SparseDistributedMatrix)objInputStream.readObject(); - - assertTrue(MathTestConstants.VAL_NOT_EQUALS, cacheMatrix.equals(objRestored)); - assertEquals(MathTestConstants.VAL_NOT_EQUALS, objRestored.get(1, 1), 1.0, PRECISION); - } - - /** Test simple math. */ - public void testMath() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - initMtx(cacheMatrix); - - cacheMatrix.assign(2.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 2.0, cacheMatrix.get(i, j), PRECISION); - - cacheMatrix.plus(3.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 5.0, cacheMatrix.get(i, j), PRECISION); - - cacheMatrix.times(2.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 10.0, cacheMatrix.get(i, j), PRECISION); - - cacheMatrix.divide(10.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 1.0, cacheMatrix.get(i, j), PRECISION); - - assertEquals(UNEXPECTED_VAL, cacheMatrix.rowSize() * cacheMatrix.columnSize(), cacheMatrix.sum(), PRECISION); - } - - /** - * TODO: IGNITE-5102, wrong min/max, wait for fold/map fix - */ - public void testMinMax() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - cacheMatrix.set(i, j, i * cols + j + 1); - - assertEquals(UNEXPECTED_VAL, 1.0, cacheMatrix.minValue(), PRECISION); - assertEquals(UNEXPECTED_VAL, rows * cols, cacheMatrix.maxValue(), PRECISION); - - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - cacheMatrix.set(i, j, -1.0 * (i * cols + j + 1)); - - assertEquals(UNEXPECTED_VAL, -rows * cols, cacheMatrix.minValue(), PRECISION); - assertEquals(UNEXPECTED_VAL, -1.0, cacheMatrix.maxValue(), PRECISION); - - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - cacheMatrix.set(i, j, i * cols + j); - - assertEquals(UNEXPECTED_VAL, 1.0, cacheMatrix.minValue(), PRECISION); - assertEquals(UNEXPECTED_VAL, rows * cols - 1.0, cacheMatrix.maxValue(), PRECISION); - } - - /** */ - public void testMap() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - initMtx(cacheMatrix); - - cacheMatrix.map(i -> 100.0); - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assertEquals(UNEXPECTED_VAL, 100.0, cacheMatrix.get(i, j), PRECISION); - } - - /** */ - public void testCopy() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - Matrix copiedMtx = cacheMatrix.copy(); - - for (int i = 0; i < cacheMatrix.rowSize(); i++) - for (int j = 0; j < cacheMatrix.columnSize(); j++) - assert copiedMtx.get(i, j) == cacheMatrix.get(i, j); - } - - /** */ - public void testCacheBehaviour() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - SparseDistributedMatrix cacheMatrix1 = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - SparseDistributedMatrix cacheMatrix2 = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - initMtx(cacheMatrix1); - initMtx(cacheMatrix2); - - Collection cacheNames = ignite.cacheNames(); - - assert cacheNames.contains(((DistributedStorage)cacheMatrix1.getStorage()).cacheName()); - - IgniteCache> cache = ignite.getOrCreateCache(((DistributedStorage)cacheMatrix1.getStorage()).cacheName()); - - Set keySet1 = ((SparseDistributedMatrixStorage)cacheMatrix1.getStorage()).getAllKeys(); - Set keySet2 = ((SparseDistributedMatrixStorage)cacheMatrix2.getStorage()).getAllKeys(); - - assert cache.containsKeys(keySet1) || - keySet1.stream().allMatch(k -> cache.invoke(k, (entry, arguments) -> entry.getKey().equals(k) && entry.getValue().size() == 100)); - assert cache.containsKeys(keySet2) || - keySet2.stream().allMatch(k -> cache.invoke(k, (entry, arguments) -> entry.getKey().equals(k) && entry.getValue().size() == 100)); - - cacheMatrix2.destroy(); - - assert cache.containsKeys(keySet1) || - keySet1.stream().allMatch(k -> cache.invoke(k, (entry, arguments) -> entry.getKey().equals(k) && entry.getValue().size() == 100)); - assert !cache.containsKeys(keySet2) && - keySet2.stream().allMatch(k -> cache.invoke(k, (entry, arguments) -> entry.getKey().equals(k) && entry.getValue() == null)); - - cacheMatrix1.destroy(); - - assert !cache.containsKeys(keySet1) && - keySet1.stream().allMatch(k -> cache.invoke(k, (entry, arguments) -> entry.getKey().equals(k) && entry.getValue() == null)); - } - - /** */ - public void testLike() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - assertNotNull(cacheMatrix.like(1, 1)); - } - - /** */ - public void testLikeVector() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - cacheMatrix = new SparseDistributedMatrix(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - Vector v = cacheMatrix.likeVector(1); - assert v.size() == 1; - assert v instanceof SparseDistributedVector; - } - - /** */ - public void testMatrixTimes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - int size = MATRIX_SIZE; - - SparseDistributedMatrix cacheMatrix1 = new SparseDistributedMatrix(size, size, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - SparseDistributedMatrix cacheMatrix2 = new SparseDistributedMatrix(size, size, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - for (int i = 0; i < size; i++) { - cacheMatrix1.setX(i, i, i); - cacheMatrix2.setX(i, i, i); - } - - Matrix res = cacheMatrix1.times(cacheMatrix2); - - for (int i = 0; i < size; i++) - for (int j = 0; j < size; j++) - if (i == j) - assertEquals(UNEXPECTED_VAL, i * i, res.get(i, j), PRECISION); - else - assertEquals(UNEXPECTED_VAL, 0, res.get(i, j), PRECISION); - } - - /** */ - private void initMtx(Matrix m) { - for (int i = 0; i < m.rowSize(); i++) - for (int j = 0; j < m.columnSize(); j++) - m.set(i, j, 1.0); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixViewTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixViewTest.java deleted file mode 100644 index 22ec7ccc45bcd..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/TransposedMatrixViewTest.java +++ /dev/null @@ -1,87 +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.ExternalizeTest; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Tests for {@link TransposedMatrixView}. - */ -public class TransposedMatrixViewTest extends ExternalizeTest { - /** */ - private static final String UNEXPECTED_VALUE = "Unexpected value"; - /** */ - private TransposedMatrixView testMatrix; - /** */ - private DenseLocalOnHeapMatrix parent; - - /** */ - @Before - public void setup() { - parent = new DenseLocalOnHeapMatrix(MathTestConstants.STORAGE_SIZE, MathTestConstants.STORAGE_SIZE); - fillMatrix(parent); - testMatrix = new TransposedMatrixView(parent); - } - - /** {@inheritDoc} */ - @Override public void externalizeTest() { - externalizeTest(testMatrix); - } - - /** */ - @Test - public void testView() { - assertEquals(UNEXPECTED_VALUE, parent.rowSize(), testMatrix.columnSize()); - assertEquals(UNEXPECTED_VALUE, parent.columnSize(), testMatrix.rowSize()); - - for (int i = 0; i < parent.rowSize(); i++) - for (int j = 0; j < parent.columnSize(); j++) - assertEquals(UNEXPECTED_VALUE, parent.get(i, j), testMatrix.get(j, i), 0d); - } - - /** */ - @Test - public void testNullParams() { - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new TransposedMatrixView(null), "Null Matrix parameter"); - } - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void testLike() { - testMatrix.like(0, 0); - } - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void testLikeVector() { - testMatrix.likeVector(0); - } - - /** */ - private void fillMatrix(DenseLocalOnHeapMatrix mtx) { - for (int i = 0; i < mtx.rowSize(); i++) - for (int j = 0; j < mtx.columnSize(); j++) - mtx.setX(i, j, i * mtx.rowSize() + j); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java deleted file mode 100644 index 7c2d4152a29be..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/matrix/SparseDistributedMatrixStorageTest.java +++ /dev/null @@ -1,126 +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 org.apache.ignite.Ignite; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -/** - * Tests for {@link SparseDistributedMatrixStorage}. - */ -@GridCommonTest(group = "Distributed Models") -public class SparseDistributedMatrixStorageTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - /** Cache name. */ - private static final String CACHE_NAME = "test-cache"; - /** */ - private static final String UNEXPECTED_ATTRIBUTE_VALUE = "Unexpected attribute value."; - /** Grid instance. */ - private Ignite ignite; - - /** - * Default constructor. - */ - public SparseDistributedMatrixStorageTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * {@inheritDoc} - */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - ignite.destroyCache(CACHE_NAME); - } - - /** */ - public void testCacheCreation() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int rows = MathTestConstants.STORAGE_SIZE; - final int cols = MathTestConstants.STORAGE_SIZE; - - SparseDistributedMatrixStorage storage = new SparseDistributedMatrixStorage(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - assertNotNull("SparseDistributedMatrixStorage cache is null.", storage.cache()); - } - - /** */ - public void testSetGet() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int rows = MathTestConstants.STORAGE_SIZE; - final int cols = MathTestConstants.STORAGE_SIZE; - - SparseDistributedMatrixStorage storage = new SparseDistributedMatrixStorage(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - double v = Math.random(); - storage.set(i, j, v); - - assert Double.compare(v, storage.get(i, j)) == 0; - assert Double.compare(v, storage.get(i, j)) == 0; - } - } - } - - /** */ - public void testAttributes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int rows = MathTestConstants.STORAGE_SIZE; - final int cols = MathTestConstants.STORAGE_SIZE; - - SparseDistributedMatrixStorage storage = new SparseDistributedMatrixStorage(rows, cols, StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - - assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.rowSize(), rows); - assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.columnSize(), cols); - - assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, storage.isArrayBased()); - assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, storage.isDense()); - assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, storage.isDistributed()); - - assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.isRandomAccess(), !storage.isSequentialAccess()); - assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, storage.isRandomAccess()); - - } - -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorageTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorageTest.java deleted file mode 100644 index f4337252a8e54..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/storage/vector/SparseDistributedVectorStorageTest.java +++ /dev/null @@ -1,123 +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 org.apache.ignite.Ignite; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -/** - * Tests for {@link SparseDistributedVectorStorage}. - */ -@GridCommonTest(group = "Distributed Models") -public class SparseDistributedVectorStorageTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - - /** Cache name. */ - private static final String CACHE_NAME = "test-cache"; - - /** */ - private static final String UNEXPECTED_ATTRIBUTE_VALUE = "Unexpected attribute value."; - - /** Grid instance. */ - private Ignite ignite; - - /** - * Default constructor. - */ - public SparseDistributedVectorStorageTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * {@inheritDoc} - */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - ignite.destroyCache(CACHE_NAME); - } - - /** */ - public void testCacheCreation() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int size = MathTestConstants.STORAGE_SIZE; - - SparseDistributedVectorStorage storage = new SparseDistributedVectorStorage(size, StorageConstants.RANDOM_ACCESS_MODE); - - assertNotNull("SparseDistributedMatrixStorage cache is null.", storage.cache()); - } - - /** */ - public void testSetGet() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int size = MathTestConstants.STORAGE_SIZE; - - SparseDistributedVectorStorage storage = new SparseDistributedVectorStorage(size, StorageConstants.RANDOM_ACCESS_MODE); - - for (int i = 0; i < size; i++) { - double v = Math.random(); - storage.set(i, v); - - assert Double.compare(v, storage.get(i)) == 0; - assert Double.compare(v, storage.get(i)) == 0; - } - } - - /** */ - public void testAttributes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int size = MathTestConstants.STORAGE_SIZE; - - SparseDistributedVectorStorage storage = new SparseDistributedVectorStorage(size, StorageConstants.RANDOM_ACCESS_MODE); - - assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.size(), size); - - assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, storage.isArrayBased()); - assertFalse(UNEXPECTED_ATTRIBUTE_VALUE, storage.isDense()); - assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, storage.isDistributed()); - - assertEquals(UNEXPECTED_ATTRIBUTE_VALUE, storage.isRandomAccess(), !storage.isSequentialAccess()); - assertTrue(UNEXPECTED_ATTRIBUTE_VALUE, storage.isRandomAccess()); - - } - -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java deleted file mode 100644 index 1008cc2cb0ed4..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/CacheVectorTest.java +++ /dev/null @@ -1,430 +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 java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.stream.IntStream; -import junit.framework.TestCase; -import org.apache.ignite.Ignite; -import org.apache.ignite.IgniteCache; -import org.apache.ignite.configuration.CacheConfiguration; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.IdentityValueMapper; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.distributed.VectorKeyMapper; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -/** - * Tests for {@link CacheVector}. - */ -@GridCommonTest(group = "Distributed Models") -public class CacheVectorTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - /** Cache name. */ - private static final String CACHE_NAME = "test-cache"; - /** Cache size. */ - private static final int size = MathTestConstants.STORAGE_SIZE; - - /** Grid instance. */ - private Ignite ignite; - /** Default key mapper. */ - private VectorKeyMapper keyMapper = new TestKeyMapper(); - - /** - * Default constructor. - */ - public CacheVectorTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** {@inheritDoc} */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - ignite.destroyCache(CACHE_NAME); - } - - /** */ - public void testGetSet() throws Exception { - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - for (int i = 0; i < size; i++) { - double random = Math.random(); - cacheVector.set(i, random); - assertEquals("Unexpected value.", random, cacheVector.get(i), 0d); - } - } - - /** */ - public void testMap() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - initVector(cacheVector); - - cacheVector.map(value -> 110d); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), 110d, 0d); - } - - /** */ - public void testMapBiFunc() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - initVector(cacheVector); - - cacheVector.map(Functions.PLUS, 1d); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), 1d, 0d); - } - - /** */ - public void testSum() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - initVector(cacheVector); - - assertEquals("Unexpected value.", cacheVector.sum(), 0d, 0d); - - cacheVector.assign(1d); - - assertEquals("Unexpected value.", cacheVector.sum(), size, 0d); - } - - /** */ - public void testSumEmptyVector() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - cacheVector.sum(); - } - - /** */ - public void testAssign() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - initVector(cacheVector); - - cacheVector.assign(1d); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), 1d, 0d); - } - - /** */ - public void testAssignRange() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - cacheVector.assign(IntStream.range(0, size).asDoubleStream().toArray()); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), i, 0d); - } - - /** */ - public void testAssignVector() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); - - cacheVector.assign(testVec); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), testVec.get(i), 0d); - } - - /** */ - public void testAssignFunc() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - cacheVector.assign(idx -> idx); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), i, 0d); - } - - /** */ - public void testPlus() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - initVector(cacheVector); - - cacheVector.plus(1d); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), 1d, 0d); - } - - /** */ - public void testPlusVec() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); - - try { - cacheVector.plus(testVec); - TestCase.fail(); - } - catch (UnsupportedOperationException ignored) { - - } - } - - /** */ - public void testDivide() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int size = MathTestConstants.STORAGE_SIZE; - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - initVector(cacheVector); - cacheVector.assign(1d); - - cacheVector.divide(2d); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), 1d / 2d, 0d); - } - - /** */ - public void testTimes() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - final int size = MathTestConstants.STORAGE_SIZE; - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - initVector(cacheVector); - cacheVector.assign(1d); - - cacheVector.times(2d); - - for (int i = 0; i < size; i++) - assertEquals("Unexpected value.", cacheVector.get(i), 2d, 0d); - } - - /** */ - public void testTimesVector() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - cacheVector.assign(1d); - Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); - - try { - cacheVector.times(testVec); - TestCase.fail(); - } - catch (UnsupportedOperationException ignored) { - - } - - } - - /** */ - public void testMin() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); - - cacheVector.assign(testVec); - - assertEquals("Unexpected value.", cacheVector.minValue(), 0d, 0d); - } - - /** */ - public void testMax() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - Vector testVec = new DenseLocalOnHeapVector(IntStream.range(0, size).asDoubleStream().toArray()); - - cacheVector.assign(testVec); - - assertEquals("Unexpected value.", cacheVector.maxValue(), testVec.get(size - 1), 0d); - } - - /** */ - public void testLike() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - try { - cacheVector.like(size); - TestCase.fail("Unsupported case"); - } - catch (UnsupportedOperationException ignored) { - - } - } - - /** */ - public void testLikeMatrix() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - try { - cacheVector.likeMatrix(size, size); - TestCase.fail("Unsupported case"); - } - catch (UnsupportedOperationException ignored) { - - } - } - - /** */ - public void testCopy() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - try { - cacheVector.copy(); - TestCase.fail("Unsupported case"); - } - catch (UnsupportedOperationException ignored) { - - } - } - - /** */ - public void testExternalize() throws IOException, ClassNotFoundException { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - IdentityValueMapper valMapper = new IdentityValueMapper(); - CacheVector cacheVector = new CacheVector<>(size, getCache(), keyMapper, valMapper); - - cacheVector.set(1, 1.0); - - ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objOutputStream = new ObjectOutputStream(byteArrOutputStream); - - objOutputStream.writeObject(cacheVector); - - ByteArrayInputStream byteArrInputStream = new ByteArrayInputStream(byteArrOutputStream.toByteArray()); - ObjectInputStream objInputStream = new ObjectInputStream(byteArrInputStream); - - CacheVector objRestored = (CacheVector)objInputStream.readObject(); - - assertTrue(MathTestConstants.VAL_NOT_EQUALS, cacheVector.equals(objRestored)); - assertEquals(MathTestConstants.VAL_NOT_EQUALS, objRestored.get(1), 1.0, 0.0); - } - - /** */ - private void initVector(CacheVector cacheVector) { - for (int i = 0; i < cacheVector.size(); i++) - cacheVector.set(i, 0d); - } - - /** */ - private IgniteCache getCache() { - assert ignite != null; - - CacheConfiguration cfg = new CacheConfiguration(); - cfg.setName(CACHE_NAME); - - IgniteCache cache = ignite.getOrCreateCache(CACHE_NAME); - - assert cache != null; - return cache; - } - - /** */ - private static class TestKeyMapper implements VectorKeyMapper { - /** {@inheritDoc} */ - @Override public Integer apply(int i) { - return i; - } - - /** {@inheritDoc} */ - @Override public boolean isValid(Integer i) { - return i < size; - } - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java deleted file mode 100644 index c5c0bbd779a2f..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/ConstantVectorConstructorTest.java +++ /dev/null @@ -1,52 +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.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** */ -public class ConstantVectorConstructorTest { - /** */ - private static final int IMPOSSIBLE_SIZE = -1; - - /** */ - @Test(expected = AssertionError.class) - public void negativeSizeTest() { - assertEquals("Negative size.", IMPOSSIBLE_SIZE, - new ConstantVector(-1, 1).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void zeroSizeTest() { - assertEquals("Zero size.", IMPOSSIBLE_SIZE, - new ConstantVector(0, 1).size()); - } - - /** */ - @Test - public void primitiveTest() { - assertEquals("1 size.", 1, - new ConstantVector(1, 1).size()); - - assertEquals("2 size.", 2, - new ConstantVector(2, 1).size()); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java deleted file mode 100644 index 1e99f7eaeaa66..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/FunctionVectorConstructorTest.java +++ /dev/null @@ -1,121 +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 java.util.HashMap; -import java.util.Map; -import java.util.function.IntToDoubleFunction; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.functions.IntDoubleToVoidFunction; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** */ -public class FunctionVectorConstructorTest { - /** */ - private static final int IMPOSSIBLE_SIZE = -1; - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void mapInvalidArgsTest() { - assertEquals("Expect exception due to invalid args.", IMPOSSIBLE_SIZE, - new FunctionVector(new HashMap() {{ - put("invalid", 99); - }}).size()); - } - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void mapMissingArgsTest() { - final Map test = new HashMap() {{ - put("size", 1); - put("paramMissing", "whatever"); - }}; - - assertEquals("Expect exception due to missing args.", - -1, new FunctionVector(test).size()); - } - - /** */ - @Test(expected = ClassCastException.class) - public void mapInvalidParamTypeTest() { - final Map test = new HashMap() {{ - put("size", "whatever"); - - put("getFunc", (IntToDoubleFunction)i -> i); - }}; - - assertEquals("Expect exception due to invalid param type.", IMPOSSIBLE_SIZE, - new FunctionVector(test).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void mapNullTest() { - //noinspection ConstantConditions - assertEquals("Null map args.", IMPOSSIBLE_SIZE, - new FunctionVector(null).size()); - } - - /** */ - @Test - public void mapTest() { - assertEquals("Size from args.", 99, - new FunctionVector(new HashMap() {{ - put("size", 99); - - put("getFunc", (IgniteFunction)i -> (double)i); - }}).size()); - - assertEquals("Size from args with setFunc.", 99, - new FunctionVector(new HashMap() {{ - put("size", 99); - - put("getFunc", (IgniteFunction)i -> (double)i); - - put("setFunc", (IntDoubleToVoidFunction)(integer, aDouble) -> { - }); - }}).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void negativeSizeTest() { - assertEquals("Negative size.", IMPOSSIBLE_SIZE, - new FunctionVector(-1, (i) -> (double)i).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void zeroSizeTest() { - assertEquals("0 size.", IMPOSSIBLE_SIZE, - new FunctionVector(0, (i) -> (double)i).size()); - } - - /** */ - @Test - public void primitiveTest() { - assertEquals("1 size.", 1, - new FunctionVector(1, (i) -> (double)i).size()); - - assertEquals("2 size.", 2, - new FunctionVector(2, (i) -> (double)i).size()); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java deleted file mode 100644 index f07a3fdd91508..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorViewConstructorTest.java +++ /dev/null @@ -1,211 +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.Vector; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - -/** */ -public class PivotedVectorViewConstructorTest { - /** */ - private static final int IMPOSSIBLE_SIZE = -1; - - /** */ - private static final SampleParams sampleParams = new SampleParams(); - - /** */ - @Test(expected = NullPointerException.class) - public void nullVecParamTest() { - assertEquals("Expect exception due to null vector param.", IMPOSSIBLE_SIZE, - new PivotedVectorView(null, sampleParams.pivot).size()); - } - - /** */ - @Test(expected = NullPointerException.class) - public void nullVecParam2Test() { - assertEquals("Expect exception due to null vector param, with unpivot.", IMPOSSIBLE_SIZE, - new PivotedVectorView(null, sampleParams.pivot, sampleParams.unpivot).size()); - } - - /** */ - @Test(expected = NullPointerException.class) - public void nullPivotParamTest() { - assertEquals("Expect exception due to null pivot param.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, null).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullPivotParam2Test() { - assertEquals("Expect exception due to null pivot param, with unpivot.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, null, sampleParams.unpivot).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullUnpivotParam2Test() { - assertEquals("Expect exception due to null unpivot param.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, sampleParams.pivot, null).size()); - } - - /** */ - @Test(expected = CardinalityException.class) - public void emptyPivotTest() { - assertEquals("Expect exception due to empty pivot param.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, new int[] {}).size()); - } - - /** */ - @Test(expected = CardinalityException.class) - public void emptyPivot2Test() { - assertEquals("Expect exception due to empty pivot param, with unpivot.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, new int[] {}, sampleParams.unpivot).size()); - } - - /** */ - @Test(expected = CardinalityException.class) - public void wrongPivotTest() { - assertEquals("Expect exception due to wrong pivot param.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, new int[] {0}).size()); - } - - /** */ - @Test(expected = CardinalityException.class) - public void wrongPivot2Test() { - assertEquals("Expect exception due to wrong pivot param, with unpivot.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, new int[] {0}, sampleParams.unpivot).size()); - } - - /** */ - @Test(expected = CardinalityException.class) - public void emptyUnpivotTest() { - assertEquals("Expect exception due to empty unpivot param.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, sampleParams.pivot, new int[] {}).size()); - } - - /** */ - @Test(expected = CardinalityException.class) - public void wrongUnpivotTest() { - assertEquals("Expect exception due to wrong unpivot param, with unpivot.", IMPOSSIBLE_SIZE, - new PivotedVectorView(sampleParams.vec, sampleParams.pivot, new int[] {0}).size()); - } - - /** */ - @Test - public void basicPivotTest() { - final PivotedVectorView pvv = new PivotedVectorView(sampleParams.vec, sampleParams.pivot); - - final int size = sampleParams.vec.size(); - - assertEquals("View size differs from expected.", size, pvv.size()); - - assertSame("Base vector differs from expected.", sampleParams.vec, pvv.getBaseVector()); - - for (int idx = 0; idx < size; idx++) { - assertEquals("Sample pivot and unpivot differ from expected", - idx, sampleParams.unpivot[sampleParams.pivot[idx]]); - - assertEquals("Pivot differs from expected at index " + idx, - sampleParams.pivot[idx], pvv.pivot(idx)); - - assertEquals("Default unpivot differs from expected at index " + idx, - sampleParams.unpivot[idx], pvv.unpivot(idx)); - - final Metric metric = new Metric(sampleParams.vec.get(idx), pvv.get(pvv.pivot(idx))); - - assertTrue("Not close enough at index " + idx + ", " + metric, metric.closeEnough()); - } - - for (int idx = 0; idx < size; idx++) { - sampleParams.vec.set(idx, sampleParams.vec.get(idx) + idx + 1); - - final Metric metric = new Metric(sampleParams.vec.get(idx), pvv.get(pvv.pivot(idx))); - - assertTrue("Modified value not close enough at index " + idx + ", " + metric, metric.closeEnough()); - } - } - - /** */ - @Test - public void basicUnpivotTest() { - final PivotedVectorView pvv = new PivotedVectorView(sampleParams.vec, sampleParams.pivot, sampleParams.unpivot); - - final int size = sampleParams.vec.size(); - - assertEquals("View size differs from expected.", size, pvv.size()); - - for (int idx = 0; idx < size; idx++) { - assertEquals("Unpivot differs from expected at index " + idx, - sampleParams.unpivot[idx], pvv.unpivot(idx)); - - final Metric metric = new Metric(sampleParams.vec.get(idx), pvv.get(pvv.unpivot(idx))); - - assertTrue("Not close enough at index " + idx + ", " + metric, metric.closeEnough()); - } - } - - /** */ - private static class SampleParams { - /** */ - final double[] data = new double[] {0, 1}; - /** */ - final Vector vec = new DenseLocalOnHeapVector(data); - /** */ - final int[] pivot = new int[] {1, 0}; - /** */ - final int[] unpivot = new int[] {1, 0}; - } - - /** */ - private static class Metric { // TODO: IGNITE-5824, consider if softer tolerance (like say 0.1 or 0.01) would make sense here - /** */ - private final double exp; - - /** */ - private final double obtained; - - /** **/ - Metric(double exp, double obtained) { - this.exp = exp; - this.obtained = obtained; - } - - /** */ - boolean closeEnough() { - return new Double(exp).equals(obtained) || closeEnoughToZero(); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "Metric{" + "expected=" + exp + - ", obtained=" + obtained + - '}'; - } - - /** */ - private boolean closeEnoughToZero() { - return (new Double(exp).equals(0.0) && new Double(obtained).equals(-0.0)) - || (new Double(exp).equals(-0.0) && new Double(obtained).equals(0.0)); - } - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/RandomVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/RandomVectorConstructorTest.java deleted file mode 100644 index 49e1a50007e02..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/RandomVectorConstructorTest.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.vector; - -import java.util.HashMap; -import java.util.Map; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** */ -public class RandomVectorConstructorTest { - /** */ - private static final int IMPOSSIBLE_SIZE = -1; - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void mapInvalidArgsTest() { - assertEquals("Expect exception due to invalid args.", IMPOSSIBLE_SIZE, - new RandomVector(new HashMap() {{ - put("invalid", 99); - }}).size()); - } - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void mapMissingArgsTest() { - final Map test = new HashMap() {{ - put("paramMissing", "whatever"); - }}; - - assertEquals("Expect exception due to missing args.", - -1, new RandomVector(test).size()); - } - - /** */ - @Test(expected = ClassCastException.class) - public void mapInvalidParamTypeTest() { - final Map test = new HashMap() {{ - put("size", "whatever"); - put("fastHash", true); - }}; - - assertEquals("Expect exception due to invalid param type.", IMPOSSIBLE_SIZE, - new RandomVector(test).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void mapNullTest() { - //noinspection ConstantConditions - assertEquals("Null map args.", IMPOSSIBLE_SIZE, - new RandomVector(null).size()); - } - - /** */ - @Test - public void mapTest() { - assertEquals("Size from args.", 99, - new RandomVector(new HashMap() {{ - put("size", 99); - }}).size()); - - final int test = 99; - - assertEquals("Size from args with fastHash false.", test, - new RandomVector(new HashMap() {{ - put("size", test); - put("fastHash", false); - }}).size()); - - assertEquals("Size from args with fastHash true.", test, - new RandomVector(new HashMap() {{ - put("size", test); - put("fastHash", true); - }}).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void negativeSizeTest() { - assertEquals("Negative size.", IMPOSSIBLE_SIZE, - new RandomVector(-1).size()); - } - - /** */ - @Test - public void basicTest() { - final int basicSize = 3; - - Vector v1 = new RandomVector(basicSize); - - //noinspection EqualsWithItself - assertTrue("Expect vector to be equal to self", v1.equals(v1)); - - //noinspection ObjectEqualsNull - assertFalse("Expect vector to be not equal to null", v1.equals(null)); - - assertEquals("Size differs from expected", basicSize, v1.size()); - - verifyValues(v1); - - Vector v2 = new RandomVector(basicSize, true); - - assertEquals("Size differs from expected", basicSize, v2.size()); - - verifyValues(v2); - - Vector v3 = new RandomVector(basicSize, false); - - assertEquals("Size differs from expected", basicSize, v3.size()); - - verifyValues(v3); - } - - /** */ - private void verifyValues(Vector v) { - for (Vector.Element e : v.all()) { - double val = e.get(); - - assertTrue("Value too small: " + val + " at index " + e.index(), -1d <= val); - - assertTrue("Value too large: " + val + " at index " + e.index(), val <= 1d); - } - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorConstructorTest.java deleted file mode 100644 index db4d5decbc759..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorConstructorTest.java +++ /dev/null @@ -1,159 +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 java.util.HashMap; -import java.util.Map; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** */ -public class SingleElementVectorConstructorTest { - /** */ - private static final int IMPOSSIBLE_SIZE = -1; - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void mapInvalidArgsTest() { - assertEquals("Expect exception due to invalid args.", IMPOSSIBLE_SIZE, - new SingleElementVector(new HashMap() {{ - put("invalid", 99); - }}).size()); - } - - /** */ - @Test(expected = UnsupportedOperationException.class) - public void mapMissingArgsTest() { - final Map test = new HashMap() {{ - put("size", 1); - - put("paramMissing", "whatever"); - }}; - - assertEquals("Expect exception due to missing args.", - -1, new SingleElementVector(test).size()); - } - - /** */ - @Test(expected = ClassCastException.class) - public void mapInvalidParamTypeTest() { - final Map test = new HashMap() {{ - put("size", "whatever"); - - put("index", 0); - put("value", 1.0); - }}; - - assertEquals("Expect exception due to invalid param type.", IMPOSSIBLE_SIZE, - new SingleElementVector(test).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void mapNullTest() { - //noinspection ConstantConditions - assertEquals("Null map args.", IMPOSSIBLE_SIZE, - new SingleElementVector(null).size()); - } - - /** */ - @Test - public void mapTest() { - assertEquals("Size from array in args.", 99, - new SingleElementVector(new HashMap() {{ - put("size", 99); - put("index", 0); - put("value", 1.0); - }}).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void negativeSizeTest() { - assertEquals("Negative size.", IMPOSSIBLE_SIZE, - new SingleElementVector(-1, 0, 1.0).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void zeroSizeTest() { - assertEquals("Zero size.", IMPOSSIBLE_SIZE, - new SingleElementVector(0, 0, 1.0).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void wrongIndexTest() { - //noinspection ConstantConditions - assertEquals("Wrong index.", IMPOSSIBLE_SIZE, - new SingleElementVector(1, 2, 1.0).size()); - } - - /** */ - @Test - public void basicTest() { - final int[] sizes = new int[] {1, 4, 8}; - - for (int size : sizes) - for (int idx = 0; idx < size; idx++) - basicTest(size, idx); - } - - /** */ - private void basicTest(int size, int idx) { - final Double expVal = (double)(size - idx); - - Vector v = new SingleElementVector(size, idx, expVal); - - assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, - expVal.equals(v.get(idx))); - - final double delta = 1.0; - - v.set(idx, expVal - delta); - - assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, - expVal.equals(v.get(idx) + delta)); - - final Double zero = 0.0; - - for (int i = 0; i < size; i++) { - if (i == idx) - continue; - - assertTrue("Expect zero at index " + i + " for size " + size, - zero.equals(v.get(i))); - - boolean eCaught = false; - - try { - v.set(i, 1.0); - } - catch (UnsupportedOperationException uoe) { - eCaught = true; - } - - assertTrue("Expect " + java.lang.UnsupportedOperationException.class.getSimpleName() - + " at index " + i + " for size " + size, eCaught); - } - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorViewConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorViewConstructorTest.java deleted file mode 100644 index a6933199629ff..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorViewConstructorTest.java +++ /dev/null @@ -1,137 +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.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** */ -public class SingleElementVectorViewConstructorTest { - /** */ - private static final int IMPOSSIBLE_SIZE = -1; - - /** */ - private static final SampleHelper helper = new SampleHelper(); - - /** */ - @Test(expected = AssertionError.class) - public void nullVecParamTest() { - assertEquals("Expect exception due to null vector param.", IMPOSSIBLE_SIZE, - new SingleElementVectorView(null, helper.idx).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void negativeIdxParamTest() { - assertEquals("Expect exception due to negative index param.", IMPOSSIBLE_SIZE, - new SingleElementVectorView(helper.vec, -1).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void tooLargeIdxParamTest() { - assertEquals("Expect exception due to too large index param.", IMPOSSIBLE_SIZE, - new SingleElementVectorView(helper.vec, helper.vec.size()).size()); - } - - /** */ - @Test(expected = AssertionError.class) - public void emptyVecParamTest() { - assertEquals("Expect exception due to empty vector param.", IMPOSSIBLE_SIZE, - new SingleElementVectorView(helper.vecEmpty, 0).size()); - } - - /** */ - @Test - public void basicTest() { - final int[] sizes = new int[] {1, 4, 8}; - - for (int size : sizes) - for (int idx = 0; idx < size; idx++) - basicTest(size, idx); - } - - /** */ - private void basicTest(int size, int idx) { - final Double expVal = (double)(size - idx); - - Vector orig = helper.newSample(size, idx, expVal); - - SingleElementVectorView svv = new SingleElementVectorView(orig, idx); - - assertEquals("Size differs from expected", size, svv.size()); - - assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, - expVal.equals(svv.get(idx))); - - final double delta = 1.0; - - svv.set(idx, expVal - delta); - - assertTrue("Expect value " + expVal + " at index " + idx + " for size " + size, - expVal.equals(orig.get(idx) + delta)); - - final Double zero = 0.0; - - for (int i = 0; i < size; i++) { - if (i == idx) - continue; - - assertTrue("Expect zero at index " + i + " for size " + size, - zero.equals(svv.get(i))); - - boolean eCaught = false; - - try { - svv.set(i, 1.0); - } - catch (UnsupportedOperationException uoe) { - eCaught = true; - } - - assertTrue("Expect " + UnsupportedOperationException.class.getSimpleName() - + " at index " + i + " for size " + size, eCaught); - } - } - - /** */ - private static class SampleHelper { - /** */ - final double[] data = new double[] {0, 1}; - /** */ - final Vector vec = new DenseLocalOnHeapVector(data); - /** */ - final Vector vecEmpty = new DenseLocalOnHeapVector(new double[] {}); - /** */ - final int idx = 0; - - /** */ - Vector newSample(int size, int idx, double expVal) { - final Vector v = new DenseLocalOnHeapVector(size); - - for (int i = 0; i < size; i++) - v.set(i, i == idx ? expVal : i); - - return v; - } - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVectorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVectorTest.java deleted file mode 100644 index 6b2590c4cb67c..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVectorTest.java +++ /dev/null @@ -1,186 +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 java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import org.apache.ignite.Ignite; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -import static org.apache.ignite.ml.math.impls.MathTestConstants.UNEXPECTED_VAL; - -/** - * Tests for {@link SparseDistributedVector}. - */ -@GridCommonTest(group = "Distributed Models") -public class SparseBlockDistributedVectorTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - - /** Precision. */ - private static final double PRECISION = 0.0; - - /** Grid instance. */ - private Ignite ignite; - - /** Vector size */ - private final int size = MathTestConstants.STORAGE_SIZE; - - /** Vector for tests */ - private SparseBlockDistributedVector sparseBlockDistributedVector; - - /** - * Default constructor. - */ - public SparseBlockDistributedVectorTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * {@inheritDoc} - */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - if (sparseBlockDistributedVector != null) { - sparseBlockDistributedVector.destroy(); - sparseBlockDistributedVector = null; - } - } - - /** */ - public void testGetSet() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseBlockDistributedVector = new SparseBlockDistributedVector(size); - - for (int i = 0; i < size; i++) { - double v = Math.random(); - sparseBlockDistributedVector.set(i, v); - assertEquals("Unexpected value for vector element[" + i + "]", v, sparseBlockDistributedVector.get(i), PRECISION); - } - } - - /** */ - public void testExternalize() throws IOException, ClassNotFoundException { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseBlockDistributedVector = new SparseBlockDistributedVector(size); - - sparseBlockDistributedVector.set(1, 1.0); - - ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objOutputStream = new ObjectOutputStream(byteArrOutputStream); - - objOutputStream.writeObject(sparseBlockDistributedVector); - - ByteArrayInputStream byteArrInputStream = new ByteArrayInputStream(byteArrOutputStream.toByteArray()); - ObjectInputStream objInputStream = new ObjectInputStream(byteArrInputStream); - - SparseBlockDistributedVector objRestored = (SparseBlockDistributedVector)objInputStream.readObject(); - - assertTrue(MathTestConstants.VAL_NOT_EQUALS, sparseBlockDistributedVector.equals(objRestored)); - assertEquals(MathTestConstants.VAL_NOT_EQUALS, objRestored.get(1), 1.0, PRECISION); - } - - /** Test simple math. */ - public void testMath() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseBlockDistributedVector = new SparseBlockDistributedVector(size); - initVector(sparseBlockDistributedVector); - - sparseBlockDistributedVector.assign(2.0); - for (int i = 0; i < sparseBlockDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 2.0, sparseBlockDistributedVector.get(i), PRECISION); - - sparseBlockDistributedVector.plus(3.0); - for (int i = 0; i < sparseBlockDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 5.0, sparseBlockDistributedVector.get(i), PRECISION); - - sparseBlockDistributedVector.times(2.0); - for (int i = 0; i < sparseBlockDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 10.0, sparseBlockDistributedVector.get(i), PRECISION); - - sparseBlockDistributedVector.divide(10.0); - for (int i = 0; i < sparseBlockDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 1.0, sparseBlockDistributedVector.get(i), PRECISION); - } - - /** */ - public void testMap() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseBlockDistributedVector = new SparseBlockDistributedVector(size); - initVector(sparseBlockDistributedVector); - - sparseBlockDistributedVector.map(i -> 100.0); - for (int i = 0; i < sparseBlockDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 100.0, sparseBlockDistributedVector.get(i), PRECISION); - } - - /** */ - public void testCopy() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseBlockDistributedVector = new SparseBlockDistributedVector(size); - - Vector cp = sparseBlockDistributedVector.copy(); - assertNotNull(cp); - for (int i = 0; i < size; i++) - assertEquals(UNEXPECTED_VAL, cp.get(i), sparseBlockDistributedVector.get(i), PRECISION); - } - - /** */ - public void testLike() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseBlockDistributedVector = new SparseBlockDistributedVector(size); - - assertNotNull(sparseBlockDistributedVector.like(1)); - } - - /** */ - private void initVector(Vector v) { - for (int i = 0; i < v.size(); i++) - v.set(i, 1.0); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVectorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVectorTest.java deleted file mode 100644 index b5c348fada186..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVectorTest.java +++ /dev/null @@ -1,191 +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 java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import org.apache.ignite.Ignite; -import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; -import org.apache.ignite.testframework.junits.common.GridCommonTest; - -import static org.apache.ignite.ml.math.impls.MathTestConstants.UNEXPECTED_VAL; - -/** - * Tests for {@link SparseDistributedVector}. - */ -@GridCommonTest(group = "Distributed Models") -public class SparseDistributedVectorTest extends GridCommonAbstractTest { - /** Number of nodes in grid */ - private static final int NODE_COUNT = 3; - - /** Precision. */ - private static final double PRECISION = 0.0; - - /** Grid instance. */ - private Ignite ignite; - - /** Vector size */ - private final int size = MathTestConstants.STORAGE_SIZE; - - /** Vector for tests */ - private SparseDistributedVector sparseDistributedVector; - - /** - * Default constructor. - */ - public SparseDistributedVectorTest() { - super(false); - } - - /** {@inheritDoc} */ - @Override protected void beforeTestsStarted() throws Exception { - for (int i = 1; i <= NODE_COUNT; i++) - startGrid(i); - } - - /** {@inheritDoc} */ - @Override protected void afterTestsStopped() throws Exception { - stopAllGrids(); - } - - /** - * {@inheritDoc} - */ - @Override protected void beforeTest() throws Exception { - ignite = grid(NODE_COUNT); - - ignite.configuration().setPeerClassLoadingEnabled(true); - } - - /** {@inheritDoc} */ - @Override protected void afterTest() throws Exception { - if (sparseDistributedVector != null) { - sparseDistributedVector.destroy(); - sparseDistributedVector = null; - } - } - - /** */ - public void testGetSet() throws Exception { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseDistributedVector = new SparseDistributedVector(size, StorageConstants.RANDOM_ACCESS_MODE); - - for (int i = 0; i < size; i++) { - double v = Math.random(); - sparseDistributedVector.set(i, v); - assertEquals("Unexpected value for vector element[" + i + "]", v, sparseDistributedVector.get(i), PRECISION); - } - } - - /** */ - public void testExternalize() throws IOException, ClassNotFoundException { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseDistributedVector = new SparseDistributedVector(size, StorageConstants.RANDOM_ACCESS_MODE); - - sparseDistributedVector.set(1, 1.0); - - ByteArrayOutputStream byteArrOutputStream = new ByteArrayOutputStream(); - ObjectOutputStream objOutputStream = new ObjectOutputStream(byteArrOutputStream); - - objOutputStream.writeObject(sparseDistributedVector); - - ByteArrayInputStream byteArrInputStream = new ByteArrayInputStream(byteArrOutputStream.toByteArray()); - ObjectInputStream objInputStream = new ObjectInputStream(byteArrInputStream); - - SparseDistributedVector objRestored = (SparseDistributedVector)objInputStream.readObject(); - - assertTrue(MathTestConstants.VAL_NOT_EQUALS, sparseDistributedVector.equals(objRestored)); - assertEquals(MathTestConstants.VAL_NOT_EQUALS, objRestored.get(1), 1.0, PRECISION); - } - - /** Test simple math. */ - public void testMath() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseDistributedVector = new SparseDistributedVector(size, StorageConstants.RANDOM_ACCESS_MODE); - initVector(sparseDistributedVector); - - sparseDistributedVector.assign(2.0); - for (int i = 0; i < sparseDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 2.0, sparseDistributedVector.get(i), PRECISION); - - sparseDistributedVector.plus(3.0); - for (int i = 0; i < sparseDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 5.0, sparseDistributedVector.get(i), PRECISION); - - sparseDistributedVector.times(2.0); - for (int i = 0; i < sparseDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 10.0, sparseDistributedVector.get(i), PRECISION); - - sparseDistributedVector.divide(10.0); - for (int i = 0; i < sparseDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 1.0, sparseDistributedVector.get(i), PRECISION); - - // assertEquals(UNEXPECTED_VAL, sparseDistributedVector.rowSize() * sparseDistributedVector.columnSize(), sparseDistributedVector.sum(), PRECISION); - } - - /** */ - public void testMap() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseDistributedVector = new SparseDistributedVector(size, StorageConstants.RANDOM_ACCESS_MODE); - initVector(sparseDistributedVector); - - sparseDistributedVector.map(i -> 100.0); - for (int i = 0; i < sparseDistributedVector.size(); i++) - assertEquals(UNEXPECTED_VAL, 100.0, sparseDistributedVector.get(i), PRECISION); - } - - /** */ - public void testCopy() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseDistributedVector = new SparseDistributedVector(size, StorageConstants.RANDOM_ACCESS_MODE); - - Vector cp = sparseDistributedVector.copy(); - - assertNotNull(cp); - - for (int i = 0; i < size; i++) - assertEquals(UNEXPECTED_VAL, cp.get(i), sparseDistributedVector.get(i), PRECISION); - } - - /** */ - public void testLike() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - sparseDistributedVector = new SparseDistributedVector(size, StorageConstants.RANDOM_ACCESS_MODE); - - assertNotNull(sparseDistributedVector.like(1)); - } - - /** */ - private void initVector(Vector v) { - for (int i = 0; i < v.size(); i++) - v.set(i, 1.0); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsFixtures.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsFixtures.java index be3bb227297b0..91200db00613b 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsFixtures.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsFixtures.java @@ -17,9 +17,6 @@ package org.apache.ignite.ml.math.impls.vector; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; @@ -36,7 +33,6 @@ import org.apache.ignite.ml.math.StorageConstants; import org.apache.ignite.ml.math.Vector; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.storage.vector.FunctionVectorStorage; import org.jetbrains.annotations.NotNull; import static org.junit.Assert.assertEquals; @@ -49,13 +45,7 @@ class VectorImplementationsFixtures { (Supplier>)DenseLocalOnHeapVectorFixture::new, (Supplier>)DenseLocalOffHeapVectorFixture::new, (Supplier>)SparseLocalVectorFixture::new, - (Supplier>)RandomVectorFixture::new, - (Supplier>)ConstantVectorFixture::new, (Supplier>)DelegatingVectorFixture::new, - (Supplier>)FunctionVectorFixture::new, - (Supplier>)SingleElementVectorFixture::new, - (Supplier>)PivotedVectorViewFixture::new, - (Supplier>)SingleElementVectorViewFixture::new, (Supplier>)MatrixVectorViewFixture::new, (Supplier>)SparseLocalOffHeapVectorFixture::new ); @@ -110,134 +100,7 @@ private static class SparseLocalVectorFixture extends VectorSizesExtraFixture { - /** */ - ConstantVectorFixture() { - super("ConstantVector", ConstantVector::new, - "value", new Double[] {-1.0, 0.0, 0.5, 1.0, 2.0, null}); - } - } - /** */ - private static class FunctionVectorFixture extends VectorSizesExtraFixture { - /** */ - FunctionVectorFixture() { - super("FunctionVector", - (size, scale) -> new FunctionVectorForTest(new double[size], scale), - "scale", new Double[] {0.5, 1.0, 2.0, null}); - } - } - - /** */ - private static class SingleElementVectorFixture implements Iterable { - /** */ - private final Supplier> iter; - - /** */ - private final AtomicReference ctxDescrHolder = new AtomicReference<>("Iterator not started."); - - /** */ - SingleElementVectorFixture() { - iter = () -> new TwoParamsIterator("SingleElementVector", - null, ctxDescrHolder::set, - "size", new Integer[] {1, null}, - "value", new Double[] {-1.0, 0.0, 0.5, 1.0, 2.0, null}) { - - /** {@inheritDoc} */ - @Override BiFunction ctor() { - return (size, value) -> new SingleElementVector(size, 0, value); - } - }; - } - - /** {@inheritDoc} */ - @NotNull - @Override public Iterator iterator() { - return iter.get();//( - } - - /** {@inheritDoc} */ - @Override public String toString() { - // IMPL NOTE index within bounds is expected to be guaranteed by proper code in this class - return ctxDescrHolder.get(); - } - } - - /** */ - private static class PivotedVectorViewFixture extends VectorSizesFixture { - /** */ - PivotedVectorViewFixture() { - super("PivotedVectorView", PivotedVectorViewFixture::pivotedVectorView); - } - - /** */ - private static PivotedVectorView pivotedVectorView(int size) { - final DenseLocalOnHeapVector vec = new DenseLocalOnHeapVector(size); - - final int[] pivot = new int[size]; - - for (int idx = 0; idx < size; idx++) - pivot[idx] = size - 1 - idx; - - PivotedVectorView tmp = new PivotedVectorView(vec, pivot); - - final int[] unpivot = new int[size]; - - for (int idx = 0; idx < size; idx++) - unpivot[idx] = tmp.unpivot(idx); - - final int[] idxRecovery = new int[size]; - - for (int idx = 0; idx < size; idx++) - idxRecovery[idx] = idx; - - return new PivotedVectorView(new PivotedVectorView(tmp, unpivot), idxRecovery); - } - } - - /** */ - private static class SingleElementVectorViewFixture implements Iterable { - /** */ - private final Supplier> iter; - - /** */ - private final AtomicReference ctxDescrHolder = new AtomicReference<>("Iterator not started."); - - /** */ - SingleElementVectorViewFixture() { - iter = () -> new TwoParamsIterator("SingleElementVectorView", - null, ctxDescrHolder::set, - "size", new Integer[] {1, null}, - "value", new Double[] {-1.0, 0.0, 0.5, 1.0, 2.0, null}) { - - /** {@inheritDoc} */ - @Override BiFunction ctor() { - return (size, value) -> new SingleElementVectorView(new SingleElementVector(size, 0, value), 0); - } - }; - } - - /** {@inheritDoc} */ - @NotNull - @Override public Iterator iterator() { - return iter.get(); - } - - /** {@inheritDoc} */ - @Override public String toString() { - // IMPL NOTE index within bounds is expected to be guaranteed by proper code in this class - return ctxDescrHolder.get(); - } - } /** */ private static class MatrixVectorViewFixture extends VectorSizesExtraFixture { @@ -577,72 +440,6 @@ private static class DelegatingVectorFixture implements Iterable { } } - /** Subclass tweaked for serialization */ - private static class FunctionVectorForTest extends FunctionVector { - /** */ - double[] arr; - - /** */ - double scale; - - /** */ - public FunctionVectorForTest() { - // No-op. - } - - /** */ - FunctionVectorForTest(double[] arr, double scale) { - super(arr.length, idx -> arr[idx] * scale, (idx, value) -> arr[idx] = value / scale); - - this.arr = arr; - - this.scale = scale; - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeObject(arr); - - out.writeDouble(scale); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - arr = (double[])in.readObject(); - - scale = in.readDouble(); - - setStorage(new FunctionVectorStorage(arr.length, idx -> arr[idx] * scale, (idx, value) -> arr[idx] = value / scale)); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + Double.hashCode(scale); - res = res * 37 + Integer.hashCode(getStorage().size()); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - FunctionVectorForTest that = (FunctionVectorForTest)o; - - return new Double(scale).equals(that.scale) - && (arr != null ? Arrays.equals(arr, that.arr) : that.arr == null); - } - } /** */ private static class SparseLocalOffHeapVectorFixture extends VectorSizesFixture { diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java deleted file mode 100644 index 8075b294354d7..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java +++ /dev/null @@ -1,861 +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 java.util.Arrays; -import java.util.concurrent.atomic.AtomicReference; -import java.util.function.BiConsumer; -import java.util.function.BiFunction; -import java.util.function.Consumer; -import java.util.function.Function; -import org.apache.ignite.IgniteException; -import org.apache.ignite.ml.math.ExternalizeTest; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** See also: {@link AbstractVectorTest} and {@link VectorToMatrixTest}. */ -public class VectorImplementationsTest { // TODO: IGNTIE-5723, split this to smaller cohesive test classes - /** */ - @Test - public void vectorImplementationsFixturesTest() { - new VectorImplementationsFixtures().selfTest(); - } - - /** */ - @Test - public void setGetTest() { - consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { - vec.set(idx, val); - - return val; - })); - } - - /** */ - @Test - public void setXTest() { - consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { - vec.setX(idx, val); - - return val; - })); - } - - /** */ - @Test - public void incrementTest() { - consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { - double old = vec.get(idx); - - vec.increment(idx, val); - - return old + val; - })); - } - - /** */ - @Test - public void incrementXTest() { - consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { - double old = vec.getX(idx); - - vec.incrementX(idx, val); - - return old + val; - })); - } - - /** */ - @Test - public void operateXOutOfBoundsTest() { - consumeSampleVectors((v, desc) -> { - if (v instanceof DenseLocalOffHeapVector || v instanceof SparseLocalVector || v instanceof SparseLocalOffHeapVector) - return; // TODO: IGNTIE-5723, find out if it's OK to skip by instances here - - boolean expECaught = false; - - try { - v.getX(-1); - } - catch (ArrayIndexOutOfBoundsException | IgniteException e) { - expECaught = true; - } - - if (!getXOutOfBoundsOK(v)) - assertTrue("Expect exception at negative index getX in " + desc, expECaught); - - expECaught = false; - - try { - v.setX(-1, 0); - } - catch (ArrayIndexOutOfBoundsException | IgniteException e) { - expECaught = true; - } - - assertTrue("Expect exception at negative index setX in " + desc, expECaught); - - expECaught = false; - - try { - v.incrementX(-1, 1); - } - catch (ArrayIndexOutOfBoundsException | IgniteException e) { - expECaught = true; - } - - assertTrue("Expect exception at negative index incrementX in " + desc, expECaught); - - expECaught = false; - - try { - v.getX(v.size()); - } - catch (ArrayIndexOutOfBoundsException | IgniteException e) { - expECaught = true; - } - - if (!getXOutOfBoundsOK(v)) - assertTrue("Expect exception at too large index getX in " + desc, expECaught); - - expECaught = false; - - try { - v.setX(v.size(), 1); - } - catch (ArrayIndexOutOfBoundsException | IgniteException e) { - expECaught = true; - } - - assertTrue("Expect exception at too large index setX in " + desc, expECaught); - - expECaught = false; - - try { - v.incrementX(v.size(), 1); - } - catch (ArrayIndexOutOfBoundsException | IgniteException e) { - expECaught = true; - } - - assertTrue("Expect exception at too large index incrementX in " + desc, expECaught); - }); - } - - /** */ - @Test - public void sizeTest() { - final AtomicReference expSize = new AtomicReference<>(0); - - consumeSampleVectors( - expSize::set, - (v, desc) -> Assert.assertEquals("Expected size for " + desc, - (int)expSize.get(), v.size()) - ); - } - - /** */ - @Test - public void getElementTest() { - consumeSampleVectors((v, desc) -> new ElementsChecker(v, desc).assertCloseEnough(v)); - } - - /** */ - @Test - public void copyTest() { - consumeSampleVectors((v, desc) -> new ElementsChecker(v, desc).assertCloseEnough(v.copy())); - } - - /** */ - @Test - public void divideTest() { - operationTest((val, operand) -> val / operand, Vector::divide); - } - - /** */ - @Test - public void likeTest() { - for (int card : new int[] {1, 2, 4, 8, 16, 32, 64, 128}) - consumeSampleVectors((v, desc) -> { - Class expType = expLikeType(v); - - if (expType == null) { - try { - v.like(card); - } - catch (UnsupportedOperationException uoe) { - return; - } - - fail("Expected exception wasn't caught for " + desc); - - return; - } - - Vector vLike = v.like(card); - - assertNotNull("Expect non-null like vector for " + expType.getSimpleName() + " in " + desc, vLike); - assertEquals("Expect size equal to cardinality at " + desc, card, vLike.size()); - - Class actualType = vLike.getClass(); - - assertTrue("Actual vector type " + actualType.getSimpleName() - + " should be assignable from expected type " + expType.getSimpleName() + " in " + desc, - actualType.isAssignableFrom(expType)); - }); - } - - /** */ - @Test - public void minusTest() { - operationVectorTest((operand1, operand2) -> operand1 - operand2, Vector::minus); - } - - /** */ - @Test - public void plusVectorTest() { - operationVectorTest((operand1, operand2) -> operand1 + operand2, Vector::plus); - } - - /** */ - @Test - public void plusDoubleTest() { - operationTest((val, operand) -> val + operand, Vector::plus); - } - - /** */ - @Test - public void timesVectorTest() { - operationVectorTest((operand1, operand2) -> operand1 * operand2, Vector::times); - } - - /** */ - @Test - public void timesDoubleTest() { - operationTest((val, operand) -> val * operand, Vector::times); - } - - /** */ - @Test - public void viewPartTest() { - consumeSampleVectors((v, desc) -> { - final int size = v.size(); - final double[] ref = new double[size]; - final int delta = size > 32 ? 3 : 1; // IMPL NOTE this is for faster test execution - - final ElementsChecker checker = new ElementsChecker(v, ref, desc); - - for (int off = 0; off < size; off += delta) - for (int len = 1; len < size - off; len += delta) - checker.assertCloseEnough(v.viewPart(off, len), Arrays.copyOfRange(ref, off, off + len)); - }); - } - - /** */ - @Test - public void sumTest() { - toDoubleTest( - ref -> Arrays.stream(ref).sum(), - Vector::sum); - } - - /** */ - @Test - public void minValueTest() { - toDoubleTest( - ref -> Arrays.stream(ref).min().getAsDouble(), - Vector::minValue); - } - - /** */ - @Test - public void maxValueTest() { - toDoubleTest( - ref -> Arrays.stream(ref).max().getAsDouble(), - Vector::maxValue); - } - - /** */ - @Test - public void sortTest() { - consumeSampleVectors((v, desc) -> { - if (readOnly(v) || !v.isArrayBased()) { - boolean expECaught = false; - - try { - v.sort(); - } - catch (UnsupportedOperationException uoe) { - expECaught = true; - } - - assertTrue("Expected exception was not caught for sort in " + desc, expECaught); - - return; - } - - final int size = v.size(); - final double[] ref = new double[size]; - - new ElementsChecker(v, ref, desc).assertCloseEnough(v.sort(), Arrays.stream(ref).sorted().toArray()); - }); - } - - /** */ - @Test - public void metaAttributesTest() { - consumeSampleVectors((v, desc) -> { - assertNotNull("Null meta storage in " + desc, v.getMetaStorage()); - - final String key = "test key"; - final String val = "test value"; - final String details = "key [" + key + "] for " + desc; - - v.setAttribute(key, val); - assertTrue("Expect to have meta attribute for " + details, v.hasAttribute(key)); - assertEquals("Unexpected meta attribute value for " + details, val, v.getAttribute(key)); - - v.removeAttribute(key); - assertFalse("Expect not to have meta attribute for " + details, v.hasAttribute(key)); - assertNull("Unexpected meta attribute value for " + details, v.getAttribute(key)); - }); - } - - /** */ - @Test - public void assignDoubleTest() { - consumeSampleVectors((v, desc) -> { - if (readOnly(v)) - return; - - for (double val : new double[] {0, -1, 0, 1}) { - v.assign(val); - - for (int idx = 0; idx < v.size(); idx++) { - final Metric metric = new Metric(val, v.get(idx)); - - assertTrue("Not close enough at index " + idx + ", val " + val + ", " + metric - + ", " + desc, metric.closeEnough()); - } - } - }); - } - - /** */ - @Test - public void assignDoubleArrTest() { - consumeSampleVectors((v, desc) -> { - if (readOnly(v)) - return; - - final int size = v.size(); - final double[] ref = new double[size]; - - final ElementsChecker checker = new ElementsChecker(v, ref, desc); - - for (int idx = 0; idx < size; idx++) - ref[idx] = -ref[idx]; - - v.assign(ref); - - checker.assertCloseEnough(v, ref); - - assignDoubleArrWrongCardinality(v, desc); - }); - } - - /** */ - @Test - public void assignVectorTest() { - consumeSampleVectors((v, desc) -> { - if (readOnly(v)) - return; - - final int size = v.size(); - final double[] ref = new double[size]; - - final ElementsChecker checker = new ElementsChecker(v, ref, desc); - - for (int idx = 0; idx < size; idx++) - ref[idx] = -ref[idx]; - - v.assign(new DenseLocalOnHeapVector(ref)); - - checker.assertCloseEnough(v, ref); - - assignVectorWrongCardinality(v, desc); - }); - } - - /** */ - @Test - public void assignFunctionTest() { - consumeSampleVectors((v, desc) -> { - if (readOnly(v)) - return; - - final int size = v.size(); - final double[] ref = new double[size]; - - final ElementsChecker checker = new ElementsChecker(v, ref, desc); - - for (int idx = 0; idx < size; idx++) - ref[idx] = -ref[idx]; - - v.assign((idx) -> ref[idx]); - - checker.assertCloseEnough(v, ref); - }); - } - - /** */ - @Test - public void minElementTest() { - consumeSampleVectors((v, desc) -> { - final ElementsChecker checker = new ElementsChecker(v, desc); - - final Vector.Element minE = v.minElement(); - - final int minEIdx = minE.index(); - - assertTrue("Unexpected index from minElement " + minEIdx + ", " + desc, - minEIdx >= 0 && minEIdx < v.size()); - - final Metric metric = new Metric(minE.get(), v.minValue()); - - assertTrue("Not close enough minElement at index " + minEIdx + ", " + metric - + ", " + desc, metric.closeEnough()); - - checker.assertNewMinElement(v); - }); - } - - /** */ - @Test - public void maxElementTest() { - consumeSampleVectors((v, desc) -> { - final ElementsChecker checker = new ElementsChecker(v, desc); - - final Vector.Element maxE = v.maxElement(); - - final int minEIdx = maxE.index(); - - assertTrue("Unexpected index from minElement " + minEIdx + ", " + desc, - minEIdx >= 0 && minEIdx < v.size()); - - final Metric metric = new Metric(maxE.get(), v.maxValue()); - - assertTrue("Not close enough maxElement at index " + minEIdx + ", " + metric - + ", " + desc, metric.closeEnough()); - - checker.assertNewMaxElement(v); - }); - } - - /** */ - @Test - public void externalizeTest() { - (new ExternalizeTest() { - /** {@inheritDoc} */ - @Override public void externalizeTest() { - consumeSampleVectors((v, desc) -> { - if (v instanceof SparseLocalOffHeapVector) - return; //TODO: IGNITE-5801, wait till SparseLocalOffHeapVector externalization support. - - externalizeTest(v); - }); - } - }).externalizeTest(); - } - - /** */ - @Test - public void hashCodeTest() { - consumeSampleVectors((v, desc) -> assertTrue("Zero hash code for " + desc, v.hashCode() != 0)); - } - - /** */ - private boolean getXOutOfBoundsOK(Vector v) { - // TODO: IGNTIE-5723, find out if this is indeed OK - return v instanceof RandomVector || v instanceof ConstantVector - || v instanceof SingleElementVector || v instanceof SingleElementVectorView; - } - - /** */ - private void mutateAtIdxTest(Vector v, String desc, MutateAtIdx operation) { - if (readOnly(v)) { - if (v.size() < 1) - return; - - boolean expECaught = false; - - try { - operation.apply(v, 0, 1); - } - catch (UnsupportedOperationException uoe) { - expECaught = true; - } - - assertTrue("Expect exception at attempt to mutate element in " + desc, expECaught); - - return; - } - - for (double val : new double[] {0, -1, 0, 1}) - for (int idx = 0; idx < v.size(); idx++) { - double exp = operation.apply(v, idx, val); - - final Metric metric = new Metric(exp, v.get(idx)); - - assertTrue("Not close enough at index " + idx + ", val " + val + ", " + metric - + ", " + desc, metric.closeEnough()); - } - } - - /** */ - private Class expLikeType(Vector v) { - Class clazz = v.getClass(); - - if (clazz.isAssignableFrom(PivotedVectorView.class) || clazz.isAssignableFrom(SingleElementVectorView.class)) - return null; - - if (clazz.isAssignableFrom(MatrixVectorView.class) || clazz.isAssignableFrom(DelegatingVector.class)) - return DenseLocalOnHeapVector.class; // IMPL NOTE per fixture - - return clazz; - } - - /** */ - private void toDoubleTest(Function calcRef, Function calcVec) { - consumeSampleVectors((v, desc) -> { - final int size = v.size(); - final double[] ref = new double[size]; - - new ElementsChecker(v, ref, desc); // IMPL NOTE this initialises vector and reference array - - final Metric metric = new Metric(calcRef.apply(ref), calcVec.apply(v)); - - assertTrue("Not close enough at " + desc - + ", " + metric, metric.closeEnough()); - }); - } - - /** */ - private void operationVectorTest(BiFunction operation, - BiFunction vecOperation) { - consumeSampleVectors((v, desc) -> { - // TODO : IGNTIE-5723, find out if more elaborate testing scenario is needed or it's okay as is. - final int size = v.size(); - final double[] ref = new double[size]; - - final ElementsChecker checker = new ElementsChecker(v, ref, desc); - final Vector operand = v.copy(); - - for (int idx = 0; idx < size; idx++) - ref[idx] = operation.apply(ref[idx], ref[idx]); - - checker.assertCloseEnough(vecOperation.apply(v, operand), ref); - - assertWrongCardinality(v, desc, vecOperation); - }); - } - - /** */ - private void assignDoubleArrWrongCardinality(Vector v, String desc) { - boolean expECaught = false; - - try { - v.assign(new double[v.size() + 1]); - } - catch (CardinalityException ce) { - expECaught = true; - } - - assertTrue("Expect exception at too large size in " + desc, expECaught); - - if (v.size() < 2) - return; - - expECaught = false; - - try { - v.assign(new double[v.size() - 1]); - } - catch (CardinalityException ce) { - expECaught = true; - } - - assertTrue("Expect exception at too small size in " + desc, expECaught); - } - - /** */ - private void assignVectorWrongCardinality(Vector v, String desc) { - boolean expECaught = false; - - try { - v.assign(new DenseLocalOnHeapVector(v.size() + 1)); - } - catch (CardinalityException ce) { - expECaught = true; - } - - assertTrue("Expect exception at too large size in " + desc, expECaught); - - if (v.size() < 2) - return; - - expECaught = false; - - try { - v.assign(new DenseLocalOnHeapVector(v.size() - 1)); - } - catch (CardinalityException ce) { - expECaught = true; - } - - assertTrue("Expect exception at too small size in " + desc, expECaught); - } - - /** */ - private void assertWrongCardinality( - Vector v, String desc, BiFunction vecOperation) { - boolean expECaught = false; - - try { - vecOperation.apply(v, new DenseLocalOnHeapVector(v.size() + 1)); - } - catch (CardinalityException ce) { - expECaught = true; - } - - assertTrue("Expect exception at too large size in " + desc, expECaught); - - if (v.size() < 2) - return; - - expECaught = false; - - try { - vecOperation.apply(v, new DenseLocalOnHeapVector(v.size() - 1)); - } - catch (CardinalityException ce) { - expECaught = true; - } - - assertTrue("Expect exception at too small size in " + desc, expECaught); - } - - /** */ - private void operationTest(BiFunction operation, - BiFunction vecOperation) { - for (double val : new double[] {0, 0.1, 1, 2, 10}) - consumeSampleVectors((v, desc) -> { - final int size = v.size(); - final double[] ref = new double[size]; - - final ElementsChecker checker = new ElementsChecker(v, ref, "val " + val + ", " + desc); - - for (int idx = 0; idx < size; idx++) - ref[idx] = operation.apply(ref[idx], val); - - checker.assertCloseEnough(vecOperation.apply(v, val), ref); - }); - } - - /** */ - private void consumeSampleVectors(BiConsumer consumer) { - consumeSampleVectors(null, consumer); - } - - /** */ - private void consumeSampleVectors(Consumer paramsConsumer, BiConsumer consumer) { - new VectorImplementationsFixtures().consumeSampleVectors(paramsConsumer, consumer); - } - - /** */ - private static boolean readOnly(Vector v) { - return v instanceof RandomVector || v instanceof ConstantVector; - } - - /** */ - private interface MutateAtIdx { - /** */ - double apply(Vector v, int idx, double val); - } - - /** */ - static class ElementsChecker { - /** */ - private final String fixtureDesc; - - /** */ - private final double[] refReadOnly; - - /** */ - private final boolean nonNegative; - - /** */ - ElementsChecker(Vector v, double[] ref, String fixtureDesc, boolean nonNegative) { - this.fixtureDesc = fixtureDesc; - - this.nonNegative = nonNegative; - - refReadOnly = readOnly(v) && ref == null ? new double[v.size()] : null; - - init(v, ref); - } - - /** */ - ElementsChecker(Vector v, double[] ref, String fixtureDesc) { - this(v, ref, fixtureDesc, false); - } - - /** */ - ElementsChecker(Vector v, String fixtureDesc) { - this(v, null, fixtureDesc); - } - - /** */ - void assertCloseEnough(Vector obtained, double[] exp) { - final int size = obtained.size(); - - for (int i = 0; i < size; i++) { - final Vector.Element e = obtained.getElement(i); - - if (refReadOnly != null && exp == null) - exp = refReadOnly; - - final Metric metric = new Metric(exp == null ? generated(i) : exp[i], e.get()); - - assertEquals("Unexpected vector index at " + fixtureDesc, i, e.index()); - assertTrue("Not close enough at index " + i + ", size " + size + ", " + metric - + ", " + fixtureDesc, metric.closeEnough()); - } - } - - /** */ - void assertCloseEnough(Vector obtained) { - assertCloseEnough(obtained, null); - } - - /** */ - void assertNewMinElement(Vector v) { - if (readOnly(v)) - return; - - int exp = v.size() / 2; - - v.set(exp, -(v.size() * 2 + 1)); - - assertEquals("Unexpected minElement index at " + fixtureDesc, exp, v.minElement().index()); - } - - /** */ - void assertNewMaxElement(Vector v) { - if (readOnly(v)) - return; - - int exp = v.size() / 2; - - v.set(exp, v.size() * 2 + 1); - - assertEquals("Unexpected minElement index at " + fixtureDesc, exp, v.maxElement().index()); - } - - /** */ - private void init(Vector v, double[] ref) { - if (readOnly(v)) { - initReadonly(v, ref); - - return; - } - - for (Vector.Element e : v.all()) { - int idx = e.index(); - - // IMPL NOTE introduce negative values because their absence - // blocked catching an ugly bug in AbstractVector#kNorm - int val = generated(idx); - - e.set(val); - - if (ref != null) - ref[idx] = val; - } - } - - /** */ - private void initReadonly(Vector v, double[] ref) { - if (refReadOnly != null) - for (Vector.Element e : v.all()) - refReadOnly[e.index()] = e.get(); - - if (ref != null) - for (Vector.Element e : v.all()) - ref[e.index()] = e.get(); - } - - /** */ - private int generated(int idx) { - return nonNegative || (idx & 1) == 0 ? idx : -idx; - } - } - - /** */ - static class Metric { //TODO: IGNITE-5824, consider if softer tolerance (like say 0.1 or 0.01) would make sense here - /** */ - private final double exp; - - /** */ - private final double obtained; - - /** **/ - Metric(double exp, double obtained) { - this.exp = exp; - this.obtained = obtained; - } - - /** */ - boolean closeEnough() { - return new Double(exp).equals(obtained) || closeEnoughToZero(); - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "Metric{" + "expected=" + exp + - ", obtained=" + obtained + - '}'; - } - - /** */ - private boolean closeEnoughToZero() { - return (new Double(exp).equals(0.0) && new Double(obtained).equals(-0.0)) - || (new Double(exp).equals(-0.0) && new Double(obtained).equals(0.0)); - } - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorIterableTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorIterableTest.java deleted file mode 100644 index 6400c80a7cdf9..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorIterableTest.java +++ /dev/null @@ -1,376 +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 java.util.Arrays; -import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.Spliterator; -import java.util.function.BiConsumer; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.MathTestConstants; -import org.junit.Test; - -import static java.util.Spliterator.ORDERED; -import static java.util.Spliterator.SIZED; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** */ -public class VectorIterableTest { - /** */ - @Test - public void allTest() { - consumeSampleVectors( - (v, desc) -> { - int expIdx = 0; - - for (Vector.Element e : v.all()) { - int actualIdx = e.index(); - - assertEquals("Unexpected index for " + desc, - expIdx, actualIdx); - - expIdx++; - } - - assertEquals("Unexpected amount of elements for " + desc, - expIdx, v.size()); - } - ); - } - - /** */ - @Test - public void allTestBound() { - consumeSampleVectors( - (v, desc) -> iteratorTestBound(v.all().iterator(), desc) - ); - } - - /** */ - @Test - public void nonZeroesTestBasic() { - final int size = 5; - - final double[] nonZeroesOddData = new double[size], nonZeroesEvenData = new double[size]; - - for (int idx = 0; idx < size; idx++) { - final boolean odd = (idx & 1) == 1; - - nonZeroesOddData[idx] = odd ? 1 : 0; - - nonZeroesEvenData[idx] = odd ? 0 : 1; - } - - assertTrue("Arrays failed to initialize.", - !isZero(nonZeroesEvenData[0]) - && isZero(nonZeroesEvenData[1]) - && isZero(nonZeroesOddData[0]) - && !isZero(nonZeroesOddData[1])); - - final Vector nonZeroesEvenVec = new DenseLocalOnHeapVector(nonZeroesEvenData), - nonZeroesOddVec = new DenseLocalOnHeapVector(nonZeroesOddData); - - assertTrue("Vectors failed to initialize.", - !isZero(nonZeroesEvenVec.getElement(0).get()) - && isZero(nonZeroesEvenVec.getElement(1).get()) - && isZero(nonZeroesOddVec.getElement(0).get()) - && !isZero(nonZeroesOddVec.getElement(1).get())); - - assertTrue("Iterator(s) failed to start.", - nonZeroesEvenVec.nonZeroes().iterator().next() != null - && nonZeroesOddVec.nonZeroes().iterator().next() != null); - - int nonZeroesActual = 0; - - for (Vector.Element e : nonZeroesEvenVec.nonZeroes()) { - final int idx = e.index(); - - final boolean odd = (idx & 1) == 1; - - final double val = e.get(); - - assertTrue("Not an even index " + idx + ", for value " + val, !odd); - - assertTrue("Zero value " + val + " at even index " + idx, !isZero(val)); - - nonZeroesActual++; - } - - final int nonZeroesOddExp = (size + 1) / 2; - - assertEquals("Unexpected num of iterated odd non-zeroes.", nonZeroesOddExp, nonZeroesActual); - - assertEquals("Unexpected nonZeroElements of odd.", nonZeroesOddExp, nonZeroesEvenVec.nonZeroElements()); - - nonZeroesActual = 0; - - for (Vector.Element e : nonZeroesOddVec.nonZeroes()) { - final int idx = e.index(); - - final boolean odd = (idx & 1) == 1; - - final double val = e.get(); - - assertTrue("Not an odd index " + idx + ", for value " + val, odd); - - assertTrue("Zero value " + val + " at even index " + idx, !isZero(val)); - - nonZeroesActual++; - } - - final int nonZeroesEvenExp = size / 2; - - assertEquals("Unexpected num of iterated even non-zeroes", nonZeroesEvenExp, nonZeroesActual); - - assertEquals("Unexpected nonZeroElements of even", nonZeroesEvenExp, nonZeroesOddVec.nonZeroElements()); - } - - /** */ - @Test - public void nonZeroesTest() { - // TODO: IGNTIE-5723, make RandomVector constructor that accepts a function and use it here. - // in order to *reliably* test non-zeroes in there - consumeSampleVectors( - (v, desc) -> consumeSampleVectorsWithZeroes(v, (vec, numZeroes) - -> { - int numZeroesActual = vec.size(); - - for (Vector.Element e : vec.nonZeroes()) { - numZeroesActual--; - - assertTrue("Unexpected zero at " + desc + ", index " + e.index(), !isZero(e.get())); - } - - assertEquals("Unexpected num zeroes at " + desc, (int)numZeroes, numZeroesActual); - })); - } - - /** */ - @Test - public void nonZeroesTestBound() { - consumeSampleVectors( - (v, desc) -> consumeSampleVectorsWithZeroes(v, (vec, numZeroes) - -> iteratorTestBound(vec.nonZeroes().iterator(), desc))); - } - - /** */ - @Test - public void nonZeroElementsTest() { - consumeSampleVectors( - (v, desc) -> consumeSampleVectorsWithZeroes(v, (vec, numZeroes) - -> assertEquals("Unexpected num zeroes at " + desc, - (int)numZeroes, vec.size() - vec.nonZeroElements()))); - } - - /** */ - @Test - public void allSpliteratorTest() { - consumeSampleVectors( - (v, desc) -> { - final String desc1 = " " + desc; - - Spliterator spliterator = v.allSpliterator(); - - assertNotNull(MathTestConstants.NULL_VAL + desc1, spliterator); - - assertNull(MathTestConstants.NOT_NULL_VAL + desc1, spliterator.trySplit()); - - assertTrue(MathTestConstants.UNEXPECTED_VAL + desc1, spliterator.hasCharacteristics(ORDERED | SIZED)); - - if (!readOnly(v)) - fillWithNonZeroes(v); - - spliterator = v.allSpliterator(); - - assertNotNull(MathTestConstants.NULL_VAL + desc1, spliterator); - - assertEquals(MathTestConstants.VAL_NOT_EQUALS + desc1, spliterator.estimateSize(), v.size()); - - assertEquals(MathTestConstants.VAL_NOT_EQUALS + desc1, spliterator.getExactSizeIfKnown(), v.size()); - - assertTrue(MathTestConstants.UNEXPECTED_VAL + desc1, spliterator.hasCharacteristics(ORDERED | SIZED)); - - Spliterator secondHalf = spliterator.trySplit(); - - assertNull(MathTestConstants.NOT_NULL_VAL + desc1, secondHalf); - - spliterator.tryAdvance(x -> { - }); - } - ); - } - - /** */ - @Test - public void nonZeroSpliteratorTest() { - consumeSampleVectors( - (v, desc) -> consumeSampleVectorsWithZeroes(v, (vec, numZeroes) - -> { - final String desc1 = " Num zeroes " + numZeroes + " " + desc; - - Spliterator spliterator = vec.nonZeroSpliterator(); - - assertNotNull(MathTestConstants.NULL_VAL + desc1, spliterator); - - assertNull(MathTestConstants.NOT_NULL_VAL + desc1, spliterator.trySplit()); - - assertTrue(MathTestConstants.UNEXPECTED_VAL + desc1, spliterator.hasCharacteristics(ORDERED | SIZED)); - - spliterator = vec.nonZeroSpliterator(); - - assertNotNull(MathTestConstants.NULL_VAL + desc1, spliterator); - - assertEquals(MathTestConstants.VAL_NOT_EQUALS + desc1, spliterator.estimateSize(), vec.size() - numZeroes); - - assertEquals(MathTestConstants.VAL_NOT_EQUALS + desc1, spliterator.getExactSizeIfKnown(), vec.size() - numZeroes); - - assertTrue(MathTestConstants.UNEXPECTED_VAL + desc1, spliterator.hasCharacteristics(ORDERED | SIZED)); - - Spliterator secondHalf = spliterator.trySplit(); - - assertNull(MathTestConstants.NOT_NULL_VAL + desc1, secondHalf); - - double[] data = new double[vec.size()]; - - for (Vector.Element e : vec.all()) - data[e.index()] = e.get(); - - spliterator = vec.nonZeroSpliterator(); - - assertNotNull(MathTestConstants.NULL_VAL + desc1, spliterator); - - assertEquals(MathTestConstants.VAL_NOT_EQUALS + desc1, spliterator.estimateSize(), - Arrays.stream(data).filter(x -> x != 0d).count()); - - assertEquals(MathTestConstants.VAL_NOT_EQUALS + desc1, spliterator.getExactSizeIfKnown(), - Arrays.stream(data).filter(x -> x != 0d).count()); - - assertTrue(MathTestConstants.UNEXPECTED_VAL + desc1, spliterator.hasCharacteristics(ORDERED | SIZED)); - - secondHalf = spliterator.trySplit(); - - assertNull(MathTestConstants.NOT_NULL_VAL + desc1, secondHalf); - - if (!spliterator.tryAdvance(x -> { - })) - fail(MathTestConstants.NO_NEXT_ELEMENT + desc1); - })); - } - - /** */ - private void iteratorTestBound(Iterator it, String desc) { - while (it.hasNext()) - assertNotNull(it.next()); - - boolean expECaught = false; - - try { - it.next(); - } - catch (NoSuchElementException e) { - expECaught = true; - } - - assertTrue("Expected exception missed for " + desc, - expECaught); - } - - /** */ - private void consumeSampleVectorsWithZeroes(Vector sample, - BiConsumer consumer) { - if (readOnly(sample)) { - int numZeroes = 0; - - for (Vector.Element e : sample.all()) - if (isZero(e.get())) - numZeroes++; - - consumer.accept(sample, numZeroes); - - return; - } - - fillWithNonZeroes(sample); - - consumer.accept(sample, 0); - - final int sampleSize = sample.size(); - - if (sampleSize == 0) - return; - - for (Vector.Element e : sample.all()) - e.set(0); - - consumer.accept(sample, sampleSize); - - fillWithNonZeroes(sample); - - for (int testIdx : new int[] {0, sampleSize / 2, sampleSize - 1}) { - final Vector.Element e = sample.getElement(testIdx); - - final double backup = e.get(); - - e.set(0); - - consumer.accept(sample, 1); - - e.set(backup); - } - - if (sampleSize < 3) - return; - - sample.getElement(sampleSize / 3).set(0); - - sample.getElement((2 * sampleSize) / 3).set(0); - - consumer.accept(sample, 2); - } - - /** */ - private void fillWithNonZeroes(Vector sample) { - int idx = 0; - - for (Vector.Element e : sample.all()) - e.set(1 + idx++); - - assertEquals("Not all filled with non-zeroes", idx, sample.size()); - } - - /** */ - private void consumeSampleVectors(BiConsumer consumer) { - new VectorImplementationsFixtures().consumeSampleVectors(null, consumer); - } - - /** */ - private boolean isZero(double val) { - return val == 0.0; - } - - /** */ - private boolean readOnly(Vector v) { - return v instanceof RandomVector || v instanceof ConstantVector; - } -} - diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorToMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorToMatrixTest.java index a003dcfd62a85..7a39d023dc13b 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorToMatrixTest.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorToMatrixTest.java @@ -27,7 +27,6 @@ import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrix; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.RandomMatrix; import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix; import org.junit.Test; @@ -41,41 +40,17 @@ public class VectorToMatrixTest { /** */ private static final Map, Class> typesMap = typesMap(); - /** */ - private static final List> likeMatrixUnsupported = Arrays.asList(FunctionVector.class, - SingleElementVector.class, SingleElementVectorView.class, ConstantVector.class); - /** */ @Test - public void testHaveLikeMatrix() throws InstantiationException, IllegalAccessException { + public void testHaveLikeMatrix() { for (Class key : typesMap.keySet()) { Class val = typesMap.get(key); - if (val == null && likeMatrixSupported(key)) + if (val == null) System.out.println("Missing test for implementation of likeMatrix for " + key.getSimpleName()); } } - /** */ - @Test - public void testLikeMatrixUnsupported() throws Exception { - consumeSampleVectors((v, desc) -> { - if (likeMatrixSupported(v.getClass())) - return; - - boolean expECaught = false; - - try { - assertNull("Null view instead of exception in " + desc, v.likeMatrix(1, 1)); - } - catch (UnsupportedOperationException uoe) { - expECaught = true; - } - - assertTrue("Expected exception was not caught in " + desc, expECaught); - }); - } - /** */ @Test public void testLikeMatrix() { @@ -223,9 +198,6 @@ private void assertToMatrixValue(String desc, Matrix matrixRow, Matrix matrixCol /** */ private void fillWithNonZeroes(Vector sample) { - if (sample instanceof RandomVector) - return; - for (Vector.Element e : sample.all()) e.set(1 + e.index()); } @@ -234,9 +206,6 @@ private void fillWithNonZeroes(Vector sample) { private boolean availableForTesting(Vector v) { assertNotNull("Error in test: vector is null", v); - if (!likeMatrixSupported(v.getClass())) - return false; - final boolean availableForTesting = typesMap.get(v.getClass()) != null; final Matrix actualLikeMatrix = v.likeMatrix(1, 1); @@ -247,15 +216,6 @@ private boolean availableForTesting(Vector v) { return availableForTesting; } - /** Ignore test for given vector type. */ - private boolean likeMatrixSupported(Class clazz) { - for (Class ignoredClass : likeMatrixUnsupported) - if (ignoredClass.isAssignableFrom(clazz)) - return false; - - return true; - } - /** */ private void consumeSampleVectors(BiConsumer consumer) { new VectorImplementationsFixtures().consumeSampleVectors(null, consumer); @@ -266,13 +226,7 @@ private static Map, Class> typesMap() return new LinkedHashMap, Class>() {{ put(DenseLocalOnHeapVector.class, DenseLocalOnHeapMatrix.class); put(DenseLocalOffHeapVector.class, DenseLocalOffHeapMatrix.class); - put(RandomVector.class, RandomMatrix.class); put(SparseLocalVector.class, SparseLocalOnHeapMatrix.class); - put(SingleElementVector.class, null); // TODO: IGNTIE-5723, find out if we need SingleElementMatrix to match, or skip it. - put(ConstantVector.class, null); - put(FunctionVector.class, null); - put(PivotedVectorView.class, DenseLocalOnHeapMatrix.class); // IMPL NOTE per fixture - put(SingleElementVectorView.class, null); put(MatrixVectorView.class, DenseLocalOnHeapMatrix.class); // IMPL NOTE per fixture put(DelegatingVector.class, DenseLocalOnHeapMatrix.class); // IMPL NOTE per fixture // IMPL NOTE check for presence of all implementations here will be done in testHaveLikeMatrix via Fixture diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseBlockDistributedMatrixMulBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseBlockDistributedMatrixMulBenchmark.java deleted file mode 100644 index 87626207e58ae..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseBlockDistributedMatrixMulBenchmark.java +++ /dev/null @@ -1,32 +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.yardstick.ml.math; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteSparseBlockDistributedMatrixMulBenchmark extends IgniteAbstractMatrixMulBenchmark { - /** {@inheritDoc} */ - @Override Matrix newMatrix(int rowSize, int colSize) { - return new SparseBlockDistributedMatrix(rowSize, colSize); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMul2Benchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMul2Benchmark.java deleted file mode 100644 index e1afc34bf0438..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMul2Benchmark.java +++ /dev/null @@ -1,74 +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.yardstick.ml.math; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; - -/** - * Ignite benchmark that performs trivially optimized matrix multiplication of {@link SparseDistributedMatrix} that - * uses delegating to {@link SparseBlockDistributedMatrix}. For unoptimized benchmark refer - * {@link IgniteSparseDistributedMatrixMulBenchmark}. - */ -@SuppressWarnings("unused") -public class IgniteSparseDistributedMatrixMul2Benchmark extends IgniteAbstractMatrixMulBenchmark { - /** {@inheritDoc} */ - @Override Matrix newMatrix(int rowSize, int colSize) { - return new SparseDistributedMatrix(rowSize, colSize, - StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** {@inheritDoc} */ - @Override public Matrix times(Matrix m1, Matrix m2) { - return new OptimisedTimes(m1, m2).times(); - } - - /** */ - private static class OptimisedTimes { - /** */ - private final Matrix m1; - - /** */ - private final Matrix m2; - - /** */ - OptimisedTimes(Matrix m1, Matrix m2) { - this.m1 = m1; - this.m2 = m2; - } - - /** */ - Matrix times() { - Matrix m1Block = new SparseBlockDistributedMatrix(m1.rowSize(), m1.columnSize()).assign(m1); - Matrix m2Block = new SparseBlockDistributedMatrix(m2.rowSize(), m2.columnSize()).assign(m2); - - Matrix m3Block = m1Block.times(m2Block); - - Matrix res = new SparseDistributedMatrix(m3Block.rowSize(), m3Block.columnSize(), - StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE).assign(m3Block); - - m1Block.destroy(); - m2Block.destroy(); - m3Block.destroy(); - - return res; - } - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMulBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMulBenchmark.java deleted file mode 100644 index 0f9d288085342..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseDistributedMatrixMulBenchmark.java +++ /dev/null @@ -1,49 +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.yardstick.ml.math; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.StorageConstants; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; - -/** - * Ignite benchmark that performs unoptimized matrix multiplication of {@link SparseDistributedMatrix}. - * For optimized benchmark refer {@link IgniteSparseBlockDistributedMatrixMulBenchmark}. - */ -@SuppressWarnings("unused") -public class IgniteSparseDistributedMatrixMulBenchmark extends IgniteAbstractMatrixMulBenchmark { - /** This benchmark is unacceptably slow without scaling down, see IGNITE-7097. */ - private static final int SCALE_DOWN = 2; - - /** {@inheritDoc} */ - @Override Matrix newMatrix(int rowSize, int colSize) { - return new SparseDistributedMatrix(rowSize >> SCALE_DOWN, colSize >> SCALE_DOWN, - StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** {@inheritDoc} */ - @Override Matrix createAndFill(double[][] data, double scale) { - Matrix res = newMatrix(data.length, data[0].length); - - for (int row = 0; row < res.rowSize(); row++) - for (int col = 0; col < res.columnSize(); col++) - res.set(row, col, row == col ? data[row][col] * scale : data[row][col]); - - return res; - } -} From fb5eb39c0b6f5f009514b5145739646372519f2d Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Tue, 15 May 2018 12:43:35 +0300 Subject: [PATCH 3/5] IGNITE-8450: remove yardstick unused tests --- .../ignite/yardstick/ml/DataChanger.java | 65 ---------- .../IgniteCholeskyDecompositionBenchmark.java | 69 ---------- .../IgniteEigenDecompositionBenchmark.java | 69 ---------- .../IgniteLUDecompositionBenchmark.java | 75 ----------- ...teSingularValueDecompositionBenchmark.java | 59 --------- .../ml/decomposition/package-info.java | 22 ---- .../IgniteAbstractMatrixMulBenchmark.java | 120 ------------------ ...teDenseLocalOffHeapMatrixMulBenchmark.java | 32 ----- ...iteDenseLocalOnHeapMatrixMulBenchmark.java | 32 ----- .../IgniteSparseLocalMatrixMulBenchmark.java | 32 ----- .../yardstick/ml/math/package-info.java | 22 ---- .../ignite/yardstick/ml/package-info.java | 22 ---- 12 files changed, 619 deletions(-) delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/DataChanger.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteCholeskyDecompositionBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteEigenDecompositionBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteLUDecompositionBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteSingularValueDecompositionBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/package-info.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteAbstractMatrixMulBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOffHeapMatrixMulBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOnHeapMatrixMulBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseLocalMatrixMulBenchmark.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/package-info.java delete mode 100644 modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/package-info.java diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/DataChanger.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/DataChanger.java deleted file mode 100644 index 600113b673abc..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/DataChanger.java +++ /dev/null @@ -1,65 +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.yardstick.ml; - -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; -import org.apache.ignite.yardstick.IgniteAbstractBenchmark; - -/** - * Helps to ensure use of meaningfully different data in repeated invocations of benchmarked code in - * {@link IgniteAbstractBenchmark#test(Map)} method. - */ -public class DataChanger { - /** */ - private static final AtomicInteger changer = new AtomicInteger(0); - - /** - * @return Positive "seed" to mutate data. - */ - public static double next() { - return (changer.incrementAndGet() % 17) + 1; - } - - /** */ - public static class Scale { - /** */ - private final double scale; - - /** */ - public Scale() { - this.scale = DataChanger.next(); - } - - /** */ - public double[][] mutate(double[][] orig) { - for (int i = 0; i < orig.length; i++) - orig[i] = mutate(orig[i]); - - return orig; - } - - /** */ - public double[] mutate(double[] orig) { - for (int i = 0; i < orig.length; i++) - orig[i] *= scale; - - return orig; - } - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteCholeskyDecompositionBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteCholeskyDecompositionBenchmark.java deleted file mode 100644 index 6997180e671b3..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteCholeskyDecompositionBenchmark.java +++ /dev/null @@ -1,69 +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.yardstick.ml.decomposition; - -import java.util.Map; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.decompositions.CholeskyDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.yardstick.IgniteAbstractBenchmark; -import org.apache.ignite.yardstick.ml.DataChanger; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteCholeskyDecompositionBenchmark extends IgniteAbstractBenchmark { - /** {@inheritDoc} */ - @Override public boolean test(Map ctx) throws Exception { - runCholeskyDecomposition(); - - return true; - } - - /** - * Based on CholeskyDecompositionTest. - */ - private void runCholeskyDecomposition() { - final DataChanger.Scale scale = new DataChanger.Scale(); - - Matrix m = new DenseLocalOnHeapMatrix(scale.mutate(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - - CholeskyDecomposition dec = new CholeskyDecomposition(m); - - dec.getL(); - dec.getLT(); - - Matrix bs = new DenseLocalOnHeapMatrix(scale.mutate(new double[][] { - {4.0, -6.0, 7.0}, - {1.0, 1.0, 1.0} - })).transpose(); - dec.solve(bs); - - Vector b = new DenseLocalOnHeapVector(scale.mutate(new double[] {4.0, -6.0, 7.0})); - dec.solve(b); - - dec.destroy(); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteEigenDecompositionBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteEigenDecompositionBenchmark.java deleted file mode 100644 index e61c40c4578b9..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteEigenDecompositionBenchmark.java +++ /dev/null @@ -1,69 +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.yardstick.ml.decomposition; - -import java.util.Map; -import org.apache.ignite.ml.math.decompositions.EigenDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.yardstick.IgniteAbstractBenchmark; -import org.apache.ignite.yardstick.ml.DataChanger; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteEigenDecompositionBenchmark extends IgniteAbstractBenchmark { - /** {@inheritDoc} */ - @Override public boolean test(Map ctx) throws Exception { - runEigenDecomposition(); - - return true; - } - - /** - * Based on EigenDecompositionTest. - */ - private void runEigenDecomposition() { - final DataChanger.Scale scale = new DataChanger.Scale(); - - EigenDecomposition decomposition1 = new EigenDecomposition(new DenseLocalOnHeapMatrix( - scale.mutate(new double[][] { - {1.0d, 0.0d, 0.0d, 1.0d}, - {0.0d, 1.0d, 0.0d, 1.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d}}))); - - decomposition1.getD(); - decomposition1.getV(); - - decomposition1.destroy(); - - EigenDecomposition decomposition2 = new EigenDecomposition(new DenseLocalOnHeapMatrix( - scale.mutate(new double[][] { - {1.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d}, - {0.0d, 0.0d, 2.0d}, - {1.0d, 1.0d, 0.0d}}))); - // TODO: IGNITE-5828, find out why decomposition of 3X4 matrix throws row index exception - - decomposition2.getD(); - decomposition2.getV(); - - decomposition2.destroy(); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteLUDecompositionBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteLUDecompositionBenchmark.java deleted file mode 100644 index 1368ef930ed86..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteLUDecompositionBenchmark.java +++ /dev/null @@ -1,75 +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.yardstick.ml.decomposition; - -import java.util.Map; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.decompositions.LUDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.yardstick.IgniteAbstractBenchmark; -import org.apache.ignite.yardstick.ml.DataChanger; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteLUDecompositionBenchmark extends IgniteAbstractBenchmark { - /** {@inheritDoc} */ - @Override public boolean test(Map ctx) throws Exception { - runLUDecomposition(); - - return true; - } - - /** - * Based on LUDecompositionTest. - */ - private void runLUDecomposition() { - Matrix testMatrix = new DenseLocalOnHeapMatrix(new DataChanger.Scale().mutate(new double[][] { - {2.0d, 1.0d, 1.0d, 0.0d}, - {4.0d, 3.0d, 3.0d, 1.0d}, - {8.0d, 7.0d, 9.0d, 5.0d}, - {6.0d, 7.0d, 9.0d, 8.0d}})); - - LUDecomposition dec1 = new LUDecomposition(new PivotedMatrixView(testMatrix)); - - dec1.solve(new DenseLocalOnHeapVector(testMatrix.rowSize())); - - dec1.destroy(); - - LUDecomposition dec2 = new LUDecomposition(new PivotedMatrixView(testMatrix)); - - dec2.solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())); - - dec2.destroy(); - - LUDecomposition dec3 = new LUDecomposition(testMatrix); - - dec3.getL(); - - dec3.getU(); - - dec3.getP(); - - dec3.getPivot(); - - dec3.destroy(); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteSingularValueDecompositionBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteSingularValueDecompositionBenchmark.java deleted file mode 100644 index 9f02852c2ee17..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/IgniteSingularValueDecompositionBenchmark.java +++ /dev/null @@ -1,59 +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.yardstick.ml.decomposition; - -import java.util.Map; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.decompositions.SingularValueDecomposition; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.yardstick.IgniteAbstractBenchmark; -import org.apache.ignite.yardstick.ml.DataChanger; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteSingularValueDecompositionBenchmark extends IgniteAbstractBenchmark { - /** {@inheritDoc} */ - @Override public boolean test(Map ctx) throws Exception { - runSingularValueDecomposition(); - - return true; - } - - /** - * Based on SingularValueDecompositionTest#basicTest. - */ - private void runSingularValueDecomposition() { - Matrix m = new DenseLocalOnHeapMatrix(new DataChanger.Scale().mutate(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - - SingularValueDecomposition dec = new SingularValueDecomposition(m); - - Matrix s = dec.getS(); - Matrix u = dec.getU(); - Matrix v = dec.getV(); - - u.times(s).times(v.transpose()); - - dec.destroy(); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/package-info.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/package-info.java deleted file mode 100644 index 4aaece3ff61ba..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/decomposition/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. - */ - -/** - * - * ML Grid decomposition benchmarks. - */ -package org.apache.ignite.yardstick.ml.decomposition; \ No newline at end of file diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteAbstractMatrixMulBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteAbstractMatrixMulBenchmark.java deleted file mode 100644 index 6cb2200fc7abc..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteAbstractMatrixMulBenchmark.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.yardstick.ml.math; - -import java.util.Map; -import org.apache.ignite.Ignite; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.resources.IgniteInstanceResource; -import org.apache.ignite.thread.IgniteThread; -import org.apache.ignite.yardstick.IgniteAbstractBenchmark; -import org.apache.ignite.yardstick.ml.DataChanger; - -/** - * Ignite benchmark that performs ML Grid operations of matrix multiplication. - */ -abstract class IgniteAbstractMatrixMulBenchmark extends IgniteAbstractBenchmark { - /** */ - private static final int SIZE = 1 << 7; - - /** */ - private double[][] dataSquare = createAndFill(SIZE, SIZE); - - /** */ - private double[][] dataRect1 = createAndFill(SIZE / 2, SIZE); - - /** */ - private double[][] dataRect2 = createAndFill(SIZE, SIZE / 2); - - /** */ - @SuppressWarnings("unused") - @IgniteInstanceResource - private Ignite ignite; - - /** {@inheritDoc} */ - @Override public boolean test(Map ctx) throws Exception { - final double scale = DataChanger.next(); - - // Create IgniteThread, we may want to work with SparseDistributedMatrix inside IgniteThread - // because we create ignite cache internally. - IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), - this.getClass().getSimpleName(), new Runnable() { - /** {@inheritDoc} */ - @Override public void run() { - Matrix m1, m2, m3, m4, m5, m6; - - Matrix m7 = times(m1 = createAndFill(dataSquare, scale), m2 = createAndFill(dataSquare, scale)); - Matrix m8 = times(m3 = createAndFill(dataRect1, scale), m4 = createAndFill(dataRect2, scale)); - Matrix m9 = times(m5 = createAndFill(dataRect2, scale), m6 = createAndFill(dataRect1, scale)); - - m1.destroy(); - m2.destroy(); - m3.destroy(); - m4.destroy(); - m5.destroy(); - m6.destroy(); - m7.destroy(); - m8.destroy(); - m9.destroy(); - } - }); - - igniteThread.start(); - - igniteThread.join(); - - return true; - } - - /** - * Override in subclasses with specific type Matrix. Note that size of result matrix may be smaller than requested. - * - * @param rowSize Requested row size. - * @param colSize Requested column size. - * @return Matrix of desired type of size that doesn't exceed requested. - */ - abstract Matrix newMatrix(int rowSize, int colSize); - - /** Override in subclasses if needed. */ - Matrix times(Matrix m1, Matrix m2) { - return m1.times(m2); - } - - /** Override in subclasses if needed to account for smaller matrix size. */ - Matrix createAndFill(double[][] data, double scale) { - // IMPL NOTE yardstick 0.8.3 fails to discover benchmark if we try to account for smaller size - // matrix by using method with lambda parameter here: assign((row, col) -> data[row][col]). - Matrix res = newMatrix(data.length, data[0].length).assign(data); - - for (int i = 0; i < data.length && i < data[0].length; i++) - res.set(i, i, data[i][i] * scale); - - return res; - } - - /** */ - private double[][] createAndFill(int rowSize, int colSize) { - double[][] data = new double[rowSize][colSize]; - - for (int i = 0; i < rowSize; i++) - for (int j = 0; j < colSize; j++) - data[i][j] = -0.5d + Math.random(); - - return data; - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOffHeapMatrixMulBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOffHeapMatrixMulBenchmark.java deleted file mode 100644 index 027b515437f55..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOffHeapMatrixMulBenchmark.java +++ /dev/null @@ -1,32 +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.yardstick.ml.math; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrix; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteDenseLocalOffHeapMatrixMulBenchmark extends IgniteAbstractMatrixMulBenchmark { - /** {@inheritDoc} */ - @Override Matrix newMatrix(int rowSize, int colSize) { - return new DenseLocalOffHeapMatrix(rowSize, colSize); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOnHeapMatrixMulBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOnHeapMatrixMulBenchmark.java deleted file mode 100644 index 6830130807623..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteDenseLocalOnHeapMatrixMulBenchmark.java +++ /dev/null @@ -1,32 +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.yardstick.ml.math; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteDenseLocalOnHeapMatrixMulBenchmark extends IgniteAbstractMatrixMulBenchmark { - /** {@inheritDoc} */ - @Override Matrix newMatrix(int rowSize, int colSize) { - return new DenseLocalOnHeapMatrix(rowSize, colSize); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseLocalMatrixMulBenchmark.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseLocalMatrixMulBenchmark.java deleted file mode 100644 index de2c99b428493..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/IgniteSparseLocalMatrixMulBenchmark.java +++ /dev/null @@ -1,32 +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.yardstick.ml.math; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix; - -/** - * Ignite benchmark that performs ML Grid operations. - */ -@SuppressWarnings("unused") -public class IgniteSparseLocalMatrixMulBenchmark extends IgniteAbstractMatrixMulBenchmark { - /** {@inheritDoc} */ - @Override Matrix newMatrix(int rowSize, int colSize) { - return new SparseLocalOnHeapMatrix(rowSize, colSize); - } -} diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/package-info.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/package-info.java deleted file mode 100644 index d37e967572a66..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/math/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. - */ - -/** - * - * ML Grid math benchmarks. - */ -package org.apache.ignite.yardstick.ml.math; \ No newline at end of file diff --git a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/package-info.java b/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/package-info.java deleted file mode 100644 index 4751f35adeea6..0000000000000 --- a/modules/yardstick/src/main/java/org/apache/ignite/yardstick/ml/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. - */ - -/** - * - * ML Grid benchmarks. - */ -package org.apache.ignite.yardstick.ml; \ No newline at end of file From 3ec74ca8dccb7997f9ae9fc01c89e283c7025c77 Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Wed, 16 May 2018 10:31:52 +0300 Subject: [PATCH 4/5] IGNITE-8450: fix compile errors --- .../ml/math/MathImplLocalTestSuite.java | 2 - .../impls/matrix/MatrixAttributeTest.java | 8 +- .../impls/matrix/MatrixKeyMapperForTests.java | 74 -- .../matrix/RandomMatrixConstructorTest.java | 71 -- .../impls/vector/VectorAttributesTest.java | 25 +- .../vector/VectorImplementationsTest.java | 857 ++++++++++++++++++ 6 files changed, 862 insertions(+), 175 deletions(-) delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixKeyMapperForTests.java delete mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrixConstructorTest.java create mode 100644 modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java index ee9d41804acc5..85e286d39550d 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java @@ -23,7 +23,6 @@ import org.apache.ignite.ml.math.impls.matrix.MatrixAttributeTest; import org.apache.ignite.ml.math.impls.matrix.MatrixImplementationsTest; import org.apache.ignite.ml.math.impls.matrix.MatrixViewConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.RandomMatrixConstructorTest; import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrixConstructorTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixArrayStorageTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixOffHeapStorageTest; @@ -78,7 +77,6 @@ // Matrix constructors tests. DenseLocalOnHeapMatrixConstructorTest.class, DenseLocalOffHeapMatrixConstructorTest.class, - RandomMatrixConstructorTest.class, MatrixViewConstructorTest.class, SparseLocalOnHeapMatrixConstructorTest.class, // Matrix tests. diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java index c645bd75d8fbe..a00259a7dec42 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java @@ -32,20 +32,16 @@ public class MatrixAttributeTest { /** */ private final List attrCfgs = Arrays.asList( new AttrCfg("isDense", Matrix::isDense, - DenseLocalOnHeapMatrix.class, DenseLocalOffHeapMatrix.class, RandomMatrix.class, DiagonalMatrix.class), + DenseLocalOnHeapMatrix.class, DenseLocalOffHeapMatrix.class), new AttrCfg("isArrayBased", Matrix::isArrayBased, DenseLocalOnHeapMatrix.class), new AttrCfg("isDistributed", Matrix::isDistributed), - new AttrCfg("isRandomAccess", Matrix::isRandomAccess, DenseLocalOnHeapMatrix.class, DenseLocalOffHeapMatrix.class, RandomMatrix.class, DiagonalMatrix.class, SparseLocalOnHeapMatrix.class), - new AttrCfg("isSequentialAccess", Matrix::isSequentialAccess, DiagonalMatrix.class) + new AttrCfg("isRandomAccess", Matrix::isRandomAccess, DenseLocalOnHeapMatrix.class, DenseLocalOffHeapMatrix.class, SparseLocalOnHeapMatrix.class) ); /** */ private final List specFixture = Arrays.asList( new Specification(new DenseLocalOnHeapMatrix(1, 1)), new Specification(new DenseLocalOffHeapMatrix(1, 1)), - new Specification(new RandomMatrix(1, 1)), - new Specification(new DiagonalMatrix(new double[] {1.0})), - new Specification(new FunctionMatrix(1, 1, (x, y) -> 1.0)), new Specification(new SparseLocalOnHeapMatrix(1, 1)) ); diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixKeyMapperForTests.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixKeyMapperForTests.java deleted file mode 100644 index 10e6e3f206ff3..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixKeyMapperForTests.java +++ /dev/null @@ -1,74 +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.distributed.MatrixKeyMapper; - -/** */ -public class MatrixKeyMapperForTests implements MatrixKeyMapper { - /** */ - private int rows; - /** */ - private int cols; - - /** */ - public MatrixKeyMapperForTests() { - // No-op. - } - - /** - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - */ - public MatrixKeyMapperForTests(int rows, int cols) { - this.rows = rows; - this.cols = cols; - } - - /** {@inheritDoc} */ - @Override public Integer apply(int x, int y) { - return x * cols + y; - } - - /** {@inheritDoc} */ - @Override public boolean isValid(Integer integer) { - return (rows * cols) > integer; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int hash = 1; - - hash += hash * 31 + rows; - hash += hash * 31 + cols; - - return hash; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null || getClass() != obj.getClass()) - return false; - - MatrixKeyMapperForTests that = (MatrixKeyMapperForTests)obj; - - return rows == that.rows && cols == that.cols; - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrixConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrixConstructorTest.java deleted file mode 100644 index 558e4d83cdf44..0000000000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrixConstructorTest.java +++ /dev/null @@ -1,71 +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.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** */ -public class RandomMatrixConstructorTest { - /** */ - @Test - public void invalidArgsTest() { - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new RandomMatrix(0, 1), "invalid row parameter"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new RandomMatrix(1, 0), "invalid col parameter"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new RandomMatrix(0, 1, true), "invalid row parameter, fastHash true"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new RandomMatrix(1, 0, true), "invalid col parameter, fastHash true"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new RandomMatrix(0, 1, false), "invalid row parameter, fastHash false"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new RandomMatrix(1, 0, false), "invalid col parameter, fastHash false"); - } - - /** */ - @Test - public void basicTest() { - assertEquals("Expected number of rows, int parameters.", 1, - new RandomMatrix(1, 2).rowSize()); - - assertEquals("Expected number of cols, int parameters.", 1, - new RandomMatrix(2, 1).columnSize()); - - assertEquals("Expected number of rows, int parameters, fastHash true.", 1, - new RandomMatrix(1, 2, true).rowSize()); - - assertEquals("Expected number of cols, int parameters, fastHash true.", 1, - new RandomMatrix(2, 1, true).columnSize()); - - assertEquals("Expected number of rows, int parameters, fastHash false.", 1, - new RandomMatrix(1, 2, false).rowSize()); - - assertEquals("Expected number of cols, int parameters, fastHash false.", 1, - new RandomMatrix(2, 1, false).columnSize()); - - RandomMatrix m = new RandomMatrix(1, 1); - //noinspection EqualsWithItself - assertTrue("Matrix is expected to be equal to self.", m.equals(m)); - //noinspection ObjectEqualsNull - assertFalse("Matrix is expected to be not equal to null.", m.equals(null)); - } -} diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorAttributesTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorAttributesTest.java index 52232ca5c0663..60d8d4324e827 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorAttributesTest.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorAttributesTest.java @@ -32,17 +32,14 @@ public class VectorAttributesTest { /** */ private final List attrCfgs = Arrays.asList( new AttrCfg("isDense", Vector::isDense, - DenseLocalOnHeapVector.class, DenseLocalOffHeapVector.class, RandomVector.class, ConstantVector.class, - SingleElementVector.class), + DenseLocalOnHeapVector.class, DenseLocalOffHeapVector.class), new AttrCfg("isArrayBased", Vector::isArrayBased, DenseLocalOnHeapVector.class), new AttrCfg("isSequentialAccess", Vector::isSequentialAccess, - DenseLocalOnHeapVector.class, DenseLocalOffHeapVector.class, SparseLocalVectorSequentialAccess.class, - RandomVector.class, ConstantVector.class, SingleElementVector.class), + DenseLocalOnHeapVector.class, DenseLocalOffHeapVector.class, SparseLocalVectorSequentialAccess.class), new AttrCfg("guidNotNull", v -> v.guid() == null), // IMPL NOTE this is somewhat artificial new AttrCfg("isRandomAccess", Vector::isRandomAccess, - DenseLocalOnHeapVector.class, DenseLocalOffHeapVector.class, RandomVector.class, ConstantVector.class, - SingleElementVector.class, SparseLocalVectorSequentialAccess.class, SparseLocalVectorRandomAccess.class), + DenseLocalOnHeapVector.class, DenseLocalOffHeapVector.class, SparseLocalVectorSequentialAccess.class, SparseLocalVectorRandomAccess.class), new AttrCfg("isDistributed", Vector::isDistributed)); /** */ @@ -57,22 +54,6 @@ public class VectorAttributesTest { "isRandomAccess", "isDistributed"), new Specification(new SparseLocalVectorSequentialAccess(1)), new Specification(new SparseLocalVectorRandomAccess(1)), - new Specification(new RandomVector(1)), - new Specification(new ConstantVector(1, 1.0)), - new Specification(new FunctionVector(1, idx -> (double)idx)), - new Specification(new SingleElementVector(1, 0, 1.0)), - new Specification(new PivotedVectorView(new DenseLocalOnHeapVector(1), new int[] {0}), - DenseLocalOnHeapVector.class, "isDense", "isArrayBased", "isSequentialAccess", - "isRandomAccess", "isDistributed"), - new Specification(new PivotedVectorView(new DenseLocalOffHeapVector(1), new int[] {0}), - DenseLocalOffHeapVector.class, "isDense", "isArrayBased", "isSequentialAccess", - "isRandomAccess", "isDistributed"), - new Specification(new SingleElementVectorView(new DenseLocalOnHeapVector(1), 0), - DenseLocalOnHeapVector.class, "isDense", "isSequentialAccess", - "isRandomAccess", "isDistributed"), - new Specification(new SingleElementVectorView(new DenseLocalOffHeapVector(1), 0), - DenseLocalOffHeapVector.class, "isDense", "isSequentialAccess", - "isRandomAccess", "isDistributed"), new Specification(new MatrixVectorView(new DenseLocalOnHeapMatrix(1, 1), 0, 0, 1, 1), DenseLocalOnHeapVector.class, "isDense", "isRandomAccess", "isDistributed"), // TODO: IGNTIE-5723, find out why "isSequentialAccess" fails here diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java new file mode 100644 index 0000000000000..da67a05f9059e --- /dev/null +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/vector/VectorImplementationsTest.java @@ -0,0 +1,857 @@ +/* + * 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 java.util.Arrays; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; +import org.apache.ignite.IgniteException; +import org.apache.ignite.ml.math.ExternalizeTest; +import org.apache.ignite.ml.math.Vector; +import org.apache.ignite.ml.math.exceptions.CardinalityException; +import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** See also: {@link AbstractVectorTest} and {@link VectorToMatrixTest}. */ +public class VectorImplementationsTest { // TODO: IGNTIE-5723, split this to smaller cohesive test classes + /** */ + @Test + public void vectorImplementationsFixturesTest() { + new VectorImplementationsFixtures().selfTest(); + } + + /** */ + @Test + public void setGetTest() { + consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { + vec.set(idx, val); + + return val; + })); + } + + /** */ + @Test + public void setXTest() { + consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { + vec.setX(idx, val); + + return val; + })); + } + + /** */ + @Test + public void incrementTest() { + consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { + double old = vec.get(idx); + + vec.increment(idx, val); + + return old + val; + })); + } + + /** */ + @Test + public void incrementXTest() { + consumeSampleVectors((v, desc) -> mutateAtIdxTest(v, desc, (vec, idx, val) -> { + double old = vec.getX(idx); + + vec.incrementX(idx, val); + + return old + val; + })); + } + + /** */ + @Test + public void operateXOutOfBoundsTest() { + consumeSampleVectors((v, desc) -> { + if (v instanceof DenseLocalOffHeapVector || v instanceof SparseLocalVector || v instanceof SparseLocalOffHeapVector) + return; // TODO: IGNTIE-5723, find out if it's OK to skip by instances here + + boolean expECaught = false; + + try { + v.getX(-1); + } + catch (ArrayIndexOutOfBoundsException | IgniteException e) { + expECaught = true; + } + + if (!getXOutOfBoundsOK(v)) + assertTrue("Expect exception at negative index getX in " + desc, expECaught); + + expECaught = false; + + try { + v.setX(-1, 0); + } + catch (ArrayIndexOutOfBoundsException | IgniteException e) { + expECaught = true; + } + + assertTrue("Expect exception at negative index setX in " + desc, expECaught); + + expECaught = false; + + try { + v.incrementX(-1, 1); + } + catch (ArrayIndexOutOfBoundsException | IgniteException e) { + expECaught = true; + } + + assertTrue("Expect exception at negative index incrementX in " + desc, expECaught); + + expECaught = false; + + try { + v.getX(v.size()); + } + catch (ArrayIndexOutOfBoundsException | IgniteException e) { + expECaught = true; + } + + if (!getXOutOfBoundsOK(v)) + assertTrue("Expect exception at too large index getX in " + desc, expECaught); + + expECaught = false; + + try { + v.setX(v.size(), 1); + } + catch (ArrayIndexOutOfBoundsException | IgniteException e) { + expECaught = true; + } + + assertTrue("Expect exception at too large index setX in " + desc, expECaught); + + expECaught = false; + + try { + v.incrementX(v.size(), 1); + } + catch (ArrayIndexOutOfBoundsException | IgniteException e) { + expECaught = true; + } + + assertTrue("Expect exception at too large index incrementX in " + desc, expECaught); + }); + } + + /** */ + @Test + public void sizeTest() { + final AtomicReference expSize = new AtomicReference<>(0); + + consumeSampleVectors( + expSize::set, + (v, desc) -> Assert.assertEquals("Expected size for " + desc, + (int)expSize.get(), v.size()) + ); + } + + /** */ + @Test + public void getElementTest() { + consumeSampleVectors((v, desc) -> new ElementsChecker(v, desc).assertCloseEnough(v)); + } + + /** */ + @Test + public void copyTest() { + consumeSampleVectors((v, desc) -> new ElementsChecker(v, desc).assertCloseEnough(v.copy())); + } + + /** */ + @Test + public void divideTest() { + operationTest((val, operand) -> val / operand, Vector::divide); + } + + /** */ + @Test + public void likeTest() { + for (int card : new int[] {1, 2, 4, 8, 16, 32, 64, 128}) + consumeSampleVectors((v, desc) -> { + Class expType = expLikeType(v); + + if (expType == null) { + try { + v.like(card); + } + catch (UnsupportedOperationException uoe) { + return; + } + + fail("Expected exception wasn't caught for " + desc); + + return; + } + + Vector vLike = v.like(card); + + assertNotNull("Expect non-null like vector for " + expType.getSimpleName() + " in " + desc, vLike); + assertEquals("Expect size equal to cardinality at " + desc, card, vLike.size()); + + Class actualType = vLike.getClass(); + + assertTrue("Actual vector type " + actualType.getSimpleName() + + " should be assignable from expected type " + expType.getSimpleName() + " in " + desc, + actualType.isAssignableFrom(expType)); + }); + } + + /** */ + @Test + public void minusTest() { + operationVectorTest((operand1, operand2) -> operand1 - operand2, Vector::minus); + } + + /** */ + @Test + public void plusVectorTest() { + operationVectorTest((operand1, operand2) -> operand1 + operand2, Vector::plus); + } + + /** */ + @Test + public void plusDoubleTest() { + operationTest((val, operand) -> val + operand, Vector::plus); + } + + /** */ + @Test + public void timesVectorTest() { + operationVectorTest((operand1, operand2) -> operand1 * operand2, Vector::times); + } + + /** */ + @Test + public void timesDoubleTest() { + operationTest((val, operand) -> val * operand, Vector::times); + } + + /** */ + @Test + public void viewPartTest() { + consumeSampleVectors((v, desc) -> { + final int size = v.size(); + final double[] ref = new double[size]; + final int delta = size > 32 ? 3 : 1; // IMPL NOTE this is for faster test execution + + final ElementsChecker checker = new ElementsChecker(v, ref, desc); + + for (int off = 0; off < size; off += delta) + for (int len = 1; len < size - off; len += delta) + checker.assertCloseEnough(v.viewPart(off, len), Arrays.copyOfRange(ref, off, off + len)); + }); + } + + /** */ + @Test + public void sumTest() { + toDoubleTest( + ref -> Arrays.stream(ref).sum(), + Vector::sum); + } + + /** */ + @Test + public void minValueTest() { + toDoubleTest( + ref -> Arrays.stream(ref).min().getAsDouble(), + Vector::minValue); + } + + /** */ + @Test + public void maxValueTest() { + toDoubleTest( + ref -> Arrays.stream(ref).max().getAsDouble(), + Vector::maxValue); + } + + /** */ + @Test + public void sortTest() { + consumeSampleVectors((v, desc) -> { + if (readOnly() || !v.isArrayBased()) { + boolean expECaught = false; + + try { + v.sort(); + } + catch (UnsupportedOperationException uoe) { + expECaught = true; + } + + assertTrue("Expected exception was not caught for sort in " + desc, expECaught); + + return; + } + + final int size = v.size(); + final double[] ref = new double[size]; + + new ElementsChecker(v, ref, desc).assertCloseEnough(v.sort(), Arrays.stream(ref).sorted().toArray()); + }); + } + + /** */ + @Test + public void metaAttributesTest() { + consumeSampleVectors((v, desc) -> { + assertNotNull("Null meta storage in " + desc, v.getMetaStorage()); + + final String key = "test key"; + final String val = "test value"; + final String details = "key [" + key + "] for " + desc; + + v.setAttribute(key, val); + assertTrue("Expect to have meta attribute for " + details, v.hasAttribute(key)); + assertEquals("Unexpected meta attribute value for " + details, val, v.getAttribute(key)); + + v.removeAttribute(key); + assertFalse("Expect not to have meta attribute for " + details, v.hasAttribute(key)); + assertNull("Unexpected meta attribute value for " + details, v.getAttribute(key)); + }); + } + + /** */ + @Test + public void assignDoubleTest() { + consumeSampleVectors((v, desc) -> { + if (readOnly()) + return; + + for (double val : new double[] {0, -1, 0, 1}) { + v.assign(val); + + for (int idx = 0; idx < v.size(); idx++) { + final Metric metric = new Metric(val, v.get(idx)); + + assertTrue("Not close enough at index " + idx + ", val " + val + ", " + metric + + ", " + desc, metric.closeEnough()); + } + } + }); + } + + /** */ + @Test + public void assignDoubleArrTest() { + consumeSampleVectors((v, desc) -> { + if (readOnly()) + return; + + final int size = v.size(); + final double[] ref = new double[size]; + + final ElementsChecker checker = new ElementsChecker(v, ref, desc); + + for (int idx = 0; idx < size; idx++) + ref[idx] = -ref[idx]; + + v.assign(ref); + + checker.assertCloseEnough(v, ref); + + assignDoubleArrWrongCardinality(v, desc); + }); + } + + /** */ + @Test + public void assignVectorTest() { + consumeSampleVectors((v, desc) -> { + if (readOnly()) + return; + + final int size = v.size(); + final double[] ref = new double[size]; + + final ElementsChecker checker = new ElementsChecker(v, ref, desc); + + for (int idx = 0; idx < size; idx++) + ref[idx] = -ref[idx]; + + v.assign(new DenseLocalOnHeapVector(ref)); + + checker.assertCloseEnough(v, ref); + + assignVectorWrongCardinality(v, desc); + }); + } + + /** */ + @Test + public void assignFunctionTest() { + consumeSampleVectors((v, desc) -> { + if (readOnly()) + return; + + final int size = v.size(); + final double[] ref = new double[size]; + + final ElementsChecker checker = new ElementsChecker(v, ref, desc); + + for (int idx = 0; idx < size; idx++) + ref[idx] = -ref[idx]; + + v.assign((idx) -> ref[idx]); + + checker.assertCloseEnough(v, ref); + }); + } + + /** */ + @Test + public void minElementTest() { + consumeSampleVectors((v, desc) -> { + final ElementsChecker checker = new ElementsChecker(v, desc); + + final Vector.Element minE = v.minElement(); + + final int minEIdx = minE.index(); + + assertTrue("Unexpected index from minElement " + minEIdx + ", " + desc, + minEIdx >= 0 && minEIdx < v.size()); + + final Metric metric = new Metric(minE.get(), v.minValue()); + + assertTrue("Not close enough minElement at index " + minEIdx + ", " + metric + + ", " + desc, metric.closeEnough()); + + checker.assertNewMinElement(v); + }); + } + + /** */ + @Test + public void maxElementTest() { + consumeSampleVectors((v, desc) -> { + final ElementsChecker checker = new ElementsChecker(v, desc); + + final Vector.Element maxE = v.maxElement(); + + final int minEIdx = maxE.index(); + + assertTrue("Unexpected index from minElement " + minEIdx + ", " + desc, + minEIdx >= 0 && minEIdx < v.size()); + + final Metric metric = new Metric(maxE.get(), v.maxValue()); + + assertTrue("Not close enough maxElement at index " + minEIdx + ", " + metric + + ", " + desc, metric.closeEnough()); + + checker.assertNewMaxElement(v); + }); + } + + /** */ + @Test + public void externalizeTest() { + (new ExternalizeTest() { + /** {@inheritDoc} */ + @Override public void externalizeTest() { + consumeSampleVectors((v, desc) -> { + if (v instanceof SparseLocalOffHeapVector) + return; //TODO: IGNITE-5801, wait till SparseLocalOffHeapVector externalization support. + + externalizeTest(v); + }); + } + }).externalizeTest(); + } + + /** */ + @Test + public void hashCodeTest() { + consumeSampleVectors((v, desc) -> assertTrue("Zero hash code for " + desc, v.hashCode() != 0)); + } + + /** */ + private boolean getXOutOfBoundsOK(Vector v) { + // TODO: IGNTIE-5723, find out if this is indeed OK + return false; + } + + /** */ + private void mutateAtIdxTest(Vector v, String desc, MutateAtIdx operation) { + if (readOnly()) { + if (v.size() < 1) + return; + + boolean expECaught = false; + + try { + operation.apply(v, 0, 1); + } + catch (UnsupportedOperationException uoe) { + expECaught = true; + } + + assertTrue("Expect exception at attempt to mutate element in " + desc, expECaught); + + return; + } + + for (double val : new double[] {0, -1, 0, 1}) + for (int idx = 0; idx < v.size(); idx++) { + double exp = operation.apply(v, idx, val); + + final Metric metric = new Metric(exp, v.get(idx)); + + assertTrue("Not close enough at index " + idx + ", val " + val + ", " + metric + + ", " + desc, metric.closeEnough()); + } + } + + /** */ + private Class expLikeType(Vector v) { + Class clazz = v.getClass(); + + if (clazz.isAssignableFrom(MatrixVectorView.class) || clazz.isAssignableFrom(DelegatingVector.class)) + return DenseLocalOnHeapVector.class; // IMPL NOTE per fixture + + return clazz; + } + + /** */ + private void toDoubleTest(Function calcRef, Function calcVec) { + consumeSampleVectors((v, desc) -> { + final int size = v.size(); + final double[] ref = new double[size]; + + new ElementsChecker(v, ref, desc); // IMPL NOTE this initialises vector and reference array + + final Metric metric = new Metric(calcRef.apply(ref), calcVec.apply(v)); + + assertTrue("Not close enough at " + desc + + ", " + metric, metric.closeEnough()); + }); + } + + /** */ + private void operationVectorTest(BiFunction operation, + BiFunction vecOperation) { + consumeSampleVectors((v, desc) -> { + // TODO : IGNTIE-5723, find out if more elaborate testing scenario is needed or it's okay as is. + final int size = v.size(); + final double[] ref = new double[size]; + + final ElementsChecker checker = new ElementsChecker(v, ref, desc); + final Vector operand = v.copy(); + + for (int idx = 0; idx < size; idx++) + ref[idx] = operation.apply(ref[idx], ref[idx]); + + checker.assertCloseEnough(vecOperation.apply(v, operand), ref); + + assertWrongCardinality(v, desc, vecOperation); + }); + } + + /** */ + private void assignDoubleArrWrongCardinality(Vector v, String desc) { + boolean expECaught = false; + + try { + v.assign(new double[v.size() + 1]); + } + catch (CardinalityException ce) { + expECaught = true; + } + + assertTrue("Expect exception at too large size in " + desc, expECaught); + + if (v.size() < 2) + return; + + expECaught = false; + + try { + v.assign(new double[v.size() - 1]); + } + catch (CardinalityException ce) { + expECaught = true; + } + + assertTrue("Expect exception at too small size in " + desc, expECaught); + } + + /** */ + private void assignVectorWrongCardinality(Vector v, String desc) { + boolean expECaught = false; + + try { + v.assign(new DenseLocalOnHeapVector(v.size() + 1)); + } + catch (CardinalityException ce) { + expECaught = true; + } + + assertTrue("Expect exception at too large size in " + desc, expECaught); + + if (v.size() < 2) + return; + + expECaught = false; + + try { + v.assign(new DenseLocalOnHeapVector(v.size() - 1)); + } + catch (CardinalityException ce) { + expECaught = true; + } + + assertTrue("Expect exception at too small size in " + desc, expECaught); + } + + /** */ + private void assertWrongCardinality( + Vector v, String desc, BiFunction vecOperation) { + boolean expECaught = false; + + try { + vecOperation.apply(v, new DenseLocalOnHeapVector(v.size() + 1)); + } + catch (CardinalityException ce) { + expECaught = true; + } + + assertTrue("Expect exception at too large size in " + desc, expECaught); + + if (v.size() < 2) + return; + + expECaught = false; + + try { + vecOperation.apply(v, new DenseLocalOnHeapVector(v.size() - 1)); + } + catch (CardinalityException ce) { + expECaught = true; + } + + assertTrue("Expect exception at too small size in " + desc, expECaught); + } + + /** */ + private void operationTest(BiFunction operation, + BiFunction vecOperation) { + for (double val : new double[] {0, 0.1, 1, 2, 10}) + consumeSampleVectors((v, desc) -> { + final int size = v.size(); + final double[] ref = new double[size]; + + final ElementsChecker checker = new ElementsChecker(v, ref, "val " + val + ", " + desc); + + for (int idx = 0; idx < size; idx++) + ref[idx] = operation.apply(ref[idx], val); + + checker.assertCloseEnough(vecOperation.apply(v, val), ref); + }); + } + + /** */ + private void consumeSampleVectors(BiConsumer consumer) { + consumeSampleVectors(null, consumer); + } + + /** */ + private void consumeSampleVectors(Consumer paramsConsumer, BiConsumer consumer) { + new VectorImplementationsFixtures().consumeSampleVectors(paramsConsumer, consumer); + } + + /** */ + private static boolean readOnly() { + return false; + } + + /** */ + private interface MutateAtIdx { + /** */ + double apply(Vector v, int idx, double val); + } + + /** */ + static class ElementsChecker { + /** */ + private final String fixtureDesc; + + /** */ + private final double[] refReadOnly; + + /** */ + private final boolean nonNegative; + + /** */ + ElementsChecker(Vector v, double[] ref, String fixtureDesc, boolean nonNegative) { + this.fixtureDesc = fixtureDesc; + + this.nonNegative = nonNegative; + + refReadOnly = readOnly() && ref == null ? new double[v.size()] : null; + + init(v, ref); + } + + /** */ + ElementsChecker(Vector v, double[] ref, String fixtureDesc) { + this(v, ref, fixtureDesc, false); + } + + /** */ + ElementsChecker(Vector v, String fixtureDesc) { + this(v, null, fixtureDesc); + } + + /** */ + void assertCloseEnough(Vector obtained, double[] exp) { + final int size = obtained.size(); + + for (int i = 0; i < size; i++) { + final Vector.Element e = obtained.getElement(i); + + if (refReadOnly != null && exp == null) + exp = refReadOnly; + + final Metric metric = new Metric(exp == null ? generated(i) : exp[i], e.get()); + + assertEquals("Unexpected vector index at " + fixtureDesc, i, e.index()); + assertTrue("Not close enough at index " + i + ", size " + size + ", " + metric + + ", " + fixtureDesc, metric.closeEnough()); + } + } + + /** */ + void assertCloseEnough(Vector obtained) { + assertCloseEnough(obtained, null); + } + + /** */ + void assertNewMinElement(Vector v) { + if (readOnly()) + return; + + int exp = v.size() / 2; + + v.set(exp, -(v.size() * 2 + 1)); + + assertEquals("Unexpected minElement index at " + fixtureDesc, exp, v.minElement().index()); + } + + /** */ + void assertNewMaxElement(Vector v) { + if (readOnly()) + return; + + int exp = v.size() / 2; + + v.set(exp, v.size() * 2 + 1); + + assertEquals("Unexpected minElement index at " + fixtureDesc, exp, v.maxElement().index()); + } + + /** */ + private void init(Vector v, double[] ref) { + if (readOnly()) { + initReadonly(v, ref); + + return; + } + + for (Vector.Element e : v.all()) { + int idx = e.index(); + + // IMPL NOTE introduce negative values because their absence + // blocked catching an ugly bug in AbstractVector#kNorm + int val = generated(idx); + + e.set(val); + + if (ref != null) + ref[idx] = val; + } + } + + /** */ + private void initReadonly(Vector v, double[] ref) { + if (refReadOnly != null) + for (Vector.Element e : v.all()) + refReadOnly[e.index()] = e.get(); + + if (ref != null) + for (Vector.Element e : v.all()) + ref[e.index()] = e.get(); + } + + /** */ + private int generated(int idx) { + return nonNegative || (idx & 1) == 0 ? idx : -idx; + } + } + + /** */ + static class Metric { //TODO: IGNITE-5824, consider if softer tolerance (like say 0.1 or 0.01) would make sense here + /** */ + private final double exp; + + /** */ + private final double obtained; + + /** **/ + Metric(double exp, double obtained) { + this.exp = exp; + this.obtained = obtained; + } + + /** */ + boolean closeEnough() { + return new Double(exp).equals(obtained) || closeEnoughToZero(); + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "Metric{" + "expected=" + exp + + ", obtained=" + obtained + + '}'; + } + + /** */ + private boolean closeEnoughToZero() { + return (new Double(exp).equals(0.0) && new Double(obtained).equals(-0.0)) + || (new Double(exp).equals(-0.0) && new Double(obtained).equals(0.0)); + } + } +} From ef9c72adb8f28e5fe3d93d3a438a2583e8141588 Mon Sep 17 00:00:00 2001 From: Zinoviev Alexey Date: Wed, 16 May 2018 13:40:15 +0300 Subject: [PATCH 5/5] IGNITE-8450: fix compile errors --- .../ignite/ml/math/impls/matrix/MatrixAttributeTest.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java index a00259a7dec42..9cacf6edbaa4a 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java @@ -57,12 +57,6 @@ public void isArrayBasedTest() { assertAttribute("isArrayBased"); } - /** */ - @Test - public void isSequentialAccessTest() { - assertAttribute("isSequentialAccess"); - } - /** */ @Test public void isRandomAccessTest() {