javaarraysperformancelinear-algebrajblas

Java Jblas DoubleMatrix vector addition


I feel really stupid asking this, but I can't find a method for adding a row/vector to an index or a specific set of indices.

My current workaround is to getRows(int[]) then addiRowVector(DoubleMatrix) then put(Range rs, Range cs, DoubleMatrix x) : (get rows, add your vector, then put them back)

This seems like a backwards and costly implementation, is there any alternative? Is there something simple I'm missing?

Thanks in advance!


Solution

  • For one row you can do it as in this piece of code using row number for example

    void oneRow() {
        DoubleMatrix matrix = new DoubleMatrix(new double[][] {
                {11,12,13},
                {21,22,23},
                {31,32,33}});
        DoubleMatrix otherRow = new DoubleMatrix(new double[][] {{-11, -12, -13}});
    
        int rowNumber = 0;
        DoubleMatrix row = matrix.getRow(rowNumber);
        row.addiRowVector(otherRow);
        matrix.putRow(rowNumber, row);
    
        System.out.println(matrix);
    }
    

    as result you'll see

    [0,000000, 0,000000, 0,000000; 21,000000, 22,000000, 23,000000; 31,000000, 32,000000, 33,000000]
    

    for several rows you can use loop using row numbers array for example

    void multipleRows() {
        DoubleMatrix matrix = new DoubleMatrix(new double[][] {
                {11,12,13},
                {21,22,23},
                {31,32,33}});
    
        int[] rowNumbers = {0, 2};
        DoubleMatrix otherRows = new DoubleMatrix(new double[][] {
                {-11, -12, -13},
                {-21, -22, -23}});
        int otherRowsNumber = 0;
    
        for (int r : rowNumbers) {
            DoubleMatrix row = matrix.getRow(r);
            row.addiRowVector(otherRows.getRow(otherRowsNumber++));
            matrix.putRow(r, row);
        }
        System.out.println(matrix);
    }
    

    and here for result you see

    [0,000000, 0,000000, 0,000000; 21,000000, 22,000000, 23,000000; 10,000000, 10,000000, 10,000000]