4

I am trying to figure out how to create and manipulate exact differentials (algebraic treatment of Leibniz notation) in SymPy. To be clear, it is well known that

from sympy import *
init_printing()
x, y=symbols('x, y')
f=Function('f')(x, y)
Derivative(f, x)

yields

enter image description here

In the case above, the exact differential is

enter image description here

For a physicist this is especially needed given the kind of abuse we do to the chain rule but also for simple things like V = A \ell and dV = dA d\ell, when working with integrals or this classic physics abuse of the Leibniz notation that occurs all the time in thermodynamics.

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
SKArm
  • 71
  • 1
  • 7

1 Answers1

0

Here is something very naive for univariate functions.

>>> from sympy import *
>>> x  = Symbol(' x')
>>> dx = Symbol('dx')
>>> def f (x):
...     return x**3
>>> def g (x):
...     return sin(x)

Let us take the differentials of f and g using their Taylor expansions in differential dx:

>>> f(x+dx).series(dx,0,2) - f(x)
3*dx*x**2 + O(dx**2)
>>> g(x+dx).series(dx,0,2) - g(x)
dx*cos(x) + O(dx**2)

where, by definition, O(dx**2) is zero. Of course, we could also use differentiation (diff):

>>> diff(x**3,x) * dx
3*dx*x**2
>>> diff(sin(x),x) * dx
dx*cos(x)
Rodrigo de Azevedo
  • 1,097
  • 9
  • 17