Search in sources :

Example 6 with EvaluationException

use of gov.sandia.n2a.language.EvaluationException in project n2a by frothga.

the class Matrix method determinant.

public double determinant() throws EvaluationException {
    int w = columns();
    int h = rows();
    if (h != w)
        throw new EvaluationException("Can't compute determinant of non-square matrix.");
    if (h == 1)
        return get(0, 0);
    if (h == 2)
        return get(0, 0) * get(1, 1) - get(1, 0) * get(0, 1);
    if (h == 3) {
        return get(0, 0) * get(1, 1) * get(2, 2) - get(0, 0) * get(1, 2) * get(2, 1) - get(0, 1) * get(1, 0) * get(2, 2) + get(0, 1) * get(1, 2) * get(2, 0) + get(0, 2) * get(1, 0) * get(2, 1) - get(0, 2) * get(1, 1) * get(2, 0);
    }
    throw new EvaluationException("Can't compute deteminant of matrices larger then 3x3 (because we are lazy).");
}
Also used : EvaluationException(gov.sandia.n2a.language.EvaluationException)

Example 7 with EvaluationException

use of gov.sandia.n2a.language.EvaluationException in project n2a by frothga.

the class MatrixSparse method load.

public void load(Reader stream, boolean units) throws EvaluationException {
    try (BufferedReader reader = new BufferedReader(stream)) {
        // Throw away "Sparse" line
        String line = reader.readLine();
        while (true) {
            line = reader.readLine();
            if (line == null)
                break;
            line = line.trim();
            String[] pieces = line.split(",");
            if (pieces.length < 3)
                continue;
            int r = Integer.valueOf(pieces[0].trim());
            int c = Integer.valueOf(pieces[1].trim());
            double v = Double.valueOf(pieces[2].trim());
            set(r, c, v);
        }
    } catch (IOException error) {
        throw new EvaluationException("Failed to convert input to matrix");
    }
}
Also used : BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Aggregations

EvaluationException (gov.sandia.n2a.language.EvaluationException)7 ArrayList (java.util.ArrayList)3 EquationSet (gov.sandia.n2a.eqset.EquationSet)2 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)2 IOException (java.io.IOException)2 AccountableConnection (gov.sandia.n2a.eqset.EquationSet.AccountableConnection)1 Variable (gov.sandia.n2a.eqset.Variable)1 VariableReference (gov.sandia.n2a.eqset.VariableReference)1 AccessVariable (gov.sandia.n2a.language.AccessVariable)1 Operator (gov.sandia.n2a.language.Operator)1 Visitor (gov.sandia.n2a.language.Visitor)1 Output (gov.sandia.n2a.language.function.Output)1 Scalar (gov.sandia.n2a.language.type.Scalar)1 BufferedReader (java.io.BufferedReader)1 TreeSet (java.util.TreeSet)1