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

Propose to add a double[][] convenience method to convert back from DMatrixRMaj #193

Open
ee08b397 opened this issue Jun 28, 2023 · 3 comments
Assignees

Comments

@ee08b397
Copy link
Contributor

DMatrixRMaj can be converted from a double-dimensional array (doulbe[][]) but not the other way around. Can we natively provide an interface for easier testing?

To be more specific, a DMatrixRMaj can be converted from a single-dimensional array or a double-dimensional array, as we can see from the constructors. One use case is asserting the exported matrix is the same as the input matrix. We also have convenience methods to process a Java Matrix or DenseMatrix following processing a DMatrixRMaj.

public DMatrixRMaj( double[][] data ) {

public class DMatrixRMaj extends DMatrix1Row {

    public DMatrixRMaj(double[][] data) {
        this(1, 1);
        this.set(data);
    }

    public DMatrixRMaj(double[] data) {
        this.data = (double[])data.clone();
        this.numRows = this.data.length;
        this.numCols = 1;
    }

A DMatrixRMaj can export a single-dimensional array, as this is how the data is stored internally. But shouldn't we support all types from input to output as well?

public abstract class DMatrixD1 implements ReshapeMatrix, DMatrix {

    public double[] getData() {
        return this.data;
    }
@ee08b397
Copy link
Contributor Author

It could be as simple as this, or converting from the double[] data to a double[][], but I do think this should be natively supported in the library natively.

    /**
     * Convert a {@link DMatrixRMaj} to a two-dimensional array
     * 
     * @param matrix is an input DMatrixRMaj
     * @return a 2D array
     */
    public double[][] get2DData(DMatrixRMaj matrix) { 
        double[][] array = new double[matrix.numRows][matrix.numCols];
        for (int row = 0; row < matrix.numRows; row++) {
            for (int column = 0; column < matrix.numCols; column++) {
                array[row][column] = matrix.get(row, column);
            }
        }
        
        return array;
    }

@lessthanoptimal
Copy link
Owner

Thanks for the suggestion. I think this is a good idea. If you submit a PR for adding this to DMatrixRMaj with a unit test I'll go over it, but otherwise I'll get around to it this weekend or next week.

@ee08b397
Copy link
Contributor Author

ee08b397 commented Jul 4, 2023

Thanks, @lessthanoptimal. Submitted a PR at #194 with a unit test.

So far, we only use DMatrixRMaj a lot. In the future, it might be useful for other types of matrices as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants