From e25c6917a8fdc07d662aacc9b76797de63fddaa9 Mon Sep 17 00:00:00 2001 From: Songxiao Zhang Date: Mon, 3 Jul 2023 23:26:04 -0400 Subject: [PATCH 1/3] Add DMatrixRMaj#get2DData interface --- .../src/org/ejml/data/DMatrixRMaj.java | 10 ++++++++++ .../src/org/ejml/ops/DConvertArrays.java | 18 ++++++++++++++++++ .../test/org/ejml/data/TestDMatrixRMaj.java | 11 +++++++++++ .../test/org/ejml/ops/TestDConvertArrays.java | 13 +++++++++++++ 4 files changed, 52 insertions(+) diff --git a/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java b/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java index ee76e534c..a578f185e 100644 --- a/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java +++ b/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java @@ -374,4 +374,14 @@ public void fill( double value ) { public void set( double[][] input ) { DConvertArrays.convert(input, this); } + + /** + * Export this matrix using a 2D array representation. + * + * @return 2D representation of the matrix + * @see DMatrixD1#getData() to get a 1D array representation + */ + public double[][] get2DData() { + return DConvertArrays.convert(this); + } } diff --git a/main/ejml-core/src/org/ejml/ops/DConvertArrays.java b/main/ejml-core/src/org/ejml/ops/DConvertArrays.java index 9648e7661..51a6e937f 100644 --- a/main/ejml-core/src/org/ejml/ops/DConvertArrays.java +++ b/main/ejml-core/src/org/ejml/ops/DConvertArrays.java @@ -58,6 +58,24 @@ public static DMatrixRMaj convert( double[][] src, @Nullable DMatrixRMaj dst ) { return dst; } + /** + * Convert a {@link DMatrixRMaj} to a two-dimensional array, + * given DMatrixRMaj can take a double[][] as input to constructor + * + * @param src is an input DMatrixRMaj + * @return a 2D array contains the same elements as the input matrix + */ + public static double[][] convert( DMatrixRMaj src ) { + double[][] array = new double[src.numRows][src.numCols]; + for (int row = 0; row < src.numRows; row++) { + for (int column = 0; column < src.numCols; column++) { + array[row][column] = src.get(row, column); + } + } + + return array; + } + // public static DMatrixSparseCSC convert(double[][]src , @Nullable DMatrixSparseCSC dst ) { // int rows = src.length; // if( rows == 0 ) diff --git a/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java b/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java index 16ae6e486..3ab92c16e 100644 --- a/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java +++ b/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java @@ -204,4 +204,15 @@ protected DMatrixD1 createMatrix( int numRows, int numCols ) { assertTrue(Math.abs(mat.data[i] - orig.data[i]) > UtilEjml.TEST_F64); } } + + @Test void get2DData() { + double[][] expected = {{0, 1}, {2, 3}}; + double[][] actual = new DMatrixRMaj(expected).get2DData(); + + assertArrayEquals(expected, actual); + assertEquals(0,actual[0][0], UtilEjml.TEST_F64); + assertEquals(1,actual[0][1], UtilEjml.TEST_F64); + assertEquals(2,actual[1][0], UtilEjml.TEST_F64); + assertEquals(3,actual[1][1], UtilEjml.TEST_F64); + } } diff --git a/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java b/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java index 4aa6413ee..d201fbad3 100644 --- a/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java +++ b/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java @@ -24,6 +24,7 @@ import org.ejml.data.DMatrixRMaj; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; public class TestDConvertArrays extends EjmlStandardJUnit { @@ -37,6 +38,18 @@ public void dd_to_ddrm() { assertEquals(3,m.get(1,1), UtilEjml.TEST_F64); } + @Test + public void ddrm_to_dd() { + double[][] expected = {{0, 1}, {2, 3}}; + double[][] dd = DConvertArrays.convert(new DMatrixRMaj(expected)); + + assertArrayEquals(expected, dd); + assertEquals(0,dd[0][0], UtilEjml.TEST_F64); + assertEquals(1,dd[0][1], UtilEjml.TEST_F64); + assertEquals(2,dd[1][0], UtilEjml.TEST_F64); + assertEquals(3,dd[1][1], UtilEjml.TEST_F64); + } + // @Test // public void dd_to_dscc() { // DMatrixSparseCSC m = ConvertDArrays.convert(new double[][]{{0,1},{2,3}},(DMatrixSparseCSC) null); From 2ca689ec1fe2ec71a9f44ce310e1a88b28dce711 Mon Sep 17 00:00:00 2001 From: Songxiao Zhang Date: Fri, 7 Jul 2023 22:58:01 -0400 Subject: [PATCH 2/3] use unsafe_get and run spotless --- main/ejml-core/src/org/ejml/data/DMatrixRMaj.java | 2 +- main/ejml-core/src/org/ejml/ops/DConvertArrays.java | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java b/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java index a578f185e..7eeb336c4 100644 --- a/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java +++ b/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Peter Abeles. All Rights Reserved. + * Copyright (c) 2023, Peter Abeles. All Rights Reserved. * * This file is part of Efficient Java Matrix Library (EJML). * diff --git a/main/ejml-core/src/org/ejml/ops/DConvertArrays.java b/main/ejml-core/src/org/ejml/ops/DConvertArrays.java index 51a6e937f..34f579c0e 100644 --- a/main/ejml-core/src/org/ejml/ops/DConvertArrays.java +++ b/main/ejml-core/src/org/ejml/ops/DConvertArrays.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Peter Abeles. All Rights Reserved. + * Copyright (c) 2023, Peter Abeles. All Rights Reserved. * * This file is part of Efficient Java Matrix Library (EJML). * @@ -15,7 +15,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.ejml.ops; import org.ejml.UtilEjml; @@ -69,7 +68,7 @@ public static double[][] convert( DMatrixRMaj src ) { double[][] array = new double[src.numRows][src.numCols]; for (int row = 0; row < src.numRows; row++) { for (int column = 0; column < src.numCols; column++) { - array[row][column] = src.get(row, column); + array[row][column] = src.unsafe_get(row, column); } } From 3dba7eed5da722622318870126c968711bdfc04a Mon Sep 17 00:00:00 2001 From: Songxiao Zhang Date: Fri, 7 Jul 2023 23:03:50 -0400 Subject: [PATCH 3/3] intellij reformat junit tests --- .../test/org/ejml/data/TestDMatrixRMaj.java | 8 +++--- .../test/org/ejml/ops/TestDConvertArrays.java | 28 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java b/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java index 3ab92c16e..79303937d 100644 --- a/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java +++ b/main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java @@ -210,9 +210,9 @@ protected DMatrixD1 createMatrix( int numRows, int numCols ) { double[][] actual = new DMatrixRMaj(expected).get2DData(); assertArrayEquals(expected, actual); - assertEquals(0,actual[0][0], UtilEjml.TEST_F64); - assertEquals(1,actual[0][1], UtilEjml.TEST_F64); - assertEquals(2,actual[1][0], UtilEjml.TEST_F64); - assertEquals(3,actual[1][1], UtilEjml.TEST_F64); + assertEquals(0, actual[0][0], UtilEjml.TEST_F64); + assertEquals(1, actual[0][1], UtilEjml.TEST_F64); + assertEquals(2, actual[1][0], UtilEjml.TEST_F64); + assertEquals(3, actual[1][1], UtilEjml.TEST_F64); } } diff --git a/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java b/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java index d201fbad3..205249402 100644 --- a/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java +++ b/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java @@ -30,12 +30,12 @@ public class TestDConvertArrays extends EjmlStandardJUnit { @Test public void dd_to_ddrm() { - DMatrixRMaj m = DConvertArrays.convert(new double[][]{{0,1},{2,3}},(DMatrixRMaj)null); + DMatrixRMaj m = DConvertArrays.convert(new double[][]{{0, 1}, {2, 3}}, (DMatrixRMaj)null); - assertEquals(0,m.get(0,0), UtilEjml.TEST_F64); - assertEquals(1,m.get(0,1), UtilEjml.TEST_F64); - assertEquals(2,m.get(1,0), UtilEjml.TEST_F64); - assertEquals(3,m.get(1,1), UtilEjml.TEST_F64); + assertEquals(0, m.get(0, 0), UtilEjml.TEST_F64); + assertEquals(1, m.get(0, 1), UtilEjml.TEST_F64); + assertEquals(2, m.get(1, 0), UtilEjml.TEST_F64); + assertEquals(3, m.get(1, 1), UtilEjml.TEST_F64); } @Test @@ -44,10 +44,10 @@ public void ddrm_to_dd() { double[][] dd = DConvertArrays.convert(new DMatrixRMaj(expected)); assertArrayEquals(expected, dd); - assertEquals(0,dd[0][0], UtilEjml.TEST_F64); - assertEquals(1,dd[0][1], UtilEjml.TEST_F64); - assertEquals(2,dd[1][0], UtilEjml.TEST_F64); - assertEquals(3,dd[1][1], UtilEjml.TEST_F64); + assertEquals(0, dd[0][0], UtilEjml.TEST_F64); + assertEquals(1, dd[0][1], UtilEjml.TEST_F64); + assertEquals(2, dd[1][0], UtilEjml.TEST_F64); + assertEquals(3, dd[1][1], UtilEjml.TEST_F64); } // @Test @@ -65,11 +65,11 @@ public void ddrm_to_dd() { @Test public void dd_to_d4() { - DMatrix4 m = DConvertArrays.convert(new double[][]{{0,1,2,3}},(DMatrix4) null); + DMatrix4 m = DConvertArrays.convert(new double[][]{{0, 1, 2, 3}}, (DMatrix4)null); - assertEquals(0,m.a1, UtilEjml.TEST_F64); - assertEquals(1,m.a2, UtilEjml.TEST_F64); - assertEquals(2,m.a3, UtilEjml.TEST_F64); - assertEquals(3,m.a4, UtilEjml.TEST_F64); + assertEquals(0, m.a1, UtilEjml.TEST_F64); + assertEquals(1, m.a2, UtilEjml.TEST_F64); + assertEquals(2, m.a3, UtilEjml.TEST_F64); + assertEquals(3, m.a4, UtilEjml.TEST_F64); } }