ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - Rob-bie/expr: An Elixir library for parsing and evaluating mathematical expressions
An Elixir library for parsing and evaluating mathematical expressions - Rob-bie/expr
Visit Site

GitHub - Rob-bie/expr: An Elixir library for parsing and evaluating mathematical expressions

GitHub - Rob-bie/expr: An Elixir library for parsing and evaluating mathematical expressions

expr

expr is an Elixir library for parsing and evaluating mathematical expressions.

Installation

Add Expr to your mix.exs dependencies:

def deps do
  [{:expr, "~> 0.1.0"}]
end

Afterwards, run the command mix deps.get and you're good to go.

Examples

Operators and constants
Operator Precedence Associativity
log10 4 RIGHT
floor 4 RIGHT
ceil 4 RIGHT
asin 4 RIGHT
acos 4 RIGHT
atan 4 RIGHT
sqrt 4 RIGHT
log 4 RIGHT
tan 4 RIGHT
cos 4 RIGHT
sin 4 RIGHT
abs 4 RIGHT
! 4 RIGHT
# 4 RIGHT
^ 4 RIGHT
/ 3 LEFT
* 3 LEFT
+ 2 LEFT
- 2 LEFT
Symbol Value
pi 3.14 ...
e 2.71 ...
Basic examples

The result of all evaluations are returned at the front of a list. As of version 0.1.0 there is no error checking or expression validation. If something goes wrong, an arithmetic error will be thrown. While the parser will understand your intent for most expressions, the more explicit you are, the better. (Be liberal with parenthesis if you're getting unexpected results!)

Expr.eval!("1 + 2 * 3")
=> [7.0]

Expr.eval!("5/2")
=> [2.5]

Expr.eval!("5! + abs(-5)")
=> [125.0]

Expr.eval!("--4") #Negation
=> [4.0]

Expr.eval!("5(sqrt(abs(-16)))") #Implicit multiplication
=> [20.0]

Expr.eval!("(5^2)(floor(2.75))") #Implicit multiplication #2
=> [50.0]

Expr.eval!("sin(60)") #RADIANS!
=> [-0.3048106211022167]
Variable usage

Valid variable names cannot contain operator names, constant names or start with numbers. Starting with capital letters, containing numbers and unused symbols are fine. However, I recommend using short, lowercase variable names. A map of variable names and their values are passed to Expr.eval! along with an expression when evaluating expressions that contain variables.

Expr.eval!("x + y / x", %{"x" => 2, "y" => 4})
=> [3.0]

Expr.eval!("-x@ + 1", %{"x@" => 2})
=> [-1.0]

Expr.eval!("-(sqrt(abs(some_var)))", %{"some_var" => -2.5})
=> [-1.5811388300841898]

Expr.eval!("ABC+2^CBA", %{"ABC" => 2, "CBA" => 3})
=> [10.0]

vars = %{"a" => 2.5, "b" => 3, "c" => 0.25, "d" => 10, "z" => 6.5}
Expr.eval!("a^(b+c)-d(z)", vars)
=> [-45.35260266120413]
Caveats
  • Technically you can have a variable name that is the same as a constant name, however the constant's value will override the value of the variable.

Expr.eval!("pi + 1", %{"pi" => 1}) => 4.141592...

  • Sometimes a malformed expression will actually evaluate and return a list of length >1, whenever this happens know something has gone wrong.

  • Complex numbers are not supported, it's not uncommon that you'll have a properly formed expression and somewhere during the calculation an arithmetic error will be thrown. For example, trying to take the square root of a negative number.

  • Negation is represented by # under the hood, # can be used in expressions.

  • Implicit multiplication is currently only partially supported. While pi(r^2) is entirely valid, pir^2 is unfortunately not. This may be coming in a future version.

TODO

  • Function support for functions with arity >2
  • More operator/constant support
  • Expression validator
  • Improved implicit multiplication
  • Clean up messy parser code
  • Comments.

License

This work is free. You can redistribute it and/or modify it under the
terms of the MIT License. See the LICENSE file for more details.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory