A scala library for parsing mathemitical expressions with support for parentheses and variables.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dev
src
.travis.yml
LICENSE
README.md
pom.xml
scalastyle-config.xml
scalastyle-on-compile.generated.xml

README.md

math-expression-parser

A scala library for parsing mathemitical expressions with support for parentheses and variables.

features:

  • math operators: +, -, *, /, **(power)
  • parentheses ( ) and comma ,
  • all function of scala.math, except of random, E and PI
  • variable name: $ with valid Java variable name

Install

  1. maven
    <dependency>
      <groupId>io.github.facaiy</groupId>
      <artifactId>math-expression-parser</artifactId>
      <version>0.0.1</version>
    </dependency>
    

Usage

A simple example:

import io.github.facaiy.math.expression.MathExp

val str = "1.0 + sqrt(2 * $a1) + $a2 ** 2"
val ex = MathExp.parse(str)

val variables = Map("a1" -> 2, "a2" -> 1)
val output = ex.eval(variables)
// output = 4.0

val output1 = ex.eval(Map("a1" -> 8.0, "a2" -> 2))
// output1 = 9.0