http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/TracerTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/TracerTest.java b/modules/math/src/test/java/org/apache/ignite/math/TracerTest.java deleted file mode 100644 index 35d2f60..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/TracerTest.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.math; - -import java.awt.Color; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import java.util.Optional; -import org.apache.ignite.math.impls.MathTestConstants; -import org.apache.ignite.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.math.impls.vector.DenseLocalOnHeapVector; -import org.junit.Test; - -import static java.nio.file.Files.createTempFile; -import static org.junit.Assert.assertEquals; - -/** - * Tests for {@link Tracer}. - */ -public class TracerTest { - /** */ private static final String DEFAULT_FORMAT = "%.10f"; - /** */ private static final double DEFAULT_DELTA = 0.000000001d; - - /** - * Color mapper that maps [0, 1] range into three distinct RGB segments. - */ - private static final Tracer.ColorMapper COLOR_MAPPER = new Tracer.ColorMapper() { - /** {@inheritDoc} */ - @Override public Color apply(Double d) { - if (d <= 0.33) - return Color.RED; - else if (d <= 0.66) - return Color.GREEN; - else - return Color.BLUE; - } - }; - - /** - * @param size Vector size. - */ - private Vector makeRandomVector(int size) { - DenseLocalOnHeapVector vec = new DenseLocalOnHeapVector(size); - - vec.assign((idx) -> Math.random()); - - return vec; - } - - /** - * @param rows Amount of rows in matrix. - * @param cols Amount of columns in matrix. - */ - private Matrix makeRandomMatrix(int rows, int cols) { - DenseLocalOnHeapMatrix mtx = new DenseLocalOnHeapMatrix(rows, cols); - - // Missing assign(f)? - mtx.map((d) -> Math.random()); - - return mtx; - } - - /** - * - */ - @Test - public void testAsciiVectorTracer() { - Vector vec = makeRandomVector(20); - - Tracer.showAscii(vec); - Tracer.showAscii(vec, "%2f"); - Tracer.showAscii(vec, "%.3g"); - } - - /** - * - */ - @Test - public void testAsciiMatrixTracer() { - Matrix mtx = makeRandomMatrix(10, 10); - - Tracer.showAscii(mtx); - Tracer.showAscii(mtx, "%2f"); - Tracer.showAscii(mtx, "%.3g"); - } - - /** - * - */ - @Test - public void testHtmlVectorTracer() throws IOException { - Vector vec1 = makeRandomVector(1000); - - // Default color mapping. - Tracer.showHtml(vec1); - - // Custom color mapping. - Tracer.showHtml(vec1, COLOR_MAPPER); - - // Default color mapping with sorted vector. - Tracer.showHtml(vec1.sort()); - } - - /** - * - */ - @Test - public void testHtmlMatrixTracer() throws IOException { - Matrix mtx1 = makeRandomMatrix(100, 100); - - // Custom color mapping. - Tracer.showHtml(mtx1, COLOR_MAPPER); - - Matrix mtx2 = new DenseLocalOnHeapMatrix(100, 100); - - double MAX = (double)(mtx2.rowSize() * mtx2.columnSize()); - - mtx2.assign((x, y) -> (double)(x * y) / MAX); - - Tracer.showHtml(mtx2); - } - - /** */ - @Test - public void testWriteVectorToCSVFile() throws IOException { - DenseLocalOnHeapVector vector = new DenseLocalOnHeapVector(MathTestConstants.STORAGE_SIZE); - - for (int i = 0; i < vector.size(); i++) - vector.set(i, Math.random()); - - Path file = createTempFile("vector", ".csv"); - - Tracer.saveAsCsv(vector, DEFAULT_FORMAT, file.toString()); - - System.out.println("Vector exported: " + file.getFileName()); - - List<String> strings = Files.readAllLines(file); - Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2); - String[] csvVals = reduce.get().split(","); - - for (int i = 0; i < vector.size(); i++) { - Double csvVal = Double.valueOf(csvVals[i]); - - assertEquals("Unexpected value.", csvVal, vector.get(i), DEFAULT_DELTA); - } - - Files.deleteIfExists(file); - } - - /** */ - @Test - public void testWriteMatrixToCSVFile() throws IOException { - DenseLocalOnHeapMatrix matrix = new DenseLocalOnHeapMatrix(MathTestConstants.STORAGE_SIZE, MathTestConstants.STORAGE_SIZE); - - for (int i = 0; i < matrix.rowSize(); i++) - for (int j = 0; j < matrix.columnSize(); j++) - matrix.set(i, j, Math.random()); - - Path file = createTempFile("matrix", ".csv"); - - Tracer.saveAsCsv(matrix, DEFAULT_FORMAT, file.toString()); - - System.out.println("Matrix exported: " + file.getFileName()); - - List<String> strings = Files.readAllLines(file); - Optional<String> reduce = strings.stream().reduce((s1, s2) -> s1 + s2); - String[] csvVals = reduce.get().split(","); - - for (int i = 0; i < matrix.rowSize(); i++) - for (int j = 0; j < matrix.columnSize(); j++) { - Double csvVal = Double.valueOf(csvVals[i * matrix.rowSize() + j]); - - assertEquals("Unexpected value.", csvVal, matrix.get(i, j), DEFAULT_DELTA); - } - - Files.deleteIfExists(file); - } -}
http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmark.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmark.java b/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmark.java deleted file mode 100644 index 4c3718a..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmark.java +++ /dev/null @@ -1,205 +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.benchmark; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import java.util.TimeZone; - -/** Refer {@link MathBenchmarkSelfTest} for usage examples. */ -class MathBenchmark { - /** */ - private final boolean outputToConsole; - - /** */ - private final String benchmarkName; - - /** */ - private final int measurementTimes; - - /** */ - private final int warmUpTimes; - - /** */ - private final String tag; - - /** */ - private final String comments; - - /** Constructor strictly for use within this class. */ - private MathBenchmark(String benchmarkName, boolean outputToConsole, int measurementTimes, int warmUpTimes, - String tag, String comments) { - this.benchmarkName = benchmarkName; - this.outputToConsole = outputToConsole; - this.measurementTimes = measurementTimes; - this.warmUpTimes = warmUpTimes; - this.tag = tag; - this.comments = comments; - validate(); - } - - /** - * Benchmark with specified name and default parameters, in particular, default output file. - * - * @param benchmarkName name - */ - MathBenchmark(String benchmarkName) { - this(benchmarkName, false, 100, 1, "", ""); - } - - /** - * Executes the code using config of this benchmark. - * - * @param code code to execute - * @throws Exception if something goes wrong - */ - void execute(BenchmarkCode code) throws Exception { - System.out.println("Started benchmark [" + benchmarkName + "]."); - - for (int cnt = 0; cnt < warmUpTimes; cnt++) - code.call(); - - final long start = System.currentTimeMillis(); - - for (int cnt = 0; cnt < measurementTimes; cnt++) - code.call(); - - final long end = System.currentTimeMillis(); - - writeResults(formatResults(start, end)); - - System.out.println("Finished benchmark [" + benchmarkName + "]."); - } - - /** - * Set optional output mode for using stdout. - * - * @return configured benchmark - */ - MathBenchmark outputToConsole() { - return new MathBenchmark(benchmarkName, true, measurementTimes, warmUpTimes, tag, comments); - } - - /** - * Set optional measurement times. - * - * @param param times - * @return configured benchmark - */ - MathBenchmark measurementTimes(int param) { - return new MathBenchmark(benchmarkName, outputToConsole, param, warmUpTimes, tag, comments); - } - - /** - * Set optional warm-up times. - * - * @param param times - * @return configured benchmark - */ - MathBenchmark warmUpTimes(int param) { - return new MathBenchmark(benchmarkName, outputToConsole, measurementTimes, param, tag, comments); - } - - /** - * Set optional tag to help filtering specific kind of benchmark results. - * - * @param param name - * @return configured benchmark - */ - MathBenchmark tag(String param) { - return new MathBenchmark(benchmarkName, outputToConsole, measurementTimes, warmUpTimes, param, comments); - } - - /** - * Set optional comments. - * - * @param param name - * @return configured benchmark - */ - MathBenchmark comments(String param) { - return new MathBenchmark(benchmarkName, outputToConsole, measurementTimes, warmUpTimes, tag, param); - } - - /** */ - private void writeResults(String results) throws Exception { - if (outputToConsole) { - System.out.println(results); - - return; - } - - new ResultsWriter().append(results); - } - - /** */ - private String formatResults(long start, long end) { - final String delim = ","; - - assert !formatDouble(1000_000_001.1).contains(delim) : "Formatted results contain [" + delim + "]."; - - final String ts = formatTs(start); - - assert !ts.contains(delim) : "Formatted timestamp contains [" + delim + "]."; - - return benchmarkName + - delim + - ts + // IMPL NOTE timestamp - delim + - formatDouble((double)(end - start) / measurementTimes) + - delim + - measurementTimes + - delim + - warmUpTimes + - delim + - tag + - delim + - comments; - } - - /** */ - private String formatDouble(double val) { - return String.format(Locale.US, "%f", val); - } - - /** */ - private String formatTs(long ts) { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); - - sdf.setTimeZone(TimeZone.getTimeZone("UTC")); - - return sdf.format(new Date(ts)); - } - - /** */ - private void validate() { - if (benchmarkName == null || benchmarkName.isEmpty()) - throw new IllegalArgumentException("Invalid benchmark name: [" + benchmarkName + "]."); - - if (measurementTimes < 1) - throw new IllegalArgumentException("Invalid measurement times: [" + measurementTimes + "]."); - } - - /** */ - interface BenchmarkCode { - // todo find out why Callable<Void> failed to work here - - /** */ - void call() throws Exception; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmarkSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmarkSelfTest.java b/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmarkSelfTest.java deleted file mode 100644 index 7a86461..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/benchmark/MathBenchmarkSelfTest.java +++ /dev/null @@ -1,100 +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.benchmark; - -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -/** */ -public class MathBenchmarkSelfTest { - /** */ - @Test - @Ignore("Benchmark tests are intended only for manual execution") - public void demoTest() throws Exception { - for (int i = 0; i < 2; i++) - new MathBenchmark("demo test") - .outputToConsole() // IMPL NOTE this is to write output into console instead of a file - .tag(null) // IMPL NOTE try null for tag, expect it to be formatted reasonably - .comments(null) // IMPL NOTE try null for comments, expect it to be formatted reasonably - .execute(() -> { - double seed = 1.1; - - for (int cnt = 0; cnt < 1000; cnt++) { - seed = Math.pow(seed, 2); - - assertTrue(seed > 0); - } - }); - } - - /** */ - @Test - @Ignore("Benchmark tests are intended only for manual execution") - public void configTest() throws Exception { - new MathBenchmark("demo config test") - .outputToConsole() - .measurementTimes(2) - .warmUpTimes(0) - .tag("demo tag") - .comments("demo comments") - .execute(() -> System.out.println("config test")); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - @Ignore("Benchmark tests are intended only for manual execution") - public void emptyNameTest() throws Exception { - new MathBenchmark("") - .outputToConsole() - .measurementTimes(1) - .warmUpTimes(1) - .tag("empty name test tag") - .comments("empty name test comments") - .execute(() -> System.out.println("empty name test")); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - @Ignore("Benchmark tests are intended only for manual execution") - public void nullDropboxPathTest() throws Exception { - new ResultsWriter(null, "whatever", "whatever"); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - @Ignore("Benchmark tests are intended only for manual execution") - public void nullDropboxUrlTest() throws Exception { - new ResultsWriter("whatever", null, "whatever"); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - @Ignore("Benchmark tests are intended only for manual execution") - public void nullDropboxTokenTest() throws Exception { - new ResultsWriter("whatever", "whatever", null); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - @Ignore("Benchmark tests are intended only for manual execution") - public void nullResultsTest() throws Exception { - new ResultsWriter("whatever", "whatever", "whatever").append(null); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/benchmark/ResultsWriter.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/benchmark/ResultsWriter.java b/modules/math/src/test/java/org/apache/ignite/math/benchmark/ResultsWriter.java deleted file mode 100644 index aeec156..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/benchmark/ResultsWriter.java +++ /dev/null @@ -1,127 +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.benchmark; - -import com.dropbox.core.DbxException; -import com.dropbox.core.DbxRequestConfig; -import com.dropbox.core.v2.DbxClientV2; -import com.dropbox.core.v2.files.WriteMode; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.util.UUID; - -/** */ -class ResultsWriter { - /** */ - private static final String DROPBOX_PATH - = "/benchmarks/math.benchmark.results.csv"; - - /** */ - private static final String DROPBOX_URL - = "https://www.dropbox.com/s/r7tcle31r7gaty8/math.benchmark.results.csv"; - - /** */ - private static final String ACCESS_TOKEN - = "1MMmQjEyzGAAAAAAAAAAfDFrQ6oBPPi4NX-iU_VrgmXB2JDXqRHGa125cTkkEQ0V"; - - /** */ - private final String dropboxPath; - /** */ - private final String dropboxUrl; - /** */ - private final String accessTok; - - /** */ - ResultsWriter(String dropboxPath, String dropboxUrl, String accessTok) { - this.dropboxPath = dropboxPath; - this.dropboxUrl = dropboxUrl; - this.accessTok = accessTok; - - if (dropboxPath == null || dropboxUrl == null || accessTok == null) - throw new IllegalArgumentException("Neither of dropbox path, URL, access token can be null."); - } - - /** **/ - ResultsWriter() { - this(DROPBOX_PATH, DROPBOX_URL, ACCESS_TOKEN); - } - - /** */ - void append(String res) throws DbxException, IOException { - if (res == null) - throw new IllegalArgumentException("benchmark result is null"); - - if (dropboxPath == null) { - System.out.println(res); - - return; - } - - append(res, client()); - } - - /** */ - private void append(String res, DbxClientV2 client) throws DbxException, IOException { - File tmp = createTmpFile(); - - try (FileOutputStream out = new FileOutputStream(tmp)) { - client.files().download(dropboxPath).download(out); - } - - writeResults(res, tmp); - - try (FileInputStream in = new FileInputStream(tmp)) { - client.files().uploadBuilder(dropboxPath).withMode(WriteMode.OVERWRITE).uploadAndFinish(in); - } - - if (!tmp.delete()) - System.out.println("Failed to delete " + tmp.getAbsolutePath()); - - System.out.println("Uploaded benchmark results to: " + dropboxUrl); - } - - /** */ - private void writeResults(String res, File tmp) throws IOException { - final String unixLineSeparator = "\n"; - - try (final PrintWriter writer = new PrintWriter(Files.newBufferedWriter(Paths.get(tmp.toURI()), - StandardOpenOption.APPEND, StandardOpenOption.CREATE))) { - writer.write(res + unixLineSeparator); - } - } - - /** */ - private File createTmpFile() throws IOException { - File tmp = File.createTempFile(UUID.randomUUID().toString(), ".csv"); - - tmp.deleteOnExit(); - - return tmp; - } - - /** */ - private DbxClientV2 client() { - return new DbxClientV2(DbxRequestConfig.newBuilder("dropbox/MathBenchmark").build(), accessTok); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/benchmark/VectorBenchmarkTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/benchmark/VectorBenchmarkTest.java b/modules/math/src/test/java/org/apache/ignite/math/benchmark/VectorBenchmarkTest.java deleted file mode 100644 index 1f7b204..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/benchmark/VectorBenchmarkTest.java +++ /dev/null @@ -1,138 +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.benchmark; - -import java.util.function.BiConsumer; -import java.util.function.Function; -import org.apache.ignite.math.Vector; -import org.apache.ignite.math.impls.vector.DenseLocalOffHeapVector; -import org.apache.ignite.math.impls.vector.DenseLocalOnHeapVector; -import org.junit.Ignore; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - -/** */ -public class VectorBenchmarkTest { - // todo add benchmarks for other methods in Vector and for other types of Vector and Matrix - - /** */ - @Test - @Ignore("Benchmark tests are intended only for manual execution") - public void testDenseLocalOnHeapVector() throws Exception { - benchmark("DenseLocalOnHeapVector basic mix", DenseLocalOnHeapVector::new, this::basicMix); - - benchmark("DenseLocalOnHeapVector fold map", DenseLocalOnHeapVector::new, this::foldMapMix); - } - - /** */ - @Test - @Ignore("Benchmark tests are intended only for manual execution") - public void testDenseLocalOffHeapVector() throws Exception { - benchmark("DenseLocalOffHeapVector basic mix", DenseLocalOffHeapVector::new, this::basicMix); - - benchmark("DenseLocalOffHeapVector fold map", DenseLocalOffHeapVector::new, this::foldMapMix); - } - - /** */ - private void benchmark(String namePrefix, Function<Integer, Vector> constructor, - BiConsumer<Integer, Function<Integer, Vector>> consumer) throws Exception { - assertNotNull(namePrefix); - - new MathBenchmark(namePrefix + " small sizes").execute(() -> { - for (int size : new int[] {2, 3, 4, 5, 6, 7}) - consumer.accept(size, constructor); - }); - - new MathBenchmark(namePrefix + " sizes powers of 2").execute(() -> { - for (int power : new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}) - consumer.accept(1 << power, constructor); - }); - - new MathBenchmark(namePrefix + " large sizes").execute(() -> { - for (int power : new int[] {10, 12, 14, 16}) - for (int delta : new int[] {-1, 0, 1}) - consumer.accept((1 << power) + delta, constructor); - }); - - new MathBenchmark(namePrefix + " extra large sizes") - .measurementTimes(10) - .execute(() -> { // IMPL NOTE trying below with power 22 almost killed my IDEA and laptop - for (int power : new int[] {17, 18, 19, 20, 21}) - for (int delta : new int[] {-1, 0}) // IMPL NOTE delta +1 is not intended for use here - consumer.accept((1 << power) + delta, constructor); - }); - } - - /** */ - private void basicMix(int size, Function<Integer, Vector> constructor) { - final Vector v1 = constructor.apply(size), v2 = constructor.apply(size); - - for (int idx = 0; idx < size; idx++) { - v1.set(idx, idx); - - v2.set(idx, size - idx); - } - - assertNotNull(v1.sum()); - - assertNotNull(v1.copy()); - - assertFalse(v1.getLengthSquared() < 0); - - assertNotNull(v1.normalize()); - - assertNotNull(v1.logNormalize()); - - assertFalse(v1.getDistanceSquared(v2) < 0); - - assertNotNull(v1.divide(2)); - - assertNotNull(v1.minus(v2)); - - assertNotNull(v1.plus(v2)); - - assertNotNull(v1.dot(v2)); - - assertNotNull(v1.assign(v2)); - - assertNotNull(v1.assign(1)); // IMPL NOTE this would better be last test for it sets all values the same - } - - /** */ - private void foldMapMix(int size, Function<Integer, Vector> constructor) { - final Vector v1 = constructor.apply(size), v2 = constructor.apply(size); - - for (int idx = 0; idx < size; idx++) { - v1.set(idx, idx); - - v2.set(idx, size - idx); - } - - assertNotNull(v1.map((val) -> (val + 1))); - - assertNotNull(v1.map(v2, (one, other) -> one + other / 2.0)); - - assertNotNull(v1.map((val, val1) -> (val + val1), 2.0)); - - assertNotNull(v1.foldMap((sum, val) -> (val + sum), (val) -> val, 0.0)); - - assertNotNull(v1.foldMap(v2, (sum, val) -> (val + sum), (val1, val2) -> val1 + val2, 0.0)); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/benchmark/package-info.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/benchmark/package-info.java b/modules/math/src/test/java/org/apache/ignite/math/benchmark/package-info.java deleted file mode 100644 index cbf5d36..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/benchmark/package-info.java +++ /dev/null @@ -1,18 +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.benchmark; http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/decompositions/CholeskyDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/decompositions/CholeskyDecompositionTest.java b/modules/math/src/test/java/org/apache/ignite/math/decompositions/CholeskyDecompositionTest.java deleted file mode 100644 index fa311e0..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/decompositions/CholeskyDecompositionTest.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.math.decompositions; - -import org.apache.ignite.math.Matrix; -import org.apache.ignite.math.Vector; -import org.apache.ignite.math.exceptions.CardinalityException; -import org.apache.ignite.math.exceptions.NonPositiveDefiniteMatrixException; -import org.apache.ignite.math.exceptions.NonSymmetricMatrixException; -import org.apache.ignite.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.math.impls.vector.DenseLocalOnHeapVector; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** */ -public class CholeskyDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link 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(expected = AssertionError.class) - public void nullMatrixTest() { - new CholeskyDecomposition(null); - } - - /** */ - @Test(expected = CardinalityException.class) - public void wrongMatrixSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - @Test(expected = NonSymmetricMatrixException.class) - public void nonSymmetricMatrixTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 10.0d}, - {-1.0d, 2.0d, -1.0d}, - {-10.0d, -1.0d, 2.0d} - })); - } - - /** */ - @Test(expected = NonPositiveDefiniteMatrixException.class) - public void nonAbsPositiveMatrixTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 0.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveWrongVectorSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapVector(2)); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveWrongMatrixSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - private void basicTest(Matrix m) { - // This decomposition is useful when dealing with systems of linear equations of the form - // m x = b where m is a Hermitian matrix. - // For such systems Cholesky decomposition provides - // more effective method of solving compared to LU decomposition. - // Suppose we want to solve system - // m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs - // as a matrix of the form - // (b1, b2, ..., bm) - // to the method Cholesky::solve which returns solutions in the form - // (sol1, sol2, ..., solm) - CholeskyDecomposition dec = new CholeskyDecomposition(m); - assertEquals("Unexpected value for decomposition determinant.", - 4d, dec.getDeterminant(), 0d); - - Matrix l = dec.getL(); - Matrix lt = dec.getLT(); - - assertNotNull("Matrix l is expected to be not null.", l); - assertNotNull("Matrix lt is expected to be not null.", lt); - - for (int row = 0; row < l.rowSize(); row++) - for (int col = 0; col < l.columnSize(); col++) - assertEquals("Unexpected value transposed matrix at (" + row + "," + col + ").", - l.get(row, col), lt.get(col, row), 0d); - - Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { - {4.0, -6.0, 7.0}, - {1.0, 1.0, 1.0} - }).transpose(); - Matrix sol = dec.solve(bs); - - assertNotNull("Solution matrix is expected to be not null.", sol); - assertEquals("Solution rows are not as expected.", bs.rowSize(), sol.rowSize()); - assertEquals("Solution columns are not as expected.", bs.columnSize(), sol.columnSize()); - - for (int i = 0; i < sol.columnSize(); i++) - assertNotNull("Solution matrix column is expected to be not null at index " + i, sol.viewColumn(i)); - - Vector b = new DenseLocalOnHeapVector(new double[] {4.0, -6.0, 7.0}); - Vector solVec = dec.solve(b); - - for (int idx = 0; idx < b.size(); idx++) - assertEquals("Unexpected value solution vector at " + idx, - b.get(idx), solVec.get(idx), 0d); - - dec.destroy(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/decompositions/EigenDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/decompositions/EigenDecompositionTest.java b/modules/math/src/test/java/org/apache/ignite/math/decompositions/EigenDecompositionTest.java deleted file mode 100644 index e4e7b15..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/decompositions/EigenDecompositionTest.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.math.decompositions; - -import org.apache.ignite.math.Matrix; -import org.apache.ignite.math.Vector; -import org.apache.ignite.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Tests for {@link EigenDecomposition} - */ -public class EigenDecompositionTest { - /** */ - private static final double EPSILON = 1e-11; - - /** */ - @Test - public void testMatrixWithRealEigenvalues() { - test(new double[][] { - {1.0d, 0.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d, 0.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d}}, - new double[] {1, 2, 2, 1}); - } - - /** */ - @Test - public void testSymmetricMatrix() { - EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d, 1.0d}, - {0.0d, 1.0d, 0.0d, 1.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d}})); - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertNotNull("Matrix d is expected to be not null.", d); - assertNotNull("Matrix v is expected to be not null.", v); - - assertEquals("Unexpected rows in d matrix.", 4, d.rowSize()); - assertEquals("Unexpected cols in d matrix.", 4, d.columnSize()); - - assertEquals("Unexpected rows in v matrix.", 4, v.rowSize()); - assertEquals("Unexpected cols in v matrix.", 4, v.columnSize()); - - assertIsDiagonalNonZero(d); - - decomposition.destroy(); - } - - /** */ - @Test - public void testNonSquareMatrix() { - EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d}, - {0.0d, 0.0d, 2.0d}, - {1.0d, 1.0d, 0.0d}})); - // todo find out why decomposition of 3X4 matrix throws row index exception - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertNotNull("Matrix d is expected to be not null.", d); - assertNotNull("Matrix v is expected to be not null.", v); - - assertEquals("Unexpected rows in d matrix.", 4, d.rowSize()); - assertEquals("Unexpected cols in d matrix.", 4, d.columnSize()); - - assertEquals("Unexpected rows in v matrix.", 4, v.rowSize()); - assertEquals("Unexpected cols in v matrix.", 3, v.columnSize()); - - assertIsDiagonal(d, true); - - decomposition.destroy(); - } - - /** */ - private void test(double[][] mRaw, double[] expRealEigenValues) { - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(mRaw); - EigenDecomposition decomposition = new EigenDecomposition(m); - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertIsDiagonalNonZero(d); - - // check that d's diagonal consists of eigenvalues of m. - assertDiagonalConsistsOfEigenvalues(m, d, v); - - // m = v d v^{-1} is equivalent to - // m v = v d - assertMatricesAreEqual(m.times(v), v.times(d)); - - assertEigenvalues(decomposition, expRealEigenValues); - - decomposition.destroy(); - } - - /** */ - private void assertEigenvalues(EigenDecomposition decomposition, double[] expRealEigenValues) { - Vector real = decomposition.getRealEigenValues(); - Vector imag = decomposition.getImagEigenvalues(); - - assertEquals("Real values size differs from expected.", expRealEigenValues.length, real.size()); - assertEquals("Imag values size differs from expected.", expRealEigenValues.length, imag.size()); - - for (int idx = 0; idx < expRealEigenValues.length; idx++) { - assertEquals("Real eigen value differs from expected at " + idx, - expRealEigenValues[idx], real.get(idx), 0d); - - assertEquals("Imag eigen value differs from expected at " + idx, - 0d, imag.get(idx), 0d); - } - - } - - /** */ - private void assertDiagonalConsistsOfEigenvalues(DenseLocalOnHeapMatrix m, Matrix d, Matrix v) { - int n = m.columnSize(); - for (int i = 0; i < n; i++) { - Vector eigenVector = v.viewColumn(i); - double eigenVal = d.getX(i, i); - assertVectorsAreEqual(m.times(eigenVector), eigenVector.times(eigenVal)); - } - - } - - /** */ - private void assertMatricesAreEqual(Matrix exp, Matrix actual) { - assertTrue("The row sizes of matrices are not equal", exp.rowSize() == actual.rowSize()); - assertTrue("The col sizes of matrices are not equal", exp.columnSize() == actual.columnSize()); - - // Since matrix is square, we need only one dimension - int n = exp.columnSize(); - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - assertEquals("Values should be equal", exp.getX(i, j), actual.getX(i, j), EPSILON); - } - - /** */ - private void assertVectorsAreEqual(Vector exp, Vector actual) { - assertTrue("Vectors sizes are not equal", exp.size() == actual.size()); - - // Since matrix is square, we need only one dimension - int n = exp.size(); - - for (int i = 0; i < n; i++) - assertEquals("Values should be equal", exp.getX(i), actual.getX(i), EPSILON); - } - - /** */ - private void assertIsDiagonalNonZero(Matrix m) { - assertIsDiagonal(m, false); - } - - /** */ - private void assertIsDiagonal(Matrix m, boolean zeroesAllowed) { - // Since matrix is square, we need only one dimension - int n = m.columnSize(); - - assertEquals("Diagonal matrix is not square", n, m.rowSize()); - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - assertTrue("Matrix is not diagonal, violation at (" + i + "," + j + ")", - ((i == j) && (zeroesAllowed || m.getX(i, j) != 0)) - || ((i != j) && m.getX(i, j) == 0)); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/decompositions/LUDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/decompositions/LUDecompositionTest.java b/modules/math/src/test/java/org/apache/ignite/math/decompositions/LUDecompositionTest.java deleted file mode 100644 index 0feb48f..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/decompositions/LUDecompositionTest.java +++ /dev/null @@ -1,250 +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.Vector; -import org.apache.ignite.math.exceptions.CardinalityException; -import org.apache.ignite.math.exceptions.SingularMatrixException; -import org.apache.ignite.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.math.impls.vector.DenseLocalOnHeapVector; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Tests for {@link LUDecomposition}. - */ -public class LUDecompositionTest { - /** */ - private Matrix testL; - /** */ - private Matrix testU; - /** */ - private Matrix testP; - /** */ - private Matrix testMatrix; - /** */ - private int[] rawPivot; - - /** */ - @Before - public void setUp() { - double[][] rawMatrix = new double[][] { - {2.0d, 1.0d, 1.0d, 0.0d}, - {4.0d, 3.0d, 3.0d, 1.0d}, - {8.0d, 7.0d, 9.0d, 5.0d}, - {6.0d, 7.0d, 9.0d, 8.0d}}; - double[][] rawL = { - {1.0d, 0.0d, 0.0d, 0.0d}, - {3.0d / 4.0d, 1.0d, 0.0d, 0.0d}, - {1.0d / 2.0d, -2.0d / 7.0d, 1.0d, 0.0d}, - {1.0d / 4.0d, -3.0d / 7.0d, 1.0d / 3.0d, 1.0d}}; - double[][] rawU = { - {8.0d, 7.0d, 9.0d, 5.0d}, - {0.0d, 7.0d / 4.0d, 9.0d / 4.0d, 17.0d / 4.0d}, - {0.0d, 0.0d, -6.0d / 7.0d, -2.0d / 7.0d}, - {0.0d, 0.0d, 0.0d, 2.0d / 3.0d}}; - double[][] rawP = new double[][] { - {0, 0, 1.0d, 0}, - {0, 0, 0, 1.0d}, - {0, 1.0d, 0, 0}, - {1.0d, 0, 0, 0}}; - - rawPivot = new int[] {3, 4, 2, 1}; - - testMatrix = new DenseLocalOnHeapMatrix(rawMatrix); - testL = new DenseLocalOnHeapMatrix(rawL); - testU = new DenseLocalOnHeapMatrix(rawU); - testP = new DenseLocalOnHeapMatrix(rawP); - } - - /** */ - @Test - public void getL() throws Exception { - Matrix luDecompositionL = new LUDecomposition(testMatrix).getL(); - - assertEquals("Unexpected row size.", testL.rowSize(), luDecompositionL.rowSize()); - assertEquals("Unexpected column size.", testL.columnSize(), luDecompositionL.columnSize()); - - for (int i = 0; i < testL.rowSize(); i++) - for (int j = 0; j < testL.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testL.getX(i, j), luDecompositionL.getX(i, j), 0.0000001d); - - luDecompositionL.destroy(); - } - - /** */ - @Test - public void getU() throws Exception { - Matrix luDecompositionU = new LUDecomposition(testMatrix).getU(); - - assertEquals("Unexpected row size.", testU.rowSize(), luDecompositionU.rowSize()); - assertEquals("Unexpected column size.", testU.columnSize(), luDecompositionU.columnSize()); - - for (int i = 0; i < testU.rowSize(); i++) - for (int j = 0; j < testU.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testU.getX(i, j), luDecompositionU.getX(i, j), 0.0000001d); - - luDecompositionU.destroy(); - } - - /** */ - @Test - public void getP() throws Exception { - Matrix luDecompositionP = new LUDecomposition(testMatrix).getP(); - - assertEquals("Unexpected row size.", testP.rowSize(), luDecompositionP.rowSize()); - assertEquals("Unexpected column size.", testP.columnSize(), luDecompositionP.columnSize()); - - for (int i = 0; i < testP.rowSize(); i++) - for (int j = 0; j < testP.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testP.getX(i, j), luDecompositionP.getX(i, j), 0.0000001d); - - luDecompositionP.destroy(); - } - - /** */ - @Test - public void getPivot() throws Exception { - Vector pivot = new LUDecomposition(testMatrix).getPivot(); - - assertEquals("Unexpected pivot size.", rawPivot.length, pivot.size()); - - for (int i = 0; i < testU.rowSize(); i++) - assertEquals("Unexpected value at " + i, rawPivot[i], (int)pivot.get(i) + 1); - } - - /** - * Test for {@link DecompositionSupport} features. - */ - @Test - public void decompositionSupportTest() { - LUDecomposition dec = new LUDecomposition(new PivotedMatrixView(testMatrix)); - Matrix luDecompositionL = dec.getL(); - - assertEquals("Unexpected L row size.", testL.rowSize(), luDecompositionL.rowSize()); - assertEquals("Unexpected L column size.", testL.columnSize(), luDecompositionL.columnSize()); - - for (int i = 0; i < testL.rowSize(); i++) - for (int j = 0; j < testL.columnSize(); j++) - assertEquals("Unexpected L value at (" + i + "," + j + ").", - testL.getX(i, j), luDecompositionL.getX(i, j), 0.0000001d); - - Matrix luDecompositionU = dec.getU(); - - assertEquals("Unexpected U row size.", testU.rowSize(), luDecompositionU.rowSize()); - assertEquals("Unexpected U column size.", testU.columnSize(), luDecompositionU.columnSize()); - - for (int i = 0; i < testU.rowSize(); i++) - for (int j = 0; j < testU.columnSize(); j++) - assertEquals("Unexpected U value at (" + i + "," + j + ").", - testU.getX(i, j), luDecompositionU.getX(i, j), 0.0000001d); - - Matrix luDecompositionP = dec.getP(); - - assertEquals("Unexpected P row size.", testP.rowSize(), luDecompositionP.rowSize()); - assertEquals("Unexpected P column size.", testP.columnSize(), luDecompositionP.columnSize()); - - for (int i = 0; i < testP.rowSize(); i++) - for (int j = 0; j < testP.columnSize(); j++) - assertEquals("Unexpected P value at (" + i + "," + j + ").", - testP.getX(i, j), luDecompositionP.getX(i, j), 0.0000001d); - - dec.destroy(); - } - - /** */ - @Test - public void singularDeterminant() throws Exception { - assertEquals("Unexpected determinant for singular matrix decomposition.", - 0d, new LUDecomposition(new DenseLocalOnHeapMatrix(2, 2)).determinant(), 0d); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveVecWrongSize() throws Exception { - new LUDecomposition(testMatrix).solve(new DenseLocalOnHeapVector(testMatrix.rowSize() + 1)); - } - - /** */ - @Test(expected = SingularMatrixException.class) - public void solveVecSingularMatrix() throws Exception { - new LUDecomposition(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())) - .solve(new DenseLocalOnHeapVector(testMatrix.rowSize())); - } - - /** */ - @Test - public void solveVec() throws Exception { - Vector sol = new LUDecomposition(new PivotedMatrixView(testMatrix)) - .solve(new DenseLocalOnHeapVector(testMatrix.rowSize())); - - assertEquals("Wrong solution vector size.", testMatrix.rowSize(), sol.size()); - - for (int i = 0; i < sol.size(); i++) - assertEquals("Unexpected value at index " + i, 0d, sol.getX(i), 0.0000001d); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveMtxWrongSize() throws Exception { - new LUDecomposition(testMatrix).solve( - new DenseLocalOnHeapMatrix(testMatrix.rowSize() + 1, testMatrix.rowSize())); - } - - /** */ - @Test(expected = SingularMatrixException.class) - public void solveMtxSingularMatrix() throws Exception { - new LUDecomposition(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())) - .solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())); - } - - /** */ - @Test - public void solveMtx() throws Exception { - Matrix sol = new LUDecomposition(new PivotedMatrixView(testMatrix)) - .solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())); - - assertEquals("Wrong solution matrix row size.", testMatrix.rowSize(), sol.rowSize()); - - assertEquals("Wrong solution matrix column size.", testMatrix.rowSize(), sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected P value at (" + row + "," + col + ").", - 0d, sol.getX(row, col), 0.0000001d); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new LUDecomposition(null); - } - - /** */ - @Test(expected = CardinalityException.class) - public void nonSquareMatrixTest() { - new LUDecomposition(new DenseLocalOnHeapMatrix(2, 3)); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/decompositions/QRDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/decompositions/QRDecompositionTest.java b/modules/math/src/test/java/org/apache/ignite/math/decompositions/QRDecompositionTest.java deleted file mode 100644 index 3bb92d1..0000000 --- a/modules/math/src/test/java/org/apache/ignite/math/decompositions/QRDecompositionTest.java +++ /dev/null @@ -1,139 +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 QRDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link 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(expected = AssertionError.class) - public void nullMatrixTest() { - new QRDecomposition(null); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - public void solveWrongMatrixSizeTest() { - new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - private void basicTest(Matrix m) { - QRDecomposition dec = new QRDecomposition(m); - assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank()); - - Matrix q = dec.getQ(); - Matrix r = dec.getR(); - - assertNotNull("Matrix q is expected to be not null.", q); - assertNotNull("Matrix r is expected to be not null.", r); - - Matrix qSafeCp = safeCopy(q); - - Matrix expIdentity = qSafeCp.times(qSafeCp.transpose()); - - final double delta = 0.0001; - - for (int row = 0; row < expIdentity.rowSize(); row++) - for (int col = 0; col < expIdentity.columnSize(); col++) - assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", - row == col ? 1d : 0d, expIdentity.get(col, row), delta); - - for (int row = 0; row < r.rowSize(); row++) - for (int col = 0; col < row - 1; col++) - assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", - 0d, r.get(row, col), delta); - - Matrix recomposed = qSafeCp.times(r); - - for (int row = 0; row < m.rowSize(); row++) - for (int col = 0; col < m.columnSize(); col++) - assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", - m.get(row, col), recomposed.get(row, col), delta); - - Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10)); - assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize()); - assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", - 0d, sol.get(row, col), delta); - - dec.destroy(); - - QRDecomposition dec1 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d}, - {-1.0d, 2.0d}, - {0.0d, -1.0d} - })); - - assertTrue("Unexpected value for full rank in decomposition " + dec1, dec1.hasFullRank()); - - dec1.destroy(); - - QRDecomposition dec2 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d, 0.0d}, - {0.0d, -1.0d, 2.0d, 0.0d} - })); - - assertTrue("Unexpected value for full rank in decomposition " + dec2, dec2.hasFullRank()); - - dec2.destroy(); - } - - /** */ - private Matrix safeCopy(Matrix orig) { - return new DenseLocalOnHeapMatrix(orig.rowSize(), orig.columnSize()).assign(orig); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/732dfea9/modules/math/src/test/java/org/apache/ignite/math/decompositions/SingularValueDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/decompositions/SingularValueDecompositionTest.java b/modules/math/src/test/java/org/apache/ignite/math/decompositions/SingularValueDecompositionTest.java deleted file mode 100644 index d0b89f8..0000000 --- a/modules/math/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/732dfea9/modules/math/src/test/java/org/apache/ignite/math/impls/MathTestConstants.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/impls/MathTestConstants.java b/modules/math/src/test/java/org/apache/ignite/math/impls/MathTestConstants.java deleted file mode 100644 index 122c62e..0000000 --- a/modules/math/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/732dfea9/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/CacheMatrixTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/CacheMatrixTest.java b/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/CacheMatrixTest.java deleted file mode 100644 index 8a6d077..0000000 --- a/modules/math/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/732dfea9/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOffHeapMatrixConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOffHeapMatrixConstructorTest.java b/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOffHeapMatrixConstructorTest.java deleted file mode 100644 index 1f0537c..0000000 --- a/modules/math/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/732dfea9/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOnHeapMatrixConstructorTest.java ---------------------------------------------------------------------- diff --git a/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOnHeapMatrixConstructorTest.java b/modules/math/src/test/java/org/apache/ignite/math/impls/matrix/DenseLocalOnHeapMatrixConstructorTest.java deleted file mode 100644 index 0a25e31..0000000 --- a/modules/math/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); - } -}
