A java library capable of parsing and evaluating mathematical expressions.
- Basic arithmetic operations
- Trigonometric functions
- Logarithmic functions
- Exponential functions
- Calculus
- Complex numbers
- Vectors
- Matrices
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("2+2"); // 2 + 2
System.out.println(expression.evaluate());
}
}
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("sin(pi/2)"); // sin(π/2)
System.out.println(expression.evaluate());
}
}
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("log(10, 2)"); // log₂(10)
System.out.println(expression.evaluate());
}
}
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("2^3"); // 2³
System.out.println(expression.evaluate());
}
}
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("x^2"); // x²
expression.withVariable("x", 2);
System.out.println(expression.evaluate());
}
}
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("sum(1, 2)"); // 1 + 2
expression.withFunction("sum", (values) -> values[0] + values[1]);
System.out.println(expression.evaluate());
}
}
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("pi"); // π
System.out.println(expression.evaluate());
}
}
import dev.joao_guilherme.Expression;
import dev.joao_guilherme.evaluators.PercentageBasedAdditionEvaluator;
public class Main {
public static void main(String[] args) {
Expression expression = new Expression("2000+50%", new PercentageBasedAdditionEvaluator()); // 2000 + 50% of 2000
System.out.println(expression.evaluate());
}
}
This feature allows you to solve equations for a given variable.
import dev.joao_guilherme.Expression;
public class Main {
public static void main(String[] args) {
Expression equation = new Expression("2^x = 16");
System.out.println(equation.solveForX()); // x = 4
}
}
This feature is still in beta and may not work as expected.
sqrt(x)
: Returns the square root ofx
.nrt(n, x)
: Returns then
-th root ofx
.log(x, base)
: Returns the logarithm ofx
in the basebase
.ln(x)
: Returns the natural logarithm ofx
.exp(x)
: Returns the exponential ofx
.sin(x)
: Returns the sine ofx
.cos(x)
: Returns the cosine ofx
.tan(x)
: Returns the tangent ofx
.abs(x)
: Returns the absolute value ofx
.ceil(x)
: Returns the smallest integer greater than or equal tox
.floor(x)
: Returns the largest integer less than or equal tox
.
pi
: The mathematical constant π.e
: The mathematical constant e.
<dependency>
<groupId>dev.joao_guilherme</groupId>
<artifactId>math-expression</artifactId>
<version>0.3.3</version>
</dependency>