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

Link to this function

conctostr(hpri, hstr, lpri, ltxt, rpri, rtxt)

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

Link to this function

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]"

Link to this function

eval_ast(txt, context)

Link to this function

innertostr(arg)

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"}}}}

Link to this function

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

Link to this function

parse_text(txt)

Link to this function

repl(other, pc)

Link to this function

replace(tree, pc)

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}}}}