Giter VIP home page Giter VIP logo

math-expression-parser's Introduction

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.2</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

Release

# Ref: https://central.sonatype.org/pages/apache-maven.html
mvn release:clean release:prepare
mvn release:perform

math-expression-parser's People

Contributors

benjaminjacobberg avatar facaiy avatar upx86 avatar vbachurin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

math-expression-parser's Issues

'ArrayIndexOutOfBoundsException: 2' for all 2-arguments methods from scala.math

Hi,

Any 2 arguments function from scala.math (max, min, pow, atan2, hypot) throws a ArrayIndexOutOfBoundsException:

ex: io.github.facaiy.math.expression.Expression[String => Double,Double] = Expression(<function1>)
java.lang.ArrayIndexOutOfBoundsException: 2
at io.github.facaiy.math.expression.Expression$$anonfun$toExpression$3.apply(Expression.scala:29)
at io.github.facaiy.math.expression.Expression$$anonfun$toExpression$3.apply(Expression.scala:26)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:44)
... 38 elided

Code example:

val ex = io.github.facaiy.math.expression.MathExp.parse("pow(2, 10)")
ex.eval(Map.empty)

Provide API for registering other functions.

We are interested in using your library but would like to extend it to other types, for example Saddle Vecs.

For example, be able to define operators that are Map[String, Vec[Double] => Vec[Double]]

As it stands now, we have to fork your library, and rewrite object FunctionRegister to have two different function registers: OperatorDouble and OperatorVector

(Saddle is a Scala library for numeric operations not unlike Pandas - http://saddle.github.io/)

Travis CI - master branch build failure

The Travis CI build for math-expression-parser is failing.

Installing oraclejdk8
$ export JAVA_HOME=~/oraclejdk8
$ export PATH="$JAVA_HOME/bin:$PATH"
$ ~/bin/install-jdk.sh --target "/home/travis/oraclejdk8" --workspace "/home/travis/.cache/install-jdk" --feature "8" --license "BCL"
Ignoring license option: BCL -- using GPLv2+CE by default
install-jdk.sh 2019-07-17
Expected feature release number in range of 9 to 14, but got: 8
The command "~/bin/install-jdk.sh --target "/home/travis/oraclejdk8" --workspace "/home/travis/.cache/install-jdk" --feature "8" --license "BCL"" failed and exited with 3 during .
Your build has been stopped.

I propose we use OpenJDK8.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.