# substitute_function when function is a diff ?

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 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...

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.

Sort by » oldest newest most voted

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

So we can avoid using substitute_function.

Example:

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

more

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

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