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
18package org.apache.spark.ml.linalg
19
20import breeze.linalg.{CSCMatrix => BSM, DenseMatrix => BDM}
21
22import org.apache.spark.ml.SparkMLFunSuite
23
24class BreezeMatrixConversionSuite extends SparkMLFunSuite {
25  test("dense matrix to breeze") {
26    val mat = Matrices.dense(3, 2, Array(0.0, 1.0, 2.0, 3.0, 4.0, 5.0))
27    val breeze = mat.asBreeze.asInstanceOf[BDM[Double]]
28    assert(breeze.rows === mat.numRows)
29    assert(breeze.cols === mat.numCols)
30    assert(breeze.data.eq(mat.asInstanceOf[DenseMatrix].values), "should not copy data")
31  }
32
33  test("dense breeze matrix to matrix") {
34    val breeze = new BDM[Double](3, 2, Array(0.0, 1.0, 2.0, 3.0, 4.0, 5.0))
35    val mat = Matrices.fromBreeze(breeze).asInstanceOf[DenseMatrix]
36    assert(mat.numRows === breeze.rows)
37    assert(mat.numCols === breeze.cols)
38    assert(mat.values.eq(breeze.data), "should not copy data")
39    // transposed matrix
40    val matTransposed = Matrices.fromBreeze(breeze.t).asInstanceOf[DenseMatrix]
41    assert(matTransposed.numRows === breeze.cols)
42    assert(matTransposed.numCols === breeze.rows)
43    assert(matTransposed.values.eq(breeze.data), "should not copy data")
44  }
45
46  test("sparse matrix to breeze") {
47    val values = Array(1.0, 2.0, 4.0, 5.0)
48    val colPtrs = Array(0, 2, 4)
49    val rowIndices = Array(1, 2, 1, 2)
50    val mat = Matrices.sparse(3, 2, colPtrs, rowIndices, values)
51    val breeze = mat.asBreeze.asInstanceOf[BSM[Double]]
52    assert(breeze.rows === mat.numRows)
53    assert(breeze.cols === mat.numCols)
54    assert(breeze.data.eq(mat.asInstanceOf[SparseMatrix].values), "should not copy data")
55  }
56
57  test("sparse breeze matrix to sparse matrix") {
58    val values = Array(1.0, 2.0, 4.0, 5.0)
59    val colPtrs = Array(0, 2, 4)
60    val rowIndices = Array(1, 2, 1, 2)
61    val breeze = new BSM[Double](values, 3, 2, colPtrs, rowIndices)
62    val mat = Matrices.fromBreeze(breeze).asInstanceOf[SparseMatrix]
63    assert(mat.numRows === breeze.rows)
64    assert(mat.numCols === breeze.cols)
65    assert(mat.values.eq(breeze.data), "should not copy data")
66    val matTransposed = Matrices.fromBreeze(breeze.t).asInstanceOf[SparseMatrix]
67    assert(matTransposed.numRows === breeze.cols)
68    assert(matTransposed.numCols === breeze.rows)
69    assert(!matTransposed.values.eq(breeze.data), "has to copy data")
70  }
71}
72