# Partial Derivatives in sympy


```python
import sympy
```


```python
x, u = sympy.symbols('x u', real=True)
```


```python
U = sympy.Function('U')(x,u)
```


```python
U
```




    U(x, u)



### The case of a(n arbitrary) point transformation

cf. Introduction to Differential Invariants, Chapter 2 Lie Transformations pp. 16


```python
x = sympy.Symbol('x',real=True)
```


```python
y = sympy.Function('y')(x)
```


```python
U = sympy.Function('U')(x,y)
X = sympy.Function('X')(x,y)
Y = sympy.Function('Y')(X)
```


```python
sympy.pprint(sympy.diff(U,x))
```

    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(U(x, ξ₂))⎟│        + ⎜───(U(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x



```python
sympy.pprint( sympy.diff(Y,x))
```

    ⎛d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    ⎞ ⎛ d        ⎞│    
    ⎜──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    ⎟⋅⎜───(Y(ξ₁))⎟│    
    ⎝dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x⎠ ⎝dξ₁       ⎠│ξ₁=X
    
             
             
    (x, y(x))



```python
sympy.pprint( sympy.diff(Y,x).args[0] )
```

    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x



```python
sympy.pprint( sympy.diff(U,x)/ sympy.diff(Y,x).args[0])
```

    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(U(x, ξ₂))⎟│        + ⎜───(U(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x
    ──────────────────────────────────────────────────────────
    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x


For $Y''(X)$,


```python
YprimeX = sympy.diff(U,x)/sympy.diff(Y,x).args[0]
sympy.pprint( sympy.diff(YprimeX,x).simplify() )
```

                                                                   ⎛⎛            2
      ⎛d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    ⎞ ⎜⎜d          d 
    - ⎜──(y(x))⋅⎜───(U(x, ξ₂))⎟│        + ⎜───(U(ξ₁, y(x)))⎟│    ⎟⋅⎜⎜──(y(x))⋅────
      ⎝dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x⎠ ⎜⎜dx           
                                                                   ⎝⎝         dy(x
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                                                                  
                                                                                  
                                                                                  
    
                         2               ⎞                     ⎛   2            ⎞│
                        d                ⎟ d          d        ⎜  ∂             ⎟│
    ──(X(x, y(x))) + ────────(X(x, y(x)))⎟⋅──(y(x)) + ──(y(x))⋅⎜──────(X(x, ξ₃))⎟│
     2               dx dy(x)            ⎟ dx         dx       ⎝∂ξ₃ ∂x          ⎠│
    )                                    ⎠                                        
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                                                                  
                                                                                  
                                                                                  
    
                2                 2                              ⎞                
               d                 d        ⎛ ∂           ⎞│       ⎟   ⎛d        ⎛ ∂
            + ───(X(x, y(x))) + ───(y(x))⋅⎜───(X(x, ξ₂))⎟│       ⎟ + ⎜──(y(x))⋅⎜──
    ξ₃=y(x)     2                 2       ⎝∂ξ₂          ⎠│ξ₂=y(x)⎟   ⎝dx       ⎝∂ξ
              dx                dx                               ⎠                
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                    ⎛d        ⎛ ∂           ⎞│          ⎛ ∂       
                                    ⎜──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁,
                                    ⎝dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁      
    
                                                    ⎛⎛            2               
               ⎞│          ⎛ ∂              ⎞│    ⎞ ⎜⎜d          d                
    ─(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    ⎟⋅⎜⎜──(y(x))⋅──────(U(x, y(x))) 
    ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x⎠ ⎜⎜dx            2             
                                                    ⎝⎝         dy(x)              
    ──────────────────────────────────────────────────────────────────────────────
                  2                                                               
           ⎞│    ⎞                                                                
     y(x)))⎟│    ⎟                                                                
           ⎠│ξ₁=x⎠                                                                
    
          2               ⎞                     ⎛   2            ⎞│            2  
         d                ⎟ d          d        ⎜  ∂             ⎟│           d   
    + ────────(U(x, y(x)))⎟⋅──(y(x)) + ──(y(x))⋅⎜──────(U(x, ξ₃))⎟│        + ───(U
      dx dy(x)            ⎟ dx         dx       ⎝∂ξ₃ ∂x          ⎠│ξ₃=y(x)     2  
                          ⎠                                                  dx   
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                                                                  
                                                                                  
                                                                                  
    
                   2                              ⎞
                  d        ⎛ ∂           ⎞│       ⎟
    (x, y(x))) + ───(y(x))⋅⎜───(U(x, ξ₂))⎟│       ⎟
                   2       ⎝∂ξ₂          ⎠│ξ₂=y(x)⎟
                 dx                               ⎠
    ───────────────────────────────────────────────
                                                   
                                                   
                                                   
                                                   



```python
sympy.factor_list( sympy.diff(Y,x)) # EY 20160522 I don't know how to simply obtain the factors of an expression 
# EY 20160522 update resolved: look at above and look at this page; it explains all: 
# http://docs.sympy.org/dev/tutorial/manipulation.html
```




    (1,
     [(Subs(Derivative(Y(_xi_1), _xi_1), (_xi_1,), (X(x, y(x)),)), 1),
      (Derivative(y(x), x)*Subs(Derivative(X(x, _xi_2), _xi_2), (_xi_2,), (y(x),)) + Subs(Derivative(X(_xi_1, y(x)), _xi_1), (_xi_1,), (x,)),
       1)])




```python
t, x, u, u_1, x_t, u_t, u_1t = sympy.symbols('t x u u_1 x_t u_t u_1t', real=True)
X = -u_1
U = u - x*u_1
U_1 = x
```

cf. How to do total derivatives: http://robotfantastic.org/total-derivatives-in-sympy.html


```python
from sympy import Derivative, diff, expr
def difftotal(expr, diffby, diffmap):
    """Take the total derivative with respect to a variable.

    Example:

        theta, t, theta_dot = symbols("theta t theta_dot")
        difftotal(cos(theta), t, {theta: theta_dot})

    returns

        -theta_dot*sin(theta)
    """
    # Replace all symbols in the diffmap by a functional form
    fnexpr = expr.subs({s:s(diffby) for s in diffmap})
    # Do the differentiation
    diffexpr = diff(fnexpr, diffby)
    # Replace the Derivatives with the variables in diffmap
    derivmap = {Derivative(v(diffby), diffby):dv 
                for v,dv in diffmap.iteritems()}
    finaldiff = diffexpr.subs(derivmap)
    # Replace the functional forms with their original form
    return finaldiff.subs({s(diffby):s for s in diffmap})
```


```python
difftotal( U,t,{x:x_t, u:u_t, u_1:u_1t}) + (-U_1)* (-u_1t)
```




    -u_1*x_t + u_t



This transformation is the Legendre transformation

cf. 4. Exercises Chapter 2 Lie Transformations Introduction to Differential Invariants.   

Consider transformation $(x,u)=(x,u(x)) \to (X,U)=(X(x,u),U(x,u))=(u,x)$.  Let $Y=Y(X)$.  $Y(X) \in \Gamma(\mathbb{R}^1 \times \mathbb{R}^1)$, i.e. $Y(X)$ is a section. So $Y(X) = Y(X(x,u)) = U(x,u)$.  And so in this case,
$Y(X(x,u))=Y(u)=U(x,u) = x$


```python
x = sympy.Symbol('x',real=True)
u = sympy.Function('u')(x)
U = x
X = u
Y = sympy.Function('Y')(X)
```


```python
sympy.pprint( sympy.diff(Y,x))
```

    d        ⎛ d        ⎞│       
    ──(u(x))⋅⎜───(Y(ξ₁))⎟│       
    dx       ⎝dξ₁       ⎠│ξ₁=u(x)



```python
sympy.pprint(sympy.diff(U,x))
```

    1


And so $Y'(X)$ is


```python
sympy.pprint( 1/ sympy.diff(Y,x).args[0])
```

       1    
    ────────
    d       
    ──(u(x))
    dx      


And so $Y''(X)$ is


```python
sympy.pprint( sympy.diff( 1/ sympy.diff(Y,x).args[0], x))
```

       2       
      d        
    -───(u(x)) 
       2       
     dx        
    ───────────
              2
    ⎛d       ⎞ 
    ⎜──(u(x))⎟ 
    ⎝dx      ⎠ 


cf. (2) from 4. Exercises, Chapter 2 Lie Transformations pp. 20

Recall an arbitrary point transformation:


```python
x = sympy.Symbol('x',real=True)
```


```python
y = sympy.Function('y')(x)
```


```python
U = sympy.Function('U')(x,y)
X = sympy.Function('X')(x,y)
Y = sympy.Function('Y')(X)
```


```python
sympy.pprint(sympy.diff(U,x))
```

    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(U(x, ξ₂))⎟│        + ⎜───(U(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x



```python
sympy.pprint( sympy.diff(Y,x))
```

    ⎛d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    ⎞ ⎛ d        ⎞│    
    ⎜──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    ⎟⋅⎜───(Y(ξ₁))⎟│    
    ⎝dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x⎠ ⎝dξ₁       ⎠│ξ₁=X
    
             
             
    (x, y(x))



```python
sympy.pprint( sympy.diff(Y,x).args[0] )
```

    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x



```python
sympy.pprint( sympy.diff(U,x)/ sympy.diff(Y,x).args[0])
```

    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(U(x, ξ₂))⎟│        + ⎜───(U(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x
    ──────────────────────────────────────────────────────────
    d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    
    ──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    
    dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x


For $Y''(X)$,


```python
YprimeX = sympy.diff(U,x)/sympy.diff(Y,x).args[0]
Yprime2X = sympy.diff(YprimeX,x)
sympy.pprint( Yprime2X.simplify() )
```

                                                                   ⎛⎛            2
      ⎛d        ⎛ ∂           ⎞│          ⎛ ∂              ⎞│    ⎞ ⎜⎜d          d 
    - ⎜──(y(x))⋅⎜───(U(x, ξ₂))⎟│        + ⎜───(U(ξ₁, y(x)))⎟│    ⎟⋅⎜⎜──(y(x))⋅────
      ⎝dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x⎠ ⎜⎜dx           
                                                                   ⎝⎝         dy(x
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                                                                  
                                                                                  
                                                                                  
    
                         2               ⎞                     ⎛   2            ⎞│
                        d                ⎟ d          d        ⎜  ∂             ⎟│
    ──(X(x, y(x))) + ────────(X(x, y(x)))⎟⋅──(y(x)) + ──(y(x))⋅⎜──────(X(x, ξ₃))⎟│
     2               dx dy(x)            ⎟ dx         dx       ⎝∂ξ₃ ∂x          ⎠│
    )                                    ⎠                                        
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                                                                  
                                                                                  
                                                                                  
    
                2                 2                              ⎞                
               d                 d        ⎛ ∂           ⎞│       ⎟   ⎛d        ⎛ ∂
            + ───(X(x, y(x))) + ───(y(x))⋅⎜───(X(x, ξ₂))⎟│       ⎟ + ⎜──(y(x))⋅⎜──
    ξ₃=y(x)     2                 2       ⎝∂ξ₂          ⎠│ξ₂=y(x)⎟   ⎝dx       ⎝∂ξ
              dx                dx                               ⎠                
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                    ⎛d        ⎛ ∂           ⎞│          ⎛ ∂       
                                    ⎜──(y(x))⋅⎜───(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁,
                                    ⎝dx       ⎝∂ξ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁      
    
                                                    ⎛⎛            2               
               ⎞│          ⎛ ∂              ⎞│    ⎞ ⎜⎜d          d                
    ─(X(x, ξ₂))⎟│        + ⎜───(X(ξ₁, y(x)))⎟│    ⎟⋅⎜⎜──(y(x))⋅──────(U(x, y(x))) 
    ₂          ⎠│ξ₂=y(x)   ⎝∂ξ₁             ⎠│ξ₁=x⎠ ⎜⎜dx            2             
                                                    ⎝⎝         dy(x)              
    ──────────────────────────────────────────────────────────────────────────────
                  2                                                               
           ⎞│    ⎞                                                                
     y(x)))⎟│    ⎟                                                                
           ⎠│ξ₁=x⎠                                                                
    
          2               ⎞                     ⎛   2            ⎞│            2  
         d                ⎟ d          d        ⎜  ∂             ⎟│           d   
    + ────────(U(x, y(x)))⎟⋅──(y(x)) + ──(y(x))⋅⎜──────(U(x, ξ₃))⎟│        + ───(U
      dx dy(x)            ⎟ dx         dx       ⎝∂ξ₃ ∂x          ⎠│ξ₃=y(x)     2  
                          ⎠                                                  dx   
    ──────────────────────────────────────────────────────────────────────────────
                                                                                  
                                                                                  
                                                                                  
                                                                                  
    
                   2                              ⎞
                  d        ⎛ ∂           ⎞│       ⎟
    (x, y(x))) + ───(y(x))⋅⎜───(U(x, ξ₂))⎟│       ⎟
                   2       ⎝∂ξ₂          ⎠│ξ₂=y(x)⎟
                 dx                               ⎠
    ───────────────────────────────────────────────
                                                   
                                                   
                                                   
                                                   



```python

```

0298b12d29b6eb7273f677d653f82b6c1e7d5ad7
ipynb
Jupyter Notebook
partiald_sympy.ipynb
ernestyalumni/CompPhys
1f5d7559146a14a21182653b77fd35e6d6829855
Apache-2.0
2017-07-24T04:09:27.000Z
2021-12-24T16:00:41.000Z
partiald_sympy.ipynb
ernestyalumni/CompPhys
1f5d7559146a14a21182653b77fd35e6d6829855
Apache-2.0
2018-01-16T22:34:47.000Z
2019-01-29T22:37:10.000Z
partiald_sympy.ipynb
ernestyalumni/CompPhys
1f5d7559146a14a21182653b77fd35e6d6829855
Apache-2.0
2017-01-24T19:18:42.000Z
2021-03-01T07:13:35.000Z
Qwen/Qwen-72B
1. YES
2. YES