ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 09 Nov 2012 19:22:41 -0600Computing Variational Derivativeshttp://ask.sagemath.org/question/7929/computing-variational-derivatives/Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
the *variational derivative* of $I$ is defined as
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.
From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?
One issue is that if you define a function
sage: u = function('u', x)
you can take derivatives of `u` with respect to `x`, of course, but not with respect to `u`.
sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!Mon, 07 Feb 2011 04:26:47 -0600http://ask.sagemath.org/question/7929/computing-variational-derivatives/Comment by kcrisman for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=21797#post-id-21797Added a ref to this discussion there, thanks.Mon, 25 Apr 2011 16:36:03 -0500http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=21797#post-id-21797Comment by kcrisman for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=18840#post-id-18840@beardedone85 - If you have some ideas to put at #6466 that would be great! I think that some of these more abstract symbolic things are just not quite as up the alley of the folks who put the most time in, but we definitely welcome new ideas! It's not like there is a dedicated staff looking at Trac tickets all the time, unfortunately, so things can languish without fresh wind.Sat, 20 Oct 2012 15:28:40 -0500http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=18840#post-id-18840Comment by cswiercz for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22172#post-id-22172Thanks for the reference. That conversation happened in 2009. I wonder what came of it...hrm.Tue, 08 Feb 2011 13:38:06 -0600http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22172#post-id-22172Comment by cswiercz for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22175#post-id-22175Well, I have some ideas of my own on how to implement this but it involves string parsing which I don't consider an elegant approach. Just looking for some ideas. :)Tue, 08 Feb 2011 12:01:43 -0600http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22175#post-id-22175Comment by niles for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22178#post-id-22178I don't have any ideas, but it sounds interesting :)Tue, 08 Feb 2011 03:49:41 -0600http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22178#post-id-22178Comment by DSM for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22174#post-id-22174I have no nice answers either, but support would definitely be worth adding. There are ways to hack around it in any given case, but they're not pretty. I think there was a related thread a while back: http://www.mail-archive.com/sage-devel@googlegroups.com/msg22657.htmlTue, 08 Feb 2011 12:49:16 -0600http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22174#post-id-22174Comment by DaveP for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22171#post-id-22171I was also looking for a solution to this problem. I notice that there is a very old trac request http://trac.sagemath.org/sage_trac/ticket/6466 which has not been acted on in 19 months.Tue, 08 Feb 2011 15:28:08 -0600http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=22171#post-id-22171Comment by beardedone85 for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=18841#post-id-18841@cswiercz It looks like they took a break to wait for Sage4.0 and then never came back. It sure would be a nice feature though. Is there any way we can get it back into their list?Sat, 20 Oct 2012 04:23:23 -0500http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=18841#post-id-18841Answer by Kelvin Li for <p>Allow me to start with a definition. Given a function $u = u(x)$, a function $L$ of $x, u$, and all derivatives of $u$; and</p>
<p align="center">
$I = \int L(x,u,u_x u_{xx}, \ldots)dx$
</p>
<p>the <em>variational derivative</em> of $I$ is defined as</p>
<p align="center">
$\frac{\delta I}{\delta u} := \frac{\partial L}{\partial u} - \frac{d}{dx} \frac{\partial L}{\partial u_x} + \frac{d^2}{dx^2} \frac{\partial L}{\partial u_{xx}} - \cdots$
</p>
<p>For example, with $L=u^3 + u_x^2/2$ we have $\frac{\delta I}{\delta u} = 3u^2 - u_{xx}$.</p>
<p>From my poking around I suspect that Sage doesn't have the option of computing the variational derivative of an integral operator. However, I'd like to write some code that does this. Does anyone have any suggestions on how to go about doing this?</p>
<p>One issue is that if you define a function</p>
<pre><code>sage: u = function('u', x)
</code></pre>
<p>you can take derivatives of <code>u</code> with respect to <code>x</code>, of course, but not with respect to <code>u</code>.</p>
<pre><code>sage: u.derivative(u) # this should be equal to 1
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
sage: u_x = u.derivative(x)
sage: L = u_x^2/2
sage: L.derivative(u_x) # this should be equal to u_x
Traceback (click to the left of this block for traceback)
...
TypeError: argument symb must be a symbol
</code></pre>
<p>My thoughts include doing some string parsing so I can take the necessary derivatives with respect to $u,u_x,u_{xx},\ldots$ but that's starting to sound a bit messy. Any suggestions on how to cleanly go about doing this in Sage would be greatly appreciated!</p>
http://ask.sagemath.org/question/7929/computing-variational-derivatives/?answer=12318#post-id-12318The trick is to substitute `u` with a variable, differentiate, then substitute back.
Given the python module called `func_diff.py` (shown later), one can use it as follows:
goofy@wdeb:temp$ ls
func_diff.py
goofy@wdeb:temp$ sage
----------------------------------------------------------------------
| Sage Version 4.6.2, Release Date: 2011-02-25 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import func_diff
sage: u(x) = function('u', x)
sage: u
x |--> u(x)
sage: L = u^3 + (1/2)*u.diff(x)^2
sage: L
x |--> u(x)^3 + 1/2*D[0](u)(x)^2
sage: func_diff.func_diff(L, u)
x |--> 3*u(x)^2 - D[0, 0](u)(x)
Note that `func_diff` as-is does not actually compute the functional derivative. In the context of @cswiercz's definition, it takes $L$, not $I$, as the input. But this should be relatively easy to change. Simply use `func_diff(I.diff(x), u)`
Here is the module `func_diff.py`:
from sage.all import *
import sage.symbolic.operators
def is_op_du(expr_op, u):
is_derivative = isinstance(
expr_op,
sage.symbolic.operators.FDerivativeOperator
)
if is_derivative:
# Returns True if the differentiated function is `u`.
return expr_op.function() == u.operator()
else:
return False
def iter_du_orders(expr, u):
for sub_expr in expr.operands():
if sub_expr == []:
# hit end of tree
continue
elif is_op_du(sub_expr.operator(), u):
# yield order of differentiation
yield len(sub_expr.operator().parameter_set())
else:
# iterate into sub expression
for order in iter_du_orders(sub_expr, u):
yield order
def func_diff(L, u_in):
# `u` must be a callable symbolic expression
# in one variable.
if len(u_in.variables()) == 1:
x = u_in.variables()[0]
u = u_in.function(x)
else:
raise TypeError
# This variable name must not collide
# with an existing one.
# I use an empty string in hopes that
# nobody else does this...
t = SR.var('')
result = SR(0)
# `orders` is the set of all
# orders of differentiation of `u`
orders = set(iter_du_orders(L, u)).union((0,))
for c in orders:
du = u(x).diff(x, c)
sign = Integer(-1)**c
# Temporarily replace all `c`th derivatives of `u` with `t`;
# differentiate; then substitute back.
dL_du = L.subs({du:t}).diff(t).subs({t:du})
# Append intermediate term to `result`
result += sign * dL_du.diff(x, c)
return result
The bulk of the code is rather uninteresting. For example, the generator `iter_du_orders` iterates through the given expression `L` and returns all of the orders of differention of `u` in `L`.
Unfornately, there is a bug which I do not know how to cleanly circumvent. It is due to the line `t = SR.var('')`, which creates a temporary variable. This variable is used to substitute out all instances of $\frac{d^n u}{d x^n}$ in `L` when computing the derivatives. The problem is if `L` itself contains a variable with an empty-string representation (i.e. the same as that of `t`), then things will get pretty messed up. For example:
sage: b = SR.var('')
sage: b
sage: u = function('u', b).function(b)
sage: u
|--> u()
sage: L = u^3 + (1/2)*u.diff(b)^2
sage: L
|--> u()^3 + 1/2*D[0](u)()^2
sage: func_diff.func_diff(L, u)
|--> -3*u(D[0](u)())^2*D[0, 0](u)()*D[0, 0](u)(D[0](u)()) - 6*u(D[0](u)())*D[0](u)(D[0](u)())^2*D[0, 0](u)() + 3*u()^2 + D[0](u)(u())*D[0, 0](u)(u()) - D[0, 0](u)()Mon, 25 Apr 2011 14:08:59 -0500http://ask.sagemath.org/question/7929/computing-variational-derivatives/?answer=12318#post-id-12318Comment by Kelvin Li for <div class="snippet"><p>The trick is to substitute <code>u</code> with a variable, differentiate, then substitute back.</p>
<p>Given the python module called <code>func_diff.py</code> (shown later), one can use it as follows:</p>
<pre><code>goofy@wdeb:temp$ ls
func_diff.py
goofy@wdeb:temp$ sage
----------------------------------------------------------------------
| Sage Version 4.6.2, Release Date: 2011-02-25 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import func_diff
sage: u(x) = function('u', x)
sage: u
x |--> u(x)
sage: L = u^3 + (1/2)*u.diff(x)^2
sage: L
x |--> u(x)^3 + 1/2*D[0](u)(x)^2
sage: func_diff.func_diff(L, u)
x |--> 3*u(x)^2 - D[0, 0](u)(x)
</code></pre>
<p>Note that <code>func_diff</code> as-is does not actually compute the functional derivative. In the context of <a href="/users/6/cswiercz/">@cswiercz</a>'s definition, it takes $L$, not $I$, as the input. But this should be relatively easy to change. Simply use <code>func_diff(I.diff(x), u)</code></p>
<p>Here is the module <code>func_diff.py</code>:</p>
<pre><code>from sage.all import *
import sage.symbolic.operators
def is_op_du(expr_op, u):
is_derivative = isinstance(
expr_op,
sage.symbolic.operators.FDerivativeOperator
)
if is_derivative:
# Returns True if the differentiated function is `u`.
return expr_op.function() == u.operator()
else:
return False
def iter_du_orders(expr, u):
for sub_expr in expr.operands():
if sub_expr == []:
# hit end of tree
continue
elif is_op_du(sub_expr.operator(), u):
# yield order of differentiation
yield len(sub_expr.operator().parameter_set())
else:
# iterate into sub expression
for order in iter_du_orders(sub_expr, u):
yield order
def func_diff(L, u_in):
# `u` must be a callable symbolic expression
# in one variable.
if len(u_in.variables()) == 1:
x = u_in.variables()[0]
u = u_in.function(x)
else:
raise TypeError
# This variable name must not collide
# with an existing one.
# I use an empty string in hopes that
# nobody else does this...
t = SR.var('')
result = SR(0)
# `orders` is the set of all
# orders of differentiation of `u`
orders = set(iter_du_orders(L, u)).union((0,))
for c in orders:
du = u(x).diff(x, c)
sign = Integer(-1)**c
# Temporarily replace all `c`th derivatives of `u` with `t`;
# differentiate; then substitute back.
dL_du = L.subs({du:t}).diff(t).subs({t:du})
# Append intermediate term to `result`
result += sign * dL_du.diff(x, c)
return result
</code></pre>
<p>The bulk of the code is rather uninteresting. For example, the generator <code>iter_du_orders</code> iterates through the given expression <code>L</code> and returns all of the orders of differention of <code>u</code> in <code>L</code>.</p>
<p>Unfornately, there is a bug which I do not know how to cleanly circumvent. It is due to the line <code>t = SR.var('')</code>, which creates a temporary variable. This variable is used to substitute out all instances of $\frac{d^n u}{d x^n}$ in <code>L</code> when computing the derivatives. The problem is if <code>L</code> itself contains a variable with an empty-string representation (i.e. the same as that of <code>t</code>), then things will get pretty messed up. For example:</p>
<pre><code>sage: b = SR.var('')
sage: b
sage: u = function('u', b).function(b)
sage: u
|--> u()
sage: L = u^3 + (1/2)*u.diff(b)^2
sage ...</code></pre><span class="expander"> <a>(more)</a></span></div>http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=21798#post-id-21798Because I don't know anything about functional derivatives, I have tested this only with @cswiercz's original example. The code isn't beautiful, but it is marginally better than hacking string representations. :-)Mon, 25 Apr 2011 14:11:57 -0500http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=21798#post-id-21798Comment by burcin for <div class="snippet"><p>The trick is to substitute <code>u</code> with a variable, differentiate, then substitute back.</p>
<p>Given the python module called <code>func_diff.py</code> (shown later), one can use it as follows:</p>
<pre><code>goofy@wdeb:temp$ ls
func_diff.py
goofy@wdeb:temp$ sage
----------------------------------------------------------------------
| Sage Version 4.6.2, Release Date: 2011-02-25 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import func_diff
sage: u(x) = function('u', x)
sage: u
x |--> u(x)
sage: L = u^3 + (1/2)*u.diff(x)^2
sage: L
x |--> u(x)^3 + 1/2*D[0](u)(x)^2
sage: func_diff.func_diff(L, u)
x |--> 3*u(x)^2 - D[0, 0](u)(x)
</code></pre>
<p>Note that <code>func_diff</code> as-is does not actually compute the functional derivative. In the context of <a href="/users/6/cswiercz/">@cswiercz</a>'s definition, it takes $L$, not $I$, as the input. But this should be relatively easy to change. Simply use <code>func_diff(I.diff(x), u)</code></p>
<p>Here is the module <code>func_diff.py</code>:</p>
<pre><code>from sage.all import *
import sage.symbolic.operators
def is_op_du(expr_op, u):
is_derivative = isinstance(
expr_op,
sage.symbolic.operators.FDerivativeOperator
)
if is_derivative:
# Returns True if the differentiated function is `u`.
return expr_op.function() == u.operator()
else:
return False
def iter_du_orders(expr, u):
for sub_expr in expr.operands():
if sub_expr == []:
# hit end of tree
continue
elif is_op_du(sub_expr.operator(), u):
# yield order of differentiation
yield len(sub_expr.operator().parameter_set())
else:
# iterate into sub expression
for order in iter_du_orders(sub_expr, u):
yield order
def func_diff(L, u_in):
# `u` must be a callable symbolic expression
# in one variable.
if len(u_in.variables()) == 1:
x = u_in.variables()[0]
u = u_in.function(x)
else:
raise TypeError
# This variable name must not collide
# with an existing one.
# I use an empty string in hopes that
# nobody else does this...
t = SR.var('')
result = SR(0)
# `orders` is the set of all
# orders of differentiation of `u`
orders = set(iter_du_orders(L, u)).union((0,))
for c in orders:
du = u(x).diff(x, c)
sign = Integer(-1)**c
# Temporarily replace all `c`th derivatives of `u` with `t`;
# differentiate; then substitute back.
dL_du = L.subs({du:t}).diff(t).subs({t:du})
# Append intermediate term to `result`
result += sign * dL_du.diff(x, c)
return result
</code></pre>
<p>The bulk of the code is rather uninteresting. For example, the generator <code>iter_du_orders</code> iterates through the given expression <code>L</code> and returns all of the orders of differention of <code>u</code> in <code>L</code>.</p>
<p>Unfornately, there is a bug which I do not know how to cleanly circumvent. It is due to the line <code>t = SR.var('')</code>, which creates a temporary variable. This variable is used to substitute out all instances of $\frac{d^n u}{d x^n}$ in <code>L</code> when computing the derivatives. The problem is if <code>L</code> itself contains a variable with an empty-string representation (i.e. the same as that of <code>t</code>), then things will get pretty messed up. For example:</p>
<pre><code>sage: b = SR.var('')
sage: b
sage: u = function('u', b).function(b)
sage: u
|--> u()
sage: L = u^3 + (1/2)*u.diff(b)^2
sage ...</code></pre><span class="expander"> <a>(more)</a></span></div>http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=18731#post-id-18731You can use `SR.symbol()` to create a variable that will not collide with anything else in your input.Fri, 09 Nov 2012 19:22:41 -0600http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=18731#post-id-18731Comment by kcrisman for <div class="snippet"><p>The trick is to substitute <code>u</code> with a variable, differentiate, then substitute back.</p>
<p>Given the python module called <code>func_diff.py</code> (shown later), one can use it as follows:</p>
<pre><code>goofy@wdeb:temp$ ls
func_diff.py
goofy@wdeb:temp$ sage
----------------------------------------------------------------------
| Sage Version 4.6.2, Release Date: 2011-02-25 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: import func_diff
sage: u(x) = function('u', x)
sage: u
x |--> u(x)
sage: L = u^3 + (1/2)*u.diff(x)^2
sage: L
x |--> u(x)^3 + 1/2*D[0](u)(x)^2
sage: func_diff.func_diff(L, u)
x |--> 3*u(x)^2 - D[0, 0](u)(x)
</code></pre>
<p>Note that <code>func_diff</code> as-is does not actually compute the functional derivative. In the context of <a href="/users/6/cswiercz/">@cswiercz</a>'s definition, it takes $L$, not $I$, as the input. But this should be relatively easy to change. Simply use <code>func_diff(I.diff(x), u)</code></p>
<p>Here is the module <code>func_diff.py</code>:</p>
<pre><code>from sage.all import *
import sage.symbolic.operators
def is_op_du(expr_op, u):
is_derivative = isinstance(
expr_op,
sage.symbolic.operators.FDerivativeOperator
)
if is_derivative:
# Returns True if the differentiated function is `u`.
return expr_op.function() == u.operator()
else:
return False
def iter_du_orders(expr, u):
for sub_expr in expr.operands():
if sub_expr == []:
# hit end of tree
continue
elif is_op_du(sub_expr.operator(), u):
# yield order of differentiation
yield len(sub_expr.operator().parameter_set())
else:
# iterate into sub expression
for order in iter_du_orders(sub_expr, u):
yield order
def func_diff(L, u_in):
# `u` must be a callable symbolic expression
# in one variable.
if len(u_in.variables()) == 1:
x = u_in.variables()[0]
u = u_in.function(x)
else:
raise TypeError
# This variable name must not collide
# with an existing one.
# I use an empty string in hopes that
# nobody else does this...
t = SR.var('')
result = SR(0)
# `orders` is the set of all
# orders of differentiation of `u`
orders = set(iter_du_orders(L, u)).union((0,))
for c in orders:
du = u(x).diff(x, c)
sign = Integer(-1)**c
# Temporarily replace all `c`th derivatives of `u` with `t`;
# differentiate; then substitute back.
dL_du = L.subs({du:t}).diff(t).subs({t:du})
# Append intermediate term to `result`
result += sign * dL_du.diff(x, c)
return result
</code></pre>
<p>The bulk of the code is rather uninteresting. For example, the generator <code>iter_du_orders</code> iterates through the given expression <code>L</code> and returns all of the orders of differention of <code>u</code> in <code>L</code>.</p>
<p>Unfornately, there is a bug which I do not know how to cleanly circumvent. It is due to the line <code>t = SR.var('')</code>, which creates a temporary variable. This variable is used to substitute out all instances of $\frac{d^n u}{d x^n}$ in <code>L</code> when computing the derivatives. The problem is if <code>L</code> itself contains a variable with an empty-string representation (i.e. the same as that of <code>t</code>), then things will get pretty messed up. For example:</p>
<pre><code>sage: b = SR.var('')
sage: b
sage: u = function('u', b).function(b)
sage: u
|--> u()
sage: L = u^3 + (1/2)*u.diff(b)^2
sage ...</code></pre><span class="expander"> <a>(more)</a></span></div>http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=21796#post-id-21796In general, this stuff is possible, but it would be worth seeing how people use Ginac and/or Maxima to do this, then see how that would work. What we don't want is semi-hacky things that are really hard to extend, or to fix later - this is some of the current problem, in fact. But if this turns out to be extensible, that would be wonderful! There is a lot of symbolic stuff physicists would like that has thus far languished.Mon, 25 Apr 2011 16:37:17 -0500http://ask.sagemath.org/question/7929/computing-variational-derivatives/?comment=21796#post-id-21796