Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
2

substitute_function when function is a diff ?

asked 4 years ago

ortollj gravatar image

updated 4 years ago

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 ?

Preview: (hide)

Comments

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 ( 4 years ago )

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 ϕx when typeset.

slelievre gravatar imageslelievre ( 4 years ago )

1 Answer

Sort by » oldest newest most voted
2

answered 4 years ago

slelievre gravatar image

updated 4 years ago

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
Preview: (hide)
link

Comments

thank you @slelievre

ortollj gravatar imageortollj ( 4 years ago )

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 ( 4 years ago )

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

slelievre gravatar imageslelievre ( 4 years ago )

ok, I see what you mean.

ortollj gravatar imageortollj ( 4 years ago )

Glad that explained it. : )

slelievre gravatar imageslelievre ( 4 years ago )

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

Stats

Asked: 4 years ago

Seen: 499 times

Last updated: Dec 16 '20