ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 27 Aug 2015 03:41:36 +0200Substitute expressions with cos and sinhttps://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/ I'm trying to simplify a symbolic expression obtained from sage integration. It contains terms of cos(theta)^2*sin(theta)^2 and many others. I wanted to collect the coefficients of cos(theta)^2*sin(theta)^2 and used subs_expr for this purpose. Here's a simplified version of what I'm trying to do:
var('theta,x,Psi')
y(theta)=cos(theta)^2*sin(theta)^2*cos(Psi)
y.subs_expr(cos(theta)^2*sin(theta)^2==x)
The result I got was "theta |--> cos(Psi)*cos(theta)^2*sin(theta)^2", not the expected "x*cos(Psi)". How can I get my expected result?
Tue, 25 Aug 2015 01:18:52 +0200https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/Answer by kcrisman for <p>I'm trying to simplify a symbolic expression obtained from sage integration. It contains terms of cos(theta)^2<em>sin(theta)^2 and many others. I wanted to collect the coefficients of cos(theta)^2</em>sin(theta)^2 and used subs_expr for this purpose. Here's a simplified version of what I'm trying to do:</p>
<pre><code>var('theta,x,Psi')
y(theta)=cos(theta)^2*sin(theta)^2*cos(Psi)
y.subs_expr(cos(theta)^2*sin(theta)^2==x)
</code></pre>
<p>The result I got was "theta |--> cos(Psi)<em>cos(theta)^2</em>sin(theta)^2", not the expected "x*cos(Psi)". How can I get my expected result?</p>
https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/?answer=29357#post-id-29357Half of your question is easier to answer. I bet if you do
var('theta,x,Psi')
y=cos(theta)^2*sin(theta)^2*cos(Psi)
y.subs_expr(cos(theta)^2*sin(theta)^2==x)
you won't get the `theta |-->` bit. That is because you are defining a function, not just an expression, in your example.
For substituting more advanced expressions, you will probably have to use [wildcards](http://stackoverflow.com/questions/15360904/how-to-express-variable-through-other-in-sage). Note that
sage: y.subs_expr(cos(theta)^2==x)
x*cos(Psi)*sin(theta)^2
works; it's the fact that your expression isn't just one thing, but a product, that probably messes things up.
sage: y.subs({cos(theta)^2*sin(theta)^2*w0:x*w0})
x*cos(Psi)
but keep in mind that for your more complex expression you may need to have a more complicated use of wildcards. Good luck!Tue, 25 Aug 2015 15:55:57 +0200https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/?answer=29357#post-id-29357Comment by Liang for <p>Half of your question is easier to answer. I bet if you do</p>
<pre><code>var('theta,x,Psi')
y=cos(theta)^2*sin(theta)^2*cos(Psi)
y.subs_expr(cos(theta)^2*sin(theta)^2==x)
</code></pre>
<p>you won't get the <code>theta |--></code> bit. That is because you are defining a function, not just an expression, in your example.</p>
<p>For substituting more advanced expressions, you will probably have to use <a href="http://stackoverflow.com/questions/15360904/how-to-express-variable-through-other-in-sage">wildcards</a>. Note that </p>
<pre><code>sage: y.subs_expr(cos(theta)^2==x)
x*cos(Psi)*sin(theta)^2
</code></pre>
<p>works; it's the fact that your expression isn't just one thing, but a product, that probably messes things up.</p>
<pre><code>sage: y.subs({cos(theta)^2*sin(theta)^2*w0:x*w0})
x*cos(Psi)
</code></pre>
<p>but keep in mind that for your more complex expression you may need to have a more complicated use of wildcards. Good luck!</p>
https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/?comment=29362#post-id-29362Thank you very much for you suggestions.
For people have similar needs as mine, I found maxima.fullratsubst did the job.Wed, 26 Aug 2015 04:56:30 +0200https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/?comment=29362#post-id-29362Comment by kcrisman for <p>Half of your question is easier to answer. I bet if you do</p>
<pre><code>var('theta,x,Psi')
y=cos(theta)^2*sin(theta)^2*cos(Psi)
y.subs_expr(cos(theta)^2*sin(theta)^2==x)
</code></pre>
<p>you won't get the <code>theta |--></code> bit. That is because you are defining a function, not just an expression, in your example.</p>
<p>For substituting more advanced expressions, you will probably have to use <a href="http://stackoverflow.com/questions/15360904/how-to-express-variable-through-other-in-sage">wildcards</a>. Note that </p>
<pre><code>sage: y.subs_expr(cos(theta)^2==x)
x*cos(Psi)*sin(theta)^2
</code></pre>
<p>works; it's the fact that your expression isn't just one thing, but a product, that probably messes things up.</p>
<pre><code>sage: y.subs({cos(theta)^2*sin(theta)^2*w0:x*w0})
x*cos(Psi)
</code></pre>
<p>but keep in mind that for your more complex expression you may need to have a more complicated use of wildcards. Good luck!</p>
https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/?comment=29366#post-id-29366Yes, Maxima has a lot of methods of this type - great that you found this!Thu, 27 Aug 2015 03:41:36 +0200https://ask.sagemath.org/question/29349/substitute-expressions-with-cos-and-sin/?comment=29366#post-id-29366