# Java中sin、log、tan等复杂数学运算怎么搞？

05/31 08:44

MXReflection可以从指定字段读取值，并将结果注入@Expression注释字段。

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

1、通过@Arg值，我们可以指定要在目标函数中使用的自定义参数名称。

2、@Expression注释值包含带有参数的函数表达式。

## 案例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


## 案例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框架支持mXparser数学库中提供的数学集合如下：

• 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

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

0
0 收藏

0 评论
0 收藏
0