http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java deleted file mode 100644 index a2ffd90..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/FunctionVector.java +++ /dev/null @@ -1,112 +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.util.Map; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.IgniteFunction; -import org.apache.ignite.ml.math.functions.IntDoubleToVoidFunction; -import org.apache.ignite.ml.math.impls.storage.vector.FunctionVectorStorage; - -/** - * Implementation of {@link Vector} that maps vector element index to {@link java.util.function} interfaces. - */ -public class FunctionVector extends AbstractVector { - /** - * - */ - public FunctionVector() { - // No-op. - } - - /** - * Creates read-write or read-only function vector. - * - * @param size Vector size. - * @param getFunc Function that returns value corresponding to given element index. - * @param setFunc Set function. If {@code null} - this will be a read-only vector. - */ - public FunctionVector(int size, IgniteFunction<Integer, Double> getFunc, IntDoubleToVoidFunction setFunc) { - setStorage(new FunctionVectorStorage(size, getFunc, setFunc)); - } - - /** - * Creates read-only function vector. - * - * @param size Vector size. - * @param getFunc Function that returns value corresponding to given element index. - */ - public FunctionVector(int size, IgniteFunction<Integer, Double> getFunc) { - setStorage(new FunctionVectorStorage(size, getFunc)); - } - - /** - * @param args Arguments for vector constructor. - */ - public FunctionVector(Map<String, Object> args) { - assert args != null; - - if (args.containsKey("size") && args.containsKey("getFunc") && args.containsKey("setFunc")) { - @SuppressWarnings("unchecked") - IgniteFunction<Integer, Double> getFunc = (IgniteFunction<Integer, Double>)args.get("getFunc"); - IntDoubleToVoidFunction setFunc = (IntDoubleToVoidFunction)args.get("setFunc"); - int size = (int)args.get("size"); - - setStorage(new FunctionVectorStorage(size, getFunc, setFunc)); - } - else if (args.containsKey("size") && args.containsKey("getFunc")) { - @SuppressWarnings("unchecked") - IgniteFunction<Integer, Double> getFunc = (IgniteFunction<Integer, Double>)args.get("getFunc"); - int size = (int)args.get("size"); - - setStorage(new FunctionVectorStorage(size, getFunc)); - } - else - throw new UnsupportedOperationException("Invalid constructor argument(s)."); - } - - /** - * - * - */ - private FunctionVectorStorage storage() { - return (FunctionVectorStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - FunctionVectorStorage sto = storage(); - - return new FunctionVector(crd, sto.getFunction(), sto.setFunction()); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - if (x == 0.0) - return like(size()).assign(0); - else - return super.times(x); - } -}
http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java deleted file mode 100644 index 365b5eb..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/PivotedVectorView.java +++ /dev/null @@ -1,163 +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.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.impls.storage.vector.PivotedVectorStorage; - -/** - * Pivoted (index mapped) view over another vector. - */ -public class PivotedVectorView extends AbstractVector { - /** */ - private Vector vec; - - /** - * @param vec Parent vector. - * @param pivot Mapping from external index to internal. - * @param unpivot Mapping from internal index to external. - */ - public PivotedVectorView(Vector vec, int[] pivot, int[] unpivot) { - setStorage(new PivotedVectorStorage(vec.getStorage(), pivot, unpivot)); - - checkCardinality(pivot); - checkCardinality(unpivot); - - this.vec = vec; - } - - /** - * @param vec Parent vector. - * @param pivot Mapping from external index to internal. - */ - public PivotedVectorView(Vector vec, int[] pivot) { - setStorage(new PivotedVectorStorage(vec.getStorage(), pivot)); - - checkCardinality(pivot); - - this.vec = vec; - } - - /** */ - private PivotedVectorStorage storage() { - return (PivotedVectorStorage)getStorage(); - } - - /** - * - */ - public PivotedVectorView() { - // No-op. - } - - /** - * @return Parent vector. - */ - public Vector getBaseVector() { - return vec; - } - - /** - * @param i Index to pivot. - * @return Mapping from external index to internal for given index. - */ - public int pivot(int i) { - return storage().pivot()[i]; - } - - /** - * @param i Index to unpivot. - * @return Mapping from internal index to external for given index. - */ - public int unpivot(int i) { - return storage().unpivot()[i]; - } - - /** - * @param idx Index of vector element. - * @return Vector element at given index. - */ - protected Vector.Element makeElement(int idx) { - checkIndex(idx); - - // External index. - int exIdx = storage().pivot()[idx]; - - return new Vector.Element() { - /** {@inheritDoc} */ - @Override public double get() { - return storageGet(idx); - } - - /** {@inheritDoc} */ - @Override public int index() { - return exIdx; - } - - /** {@inheritDoc} */ - @Override public void set(double val) { - storageSet(idx, val); - } - }; - } - - /** {@inheritDoc} */ - @Override public Vector copy() { - PivotedVectorStorage sto = storage(); - - return new PivotedVectorView(vec, sto.pivot(), sto.unpivot()); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return vec.likeMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - if (x == 0.0) - return copy().map(Functions.mult(x)); - else - return super.times(x); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeObject(vec); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - vec = (Vector)in.readObject(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java deleted file mode 100644 index 633773e..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/RandomVector.java +++ /dev/null @@ -1,130 +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 java.util.Map; -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.matrix.RandomMatrix; -import org.apache.ignite.ml.math.impls.storage.vector.RandomVectorStorage; - -/** - * Random vector. Each value is taken from {-1,0,1} with roughly equal probability. Note - * that by default, the value is determined by a relatively simple hash of the index. - */ -public class RandomVector extends AbstractReadOnlyVector { - /** */ - private boolean fastHash; - - /** - * @param size Vector cardinality. - * @param fastHash Whether or not to use fast hashing or Murmur hashing. - */ - private VectorStorage mkStorage(int size, boolean fastHash) { - this.fastHash = fastHash; - - return new RandomVectorStorage(size, fastHash); - } - - /** - * @param size Vector cardinality. - * @param fastHash Whether or not to use fast hashing or Murmur hashing. - */ - public RandomVector(int size, boolean fastHash) { - setStorage(mkStorage(size, fastHash)); - } - - /** - * @param size Vector cardinality. - */ - public RandomVector(int size) { - this(size, true); - } - - /** - * @param args Parameters to create new vector instance. - */ - public RandomVector(Map<String, Object> args) { - assert args != null; - - if (args.containsKey("size") && args.containsKey("fastHash")) - setStorage(mkStorage((int)args.get("size"), (boolean)args.get("fastHash"))); - else if (args.containsKey("size")) - setStorage(mkStorage((int)args.get("size"), true)); - else - throw new UnsupportedOperationException("Invalid constructor argument(s)."); - } - - /** */ - public RandomVector() { - // No-op. - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - return new RandomVector(crd, fastHash); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return new RandomMatrix(rows, cols); - } - - /** {@inheritDoc} */ - @Override public void writeExternal(ObjectOutput out) throws IOException { - super.writeExternal(out); - - out.writeBoolean(fastHash); - } - - /** {@inheritDoc} */ - @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { - super.readExternal(in); - - fastHash = in.readBoolean(); - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = 1; - - res = res * 37 + Boolean.hashCode(fastHash); - res = res * 37 + getStorage().hashCode(); - - return res; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (o == null || getClass() != o.getClass()) - return false; - - RandomVector that = (RandomVector)o; - VectorStorage sto = getStorage(); - - return fastHash == that.fastHash && (sto != null ? sto.equals(that.getStorage()) : that.getStorage() == null); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java deleted file mode 100644 index a5dc64b..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVector.java +++ /dev/null @@ -1,102 +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.util.Map; -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.impls.storage.vector.SingleElementVectorStorage; - -/** - * Read-write vector holding a single non-zero value at some index. - */ -public class SingleElementVector extends AbstractVector { - /** - * - */ - public SingleElementVector() { - // No-op - } - - /** - * @param size Parent vector size. - * @param idx Index of the parent vector element. - * @param val Value of the vector element. - */ - public SingleElementVector(int size, int idx, double val) { - super(new SingleElementVectorStorage(size, idx, val)); - } - - /** - * @param args Parameters to create new vector instance. - */ - public SingleElementVector(Map<String, Object> args) { - assert args != null; - - if (args.containsKey("size") && args.containsKey("index") && args.containsKey("value")) { - int size = (int)args.get("size"); - int idx = (int)args.get("index"); - double val = (double)args.get("value"); - - setStorage(new SingleElementVectorStorage(size, idx, val)); - } - else - throw new UnsupportedOperationException("Invalid constructor argument(s)."); - } - - /** - * - * - */ - private SingleElementVectorStorage storage() { - return (SingleElementVectorStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Element minElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public Element maxElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public double sum() { - return getX(storage().index()); - } - - /** {@inheritDoc} */ - @Override public int nonZeroElements() { - return isZero(get(storage().index())) ? 0 : 1; - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - int idx = storage().index(); - - return new SingleElementVector(crd, idx, getX(idx)); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java deleted file mode 100644 index c2c648b..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SingleElementVectorView.java +++ /dev/null @@ -1,97 +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.exceptions.UnsupportedOperationException; -import org.apache.ignite.ml.math.functions.Functions; -import org.apache.ignite.ml.math.impls.storage.vector.SingleElementVectorDelegateStorage; - -/** - * Single value vector view over another vector. - */ -public class SingleElementVectorView extends AbstractVector { - /** - * - */ - public SingleElementVectorView() { - // No-op. - } - - /** - * @param vec Parent vector. - * @param idx Index of the parent vector element. - */ - public SingleElementVectorView(Vector vec, int idx) { - super(new SingleElementVectorDelegateStorage(vec, idx)); - } - - /** - * - * - */ - private SingleElementVectorDelegateStorage storage() { - return (SingleElementVectorDelegateStorage)getStorage(); - } - - /** {@inheritDoc} */ - @Override public Vector.Element minElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public Vector.Element maxElement() { - return makeElement(storage().index()); - } - - /** {@inheritDoc} */ - @Override public double sum() { - return getX(storage().index()); - } - - /** {@inheritDoc} */ - @Override public int nonZeroElements() { - return isZero(getX(storage().index())) ? 0 : 1; - } - - /** {@inheritDoc} */ - @Override public Vector copy() { - SingleElementVectorDelegateStorage sto = storage(); - - return new SingleElementVectorView(sto.delegate(), sto.index()); - } - - /** {@inheritDoc} */ - @Override public Vector times(double x) { - if (x == 0.0) - return copy().map(Functions.mult(x)); - else - return super.times(x); - } - - /** {@inheritDoc} */ - @Override public Vector like(int crd) { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - throw new UnsupportedOperationException(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java deleted file mode 100644 index 535d51a..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseBlockDistributedVector.java +++ /dev/null @@ -1,135 +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.util.UUID; -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.distributed.CacheUtils; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; -import org.apache.ignite.ml.math.impls.storage.matrix.BlockVectorStorage; - -/** - * Sparse distributed vector implementation based on data grid. - * <p> - * Unlike {@link CacheVector} that is based on existing cache, this implementation creates distributed - * cache internally and doesn't rely on pre-existing cache.</p> - * <p> - * You also need to call {@link #destroy()} to remove the underlying cache when you no longer need this - * vector.</p> - * <p> - * <b>Currently fold supports only commutative operations.<b/></p> - */ -public class SparseBlockDistributedVector extends AbstractVector implements StorageConstants { - /** - * - */ - public SparseBlockDistributedVector() { - // No-op. - } - - /** - * @param size Vector size - */ - public SparseBlockDistributedVector(int size) { - - assert size > 0; - setStorage(new BlockVectorStorage(size)); - } - - /** - * @param data Data to fill storage - */ - public SparseBlockDistributedVector(double[] data) { - setStorage(new BlockVectorStorage(data.length)); - for (int i = 0; i < data.length; i++) { - double val = data[i]; - if (val != 0.0) - storage().set(i, val); - } - } - - /** */ - public BlockVectorStorage storage() { - return (BlockVectorStorage)getStorage(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Vector divide(double d) { - return mapOverValues(v -> v / d); - } - - /** {@inheritDoc} */ - @Override public Vector like(int size) { - return new SparseBlockDistributedVector(size); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return new SparseBlockDistributedMatrix(rows, cols); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Vector plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Vector times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Vector assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteDoubleFunction<Double> fun) { - return mapOverValues(fun); - } - - /** - * @param mapper Mapping function. - * @return Vector with mapped values. - */ - private Vector mapOverValues(IgniteDoubleFunction<Double> mapper) { - CacheUtils.sparseMapForVector(getUUID(), mapper, storage().cacheName()); - - return this; - } - - /** */ - public UUID getUUID() { - return ((BlockVectorStorage)getStorage()).getUUID(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java deleted file mode 100644 index 3e7f8a1..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/SparseDistributedVector.java +++ /dev/null @@ -1,147 +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.util.UUID; -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.distributed.CacheUtils; -import org.apache.ignite.ml.math.functions.IgniteDoubleFunction; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; -import org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorage; - -/** - * Sparse distributed vector implementation based on data grid. - * <p> - * Unlike {@link CacheVector} that is based on existing cache, this implementation creates distributed - * cache internally and doesn't rely on pre-existing cache.</p> - * <p> - * You also need to call {@link #destroy()} to remove the underlying cache when you no longer need this - * vector.</p> - * <p> - * <b>Currently fold supports only commutative operations.<b/></p> - */ -public class SparseDistributedVector extends AbstractVector implements StorageConstants { - /** - * - */ - public SparseDistributedVector() { - // No-op. - } - - /** - * @param size Vector size. - * @param acsMode Vector elements access mode.. - */ - public SparseDistributedVector(int size, int acsMode) { - - assert size > 0; - assertAccessMode(acsMode); - - setStorage(new SparseDistributedVectorStorage(size, acsMode)); - } - - /** - * @param size Size. - */ - public SparseDistributedVector(int size) { - this(size, StorageConstants.RANDOM_ACCESS_MODE); - } - - /** - * @param data Data. - */ - public SparseDistributedVector(double[] data) { - setStorage(new SparseDistributedVectorStorage(data.length, StorageConstants.RANDOM_ACCESS_MODE)); - - for (int i = 0; i < data.length; i++) { - double val = data[i]; - - if (val != 0.0) - storage().set(i, val); - } - } - - /** */ - public SparseDistributedVectorStorage storage() { - return (SparseDistributedVectorStorage)getStorage(); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param d Value to divide to. - */ - @Override public Vector divide(double d) { - return mapOverValues(v -> v / d); - } - - /** {@inheritDoc} */ - @Override public Vector like(int size) { - return new SparseDistributedVector(size, storage().accessMode()); - } - - /** {@inheritDoc} */ - @Override public Matrix likeMatrix(int rows, int cols) { - return new SparseDistributedMatrix(rows, cols); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to add. - */ - @Override public Vector plus(double x) { - return mapOverValues(v -> v + x); - } - - /** - * Return the same matrix with updates values (broken contract). - * - * @param x Value to multiply. - */ - @Override public Vector times(double x) { - return mapOverValues(v -> v * x); - } - - /** {@inheritDoc} */ - @Override public Vector assign(double val) { - return mapOverValues(v -> val); - } - - /** {@inheritDoc} */ - @Override public Vector map(IgniteDoubleFunction<Double> fun) { - return mapOverValues(fun); - } - - /** - * @param mapper Mapping function. - * @return Vector with mapped values. - */ - private Vector mapOverValues(IgniteDoubleFunction<Double> mapper) { - CacheUtils.sparseMapForVector(getUUID(), mapper, storage().cacheName()); - - return this; - } - - /** */ - public UUID getUUID() { - return ((SparseDistributedVectorStorage)getStorage()).getUUID(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.java deleted file mode 100644 index 999e412..0000000 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/impls/vector/VectorBlockEntry.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.Vector; - -/** - * Block for {@link SparseBlockDistributedVector}. - */ -public final class VectorBlockEntry extends SparseLocalVector { - /** Max block size. */ - public static final int MAX_BLOCK_SIZE = 32; - - /** */ - public VectorBlockEntry() { - // No-op. - } - - /** */ - public VectorBlockEntry(int size) { - super(size, RANDOM_ACCESS_MODE); - assert size <= MAX_BLOCK_SIZE; - } - - /** */ - public VectorBlockEntry(Vector v) { - assert v.size() <= MAX_BLOCK_SIZE; - - setStorage(v.getStorage()); - } - -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java b/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java index 9f14bc7..e737aab 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/math/util/MatrixUtil.java @@ -24,13 +24,8 @@ import org.apache.ignite.ml.math.StorageConstants; import org.apache.ignite.ml.math.Vector; import org.apache.ignite.ml.math.functions.IgniteBiFunction; import org.apache.ignite.ml.math.functions.IgniteTriFunction; -import org.apache.ignite.ml.math.impls.matrix.CacheMatrix; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; import org.apache.ignite.ml.math.impls.matrix.MatrixView; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.matrix.RandomMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseBlockDistributedMatrix; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix; import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrix; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; @@ -94,15 +89,6 @@ public class MatrixUtil { } /** - * Check if a given matrix is distributed. - * - * @param matrix Matrix for like. - */ - private static boolean isDistributed(Matrix matrix) { - return matrix instanceof SparseDistributedMatrix || matrix instanceof SparseBlockDistributedMatrix; - } - - /** * Create the like vector with read-only matrices support. * * @param matrix Matrix for like. @@ -143,8 +129,7 @@ public class MatrixUtil { /** */ private static boolean isCopyLikeSupport(Matrix matrix) { - return matrix instanceof RandomMatrix || matrix instanceof MatrixView || matrix instanceof CacheMatrix || - matrix instanceof PivotedMatrixView; + return matrix instanceof MatrixView; } /** */ http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java b/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java index 559206d..7d0106e 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/nn/ReplicatedVectorMatrix.java @@ -317,20 +317,6 @@ class ReplicatedVectorMatrix implements Matrix { } /** {@inheritDoc} */ - @Override public double determinant() { - // If matrix is not square throw exception. - checkCardinality(vector.size(), replicationCnt); - - // If matrix is 1x1 then determinant is its single element otherwise there are linear dependence and determinant is 0. - return vector.size() > 0 ? 0 : vector.get(1); - } - - /** {@inheritDoc} */ - @Override public Matrix inverse() { - throw new UnsupportedOperationException(); - } - - /** {@inheritDoc} */ @Override public Matrix divide(double x) { return new ReplicatedVectorMatrix(vector.divide(x), replicationCnt, asCol); } http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java index 3239116..69262ab 100644 --- a/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java +++ b/modules/ml/src/main/java/org/apache/ignite/ml/structures/LabeledDataset.java @@ -22,7 +22,6 @@ import org.apache.ignite.ml.math.exceptions.CardinalityException; import org.apache.ignite.ml.math.exceptions.NoDataException; import org.apache.ignite.ml.math.exceptions.knn.NoLabelVectorException; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVector; /** * Class for set of labeled vectors. @@ -200,9 +199,6 @@ public class LabeledDataset<L, Row extends LabeledVector> extends Dataset<Row> i /** */ public static Vector emptyVector(int size, boolean isDistributed) { - if(isDistributed) - return new SparseDistributedVector(size); - else return new DenseLocalOnHeapVector(size); } http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.java deleted file mode 100644 index 2968924..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplDistributedTestSuite.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; - -import org.apache.ignite.ml.math.impls.matrix.CacheMatrixTest; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedBlockMatrixTest; -import org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrixTest; -import org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorageTest; -import org.apache.ignite.ml.math.impls.storage.vector.SparseDistributedVectorStorageTest; -import org.apache.ignite.ml.math.impls.vector.CacheVectorTest; -import org.apache.ignite.ml.math.impls.vector.SparseBlockDistributedVectorTest; -import org.apache.ignite.ml.math.impls.vector.SparseDistributedVectorTest; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -/** - * Test suite for all distributed tests located in org.apache.ignite.ml.math.impls.* package. - */ -@RunWith(Suite.class) [email protected]({ - CacheVectorTest.class, - CacheMatrixTest.class, - SparseDistributedMatrixStorageTest.class, - SparseDistributedMatrixTest.class, - SparseDistributedBlockMatrixTest.class, - SparseDistributedVectorStorageTest.class, - SparseDistributedVectorTest.class, - SparseBlockDistributedVectorTest.class -}) -public class MathImplDistributedTestSuite { - // No-op. -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java index 926d872..85e286d 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplLocalTestSuite.java @@ -17,24 +17,13 @@ package org.apache.ignite.ml.math; -import org.apache.ignite.ml.math.decompositions.CholeskyDecompositionTest; -import org.apache.ignite.ml.math.decompositions.EigenDecompositionTest; -import org.apache.ignite.ml.math.decompositions.LUDecompositionTest; -import org.apache.ignite.ml.math.decompositions.QRDSolverTest; -import org.apache.ignite.ml.math.decompositions.QRDecompositionTest; -import org.apache.ignite.ml.math.decompositions.SingularValueDecompositionTest; import org.apache.ignite.ml.math.distances.DistanceTest; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOffHeapMatrixConstructorTest; import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrixConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.DiagonalMatrixTest; -import org.apache.ignite.ml.math.impls.matrix.FunctionMatrixConstructorTest; import org.apache.ignite.ml.math.impls.matrix.MatrixAttributeTest; import org.apache.ignite.ml.math.impls.matrix.MatrixImplementationsTest; import org.apache.ignite.ml.math.impls.matrix.MatrixViewConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixViewConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.RandomMatrixConstructorTest; import org.apache.ignite.ml.math.impls.matrix.SparseLocalOnHeapMatrixConstructorTest; -import org.apache.ignite.ml.math.impls.matrix.TransposedMatrixViewTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixArrayStorageTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixOffHeapStorageTest; import org.apache.ignite.ml.math.impls.storage.matrix.MatrixStorageImplementationTest; @@ -43,21 +32,13 @@ import org.apache.ignite.ml.math.impls.storage.vector.SparseLocalOffHeapVectorSt import org.apache.ignite.ml.math.impls.storage.vector.VectorArrayStorageTest; import org.apache.ignite.ml.math.impls.storage.vector.VectorOffheapStorageTest; import org.apache.ignite.ml.math.impls.vector.AbstractVectorTest; -import org.apache.ignite.ml.math.impls.vector.ConstantVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.DelegatingVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.DenseLocalOffHeapVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVectorConstructorTest; -import org.apache.ignite.ml.math.impls.vector.FunctionVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.MatrixVectorViewTest; -import org.apache.ignite.ml.math.impls.vector.PivotedVectorViewConstructorTest; -import org.apache.ignite.ml.math.impls.vector.RandomVectorConstructorTest; -import org.apache.ignite.ml.math.impls.vector.SingleElementVectorConstructorTest; -import org.apache.ignite.ml.math.impls.vector.SingleElementVectorViewConstructorTest; import org.apache.ignite.ml.math.impls.vector.SparseLocalVectorConstructorTest; import org.apache.ignite.ml.math.impls.vector.VectorAttributesTest; import org.apache.ignite.ml.math.impls.vector.VectorFoldMapTest; -import org.apache.ignite.ml.math.impls.vector.VectorImplementationsTest; -import org.apache.ignite.ml.math.impls.vector.VectorIterableTest; import org.apache.ignite.ml.math.impls.vector.VectorNormTest; import org.apache.ignite.ml.math.impls.vector.VectorToMatrixTest; import org.apache.ignite.ml.math.impls.vector.VectorViewTest; @@ -74,20 +55,12 @@ import org.junit.runners.Suite; DenseLocalOnHeapVectorConstructorTest.class, DenseLocalOffHeapVectorConstructorTest.class, SparseLocalVectorConstructorTest.class, - RandomVectorConstructorTest.class, - ConstantVectorConstructorTest.class, - FunctionVectorConstructorTest.class, - SingleElementVectorConstructorTest.class, - PivotedVectorViewConstructorTest.class, - SingleElementVectorViewConstructorTest.class, DelegatingVectorConstructorTest.class, // Various vectors tests. AbstractVectorTest.class, - VectorImplementationsTest.class, VectorViewTest.class, MatrixVectorViewTest.class, // Vector particular features tests. - VectorIterableTest.class, VectorAttributesTest.class, VectorToMatrixTest.class, VectorNormTest.class, @@ -104,23 +77,11 @@ import org.junit.runners.Suite; // Matrix constructors tests. DenseLocalOnHeapMatrixConstructorTest.class, DenseLocalOffHeapMatrixConstructorTest.class, - RandomMatrixConstructorTest.class, - FunctionMatrixConstructorTest.class, MatrixViewConstructorTest.class, - PivotedMatrixViewConstructorTest.class, SparseLocalOnHeapMatrixConstructorTest.class, // Matrix tests. MatrixImplementationsTest.class, - DiagonalMatrixTest.class, MatrixAttributeTest.class, - TransposedMatrixViewTest.class, - // Decompositions. - LUDecompositionTest.class, - EigenDecompositionTest.class, - CholeskyDecompositionTest.class, - QRDecompositionTest.class, - SingularValueDecompositionTest.class, - QRDSolverTest.class, DistanceTest.class, LSQROnHeapTest.class }) http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java index 974b7bb..cd6ae98 100644 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java +++ b/modules/ml/src/test/java/org/apache/ignite/ml/math/MathImplMainTestSuite.java @@ -26,7 +26,6 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ MathImplLocalTestSuite.class, - MathImplDistributedTestSuite.class, TracerTest.class, BlasTest.class }) http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java deleted file mode 100644 index cc726a8..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/CholeskyDecompositionTest.java +++ /dev/null @@ -1,160 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.exceptions.NonPositiveDefiniteMatrixException; -import org.apache.ignite.ml.math.exceptions.NonSymmetricMatrixException; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -/** */ -public class CholeskyDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - basicTest(new PivotedMatrixView(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }))); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new CholeskyDecomposition(null); - } - - /** */ - @Test(expected = CardinalityException.class) - public void wrongMatrixSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - @Test(expected = NonSymmetricMatrixException.class) - public void nonSymmetricMatrixTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 10.0d}, - {-1.0d, 2.0d, -1.0d}, - {-10.0d, -1.0d, 2.0d} - })); - } - - /** */ - @Test(expected = NonPositiveDefiniteMatrixException.class) - public void nonAbsPositiveMatrixTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 0.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveWrongVectorSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapVector(2)); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveWrongMatrixSizeTest() { - new CholeskyDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - private void basicTest(Matrix m) { - // This decomposition is useful when dealing with systems of linear equations of the form - // m x = b where m is a Hermitian matrix. - // For such systems Cholesky decomposition provides - // more effective method of solving compared to LU decomposition. - // Suppose we want to solve system - // m x = b for various bs. Then after we computed Cholesky decomposition, we can feed various bs - // as a matrix of the form - // (b1, b2, ..., bm) - // to the method Cholesky::solve which returns solutions in the form - // (sol1, sol2, ..., solm) - CholeskyDecomposition dec = new CholeskyDecomposition(m); - assertEquals("Unexpected value for decomposition determinant.", - 4d, dec.getDeterminant(), 0d); - - Matrix l = dec.getL(); - Matrix lt = dec.getLT(); - - assertNotNull("Matrix l is expected to be not null.", l); - assertNotNull("Matrix lt is expected to be not null.", lt); - - for (int row = 0; row < l.rowSize(); row++) - for (int col = 0; col < l.columnSize(); col++) - assertEquals("Unexpected value transposed matrix at (" + row + "," + col + ").", - l.get(row, col), lt.get(col, row), 0d); - - Matrix bs = new DenseLocalOnHeapMatrix(new double[][] { - {4.0, -6.0, 7.0}, - {1.0, 1.0, 1.0} - }).transpose(); - Matrix sol = dec.solve(bs); - - assertNotNull("Solution matrix is expected to be not null.", sol); - assertEquals("Solution rows are not as expected.", bs.rowSize(), sol.rowSize()); - assertEquals("Solution columns are not as expected.", bs.columnSize(), sol.columnSize()); - - for (int i = 0; i < sol.columnSize(); i++) - assertNotNull("Solution matrix column is expected to be not null at index " + i, sol.viewColumn(i)); - - Vector b = new DenseLocalOnHeapVector(new double[] {4.0, -6.0, 7.0}); - Vector solVec = dec.solve(b); - - for (int idx = 0; idx < b.size(); idx++) - assertEquals("Unexpected value solution vector at " + idx, - b.get(idx), solVec.get(idx), 0d); - - dec.destroy(); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java deleted file mode 100644 index 76aca0b..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/EigenDecompositionTest.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.ml.math.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Tests for {@link EigenDecomposition} - */ -public class EigenDecompositionTest { - /** */ - private static final double EPSILON = 1e-11; - - /** */ - @Test - public void testMatrixWithRealEigenvalues() { - test(new double[][] { - {1.0d, 0.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d, 0.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d}}, - new double[] {1, 2, 2, 1}); - } - - /** */ - @Test - public void testSymmetricMatrix() { - EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d, 1.0d}, - {0.0d, 1.0d, 0.0d, 1.0d}, - {0.0d, 0.0d, 2.0d, 0.0d}, - {1.0d, 1.0d, 0.0d, 2.0d}})); - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertNotNull("Matrix d is expected to be not null.", d); - assertNotNull("Matrix v is expected to be not null.", v); - - assertEquals("Unexpected rows in d matrix.", 4, d.rowSize()); - assertEquals("Unexpected cols in d matrix.", 4, d.columnSize()); - - assertEquals("Unexpected rows in v matrix.", 4, v.rowSize()); - assertEquals("Unexpected cols in v matrix.", 4, v.columnSize()); - - assertIsDiagonalNonZero(d); - - decomposition.destroy(); - } - - /** */ - @Test - public void testNonSquareMatrix() { - EigenDecomposition decomposition = new EigenDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {1.0d, 0.0d, 0.0d}, - {0.0d, 1.0d, 0.0d}, - {0.0d, 0.0d, 2.0d}, - {1.0d, 1.0d, 0.0d}})); - // TODO: IGNITE-5828, find out why decomposition of 3X4 matrix throws row index exception - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertNotNull("Matrix d is expected to be not null.", d); - assertNotNull("Matrix v is expected to be not null.", v); - - assertEquals("Unexpected rows in d matrix.", 4, d.rowSize()); - assertEquals("Unexpected cols in d matrix.", 4, d.columnSize()); - - assertEquals("Unexpected rows in v matrix.", 4, v.rowSize()); - assertEquals("Unexpected cols in v matrix.", 3, v.columnSize()); - - assertIsDiagonal(d, true); - - decomposition.destroy(); - } - - /** */ - private void test(double[][] mRaw, double[] expRealEigenValues) { - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(mRaw); - EigenDecomposition decomposition = new EigenDecomposition(m); - - Matrix d = decomposition.getD(); - Matrix v = decomposition.getV(); - - assertIsDiagonalNonZero(d); - - // check that d's diagonal consists of eigenvalues of m. - assertDiagonalConsistsOfEigenvalues(m, d, v); - - // m = v d v^{-1} is equivalent to - // m v = v d - assertMatricesAreEqual(m.times(v), v.times(d)); - - assertEigenvalues(decomposition, expRealEigenValues); - - decomposition.destroy(); - } - - /** */ - private void assertEigenvalues(EigenDecomposition decomposition, double[] expRealEigenValues) { - Vector real = decomposition.getRealEigenValues(); - Vector imag = decomposition.getImagEigenvalues(); - - assertEquals("Real values size differs from expected.", expRealEigenValues.length, real.size()); - assertEquals("Imag values size differs from expected.", expRealEigenValues.length, imag.size()); - - for (int idx = 0; idx < expRealEigenValues.length; idx++) { - assertEquals("Real eigen value differs from expected at " + idx, - expRealEigenValues[idx], real.get(idx), 0d); - - assertEquals("Imag eigen value differs from expected at " + idx, - 0d, imag.get(idx), 0d); - } - - } - - /** */ - private void assertDiagonalConsistsOfEigenvalues(DenseLocalOnHeapMatrix m, Matrix d, Matrix v) { - int n = m.columnSize(); - for (int i = 0; i < n; i++) { - Vector eigenVector = v.viewColumn(i); - double eigenVal = d.getX(i, i); - assertVectorsAreEqual(m.times(eigenVector), eigenVector.times(eigenVal)); - } - - } - - /** */ - private void assertMatricesAreEqual(Matrix exp, Matrix actual) { - assertTrue("The row sizes of matrices are not equal", exp.rowSize() == actual.rowSize()); - assertTrue("The col sizes of matrices are not equal", exp.columnSize() == actual.columnSize()); - - // Since matrix is square, we need only one dimension - int n = exp.columnSize(); - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - assertEquals("Values should be equal", exp.getX(i, j), actual.getX(i, j), EPSILON); - } - - /** */ - private void assertVectorsAreEqual(Vector exp, Vector actual) { - assertTrue("Vectors sizes are not equal", exp.size() == actual.size()); - - // Since matrix is square, we need only one dimension - int n = exp.size(); - - for (int i = 0; i < n; i++) - assertEquals("Values should be equal", exp.getX(i), actual.getX(i), EPSILON); - } - - /** */ - private void assertIsDiagonalNonZero(Matrix m) { - assertIsDiagonal(m, false); - } - - /** */ - private void assertIsDiagonal(Matrix m, boolean zeroesAllowed) { - // Since matrix is square, we need only one dimension - int n = m.columnSize(); - - assertEquals("Diagonal matrix is not square", n, m.rowSize()); - - for (int i = 0; i < n; i++) - for (int j = 0; j < n; j++) - assertTrue("Matrix is not diagonal, violation at (" + i + "," + j + ")", - ((i == j) && (zeroesAllowed || m.getX(i, j) != 0)) - || ((i != j) && m.getX(i, j) == 0)); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java deleted file mode 100644 index 8e8b920..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/LUDecompositionTest.java +++ /dev/null @@ -1,252 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.Vector; -import org.apache.ignite.ml.math.exceptions.CardinalityException; -import org.apache.ignite.ml.math.exceptions.SingularMatrixException; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Tests for {@link LUDecomposition}. - */ -public class LUDecompositionTest { - /** */ - private Matrix testL; - /** */ - private Matrix testU; - /** */ - private Matrix testP; - /** */ - private Matrix testMatrix; - /** */ - private int[] rawPivot; - - /** */ - @Before - public void setUp() { - double[][] rawMatrix = new double[][] { - {2.0d, 1.0d, 1.0d, 0.0d}, - {4.0d, 3.0d, 3.0d, 1.0d}, - {8.0d, 7.0d, 9.0d, 5.0d}, - {6.0d, 7.0d, 9.0d, 8.0d}}; - double[][] rawL = { - {1.0d, 0.0d, 0.0d, 0.0d}, - {3.0d / 4.0d, 1.0d, 0.0d, 0.0d}, - {1.0d / 2.0d, -2.0d / 7.0d, 1.0d, 0.0d}, - {1.0d / 4.0d, -3.0d / 7.0d, 1.0d / 3.0d, 1.0d}}; - double[][] rawU = { - {8.0d, 7.0d, 9.0d, 5.0d}, - {0.0d, 7.0d / 4.0d, 9.0d / 4.0d, 17.0d / 4.0d}, - {0.0d, 0.0d, -6.0d / 7.0d, -2.0d / 7.0d}, - {0.0d, 0.0d, 0.0d, 2.0d / 3.0d}}; - double[][] rawP = new double[][] { - {0, 0, 1.0d, 0}, - {0, 0, 0, 1.0d}, - {0, 1.0d, 0, 0}, - {1.0d, 0, 0, 0}}; - - rawPivot = new int[] {3, 4, 2, 1}; - - testMatrix = new DenseLocalOnHeapMatrix(rawMatrix); - testL = new DenseLocalOnHeapMatrix(rawL); - testU = new DenseLocalOnHeapMatrix(rawU); - testP = new DenseLocalOnHeapMatrix(rawP); - } - - /** */ - @Test - public void getL() throws Exception { - Matrix luDecompositionL = new LUDecomposition(testMatrix).getL(); - - assertEquals("Unexpected row size.", testL.rowSize(), luDecompositionL.rowSize()); - assertEquals("Unexpected column size.", testL.columnSize(), luDecompositionL.columnSize()); - - for (int i = 0; i < testL.rowSize(); i++) - for (int j = 0; j < testL.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testL.getX(i, j), luDecompositionL.getX(i, j), 0.0000001d); - - luDecompositionL.destroy(); - } - - /** */ - @Test - public void getU() throws Exception { - Matrix luDecompositionU = new LUDecomposition(testMatrix).getU(); - - assertEquals("Unexpected row size.", testU.rowSize(), luDecompositionU.rowSize()); - assertEquals("Unexpected column size.", testU.columnSize(), luDecompositionU.columnSize()); - - for (int i = 0; i < testU.rowSize(); i++) - for (int j = 0; j < testU.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testU.getX(i, j), luDecompositionU.getX(i, j), 0.0000001d); - - luDecompositionU.destroy(); - } - - /** */ - @Test - public void getP() throws Exception { - Matrix luDecompositionP = new LUDecomposition(testMatrix).getP(); - - assertEquals("Unexpected row size.", testP.rowSize(), luDecompositionP.rowSize()); - assertEquals("Unexpected column size.", testP.columnSize(), luDecompositionP.columnSize()); - - for (int i = 0; i < testP.rowSize(); i++) - for (int j = 0; j < testP.columnSize(); j++) - assertEquals("Unexpected value at (" + i + "," + j + ").", - testP.getX(i, j), luDecompositionP.getX(i, j), 0.0000001d); - - luDecompositionP.destroy(); - } - - /** */ - @Test - public void getPivot() throws Exception { - Vector pivot = new LUDecomposition(testMatrix).getPivot(); - - assertEquals("Unexpected pivot size.", rawPivot.length, pivot.size()); - - for (int i = 0; i < testU.rowSize(); i++) - assertEquals("Unexpected value at " + i, rawPivot[i], (int)pivot.get(i) + 1); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - LUDecomposition dec = new LUDecomposition(new PivotedMatrixView(testMatrix)); - Matrix luDecompositionL = dec.getL(); - - assertEquals("Unexpected L row size.", testL.rowSize(), luDecompositionL.rowSize()); - assertEquals("Unexpected L column size.", testL.columnSize(), luDecompositionL.columnSize()); - - for (int i = 0; i < testL.rowSize(); i++) - for (int j = 0; j < testL.columnSize(); j++) - assertEquals("Unexpected L value at (" + i + "," + j + ").", - testL.getX(i, j), luDecompositionL.getX(i, j), 0.0000001d); - - Matrix luDecompositionU = dec.getU(); - - assertEquals("Unexpected U row size.", testU.rowSize(), luDecompositionU.rowSize()); - assertEquals("Unexpected U column size.", testU.columnSize(), luDecompositionU.columnSize()); - - for (int i = 0; i < testU.rowSize(); i++) - for (int j = 0; j < testU.columnSize(); j++) - assertEquals("Unexpected U value at (" + i + "," + j + ").", - testU.getX(i, j), luDecompositionU.getX(i, j), 0.0000001d); - - Matrix luDecompositionP = dec.getP(); - - assertEquals("Unexpected P row size.", testP.rowSize(), luDecompositionP.rowSize()); - assertEquals("Unexpected P column size.", testP.columnSize(), luDecompositionP.columnSize()); - - for (int i = 0; i < testP.rowSize(); i++) - for (int j = 0; j < testP.columnSize(); j++) - assertEquals("Unexpected P value at (" + i + "," + j + ").", - testP.getX(i, j), luDecompositionP.getX(i, j), 0.0000001d); - - dec.destroy(); - } - - /** */ - @Test - public void singularDeterminant() throws Exception { - assertEquals("Unexpected determinant for singular matrix decomposition.", - 0d, new LUDecomposition(new DenseLocalOnHeapMatrix(2, 2)).determinant(), 0d); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveVecWrongSize() throws Exception { - new LUDecomposition(testMatrix).solve(new DenseLocalOnHeapVector(testMatrix.rowSize() + 1)); - } - - /** */ - @Test(expected = SingularMatrixException.class) - public void solveVecSingularMatrix() throws Exception { - new LUDecomposition(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())) - .solve(new DenseLocalOnHeapVector(testMatrix.rowSize())); - } - - /** */ - @Test - public void solveVec() throws Exception { - Vector sol = new LUDecomposition(new PivotedMatrixView(testMatrix)) - .solve(new DenseLocalOnHeapVector(testMatrix.rowSize())); - - assertEquals("Wrong solution vector size.", testMatrix.rowSize(), sol.size()); - - for (int i = 0; i < sol.size(); i++) - assertEquals("Unexpected value at index " + i, 0d, sol.getX(i), 0.0000001d); - } - - /** */ - @Test(expected = CardinalityException.class) - public void solveMtxWrongSize() throws Exception { - new LUDecomposition(testMatrix).solve( - new DenseLocalOnHeapMatrix(testMatrix.rowSize() + 1, testMatrix.rowSize())); - } - - /** */ - @Test(expected = SingularMatrixException.class) - public void solveMtxSingularMatrix() throws Exception { - new LUDecomposition(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())) - .solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())); - } - - /** */ - @Test - public void solveMtx() throws Exception { - Matrix sol = new LUDecomposition(new PivotedMatrixView(testMatrix)) - .solve(new DenseLocalOnHeapMatrix(testMatrix.rowSize(), testMatrix.rowSize())); - - assertEquals("Wrong solution matrix row size.", testMatrix.rowSize(), sol.rowSize()); - - assertEquals("Wrong solution matrix column size.", testMatrix.rowSize(), sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected P value at (" + row + "," + col + ").", - 0d, sol.getX(row, col), 0.0000001d); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new LUDecomposition(null); - } - - /** */ - @Test(expected = CardinalityException.class) - public void nonSquareMatrixTest() { - new LUDecomposition(new DenseLocalOnHeapMatrix(2, 3)); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java deleted file mode 100644 index d3e8e76..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDSolverTest.java +++ /dev/null @@ -1,87 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** */ -public class QRDSolverTest { - /** */ - @Test - public void basicTest() { - Matrix m = new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }); - - QRDecomposition dec = new QRDecomposition(m); - assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank()); - - Matrix q = dec.getQ(); - Matrix r = dec.getR(); - - assertNotNull("Matrix q is expected to be not null.", q); - assertNotNull("Matrix r is expected to be not null.", r); - - Matrix qSafeCp = safeCopy(q); - - Matrix expIdentity = qSafeCp.times(qSafeCp.transpose()); - - final double delta = 0.0001; - - for (int row = 0; row < expIdentity.rowSize(); row++) - for (int col = 0; col < expIdentity.columnSize(); col++) - assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", - row == col ? 1d : 0d, expIdentity.get(col, row), delta); - - for (int row = 0; row < r.rowSize(); row++) - for (int col = 0; col < row - 1; col++) - assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", - 0d, r.get(row, col), delta); - - Matrix recomposed = qSafeCp.times(r); - - for (int row = 0; row < m.rowSize(); row++) - for (int col = 0; col < m.columnSize(); col++) - assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", - m.get(row, col), recomposed.get(row, col), delta); - - Matrix sol = new QRDSolver(q, r).solve(new DenseLocalOnHeapMatrix(3, 10)); - assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize()); - assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", - 0d, sol.get(row, col), delta); - - dec.destroy(); - } - - /** */ - private Matrix safeCopy(Matrix orig) { - return new DenseLocalOnHeapMatrix(orig.rowSize(), orig.columnSize()).assign(orig); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java deleted file mode 100644 index a3b083f..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/QRDecompositionTest.java +++ /dev/null @@ -1,141 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** */ -public class QRDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - basicTest(new PivotedMatrixView(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }))); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new QRDecomposition(null); - } - - /** */ - @Test(expected = IllegalArgumentException.class) - public void solveWrongMatrixSizeTest() { - new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })).solve(new DenseLocalOnHeapMatrix(2, 3)); - } - - /** */ - private void basicTest(Matrix m) { - QRDecomposition dec = new QRDecomposition(m); - assertTrue("Unexpected value for full rank in decomposition " + dec, dec.hasFullRank()); - - Matrix q = dec.getQ(); - Matrix r = dec.getR(); - - assertNotNull("Matrix q is expected to be not null.", q); - assertNotNull("Matrix r is expected to be not null.", r); - - Matrix qSafeCp = safeCopy(q); - - Matrix expIdentity = qSafeCp.times(qSafeCp.transpose()); - - final double delta = 0.0001; - - for (int row = 0; row < expIdentity.rowSize(); row++) - for (int col = 0; col < expIdentity.columnSize(); col++) - assertEquals("Unexpected identity matrix value at (" + row + "," + col + ").", - row == col ? 1d : 0d, expIdentity.get(col, row), delta); - - for (int row = 0; row < r.rowSize(); row++) - for (int col = 0; col < row - 1; col++) - assertEquals("Unexpected upper triangular matrix value at (" + row + "," + col + ").", - 0d, r.get(row, col), delta); - - Matrix recomposed = qSafeCp.times(r); - - for (int row = 0; row < m.rowSize(); row++) - for (int col = 0; col < m.columnSize(); col++) - assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", - m.get(row, col), recomposed.get(row, col), delta); - - Matrix sol = dec.solve(new DenseLocalOnHeapMatrix(3, 10)); - assertEquals("Unexpected rows in solution matrix.", 3, sol.rowSize()); - assertEquals("Unexpected cols in solution matrix.", 10, sol.columnSize()); - - for (int row = 0; row < sol.rowSize(); row++) - for (int col = 0; col < sol.columnSize(); col++) - assertEquals("Unexpected solution matrix value at (" + row + "," + col + ").", - 0d, sol.get(row, col), delta); - - dec.destroy(); - - QRDecomposition dec1 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d}, - {-1.0d, 2.0d}, - {0.0d, -1.0d} - })); - - assertTrue("Unexpected value for full rank in decomposition " + dec1, dec1.hasFullRank()); - - dec1.destroy(); - - QRDecomposition dec2 = new QRDecomposition(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d, 0.0d}, - {0.0d, -1.0d, 2.0d, 0.0d} - })); - - assertTrue("Unexpected value for full rank in decomposition " + dec2, dec2.hasFullRank()); - - dec2.destroy(); - } - - /** */ - private Matrix safeCopy(Matrix orig) { - return new DenseLocalOnHeapMatrix(orig.rowSize(), orig.columnSize()).assign(orig); - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/9dccb3db/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java ---------------------------------------------------------------------- diff --git a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java b/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java deleted file mode 100644 index 00e83d8..0000000 --- a/modules/ml/src/test/java/org/apache/ignite/ml/math/decompositions/SingularValueDecompositionTest.java +++ /dev/null @@ -1,122 +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.decompositions; - -import org.apache.ignite.ml.math.Matrix; -import org.apache.ignite.ml.math.impls.matrix.DenseLocalOnHeapMatrix; -import org.apache.ignite.ml.math.impls.matrix.PivotedMatrixView; -import org.apache.ignite.ml.math.util.MatrixUtil; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** */ -public class SingularValueDecompositionTest { - /** */ - @Test - public void basicTest() { - basicTest(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - })); - } - - /** - * Test for {@link MatrixUtil} features (more specifically, we test matrix which does not have - * a native like/copy methods support). - */ - @Test - public void matrixUtilTest() { - basicTest(new PivotedMatrixView(new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d}, - {0.0d, -1.0d, 2.0d} - }))); - } - - /** */ - @Test - public void rowsLessThanColumnsTest() { - DenseLocalOnHeapMatrix m = new DenseLocalOnHeapMatrix(new double[][] { - {2.0d, -1.0d, 0.0d}, - {-1.0d, 2.0d, -1.0d} - }); - - SingularValueDecomposition dec = new SingularValueDecomposition(m); - assertEquals("Unexpected value for singular values size.", - 2, dec.getSingularValues().length); - - Matrix s = dec.getS(); - Matrix u = dec.getU(); - Matrix v = dec.getV(); - Matrix covariance = dec.getCovariance(0.5); - - assertNotNull("Matrix s is expected to be not null.", s); - assertNotNull("Matrix u is expected to be not null.", u); - assertNotNull("Matrix v is expected to be not null.", v); - assertNotNull("Covariance matrix is expected to be not null.", covariance); - - dec.destroy(); - } - - /** */ - @Test(expected = AssertionError.class) - public void nullMatrixTest() { - new SingularValueDecomposition(null); - } - - /** */ - private void basicTest(Matrix m) { - SingularValueDecomposition dec = new SingularValueDecomposition(m); - assertEquals("Unexpected value for singular values size.", - 3, dec.getSingularValues().length); - - Matrix s = dec.getS(); - Matrix u = dec.getU(); - Matrix v = dec.getV(); - Matrix covariance = dec.getCovariance(0.5); - - assertNotNull("Matrix s is expected to be not null.", s); - assertNotNull("Matrix u is expected to be not null.", u); - assertNotNull("Matrix v is expected to be not null.", v); - assertNotNull("Covariance matrix is expected to be not null.", covariance); - - assertTrue("Decomposition cond is expected to be positive.", dec.cond() > 0); - assertTrue("Decomposition norm2 is expected to be positive.", dec.norm2() > 0); - assertEquals("Decomposition rank differs from expected.", 3, dec.rank()); - assertEquals("Decomposition singular values size differs from expected.", - 3, dec.getSingularValues().length); - - Matrix recomposed = (u.times(s).times(v.transpose())); - - for (int row = 0; row < m.rowSize(); row++) - for (int col = 0; col < m.columnSize(); col++) - assertEquals("Unexpected recomposed matrix value at (" + row + "," + col + ").", - m.get(row, col), recomposed.get(row, col), 0.001); - - for (int row = 0; row < covariance.rowSize(); row++) - for (int col = row + 1; col < covariance.columnSize(); col++) - assertEquals("Unexpected covariance matrix value at (" + row + "," + col + ").", - covariance.get(row, col), covariance.get(col, row), 0.001); - - dec.destroy(); - } -}
