You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ch...@apache.org on 2018/07/19 22:47:57 UTC
[11/16] ignite git commit: IGNITE-9021: [ML] Refactor vectors to
dence/sparse
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorView.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorView.java
deleted file mode 100644
index 3becbcf..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/MatrixVectorView.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.ml.math.impls.vector;
-
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import org.apache.ignite.ml.math.Matrix;
-import org.apache.ignite.ml.math.Vector;
-import org.apache.ignite.ml.math.exceptions.IndexException;
-import org.apache.ignite.ml.math.impls.storage.vector.MatrixVectorStorage;
-
-/**
- * Row or column vector view off the matrix.
- */
-public class MatrixVectorView extends AbstractVector {
- /** */
- private Matrix parent;
-
- /** */
- private int row;
- /** */
- private int col;
-
- /** */
- private int rowStride;
- /** */
- private int colStride;
-
- /**
- *
- */
- public MatrixVectorView() {
- // No-op.
- }
-
- /**
- * @param parent Parent matrix.
- * @param row Starting row in the view.
- * @param col Starting column in the view.
- * @param rowStride Rows stride in the view.
- * @param colStride Columns stride in the view.
- */
- public MatrixVectorView(Matrix parent, int row, int col, int rowStride, int colStride) {
- assert parent != null;
-
- if (row < 0 || row >= parent.rowSize())
- throw new IndexException(row);
- if (col < 0 || col >= parent.columnSize())
- throw new IndexException(col);
-
- this.parent = parent;
-
- this.row = row;
- this.col = col;
-
- this.rowStride = rowStride;
- this.colStride = colStride;
-
- setStorage(new MatrixVectorStorage(parent, row, col, rowStride, colStride));
- }
-
- /** {@inheritDoc} */
- @Override public Vector copy() {
- return new MatrixVectorView(parent, row, col, rowStride, colStride);
- }
-
- /** {@inheritDoc} */
- @Override public Vector like(int crd) {
- return parent.likeVector(crd);
- }
-
- /** {@inheritDoc} */
- @Override public Matrix likeMatrix(int rows, int cols) {
- return parent.like(rows, cols);
- }
-
- /** {@inheritDoc} */
- @Override public void writeExternal(ObjectOutput out) throws IOException {
- super.writeExternal(out);
-
- out.writeObject(parent);
- out.writeInt(row);
- out.writeInt(col);
- out.writeInt(rowStride);
- out.writeInt(colStride);
- }
-
- /** {@inheritDoc} */
- @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- super.readExternal(in);
-
- parent = (Matrix)in.readObject();
- row = in.readInt();
- col = in.readInt();
- rowStride = in.readInt();
- colStride = in.readInt();
- }
-
- /** {@inheritDoc} */
- @Override public int hashCode() {
- int res = 1;
-
- res = res * 37 + (parent == null ? 0 : parent.hashCode());
- res = res * 37 + row;
- res = res * 37 + col;
- res = res * 37 + rowStride;
- res = res * 37 + colStride;
-
- return res;
- }
-
- /** {@inheritDoc} */
- @Override public boolean equals(Object o) {
- if (this == o)
- return true;
-
- if (o == null || getClass() != o.getClass())
- return false;
-
- MatrixVectorView that = (MatrixVectorView)o;
-
- return (parent != null ? parent.equals(that.parent) : that.parent == null) &&
- row == that.row &&
- col == that.col &&
- rowStride == that.rowStride &&
- colStride == that.colStride;
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalOffHeapVector.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalOffHeapVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalOffHeapVector.java
deleted file mode 100644
index fa216ff..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalOffHeapVector.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.ml.math.impls.vector;
-
-import org.apache.ignite.ml.math.Matrix;
-import org.apache.ignite.ml.math.Vector;
-import org.apache.ignite.ml.math.impls.storage.vector.SparseLocalOffHeapVectorStorage;
-
-/**
- * Implementation for {@link Vector} assuming sparse logic and local offheap JVM storage.
- * It is suitable for data sets where local, non-distributed execution is satisfactory and on-heap JVM storage
- * is not enough to keep the entire data set.
- * <p>See also: <a href="https://en.wikipedia.org/wiki/Sparse_array">Wikipedia article</a>.</p>
- */
-public class SparseLocalOffHeapVector extends AbstractVector {
- /**
- * @param crd Vector cardinality.
- */
- public SparseLocalOffHeapVector(int crd) {
- setStorage(new SparseLocalOffHeapVectorStorage(crd));
- }
-
- /** {@inheritDoc} */
- @Override public Vector like(int crd) {
- return new SparseLocalOffHeapVector(crd);
- }
-
- /** {@inheritDoc} */
- @Override public Matrix likeMatrix(int rows, int cols) {
- return null;
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalVector.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalVector.java
deleted file mode 100644
index bc1e59d..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseLocalVector.java
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.ml.math.impls.vector;
-
-import it.unimi.dsi.fastutil.ints.IntSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.Spliterator;
-import java.util.function.Consumer;
-import org.apache.ignite.ml.math.Matrix;
-import org.apache.ignite.ml.math.StorageConstants;
-import org.apache.ignite.ml.math.Vector;
-import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix;
-import org.apache.ignite.ml.math.impls.storage.vector.SparseLocalOnHeapVectorStorage;
-
-/**
- * Local on-heap sparse vector based on hash map storage.
- */
-public class SparseLocalVector extends AbstractVector implements StorageConstants {
- /**
- *
- */
- public SparseLocalVector() {
- // No-op.
- }
-
- /**
- * @param map Underlying map.
- * @param cp Should given map be copied.
- */
- public SparseLocalVector(Map<Integer, Double> map, boolean cp) {
- setStorage(new SparseLocalOnHeapVectorStorage(map, cp));
- }
-
- /**
- * @param size Vector size.
- * @param acsMode Vector elements access mode.
- */
- public SparseLocalVector(int size, int acsMode) {
- assertAccessMode(acsMode);
-
- setStorage(new SparseLocalOnHeapVectorStorage(size, acsMode));
- }
-
- /** */
- private SparseLocalOnHeapVectorStorage storage() {
- return (SparseLocalOnHeapVectorStorage)getStorage();
- }
-
- /** {@inheritDoc} */
- @Override public Vector like(int crd) {
- SparseLocalOnHeapVectorStorage sto = storage();
-
- return new SparseLocalVector(crd, sto.getAccessMode());
- }
-
- /** {@inheritDoc} */
- @Override public Matrix likeMatrix(int rows, int cols) {
- return new SparseLocalOnHeapMatrix(rows, cols);
- }
-
- /** {@inheritDoc} */
- @Override public Vector times(double x) {
- if (x == 0.0)
- return assign(0);
- else
- return super.times(x);
- }
-
- /** Indexes of non-default elements. */
- public IntSet indexes() {
- return storage().indexes();
- }
-
- /** {@inheritDoc} */
- @Override public Spliterator<Double> nonZeroSpliterator() {
- return new Spliterator<Double>() {
- /** {@inheritDoc} */
- @Override public boolean tryAdvance(Consumer<? super Double> act) {
- Set<Integer> indexes = storage().indexes();
-
- for (Integer index : indexes)
- act.accept(storageGet(index));
-
- return true;
- }
-
- /** {@inheritDoc} */
- @Override public Spliterator<Double> trySplit() {
- return null; // No Splitting.
- }
-
- /** {@inheritDoc} */
- @Override public long estimateSize() {
- return storage().indexes().size();
- }
-
- /** {@inheritDoc} */
- @Override public int characteristics() {
- return ORDERED | SIZED;
- }
- };
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorView.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorView.java
deleted file mode 100644
index 756e5cc..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorView.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.ml.math.impls.vector;
-
-import java.io.Externalizable;
-import org.apache.ignite.ml.math.Matrix;
-import org.apache.ignite.ml.math.Vector;
-import org.apache.ignite.ml.math.VectorStorage;
-import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException;
-import org.apache.ignite.ml.math.impls.storage.vector.DelegateVectorStorage;
-
-/**
- * Implements the partial view into the parent {@link Vector}.
- */
-public class VectorView extends AbstractVector {
- /**
- * Constructor for {@link Externalizable} interface.
- */
- public VectorView() {
- // No-op.
- }
-
- /**
- * @param parent Backing parent {@link Vector}.
- * @param off Offset to parent vector.
- * @param len Size of the view.
- */
- public VectorView(Vector parent, int off, int len) {
- super(new DelegateVectorStorage(parent.getStorage(), off, len));
- }
-
- /**
- * @param sto Backing parent {@link VectorStorage}.
- * @param off Offset to parent vector.
- * @param len Size of the view.
- */
- public VectorView(VectorStorage sto, int off, int len) {
- super(new DelegateVectorStorage(sto, off, len));
- }
-
- /** */
- private DelegateVectorStorage storage() {
- return (DelegateVectorStorage)getStorage();
- }
-
- /** {@inheritDoc} */
- @Override public Vector copy() {
- // TODO: IGNITE-5723, revise this
- DelegateVectorStorage sto = storage();
-
- return new VectorView(sto.delegate(), sto.offset(), sto.length());
- }
-
- /** {@inheritDoc} */
- @Override public Vector like(int crd) {
- throw new UnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override public Matrix likeMatrix(int rows, int cols) {
- throw new UnsupportedOperationException();
- }
-
- /** {@inheritDoc} */
- @Override public boolean equals(Object o) {
- return this == o ||
- ((o != null)
- && o.getClass() == getClass()
- && (getStorage().equals(((VectorView)o).getStorage())));
- }
-}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/package-info.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/package-info.java
deleted file mode 100644
index b7f485c..0000000
--- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/package-info.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * <!-- Package description. -->
- * Contains specific implementations for vectors.
- */
-package org.apache.ignite.ml.math.impls.vector;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/package-info.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/package-info.java
index 5887d4b..ff9aca5 100644
--- a/modules/ml/src/main/java/org/apache/ignite/ml/math/package-info.java
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/package-info.java
@@ -17,6 +17,6 @@
/**
* <!-- Package description. -->
- * Contains main APIs for distributed code algebra.
+ * Contains main APIs for matrix/vector algebra.
*/
-package org.apache.ignite.ml.math;
\ No newline at end of file
+package org.apache.ignite.ml.math;
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/AbstractMatrix.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/AbstractMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/AbstractMatrix.java
new file mode 100644
index 0000000..bed32de
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/AbstractMatrix.java
@@ -0,0 +1,921 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.Spliterator;
+import java.util.function.Consumer;
+import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.ml.math.Blas;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.math.exceptions.CardinalityException;
+import org.apache.ignite.ml.math.exceptions.ColumnIndexException;
+import org.apache.ignite.ml.math.exceptions.RowIndexException;
+import org.apache.ignite.ml.math.functions.Functions;
+import org.apache.ignite.ml.math.functions.IgniteBiFunction;
+import org.apache.ignite.ml.math.functions.IgniteDoubleFunction;
+import org.apache.ignite.ml.math.functions.IgniteFunction;
+import org.apache.ignite.ml.math.functions.IgniteTriFunction;
+import org.apache.ignite.ml.math.functions.IntIntToDoubleFunction;
+import org.apache.ignite.ml.math.primitives.vector.impl.DenseVector;
+import org.apache.ignite.ml.math.primitives.vector.impl.VectorizedViewMatrix;
+import org.apache.ignite.ml.math.util.MatrixUtil;
+
+/**
+ * This class provides a helper implementation of the {@link Matrix}
+ * interface to minimize the effort required to implement it.
+ * Subclasses may override some of the implemented methods if a more
+ * specific or optimized implementation is desirable.
+ */
+public abstract class AbstractMatrix implements Matrix {
+ // Stochastic sparsity analysis.
+ /** */
+ private static final double Z95 = 1.959964;
+ /** */
+ private static final double Z80 = 1.281552;
+ /** */
+ private static final int MAX_SAMPLES = 500;
+ /** */
+ private static final int MIN_SAMPLES = 15;
+
+ /** Cached minimum element. */
+ private Element minElm;
+ /** Cached maximum element. */
+ private Element maxElm = null;
+
+ /** Matrix storage implementation. */
+ private MatrixStorage sto;
+
+ /** Meta attributes storage. */
+ private Map<String, Object> meta = new HashMap<>();
+
+ /** Matrix's GUID. */
+ private IgniteUuid guid = IgniteUuid.randomUuid();
+
+ /**
+ * @param sto Backing {@link MatrixStorage}.
+ */
+ public AbstractMatrix(MatrixStorage sto) {
+ this.sto = sto;
+ }
+
+ /**
+ *
+ */
+ public AbstractMatrix() {
+ // No-op.
+ }
+
+ /**
+ * @param sto Backing {@link MatrixStorage}.
+ */
+ protected void setStorage(MatrixStorage sto) {
+ assert sto != null;
+
+ this.sto = sto;
+ }
+
+ /**
+ * @param row Row index in the matrix.
+ * @param col Column index in the matrix.
+ * @param v Value to set.
+ */
+ protected void storageSet(int row, int col, double v) {
+ sto.set(row, col, v);
+
+ // Reset cached values.
+ minElm = maxElm = null;
+ }
+
+ /**
+ * @param row Row index in the matrix.
+ * @param col Column index in the matrix.
+ */
+ protected double storageGet(int row, int col) {
+ return sto.get(row, col);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Element maxElement() {
+ if (maxElm == null) {
+ double max = Double.NEGATIVE_INFINITY;
+ int row = 0, col = 0;
+
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++) {
+ double d = storageGet(x, y);
+
+ if (d > max) {
+ max = d;
+ row = x;
+ col = y;
+ }
+ }
+
+ maxElm = mkElement(row, col);
+ }
+
+ return maxElm;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Element minElement() {
+ if (minElm == null) {
+ double min = Double.MAX_VALUE;
+ int row = 0, col = 0;
+
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++) {
+ double d = storageGet(x, y);
+
+ if (d < min) {
+ min = d;
+ row = x;
+ col = y;
+ }
+ }
+
+ minElm = mkElement(row, col);
+ }
+
+ return minElm;
+ }
+
+ /** {@inheritDoc} */
+ @Override public double maxValue() {
+ return maxElement().get();
+ }
+
+ /** {@inheritDoc} */
+ @Override public double minValue() {
+ return minElement().get();
+ }
+
+ /**
+ * @param row Row index in the matrix.
+ * @param col Column index in the matrix.
+ */
+ private Element mkElement(int row, int col) {
+ return new Element() {
+ /** {@inheritDoc} */
+ @Override public double get() {
+ return storageGet(row, col);
+ }
+
+ /** {@inheritDoc} */
+ @Override public int row() {
+ return row;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int column() {
+ return col;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void set(double d) {
+ storageSet(row, col, d);
+ }
+ };
+ }
+
+ /** {@inheritDoc} */
+ @Override public Element getElement(int row, int col) {
+ return mkElement(row, col);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix swapRows(int row1, int row2) {
+ checkRowIndex(row1);
+ checkRowIndex(row2);
+
+ int cols = columnSize();
+
+ for (int y = 0; y < cols; y++) {
+ double v = getX(row1, y);
+
+ setX(row1, y, getX(row2, y));
+ setX(row2, y, v);
+ }
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix swapColumns(int col1, int col2) {
+ checkColumnIndex(col1);
+ checkColumnIndex(col2);
+
+ int rows = rowSize();
+
+ for (int x = 0; x < rows; x++) {
+ double v = getX(x, col1);
+
+ setX(x, col1, getX(x, col2));
+ setX(x, col2, v);
+ }
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public MatrixStorage getStorage() {
+ return sto;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isSequentialAccess() {
+ return sto.isSequentialAccess();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isDense() {
+ return sto.isDense();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isRandomAccess() {
+ return sto.isRandomAccess();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isDistributed() {
+ return sto.isDistributed();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isArrayBased() {
+ return sto.isArrayBased();
+ }
+
+ /**
+ * Check row index bounds.
+ *
+ * @param row Row index.
+ */
+ private void checkRowIndex(int row) {
+ if (row < 0 || row >= rowSize())
+ throw new RowIndexException(row);
+ }
+
+ /**
+ * Check column index bounds.
+ *
+ * @param col Column index.
+ */
+ private void checkColumnIndex(int col) {
+ if (col < 0 || col >= columnSize())
+ throw new ColumnIndexException(col);
+ }
+
+ /**
+ * Check column and row index bounds.
+ *
+ * @param row Row index.
+ * @param col Column index.
+ */
+ private void checkIndex(int row, int col) {
+ checkRowIndex(row);
+ checkColumnIndex(col);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(sto);
+ out.writeObject(meta);
+ out.writeObject(guid);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Map<String, Object> getMetaStorage() {
+ return meta;
+ }
+
+ /** {@inheritDoc} */
+ @SuppressWarnings("unchecked")
+ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ sto = (MatrixStorage)in.readObject();
+ meta = (Map<String, Object>)in.readObject();
+ guid = (IgniteUuid)in.readObject();
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix assign(double val) {
+ if (sto.isArrayBased())
+ Arrays.fill(sto.data(), val);
+ else {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ storageSet(x, y, val);
+ }
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix assign(IntIntToDoubleFunction fun) {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ storageSet(x, y, fun.apply(x, y));
+
+ return this;
+ }
+
+ /** */
+ private void checkCardinality(Matrix mtx) {
+ checkCardinality(mtx.rowSize(), mtx.columnSize());
+ }
+
+ /** */
+ private void checkCardinality(int rows, int cols) {
+ if (rows != rowSize())
+ throw new CardinalityException(rowSize(), rows);
+
+ if (cols != columnSize())
+ throw new CardinalityException(columnSize(), cols);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix assign(double[][] vals) {
+ checkCardinality(vals.length, vals[0].length);
+
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ storageSet(x, y, vals[x][y]);
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix assign(Matrix mtx) {
+ checkCardinality(mtx);
+
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ storageSet(x, y, mtx.getX(x, y));
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix map(IgniteDoubleFunction<Double> fun) {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ storageSet(x, y, fun.apply(storageGet(x, y)));
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix map(Matrix mtx, IgniteBiFunction<Double, Double, Double> fun) {
+ checkCardinality(mtx);
+
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ storageSet(x, y, fun.apply(storageGet(x, y), mtx.getX(x, y)));
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Spliterator<Double> allSpliterator() {
+ return new Spliterator<Double>() {
+ /** {@inheritDoc} */
+ @Override public boolean tryAdvance(Consumer<? super Double> act) {
+ int rLen = rowSize();
+ int cLen = columnSize();
+
+ for (int i = 0; i < rLen; i++)
+ for (int j = 0; j < cLen; j++)
+ act.accept(storageGet(i, j));
+
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Spliterator<Double> trySplit() {
+ return null; // No Splitting.
+ }
+
+ /** {@inheritDoc} */
+ @Override public long estimateSize() {
+ return rowSize() * columnSize();
+ }
+
+ /** {@inheritDoc} */
+ @Override public int characteristics() {
+ return ORDERED | SIZED;
+ }
+ };
+ }
+
+ /** {@inheritDoc} */
+ @Override public int nonZeroElements() {
+ int cnt = 0;
+
+ for (int i = 0; i < rowSize(); i++)
+ for (int j = 0; j < rowSize(); j++)
+ if (get(i, j) != 0.0)
+ cnt++;
+
+ return cnt;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Spliterator<Double> nonZeroSpliterator() {
+ return new Spliterator<Double>() {
+ /** {@inheritDoc} */
+ @Override public boolean tryAdvance(Consumer<? super Double> act) {
+ int rLen = rowSize();
+ int cLen = columnSize();
+
+ for (int i = 0; i < rLen; i++)
+ for (int j = 0; j < cLen; j++) {
+ double val = storageGet(i, j);
+
+ if (val != 0.0)
+ act.accept(val);
+ }
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Spliterator<Double> trySplit() {
+ return null; // No Splitting.
+ }
+
+ /** {@inheritDoc} */
+ @Override public long estimateSize() {
+ return nonZeroElements();
+ }
+
+ /** {@inheritDoc} */
+ @Override public int characteristics() {
+ return ORDERED | SIZED;
+ }
+ };
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix assignColumn(int col, Vector vec) {
+ checkColumnIndex(col);
+
+ int rows = rowSize();
+
+ for (int x = 0; x < rows; x++)
+ storageSet(x, col, vec.getX(x));
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix assignRow(int row, Vector vec) {
+ checkRowIndex(row);
+
+ int cols = columnSize();
+
+ if (cols != vec.size())
+ throw new CardinalityException(cols, vec.size());
+
+ // TODO: IGNITE-5777, use Blas for this.
+ for (int y = 0; y < cols; y++)
+ storageSet(row, y, vec.getX(y));
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector foldRows(IgniteFunction<Vector, Double> fun) {
+ int rows = rowSize();
+
+ Vector vec = likeVector(rows);
+
+ for (int i = 0; i < rows; i++)
+ vec.setX(i, fun.apply(viewRow(i)));
+
+ return vec;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector foldColumns(IgniteFunction<Vector, Double> fun) {
+ int cols = columnSize();
+
+ Vector vec = likeVector(cols);
+
+ for (int i = 0; i < cols; i++)
+ vec.setX(i, fun.apply(viewColumn(i)));
+
+ return vec;
+ }
+
+ /** {@inheritDoc} */
+ @Override public <T> T foldMap(IgniteBiFunction<T, Double, T> foldFun, IgniteDoubleFunction<Double> mapFun,
+ T zeroVal) {
+ T res = zeroVal;
+
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ res = foldFun.apply(res, mapFun.apply(storageGet(x, y)));
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int columnSize() {
+ return sto.columnSize();
+ }
+
+ /** {@inheritDoc} */
+ @Override public int rowSize() {
+ return sto.rowSize();
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix divide(double d) {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ setX(x, y, getX(x, y) / d);
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public double get(int row, int col) {
+ checkIndex(row, col);
+
+ return storageGet(row, col);
+ }
+
+ /** {@inheritDoc} */
+ @Override public double getX(int row, int col) {
+ return storageGet(row, col);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix minus(Matrix mtx) {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ checkCardinality(rows, cols);
+
+ Matrix res = like(rows, cols);
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ res.setX(x, y, getX(x, y) - mtx.getX(x, y));
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix plus(double x) {
+ Matrix cp = copy();
+
+ cp.map(Functions.plus(x));
+
+ return cp;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix plus(Matrix mtx) {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ checkCardinality(rows, cols);
+
+ Matrix res = like(rows, cols);
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ res.setX(x, y, getX(x, y) + mtx.getX(x, y));
+
+ return res;
+
+ }
+
+ /** {@inheritDoc} */
+ @Override public IgniteUuid guid() {
+ return guid;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix set(int row, int col, double val) {
+ checkIndex(row, col);
+
+ storageSet(row, col, val);
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix setRow(int row, double[] data) {
+ checkRowIndex(row);
+
+ int cols = columnSize();
+
+ if (cols != data.length)
+ throw new CardinalityException(cols, data.length);
+ // TODO: IGNITE-5777, use Blas for this.
+ for (int y = 0; y < cols; y++)
+ setX(row, y, data[y]);
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector getRow(int row) {
+ checkRowIndex(row);
+
+ Vector res = new DenseVector(columnSize());
+
+ for (int i = 0; i < columnSize(); i++)
+ res.setX(i, getX(row, i));
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix setColumn(int col, double[] data) {
+ checkColumnIndex(col);
+
+ int rows = rowSize();
+
+ if (rows != data.length)
+ throw new CardinalityException(rows, data.length);
+
+ for (int x = 0; x < rows; x++)
+ setX(x, col, data[x]);
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector getCol(int col) {
+ checkColumnIndex(col);
+
+ Vector res;
+
+ if (isDistributed())
+ res = MatrixUtil.likeVector(this, rowSize());
+ else
+ res = new DenseVector(rowSize());
+
+ for (int i = 0; i < rowSize(); i++)
+ res.setX(i, getX(i, col));
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix setX(int row, int col, double val) {
+ storageSet(row, col, val);
+
+ return this;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix times(double x) {
+ Matrix cp = copy();
+
+ cp.map(Functions.mult(x));
+
+ return cp;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector times(Vector vec) {
+ int cols = columnSize();
+
+ if (cols != vec.size())
+ throw new CardinalityException(cols, vec.size());
+
+ int rows = rowSize();
+
+ Vector res = likeVector(rows);
+
+ Blas.gemv(1, this, vec, 0, res);
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix times(Matrix mtx) {
+ int cols = columnSize();
+
+ if (cols != mtx.rowSize())
+ throw new CardinalityException(cols, mtx.rowSize());
+
+ Matrix res = like(rowSize(), mtx.columnSize());
+
+ Blas.gemm(1, this, mtx, 0, res);
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public double sum() {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ double sum = 0.0;
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ sum += getX(x, y);
+
+ return sum;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix transpose() {
+ int rows = rowSize();
+ int cols = columnSize();
+
+ Matrix mtx = like(cols, rows);
+
+ for (int x = 0; x < rows; x++)
+ for (int y = 0; y < cols; y++)
+ mtx.setX(y, x, getX(x, y));
+
+ return mtx;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean density(double threshold) {
+ assert threshold >= 0.0 && threshold <= 1.0;
+
+ int n = MIN_SAMPLES;
+ int rows = rowSize();
+ int cols = columnSize();
+
+ double mean = 0.0;
+ double pq = threshold * (1 - threshold);
+
+ Random rnd = new Random();
+
+ for (int i = 0; i < MIN_SAMPLES; i++)
+ if (getX(rnd.nextInt(rows), rnd.nextInt(cols)) != 0.0)
+ mean++;
+
+ mean /= MIN_SAMPLES;
+
+ double iv = Z80 * Math.sqrt(pq / n);
+
+ if (mean < threshold - iv)
+ return false; // Sparse.
+ else if (mean > threshold + iv)
+ return true; // Dense.
+
+ while (n < MAX_SAMPLES) {
+ // Determine upper bound we may need for 'n' to likely relinquish the uncertainty.
+ // Here, we use confidence interval formula but solved for 'n'.
+ double ivX = Math.max(Math.abs(threshold - mean), 1e-11);
+
+ double stdErr = ivX / Z80;
+ double nX = Math.min(Math.max((int)Math.ceil(pq / (stdErr * stdErr)), n), MAX_SAMPLES) - n;
+
+ if (nX < 1.0) // IMPL NOTE this can happen with threshold 1.0
+ nX = 1.0;
+
+ double meanNext = 0.0;
+
+ for (int i = 0; i < nX; i++)
+ if (getX(rnd.nextInt(rows), rnd.nextInt(cols)) != 0.0)
+ meanNext++;
+
+ mean = (n * mean + meanNext) / (n + nX);
+
+ n += nX;
+
+ // Are we good now?
+ iv = Z80 * Math.sqrt(pq / n);
+
+ if (mean < threshold - iv)
+ return false; // Sparse.
+ else if (mean > threshold + iv)
+ return true; // Dense.
+ }
+
+ return mean > threshold; // Dense if mean > threshold.
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector viewRow(int row) {
+ return new VectorizedViewMatrix(this, row, 0, 0, 1);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector viewColumn(int col) {
+ return new VectorizedViewMatrix(this, 0, col, 1, 0);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector viewDiagonal() {
+ return new VectorizedViewMatrix(this, 0, 0, 1, 1);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void destroy() {
+ getStorage().destroy();
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix copy() {
+ Matrix cp = like(rowSize(), columnSize());
+
+ cp.assign(this);
+
+ return cp;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ int res = 1;
+
+ res = res * 37 + guid.hashCode();
+ res = res * 37 + sto.hashCode();
+ res = res * 37 + meta.hashCode();
+
+ return res;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * We ignore guid's for comparisons.
+ */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ AbstractMatrix that = (AbstractMatrix)o;
+
+ MatrixStorage sto = getStorage();
+
+ return (sto != null ? sto.equals(that.getStorage()) : that.getStorage() == null);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void compute(int row, int col, IgniteTriFunction<Integer, Integer, Double, Double> f) {
+ setX(row, col, f.apply(row, col, getX(row, col)));
+ }
+
+ /** {@inheritDoc} */
+ @Override public String toString() {
+ return "Matrix [rows=" + rowSize() + ", cols=" + columnSize() + "]";
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/Matrix.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/Matrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/Matrix.java
new file mode 100644
index 0000000..52b2dc1
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/Matrix.java
@@ -0,0 +1,523 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix;
+
+import java.io.Externalizable;
+import java.util.Spliterator;
+import org.apache.ignite.lang.IgniteUuid;
+import org.apache.ignite.ml.math.Destroyable;
+import org.apache.ignite.ml.math.MetaAttributes;
+import org.apache.ignite.ml.math.StorageOpsMetrics;
+import org.apache.ignite.ml.math.exceptions.CardinalityException;
+import org.apache.ignite.ml.math.exceptions.IndexException;
+import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException;
+import org.apache.ignite.ml.math.functions.IgniteBiFunction;
+import org.apache.ignite.ml.math.functions.IgniteDoubleFunction;
+import org.apache.ignite.ml.math.functions.IgniteFunction;
+import org.apache.ignite.ml.math.functions.IgniteTriFunction;
+import org.apache.ignite.ml.math.functions.IntIntToDoubleFunction;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+
+/**
+ * A matrix interface.
+ * <p>
+ * Based on its flavor it can have vastly different implementations tailored for
+ * for different types of data (e.g. dense vs. sparse), different sizes of data or different operation
+ * optimizations.</p>
+ * <p>
+ * Note also that not all operations can be supported by all underlying implementations. If an operation is not
+ * supported a {@link UnsupportedOperationException} is thrown. This exception can also be thrown in partial cases
+ * where an operation is unsupported only in special cases, e.g. where a given operation cannot be deterministically
+ * completed in polynomial time.</p>
+ * <p>
+ * Based on ideas from <a href="http://mahout.apache.org/">Apache Mahout</a>.</p>
+ */
+public interface Matrix extends MetaAttributes, Externalizable, StorageOpsMetrics, Destroyable {
+ /**
+ * Holder for matrix's element.
+ */
+ interface Element {
+ /**
+ * Gets element's value.
+ *
+ * @return The value of this matrix element.
+ */
+ double get();
+
+ /**
+ * Gets element's row index.
+ *
+ * @return The row index of this element.
+ */
+ int row();
+
+ /**
+ * Gets element's column index.
+ *
+ * @return The column index of this element.
+ */
+ int column();
+
+ /**
+ * Sets element's value.
+ *
+ * @param val Value to set.
+ */
+ void set(double val);
+ }
+
+ /**
+ * Gets the maximum value in this matrix.
+ *
+ * @return Maximum value in this matrix.
+ */
+ public double maxValue();
+
+ /**
+ * Gets the minimum value in this matrix.
+ *
+ * @return Minimum value in this matrix.
+ */
+ public double minValue();
+
+ /**
+ * Gets the maximum element in this matrix.
+ *
+ * @return Maximum element in this matrix.
+ */
+ public Element maxElement();
+
+ /**
+ * Gets the minimum element in this matrix.
+ *
+ * @return Minimum element in this matrix.
+ */
+ public Element minElement();
+
+ /**
+ * Gets the matrix's element at the given coordinates.
+ *
+ * @param row Row index.
+ * @param col Column index.
+ * @return Element at the given coordinates.
+ */
+ public Element getElement(int row, int col);
+
+ /**
+ * Swaps two rows in this matrix.
+ *
+ * @param row1 Row #1.
+ * @param row2 Row #2.
+ * @return This matrix.
+ */
+ public Matrix swapRows(int row1, int row2);
+
+ /**
+ * Swaps two columns in this matrix.
+ *
+ * @param col1 Column #1.
+ * @param col2 Column #2.
+ * @return This matrix.
+ */
+ public Matrix swapColumns(int col1, int col2);
+
+ /**
+ * Assigns given value to all elements of this matrix.
+ *
+ * @param val Value to assign to all elements.
+ * @return This matrix.
+ */
+ public Matrix assign(double val);
+
+ /**
+ * Assigns given values to this matrix.
+ *
+ * @param vals Values to assign.
+ * @return This matrix.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix assign(double[][] vals);
+
+ /**
+ * Assigns values from given matrix to this matrix.
+ *
+ * @param mtx Matrix to assign to this matrix.
+ * @return This matrix.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix assign(Matrix mtx);
+
+ /**
+ * Assigns each matrix element to the value generated by given function.
+ *
+ * @param fun Function that takes the row and column and returns the value to assign.
+ * @return This matrix.
+ */
+ public Matrix assign(IntIntToDoubleFunction fun);
+
+ /**
+ * Maps all values in this matrix through a given function.
+ *
+ * @param fun Mapping function.
+ * @return This matrix.
+ */
+ public Matrix map(IgniteDoubleFunction<Double> fun);
+
+ /**
+ * Maps all values in this matrix through a given function.
+ * <p>
+ * For this matrix {@code A}, argument matrix {@code B} and the
+ * function {@code F} this method maps every cell {@code x, y} as:
+ * {@code A(x,y) = fun(A(x,y), B(x,y))}.</p>
+ *
+ * @param mtx Argument matrix.
+ * @param fun Mapping function.
+ * @return This function.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix map(Matrix mtx, IgniteBiFunction<Double, Double, Double> fun);
+
+ /**
+ * Gets number of non-zero elements in this matrix.
+ *
+ * @return Number of non-zero elements in this matrix.
+ */
+ public int nonZeroElements();
+
+ /**
+ * Gets spliterator for all values in this matrix.
+ *
+ * @return Spliterator for all values.
+ */
+ public Spliterator<Double> allSpliterator();
+
+ /**
+ * Gets spliterator for all non-zero values in this matrix.
+ *
+ * @return Spliterator for all non-zero values.
+ */
+ public Spliterator<Double> nonZeroSpliterator();
+
+ /**
+ * Assigns values from given vector to the specified column in this matrix.
+ *
+ * @param col Column index.
+ * @param vec Vector to get values from.
+ * @return This matrix.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix assignColumn(int col, Vector vec);
+
+ /**
+ * Assigns values from given vector to the specified row in this matrix.
+ *
+ * @param row Row index.
+ * @param vec Vector to get values from.
+ * @return This matrix.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix assignRow(int row, Vector vec);
+
+ /**
+ * Collects the results of applying a given function to all rows in this matrix.
+ *
+ * @param fun Aggregating function.
+ * @return Vector of row aggregates.
+ */
+ public Vector foldRows(IgniteFunction<Vector, Double> fun);
+
+ /**
+ * Collects the results of applying a given function to all columns in this matrix.
+ *
+ * @param fun Aggregating function.
+ * @return Vector of column aggregates.
+ */
+ public Vector foldColumns(IgniteFunction<Vector, Double> fun);
+
+ /**
+ * Folds this matrix into a single value.
+ *
+ * @param foldFun Folding function that takes two parameters: accumulator and the current value.
+ * @param mapFun Mapping function that is called on each matrix cell before its passed to the accumulator (as its
+ * second parameter).
+ * @param <T> Type of the folded value.
+ * @param zeroVal Zero value for fold function.
+ * @return Folded value of this matrix.
+ */
+ public <T> T foldMap(IgniteBiFunction<T, Double, T> foldFun, IgniteDoubleFunction<Double> mapFun, T zeroVal);
+
+ /**
+ * Calculates the density of the matrix based on supplied criteria.
+ * Returns {@code true} if this matrix is denser than threshold with at least 80% confidence.
+ *
+ * @param threshold the threshold value [0, 1] of non-zero elements above which the matrix is considered dense.
+ */
+ public boolean density(double threshold);
+
+ /**
+ * Gets number of columns in this matrix.
+ *
+ * @return The number of columns in this matrix.
+ */
+ public int columnSize();
+
+ /**
+ * Gets number of rows in this matrix.
+ *
+ * @return The number of rows in this matrix.
+ */
+ public int rowSize();
+
+ /**
+ * Divides each value in this matrix by the argument.
+ *
+ * @param x Divider value.
+ * @return This matrix.
+ */
+ public Matrix divide(double x);
+
+ /**
+ * Gets the matrix value at the provided location.
+ *
+ * @param row Row index.
+ * @param col Column index.
+ * @return Matrix value.
+ * @throws IndexException Thrown in case of index is out of bound.
+ */
+ public double get(int row, int col);
+
+ /**
+ * Gets the matrix value at the provided location without checking boundaries.
+ * This method is marginally quicker than its {@link #get(int, int)} sibling.
+ *
+ * @param row Row index.
+ * @param col Column index.
+ * @return Matrix value.
+ */
+ public double getX(int row, int col);
+
+ /**
+ * Gets matrix storage model.
+ */
+ public MatrixStorage getStorage();
+
+ /**
+ * Clones this matrix.
+ * <p>
+ * NOTE: new matrix will have the same flavor as the this matrix but a different ID.</p>
+ *
+ * @return New matrix of the same underlying class, the same size and the same values.
+ */
+ public Matrix copy();
+
+ /**
+ * Creates new empty matrix of the same underlying class but of different size.
+ * <p>
+ * NOTE: new matrix will have the same flavor as the this matrix but a different ID.</p>
+ *
+ * @param rows Number of rows for new matrix.
+ * @param cols Number of columns for new matrix.
+ * @return New matrix of the same underlying class and size.
+ */
+ public Matrix like(int rows, int cols);
+
+ /**
+ * Creates new empty vector of compatible properties (similar or the same flavor) to this matrix.
+ *
+ * @param crd Cardinality of the vector.
+ * @return Newly created empty vector "compatible" to this matrix.
+ */
+ public Vector likeVector(int crd);
+
+ /**
+ * Creates new matrix where each value is a difference between corresponding value of this matrix and
+ * passed in argument matrix.
+ *
+ * @param mtx Argument matrix.
+ * @return New matrix of the same underlying class and size.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix minus(Matrix mtx);
+
+ /**
+ * Creates new matrix where each value is a sum of the corresponding value of this matrix and
+ * argument value.
+ *
+ * @param x Value to add.
+ * @return New matrix of the same underlying class and size.
+ */
+ public Matrix plus(double x);
+
+ /**
+ * Creates new matrix where each value is a sum of corresponding values of this matrix and
+ * passed in argument matrix.
+ *
+ * @param mtx Argument matrix.
+ * @return New matrix of the same underlying class and size.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix plus(Matrix mtx);
+
+ /**
+ * Auto-generated globally unique matrix ID.
+ *
+ * @return Matrix GUID.
+ */
+ public IgniteUuid guid();
+
+ /**
+ * Sets given value.
+ *
+ * @param row Row index.
+ * @param col Column index.
+ * @param val Value to set.
+ * @return This matrix.
+ * @throws IndexException Thrown in case of either index is out of bound.
+ */
+ public Matrix set(int row, int col, double val);
+
+ /**
+ * Sets values for given row.
+ *
+ * @param row Row index.
+ * @param data Row data to set.
+ * @return This matrix.
+ * @throws IndexException Thrown in case of index is out of bound.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix setRow(int row, double[] data);
+
+ /**
+ * Get a specific row from matrix.
+ *
+ * @param row Row index.
+ * @return row.
+ */
+ public Vector getRow(int row);
+
+ /**
+ * Sets values for given column.
+ *
+ * @param col Column index.
+ * @param data Column data to set.
+ * @return This matrix.
+ * @throws IndexException Thrown in case of index is out of bound.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix setColumn(int col, double[] data);
+
+ /**
+ * Get a specific row from matrix.
+ *
+ * @param col Col index.
+ * @return Col.
+ */
+ public Vector getCol(int col);
+
+ /**
+ * Sets given value without checking for index bounds. This method is marginally faster
+ * than its {@link #set(int, int, double)} sibling.
+ *
+ * @param row Row index.
+ * @param col Column index.
+ * @param val Value to set.
+ * @return This matrix.
+ */
+ public Matrix setX(int row, int col, double val);
+
+ /**
+ * Creates new matrix containing the product of given value and values in this matrix.
+ *
+ * @param x Value to multiply.
+ * @return New matrix.
+ */
+ public Matrix times(double x);
+
+ /**
+ * Creates new matrix that is the product of multiplying this matrix and the argument matrix.
+ *
+ * @param mtx Argument matrix.
+ * @return New matrix.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Matrix times(Matrix mtx);
+
+ /**
+ * Creates new matrix that is the product of multiplying this matrix and the argument vector.
+ *
+ * @param vec Argument vector.
+ * @return New matrix.
+ * @throws CardinalityException Thrown if cardinalities mismatch.
+ */
+ public Vector times(Vector vec);
+
+ /**
+ * Gets sum of all elements in the matrix.
+ *
+ * @return Sum of all elements in this matrix.
+ */
+ public double sum();
+
+ /**
+ * Creates new matrix that is transpose of this matrix.
+ *
+ * @return New transposed matrix.
+ */
+ public Matrix transpose();
+
+ /**
+ * Creates new view into matrix row. Changes to the view will be propagated to this matrix.
+ *
+ * @param row Row index.
+ * @return New view.
+ * @throws IndexException Thrown in case of index is out of bound.
+ */
+ public Vector viewRow(int row);
+
+ /**
+ * Creates new view into matrix column . Changes to the view will be propagated to this matrix.
+ *
+ * @param col Column index.
+ * @return New view.
+ * @throws IndexException Thrown in case of index is out of bound.
+ */
+ public Vector viewColumn(int col);
+
+ /**
+ * Creates new view into matrix diagonal. Changes to the view will be propagated to this matrix.
+ *
+ * @return New view.
+ */
+ public Vector viewDiagonal();
+
+ /**
+ * Destroys matrix if managed outside of JVM. It's a no-op in all other cases.
+ */
+ public default void destroy() {
+ // No-op.
+ }
+
+ /**
+ * Replace matrix entry with value oldVal at (row, col) with result of computing f(row, col, oldVal).
+ *
+ * @param row Row.
+ * @param col Column.
+ * @param f Function used for replacing.
+ */
+ public void compute(int row, int col, IgniteTriFunction<Integer, Integer, Double, Double> f);
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/MatrixStorage.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/MatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/MatrixStorage.java
new file mode 100644
index 0000000..00d1ce0
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/MatrixStorage.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix;
+
+import java.io.Externalizable;
+import org.apache.ignite.ml.math.Blas;
+import org.apache.ignite.ml.math.Destroyable;
+import org.apache.ignite.ml.math.StorageConstants;
+import org.apache.ignite.ml.math.StorageOpsMetrics;
+
+/**
+ * Data storage support for {@link Matrix}.
+ */
+public interface MatrixStorage extends Externalizable, StorageOpsMetrics, Destroyable {
+ /**
+ * @param x Matrix row index.
+ * @param y Matrix column index.
+ * @return Value corresponding to given row and column.
+ */
+ public double get(int x, int y);
+
+ /**
+ * @param x Matrix row index.
+ * @param y Matrix column index.
+ * @param v Value to set at given row and column.
+ */
+ public void set(int x, int y, double v);
+
+ /**
+ *
+ */
+ public int columnSize();
+
+ /**
+ *
+ */
+ public int rowSize();
+
+ /**
+ * @return Matrix elements storage mode.
+ * @see StorageConstants
+ */
+ public int storageMode();
+
+ /**
+ * @return Matrix access mode.
+ * @see StorageConstants
+ */
+ public int accessMode();
+
+ /**
+ * Gets underlying data, if {@link StorageOpsMetrics#isArrayBased()} returns {@code false} this method return
+ * copy of data. The data must be adapted for {@link Blas}.
+ *
+ * @see StorageOpsMetrics#isArrayBased()
+ */
+ default public double[] data() {
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/OrderedMatrix.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/OrderedMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/OrderedMatrix.java
new file mode 100644
index 0000000..57ddef2
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/OrderedMatrix.java
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix;
+
+/** Interface for matrix with particular order for storing entities. */
+public interface OrderedMatrix {
+ /** Get access mode. */
+ public int accessMode();
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/DenseMatrix.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/DenseMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/DenseMatrix.java
new file mode 100644
index 0000000..e4792ee
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/DenseMatrix.java
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix.impl;
+
+import org.apache.ignite.ml.math.primitives.matrix.Matrix;
+import org.apache.ignite.ml.math.primitives.matrix.OrderedMatrix;
+import org.apache.ignite.ml.math.StorageConstants;
+import org.apache.ignite.ml.math.primitives.vector.impl.DenseVector;
+import org.apache.ignite.ml.math.primitives.matrix.storage.DenseMatrixStorage;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.math.primitives.matrix.AbstractMatrix;
+
+/**
+ * Basic implementation for matrix.
+ *
+ * This is a trivial implementation for matrix assuming dense logic, local on-heap JVM storage
+ * based on <code>double[][]</code> array. It is only suitable for data sets where
+ * local, non-distributed execution is satisfactory and on-heap JVM storage is enough
+ * to keep the entire data set.
+ */
+public class DenseMatrix extends AbstractMatrix implements OrderedMatrix {
+ /**
+ *
+ */
+ public DenseMatrix() {
+ // No-op.
+ }
+
+ /**
+ * @param rows Amount of rows in matrix.
+ * @param cols Amount of columns in matrix.
+ */
+ public DenseMatrix(int rows, int cols) {
+ this(rows, cols, StorageConstants.ROW_STORAGE_MODE);
+ }
+
+ /**
+ * @param rows Amount of rows in matrix.
+ * @param cols Amount of columns in matrix.
+ * @param acsMode Storage order (row or column-based).
+ */
+ public DenseMatrix(int rows, int cols, int acsMode) {
+ assert rows > 0;
+ assert cols > 0;
+
+ setStorage(new DenseMatrixStorage(rows, cols, acsMode));
+ }
+
+ /**
+ * @param mtx Backing data array.
+ * @param acsMode Access mode.
+ */
+ public DenseMatrix(double[][] mtx, int acsMode) {
+ assert mtx != null;
+
+ setStorage(new DenseMatrixStorage(mtx, acsMode));
+ }
+
+ /**
+ * @param mtx Backing data array.
+ */
+ public DenseMatrix(double[][] mtx) {
+ this(mtx, StorageConstants.ROW_STORAGE_MODE);
+ }
+
+ /**
+ * @param mtx Backing data array.
+ * @param acsMode Access mode.
+ */
+ public DenseMatrix(double[] mtx, int rows, int acsMode) {
+ assert mtx != null;
+
+ setStorage(new DenseMatrixStorage(mtx, rows, acsMode));
+ }
+
+ /**
+ * Build new matrix from flat raw array.
+ */
+ public DenseMatrix(double[] mtx, int rows) {
+ this(mtx, StorageConstants.ROW_STORAGE_MODE, rows);
+ }
+
+ /** */
+ private DenseMatrix(DenseMatrix orig) {
+ this(orig, orig.accessMode());
+ }
+
+ /**
+ * @param orig Original matrix.
+ * @param acsMode Access mode.
+ */
+ private DenseMatrix(DenseMatrix orig, int acsMode) {
+ assert orig != null;
+
+ setStorage(new DenseMatrixStorage(orig.rowSize(), orig.columnSize(), acsMode));
+
+ assign(orig);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix copy() {
+ return new DenseMatrix(this, accessMode());
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix like(int rows, int cols) {
+ return new DenseMatrix(rows, cols, getStorage() != null ? accessMode() : StorageConstants.ROW_STORAGE_MODE);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector likeVector(int crd) {
+ return new DenseVector(crd);
+ }
+
+ /** {@inheritDoc} */
+ @Override public int accessMode() {
+ return getStorage().accessMode();
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/SparseMatrix.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/SparseMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/SparseMatrix.java
new file mode 100644
index 0000000..119bb8d
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/SparseMatrix.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix.impl;
+
+import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
+import it.unimi.dsi.fastutil.ints.IntIterator;
+import it.unimi.dsi.fastutil.ints.IntSet;
+import org.apache.ignite.ml.math.primitives.matrix.Matrix;
+import org.apache.ignite.ml.math.primitives.matrix.MatrixStorage;
+import org.apache.ignite.ml.math.StorageConstants;
+import org.apache.ignite.ml.math.primitives.matrix.storage.SparseMatrixStorage;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.math.functions.IgniteTriFunction;
+import org.apache.ignite.ml.math.primitives.matrix.AbstractMatrix;
+import org.apache.ignite.ml.math.primitives.vector.impl.SparseVector;
+
+/**
+ * Sparse local onheap matrix with {@link SparseVector} as rows.
+ */
+public class SparseMatrix extends AbstractMatrix implements StorageConstants {
+ /**
+ *
+ */
+ public SparseMatrix() {
+ // No-op.
+ }
+
+ /**
+ * Construct new {@link SparseMatrix}.
+ *
+ * By default storage sets in row optimized mode and in random access mode.
+ */
+ public SparseMatrix(int rows, int cols) {
+ setStorage(mkStorage(rows, cols));
+ }
+
+ /**
+ * Create new {@link SparseMatrixStorage}.
+ */
+ private MatrixStorage mkStorage(int rows, int cols) {
+ return new SparseMatrixStorage(rows, cols, StorageConstants.RANDOM_ACCESS_MODE, StorageConstants.ROW_STORAGE_MODE);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix like(int rows, int cols) {
+ return new SparseMatrix(rows, cols);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector likeVector(int crd) {
+ return new SparseVector(crd, StorageConstants.RANDOM_ACCESS_MODE);
+ }
+
+ /** {@inheritDoc} */
+ @Override public int nonZeroElements() {
+ int res = 0;
+ IntIterator rowIter = indexesMap().keySet().iterator();
+
+ while (rowIter.hasNext()) {
+ int row = rowIter.nextInt();
+ res += indexesMap().get(row).size();
+ }
+
+ return res;
+ }
+
+ /** */
+ public Int2ObjectArrayMap<IntSet> indexesMap() {
+ return ((SparseMatrixStorage)getStorage()).indexesMap();
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix copy() {
+ Matrix cp = like(rowSize(), columnSize());
+
+ cp.assign(this);
+
+ return cp;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void compute(int row, int col, IgniteTriFunction<Integer, Integer, Double, Double> f) {
+ ((SparseMatrixStorage)getStorage()).compute(row, col, f);
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/ViewMatrix.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/ViewMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/ViewMatrix.java
new file mode 100644
index 0000000..df769c7
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/ViewMatrix.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix.impl;
+
+import java.io.Externalizable;
+import org.apache.ignite.ml.math.primitives.matrix.Matrix;
+import org.apache.ignite.ml.math.primitives.matrix.MatrixStorage;
+import org.apache.ignite.ml.math.primitives.vector.Vector;
+import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException;
+import org.apache.ignite.ml.math.primitives.matrix.AbstractMatrix;
+import org.apache.ignite.ml.math.primitives.matrix.storage.ViewMatrixStorage;
+
+/**
+ * Implements the rectangular view into the parent {@link Matrix}.
+ */
+public class ViewMatrix extends AbstractMatrix {
+ /**
+ * Constructor for {@link Externalizable} interface.
+ */
+ public ViewMatrix() {
+ // No-op.
+ }
+
+ /**
+ * @param parent Backing parent {@link Matrix}.
+ * @param rowOff Row offset to parent matrix.
+ * @param colOff Column offset to parent matrix.
+ * @param rows Amount of rows in the view.
+ * @param cols Amount of columns in the view.
+ */
+ public ViewMatrix(Matrix parent, int rowOff, int colOff, int rows, int cols) {
+ this(parent == null ? null : parent.getStorage(), rowOff, colOff, rows, cols);
+ }
+
+ /**
+ * @param sto Backing parent {@link MatrixStorage}.
+ * @param rowOff Row offset to parent storage.
+ * @param colOff Column offset to parent storage.
+ * @param rows Amount of rows in the view.
+ * @param cols Amount of columns in the view.
+ */
+ public ViewMatrix(MatrixStorage sto, int rowOff, int colOff, int rows, int cols) {
+ super(new ViewMatrixStorage(sto, rowOff, colOff, rows, cols));
+ }
+
+ /**
+ *
+ *
+ */
+ private ViewMatrixStorage storage() {
+ return (ViewMatrixStorage)getStorage();
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix copy() {
+ ViewMatrixStorage sto = storage();
+
+ return new ViewMatrix(sto.delegate(), sto.rowOffset(), sto.columnOffset(), sto.rowSize(), sto.columnSize());
+ }
+
+ /** {@inheritDoc} */
+ @Override public Matrix like(int rows, int cols) {
+ throw new UnsupportedOperationException();
+ }
+
+ /** {@inheritDoc} */
+ @Override public Vector likeVector(int crd) {
+ throw new UnsupportedOperationException();
+ }
+}
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/package-info.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/package-info.java
new file mode 100644
index 0000000..3ae3cda
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/impl/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. -->
+ * Contains several matrix implementations.
+ */
+package org.apache.ignite.ml.math.primitives.matrix.impl;
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/package-info.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/package-info.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/package-info.java
new file mode 100644
index 0000000..7dd53ed
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/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. -->
+ * Contains matrix related classes.
+ */
+package org.apache.ignite.ml.math.primitives.matrix;
http://git-wip-us.apache.org/repos/asf/ignite/blob/26e40528/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/storage/DenseMatrixStorage.java
----------------------------------------------------------------------
diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/storage/DenseMatrixStorage.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/storage/DenseMatrixStorage.java
new file mode 100644
index 0000000..dcdaf4f
--- /dev/null
+++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/primitives/matrix/storage/DenseMatrixStorage.java
@@ -0,0 +1,225 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.ml.math.primitives.matrix.storage;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Arrays;
+import org.apache.ignite.ml.math.Blas;
+import org.apache.ignite.ml.math.primitives.matrix.MatrixStorage;
+import org.apache.ignite.ml.math.StorageConstants;
+import org.apache.ignite.ml.math.functions.IgniteIntIntToIntBiFunction;
+import org.apache.ignite.ml.math.util.MatrixUtil;
+
+/**
+ * Array based {@link MatrixStorage} implementation.
+ */
+public class DenseMatrixStorage implements MatrixStorage {
+ /** Backing data array. */
+ private double[] data;
+ /** Amount of rows in the matrix. */
+ private int rows;
+ /** Amount of columns in the matrix. */
+ private int cols;
+ /** Mode specifying if this matrix is row-major or column-major. */
+ private int stoMode;
+ /** Index mapper */
+ private IgniteIntIntToIntBiFunction idxMapper;
+
+ /**
+ *
+ */
+ public DenseMatrixStorage() {
+ // No-op.
+ }
+
+ /**
+ * @param rows Amount of rows in the matrix.
+ * @param cols Amount of columns in the matrix.
+ */
+ public DenseMatrixStorage(int rows, int cols) {
+ this(rows, cols, StorageConstants.ROW_STORAGE_MODE);
+ }
+
+ /** */
+ public DenseMatrixStorage(int rows, int cols, int stoMode) {
+ assert rows > 0;
+ assert cols > 0;
+
+ this.data = new double[rows * cols];
+ this.rows = rows;
+ this.cols = cols;
+ idxMapper = indexMapper(stoMode);
+ this.stoMode = stoMode;
+ }
+
+ /**
+ * @param data Backing data array.
+ */
+ public DenseMatrixStorage(double[][] data, int stoMode) {
+ this(MatrixUtil.flatten(data, stoMode), data.length, stoMode);
+ }
+
+ /**
+ * @param data Backing data array.
+ */
+ public DenseMatrixStorage(double[][] data) {
+ this(MatrixUtil.flatten(data, StorageConstants.ROW_STORAGE_MODE), data.length);
+ }
+
+ /**
+ * @param data Backing data array.
+ */
+ public DenseMatrixStorage(double[] data, int rows, int stoMode) {
+ assert data != null;
+ assert data.length % rows == 0;
+
+ this.data = data;
+ this.rows = rows;
+ this.cols = data.length / rows;
+ idxMapper = indexMapper(stoMode);
+ this.stoMode = stoMode;
+
+ assert rows > 0;
+ assert cols > 0;
+ }
+
+ /**
+ * @param data Backing data array.
+ */
+ public DenseMatrixStorage(double[] data, int rows) {
+ this(data, rows, StorageConstants.ROW_STORAGE_MODE);
+ }
+
+ /** {@inheritDoc} */
+ @Override public double get(int x, int y) {
+ return data[idxMapper.apply(x, y)];
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isSequentialAccess() {
+ return false;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isDense() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isRandomAccess() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isDistributed() {
+ return false;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void set(int x, int y, double v) {
+ data[idxMapper.apply(x, y)] = v;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int columnSize() {
+ return cols;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int rowSize() {
+ return rows;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean isArrayBased() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override public double[] data() {
+ return data;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int storageMode() {
+ return stoMode;
+ }
+
+ /**
+ * Get the index mapper for given access mode.
+ *
+ * NB: inverted for {@link Blas}.
+ *
+ * @param stoMode Access mode.
+ */
+ private IgniteIntIntToIntBiFunction indexMapper(int stoMode) {
+ return stoMode == StorageConstants.COLUMN_STORAGE_MODE ? (r, c) -> r * cols + c :
+ (r, c) -> c * rows + r;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeInt(rows);
+ out.writeInt(cols);
+ out.writeInt(stoMode);
+
+ out.writeObject(data);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ rows = in.readInt();
+ cols = in.readInt();
+ stoMode = in.readInt();
+ idxMapper = indexMapper(stoMode);
+
+ data = (double[])in.readObject();
+ }
+
+ /** Get the access mode of this storage. */
+ public int accessMode() {
+ return stoMode;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ int res = 1;
+
+ res += res * 37 + rows;
+ res += res * 37 + cols;
+ res += res * 37 + stoMode;
+ res += res * 37 + Arrays.hashCode(data);
+
+ return res;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ DenseMatrixStorage that = (DenseMatrixStorage)o;
+
+ return stoMode == that.stoMode && Arrays.equals(data, that.data);
+ }
+}