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

出处 | 程序猿DD 责编 | 张红月

MXReflection,一个基于mXparser库功能的Java复杂计算框架。

还记不记得求学时代各种复杂的数学公式?sin、log2、tan等等等,是不是看到这就觉得算起来麻烦?

通过MXReflection框架,你可以使用Java计算这些曾经我们觉得无比复杂的数学运算和函数,只需使用与类相关的字段。

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

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


dependencies {
 
    compile group: 'com.github.ismail-mekni', name: 'mxreflection', version: '1.0.1'
 
}

那如何使用这个框架呢?

很简单,只需要两个java注释。

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

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

这个框架的使用其实不难,下面是两个计算案例,通过案例其实很容易理解该怎么使用此框架:

案例1:

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 {
 
        @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:

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{
        @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

在参数解析方面,MXReflection支持以数字内容作为参数的所有字段数据类型。您可以将所有Java类型与返回数值结果的toString实现一起使用。支持的结果字段java类型有:

  • Double

  • double

  • Long

  • long

  • String

  • BigInteger

但是要注意的是注意,对于long、long和BigInteger,MXReflection使用在注入前解析最终结果。建议确保表达式返回整数类型。

猜你喜欢

转载自blog.csdn.net/Crystalqy/article/details/115334360