You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2018/01/17 10:47:28 UTC
[11/32] ignite git commit: IGNITE-7203 Java 8 by default
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java
new file mode 100644
index 0000000..3b4f27d
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixCustomStorageExample.java
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.matrix;
+
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.MatrixStorage;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.impls.matrix.AbstractMatrix;
+import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
+
+/**
+ * This example shows how to create custom {@link Matrix} based on custom {@link MatrixStorage}.
+ */
+public final class MatrixCustomStorageExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Matrix API usage example started.");
+
+ System.out.println("\n>>> Creating a matrix to be transposed.");
+ double[][] data = new double[][] {{1, 2, 3}, {4, 5, 6}};
+ Matrix m = new MatrixCustomStorage(data);
+ Matrix transposed = m.transpose();
+
+ System.out.println(">>> Matrix: ");
+ MatrixExampleUtil.print(m);
+ System.out.println(">>> Transposed matrix: ");
+ MatrixExampleUtil.print(transposed);
+
+ MatrixExampleUtil.verifyTransposition(m, transposed);
+
+ System.out.println("\n>>> Creating matrices to be multiplied.");
+ double[][] data1 = new double[][] {{1, 2}, {3, 4}};
+ double[][] data2 = new double[][] {{5, 6}, {7, 8}};
+
+ Matrix m1 = new MatrixCustomStorage(data1);
+ Matrix m2 = new MatrixCustomStorage(data2);
+ Matrix mult = m1.times(m2);
+
+ System.out.println(">>> First matrix: ");
+ MatrixExampleUtil.print(m1);
+ System.out.println(">>> Second matrix: ");
+ MatrixExampleUtil.print(m2);
+ System.out.println(">>> Matrix product: ");
+ MatrixExampleUtil.print(mult);
+
+ System.out.println("\n>>> Calculating matrices determinants.");
+ double det1 = m1.determinant();
+ double det2 = m2.determinant();
+ double detMult = mult.determinant();
+ boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d;
+
+ System.out.println(">>> First matrix determinant: [" + det1 + "].");
+ System.out.println(">>> Second matrix determinant: [" + det2 + "].");
+ System.out.println(">>> Matrix product determinant: [" + detMult
+ + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "].");
+
+ System.out.println("Determinant of product matrix [" + detMult
+ + "] should be equal to product of determinants [" + (det1 * det2) + "].");
+
+ System.out.println("\n>>> Matrix API usage example completed.");
+ }
+
+ /**
+ * Example of vector with custom storage, modeled after
+ * {@link org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix}.
+ */
+ static class MatrixCustomStorage extends AbstractMatrix {
+ /**
+ *
+ */
+ public MatrixCustomStorage() {
+ // No-op.
+ }
+
+ /**
+ * @param rows Amount of rows in a matrix.
+ * @param cols Amount of columns in a matrix.
+ */
+ MatrixCustomStorage(int rows, int cols) {
+ assert rows > 0;
+ assert cols > 0;
+
+ setStorage(new ExampleMatrixStorage(rows, cols));
+ }
+
+ /**
+ * @param mtx Source matrix.
+ */
+ MatrixCustomStorage(double[][] mtx) {
+ assert mtx != null;
+
+ setStorage(new ExampleMatrixStorage(mtx));
+ }
+
+ /**
+ * @param orig original matrix to be copied.
+ */
+ private MatrixCustomStorage(MatrixCustomStorage orig) {
+ assert orig != null;
+
+ setStorage(new ExampleMatrixStorage(orig.rowSize(), orig.columnSize()));
+
+ assign(orig);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix copy() {
+ return new MatrixCustomStorage(this);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix like(int rows, int cols) {
+ return new MatrixCustomStorage(rows, cols);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector likeVector(int crd) {
+ return new DenseLocalOnHeapVector(crd);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java
new file mode 100644
index 0000000..755f36c
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExample.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.matrix;
+
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
+
+/**
+ * This example shows how to use {@link Matrix} API.
+ */
+public final class MatrixExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Basic Matrix API usage example started.");
+
+ System.out.println("\n>>> Creating a matrix to be transposed.");
+ double[][] data = new double[][] {{1, 2, 3}, {4, 5, 6}};
+ Matrix m = new DenseLocalOnHeapMatrix(data);
+ Matrix transposed = m.transpose();
+
+ System.out.println(">>> Matrix: ");
+ MatrixExampleUtil.print(m);
+ System.out.println(">>> Transposed matrix: ");
+ MatrixExampleUtil.print(transposed);
+
+ MatrixExampleUtil.verifyTransposition(m, transposed);
+
+ System.out.println("\n>>> Creating matrices to be multiplied.");
+ double[][] data1 = new double[][] {{1, 2}, {3, 4}};
+ double[][] data2 = new double[][] {{5, 6}, {7, 8}};
+
+ Matrix m1 = new DenseLocalOnHeapMatrix(data1);
+ Matrix m2 = new DenseLocalOnHeapMatrix(data2);
+ Matrix mult = m1.times(m2);
+
+ System.out.println(">>> First matrix: ");
+ MatrixExampleUtil.print(m1);
+ System.out.println(">>> Second matrix: ");
+ MatrixExampleUtil.print(m2);
+ System.out.println(">>> Matrix product: ");
+ MatrixExampleUtil.print(mult);
+
+ System.out.println("\n>>> Calculating matrices determinants.");
+ double det1 = m1.determinant();
+ double det2 = m2.determinant();
+ double detMult = mult.determinant();
+ boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d;
+
+ System.out.println(">>> First matrix determinant: [" + det1 + "].");
+ System.out.println(">>> Second matrix determinant: [" + det2 + "].");
+ System.out.println(">>> Matrix product determinant: [" + detMult
+ + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "].");
+
+ System.out.println("Determinant of product matrix [" + detMult
+ + "] should be equal to product of determinants [" + (det1 * det2) + "].");
+
+ System.out.println("\n>>> Basic Matrix API usage example completed.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExampleUtil.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExampleUtil.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExampleUtil.java
new file mode 100644
index 0000000..af12e15
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/MatrixExampleUtil.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.matrix;
+
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.Tracer;
+
+/**
+ * Utility functions for {@link Matrix} API examples.
+ */
+class MatrixExampleUtil {
+ /**
+ * Verifies matrix transposition.
+ *
+ * @param m Original matrix.
+ * @param transposed Transposed matrix.
+ */
+ static void verifyTransposition(Matrix m, Matrix transposed) {
+ for (int row = 0; row < m.rowSize(); row++)
+ for (int col = 0; col < m.columnSize(); col++) {
+ double val = m.get(row, col);
+ double valTransposed = transposed.get(col, row);
+
+ assert val == valTransposed : "Values not equal at (" + row + "," + col
+ + "), original: " + val + " transposed: " + valTransposed;
+ }
+ }
+
+ /**
+ * Prints matrix values to console.
+ *
+ * @param m Matrix to print.
+ */
+ static void print(Matrix m) {
+ Tracer.showAscii(m);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java
new file mode 100644
index 0000000..db01794
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/OffHeapMatrixExample.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.matrix;
+
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrix;
+
+/**
+ * This example shows how to create and use off-heap versions of {@link Matrix}.
+ */
+public final class OffHeapMatrixExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Off-heap matrix API usage example started.");
+
+ System.out.println("\n>>> Creating a matrix to be transposed.");
+ double[][] data = new double[][] {{1, 2, 3}, {4, 5, 6}};
+ Matrix m = new DenseLocalOffHeapMatrix(data.length, data[0].length);
+ m.assign(data);
+ Matrix transposed = m.transpose();
+
+ System.out.println(">>> Matrix: ");
+ MatrixExampleUtil.print(m);
+ System.out.println(">>> Transposed matrix: ");
+ MatrixExampleUtil.print(transposed);
+
+ MatrixExampleUtil.verifyTransposition(m, transposed);
+
+ System.out.println("\n>>> Creating matrices to be multiplied.");
+ double[][] data1 = new double[][] {{1, 2}, {3, 4}};
+ double[][] data2 = new double[][] {{5, 6}, {7, 8}};
+
+ Matrix m1 = new DenseLocalOffHeapMatrix(data1.length, data1[0].length);
+ Matrix m2 = new DenseLocalOffHeapMatrix(data2.length, data2[0].length);
+
+ m1.assign(data1);
+ m2.assign(data2);
+
+ Matrix mult = m1.times(m2);
+
+ System.out.println(">>> First matrix: ");
+ MatrixExampleUtil.print(m1);
+ System.out.println(">>> Second matrix: ");
+ MatrixExampleUtil.print(m2);
+ System.out.println(">>> Matrix product: ");
+ MatrixExampleUtil.print(mult);
+
+ System.out.println("\n>>> Calculating matrices determinants.");
+ double det1 = m1.determinant();
+ double det2 = m2.determinant();
+ double detMult = mult.determinant();
+ boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d;
+
+ System.out.println(">>> First matrix determinant: [" + det1 + "].");
+ System.out.println(">>> Second matrix determinant: [" + det2 + "].");
+ System.out.println(">>> Matrix product determinant: [" + detMult
+ + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "].");
+
+ System.out.println("Determinant of product matrix [" + detMult
+ + "] should be equal to product of determinants [" + (det1 * det2) + "].");
+
+ System.out.println("\n>>> Off-heap matrix API usage example completed.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java
new file mode 100644
index 0000000..c73a8a0
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseDistributedMatrixExample.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.matrix;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.StorageConstants;
+import org.apache.ignite.ml.math.impls.matrix.CacheMatrix;
+import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix;
+import org.apache.ignite.thread.IgniteThread;
+
+/**
+ * This example shows how to create and use {@link SparseDistributedMatrix} API.
+ *
+ * Unlike the {@link CacheMatrix} the {@link SparseDistributedMatrix} creates it's own cache.
+ */
+public class SparseDistributedMatrixExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) throws InterruptedException {
+ System.out.println();
+ System.out.println(">>> Sparse distributed matrix API usage example started.");
+ // Start ignite grid.
+ try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
+ System.out.println(">>> Ignite grid started.");
+ // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
+ // because we create ignite cache internally.
+ IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(), SparseDistributedMatrixExample.class.getSimpleName(), () -> {
+
+ double[][] testValues = {{1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}};
+
+ System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
+ // Create SparseDistributedMatrix, new cache will be created automagically.
+ SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(testValues.length, testValues[0].length,
+ StorageConstants.ROW_STORAGE_MODE, StorageConstants.RANDOM_ACCESS_MODE);
+
+ distributedMatrix.assign(testValues);
+
+ System.out.println("Sum of all matrix elements is " + distributedMatrix.sum());
+
+ System.out.println(">>> Destroy SparseDistributedMatrix after using.");
+ // Destroy internal cache.
+ distributedMatrix.destroy();
+ });
+
+ igniteThread.start();
+
+ igniteThread.join();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java
new file mode 100644
index 0000000..a03688d
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/SparseMatrixExample.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.matrix;
+
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix;
+
+/**
+ * This example shows how to use sparse {@link Matrix} API.
+ */
+public final class SparseMatrixExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Sparse matrix API usage example started.");
+
+ System.out.println("\n>>> Creating a matrix to be transposed.");
+ double[][] data = new double[][] {{1, 2, 3}, {4, 5, 6}};
+ Matrix m = new SparseLocalOnHeapMatrix(data.length, data[0].length);
+ m.assign(data);
+ Matrix transposed = m.transpose();
+
+ System.out.println(">>> Matrix: ");
+ MatrixExampleUtil.print(m);
+ System.out.println(">>> Transposed matrix: ");
+ MatrixExampleUtil.print(transposed);
+
+ MatrixExampleUtil.verifyTransposition(m, transposed);
+
+ System.out.println("\n>>> Creating matrices to be multiplied.");
+ double[][] data1 = new double[][] {{1, 2}, {3, 4}};
+ double[][] data2 = new double[][] {{5, 6}, {7, 8}};
+
+ Matrix m1 = new SparseLocalOnHeapMatrix(data1.length, data1[0].length);
+ Matrix m2 = new SparseLocalOnHeapMatrix(data2.length, data2[0].length);
+
+ m1.assign(data1);
+ m2.assign(data2);
+
+ Matrix mult = m1.times(m2);
+
+ System.out.println(">>> First matrix: ");
+ MatrixExampleUtil.print(m1);
+ System.out.println(">>> Second matrix: ");
+ MatrixExampleUtil.print(m2);
+ System.out.println(">>> Matrix product: ");
+ MatrixExampleUtil.print(mult);
+
+ System.out.println("\n>>> Calculating matrices determinants.");
+ double det1 = m1.determinant();
+ double det2 = m2.determinant();
+ double detMult = mult.determinant();
+ boolean detMultIsAsExp = Math.abs(detMult - det1 * det2) < 0.0001d;
+
+ System.out.println(">>> First matrix determinant: [" + det1 + "].");
+ System.out.println(">>> Second matrix determinant: [" + det2 + "].");
+ System.out.println(">>> Matrix product determinant: [" + detMult
+ + "], equals product of two other matrices determinants: [" + detMultIsAsExp + "].");
+
+ System.out.println("Determinant of product matrix [" + detMult
+ + "] should be equal to product of determinants [" + (det1 * det2) + "].");
+
+ System.out.println("\n>>> Sparse matrix API usage example completed.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/package-info.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/package-info.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/package-info.java
new file mode 100644
index 0000000..354119a
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/matrix/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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 description. -->
+ * Core algebra matrix examples.
+ */
+package org.apache.ignite.examples.ml.math.matrix;
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/package-info.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/package-info.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/package-info.java
new file mode 100644
index 0000000..52344bf
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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 description. -->
+ * Core algebra examples.
+ */
+package org.apache.ignite.examples.ml.math;
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/TracerExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/TracerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/TracerExample.java
new file mode 100644
index 0000000..c14335a
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/TracerExample.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.tracer;
+
+import java.awt.Color;
+import java.io.IOException;
+import org.apache.ignite.ml.math.Tracer;
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
+
+/**
+ * Example of using {@link Tracer} utility API.
+ */
+public class TracerExample {
+ /**
+ * Double to color mapper example.
+ */
+ private static final Tracer.ColorMapper COLOR_MAPPER = d -> {
+ if (d <= 1.5)
+ return Color.RED;
+ else if (d <= 2.5)
+ return Color.GREEN;
+ else
+ return Color.BLUE;
+ };
+
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) throws IOException {
+ System.out.println(">>> Tracer utility example started.");
+
+ // Tracer is a simple utility class that allows pretty-printing of matrices/vectors.
+ DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] {
+ {1.12345, 2.12345},
+ {3.12345, 4.12345}
+ });
+
+ System.out.println("\n>>> Tracer output to console in ASCII.");
+ Tracer.showAscii(m, "%.3g");
+
+ System.out.println("\n>>> Tracer output to browser in HTML.");
+ Tracer.showHtml(m, COLOR_MAPPER, true);
+
+ System.out.println("\n>>> Tracer utility example completed.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/package-info.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/package-info.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/package-info.java
new file mode 100644
index 0000000..574e598
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/tracer/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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 description. -->
+ * Core algebra tracer example.
+ */
+package org.apache.ignite.examples.ml.math.tracer;
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java
new file mode 100644
index 0000000..4253ac1
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/CacheVectorExample.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.vector;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.ml.math.IdentityValueMapper;
+import org.apache.ignite.ml.math.distributed.ValueMapper;
+import org.apache.ignite.ml.math.distributed.VectorKeyMapper;
+import org.apache.ignite.ml.math.impls.vector.CacheVector;
+
+/**
+ * This example shows how to use {@link CacheVector} API.
+ * <p>
+ * Basically CacheVector is a view over existing data in cache. So we have {@link VectorKeyMapper} and
+ * {@link ValueMapper} for this purpose. A {@link VectorKeyMapper} allows us to map vector indices to cache keys.
+ * And a {@link ValueMapper} allows us map cache object to vector elements - doubles.</p>
+ * <p>
+ * In this example we use simple flat mapping for keys and {@link IdentityValueMapper} for cache
+ * objects because they are Doubles.</p>
+ */
+public class CacheVectorExample {
+ /** */
+ private static final String CACHE_NAME = CacheVectorExample.class.getSimpleName();
+
+ /** */
+ private static final int CARDINALITY = 10;
+
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ @SuppressWarnings("unchecked")
+ public static void main(String[] args) {
+ try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
+ System.out.println();
+ System.out.println(">>> CacheVector example started.");
+
+ CacheConfiguration<Integer, Double> cfg = new CacheConfiguration<>();
+
+ cfg.setName(CACHE_NAME);
+
+ try (IgniteCache<Integer, Double> cache = ignite.getOrCreateCache(cfg)) {
+ double[] testValues1 = {1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+ double[] testValues2 = {0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
+
+ ValueMapper valMapper = new IdentityValueMapper();
+
+ // Map vector element index to cache keys.
+ VectorKeyMapper<Integer> keyMapper1 = new VectorKeyMapper<Integer>() {
+ @Override public Integer apply(int i) {
+ return i;
+ }
+
+ @Override public boolean isValid(Integer integer) {
+ return integer >= 0 && CARDINALITY > integer;
+ }
+ };
+
+ // Map vector element index to cache keys with shift.
+ VectorKeyMapper<Integer> keyMapper2 = new VectorKeyMapper<Integer>() {
+ @Override public Integer apply(int i) {
+ return i + CARDINALITY;
+ }
+
+ @Override public boolean isValid(Integer integer) {
+ return integer >= 0 && CARDINALITY > integer;
+ }
+ };
+
+ // Create two cache vectors over one cache.
+ CacheVector cacheVector1 = new CacheVector(CARDINALITY, cache, keyMapper1, valMapper);
+ System.out.println(">>> First cache vector created.");
+
+ CacheVector cacheVector2 = new CacheVector(CARDINALITY, cache, keyMapper2, valMapper);
+ System.out.println(">>> Second cache vector created.");
+
+ cacheVector1.assign(testValues1);
+ cacheVector2.assign(testValues2);
+
+ // Dot product for orthogonal vectors is 0.0.
+ assert cacheVector1.dot(cacheVector2) == 0.0;
+
+ System.out.println(">>>");
+ System.out.println(">>> Finished executing Ignite \"CacheVector\" example.");
+ System.out.println(">>> Dot product is 0.0 for orthogonal vectors.");
+ System.out.println(">>>");
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/ExampleVectorStorage.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/ExampleVectorStorage.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/ExampleVectorStorage.java
new file mode 100644
index 0000000..0289a8c
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/ExampleVectorStorage.java
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.vector;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Arrays;
+
+import org.apache.ignite.ml.math.VectorStorage;
+import org.apache.ignite.ml.math.impls.storage.vector.ArrayVectorStorage;
+
+/**
+ * Example vector storage, modeled after {@link ArrayVectorStorage}.
+ */
+class ExampleVectorStorage implements VectorStorage {
+ /** */
+ private double[] data;
+
+ /**
+ * IMPL NOTE required by Externalizable.
+ */
+ public ExampleVectorStorage() {
+ // No-op.
+ }
+
+ /**
+ * @param data Backing data array.
+ */
+ ExampleVectorStorage(double[] data) {
+ assert data != null;
+
+ this.data = data;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int size() {
+ return data == null ? 0 : data.length;
+ }
+
+ /** {@inheritDoc} */
+ @Override public double get(int i) {
+ return data[i];
+ }
+
+ /** {@inheritDoc} */
+ @Override public void set(int i, double v) {
+ data[i] = v;
+ }
+
+ /** {@inheritDoc}} */
+ @Override public boolean isArrayBased() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public double[] data() {
+ return data;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isSequentialAccess() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isDense() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isRandomAccess() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isDistributed() {
+ return false;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(data);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ data = (double[])in.readObject();
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ int res = 1;
+
+ res = res * 37 + Arrays.hashCode(data);
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+
+ if (obj == null || getClass() != obj.getClass())
+ return false;
+
+ ExampleVectorStorage that = (ExampleVectorStorage)obj;
+
+ return Arrays.equals(data, (that.data));
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/OffHeapVectorExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/OffHeapVectorExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/OffHeapVectorExample.java
new file mode 100644
index 0000000..e38f62c
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/OffHeapVectorExample.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.vector;
+
+import java.util.Arrays;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVector;
+
+/**
+ * This example shows how to create and use off-heap versions of {@link Vector}.
+ */
+public final class OffHeapVectorExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Off-heap vector API usage example started.");
+
+ System.out.println("\n>>> Creating perpendicular off-heap vectors.");
+ double[] data1 = new double[] {1, 0, 3, 0, 5, 0};
+ double[] data2 = new double[] {0, 2, 0, 4, 0, 6};
+
+ Vector v1 = new DenseLocalOffHeapVector(data1.length);
+ Vector v2 = new DenseLocalOffHeapVector(data2.length);
+
+ v1.assign(data1);
+ v2.assign(data2);
+
+ System.out.println(">>> First vector: " + Arrays.toString(data1));
+ System.out.println(">>> Second vector: " + Arrays.toString(data2));
+
+ double dotProduct = v1.dot(v2);
+ boolean dotProductIsAsExp = dotProduct == 0;
+
+ System.out.println("\n>>> Dot product of vectors: [" + dotProduct
+ + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
+
+ assert dotProductIsAsExp : "Expect dot product of perpendicular vectors to be 0.";
+
+ Vector hypotenuse = v1.plus(v2);
+
+ System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
+
+ double lenSquared1 = v1.getLengthSquared();
+ double lenSquared2 = v2.getLengthSquared();
+ double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
+
+ boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
+
+ System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
+ System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
+ System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse
+ + "], equals sum of squared lengths of two original vectors as expected: ["
+ + lenSquaredHypotenuseIsAsExp + "].");
+
+ assert lenSquaredHypotenuseIsAsExp : "Expect squared length of hypotenuse to be as per Pythagorean theorem.";
+
+ System.out.println("\n>>> Off-heap vector API usage example completed.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/SparseVectorExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/SparseVectorExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/SparseVectorExample.java
new file mode 100644
index 0000000..be3ade1
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/SparseVectorExample.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.vector;
+
+import java.util.Arrays;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.impls.vector.SparseLocalVector;
+
+import static org.apache.ignite.ml.math.StorageConstants.RANDOM_ACCESS_MODE;
+
+/**
+ * This example shows how to use sparse {@link Vector} API.
+ */
+public final class SparseVectorExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Sparse vector API usage example started.");
+
+ System.out.println("\n>>> Creating perpendicular sparse vectors.");
+ double[] data1 = new double[] {1, 0, 3, 0, 5, 0};
+ double[] data2 = new double[] {0, 2, 0, 4, 0, 6};
+
+ Vector v1 = new SparseLocalVector(data1.length, RANDOM_ACCESS_MODE);
+ Vector v2 = new SparseLocalVector(data2.length, RANDOM_ACCESS_MODE);
+
+ v1.assign(data1);
+ v2.assign(data2);
+
+ System.out.println(">>> First vector: " + Arrays.toString(data1));
+ System.out.println(">>> Second vector: " + Arrays.toString(data2));
+
+ double dotProduct = v1.dot(v2);
+ boolean dotProductIsAsExp = dotProduct == 0;
+
+ System.out.println("\n>>> Dot product of vectors: [" + dotProduct
+ + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
+
+ Vector hypotenuse = v1.plus(v2);
+
+ System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
+
+ double lenSquared1 = v1.getLengthSquared();
+ double lenSquared2 = v2.getLengthSquared();
+ double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
+
+ boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
+
+ System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
+ System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
+ System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse
+ + "], equals sum of squared lengths of two original vectors as expected: ["
+ + lenSquaredHypotenuseIsAsExp + "].");
+
+ System.out.println("\n>>> Sparse vector API usage example completed.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorCustomStorageExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorCustomStorageExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorCustomStorageExample.java
new file mode 100644
index 0000000..cbb69d0
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorCustomStorageExample.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.vector;
+
+import java.util.Arrays;
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.VectorStorage;
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
+import org.apache.ignite.ml.math.impls.vector.AbstractVector;
+
+/**
+ * This example shows how to use {@link Vector} based on custom {@link VectorStorage}.
+ */
+public final class VectorCustomStorageExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Vector custom storage API usage example started.");
+
+ System.out.println("\n>>> Creating perpendicular vectors.");
+ double[] data1 = new double[] {1, 0, 3, 0, 5, 0};
+ double[] data2 = new double[] {0, 2, 0, 4, 0, 6};
+
+ Vector v1 = new VectorCustomStorage(data1);
+ Vector v2 = new VectorCustomStorage(data2);
+
+ System.out.println(">>> First vector: " + Arrays.toString(data1));
+ System.out.println(">>> Second vector: " + Arrays.toString(data2));
+
+ double dotProduct = v1.dot(v2);
+ boolean dotProductIsAsExp = dotProduct == 0;
+
+ System.out.println("\n>>> Dot product of vectors: [" + dotProduct
+ + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
+
+ Vector hypotenuse = v1.plus(v2);
+
+ System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
+
+ double lenSquared1 = v1.getLengthSquared();
+ double lenSquared2 = v2.getLengthSquared();
+ double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
+
+ boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
+
+ System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
+ System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
+ System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse
+ + "], equals sum of squared lengths of two original vectors as expected: ["
+ + lenSquaredHypotenuseIsAsExp + "].");
+
+ System.out.println("\n>>> Vector custom storage API usage example completed.");
+ }
+
+ /**
+ * Example of vector with custom storage, modeled after
+ * {@link org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector}.
+ */
+ static class VectorCustomStorage extends AbstractVector {
+ /**
+ * @param arr Source array.
+ * @param cp {@code true} to clone array, reuse it otherwise.
+ */
+ private VectorStorage mkStorage(double[] arr, boolean cp) {
+ assert arr != null;
+
+ return new ExampleVectorStorage(cp ? arr.clone() : arr);
+ }
+
+ /** */
+ public VectorCustomStorage() {
+ // No-op.
+ }
+
+ /**
+ * @param arr Source array.
+ * @param shallowCp {@code true} to use shallow copy.
+ */
+ VectorCustomStorage(double[] arr, boolean shallowCp) {
+ setStorage(mkStorage(arr, shallowCp));
+ }
+
+ /**
+ * @param arr Source array.
+ */
+ VectorCustomStorage(double[] arr) {
+ this(arr, false);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix likeMatrix(int rows, int cols) {
+ return new DenseLocalOnHeapMatrix(rows, cols);
+ }
+
+ /** {@inheritDoc */
+ @Override public Vector like(int crd) {
+ return new org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector(crd);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorExample.java
new file mode 100644
index 0000000..9970531
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/VectorExample.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.math.vector;
+
+import java.util.Arrays;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector;
+
+/**
+ * This example shows how to use {@link Vector} API.
+ *
+ * Just simple local onheap version.
+ */
+public final class VectorExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ System.out.println();
+ System.out.println(">>> Basic Vector API usage example started.");
+
+ System.out.println("\n>>> Creating perpendicular vectors.");
+ double[] data1 = new double[] {1, 0, 3, 0, 5, 0};
+ double[] data2 = new double[] {0, 2, 0, 4, 0, 6};
+
+ Vector v1 = new DenseLocalOnHeapVector(data1);
+ Vector v2 = new DenseLocalOnHeapVector(data2);
+
+ System.out.println(">>> First vector: " + Arrays.toString(data1));
+ System.out.println(">>> Second vector: " + Arrays.toString(data2));
+
+ double dotProduct = v1.dot(v2);
+ boolean dotProductIsAsExp = dotProduct == 0;
+
+ System.out.println("\n>>> Dot product of vectors: [" + dotProduct
+ + "], it is 0 as expected: [" + dotProductIsAsExp + "].");
+
+ Vector hypotenuse = v1.plus(v2);
+
+ System.out.println("\n>>> Hypotenuse (sum of vectors): " + Arrays.toString(hypotenuse.getStorage().data()));
+
+ double lenSquared1 = v1.getLengthSquared();
+ double lenSquared2 = v2.getLengthSquared();
+ double lenSquaredHypotenuse = hypotenuse.getLengthSquared();
+
+ boolean lenSquaredHypotenuseIsAsExp = lenSquaredHypotenuse == lenSquared1 + lenSquared2;
+
+ System.out.println(">>> Squared length of first vector: [" + lenSquared1 + "].");
+ System.out.println(">>> Squared length of second vector: [" + lenSquared2 + "].");
+ System.out.println(">>> Squared length of hypotenuse: [" + lenSquaredHypotenuse
+ + "], equals sum of squared lengths of two original vectors as expected: ["
+ + lenSquaredHypotenuseIsAsExp + "].");
+
+ System.out.println("\n>>> Basic Vector API usage example completed.");
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/package-info.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/package-info.java b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/package-info.java
new file mode 100644
index 0000000..2cc74ac
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/math/vector/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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 description. -->
+ * Core algebra vector examples.
+ */
+package org.apache.ignite.examples.ml.math.vector;
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPGroupTrainerExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPGroupTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPGroupTrainerExample.java
new file mode 100644
index 0000000..d106fad
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPGroupTrainerExample.java
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.nn;
+
+import java.util.Random;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteDataStreamer;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.examples.ExampleNodeStartup;
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.StorageConstants;
+import org.apache.ignite.ml.math.Tracer;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
+import org.apache.ignite.ml.nn.Activators;
+import org.apache.ignite.ml.nn.LabeledVectorsCache;
+import org.apache.ignite.ml.nn.MLPGroupUpdateTrainerCacheInput;
+import org.apache.ignite.ml.nn.MultilayerPerceptron;
+import org.apache.ignite.ml.nn.architecture.MLPArchitecture;
+import org.apache.ignite.ml.nn.initializers.RandomInitializer;
+import org.apache.ignite.ml.nn.trainers.distributed.MLPGroupUpdateTrainer;
+import org.apache.ignite.ml.optimization.updatecalculators.RPropParameterUpdate;
+import org.apache.ignite.ml.structures.LabeledVector;
+import org.apache.ignite.thread.IgniteThread;
+
+/**
+ * <p>
+ * Example of using distributed {@link MultilayerPerceptron}.</p>
+ * <p>
+ * Remote nodes should always be started with special configuration file which
+ * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}.</p>
+ * <p>
+ * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will start node
+ * with {@code examples/config/example-ignite.xml} configuration.</p>
+ */
+public class MLPGroupTrainerExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) throws InterruptedException {
+ // IMPL NOTE based on MLPGroupTrainerTest#testXOR
+ System.out.println(">>> Distributed multilayer perceptron example started.");
+
+ // Start ignite grid.
+ try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
+ System.out.println(">>> Ignite grid started.");
+
+ // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
+ // because we create ignite cache internally.
+ IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(),
+ MLPGroupTrainerExample.class.getSimpleName(), () -> {
+
+ int samplesCnt = 1000;
+
+ Matrix xorInputs = new DenseLocalOnHeapMatrix(
+ new double[][] {{0.0, 0.0}, {0.0, 1.0}, {1.0, 0.0}, {1.0, 1.0}},
+ StorageConstants.ROW_STORAGE_MODE).transpose();
+
+ Matrix xorOutputs = new DenseLocalOnHeapMatrix(
+ new double[][] {{0.0}, {1.0}, {1.0}, {0.0}},
+ StorageConstants.ROW_STORAGE_MODE).transpose();
+
+ MLPArchitecture conf = new MLPArchitecture(2).
+ withAddedLayer(10, true, Activators.RELU).
+ withAddedLayer(1, false, Activators.SIGMOID);
+
+ IgniteCache<Integer, LabeledVector<Vector, Vector>> cache = LabeledVectorsCache.createNew(ignite);
+ String cacheName = cache.getName();
+ Random rnd = new Random(12345L);
+
+ try (IgniteDataStreamer<Integer, LabeledVector<Vector, Vector>> streamer =
+ ignite.dataStreamer(cacheName)) {
+ streamer.perNodeBufferSize(10000);
+
+ for (int i = 0; i < samplesCnt; i++) {
+ int col = Math.abs(rnd.nextInt()) % 4;
+ streamer.addData(i, new LabeledVector<>(xorInputs.getCol(col), xorOutputs.getCol(col)));
+ }
+ }
+
+ int totalCnt = 100;
+ int failCnt = 0;
+ MLPGroupUpdateTrainer<RPropParameterUpdate> trainer = MLPGroupUpdateTrainer.getDefault(ignite).
+ withSyncRate(3).
+ withTolerance(0.001).
+ withMaxGlobalSteps(1000);
+
+ for (int i = 0; i < totalCnt; i++) {
+
+ MLPGroupUpdateTrainerCacheInput trainerInput = new MLPGroupUpdateTrainerCacheInput(conf,
+ new RandomInitializer(rnd), 6, cache, 4);
+
+ MultilayerPerceptron mlp = trainer.train(trainerInput);
+
+ Matrix predict = mlp.apply(xorInputs);
+
+ System.out.println(">>> Prediction data at step " + i + " of total " + totalCnt + ":");
+
+ Tracer.showAscii(predict);
+
+ System.out.println("Difference estimate: " + xorOutputs.getRow(0).minus(predict.getRow(0)).kNorm(2));
+
+ failCnt += closeEnough(xorOutputs.getRow(0), predict.getRow(0)) ? 0 : 1;
+ }
+
+ double failRatio = (double)failCnt / totalCnt;
+
+ System.out.println("\n>>> Fail percentage: " + (failRatio * 100) + "%.");
+
+ System.out.println("\n>>> Distributed multilayer perceptron example completed.");
+ });
+
+ igniteThread.start();
+
+ igniteThread.join();
+ }
+ }
+
+ /** */
+ private static boolean closeEnough(Vector v1, Vector v2) {
+ return v1.minus(v2).kNorm(2) < 5E-1;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPLocalTrainerExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPLocalTrainerExample.java b/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPLocalTrainerExample.java
new file mode 100644
index 0000000..b557458
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/nn/MLPLocalTrainerExample.java
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.nn;
+
+import java.util.Random;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.StorageConstants;
+import org.apache.ignite.ml.math.Tracer;
+import org.apache.ignite.ml.math.functions.IgniteSupplier;
+import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix;
+import org.apache.ignite.ml.nn.Activators;
+import org.apache.ignite.ml.trainers.local.LocalBatchTrainerInput;
+import org.apache.ignite.ml.optimization.LossFunctions;
+import org.apache.ignite.ml.nn.MultilayerPerceptron;
+import org.apache.ignite.ml.nn.architecture.MLPArchitecture;
+import org.apache.ignite.ml.nn.initializers.RandomInitializer;
+import org.apache.ignite.ml.nn.trainers.local.MLPLocalBatchTrainer;
+import org.apache.ignite.ml.optimization.updatecalculators.RPropUpdateCalculator;
+import org.apache.ignite.ml.util.Utils;
+
+/**
+ * Example of using local {@link MultilayerPerceptron}.
+ */
+public class MLPLocalTrainerExample {
+ /**
+ * Executes example.
+ *
+ * @param args Command line arguments, none required.
+ */
+ public static void main(String[] args) {
+ // IMPL NOTE based on MLPLocalTrainerTest#testXORRProp
+ System.out.println(">>> Local multilayer perceptron example started.");
+
+ Matrix xorInputs = new DenseLocalOnHeapMatrix(new double[][] {{0.0, 0.0}, {0.0, 1.0}, {1.0, 0.0}, {1.0, 1.0}},
+ StorageConstants.ROW_STORAGE_MODE).transpose();
+
+ System.out.println("\n>>> Input data:");
+
+ Tracer.showAscii(xorInputs);
+
+ Matrix xorOutputs = new DenseLocalOnHeapMatrix(new double[][] {{0.0}, {1.0}, {1.0}, {0.0}},
+ StorageConstants.ROW_STORAGE_MODE).transpose();
+
+ MLPArchitecture conf = new MLPArchitecture(2).
+ withAddedLayer(10, true, Activators.RELU).
+ withAddedLayer(1, false, Activators.SIGMOID);
+
+ SimpleMLPLocalBatchTrainerInput trainerInput = new SimpleMLPLocalBatchTrainerInput(conf,
+ new Random(1234L), xorInputs, xorOutputs, 4);
+
+ System.out.println("\n>>> Perform training.");
+
+ MultilayerPerceptron mlp = new MLPLocalBatchTrainer<>(LossFunctions.MSE,
+ () -> new RPropUpdateCalculator<>(),
+ 0.0001,
+ 16000).train(trainerInput);
+
+ System.out.println("\n>>> Apply model.");
+
+ Matrix predict = mlp.apply(xorInputs);
+
+ System.out.println("\n>>> Predicted data:");
+
+ Tracer.showAscii(predict);
+
+ System.out.println("\n>>> Reference expected data:");
+
+ Tracer.showAscii(xorOutputs);
+
+ System.out.println("\n>>> Difference estimate: " + xorOutputs.getRow(0).minus(predict.getRow(0)).kNorm(2));
+
+ System.out.println("\n>>> Local multilayer perceptron example completed.");
+ }
+
+ /**
+ * Class for local batch training of {@link MultilayerPerceptron}.
+ *
+ * It is constructed from two matrices: one containing inputs of function to approximate and other containing ground truth
+ * values of this function for corresponding inputs.
+ *
+ * We fix batch size given by this input by some constant value.
+ */
+ private static class SimpleMLPLocalBatchTrainerInput implements LocalBatchTrainerInput<MultilayerPerceptron> {
+ /**
+ * Multilayer perceptron to be trained.
+ */
+ private final MultilayerPerceptron mlp;
+
+ /**
+ * Inputs stored as columns.
+ */
+ private final Matrix inputs;
+
+ /**
+ * Ground truths stored as columns.
+ */
+ private final Matrix groundTruth;
+
+ /**
+ * Size of batch returned on each step.
+ */
+ private final int batchSize;
+
+ /**
+ * Construct instance of this class.
+ *
+ * @param arch Architecture of multilayer perceptron.
+ * @param rnd Random numbers generator.
+ * @param inputs Inputs stored as columns.
+ * @param groundTruth Ground truth stored as columns.
+ * @param batchSize Size of batch returned on each step.
+ */
+ SimpleMLPLocalBatchTrainerInput(MLPArchitecture arch, Random rnd, Matrix inputs, Matrix groundTruth, int batchSize) {
+ this.mlp = new MultilayerPerceptron(arch, new RandomInitializer(rnd));
+ this.inputs = inputs;
+ this.groundTruth = groundTruth;
+ this.batchSize = batchSize;
+ }
+
+ /** {@inheritDoc} */
+ @Override public IgniteSupplier<IgniteBiTuple<Matrix, Matrix>> batchSupplier() {
+ return () -> {
+ int inputRowSize = inputs.rowSize();
+ int outputRowSize = groundTruth.rowSize();
+
+ Matrix vectors = new DenseLocalOnHeapMatrix(inputRowSize, batchSize);
+ Matrix labels = new DenseLocalOnHeapMatrix(outputRowSize, batchSize);
+
+ int[] samples = Utils.selectKDistinct(inputs.columnSize(), batchSize);
+
+ for (int i = 0; i < batchSize; i++) {
+ vectors.assignColumn(i, inputs.getCol(samples[i]));
+ labels.assignColumn(i, groundTruth.getCol(samples[i]));
+ }
+
+ return new IgniteBiTuple<>(vectors, labels);
+ };
+ }
+
+ /** {@inheritDoc} */
+ @Override public MultilayerPerceptron mdl() {
+ return mlp;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/nn/package-info.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/nn/package-info.java b/examples/src/main/java/org/apache/ignite/examples/ml/nn/package-info.java
new file mode 100644
index 0000000..3a0ed29
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/nn/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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 description. -->
+ * ML neural network examples.
+ */
+package org.apache.ignite.examples.ml.nn;
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/package-info.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/package-info.java b/examples/src/main/java/org/apache/ignite/examples/ml/package-info.java
new file mode 100644
index 0000000..52778b5
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/package-info.java
@@ -0,0 +1,22 @@
+/*
+ * 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 description. -->
+ * Machine learning examples.
+ */
+package org.apache.ignite.examples.ml;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/43a62cdb/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/DistributedLinearRegressionExampleWithQRTrainer.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/DistributedLinearRegressionExampleWithQRTrainer.java b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/DistributedLinearRegressionExampleWithQRTrainer.java
new file mode 100644
index 0000000..98ff2a2
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/ml/regression/linear/DistributedLinearRegressionExampleWithQRTrainer.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.examples.ml.regression.linear;
+
+import java.util.Arrays;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.examples.ml.math.matrix.SparseDistributedMatrixExample;
+import org.apache.ignite.ml.Trainer;
+import org.apache.ignite.ml.math.Matrix;
+import org.apache.ignite.ml.math.Vector;
+import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix;
+import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector;
+import org.apache.ignite.ml.regressions.linear.LinearRegressionModel;
+import org.apache.ignite.ml.regressions.linear.LinearRegressionQRTrainer;
+import org.apache.ignite.thread.IgniteThread;
+
+/**
+ * Run linear regression model over distributed matrix.
+ *
+ * @see LinearRegressionQRTrainer
+ */
+public class DistributedLinearRegressionExampleWithQRTrainer {
+ /** */
+ private static final double[][] data = {
+ {8, 78, 284, 9.100000381, 109},
+ {9.300000191, 68, 433, 8.699999809, 144},
+ {7.5, 70, 739, 7.199999809, 113},
+ {8.899999619, 96, 1792, 8.899999619, 97},
+ {10.19999981, 74, 477, 8.300000191, 206},
+ {8.300000191, 111, 362, 10.89999962, 124},
+ {8.800000191, 77, 671, 10, 152},
+ {8.800000191, 168, 636, 9.100000381, 162},
+ {10.69999981, 82, 329, 8.699999809, 150},
+ {11.69999981, 89, 634, 7.599999905, 134},
+ {8.5, 149, 631, 10.80000019, 292},
+ {8.300000191, 60, 257, 9.5, 108},
+ {8.199999809, 96, 284, 8.800000191, 111},
+ {7.900000095, 83, 603, 9.5, 182},
+ {10.30000019, 130, 686, 8.699999809, 129},
+ {7.400000095, 145, 345, 11.19999981, 158},
+ {9.600000381, 112, 1357, 9.699999809, 186},
+ {9.300000191, 131, 544, 9.600000381, 177},
+ {10.60000038, 80, 205, 9.100000381, 127},
+ {9.699999809, 130, 1264, 9.199999809, 179},
+ {11.60000038, 140, 688, 8.300000191, 80},
+ {8.100000381, 154, 354, 8.399999619, 103},
+ {9.800000191, 118, 1632, 9.399999619, 101},
+ {7.400000095, 94, 348, 9.800000191, 117},
+ {9.399999619, 119, 370, 10.39999962, 88},
+ {11.19999981, 153, 648, 9.899999619, 78},
+ {9.100000381, 116, 366, 9.199999809, 102},
+ {10.5, 97, 540, 10.30000019, 95},
+ {11.89999962, 176, 680, 8.899999619, 80},
+ {8.399999619, 75, 345, 9.600000381, 92},
+ {5, 134, 525, 10.30000019, 126},
+ {9.800000191, 161, 870, 10.39999962, 108},
+ {9.800000191, 111, 669, 9.699999809, 77},
+ {10.80000019, 114, 452, 9.600000381, 60},
+ {10.10000038, 142, 430, 10.69999981, 71},
+ {10.89999962, 238, 822, 10.30000019, 86},
+ {9.199999809, 78, 190, 10.69999981, 93},
+ {8.300000191, 196, 867, 9.600000381, 106},
+ {7.300000191, 125, 969, 10.5, 162},
+ {9.399999619, 82, 499, 7.699999809, 95},
+ {9.399999619, 125, 925, 10.19999981, 91},
+ {9.800000191, 129, 353, 9.899999619, 52},
+ {3.599999905, 84, 288, 8.399999619, 110},
+ {8.399999619, 183, 718, 10.39999962, 69},
+ {10.80000019, 119, 540, 9.199999809, 57},
+ {10.10000038, 180, 668, 13, 106},
+ {9, 82, 347, 8.800000191, 40},
+ {10, 71, 345, 9.199999809, 50},
+ {11.30000019, 118, 463, 7.800000191, 35},
+ {11.30000019, 121, 728, 8.199999809, 86},
+ {12.80000019, 68, 383, 7.400000095, 57},
+ {10, 112, 316, 10.39999962, 57},
+ {6.699999809, 109, 388, 8.899999619, 94}
+ };
+
+ /** Run example. */
+ public static void main(String[] args) throws InterruptedException {
+ System.out.println();
+ System.out.println(">>> Linear regression model over sparse distributed matrix API usage example started.");
+ // Start ignite grid.
+ try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
+ System.out.println(">>> Ignite grid started.");
+ // Create IgniteThread, we must work with SparseDistributedMatrix inside IgniteThread
+ // because we create ignite cache internally.
+ IgniteThread igniteThread = new IgniteThread(ignite.configuration().getIgniteInstanceName(),
+ SparseDistributedMatrixExample.class.getSimpleName(), () -> {
+
+ // Create SparseDistributedMatrix, new cache will be created automagically.
+ System.out.println(">>> Create new SparseDistributedMatrix inside IgniteThread.");
+ SparseDistributedMatrix distributedMatrix = new SparseDistributedMatrix(data);
+
+ System.out.println(">>> Create new linear regression trainer object.");
+ Trainer<LinearRegressionModel, Matrix> trainer = new LinearRegressionQRTrainer();
+
+ System.out.println(">>> Perform the training to get the model.");
+ LinearRegressionModel model = trainer.train(distributedMatrix);
+ System.out.println(">>> Linear regression model: " + model);
+
+ System.out.println(">>> ---------------------------------");
+ System.out.println(">>> | Prediction\t| Ground Truth\t|");
+ System.out.println(">>> ---------------------------------");
+ for (double[] observation : data) {
+ Vector inputs = new SparseDistributedVector(Arrays.copyOfRange(observation, 1, observation.length));
+ double prediction = model.apply(inputs);
+ double groundTruth = observation[0];
+ System.out.printf(">>> | %.4f\t\t| %.4f\t\t|\n", prediction, groundTruth);
+ }
+ System.out.println(">>> ---------------------------------");
+ });
+
+ igniteThread.start();
+
+ igniteThread.join();
+ }
+ }
+}