MXReflection: A Java Math Framework
See what MXReflection can do for you.
Join the DZone community and get the full member experience.
Join For FreeMXReflection is a Java math framework based on mXparser library capabilities.
You can calculate complex mathematical operations and functions with Java, just by using class-related fields. MXReflection reads values from the assigned fields and injects the results in the @Expression
annotated fields.
Installation
Maven
<dependency>
<groupId>com.github.ismail-mekni</groupId>
<artifactId>mxreflection</artifactId>
<version>1.0.1</version>
</dependency>
Gradle
xxxxxxxxxx
dependencies {
compile group: 'com.github.ismail-mekni', name: 'mxreflection', version: '1.0.1'
}
How to Use It
You just need two Java annotations:
- With an
@Arg
value, we can assign customized argument names to be used in the target function. - An
@Expression
annotation value contains the function expression with the arguments.
First example:
package com.ismail.mxreflection.example;
import com.ismail.mxreflection.annotations.Arg;
import com.ismail.mxreflection.annotations.Expression;
import com.ismail.mxreflection.core.Calculator;
import com.ismail.mxreflection.factory.MXFactory;
import org.junit.Test;
public class Example1Test {
class Example1 {
"f1") (
String field1;
"f2") (
int field2;
"f1 * sin(f2) * log2(f1 + f2) + der(cos(f1), f1) * pi + int(tan(f2), f2, 0, e)") (
double field3;
}
public void example1Test() {
Example1 example1 = new Example1();
example1.field1 = "2.2";
example1.field2 = 5;
Calculator<Example1> calculator = MXFactory.createCalculator(Example1.class);
calculator.calculate(example1);
System.out.println("Field 3 result: " + example1.field3);
}
}
Output:
Field 3 result: -34.32819235851987
Supported Math Collections
MXReflection supports the math collection available in the mXparser math library:
- Operators (+, -, *, /, #, !, ^)
- Binary Relations (=, ==, =<, =>, <, >, <>, !=, ~=)
- Boolean Operators (&, &&, /, ~&, ~&&, ~/, |, ||…)
- Bitwise Operators (@~, @&, @^, @|, @<<, @>>)
- Unary Functions (sin, cos, tan, tg, ctan, ctg, cot, sec,…)
- Binary Functions (log, mod, C, Bern, Stirl1, Stirl2, …)
- 3-args Functions (if, chi, CHi, Chi, cHi, pUni, cUni, qUni, pNor, cNor, qNor)
- Variadic Functions (iff, min, max, ConFrac, ConPol, gcd, …)
- Iterated Operators (sum, prod, avg, vari, stdi, mini, maxi)
- Calculus Operators (int, der, der-, der+, dern, diff, difb)
- Math Constants (pi, e, [gam], [phi], [PN], [B*], [F’d], [F’a], …)
- Physical Constants ([c], [G.], [g], [hP], [h-], [lP], [mP], [tP])
- Astronomical Constants ([ly], [au], [pc], [kpc], [Earth-R-eq], …)
- Random Variables ([Uni], [Int], [Int1], [Int2], [Int3], [Int4], …)
- Metric prefixes ([%], [%%], [Y], [sept], [Z], [sext], [E], …)
- Parser Symbols ((, ), ,, ;)
- Units
MXReflection Parsing
Argument Parsing
MXReflection supports all field data types with numeric content as an argument. You can use all Java types with toString
implementations that return numeric results.
Result Parsing
Supported result field java types:
- Double
- double
- Long
- long
- String
- BigInteger
Note that for long, Long, and BigInteger, MXReflection uses Math.round
to parse the final result before injecting it. It is recommended to be sure that the expression returns an integer type.
Result Reuse
With MXReflection, you can use function results as arguments for other results:
Second example:
xxxxxxxxxx
package com.ismail.mxreflection.example;
import com.ismail.mxreflection.annotations.Arg;
import com.ismail.mxreflection.annotations.Expression;
import com.ismail.mxreflection.core.Calculator;
import com.ismail.mxreflection.factory.MXFactory;
import org.junit.Test;
public class Example2Test {
public class Example2{
"f1") (
private String field1;
"f2") (
private Long field2;
"f2 - f1") (
"f3") (
private double field3;
"f3 - f2") (
"f4") (
private double field4;
"f1 - f2") (
"f5") (
private Double field5;
"f4-f5") (
"f6") (
private String field6;
"f6-f5") (
"f7") (
private long field7;
"f7+5") (
private Long field8;
}
public void exampleTest() {
Example2 example2 = new Example2();
example2.field1 = "2.2";
example2.field2 = 5L;
Calculator<Example2> calculator = MXFactory.createCalculator(Example2.class);
calculator.calculate(example2);
System.out.println("Field 3 result: " + example2.field3);
System.out.println("Field 4 result: " + example2.field4);
System.out.println("Field 5 result: " + example2.field5);
System.out.println("Field 6 result: " + example2.field6);
System.out.println("Field 7 result: " + example2.field7);
System.out.println("Field 8 result: " + example2.field8);
}
}
Output:
Field 3 result: 2.8
Field 4 result: -2.2
Field 5 result: -2.8
Field 6 result: 0.6
Field 7 result: 3
Field 8 result: 8
MXReflection resolves a graph of dependencies between functions and arguments. It makes sure that there is no cycle in the field dependency.
Links
Published at DZone with permission of Ismail Mekni. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments