[Newbie]: Why has function definition changed?

92 views
Skip to first unread message

Bernd Breitschaedel

unread,
Feb 24, 2025, 5:14:24 AMFeb 24
to sage-support
Hi! 

I'm a newbe to sage. So please be kind if this is a beginners failure. See my sreenshot with the question. sage.png

Thanks for your Support,  Bernd

Eric Gourgoulhon

unread,
Feb 24, 2025, 8:04:17 AMFeb 24
to sage-support
Hi, 

The issue occurs because the line
H(x,y,t)=x*y
redefines (silently!) x and y to be symbolic variables (and thus independent of t). 
You can check this by asking what Sage's preparser is performing on the command H(x,y,t)=x*y before passing it to the Python interpreter: simply type
preparse("H(x,y,t)=x*y")
The output is
'__tmp__=var("x,y,t"); H = symbolic_expression(x*y).function(x,y,t)'
The part var("x,y,t") redefines x and y as symbolic variables.
Previously, x and y were callable symbolic expressions x(t) and y(t)

Eric.

Eric Gourgoulhon

unread,
Feb 24, 2025, 8:06:43 AMFeb 24
to sage-support
Le lundi 24 février 2025 à 14:04:17 UTC+1, Eric Gourgoulhon a écrit :
Previously, x and y were callable symbolic expressions x(t) and y(t)

Sorry I meant :  x and y were symbolic expressions x(t) and y(t)

Bernd Breitschaedel

unread,
Feb 24, 2025, 11:39:30 AMFeb 24
to sage-support
Thanks for your info Eric,


Basically I want to do Hamilton optimization (Hamiltonian (control theory) - Wikipedia)  using SageMath.
This is what i tried, and where I have abserved this behaviour. 

So how should i define  H in 'In [2]',  so that 'In [4]'  shows the expected result. 


Thank you, Bernd

sage2.png

Eric Gourgoulhon

unread,
Feb 25, 2025, 5:01:27 AMFeb 25
to sage-support
Hi, 

Le lundi 24 février 2025 à 17:39:30 UTC+1, bernd.bre...@gmail.com a écrit :

So how should i define  H in 'In [2]',  so that 'In [4]'  shows the expected result. 

Basically you should define H as a symbolic expression, not a function, and make a distinction between functions and symbolic variables. 
A solution is 

t = var('t')
r = var('r')
delta = var('delta')
alpha = var('alpha')

x = function('x')
k = function('k')
lambda_ = function('lambda_') 

X = var('X')
K = var('K')
L = var('L')
H = K*(1 - X)*exp(-r*t) + L*(K^alpha*X - delta*K)

to_functions = {X: x(t), K: k(t), L: lambda_(t)}  # a dictionary to perform substitutions

eq1 = (diff(H, X).subs(to_functions) == 0)
eq2 = (diff(H, K).subs(to_functions) == - diff(lambda_(t), t))

Best wishes,

Eric. . 

Emmanuel Charpentier

unread,
Feb 25, 2025, 12:08:15 PMFeb 25
to sage-support

Complement : Sage’s notation for derivatives is somewhat baroque, and this does not help in the present case.

Here, both k and x are functions of t ; writing h as a function of x and k is just an intricate way to write a function of t, unique independent variable.

As for notations : what we’d “manually” write in this case would be :
\displaystyle{\frac{\partial h}{\partial t}}\,=\,\displaystyle{\frac{\partial h}{\partial x}\,\frac{\partial x}{\partial t}}

and we could deduce

\displaystyle{\frac{\partial h}{\partial x}}\,=\,\frac{\frac{\partial h}{\partial t}}{\frac{\partial x}{\partial t}}.

Sage doesn’t have a direct notation for denoting derivative with respect to another function ; instead, it uses the Doperator, which creates derivative operators :

sage: t=var("t") sage: h, x=function("h, x") sage: diff(h(x(t)), t) D[0](h)(x(t))*diff(x(t), t) sage: diff(h(x(t)), t)/diff(x(t), t) D[0](h)(x(t))

Eric’s notation is an elegant way to work around this problem.

The use of standard notations leads to :

sage: r, t, alpha, delta, r = var('r, t, alpha, delta, r') sage: k, x, lambda_ = function("k, x, lambda_") sage: h(t) = k(t)*(1-x(t))*e^(-r*t) + lambda_(t)*(k(t)^alpha*x(t)-delta*k(t)) sage: D[0](h)(x(t)) r*(x(x(t)) - 1)*e^(-r*x(t))*k(x(t)) - (x(x(t)) - 1)*e^(-r*x(t))*D[0](k)(x(t)) - e^(-r*x(t))*k(x(t))*D[0](x)(x(t)) + (alpha*k(x(t))^(alpha - 1)*x(x(t))*D[0](k)(x(t)) - delta*D[0](k)(x(t)) + k(x(t))^alpha*D[0](x)(x(t)))*lambda_(x(t)) - (delta*k(x(t)) - k(x(t))^alpha*x(x(t)))*D[0](lambda_)(x(t))

