Ask Your Question
7

Computing Variational Derivatives

asked 2011-02-07 11:26:47 +0100

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

$I = \int L(x,u,u_x u_{xx}, \ldots)dx$

the variational derivative of $I$ is defined as

$\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$

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!

edit retag flag offensive close merge delete

Comments

I don't have any ideas, but it sounds interesting :)

niles gravatar imageniles ( 2011-02-08 10:49:41 +0100 )edit

Well, 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. :)

cswiercz gravatar imagecswiercz ( 2011-02-08 19:01:43 +0100 )edit

I 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.html

DSM gravatar imageDSM ( 2011-02-08 19:49:16 +0100 )edit

Thanks for the reference. That conversation happened in 2009. I wonder what came of it...hrm.

cswiercz gravatar imagecswiercz ( 2011-02-08 20:38:06 +0100 )edit

I 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.

DaveP gravatar imageDaveP ( 2011-02-08 22:28:08 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2011-04-25 21:08:59 +0100

Kelvin Li gravatar image

The 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 ...
(more)
edit flag offensive delete link more

Comments

Because 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. :-)

Kelvin Li gravatar imageKelvin Li ( 2011-04-25 21:11:57 +0100 )edit

In 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.

kcrisman gravatar imagekcrisman ( 2011-04-25 23:37:17 +0100 )edit
1

You can use `SR.symbol()` to create a variable that will not collide with anything else in your input.

burcin gravatar imageburcin ( 2012-11-10 02:22:41 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2011-02-07 11:26:47 +0100

Seen: 10,037 times

Last updated: Apr 25 '11