Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
7

Computing Variational Derivatives

asked 14 years ago

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=L(x,u,uxuxx,)dx

the variational derivative of I is defined as

δIδu:=LuddxLux+d2dx2Luxx

For example, with L=u3+u2x/2 we have δIδu=3u2uxx.

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,ux,uxx, 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!

Preview: (hide)

Comments

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

niles gravatar imageniles ( 14 years ago )

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 ( 14 years ago )

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 ( 14 years ago )

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

cswiercz gravatar imagecswiercz ( 14 years ago )

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 ( 14 years ago )

1 Answer

Sort by » oldest newest most voted
3

answered 13 years ago

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 dnudxn 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)
Preview: (hide)
link

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 ( 13 years ago )

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 ( 13 years ago )
1

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

burcin gravatar imageburcin ( 12 years ago )

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: 14 years ago

Seen: 10,205 times

Last updated: Apr 25 '11