What about complex mathematical operations such as sin, log and tan in Java?
Program description DD 2021-06-25 02:34:50

Do you remember all kinds of complicated mathematical formulas in the school days ?sin、log2、tan And so on. , If I see this, I think it's troublesome ?

adopt MXReflection frame , You can use Java Computing these mathematical operations and functions that we once thought were extremely complex , Just use the fields related to the class .

MXReflection You can read the value from the specified field , And inject the results into the @Expression Comment fields .

<dependency>
<groupId>com.github.ismail-mekni</groupId>
<artifactId>mxreflection</artifactId>
<version>1.0.1</version>
</dependency>

So how to use this framework ?

It's simple , Only two... Are needed java notes .

1、 adopt @Arg value , We can specify the name of the custom parameter to be used in the objective function .

2、@Expression Comment values contain function expressions with arguments .

It's not hard to use this framework , Here are two calculation cases , It's easy to understand how to use this framework through cases :

Case study 1

public class Example1Test {

class Example1 {

@Arg("f1")

String field1;

@Arg("f2")

int field2;

@Expression("f1 * sin(f2) * log2(f1 + f2) + der(cos(f1), f1) * pi + int(tan(f2), f2, 0, e)")

double field3;

}

@Test

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

Case study 2


public class Example2Test {

public class Example2{

@Arg("f1")

private String field1;

@Arg("f2")

private Long field2;

@Expression("f2 - f1")

@Arg("f3")

private double field3;

@Expression("f3 - f2")

@Arg("f4")

private double field4;

@Expression("f1 - f2")

@Arg("f5")

private Double field5;

@Expression("f4-f5")

@Arg("f6")

private String field6;

@Expression("f6-f5")

@Arg("f7")

private long field7;

@Expression("f7+5")

private Long field8;

}

@Test

public void exampleTest() {

Example2 example2 = new Example2();

example2.field1 = "2.2";
example1.field2 = 5; 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 Framework support mXparser The math set provided in the math library is as follows :

  • 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

In parameter analysis ,MXReflection Supports all field data types with digital content as parameters . You can put all of Java The type is the same as... That returns the numerical result toString Implementation used together . Supported result fields java Type a :

  • Double
  • double
  • Long
  • long
  • String
  • BigInteger

But pay attention to , about long、long and BigInteger,MXReflection Use to parse the final result before injection . It is recommended to make sure that the expression returns an integer type .

Scan the QR code and follow me , Reply key words mxreflection, Get source code library and project examples !

Please bring the original link to reprint ,thank
Similar articles