:plus
:minus
:times
:divided_by
:raised_to
:log10
:log
:log2
:cos
:sin
:tan
:e
:pi
The atom:e
represents Euler's number.:pi
represents the mathemetical constant π.
Representing 2x
iex> Expression.new(2, :times, :x) |> IO.puts
(2 * x)
Representing 2*cos(x^2)
iex> Expression.new(2, :times, Expression.new(:cos, Expression.new(:x, :raised_to, 2))) |> IO.puts
(2 * cos((x ^ 2)))
If your expression contains variables, you can give those variables values with the Expression.set_variable
function.
iex> Expression.new(:sin, Expression.new(:x, :raised_to, 2)) |> Expression.set_variable(:x, :pi) |> IO.puts
sin((pi ^ 2))
If an expression does not contain any variables, the expression can be evaluated.
iex> Expression.new(:sin, Expression.new(:x, :raised_to, 2)) |> Expression.set_variable(:x, :pi) |> Expression.evaluate |> IO.puts
-0.43030121700009166
The Expression.Simplifier
module allows for basic simplifications like 0 * x => 0
, x^1 => x
. As of now, expressions like 2x + x
don't get simplified to 3x
.
Simplifying 1^1^1
:
iex> Expression.new(1, :raised_to, Expression.new(1, :raised_to, 1)) |> Expression.Simplifier.simplify |> IO.puts
1.0
Computing the derivative of cos(sin(x))
with respect to x
and simplifying the result:
iex> Expression.new(:cos, Expression.new(:sin, :x)) |> Expression.Differentiator.differentiate(:x) |> Expression.Simplifier.simplify |> IO.puts
((-1 * sin(sin(x))) * cos(x))
:ok
The Expression.TaylorSeries
module allows you to compute Taylor and McLaurin series of functions.
First five terms for the Maclaurin series of sin(x)
:
iex> Expression.new(:sin, :x) |> Expression.TaylorSeries.compute_maclaurin_series(:x, 5) |> IO.puts
((((sin(x) + (cos(x) * x)) + (((-1 * sin(x)) / 2) * (x ^ 2))) + (((-1 * cos(x)) / 6) * (x ^ 3))) + (((-1 * (-1 * sin(x))) / 24) * (x ^ 4)))
- Add more functions (and the corresponding differentiation rules).
- Add simplifications rules for more complicated rules like
a*x + b*x => (a+b)*x
or maybe evensin(2pi*x) => 0
- Write unit tests.