Ask Your Question
1

Substitute for sub-expression

asked 2022-06-21 01:03:22 +0100

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2022-06-21 07:31:10 +0100

Emmanuel Charpentier gravatar image

updated 2022-06-21 10:57:15 +0100

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 = \frac{V_{0} p_{1} \cos\left(a \sqrt{\beta}\right) \sin\left(a \sqrt{\beta}\right) - \sqrt{E} \sqrt{\beta} \sqrt{p_{1}} \cos\left(a \sqrt{\beta}\right)^{2} + \sqrt{E} \sqrt{\beta} \sqrt{p_{1}} \sin\left(a \sqrt{\beta}\right)^{2}}{2 \, p_{1} \cos\left(a \sqrt{\beta}\right) \sin\left(a \sqrt{\beta}\right)}$$

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 = \frac{1}{2} \, V_{0} + \frac{{\left(2 \, \sin\left(a \sqrt{\beta}\right)^{2} - 1\right)} \sqrt{E} \sqrt{\beta}}{2 \, \sqrt{p_{1}} \cos\left(a \sqrt{\beta}\right) \sin\left(a \sqrt{\beta}\right)}$$

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 = -\frac{\sqrt{E} \sqrt{\beta} \cos\left(2 \, a \sqrt{\beta}\right) \csc\left(2 \, a \sqrt{\beta}\right)}{\sqrt{p_{1}}} + \frac{1}{2} \, V_{0}$$

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 = -\frac{\sqrt{E} \sqrt{\beta} \cot\left(2 \, a \sqrt{\beta}\right)}{\sqrt{p_{1}}} + \frac{1}{2} \, V_{0}$$

Season to taste...

edit flag offensive delete link more
0

answered 2022-06-21 16:56:22 +0100

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.

edit flag offensive delete link more

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 ( 2022-06-21 18:27:38 +0100 )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

Stats

Asked: 2022-06-21 01:03:22 +0100

Seen: 691 times

Last updated: Jun 21 '22