Exun (exun v0.1.1)

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

Same as eval but with empty 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.

Same as eval_ast but with empty context

Same as eval but returns AST

Parse a math expression, without context

Parse a math expression, not a 'equality', with context definitions For example, express 'x' squared meters, and then define x to be 3.

Expand 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

Same as eval but with empty context

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

Same as eval_ast but with empty context

Link to this function

eval_ast(txt, context)

Same as eval but returns AST

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

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