### Create Basic Expressions with ExpressionBuilder Source: https://context7.com/fasseg/exp4j/llms.txt Demonstrates building and evaluating simple arithmetic, parentheses, power, unary minus, and modulo operations. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; // Simple arithmetic expression double result = new ExpressionBuilder("2 + 3 * 4 - 12") .build() .evaluate(); // result = 2.0 // Expression with parentheses double result2 = new ExpressionBuilder("(2 + 4) * 5") .build() .evaluate(); // result2 = 30.0 // Expression with power operator double result3 = new ExpressionBuilder("2^3") .build() .evaluate(); // result3 = 8.0 // Negative numbers and unary minus double result4 = new ExpressionBuilder("-3 * -24.23") .build() .evaluate(); // result4 = 72.69 // Modulo operation double result5 = new ExpressionBuilder("24.3343 % 3") .build() .evaluate(); // result5 = 0.3343 ``` -------------------------------- ### Manage Variables in Expressions Source: https://context7.com/fasseg/exp4j/llms.txt Shows how to define, set, and update variables within expressions, including using Maps and retrieving variable names. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; import java.util.HashMap; import java.util.Map; // Single variable double result = new ExpressionBuilder("cos(x)") .variables("x") .build() .setVariable("x", Math.PI) .evaluate(); // result = -1.0 // Multiple variables Expression e = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)") .variables("x", "y") .build() .setVariable("x", 2.3) .setVariable("y", 3.14); double result2 = e.evaluate(); // result2 ≈ 6.02 // Using a Map for variables Map variables = new HashMap<>(); variables.put("foo", 2.0); variables.put("bar", 3.3); Expression e2 = new ExpressionBuilder("12.23 * foo - bar") .variables(variables.keySet()) .build() .setVariables(variables); double result3 = e2.evaluate(); // result3 = 21.16 // Reusing expressions with different variable values Expression e3 = new ExpressionBuilder("7*x + 3*y - log(y/x*12)^y") .variables("x", "y") .build(); e3.setVariable("x", 1.3).setVariable("y", 4.22); double result4 = e3.evaluate(); // Update variables and re-evaluate e3.setVariable("x", 1.79854).setVariable("y", 9281.123); double result5 = e3.evaluate(); // Get variable names from expression Expression e4 = new ExpressionBuilder("b*a-9.24c") .variables("b", "a", "c") .build(); Set varNames = e4.getVariableNames(); // varNames contains: "a", "b", "c" ``` -------------------------------- ### exp4j Logarithm with Custom Base Source: https://context7.com/fasseg/exp4j/llms.txt Calculate logarithms with a custom base using the logb(value, base) function. ```java // Logarithm with custom base using logb(value, base) double logbResult = new ExpressionBuilder("logb(8, 2)") .build().evaluate(); // logbResult = 3.0 ``` -------------------------------- ### Create Two-Argument Custom Function in Java Source: https://context7.com/fasseg/exp4j/llms.txt Implement a custom function that takes exactly two arguments. The constructor specifies the function name and the number of arguments. ```Java // Two argument custom function Function logBase = new Function("logb", 2) { @Override public double apply(double... args) { return Math.log(args[0]) / Math.log(args[1]); } }; double logResult = new ExpressionBuilder("logb(8, 2)") .function(logBase) .build() .evaluate(); // logResult = 3.0 ``` -------------------------------- ### exp4j Root Functions Source: https://context7.com/fasseg/exp4j/llms.txt Compute square roots with sqrt() and cube roots with cbrt(). ```java // Root functions: sqrt, cbrt double sqrtResult = new ExpressionBuilder("sqrt(16)") .build().evaluate(); // sqrtResult = 4.0 double cbrtResult = new ExpressionBuilder("cbrt(27)") .build().evaluate(); // cbrtResult = 3.0 ``` -------------------------------- ### exp4j Trigonometric Functions Source: https://context7.com/fasseg/exp4j/llms.txt Use exp4j's built-in trigonometric functions such as sin, cos, tan, cot, sec, and csc. Ensure correct input for expected results. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; // Trigonometric functions: sin, cos, tan, cot, sec, csc double sinResult = new ExpressionBuilder("sin(0.5)") .build().evaluate(); // sinResult ≈ 0.479 double cosResult = new ExpressionBuilder("cos(x)") .variables("x").build() .setVariable("x", Math.PI).evaluate(); // cosResult = -1.0 ``` -------------------------------- ### exp4j Rounding and Absolute Value Functions Source: https://context7.com/fasseg/exp4j/llms.txt Apply rounding functions like ceil(), floor(), and the absolute value function abs(). ```java // Rounding functions: ceil, floor, abs double ceilResult = new ExpressionBuilder("ceil(2.3)") .build().evaluate(); // ceilResult = 3.0 double floorResult = new ExpressionBuilder("floor(2.9)") .build().evaluate(); // floorResult = 2.0 double absResult = new ExpressionBuilder("abs(-5.5)") .build().evaluate(); // absResult = 5.5 ``` -------------------------------- ### Create Variable-Argument Custom Function in Java Source: https://context7.com/fasseg/exp4j/llms.txt Implement a custom function that can accept a variable number of arguments. The `numArguments` field can be set to a maximum expected, and the `apply` method handles the actual number of arguments passed. ```Java // Variable-argument max function Function max = new Function("max", 5) { @Override public double apply(double... args) { double maxVal = args[0]; for (int i = 1; i < numArguments; i++) { if (args[i] > maxVal) { maxVal = args[i]; } } return maxVal; } }; double maxResult = new ExpressionBuilder("max(1, 2.43, 51.13, 43, 12)") .function(max) .build() .evaluate(); // maxResult = 51.13 ``` -------------------------------- ### Create Multi-Argument Custom Function in Java Source: https://context7.com/fasseg/exp4j/llms.txt Define a custom function that accepts a fixed number of arguments greater than two. The `apply` method iterates through the provided arguments. ```Java // Multi-argument function (4 arguments) Function avg = new Function("avg", 4) { @Override public double apply(double... args) { double sum = 0; for (double arg : args) { sum += arg; } return sum / args.length; } }; double avgResult = new ExpressionBuilder("avg(1, 2, 3, 4)") .function(avg) .build() .evaluate(); // avgResult = 2.5 ``` -------------------------------- ### exp4j Hyperbolic Functions Source: https://context7.com/fasseg/exp4j/llms.txt Implement hyperbolic functions including sinh, cosh, tanh, csch, sech, and coth for advanced mathematical operations. ```java // Hyperbolic functions: sinh, cosh, tanh, csch, sech, coth double sinhResult = new ExpressionBuilder("sinh(1)") .build().evaluate(); // sinhResult ≈ 1.175 ``` -------------------------------- ### Create Single-Argument Custom Function in Java Source: https://context7.com/fasseg/exp4j/llms.txt Define a custom function that accepts a single argument. Ensure the function name is unique and the implementation correctly applies the logic. ```Java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; import net.objecthunter.exp4j.function.Function; // Single argument custom function Function timesPI = new Function("timespi") { @Override public double apply(double... args) { return args[0] * Math.PI; } }; double result = new ExpressionBuilder("timespi(2)") .function(timesPI) .build() .evaluate(); // result ≈ 6.283 ``` -------------------------------- ### exp4j Using pi Constant Source: https://context7.com/fasseg/exp4j/llms.txt Incorporate the 'pi' constant for calculations involving circles or trigonometric functions. ```java // Using pi in calculations double circleArea = new ExpressionBuilder("pi * r^2") .variables("r") .build() .setVariable("r", 5) .evaluate(); // circleArea ≈ 78.54 ``` -------------------------------- ### exp4j Using Golden Ratio Constant Source: https://context7.com/fasseg/exp4j/llms.txt Use the golden ratio constant 'φ' for calculations related to the golden ratio. ```java // Golden ratio φ ≈ 1.618 double goldenResult = new ExpressionBuilder("φ * 10") .build() .evaluate(); // goldenResult ≈ 16.18 ``` -------------------------------- ### Implement a custom function Source: https://github.com/fasseg/exp4j/blob/master/src/main/javadoc/overview.html Extend the Function class to define custom logic, such as a logarithm with an arbitrary base. ```java Function logb = new Function("logb", 2) { @Override public double apply(double... args) { return Math.log(args[0]) / Math.log(args[1]); } }; double result = new ExpressionBuilder("logb(8, 2)") .function(logb) .build() .evaluate(); double expected = 3; assertEquals(expected, result, 0d); ``` -------------------------------- ### exp4j Power Function Source: https://context7.com/fasseg/exp4j/llms.txt Calculate powers using the pow(base, exponent) function. ```java // Power function: pow(base, exponent) double powResult = new ExpressionBuilder("pow(2, 10)") .build().evaluate(); // powResult = 1024.0 ``` -------------------------------- ### exp4j Using e Constant Source: https://context7.com/fasseg/exp4j/llms.txt Employ the 'e' constant (Euler's number) for exponential calculations. ```java // Using e (Euler's number) double naturalExp = new ExpressionBuilder("e^2") .build() .evaluate(); // naturalExp ≈ 7.389 ``` -------------------------------- ### Create Binary Custom Operator in Java Source: https://context7.com/fasseg/exp4j/llms.txt Implement a custom binary operator, like a greater than or equal to comparison. Define its symbol, operand count (2), associativity, and precedence. ```Java // Binary comparison operator: >= Operator gteq = new Operator(">=", 2, true, Operator.PRECEDENCE_ADDITION - 1) { @Override public double apply(double... values) { return values[0] >= values[1] ? 1.0 : 0.0; } }; double compResult = new ExpressionBuilder("5 >= 3") .operator(gteq) .build() .evaluate(); // compResult = 1.0 (true) ``` -------------------------------- ### exp4j Logarithmic Functions Source: https://context7.com/fasseg/exp4j/llms.txt Perform logarithmic calculations using natural log (log), log base 10 (log10), log base 2 (log2), and log of (1+x) (log1p). ```java // Logarithmic functions: log (natural), log10, log2, log1p double logResult = new ExpressionBuilder("log(e)") .variables("e").build() .setVariable("e", Math.E).evaluate(); // logResult = 1.0 double log10Result = new ExpressionBuilder("log10(100)") .build().evaluate(); // log10Result = 2.0 double log2Result = new ExpressionBuilder("log2(8)") .build().evaluate(); // log2Result = 3.0 ``` -------------------------------- ### exp4j Built-in Constants Usage Source: https://context7.com/fasseg/exp4j/llms.txt Utilize built-in mathematical constants like pi, e, and φ (golden ratio) directly in expressions without explicit declaration. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; // Built-in constants: pi, π, e, φ (golden ratio) double result = new ExpressionBuilder("pi + π + e + φ") .build() .evaluate(); // result = 2*π + e + φ ≈ 10.97 ``` -------------------------------- ### Add Multiple Custom Functions at Once in Java Source: https://context7.com/fasseg/exp4j/llms.txt Register multiple custom functions with an `ExpressionBuilder` using the `functions()` method. This is useful when an expression relies on several user-defined functions. ```Java // Multiple custom functions Function foo = new Function("foo") { @Override public double apply(double... values) { return values[0] * Math.E; } }; Function bar = new Function("bar") { @Override public double apply(double... values) { return values[0] * Math.PI; } }; double multiResult = new ExpressionBuilder("bar(foo(log(x)))") .variables("x") .functions(foo, bar) // Add multiple functions at once .build() .setVariable("x", 32.24979131) .evaluate(); // multiResult = log(32.24...) * e * π ``` -------------------------------- ### exp4j Exponential Functions Source: https://context7.com/fasseg/exp4j/llms.txt Use exponential functions like exp(x) and expm1(x) for calculations involving powers of e. ```java // Exponential functions: exp, expm1 double expResult = new ExpressionBuilder("exp(1)") .build().evaluate(); // expResult ≈ 2.718 (e) ``` -------------------------------- ### exp4j Angle Conversion Functions Source: https://context7.com/fasseg/exp4j/llms.txt Convert angles between degrees and radians using todegree() and toradian(). ```java // Angle conversion: toradian, todegree double radResult = new ExpressionBuilder("toradian(180)") .build().evaluate(); // radResult ≈ 3.14159 (π) double degResult = new ExpressionBuilder("todegree(3.14159)") .build().evaluate(); // degResult ≈ 180.0 ``` -------------------------------- ### Use scientific notation in expressions Source: https://context7.com/fasseg/exp4j/llms.txt exp4j supports E-notation for floating-point numbers, including positive and negative exponents. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; // Basic scientific notation double result1 = new ExpressionBuilder("1e1") .build() .evaluate(); // result1 = 10.0 // Negative exponent double result2 = new ExpressionBuilder("1.11e-1") .build() .evaluate(); // result2 = 0.111 // Positive exponent with explicit sign double result3 = new ExpressionBuilder("1.11e+1") .build() .evaluate(); // result3 = 11.1 // Avogadro's number double avogadro = new ExpressionBuilder("6.02214E23") .build() .evaluate(); // avogadro = 6.02214e23 // Fine structure constant double alpha = new ExpressionBuilder("7.2973525698e-3") .build() .evaluate(); // alpha ≈ 0.00729... // Scientific notation with variables double result4 = new ExpressionBuilder("x * 1.0e5 + 5") .variables("x") .build() .setVariable("x", Math.E) .evaluate(); // result4 = e * 100000 + 5 ``` -------------------------------- ### exp4j Inverse Trigonometric Functions Source: https://context7.com/fasseg/exp4j/llms.txt Utilize inverse trigonometric functions like asin, acos, and atan for calculations involving angles. ```java // Inverse trigonometric: asin, acos, atan double atanResult = new ExpressionBuilder("atan(1)") .build().evaluate(); // atanResult ≈ 0.785 (π/4) ``` -------------------------------- ### Register Multiple Custom Operators in Java Source: https://context7.com/fasseg/exp4j/llms.txt Add multiple custom operators to an `ExpressionBuilder` to support complex expressions involving user-defined operations. Ensure correct precedence is set. ```Java // Greater than operator Operator gt = new Operator(">", 2, true, Operator.PRECEDENCE_ADDITION - 1) { @Override public double apply(double... values) { return values[0] > values[1] ? 1.0 : 0.0; } }; // Multiple operators in one expression double multiOpResult = new ExpressionBuilder("3! + 2 >= 7") .operator(factorial) .operator(gteq) .build() .evaluate(); // multiOpResult = 1.0 (6 + 2 >= 7 is true) ``` -------------------------------- ### Evaluate a mathematical expression Source: https://github.com/fasseg/exp4j/blob/master/src/main/javadoc/overview.html Use ExpressionBuilder to define variables and evaluate a string-based mathematical expression. ```java Expression e = new ExpressionBuilder("3 * sin(y) - 2 / (x - 2)") .variables("x", "y") .build() .setVariable("x", 2.3) .setVariable("y", 3.14); double result = e.evaluate(); ``` -------------------------------- ### Configure implicit multiplication Source: https://context7.com/fasseg/exp4j/llms.txt Implicit multiplication is enabled by default for numbers, variables, and functions. Use implicitMultiplication(false) to disable this behavior. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; // Implicit multiplication between number and variable double result1 = new ExpressionBuilder("2x") .variables("x") .build() .setVariable("x", 3) .evaluate(); // result1 = 6.0 // Implicit multiplication between variables double result2 = new ExpressionBuilder("xy") .variables("x", "y") .build() .setVariable("x", 2) .setVariable("y", 3) .evaluate(); // result2 = 6.0 // Implicit multiplication with functions double result3 = new ExpressionBuilder("2sin(x)") .variables("x") .build() .setVariable("x", Math.PI / 2) .evaluate(); // result3 = 2.0 // Implicit multiplication with parentheses double result4 = new ExpressionBuilder("(2)(3)") .build() .evaluate(); // result4 = 6.0 // Complex implicit multiplication double result5 = new ExpressionBuilder("2cos(xy)") .variables("x", "y") .build() .setVariable("x", 0.5) .setVariable("y", 0.25) .evaluate(); // result5 = 2 * cos(0.125) // Disable implicit multiplication try { Expression e = new ExpressionBuilder("2x") .variable("x") .implicitMultiplication(false) .build(); e.evaluate(); } catch (IllegalArgumentException ex) { System.out.println("Implicit multiplication disabled: " + ex.getMessage()); } ``` -------------------------------- ### Validate expressions with exp4j Source: https://context7.com/fasseg/exp4j/llms.txt Use the validate() method to check for structural correctness and variable assignment. You can optionally skip variable checks by passing false to the method. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; import net.objecthunter.exp4j.ValidationResult; // Validate expression with unset variables Expression e = new ExpressionBuilder("x * y + z") .variables("x", "y", "z") .build(); ValidationResult result = e.validate(); if (!result.isValid()) { System.out.println("Validation errors:"); for (String error : result.getErrors()) { System.out.println(" - " + error); } } // Output: 3 errors for unset variables x, y, z // Set variables and validate again e.setVariable("x", 1).setVariable("y", 2).setVariable("z", 3); result = e.validate(); System.out.println("Is valid: " + result.isValid()); // Output: Is valid: true // Validate structure only (skip variable check) Expression e2 = new ExpressionBuilder("x + y") .variables("x", "y") .build(); ValidationResult structureOnly = e2.validate(false); System.out.println("Structure valid: " + structureOnly.isValid()); // Output: Structure valid: true (variables not checked) // Detect invalid expressions Expression invalid = new ExpressionBuilder("x - y *") .variables("x", "y") .build(); ValidationResult invalidResult = invalid.validate(false); if (!invalidResult.isValid()) { System.out.println("Error: " + invalidResult.getErrors().get(0)); } // Output: Error: Too many operators // Check for successful validation constant if (result == ValidationResult.SUCCESS) { System.out.println("Expression is valid!"); } ``` -------------------------------- ### Utilize Unicode in Variable and Function Names Source: https://context7.com/fasseg/exp4j/llms.txt exp4j supports Unicode characters for variable and custom function names, allowing for more natural mathematical notation. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; import net.objecthunter.exp4j.function.Function; // Greek letter variables Expression e1 = new ExpressionBuilder("λ * 2") .variable("λ") .build() .setVariable("λ", Math.E); double result1 = e1.evaluate(); // result1 = 2e // Multiple Unicode variables Expression e2 = new ExpressionBuilder("log(3ε + 1)") .variable("ε") .build() .setVariable("ε", Math.E); double result2 = e2.evaluate(); // result2 = log(3e + 1) // Unicode function name Function greekLog = new Function("λωγ", 1) { @Override public double apply(double... args) { return Math.log(args[0]); } }; Expression e3 = new ExpressionBuilder("λωγ(π)") .variable("π") .function(greekLog) .build() .setVariable("π", Math.PI); double result3 = e3.evaluate(); // result3 = log(π) // Complex Unicode expression Function greekLog2 = new Function("λ_ωγ", 1) { @Override public double apply(double... args) { return Math.log(args[0]); } }; Expression e4 = new ExpressionBuilder("3λ_ωγ(πε6)") .variables("π", "ε") .function(greekLog2) .build() .setVariable("π", Math.PI) .setVariable("ε", Math.E); double result4 = e4.evaluate(); // result4 = 3 * log(π * e * 6) ``` -------------------------------- ### Combine Custom Operators in Java Source: https://context7.com/fasseg/exp4j/llms.txt Use custom operators within expressions that also contain other operations. The evaluation order depends on the defined precedence levels. ```Java // Using factorial with other operations double combined = new ExpressionBuilder("2^3!") .operator(factorial) .build() .evaluate(); // combined = 2^6 = 64.0 (3! is evaluated first due to higher precedence) ``` -------------------------------- ### Handle exp4j Expression Errors Source: https://context7.com/fasseg/exp4j/llms.txt Catch IllegalArgumentException for issues like empty expressions, unmatched parentheses, unknown functions, invalid function names, variable/function name conflicts, and incorrect argument counts. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; import net.objecthunter.exp4j.function.Function; import net.objecthunter.exp4j.operator.Operator; // Empty expression try { new ExpressionBuilder("").build(); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); // Error: Expression can not be empty } // Unmatched parentheses try { new ExpressionBuilder("(1 * 2").build(); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); // Error: Mismatched parentheses } // Unknown function try { new ExpressionBuilder("unknown(1)").build().evaluate(); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } // Missing variable value try { new ExpressionBuilder("x + y") .variables("x", "y") .build() .setVariable("x", 1) .evaluate(); // y is not set } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); // Error: No value has been set for the variable 'y' } // Invalid function name (starts with digit) try { Function invalid = new Function("1func") { @Override public double apply(double... args) { return args[0]; } }; } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); // Error: The function name '1func' is invalid } // Variable name conflicts with function name try { new ExpressionBuilder("log10(log10)") .variables("log10") .build(); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); // Error: A variable can not have the same name as a function } // Wrong number of function arguments try { new ExpressionBuilder("log(2, 2)").build().evaluate(); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } ``` ```java // Division by zero try { new ExpressionBuilder("1 / 0").build().evaluate(); } catch (ArithmeticException e) { System.out.println("Error: " + e.getMessage()); // Error: Division by zero } ``` -------------------------------- ### exp4j Signum Function Source: https://context7.com/fasseg/exp4j/llms.txt Determine the sign of a number using the signum() function, which returns -1, 0, or 1. ```java // Signum function double signumResult = new ExpressionBuilder("signum(-42)") .build().evaluate(); // signumResult = -1.0 ``` -------------------------------- ### Copy Expression for Independent Evaluation Source: https://context7.com/fasseg/exp4j/llms.txt Create independent copies of an `Expression` object using its copy constructor. Modifications to a copied expression do not affect the original. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; // Create original expression Expression original = new ExpressionBuilder("x^2 + y^2") .variables("x", "y") .build() .setVariable("x", 3) .setVariable("y", 4); double originalResult = original.evaluate(); // originalResult = 25.0 // Create a copy Expression copy = new Expression(original); // Modify copy without affecting original copy.setVariable("x", 5).setVariable("y", 12); double copyResult = copy.evaluate(); // copyResult = 169.0 // Original still has its values double checkOriginal = original.evaluate(); // checkOriginal = 25.0 (unchanged) ``` -------------------------------- ### Implement a custom operator Source: https://github.com/fasseg/exp4j/blob/master/src/main/javadoc/overview.html Extend the Operator class to define custom operators, specifying symbol, operand count, associativity, and precedence. ```java Operator factorial = new Operator("!", 1, true, Operator.PRECEDENCE_POWER + 1) { @Override public double apply(double... args) { final int arg = (int) args[0]; if ((double) arg != args[0]) { throw new IllegalArgumentException("Operand for factorial has to be an integer"); } if (arg < 0) { throw new IllegalArgumentException("The operand of the factorial can not be less than zero"); } double result = 1; for (int i = 1; i <= arg; i++) { result *= i; } return result; } }; double result = new ExpressionBuilder("3!") .operator(factorial) .build() .evaluate(); double expected = 6d; assertEquals(expected, result, 0d); ``` -------------------------------- ### Evaluate Expression Asynchronously Source: https://context7.com/fasseg/exp4j/llms.txt Use `ExecutorService` and `Future` for asynchronous expression evaluation. This allows other operations to proceed while the expression is being computed. ```java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; ExecutorService executor = Executors.newFixedThreadPool(4); try { Expression e = new ExpressionBuilder("3 * log(y) / (x + 1)") .variables("x", "y") .build() .setVariable("x", 2.3) .setVariable("y", 3.14); // Evaluate asynchronously Future futureResult = e.evaluateAsync(executor); // Do other work while expression evaluates... System.out.println("Computing..."); // Get result (blocks until complete) double result = futureResult.get(); System.out.println("Result: " + result); // Result ≈ 1.047 } catch (Exception ex) { ex.printStackTrace(); } finally { executor.shutdown(); } ``` -------------------------------- ### Create Unary Custom Operator in Java Source: https://context7.com/fasseg/exp4j/llms.txt Define a custom unary operator, such as factorial. Specify its symbol, number of operands (1), associativity (true for right-associative), and precedence. ```Java import net.objecthunter.exp4j.Expression; import net.objecthunter.exp4j.ExpressionBuilder; import net.objecthunter.exp4j.operator.Operator; // Unary operator: factorial (!) Operator factorial = new Operator("!", 1, true, Operator.PRECEDENCE_POWER + 1) { @Override public double apply(double... args) { final int arg = (int) args[0]; if ((double) arg != args[0]) { throw new IllegalArgumentException("Operand for factorial must be an integer"); } if (arg < 0) { throw new IllegalArgumentException("Factorial operand cannot be negative"); } double result = 1; for (int i = 1; i <= arg; i++) { result *= i; } return result; } }; double factorialResult = new ExpressionBuilder("5!") .operator(factorial) .build() .evaluate(); // factorialResult = 120.0 ``` -------------------------------- ### exp4j Complex Nested Expression Evaluation Source: https://context7.com/fasseg/exp4j/llms.txt Evaluate complex mathematical expressions involving multiple functions and operations. ```java // Complex nested expressions double complexResult = new ExpressionBuilder("(sin(12) + log(34)) * 3.42 - cos(2.234-log(2))") .build().evaluate(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.