CalM

CalM is Java library for evaluating mathematical expressions. The following snippet shows how to use the math parser in code.

String input = "5.2+ln 2e^sin 2.6pi";
MathParser parser = StandardConfig.getParser();
try {
    Expression express = parser.parse(input);
    Operand result = express.evaluate(true);
    System.out.println(result.getValue());
} catch (RPNException ex) {
    System.out.println("Error: " + ex.getMessage());
    // The exception might have a fragment with the location of the error.
    ex.getDetail().ifPresent(fragment -> {
        // The fragment's position is a zero-based char index.
        System.out.println("At position: " + (fragment.getPosition() + 1));
    });
}

CalM also includes a script interpreter that's based on the math parser. Below is a script that calculates the first iterations of the sequence Zn+1 = Zn^2 + c in the orbit of zero. The script prompts the user for the complex number c and the number of iterations to run. Convergence of this sequence determines whether c is in the Mandelbrot set or not.

exec print "Fc(Z) = Z^2 + c"
c := prompt "c: "
Fc(Z) := Z^2 + c
n := prompt "How many iterations? "
Z := 0
I
exec
    for I := 1; I <= n; I inc 1 do
        print "Fc(" + Z + ") = " + (Z := round(Fc(Z), 7))
    end
 

Last edit: Tone Sommerland ™ 2016-05-17