Search in sources :

Example 6 with DenseMatrix

use of org.openlca.core.matrix.format.DenseMatrix in project olca-modules by GreenDelta.

the class SimpleBinTest method testMatrixIO.

@Test
public void testMatrixIO() throws Exception {
    var f = Files.createTempFile("olcamat-", ".bin").toFile();
    Matrix m = new DenseMatrix(2, 3);
    m.setValues(new double[][] { { 1, 2, 3 }, { 4, 5, 6 } });
    SimpleBin.write(m, f);
    m = SimpleBin.read(f);
    Assert.assertArrayEquals(new double[] { 1, 4 }, m.getColumn(0), 1e-16);
    Assert.assertArrayEquals(new double[] { 2, 5 }, m.getColumn(1), 1e-16);
    Assert.assertArrayEquals(new double[] { 3, 6 }, m.getColumn(2), 1e-16);
    // Files.delete(f.toPath());
    if (!f.delete()) {
        f.deleteOnExit();
    }
}
Also used : Matrix(org.openlca.core.matrix.format.Matrix) DenseMatrix(org.openlca.core.matrix.format.DenseMatrix) DenseMatrix(org.openlca.core.matrix.format.DenseMatrix) Test(org.junit.Test)

Example 7 with DenseMatrix

use of org.openlca.core.matrix.format.DenseMatrix in project olca-modules by GreenDelta.

the class NpyTest method setup.

@Before
public void setup() throws Exception {
    matrix = new DenseMatrix(2, 3);
    matrix.setValues(new double[][] { { 1., 2., 3. }, { 4., 5., 6. } });
    var dir = Files.createTempDirectory("_olca_tests").toFile();
    npy = NpyMatrix.write(dir, "M", matrix);
}
Also used : DenseMatrix(org.openlca.core.matrix.format.DenseMatrix) Before(org.junit.Before)

Example 8 with DenseMatrix

use of org.openlca.core.matrix.format.DenseMatrix in project olca-modules by GreenDelta.

the class JuliaSolver method multiply.

@Override
public DenseMatrix multiply(MatrixReader a, MatrixReader b) {
    DenseMatrix _a = MatrixConverter.dense(a);
    DenseMatrix _b = MatrixConverter.dense(b);
    int rowsA = _a.rows();
    int colsB = _b.columns();
    int k = _a.columns();
    DenseMatrix c = new DenseMatrix(rowsA, colsB);
    if (colsB == 1) {
        Julia.mvmult(rowsA, k, _a.data, _b.data, c.data);
    } else {
        Julia.mmult(rowsA, colsB, k, _a.data, _b.data, c.data);
    }
    return c;
}
Also used : DenseMatrix(org.openlca.core.matrix.format.DenseMatrix)

Example 9 with DenseMatrix

use of org.openlca.core.matrix.format.DenseMatrix in project olca-modules by GreenDelta.

the class JuliaSolver method invert.

@Override
public DenseMatrix invert(MatrixReader a) {
    if (!a.isSquare())
        throw new NonSquareMatrixException(a.rows(), a.columns());
    DenseMatrix _a = MatrixConverter.dense(a);
    DenseMatrix i = _a == a ? _a.copy() : _a;
    int info = Julia.invert(_a.columns(), i.data);
    if (info > 0)
        throw new SingularMatrixException();
    if (info < 0)
        throw new InsufficientDataException();
    return i;
}
Also used : SingularMatrixException(org.apache.commons.math3.linear.SingularMatrixException) InsufficientDataException(org.apache.commons.math3.exception.InsufficientDataException) NonSquareMatrixException(org.apache.commons.math3.linear.NonSquareMatrixException) DenseMatrix(org.openlca.core.matrix.format.DenseMatrix)

Aggregations

DenseMatrix (org.openlca.core.matrix.format.DenseMatrix)9 Test (org.junit.Test)4 InsufficientDataException (org.apache.commons.math3.exception.InsufficientDataException)2 NonSquareMatrixException (org.apache.commons.math3.linear.NonSquareMatrixException)2 SingularMatrixException (org.apache.commons.math3.linear.SingularMatrixException)2 Before (org.junit.Before)1 Matrix (org.openlca.core.matrix.format.Matrix)1