Exun (exun v0.1.0)
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 iex> Exun.eval "x[m^2]+4[cm^2]",%{"x"=>"3"} "3.0004[m^2]"
Parse a math expression, without context: iex(1)> Exun.parse "x*y^(1+x)" {:mult, {:vari, "x"}, {:elev, {:vari, "y"}, {:suma, {:numb, 1}, {:vari, "x"}}}}
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}}}, %{"x" => {:numb, 3}}}
Expand definitions in context into main tree expression until no more expansion is posssible
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"}}, %{"z" => {:suma, {:vari, "y"}, {:numb, 1}}}}
Link to this section Functions
conctostr(hpri, hstr, lpri, ltxt, rpri, rtxt)
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)
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)
eval_ast(txt, context)
innertostr(arg)
parse(txt)
Parse a math expression, without context: iex(1)> Exun.parse "x*y^(1+x)" {:mult, {:vari, "x"}, {:elev, {:vari, "y"}, {:suma, {:numb, 1}, {:vari, "x"}}}}
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}}}, %{"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)
repl(other, pc)
replace(tree, pc)
Expand 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"}}, %{"z" => {:suma, {:vari, "y"}, {:numb, 1}}}}