0

I am trying to create a function that take a function as input that return a tuple with the function and its derivative. In all my attempt the derivative of the function is evaluated to zero or one even if it is a symbolic function: I simplified the code to return only its derivative:

t = var("t")
q = function("q")(t)

print("Expected",diff(q,t))  # return diff(q(t), t) as expected

Gamma(a) = diff(a)
print("Unexpected_v1 ",Gamma(q))  # return 1

Gamma(a) = diff(a,a.arguments()[0]) 
print("Unexpected_v2 ", Gamma(q)) # return 1

Gamma(a,b) = diff(a,b)
print("Unexpected_v3 ", Gamma(q,t)) # return 0

How can this be achieved?

1 Answers1

0

I'm not really sure what you are trying to achieve. But at any rate your line

Gamma(a) = diff(a)

is kind of dangerous, because this is not legal Python (a is not defined). What happens is that Sage "preparses" it:

sage: preparse('Gamma(a) = diff(a)')
'__tmp__=var("a"); Gamma = symbolic_expression(diff(a)).function(a)'

So you can see that a is now a symbolic variable, and so it has a default variable a, so the derivative will obviously be 1, and you have assigned Gamma(a) to always return this. The other ones will behave analogously.

If you are sure your input will always be correct, you should https://sagecell.sagemath.org/?z=eJxLSU1T8K10K81L1kjTtOLlUgCCotSS0qI8hTSdlMy0NKAwLxcvV4mCrUJZYpGGUokSkF8I5KUB9ZRk5udpKBUqaWqUAEWh5hRqAgD_FBee&lang=sage&interacts=eJyLjgUAARUAuQ==.

def MyFunc(f):
    return f,diff(f)

t = var("t")
q = function("q")(t)
MyFunc(q)

This is super generic, though, so it might be too much so for your purposes. Hopefully it points you in the right direction.

kcrisman
  • 4,374
  • 20
  • 41