|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.mllib.linalg; |
| 19 | + |
| 20 | +import static org.junit.Assert.*; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +import java.io.Serializable; |
| 24 | + |
| 25 | +public class JavaMatricesSuite implements Serializable { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void randMatrixConstruction() { |
| 29 | + Matrix r = Matrices.rand(3, 4, 24); |
| 30 | + DenseMatrix dr = DenseMatrix.rand(3, 4, 24); |
| 31 | + assertArrayEquals(r.toArray(), dr.toArray(), 0.0); |
| 32 | + |
| 33 | + Matrix rn = Matrices.randn(3, 4, 24); |
| 34 | + DenseMatrix drn = DenseMatrix.randn(3, 4, 24); |
| 35 | + assertArrayEquals(rn.toArray(), drn.toArray(), 0.0); |
| 36 | + |
| 37 | + Matrix s = Matrices.sprand(3, 4, 0.5, 24); |
| 38 | + SparseMatrix sr = SparseMatrix.sprand(3, 4, 0.5, 24); |
| 39 | + assertArrayEquals(s.toArray(), sr.toArray(), 0.0); |
| 40 | + |
| 41 | + Matrix sn = Matrices.sprandn(3, 4, 0.5, 24); |
| 42 | + SparseMatrix srn = SparseMatrix.sprandn(3, 4, 0.5, 24); |
| 43 | + assertArrayEquals(sn.toArray(), srn.toArray(), 0.0); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void identityMatrixConstruction() { |
| 48 | + Matrix r = Matrices.eye(2); |
| 49 | + DenseMatrix dr = DenseMatrix.eye(2); |
| 50 | + SparseMatrix sr = SparseMatrix.speye(2); |
| 51 | + assertArrayEquals(r.toArray(), dr.toArray(), 0.0); |
| 52 | + assertArrayEquals(sr.toArray(), dr.toArray(), 0.0); |
| 53 | + assertArrayEquals(r.toArray(), new double[]{1.0, 0.0, 0.0, 1.0}, 0.0); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void diagonalMatrixConstruction() { |
| 58 | + Vector v = Vectors.dense(1.0, 0.0, 2.0); |
| 59 | + Vector sv = Vectors.sparse(3, new int[]{0, 2}, new double[]{1.0, 2.0}); |
| 60 | + |
| 61 | + Matrix m = Matrices.diag(v); |
| 62 | + Matrix sm = Matrices.diag(sv); |
| 63 | + DenseMatrix d = DenseMatrix.diag(v); |
| 64 | + DenseMatrix sd = DenseMatrix.diag(sv); |
| 65 | + SparseMatrix s = SparseMatrix.diag(v); |
| 66 | + SparseMatrix ss = SparseMatrix.diag(sv); |
| 67 | + |
| 68 | + assertArrayEquals(m.toArray(), sm.toArray(), 0.0); |
| 69 | + assertArrayEquals(d.toArray(), sm.toArray(), 0.0); |
| 70 | + assertArrayEquals(d.toArray(), sd.toArray(), 0.0); |
| 71 | + assertArrayEquals(sd.toArray(), s.toArray(), 0.0); |
| 72 | + assertArrayEquals(s.toArray(), ss.toArray(), 0.0); |
| 73 | + assertArrayEquals(s.values(), ss.values(), 0.0); |
| 74 | + assert(s.values().length == 2); |
| 75 | + assert(ss.values().length == 2); |
| 76 | + assert(s.colPtrs().length == 2); |
| 77 | + assert(ss.colPtrs().length == 2); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void zerosMatrixConstruction() { |
| 82 | + Matrix z = Matrices.zeros(2, 2); |
| 83 | + Matrix one = Matrices.ones(2, 2); |
| 84 | + DenseMatrix dz = DenseMatrix.zeros(2, 2); |
| 85 | + DenseMatrix done = DenseMatrix.ones(2, 2); |
| 86 | + |
| 87 | + assertArrayEquals(z.toArray(), new double[]{0.0, 0.0, 0.0, 0.0}, 0.0); |
| 88 | + assertArrayEquals(dz.toArray(), new double[]{0.0, 0.0, 0.0, 0.0}, 0.0); |
| 89 | + assertArrayEquals(one.toArray(), new double[]{1.0, 1.0, 1.0, 1.0}, 0.0); |
| 90 | + assertArrayEquals(done.toArray(), new double[]{1.0, 1.0, 1.0, 1.0}, 0.0); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void concatenateMatrices() { |
| 95 | + int m = 3; |
| 96 | + int n = 2; |
| 97 | + |
| 98 | + SparseMatrix spMat1 = SparseMatrix.sprand(m, n, 0.5, 42); |
| 99 | + DenseMatrix deMat1 = DenseMatrix.rand(m, n, 42); |
| 100 | + Matrix deMat2 = Matrices.eye(3); |
| 101 | + Matrix spMat2 = Matrices.speye(3); |
| 102 | + Matrix deMat3 = Matrices.eye(2); |
| 103 | + Matrix spMat3 = Matrices.speye(2); |
| 104 | + |
| 105 | + Matrix spHorz = Matrices.horzcat(new Matrix[]{spMat1, spMat2}); |
| 106 | + Matrix deHorz1 = Matrices.horzcat(new Matrix[]{deMat1, deMat2}); |
| 107 | + Matrix deHorz2 = Matrices.horzcat(new Matrix[]{spMat1, deMat2}); |
| 108 | + Matrix deHorz3 = Matrices.horzcat(new Matrix[]{deMat1, spMat2}); |
| 109 | + |
| 110 | + assert(deHorz1.numRows() == 3); |
| 111 | + assert(deHorz2.numRows() == 3); |
| 112 | + assert(deHorz3.numRows() == 3); |
| 113 | + assert(spHorz.numRows() == 3); |
| 114 | + assert(deHorz1.numCols() == 5); |
| 115 | + assert(deHorz2.numCols() == 5); |
| 116 | + assert(deHorz3.numCols() == 5); |
| 117 | + assert(spHorz.numCols() == 5); |
| 118 | + |
| 119 | + Matrix spVert = Matrices.vertcat(new Matrix[]{spMat1, spMat3}); |
| 120 | + Matrix deVert1 = Matrices.vertcat(new Matrix[]{deMat1, deMat3}); |
| 121 | + Matrix deVert2 = Matrices.vertcat(new Matrix[]{spMat1, deMat3}); |
| 122 | + Matrix deVert3 = Matrices.vertcat(new Matrix[]{deMat1, spMat3}); |
| 123 | + |
| 124 | + assert(deVert1.numRows() == 5); |
| 125 | + assert(deVert2.numRows() == 5); |
| 126 | + assert(deVert3.numRows() == 5); |
| 127 | + assert(spVert.numRows() == 5); |
| 128 | + assert(deVert1.numCols() == 2); |
| 129 | + assert(deVert2.numCols() == 2); |
| 130 | + assert(deVert3.numCols() == 2); |
| 131 | + assert(spVert.numCols() == 2); |
| 132 | + } |
| 133 | +} |
0 commit comments