http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/decompositions/SingularValueDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/decompositions/SingularValueDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/math/decompositions/SingularValueDecompositionTest.java deleted file mode 100644 index d0b89f8..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/math/decompositions/SingularValueDecompositionTest.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.math.decompositions; - -import org.apache.ignite.math.Matrix; -import org.apache.ignite.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.math.impls.matrix.PivotedMatrixView; -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 DecompositionSupport} features. - */ - @Test - public void decompositionSupportTest() { - 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(); - } -}
http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/MathTestConstants.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/MathTestConstants.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/MathTestConstants.java deleted file mode 100644 index 122c62e..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/math/impls/MathTestConstants.java +++ /dev/null @@ -1,88 +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.math.impls; - -/** - * Collect constants for org.apache.ignite.math tests - */ -public interface MathTestConstants { - /** */ - public double SECOND_ARG = 1d; - - /** - * We assume that we will check calculation precision in other tests. - */ - public double EXP_DELTA = 0.1d; - - /** */ - public String UNEXPECTED_VAL = "Unexpected value."; - - /** */ - public String NULL_GUID = "Null GUID."; - - /** */ - public String UNEXPECTED_GUID_VAL = "Unexpected GUID value."; - - /** */ - public String EMPTY_GUID = "Empty GUID."; - - /** */ - public String VALUES_SHOULD_BE_NOT_EQUALS = "Values should be not equals."; - - /** */ - public String NULL_VAL = "Null value."; - - /** */ - public String NULL_VALUES = "Null values."; - - /** */ - public String NOT_NULL_VAL = "Not null value."; - - /** */ - public double TEST_VAL = 1d; - - /** */ - public String VAL_NOT_EQUALS = "Values not equals."; - - /** */ - public String NO_NEXT_ELEMENT = "No next element."; - - /** */ - public int STORAGE_SIZE = 100; - - /** */ - public String WRONG_ATTRIBUTE_VAL = "Wrong attribute value."; - - /** */ - public String NULL_DATA_ELEMENT = "Null data element."; - - /** */ - public String WRONG_DATA_ELEMENT = "Wrong data element."; - - /** */ - public double NIL_DELTA = 0d; - - /** */ - public String NULL_DATA_STORAGE = "Null data storage."; - - /** */ - public String WRONG_DATA_SIZE = "Wrong data size."; - - /** */ - public String UNEXPECTED_DATA_VAL = "Unexpected data value."; -} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/CacheMatrixTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/CacheMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/CacheMatrixTest.java deleted file mode 100644 index 8a6d077..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/CacheMatrixTest.java +++ /dev/null @@ -1,369 +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.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.math.ExternalizeTest; -import org.apache.ignite.math.IdentityValueMapper; -import org.apache.ignite.math.Matrix; -import org.apache.ignite.math.MatrixKeyMapper; -import org.apache.ignite.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.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()); - - 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()); - - double sum = 0; - - 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>>() { - - @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/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOffHeapMatrixConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOffHeapMatrixConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOffHeapMatrixConstructorTest.java deleted file mode 100644 index 1f0537c..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOffHeapMatrixConstructorTest.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.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 DenseLocalOffHeapMatrixConstructorTest { - /** */ - @Test - public void invalidArgsTest() { - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new DenseLocalOffHeapMatrix(0, 1), "invalid row parameter"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new DenseLocalOffHeapMatrix(1, 0), "invalid col parameter"); - - //noinspection ConstantConditions - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new DenseLocalOffHeapMatrix(null), "null matrix parameter"); - - DenseLocalOnHeapMatrixConstructorTest.verifyAssertionError(() -> new DenseLocalOffHeapMatrix(new double[][] {null, new double[1]}), - "null row in matrix"); - } - - /** */ - @Test - public void basicTest() { - assertEquals("Expected number of rows, int parameters.", 1, - new DenseLocalOffHeapMatrix(1, 2).rowSize()); - - assertEquals("Expected number of rows, double[][] parameter.", 1, - new DenseLocalOffHeapMatrix(new double[][] {new double[2]}).rowSize()); - - assertEquals("Expected number of cols, int parameters.", 1, - new DenseLocalOffHeapMatrix(2, 1).columnSize()); - - assertEquals("Expected number of cols, double[][] parameter.", 1, - new DenseLocalOffHeapMatrix(new double[][] {new double[1], new double[1]}).columnSize()); - - double[][] data1 = new double[][] {{1, 2}, {3, 4}}, data2 = new double[][] {{1, 2}, {3, 5}}; - - assertTrue("Matrices with same values are expected to be equal", - new DenseLocalOffHeapMatrix(data1).equals(new DenseLocalOffHeapMatrix(data1))); - - assertFalse("Matrices with same values are expected to be equal", - new DenseLocalOffHeapMatrix(data1).equals(new DenseLocalOffHeapMatrix(data2))); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOnHeapMatrixConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOnHeapMatrixConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOnHeapMatrixConstructorTest.java deleted file mode 100644 index 0a25e31..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOnHeapMatrixConstructorTest.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.math.impls.matrix; - -import java.util.function.Supplier; -import org.apache.ignite.math.Matrix; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -/** */ -public class DenseLocalOnHeapMatrixConstructorTest { - /** */ - @Test - public void invalidArgsTest() { - verifyAssertionError(() -> new DenseLocalOnHeapMatrix(0, 1), "invalid row parameter"); - - verifyAssertionError(() -> new DenseLocalOnHeapMatrix(1, 0), "invalid col parameter"); - - //noinspection ConstantConditions - verifyAssertionError(() -> new DenseLocalOnHeapMatrix(null), "null matrix parameter"); - - verifyAssertionError(() -> new DenseLocalOnHeapMatrix(new double[][] {null, new double[1]}), - "null row in matrix"); - } - - /** */ - @Test - public void basicTest() { - assertEquals("Expected number of rows, int parameters.", 1, - new DenseLocalOnHeapMatrix(1, 2).rowSize()); - - assertEquals("Expected number of rows, double[][] parameter.", 1, - new DenseLocalOnHeapMatrix(new double[][] {new double[2]}).rowSize()); - - assertEquals("Expected number of cols, int parameters.", 1, - new DenseLocalOnHeapMatrix(2, 1).columnSize()); - - assertEquals("Expected number of cols, double[][] parameter.", 1, - new DenseLocalOnHeapMatrix(new double[][] {new double[1], new double[1]}).columnSize()); - } - - /** */ - static void verifyAssertionError(Supplier<Matrix> ctor, String desc) { - try { - assertNotNull("Unexpected null matrix in " + desc, ctor.get()); - } - catch (AssertionError ae) { - return; - } - - fail("Expected error not caught in " + desc); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DiagonalMatrixTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DiagonalMatrixTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/DiagonalMatrixTest.java deleted file mode 100644 index c0c2af7..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/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.math.impls.matrix; - -import org.apache.ignite.math.ExternalizeTest; -import org.apache.ignite.math.Matrix; -import org.apache.ignite.math.Vector; -import org.apache.ignite.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.math.impls.MathTestConstants; -import org.apache.ignite.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/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/FunctionMatrixConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/FunctionMatrixConstructorTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/FunctionMatrixConstructorTest.java deleted file mode 100644 index a5ac84a..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/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.math.impls.matrix; - -import org.apache.ignite.math.Matrix; -import org.apache.ignite.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/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixAttributeTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixAttributeTest.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixAttributeTest.java deleted file mode 100644 index c9fb390..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixAttributeTest.java +++ /dev/null @@ -1,156 +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.math.impls.matrix; - -import java.util.Arrays; -import java.util.List; -import java.util.function.Function; -import org.apache.ignite.math.Matrix; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Attribute tests for matrices. - * - * TODO: WIP - */ -public class MatrixAttributeTest { - /** */ - private final List<MatrixAttributeTest.AttrCfg> attrCfgs = Arrays.asList( - new AttrCfg("isDense", Matrix::isDense, - DenseLocalOnHeapMatrix.class, DenseLocalOffHeapMatrix.class, RandomMatrix.class, DiagonalMatrix.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) - ); - - /** */ - 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)) - ); - - /** */ - @Test - public void isDenseTest() { - assertAttribute("isDense"); - } - - /** */ - @Test - public void isArrayBasedTest() { - assertAttribute("isArrayBased"); - } - - /** */ - @Test - public void isSequentialAccessTest() { - assertAttribute("isSequentialAccess"); - } - - /** */ - @Test - public void isRandomAccessTest() { - assertAttribute("isRandomAccess"); - } - - /** */ - @Test - public void isDistributedTest() { - assertAttribute("isDistributed"); - } - - /** */ - private void assertAttribute(String name) { - final MatrixAttributeTest.AttrCfg attr = attrCfg(name); - - for (MatrixAttributeTest.Specification spec : specFixture) - spec.verify(attr); - } - - /** */ - private MatrixAttributeTest.AttrCfg attrCfg(String name) { - for (MatrixAttributeTest.AttrCfg attr : attrCfgs) - if (attr.name.equals(name)) - return attr; - - throw new IllegalArgumentException("Undefined attribute " + name); - } - - /** See http://en.wikipedia.org/wiki/Specification_pattern */ - private static class Specification { - /** */ - private final Matrix m; - /** */ - private final Class<? extends Matrix> underlyingType; - /** */ - private final List<String> attrsFromUnderlying; - /** */ - final String desc; - - /** */ - Specification(Matrix m, Class<? extends Matrix> underlyingType, String... attrsFromUnderlying) { - this.m = m; - this.underlyingType = underlyingType; - this.attrsFromUnderlying = Arrays.asList(attrsFromUnderlying); - final Class<? extends Matrix> clazz = m.getClass(); - desc = clazz.getSimpleName() + (clazz.equals(underlyingType) - ? "" : " (underlying type " + underlyingType.getSimpleName() + ")"); - } - - /** */ - Specification(Matrix m) { - this(m, m.getClass()); - } - - /** */ - void verify(MatrixAttributeTest.AttrCfg attr) { - final boolean obtained = attr.obtain.apply(m); - - final Class<? extends Matrix> typeToCheck - = attrsFromUnderlying.contains(attr.name) ? underlyingType : m.getClass(); - - final boolean exp = attr.trueInTypes.contains(typeToCheck); - - assertEquals("Unexpected " + attr.name + " value for " + desc, exp, obtained); - } - } - - /** */ - private static class AttrCfg { - /** */ - final String name; - /** */ - final Function<Matrix, Boolean> obtain; - /** */ - final List<Class> trueInTypes; - - /** */ - AttrCfg(String name, Function<Matrix, Boolean> obtain, Class... trueInTypes) { - this.name = name; - this.obtain = obtain; - this.trueInTypes = Arrays.asList(trueInTypes); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/d78e071a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixImplementationFixtures.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixImplementationFixtures.java b/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixImplementationFixtures.java deleted file mode 100644 index 88aa4fe..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/math/impls/matrix/MatrixImplementationFixtures.java +++ /dev/null @@ -1,381 +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.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; -import java.util.function.BiConsumer; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.function.Supplier; -import org.apache.ignite.math.Matrix; -import org.apache.ignite.math.impls.storage.matrix.FunctionMatrixStorage; -import org.jetbrains.annotations.NotNull; - -/** */ -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 - ); - - /** */ - void consumeSampleMatrix(BiConsumer<Matrix, String> consumer) { - for (Supplier<Iterable<Matrix>> fixtureSupplier : suppliers) { - final Iterable<Matrix> fixture = fixtureSupplier.get(); - - for (Matrix matrix : fixture) { - consumer.accept(matrix, fixture.toString()); - - matrix.destroy(); - } - } - } - - /** */ - private static class DenseLocalOnHeapMatrixFixture extends MatrixSizeIterator { - /** */ - DenseLocalOnHeapMatrixFixture() { - super(DenseLocalOnHeapMatrix::new, "DenseLocalOnHeapMatrix"); - } - } - - /** */ - private static class DenseLocalOffHeapMatrixFixture extends MatrixSizeIterator { - /** */ - DenseLocalOffHeapMatrixFixture() { - super(DenseLocalOffHeapMatrix::new, "DenseLocalOffHeapMatrix"); - } - } - - /** */ - private static class RandomMatrixFixture extends MatrixSizeIterator { - /** */ - RandomMatrixFixture() { - super(RandomMatrix::new, "RandomMatrix"); - } - } - - /** */ - private static class SparseLocalOnHeapMatrixFixture extends MatrixSizeIterator { - /** */ - SparseLocalOnHeapMatrixFixture() { - super(SparseLocalOnHeapMatrix::new, "SparseLocalOnHeapMatrix"); - } - } - - /** */ - private static class PivotedMatrixViewFixture extends WrapperMatrixIterator { - /** */ - PivotedMatrixViewFixture() { - super(PivotedMatrixView::new, "PivotedMatrixView over DenseLocalOnHeapMatrix"); - } - } - - /** */ - private static class MatrixViewFixture extends WrapperMatrixIterator { - /** */ - MatrixViewFixture() { - super((matrix) -> new MatrixView(matrix, 0, 0, matrix.rowSize(), matrix.columnSize()), - "MatrixView over DenseLocalOnHeapMatrix"); - } - } - - /** */ - 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}; - /** */ - private int sizeIdx = 0; - - /** */ - private BiFunction<Integer, Integer, ? extends Matrix> constructor; - /** */ - private String desc; - - /** */ - DiagonalIterator(BiFunction<Integer, Integer, ? extends Matrix> constructor, String desc) { - this.constructor = constructor; - this.desc = desc; - } - - /** */ - public BiFunction<Integer, Integer, ? extends Matrix> getConstructor() { - return constructor; - } - - /** */ - int getSizeIdx() { - return sizeIdx; - } - - /** */ - @Override public String toString() { - return desc + "{rows=" + sizes[sizeIdx] + ", cols=" + sizes[sizeIdx] + "}"; - } - - /** */ - boolean hasNextSize(int idx) { - return sizes[idx] != null; - } - - /** */ - Integer getSize(int idx) { - return sizes[idx]; - } - - /** */ - void nextIdx() { - sizeIdx++; - } - } - - /** */ - private static class WrapperMatrixIterator extends MatrixSizeIterator { - /** */ - private final Function<Matrix, Matrix> wrapperCtor; - - /** */ - WrapperMatrixIterator(Function<Matrix, Matrix> wrapperCtor, String desc) { - super(DenseLocalOnHeapMatrix::new, desc); - - this.wrapperCtor = wrapperCtor; - } - - /** {@inheritDoc} */ - @NotNull - @Override public Iterator<Matrix> iterator() { - return new Iterator<Matrix>() { - /** {@inheritDoc} */ - @Override public boolean hasNext() { - return hasNextCol(getSizeIdx()) && hasNextRow(getSizeIdx()); - } - - /** {@inheritDoc} */ - @Override public Matrix next() { - Matrix matrix = getConstructor().apply(getRow(getSizeIdx()), getCol(getSizeIdx())); - - nextIdx(); - - return wrapperCtor.apply(matrix); - } - }; - } - } - - /** */ - private static class MatrixSizeIterator implements Iterable<Matrix> { - /** */ - private final Integer[] rows = new Integer[] {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 512, 1024, null}; - /** */ - private final Integer[] cols = new Integer[] {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 1024, 512, null}; - /** */ - private int sizeIdx = 0; - - /** */ - private BiFunction<Integer, Integer, ? extends Matrix> constructor; - /** */ - private String desc; - - /** */ - MatrixSizeIterator(BiFunction<Integer, Integer, ? extends Matrix> constructor, String desc) { - this.constructor = constructor; - this.desc = desc; - } - - /** */ - public BiFunction<Integer, Integer, ? extends Matrix> getConstructor() { - return constructor; - } - - /** */ - int getSizeIdx() { - return sizeIdx; - } - - /** */ - @Override public String toString() { - return desc + "{rows=" + rows[sizeIdx] + ", cols=" + cols[sizeIdx] + "}"; - } - - /** */ - boolean hasNextRow(int idx) { - return rows[idx] != null; - } - - /** */ - boolean hasNextCol(int idx) { - return cols[idx] != null; - } - - /** */ - Integer getRow(int idx) { - return rows[idx]; - } - - /** */ - int getCol(int idx) { - return cols[idx]; - } - - /** {@inheritDoc} */ - @NotNull - @Override public Iterator<Matrix> iterator() { - return new Iterator<Matrix>() { - /** {@inheritDoc} */ - @Override public boolean hasNext() { - return hasNextCol(sizeIdx) && hasNextRow(sizeIdx); - } - - /** {@inheritDoc} */ - @Override public Matrix next() { - Matrix matrix = constructor.apply(rows[sizeIdx], cols[sizeIdx]); - - nextIdx(); - - return matrix; - } - }; - } - - /** */ - 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; - } - } -}