-
Notifications
You must be signed in to change notification settings - Fork 1
Derivatives
CGraph allows you to compute a graph's derivatives with respect to a single variable.
Currently, the graph's derivative is implemented only from the C API. There is no Lua API to handle derivatives yet. Update: Just kidding there is one!
Graph's derivative is a graph itself, however it is very unoptimized as it uses generic derivative forms to generate node, for example f(x) = x^2
is treated as f(x) = g(x)^h(x)
and the derivative of such power function is f^g * (g'*ln(f) + g*(f'/f)
.
There is definitely room for optimizations here! One can check if the LHS of the power function is a constant value then we can use less generic and more optimized derivative functions.
Let's have a look how to use the derivative's Lua API:
We want to model the function f(x) = 2x^3 + 3
local A = CGraph.double(2)
local B = CGraph.pow(CGraph.variable('x'), CGraph.double(3) )
local C = CGraph.double(3)
local f = A*B + C
local graph = CGraph.graph("test", f)
graph:plot()
local dgraph = graph:diff('x', 'diff')
dgraph:plot()
local ddgraph = dgraph:diff('x', 'diff_2')
ddgraph:plot()
local dddgraph = ddgraph:diff('x', 'diff_3')
dddgraph:plot()
Here what we get:
-
f(x)
: -
f'(x)
: -
f''(x)
: -
f'''(x)
:
I did not dare test f^(4) (x)
yet...