\frac{\partial h(t)}{\partial x(t)}\,=\,r {\left(x\left(x\left(t\right)\right) - 1\right)} e^{\left(-r x\left(t\right)\right)} k\left(x\left(t\right)\right) - {\left(x\left(x\left(t\right)\right) - 1\right)} e^{\left(-r x\left(t\right)\right)} \mathrm{D}_{0}\left(k\right)\left(x\left(t\right)\right) - e^{\left(-r x\left(t\right)\right)} k\left(x\left(t\right)\right) \mathrm{D}_{0}\left(x\right)\left(x\left(t\right)\right) + {\left(\alpha k\left(x\left(t\right)\right)^{\alpha - 1} x\left(x\left(t\right)\right) \mathrm{D}_{0}\left(k\right)\left(x\left(t\right)\right) - \delta \mathrm{D}_{0}\left(k\right)\left(x\left(t\right)\right) + k\left(x\left(t\right)\right)^{\alpha} \mathrm{D}_{0}\left(x\right)\left(x\left(t\right)\right)\right)} \lambda\left(x\left(t\right)\right) - {\left(\delta k\left(x\left(t\right)\right) - k\left(x\left(t\right)\right)^{\alpha} x\left(x\left(t\right)\right)\right)} \mathrm{D}_{0}\left(\lambda\right)\left(x\left(t\right)\right).

sage: D[0](h)(k(t)) r*(x(k(t)) - 1)*e^(-r*k(t))*k(k(t)) - (x(k(t)) - 1)*e^(-r*k(t))*D[0](k)(k(t)) - e^(-r*k(t))*k(k(t))*D[0](x)(k(t)) + (alpha*k(k(t))^(alpha - 1)*x(k(t))*D[0](k)(k(t)) - delta*D[0](k)(k(t)) + k(k(t))^alpha*D[0](x)(k(t)))*lambda_(k(t)) - (delta*k(k(t)) - k(k(t))^alpha*x(k(t)))*D[0](lambda_)(k(t))

\frac{\partial h(t)}{\partial k(t)}\,=\,r {\left(x\left(k\left(t\right)\right) - 1\right)} e^{\left(-r k\left(t\right)\right)} k\left(k\left(t\right)\right) - {\left(x\left(k\left(t\right)\right) - 1\right)} e^{\left(-r k\left(t\right)\right)} \mathrm{D}_{0}\left(k\right)\left(k\left(t\right)\right) - e^{\left(-r k\left(t\right)\right)} k\left(k\left(t\right)\right) \mathrm{D}_{0}\left(x\right)\left(k\left(t\right)\right) + {\left(\alpha k\left(k\left(t\right)\right)^{\alpha - 1} x\left(k\left(t\right)\right) \mathrm{D}_{0}\left(k\right)\left(k\left(t\right)\right) - \delta \mathrm{D}_{0}\left(k\right)\left(k\left(t\right)\right) + k\left(k\left(t\right)\right)^{\alpha} \mathrm{D}_{0}\left(x\right)\left(k\left(t\right)\right)\right)} \lambda\left(k\left(t\right)\right) - {\left(\delta k\left(k\left(t\right)\right) - k\left(k\left(t\right)\right)^{\alpha} x\left(k\left(t\right)\right)\right)} \mathrm{D}_{0}\left(\lambda\right)\left(k\left(t\right)\right).

Not a pretty sight…

Bernd Breitschaedel

unread,
Feb 26, 2025, 6:43:34 AMFeb 26
to sage-support
Thank you Eric & Emmanuel for your solution proposals.

Regards,
  Bernd

Bernd Breitschaedel

unread,
Feb 26, 2025, 7:02:00 AMFeb 26
to sage-support
Dear Emmanuel,

The results of your approach might not be pretty. Unfortunatly I think they seem to be  wrong too.

See:
sage: r, t, alpha, delta, r = var('r, t, alpha, delta, r')
sage: k, x, lambda_ = function("k, x, lambda_")
sage: h1(t) = x(t)*e^(-r*t)
sage: D[0](h1)(x(t))
-r*e^(-r*x(t))*x(x(t)) + e^(-r*x(t))*D[0](x)(x(t))


Kindly note the "x(x(t))" in the first term.  :-/


Greetings,
     Bernd

Emmanuel Charpentier

unread,
Feb 26, 2025, 8:23:35 AMFeb 26
to sage-support
Le mercredi 26 février 2025 à 13:02:00 UTC+1, bernd.bre...@gmail.com a écrit :
Dear Emmanuel,

The results of your approach might not be pretty. Unfortunatly I think they seem to be  wrong too.

See:
sage: r, t, alpha, delta, r = var('r, t, alpha, delta, r')
sage: k, x, lambda_ = function("k, x, lambda_")
sage: h1(t) = x(t)*e^(-r*t)
sage: D[0](h1)(x(t))
-r*e^(-r*x(t))*x(x(t)) + e^(-r*x(t))*D[0](x)(x(t))


Kindly note the "x(x(t))" in the first term.  :-/

Indeed. I won't pretend to understand what exactly `D` does, a,d I may be wrong. The (pseudo-)equality :

\displaystyle{\frac{\partial h}{\partial x}}\,=\,\frac{\frac{\partial h}{\partial t}}{\frac{\partial x}{\partial t}}

might be more useful.

HTH,
Reply all
Reply to author
Forward
0 new messages