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

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 \\ %{})

Same as eval but returns AST

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}}}, %{{: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

Link to this function

parse_text(txt)

Link to this function

replace(tree, pc)

Replace 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"}}, %{{:vari, "z"} => {:suma, {:vari, "y"}, {:numb, 1}}}}