Skip to content

Commit 7260df6

Browse files
committed
[SPARK-10757] [MLlib] Add test cases - Java friendly constructor for distributed matrices
1 parent 16cdc90 commit 7260df6

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.apache.spark.mllib.linalg.distributed;
2+
3+
import org.apache.spark.api.java.JavaSparkContext;
4+
import org.apache.spark.mllib.linalg.DenseMatrix;
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import scala.Tuple2;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class JavaBlockMatrixSuite {
16+
private transient JavaSparkContext sc;
17+
18+
@Before
19+
public void setUp() {
20+
sc = new JavaSparkContext("local", "JavaBlockMatrixSuite");
21+
}
22+
23+
@After
24+
public void tearDown() {
25+
sc.stop();
26+
sc = null;
27+
}
28+
29+
@Test
30+
public void blockMatrixConstruction() {
31+
List<Tuple2<Tuple2<Integer, Integer>, DenseMatrix>> blocks = Arrays.asList(
32+
new Tuple2<Tuple2<Integer, Integer>, DenseMatrix>(
33+
new Tuple2<Integer, Integer>(0, 0), new DenseMatrix(2, 2, new double[]{1.0, 0.0, 0.0, 2.0})),
34+
new Tuple2<Tuple2<Integer, Integer>, DenseMatrix>(
35+
new Tuple2<Integer, Integer>(0, 1), new DenseMatrix(2, 2, new double[]{0.0, 1.0, 0.0, 0.0})),
36+
new Tuple2<Tuple2<Integer, Integer>, DenseMatrix>(
37+
new Tuple2<Integer, Integer>(1, 0), new DenseMatrix(2, 2, new double[]{3.0, 0.0, 1.0, 1.0})),
38+
new Tuple2<Tuple2<Integer, Integer>, DenseMatrix>(
39+
new Tuple2<Integer, Integer>(1, 1), new DenseMatrix(2, 2, new double[]{1.0, 2.0, 0.0, 1.0})),
40+
new Tuple2<Tuple2<Integer, Integer>, DenseMatrix>(
41+
new Tuple2<Integer, Integer>(2, 1), new DenseMatrix(1, 2, new double[]{1.0, 5.0})));
42+
BlockMatrix blockMatrix = BlockMatrix.from(sc.parallelize(blocks), 2, 2);
43+
final DenseMatrix expectedMatrix = new DenseMatrix(5, 4, new double[]{
44+
1.0, 0.0, 0.0, 0.0,
45+
0.0, 2.0, 1.0, 0.0,
46+
3.0, 1.0, 1.0, 0.0,
47+
0.0, 1.0, 2.0, 1.0,
48+
0.0, 0.0, 1.0, 5.0}, true);
49+
assertEquals(5L, blockMatrix.numRows());
50+
assertEquals(4L, blockMatrix.numCols());
51+
assertEquals(expectedMatrix, blockMatrix.toLocalMatrix());
52+
}
53+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.apache.spark.mllib.linalg.distributed;
2+
3+
import org.apache.spark.api.java.JavaSparkContext;
4+
import org.apache.spark.mllib.linalg.DenseMatrix;
5+
import org.apache.spark.mllib.linalg.DenseVector;
6+
import org.junit.After;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
public class JavaRowMatrixSuite {
16+
private transient JavaSparkContext sc;
17+
18+
@Before
19+
public void setUp() {
20+
sc = new JavaSparkContext("local", "JavaRowMatrixSuite");
21+
}
22+
23+
@After
24+
public void tearDown() {
25+
sc.stop();
26+
sc = null;
27+
}
28+
29+
@Test
30+
public void rowMatrixConstruction() {
31+
List<DenseVector> rows = Arrays.asList(
32+
new DenseVector(new double[]{1.0, 0.0, 0.0, 0.0}),
33+
new DenseVector(new double[]{0.0, 2.0, 1.0, 0.0}),
34+
new DenseVector(new double[]{3.0, 1.0, 1.0, 0.0}),
35+
new DenseVector(new double[]{0.0, 1.0, 2.0, 1.0}),
36+
new DenseVector(new double[]{0.0, 0.0, 1.0, 5.0}));
37+
RowMatrix rowMatrix = RowMatrix.from(sc.parallelize(rows));
38+
final DenseMatrix expectedMatrix = new DenseMatrix(5, 4, new double[]{
39+
1.0, 0.0, 0.0, 0.0,
40+
0.0, 2.0, 1.0, 0.0,
41+
3.0, 1.0, 1.0, 0.0,
42+
0.0, 1.0, 2.0, 1.0,
43+
0.0, 0.0, 1.0, 5.0}, true);
44+
assertEquals(5L, rowMatrix.numRows());
45+
assertEquals(4L, rowMatrix.numCols());
46+
assertEquals(expectedMatrix.toBreeze(), rowMatrix.toBreeze());
47+
}
48+
}

0 commit comments

Comments
 (0)