Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
1

Substitute for sub-expression

asked 2 years ago

mn124700 gravatar image

The result of one of my calculations looks like this...

E == 1/2*(V0*p1*cos(sqrt(-(E - V0)*p1)*a)*sin(sqrt(-(E - V0)*p1)*a) - sqrt(-(E - V0)*p1)*sqrt(E)*sqrt(p1)*cos(sqrt(-(E - V0)*p1)*a)^2 + sqrt(-(E - V0)*p1)*sqrt(E)*sqrt(p1)*sin(sqrt(-(E - V0)*p1)*a)^2)/(p1*cos(sqrt(-(E - V0)*p1)*a)*sin(sqrt(-(E - V0)*p1)*a))

I would like to replace the expression "-(E - V0)*p1" with a simple variable, "beta". I tried using "subs", but it seems it won't substitute at that level.

Is there a way to do this?
Thanks, Eric

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 2 years ago

Emmanuel Charpentier gravatar image

updated 2 years ago

WorksFoMe(TM) on 9.7.beta3 :

sage: var("E, V0, p1, a")
(E, V0, p1, a)
sage: Ex=E == 1/2*(V0*p1*cos(sqrt(-(E - V0)*p1)*a)*sin(sqrt(-(E - V0)*p1)*a) - sqrt(-(E - V0)*p1)*sqrt(E)*sqrt(p1)*cos(sqrt(-(E - V0)*p1)*a)^2 + sqrt(-(E - V0)*p1)*sqrt(E)*sqrt(p1)*sin(sqrt(-(E - V0)*p1)*a)^2)/(p1*cos(sqrt(-(E - V0)*p1)*a)*sin(sqrt(-(E - V0)*p1)*a))
sage: var("beta")
beta
sage: Ex.subs(-(E-V0)*p1==beta)
E == 1/2*(V0*p1*cos(a*sqrt(beta))*sin(a*sqrt(beta)) - sqrt(E)*sqrt(beta)*sqrt(p1)*cos(a*sqrt(beta))^2 + sqrt(E)*sqrt(beta)*sqrt(p1)*sin(a*sqrt(beta))^2)/(p1*cos(a*sqrt(beta))*sin(a*sqrt(beta)))

E=V0p1cos(aβ)sin(aβ)Eβp1cos(aβ)2+Eβp1sin(aβ)22p1cos(aβ)sin(aβ)

One can get a "nicer"(?) expression with :

sage: w0=SR.wild(0)
sage: (Ex.subs(-(E-V0)*p1*w0==beta*w0).expand().trig_reduce()-V0/2).factor()+V0/2
E == -1/2*sqrt(-(E - V0)*p1)*sqrt(E)*(cot(sqrt(-(E - V0)*p1)*a) - tan(sqrt(-(E - V0)*p1)*a))/sqrt(p1) + 1/2*V0

Another possibility is :

sage: var("EE")
EE
sage: mathematica.FullSimplify(Ex.subs(E==EE)).sage().subs(EE=E)
(cot(sqrt(-E + V0)*a*sqrt(p1))^2 - 1)*sqrt(E)*sqrt(-E + V0)*tan(sqrt(-E + V0)*a*sqrt(p1)) + 2*E == V0

[ Note that the E variable clashes with Mathematica's E constant (=exp(1)). ]

What Sage version do you use ? And what did you expect ?

HTH,

EDIT : Yet another "nicer" answer :

sage: (Ex-V0/2).subs(-(E-V0)*p1==beta).expand().trig_reduce().factor().trig_simplify()+V0/2
E == 1/2*V0 + 1/2*(2*sin(a*sqrt(beta))^2 - 1)*sqrt(E)*sqrt(beta)/(sqrt(p1)*cos(a*sqrt(beta))*sin(a*sqrt(beta)))

E=12V0+(2sin(aβ)21)Eβ2p1cos(aβ)sin(aβ)

or

sage: (Ex-V0/2).subs(-(E-V0)*p1==beta).expand().trig_reduce().factor().trig_simplify().trig_reduce()+V0/2
E == -sqrt(E)*sqrt(beta)*cos(2*a*sqrt(beta))*csc(2*a*sqrt(beta))/sqrt(p1) + 1/2*V0

E=Eβcos(2aβ)csc(2aβ)p1+12V0

or even

sage: w1=SR.wild(1)
sage: view((Ex-V0/2).subs(-(E-V0)*p1==beta).expand().trig_reduce().factor().trig_simplify().trig_reduce().subs(cos(w0)*csc(w0)*w1==cot(w0)*w1)+V0/2)

E=Eβcot(2aβ)p1+12V0

Season to taste...

Preview: (hide)
link
0

answered 2 years ago

mn124700 gravatar image

Wow! Thanks so much for this. Very educational. And, especially thanks for the (implicit) tip-off that Mathematica has a freeware engine that I can install. Such power on my humble laptop.

Preview: (hide)
link

Comments

Note to the wise : Mathematica is not the "gold standard" Wolfram's marketing minions would like us to believe it is. It's a very useful tool, whose use can lead you in very wrong directions in some domains...

Nevertheless, the Wolfram engine is a very useful addition to Sage ; maybe its packaging as a Sage optional package should be considered, some algorithm="mathematica" keywords re-introduced (with caution...) and some (serious) deficiencies of the Sage<-->Mathematica translation mechanism revisited...

In some ways, using Wolfram engine in Sage is safer, because you have independent tools to check individual parts of your work. It also offers some serious advantages in terms of integration with the rest of the ecosystem (LaTeX, graphics, etc...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2 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: 2 years ago

Seen: 890 times

Last updated: Jun 21 '22