Add BigDecimal values to matrix

35 Views Asked by At

I need to invert matrix contains BigDecimal values.

So I should either create empty MatrixR128 and fill it with BigDecimal (Quadraple) values from input array (but there is no .set() method) or I should create matrix from existing array of elements (but don't understand how to do it).

This is my current code. Creating two-dimentional BigDecimal array from one-dimentional String may be not necessary, but it doesn't matter right now.

import org.ojalgo.matrix.MatrixR128;

import java.math.BigDecimal;

public class OJAlgoInverseMatrix {

    public static void main(String[] args) {
        String[] matrix = {
                "1", "-0.100000000000000002", "0",
                "0", "1", "-0.100000000000000002",
                "0", "0",  "0.12345678901234567890"};
        BigDecimal[][] bda = createBigDecimalArray(matrix, 3);
        MatrixR128 inBDM = createMatrix(bda, 3);
        MatrixR128 outBDM = inBDM.invert();
        printMatrix(outBDM, 3, 20);
    }

    public static MatrixR128 createMatrix(BigDecimal[][] array, int size) {
        // Variant 1 step 1: empty matrix
        MatrixR128 matrix = MatrixR128.FACTORY.make(size, size);
        // TODO: Variant 2 step 1: create matrix from input array
        int i = 0;
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                // TODO: Variant 1 step 2: add value to element
                i++;
            }
        }
        return matrix;
    }

    public static BigDecimal[][] createBigDecimalArray(String[] array, int size) {
        BigDecimal[][] bda = new BigDecimal[size][size];
        int i = 0;
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                bda[row][col] = new BigDecimal(array[i]);
                i++;
            }
        }
        return bda;
    }

    public static void printMatrix (MatrixR128 matrix, int size, int precision) {
        String format = "%." + precision + "f ";
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                System.out.printf(format, matrix.get(row, col).toBigDecimal());
            }
            System.out.println();
        }
    }
}
0

There are 0 best solutions below