javaojalgo

Insert Access2D element in Primitive64Store in OjAlgo at specific column and row


Is it possible to insert a Access2D element in a Primitive64Store in OjAlgo?

    Access2D<Double> data = Access2D.wrap(mu.toRawCopy2D());
    Primitive64Store B = Primitive64Store.FACTORY.make(rows * m, columns * n);

I want to insert data into B at a specific start row and start column.

For the moment. I have implement the procedure like this:

public class Repmat {

    static public MatrixStore<Double> repmat(MatrixStore<Double> mu, int m, int n) {
        long rows = mu.countRows();
        long columns = mu.countColumns();
        double[][] data = mu.toRawCopy2D();
        Primitive64Store B = Primitive64Store.FACTORY.make(rows * m, columns * n);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                for(int k = 0; k < rows; k++) {
                    B.fillRow(i * rows + k, j * columns, Access1D.wrap(data[k])); // Get row from data and place it into B
                }
            }
        }
        return B.get();
    }
} 

But it must be a better way to do this?


Solution

  • Perhaps something like this:

    static public MatrixStore<Double> repmat(MatrixStore<Double> mu, int m, int n) {
    
        LogicalBuilder<Double> builder = mu.logical();
    
        for (int i = 1; i < m; i++) {
            builder.below(mu);
        }
    
        MatrixStore<Double> firstCol = builder.get();
    
        for (int j = 1; j < n; j++) {
            builder.right(firstCol);
        }
    
        return builder.get();
    }