Exun (exun v0.4.4)
Symbolic Math for Elixir, with Units support
Link to this section Summary
Functions
Eval an ast (not text expression)
Parse and evaluate an expression. If ast is true returns de AST tuple, if it is false return a human-readable (and parseable) expression.
Same as eval but returns AST
Parse a math expression, not a 'equality', with context definitions For example, express 'x' squared meters, and then define x to be 3.
Parses an expression in text format. Special symbols used
Replace definitions in ParsedContext (pc) in tree
Link to this section Functions
ast_eval(ast, pctx \\ %{})
Eval an ast (not text expression)
eval(txt, context \\ %{})
Parse and evaluate an expression. If ast is true returns de AST tuple, if it is false return a human-readable (and parseable) expression.
iex> Exun.eval "x[m^2]+4[cm^2]",%{"x"=>"3"}
"3.0004[m^2]"
eval_ast(txt, context \\ %{})
Same as eval but returns AST
parse(txt, context \\ %{})
Parse a math expression, not a 'equality', with context definitions For example, express 'x' squared meters, and then define x to be 3.
iex> Exun.parse( "x[m^2]", %{"x"=>"3"})
{{:unit, {:vari, "x"}, {:elev, {:vari, "m"}, {:numb, 2}}}, %{{:vari, "x"} => {:numb, 3}}}
returns a tuple {expression, parsed_context} where expression is a tuple that holds math AST and parsed_context is a map whith all equalities (definitions) parsed as "name" => expression See parse_txt for an explanation
parse_text(txt)
Specs
Parses an expression in text format. Special symbols used:
- An Unit: <number>[UnitExpression] : 1[m/s]
- A multiplication, division, suma, rest and parenthesis works in the usual way: 2x-3(y-2)
- Function call: fname(args): sin(x)
- A derivate: <expression>'<var> : sin(x)'x. Symbol (') has the greatest priority, for example y^x'x is equals to y^(x'x). Use () to be clear: (y^x)'x
- An integral: $<expression>,<var>: $tan(x),x
- You can sum units: 1[m]+1[cm] if are of the same magnitude. For example 1[m]+1[s] will throw an exception. You can multiple/divide any two unis.
replace(tree, pc)
Replace definitions in ParsedContext (pc) in tree