Ask Your Question

Revision history [back]

How to define functions with varying number of variables

I want to compute the Frechet derivative for a vector field, and that code works perfectly for functions with two variables x and t.

var ("t x")
v  = function ("v")
u  = function ("u")
w1 = function ("w1")
w2 = function ("w2")
eqsys = [diff(v(x,t), x) - u(x,t), diff(v(x,t), t) - diff(u(x,t), x)/(u(x,t)**2)]

def FrechetD (support, dependVar, independVar, testfunction):
    frechet = []
    eps = var ("eps")
    v = independVar[:] + [eps]
    for j in range (len(support)):
        deriv = []
        for i in range (len(support)):
            r0 (x, t, eps) =  dependVar[i](*independVar)+ testfunction[i](*independVar) * eps
            s  =  support[j].substitute_function (dependVar[i], r0)
            deriv.append (diff(s, eps).subs ({eps: 0}))
        frechet.append (deriv)
    return frechet
FrechetD (eqsys, [u,v], [x,t], [w1,w2])
[[-w1(x, t), diff(w2(x, t), x)],
 [2*w1(x, t)*diff(u(x, t), x)/u(x, t)^3 - diff(w1(x, t), x)/u(x, t)^2,
  diff(w2(x, t), t)]]

but my problem is the line

r0 (x, t, eps) =  dependVar[i](*independVar)+ testfunction[i](*independVar) * eps

because this depends on the hardcoded x and t. Without it the following derivation for eps always is 0. Even when I add someting like

_r0 = function('ro')(*v)

doesn't work. What i want to do is to something like

r0 (*v) =  dependVar[i](*independVar)+ testfunction[i](*independVar) * eps

to get rid of the hardcoded variables and/or the number of variables. Is that possible ?