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 1 commit
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
10 changes: 10 additions & 0 deletions main/ejml-core/src/org/ejml/data/DMatrixRMaj.java
Original file line number Diff line number Diff line change
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);
}
}
18 changes: 18 additions & 0 deletions main/ejml-core/src/org/ejml/ops/DConvertArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
ee08b397 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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);
}
}
13 changes: 13 additions & 0 deletions main/ejml-core/test/org/ejml/ops/TestDConvertArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
Loading