import org.ojalgo.OjAlgoUtils;
import org.ojalgo.matrix.MatrixR064;
import org.ojalgo.matrix.store.RawStore;

public class TestOjAlgo {
    public static void main(String[] args) {
        System.out.println(OjAlgoUtils.getTitle() + " " + OjAlgoUtils.getVersion());

        // Solve A * x = b
        // A = [[3, 1, 1],   b = [5]   =>   x = [1]
        //      [1, 3, 1],       [5]            [1]
        //      [1, 1, 3]]       [5]            [1]
        MatrixR064 A = MatrixR064.FACTORY.copy(RawStore.wrap(new double[][]{
            {3, 1, 1},
            {1, 3, 1},
            {1, 1, 3}
        }));
        MatrixR064 b = MatrixR064.FACTORY.copy(RawStore.wrap(new double[][]{
            {5},
            {5},
            {5}
        }));

        MatrixR064 x = A.solve(b);
        System.out.println("x = " + x);

        // Verify: A * x == b
        MatrixR064 check = A.multiply(x);
        double err = b.subtract(check).norm();
        System.out.println("||b - A*x|| = " + err);
        if (err > 1e-12) System.exit(1);

        // Verify determinant: det(A) = 3*(3*3-1*1) - 1*(1*3-1*1) + 1*(1*1-3*1)
        //                       = 3*(8) - 1*(2) + 1*(-2) = 24 - 2 - 2 = 20
        double det = A.getDeterminant();
        System.out.println("det(A) = " + det);
        if (Math.abs(det - 20.0) > 1e-12) System.exit(1);

        System.out.println("ojAlgo runtime test passed");
    }
}
