Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Add DMatrixRMaj#get2DData interface #194

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion main/ejml-core/src/org/ejml/data/DMatrixRMaj.java
Original file line number Diff line number Diff line change
@@ -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).
*
Expand Down Expand Up @@ -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);
}
}
21 changes: 19 additions & 2 deletions main/ejml-core/src/org/ejml/ops/DConvertArrays.java
Original file line number Diff line number Diff line change
@@ -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).
*
Expand All @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down
11 changes: 11 additions & 0 deletions main/ejml-core/test/org/ejml/data/TestDMatrixRMaj.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
ee08b397 marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
33 changes: 23 additions & 10 deletions main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}