From 03d1c296f9b00c0c22684b9d03cf0ee30a99b965 Mon Sep 17 00:00:00 2001 From: Songxiao Zhang Date: Sat, 8 Jul 2023 01:01:44 -0400 Subject: [PATCH] Feature/Add DMatrixRMaj#get2DData interface (#194) * Add DMatrixRMaj#get2DData interface --- .../src/org/ejml/data/DMatrixRMaj.java | 12 ++++++- .../src/org/ejml/ops/DConvertArrays.java | 21 ++++++++++-- .../test/org/ejml/data/TestDMatrixRMaj.java | 11 +++++++ .../test/org/ejml/ops/TestDConvertArrays.java | 33 +++++++++++++------ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java b/main/ejml-core/src/org/ejml/data/DMatrixRMaj.java index ee76e534c..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). * @@ -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..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; @@ -58,6 +57,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.unsafe_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..79303937d 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..205249402 100644 --- a/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java +++ b/main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java @@ -24,17 +24,30 @@ 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 { @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 + 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 @@ -52,11 +65,11 @@ public void dd_to_ddrm() { @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); } }