Exun (exun v0.2.0)
Symbolic Math for Elixir, with Units support
Link to this section Summary
Functions
Tree equality, normalize compounds '' and '+' because {,{,1,2},{,3,4}} == {,{,1,3},{,2,4}} so transform both trees to {{:m,}[1,2,3,4]} before compare
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.
Replace definitions in context into main tree expression until no more expansion is posssible
Translate tree to human readable math expression
Link to this section Functions
eq(t1, t2)
Tree equality, normalize compounds '' and '+' because {,{,1,2},{,3,4}} == {,{,1,3},{,2,4}} so transform both trees to {{:m,}[1,2,3,4]} before compare
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_conext} where expression is a tuple that holds math AST and parsed_context is a map whith all equalities (definitions) parsed as "name" => expression
parse_text(txt)
replace(tree, pc)
Replace definitions in context into main tree expression until no more expansion is posssible
tostr(tree)
Translate tree to human readable math expression:
iex(1)> {_tree, _deps} = Exun.parse "4*x^(y+1)/z",%{"z"=>"y+1"}
{{:divi,
{:mult, {:numb, 4}, {:elev, {:vari, "x"}, {:suma, {:vari, "y"}, {:numb, 1}}}},
{:vari, "z"}}, %{{:vari, "z"} => {:suma, {:vari, "y"}, {:numb, 1}}}}