Skip to content

Derivatives

Soulaymen Chouri edited this page Jan 28, 2019 · 3 revisions

Update: This feature is now deprecated.

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): https://i.imgur.com/h6n9xPq.png
  • f'(x): https://i.imgur.com/Fng3OLY.png
  • f''(x): https://i.imgur.com/vORHBIk.png
  • f'''(x): https://i.imgur.com/H4QoywV.png

I did not dare test f^(4) (x) yet...

Clone this wiki locally