Exun (exun v0.5.0)

Symbolic Math for Elixir, with Units support

Link to this section Summary

Functions

Get a list of variables down a tree, checking cyclic definition from 'context' user defs. Check cyclic definitions in 'context', a map that holds values like %{"f" => "x^2+2*x+3"}. If you pass a cyclic map this function detects it, for example

Eval an ast, returns an ast(not text expression)

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 ParsedContext (pc) in tree

Link to this section Functions

Link to this function

check_cyclic(context)

Get a list of variables down a tree, checking cyclic definition from 'context' user defs. Check cyclic definitions in 'context', a map that holds values like %{"f" => "x^2+2*x+3"}. If you pass a cyclic map this function detects it, for example:

%{"a"=>"b", "b"=>"c", "c"=>"a"}

Eval an ast, returns an ast(not text expression)

Link to this function

eval2str(txt, ctx \\ %{})

Link to this function

new(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.

Exun.new("x[m^2]", %{"x"=>"3"}) "3[m^2]"

Exun.new("x[m^2]", %{"x"=>"3"}).ast {:unit, {:numb, 3, 1}, {:elev, {:vari, "m"}, {:numb, 2, 1}}}

Exun.new("x[m^2]", %{"x"=>"3"}).pc %{{:vari, "x"} => {:numb, 3, 1}}

Exun.UI.tostr(Exun.new( "x[m^2]", %{"x"=>"3"})) "3[m^2]"

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

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(ast, pc)

Replace definitions in ParsedContext (pc) in tree