http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/CacheMatrixTest.java ---------------------------------------------------------------------- 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 c6f6f86..0000000 --- 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - final CacheMatrix<Integer, Double> cacheMatrix = new CacheMatrix<>(rows, cols, cache, keyMapper, new IdentityValueMapper()); - - ExternalizeTest<CacheMatrix<Integer, Double>> externalizeTest = new ExternalizeTest<CacheMatrix<Integer, Double>>() { - /** {@inheritDoc} */ - @Override public void externalizeTest() { - super.externalizeTest(cacheMatrix); - } - }; - - externalizeTest.externalizeTest(); - } - - /** */ - public void testMinMax() { - IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName()); - - MatrixKeyMapper<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - final CacheMatrix<Integer, Double> 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<Integer> keyMapper = getKeyMapper(rows, cols); - IgniteCache<Integer, Double> cache = getCache(); - final CacheMatrix<Integer, Double> 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<Integer, Double> getCache() { - assert ignite != null; - - CacheConfiguration cfg = new CacheConfiguration(); - cfg.setName(CACHE_NAME); - - IgniteCache<Integer, Double> cache = ignite.getOrCreateCache(CACHE_NAME); - - assert cache != null; - return cache; - } - - /** */ - private MatrixKeyMapper<Integer> 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); - } -}
http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/DiagonalMatrixTest.java ---------------------------------------------------------------------- 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 a00403f..0000000 --- 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<DiagonalMatrix> { - /** */ - 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); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/FunctionMatrixConstructorTest.java ---------------------------------------------------------------------- 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 25de7d3..0000000 --- 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)); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixAttributeTest.java ---------------------------------------------------------------------- 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 c645bd7..9cacf6e 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<MatrixAttributeTest.AttrCfg> 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<MatrixAttributeTest.Specification> 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)) ); @@ -63,12 +59,6 @@ public class MatrixAttributeTest { /** */ @Test - public void isSequentialAccessTest() { - assertAttribute("isSequentialAccess"); - } - - /** */ - @Test public void isRandomAccessTest() { assertAttribute("isRandomAccess"); } http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationFixtures.java ---------------------------------------------------------------------- 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 52a0077..e0800cc 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.BiFunction; 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<Supplier<Iterable<Matrix>>> suppliers = Arrays.asList( (Supplier<Iterable<Matrix>>)DenseLocalOnHeapMatrixFixture::new, (Supplier<Iterable<Matrix>>)DenseLocalOffHeapMatrixFixture::new, - (Supplier<Iterable<Matrix>>)RandomMatrixFixture::new, (Supplier<Iterable<Matrix>>)SparseLocalOnHeapMatrixFixture::new, - (Supplier<Iterable<Matrix>>)PivotedMatrixViewFixture::new, - (Supplier<Iterable<Matrix>>)MatrixViewFixture::new, - (Supplier<Iterable<Matrix>>)FunctionMatrixFixture::new, - (Supplier<Iterable<Matrix>>)DiagonalMatrixFixture::new, - (Supplier<Iterable<Matrix>>)TransposedMatrixViewFixture::new + (Supplier<Iterable<Matrix>>)MatrixViewFixture::new ); /** */ @@ -76,14 +67,6 @@ class MatrixImplementationFixtures { } /** */ - private static class RandomMatrixFixture extends MatrixSizeIterator { - /** */ - RandomMatrixFixture() { - super(RandomMatrix::new, "RandomMatrix"); - } - } - - /** */ private static class SparseLocalOnHeapMatrixFixture extends MatrixSizeIterator { /** */ SparseLocalOnHeapMatrixFixture() { @@ -92,14 +75,6 @@ class MatrixImplementationFixtures { } /** */ - private static class PivotedMatrixViewFixture extends WrapperMatrixIterator { - /** */ - PivotedMatrixViewFixture() { - super(PivotedMatrixView::new, "PivotedMatrixView over DenseLocalOnHeapMatrix"); - } - } - - /** */ private static class MatrixViewFixture extends WrapperMatrixIterator { /** */ MatrixViewFixture() { @@ -109,52 +84,6 @@ class MatrixImplementationFixtures { } /** */ - 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<Matrix> iterator() { - return new Iterator<Matrix>() { - /** {@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<Matrix> { /** */ private final Integer[] sizes = new Integer[] {1, null}; @@ -316,66 +245,4 @@ class MatrixImplementationFixtures { 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; - } - } } http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixImplementationsTest.java ---------------------------------------------------------------------- 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 dc8ff12..00f37ba 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.RowIndexException; 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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { 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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { /** */ @Test - public void testHaveLikeVector() throws InstantiationException, IllegalAccessException { + public void testHaveLikeVector() { for (Class<? extends Matrix> key : likeVectorTypesMap().keySet()) { Class<? extends Vector> 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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @Test public void testPlus() { consumeSampleMatrix((m, desc) -> { - if (readOnly(m)) - return; - double[][] data = fillAndReturn(m); double plusVal = Math.random(); @@ -265,9 +260,6 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @Test public void testTimes() { consumeSampleMatrix((m, desc) -> { - if (readOnly(m)) - return; - double[][] data = fillAndReturn(m); double timeVal = Math.random(); @@ -390,99 +382,6 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { /** */ @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() { consumeSampleMatrix((m, desc) -> { if (ignore(m.getClass())) @@ -525,8 +424,7 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { 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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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 class MatrixImplementationsTest extends ExternalizeTest<Matrix> { @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)); @@ -1024,27 +900,15 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { } /** */ - 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 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { 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 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { /** Ignore test for given matrix type. */ private boolean ignore(Class<? extends Matrix> clazz) { - List<Class<? extends Matrix>> ignoredClasses = Arrays.asList(RandomMatrix.class, PivotedMatrixView.class, - MatrixView.class, FunctionMatrix.class, TransposedMatrixView.class); + List<Class<? extends Matrix>> ignoredClasses = Arrays.asList(MatrixView.class); for (Class<? extends Matrix> ignoredClass : ignoredClasses) if (ignoredClass.isAssignableFrom(clazz)) @@ -1112,10 +967,7 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { return new LinkedHashMap<Class<? extends Matrix>, Class<? extends Vector>>() {{ 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 @@ public class MatrixImplementationsTest extends ExternalizeTest<Matrix> { return new LinkedHashMap<Class<? extends Matrix>, Class<? extends Matrix>>() {{ 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 }}; } http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/MatrixKeyMapperForTests.java ---------------------------------------------------------------------- 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 10e6e3f..0000000 --- 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<Integer> { - /** */ - 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; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/PivotedMatrixViewConstructorTest.java ---------------------------------------------------------------------- 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 87bf3ad..0000000 --- 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); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/RandomMatrixConstructorTest.java ---------------------------------------------------------------------- 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 558e4d8..0000000 --- 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)); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/impls/matrix/SparseDistributedBlockMatrixTest.java ---------------------------------------------------------------------- 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 20dda2a..0000000 --- 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<String> cacheNames = ignite.cacheNames(); - - assert cacheNames.contains(((DistributedStorage)cacheMatrix1.getStorage()).cacheName()); - - IgniteCache<MatrixBlockKey, Object> cache = ignite.getOrCreateCache(((DistributedStorage)cacheMatrix1.getStorage()).cacheName()); - - Set<MatrixBlockKey> keySet1 = buildKeySet(cacheMatrix1); - Set<MatrixBlockKey> 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<MatrixBlockKey> buildKeySet(SparseBlockDistributedMatrix m) { - - BlockMatrixStorage storage = (BlockMatrixStorage)m.getStorage(); - - return storage.getAllKeys(); - } -}
