Exun (exun v0.4.4)

Symbolic Math for Elixir, with Units support

Link to this section Summary

Functions

Eval an ast (not text expression)

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.

Parses an expression in text format. Special symbols used

Replace definitions in ParsedContext (pc) in tree

Link to this section Functions

Link to this function

ast_eval(ast, pctx \\ %{})

Eval an ast (not text expression)

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_context} where expression is a tuple that holds math AST and parsed_context is a map whith all equalities (definitions) parsed as "name" => expression See parse_txt for an explanation

Link to this function

parse_text(txt)

Specs

parse_text(binary()) :: any()

Parses an expression in text format. Special symbols used:

  • An Unit: <number>[UnitExpression] : 1[m/s]
  • A multiplication, division, suma, rest and parenthesis works in the usual way: 2x-3(y-2)
  • Function call: fname(args): sin(x)
  • A derivate: <expression>'<var> : sin(x)'x. Symbol (') has the greatest priority, for example y^x'x is equals to y^(x'x). Use () to be clear: (y^x)'x
  • An integral: $<expression>,<var>: $tan(x),x
  • You can sum units: 1[m]+1[cm] if are of the same magnitude. For example 1[m]+1[s] will throw an exception. You can multiple/divide any two unis.
Link to this function

replace(tree, pc)

Replace definitions in ParsedContext (pc) in tree