/EvalEx-big-math

EvalEx big-math extension for math functions (pow, sqrt, log, sin, ...)

Primary LanguageJavaApache License 2.0Apache-2.0

EvalEx-big-math - an EvalEx extension to use the big-math project for calculations

Build Quality Gate Status Security Rating Vulnerabilities Maven Central

EvalEx is a handy expression evaluator for Java. It allows to evaluate mathematical and boolean expressions.

Big-math is a library by Eric Obermühlner. It provides advanced Java BigDecimal math functions using an arbitrary precision.

EvalEx-big-math adds the advanced math functions from big-math to EvalEx.

How to use it

The easiest way is to use the BigMathExpression class, which is the same as the Expression class, but has all the new functions and operators configured:

BigMathExpression expression = new BigMathExpression("SIN(x)");
EvaluationValue result = expression.with("x", 10).evaluate(); 

ExpressionConfiguration configuration = ExpressionConfiguration.builder()
    .decimalPlacesRounding(2)
    .build();

BigMathExpression expression = new BigMathExpression("SIN(x)", configuration);
EvaluationValue result = expression.with("x", 10).evaluate(); 

Alternatively, create a new EvalEx configuration and simply add all the functions and operators, existing functions and operators will be overridden:

ExpressionConfiguration configuration =
    ExpressionConfiguration.defaultConfiguration()
        .withAdditionalFunctions(BigMathFunctions.allFunctions())
        .withAdditionalOperators(BigDecimalMathOperators.allOperators());
        
Expression expression = new Expression("SIN(x) ^ COS(y)", configuration); 

You may choose only to add the functions and operators you need by specifying them separately:

ExpressionConfiguration configuration =
    ExpressionConfiguration.defaultConfiguration()
        .withAdditionalFunctions(
            Map.entry("SIN", new BigMathSinFunction()),
            Map.entry("COS", new BigMathCosFunction()))
        .withAdditionalOperators(
            Map.entry("^", new BigMathInfixPowerOfOperator()));

        
Expression expression = new Expression("SIN(x) ^ COS(y)", configuration); 

Instead of overriding the existing functions and operators, you can also add them with new names:

ExpressionConfiguration configuration =
    ExpressionConfiguration.defaultConfiguration()
        .withAdditionalFunctions(
            Map.entry("BIG_SIN", new BigMathSinFunction()),
            Map.entry("BIG_COS", new BigMathCosFunction()))
        .withAdditionalOperators(
            Map.entry("^", new BigMathInfixPowerOfOperator()));
        
Expression expression = new Expression("BIG_SIN(x) ^ BIG_COS(y)", configuration); 

Discussion

For announcements, questions and ideas visit the Discussions area.

Download / Including

You can download the binaries, source code and JavaDoc jars from Maven Central.
You will find there also copy/paste templates for including EvalEx in your project with build systems like Maven or Gradle.

Maven

To include it in your Maven project, refer to both artifacts in your pom. For example:

<dependencies>
    <dependency>
      <groupId>com.ezylang</groupId>
      <artifactId>EvalEx</artifactId>
      <version>3.2.0</version>
    </dependency>
    <dependency>
      <groupId>com.ezylang</groupId>
      <artifactId>EvalEx-big-math</artifactId>
      <version>1.0.0</version>
    </dependency>
</dependencies>

Gradle

If you're using gradle add both dependencies to your project's app build.gradle:

dependencies {
    compile 'com.ezylang:EvalEx:3.2.0'
    compile 'com.ezylang:EvalEx-big-math:1.0.0'
}
⚠️ Attention: You have to add EvalEx and EvalEx-big-math dependencies to your project

New Functions

Name Description
BN(x) Calculates the Bernoulli number for the specified index.
E() Returns the number e with the configured precision. The value is cached
EXP(x) Calculates the natural exponent of x (ex).
EXPONENT(x) Returns the exponent of the specified BigDecimal written as mantissa * 10exponent
FRACTIONALPART(x) Returns the fractional part of x (right of the decimal point).
GAMMA(x) Calculates the gamma function of x.
INTEGRALPART(x) Returns the integral part of x (left of the decimal point).
LOG2(x) The natural logarithm (base e) of x to te base of 2.
MANTISSA(x) Returns the mantissa of the specified BigDecimal written as mantissa * 10exponent
PI() Returns the number PI with the configured precision. The value is cached.
RECIPROCAL(x) Returns the reciprocal of x.
ROOT(x, n) Calculates the nth root of x.
SIGNIFICANTDIGITS(x) Returns the number of significant digits of x.

Overridden Basic Functions

Name Description
FACT(x) Calculates the factorial of x.
LOG(x) The natural logarithm (base e) of x.
LOG10(x) The natural logarithm (base e) of x to te base of 10.

Overridden Basic perators

Name Description
^ The power-of operator.

Overridden Trigonometric Functions

Name Description
ACOS(x) Returns the the arc-cosine (in degrees)
ACOSH(x) Returns the the hyperbolic arc-cosine (in degrees)
ACOSR(x) Returns the the arc-cosine (in radians)
ACOT(x) Returns the the arc-co-tangent (in degrees)
ACOTH(x) Returns the the hyperbolic arc-co-tangent (in radians)
ACOTR(x) Returns the the arc-co-tangent (in radians)
ASIN(x) Returns the the arc-sine (in degrees)
ASINH(x) Returns the hyperbolic arc-sine (in degrees)
ASINR(x) Returns the arc-sine (in radians)
ATAN2(y, x) Returns the angle of atan2 (in degrees)
ATAN2R(y, x) Returns the angle of atan2 (in radians)
ATAN(x) Returns the arc-tangent (in degrees)
ATANH(x) Returns the hyperbolic arc-tangent (in degrees)
ATANR(x) Returns the arc-tangent (in radians)
COS(x) Returns the cosine of an angle (in degrees)
COSH(x) Returns the hyperbolic cosine of x
COSR(x) Returns the cosine of an angle (in radians)
COT(x) Returns the co-tangent of an angle (in degrees)
COTH(x) Returns the hyperbolic co-tangent of x
COTR(x) Returns the co-tangent of an angle (in radians)
CSC(x) Returns the co-secant of an angle (in degrees)
CSCH(x) Returns the hyperbolic co-secant of x
CSCR(x) Returns the co-secant of an angle (in radians)
DEG(rad) Converts an angle measured in radians to an approximately equivalent angle measured in degrees
RAD(degrees) Converts an angle measured in degrees to an approximately equivalent angle measured in radians
SEC(x) Returns the secant of an angle (in degrees)
SECH(x) Returns the hyperbolic secant of an angle
SECR(x) Returns the secant of an angle (in radians)
SIN(x) Returns the sine of an angle (in degrees)
SINH(x) Returns the hyperbolic sine of x
SINR(x) Returns the sine of an angle (in radians)
TAN(x) Returns the tangent of an angle (in degrees)
TANH(x) Returns the hyperbolic tangent of x
TANR(x) Returns the tangent of an angle (in radians)

Author and License

Copyright 2012-2024 by Udo Klimaschewski

Thanks to all who contributed to this project: Contributors

The software is licensed under the Apache License, Version 2.0 ( see LICENSE file).

EvalEx is licensed under the Apache 2.0 License and has a Copyright 2012-2022 by Udo Klimaschewski.

Big-math is licensed under the MIT License and has a Copyright 2017 by Eric Obermühlner.