|
|
Arity : Arithmetic Engine for Java
Evaluates arithmetic expressions represented as strings.
In version 1.3.0 Arity adds support for complex numbers. You can define and evaluate complex functions. All the built-ins (trigonometrics, log, power, etc) accept complex arguments. Enjoy!
An expression can have one of these forms:
- constant value: 1+1
- implicit function: x+1
- constant definition: a=1+1
- function definition with explicit arguments: f(a)=a+1
- function definition with implicit arguments: f=x+1
Arity supports as built-ins:
- classic operators: + - * /
- modulo(%), power(^) and parentheses
- trigonometric functions
- logarithms, square & cube root
- constants pi, e
- factorial, gcd, combinations, permutations
- user-defined functions and constants
Try it
Download the jar and execute it:
- Run the unit tests:
java -jar arity.jar
- Pass an expression to evaluate on the command line:
java -jar arity.jar "1+1"
Usage
import org.javia.arity.Symbols;
import org.javia.arity.SyntaxException;
public class TryArity {
public static void main(String args[]) throws SyntaxException {
Symbols symbols = new Symbols();
double value = symbols.eval("2^10");
}
}Take a look at the Javadoc.
Syntax
| Expression | Evaluates to |
| -1e2 | -100 |
| (-2)^3! | 64 |
| sin(-1--1) | 0 |
| 1+2)(2+3 | 15 |
| e^(i*pi) | -1 |
| (2+i)/(1+i) | 1.5-0.5i |
| f=sin(2x) | arity 1; const 2.0; load0; mul; sin; |
| foo(a, b) = a*(b+1) | arity 2; load0; load1; const 1.0; add; mul; |
API
The public API (the stuff you interact with when using the Engine) is composed of 2 classes (Function and Symbols), and you only need to know about these 4 methods:
Vital signs
The Arity Engine was developed to work well on MIDP (Java for mobile phones), and as such it is extremely economic in both memory and CPU usage.
The expressions are compiled into an intermediary form (opcodes for a simple virtual machine) which is very fast to evaluate. Such a compiled form is well fit for repeated evaluation of the same function on multiple points (needed for example for plotting or for numerical methods -- solving, integration).
Arity's implementation has 3 KLOC (3000 lines of code), organized in about 30 classes. The obfuscated JAR is under 30 KB.
Performance
There are two distinct operations to measure: the compilation (transforming the expression string into a Function object), and the evaluation (obtaining the value of the function given the values of the function arguments).
| On Desktop Computer | On Mobile Phone | |
| Compilations / second | 50,000 | 1000 |
| Evaluations / second | 1,000,000 | 10,000 |
To measure the memory/time usage on your computer, simply download the jar and execute it (java -jar arity.jar) -- you will see profiling information similar to this:
log(x+30.5, 3)^.7*sin(x+.5): arity 1; load0; const 30.5; add; const 3.0; call; const 0.7; power; load0; const 0.5; add; sin; mul; compilation memory: 3846 bytes compilation time: 25.4 us execution memory: 0 bytes execution time: 1.07 us
