Releases: HugoGranstrom/symbolicnim
subs, better printing, copy and for loops
subs
can be used to substitute parts of an expression with another. For examplesubs((x - y)^2 + z, x-y, z)
givesz^2 + z
.- A basic taylor expansion using subs behind the scenes. Use it like this:
taylor(sin(x), x, 0, 5)
to get the 5 first terms of sin's taylor series around x=0 (i.e. the Maclaurin series). - Better and more consistent printing
- You can copy a expression using
let newCopy = copy(symExpr)
which does a deep copy. On the lower level we havecopySymNode
(shallow) andcopySymTree
(deep). - Added the ability to loop over expressions. I.e. over the terms in a
Add
and the factors in aMul
. - Check if a
SymExpr
is a number, symbol, Add, Mul, pow or func using:isNumber
,isSymbol
,isFunc
,isPow
,isAdd
,isMul
.
Fix a bug where constant * Add didn't work
Bugfix so expressions like 2*(x+y) is handled correctly and is simplified to 2x + 2y
Fixed a performance bottleneck
There was a performance bottleneck in the hashing proc which is now sorted out. In the benchmark in the tests folder I got a 50x speedup and am roughly 10x faster than sympy (and maaaany times faster than the old version).
New AST + compile time support + generate pure nim procs
The AST has gotten an overhaul to be able to run at both runtime and compiletime. It should now be on par with the old AST plus a bit more.
It has also gotten the ability to generate fast pure nim procs from symbolic expressions. The symbolic expression sin(x*y^2)
gets transformed into a proc like proc f(x, y: float): float = sin(x*pow(y, 2))
.
Also the extensibility of the library have improved. For example custom mathematical functions can be generated using the registerSymFunc
template which you get access to by importing symbolicnim/backend
. For examples see exp, ln, sin, cos, tan in deepEmbeddings.nim
.
In addition to this, custom types that contains symbolic expressions can also be compiled if provided a compile macro. A proof of concept SymMatrix
type has been created that generates an Arraymancer Tensor when compiled.
Added createVars macro
You can now create many variables in a more convinient way using the createVars
macro:
createVars(x, y, coolVar)
This is transformed to:
let x = newVariable("x")
let y = newVariable("y")
let coolVar = newVariable("coolVar")
Initial Release
Right now you can create variables like this:
let x = newVariable("x")
And combine them using the operators +
, -
, *
, /
and ^
. The mathematical functions available right now is sin
, cos
, tan
, exp
and ln
.
Derivatives can be taken using the diff
proc:
diff(x^2, x) # d/dx(x^2)
diff(x^2, x , 2) # d^2/dx^2(x^2)
diff(x^2, x, y) # d/dy(d/dx(x^2))