Ask Your Question

substitute_function when function is a diff ?

asked 2020-12-16 13:35:47 +0200

ortollj gravatar image

updated 2020-12-16 14:58:58 +0200

slelievre gravatar image

I simplified my question as I was asked:

var('m, omega, E', domain='real')

phi_x(t) = function('phi_x', latex_name=r'\phi_x')(t)
dphi_x = diff(phi_x, t, 1)
p_x = function('p_x')(t)
# p_x = m*diff(phi_x, t, 1)

eqTest = E == 1/2*m*omega^2*phi_x(t)^2 + 1/2*m*dphi_x^2
eqTest_wished = E(t) == 1/2*m*omega^2*phi_x(t)^2 + 1/2*p_x^2/m
print('We wish to obtain `eqTest_wished` with SageMath:')
show('eqTest_wished : \t', eqTest_wished)
show("by using substitutions starting from `eqTest`:")
show('eqTest : \t \t \t', eqTest)

# Failed attempt
eqTest_Modified = eqTest.substitute_function(dphi_x, p_x/m)
show('Failed attempt: \t \t ', eqTest_Modified)

How to transform equation eqTest into equation eqTest_wished using SageMath ?

edit retag flag offensive close merge delete


Your notations are inconsistent : phi_x= is a function (of the single argument t), therefore, so is dphi_x, and so is 1/2*m*omega^2*phi_x(t)^2 + 1/2*m*dphi_x^2.(Similarly, p_x is a function (of the single variable t).) eqTest is therefore a function (of the single variable t`) whose value is an equation.

On the other hand, 1/2*m*omega^2*phi_x(t)^2 + 1/2*p_x^2/m us a symbolic expression ; therefore eqTest_wished" is an equation. One notes, BTW, that E is a variable, whereas E(t) has no proper meaning (it turns out that E(t) has a meaning in "raw" Sage, left as an exercise for the reader...).

It is therefore extremely difficult for the reader to guess, suppose, postulate or otherwise divine what you are trying to accomplish...

Please clarify your thoughts...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-12-16 14:53:59 +0200 )edit

Thanks @ortollj for simplifying the question with respect to the initial version.

I simplified it a little bit more by removing unused variables from the initial variable declaration.

I also changed phi_x using latex_name so that its plain text name has no \ but it still displays nicely as $\phi_x$ when typeset.

slelievre gravatar imageslelievre ( 2020-12-16 15:01:23 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-12-16 14:47:49 +0200

slelievre gravatar image

updated 2020-12-16 15:02:48 +0200

In this case, substituting an expression with subs and a substitution dictionary seems to work.

So we can avoid using substitute_function.


sage: eqTest.subs({dphi_x: p_x/m})
t |--> E == 1/2*m*omega^2*\phi_x(t)^2 + 1/2*p_x(t)^2/m
edit flag offensive delete link more


thank you @slelievre

ortollj gravatar imageortollj ( 2020-12-16 17:38:43 +0200 )edit

but, just out of curiosity: what is the reason that if I uncomment the line of code #p_x = m * dphi_x, it no longer works? the difference between the two dictionary :

[{t |--> diff(phi_x(t), t): t |--> diff(phi_x(t), t)}] KO

[{t |--> diff(phi_x(t), t): p_x(t)/m}] OK

ortollj gravatar imageortollj ( 2020-12-17 10:30:37 +0200 )edit

To use an analogy, what do you expect from (3*x + exp(x)).subs({exp(x): exp(x)})?

slelievre gravatar imageslelievre ( 2020-12-17 16:40:04 +0200 )edit

ok, I see what you mean.

ortollj gravatar imageortollj ( 2020-12-17 17:36:03 +0200 )edit

Glad that explained it. : )

slelievre gravatar imageslelievre ( 2020-12-17 23:54:48 +0200 )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


Asked: 2020-12-16 13:35:47 +0200

Seen: 345 times

Last updated: Dec 16 '20