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.Mon, 06 Dec 2021 13:29:01 +0100Manipulate dummy variable after solving trighttps://ask.sagemath.org/question/60108/manipulate-dummy-variable-after-solving-trig/Hi!
This might be somewhat basic, but is there a way to directly reference the dummy variable that pops up, when solving trigonometric functions?
sy(t)=sin(t)/2+7*sin(t/3)/2
sols=solve(sy(t)==0,t, to_poly_solve='force')
sols[0]
returns:
t == 3*pi + 6*pi*z27541
This dummy variable obviously changes every time the command is run, so there is no way of directly referencing it without changing the reference every time. Obviously this doesn't work:
sols[0](z=0)
The closest I have come, is just blindly referencing the variable which works if there are no other variables in the right hand side, like this:
sols[0].rhs()(0)
but this gives the obnoxious error message, that "*Substitution using function-call syntax and unnamed arguments is deprecated (...)*"
Is there a way to cleanly tell sage to evaluate an expression, when all z variables are 0 or 1 etc?Mon, 06 Dec 2021 00:36:56 +0100https://ask.sagemath.org/question/60108/manipulate-dummy-variable-after-solving-trig/Answer by cav_rt for <p>Hi!
This might be somewhat basic, but is there a way to directly reference the dummy variable that pops up, when solving trigonometric functions?</p>
<pre><code>sy(t)=sin(t)/2+7*sin(t/3)/2
sols=solve(sy(t)==0,t, to_poly_solve='force')
sols[0]
</code></pre>
<p>returns:</p>
<pre><code>t == 3*pi + 6*pi*z27541
</code></pre>
<p>This dummy variable obviously changes every time the command is run, so there is no way of directly referencing it without changing the reference every time. Obviously this doesn't work:</p>
<pre><code>sols[0](z=0)
</code></pre>
<p>The closest I have come, is just blindly referencing the variable which works if there are no other variables in the right hand side, like this:</p>
<pre><code>sols[0].rhs()(0)
</code></pre>
<p>but this gives the obnoxious error message, that "<em>Substitution using function-call syntax and unnamed arguments is deprecated (...)</em>"</p>
<p>Is there a way to cleanly tell sage to evaluate an expression, when all z variables are 0 or 1 etc?</p>
https://ask.sagemath.org/question/60108/manipulate-dummy-variable-after-solving-trig/?answer=60109#post-id-60109You can get the dummy variable by
dummy_var = sols[0].rhs().variables()[0]
And then define a callable function changing the dummy variable to a proper symbolic one.
var('z')
f(z) = sols[0].rhs().subs({dummy_var:z})
Now you can simply do `f(0)`, `f(1)` or whatever you want.
PS, for some reason it does not work if you try to define a callable function using the dummy variable directly, like
f(dummy_var) = sols[0].rhs()
EDIT:
Another possibility is to use the `solveset` from `sympy`
from sympy import solveset
solveset(sy(t)._sympy_())
It gives the solution as a union of sets.Mon, 06 Dec 2021 05:42:42 +0100https://ask.sagemath.org/question/60108/manipulate-dummy-variable-after-solving-trig/?answer=60109#post-id-60109Comment by jakupl for <p>You can get the dummy variable by</p>
<pre><code>dummy_var = sols[0].rhs().variables()[0]
</code></pre>
<p>And then define a callable function changing the dummy variable to a proper symbolic one.</p>
<pre><code>var('z')
f(z) = sols[0].rhs().subs({dummy_var:z})
</code></pre>
<p>Now you can simply do <code>f(0)</code>, <code>f(1)</code> or whatever you want.</p>
<p>PS, for some reason it does not work if you try to define a callable function using the dummy variable directly, like </p>
<pre><code>f(dummy_var) = sols[0].rhs()
</code></pre>
<p>EDIT:</p>
<p>Another possibility is to use the <code>solveset</code> from <code>sympy</code></p>
<pre><code>from sympy import solveset
solveset(sy(t)._sympy_())
</code></pre>
<p>It gives the solution as a union of sets.</p>
https://ask.sagemath.org/question/60108/manipulate-dummy-variable-after-solving-trig/?comment=60114#post-id-60114Thank you! I had been trying something like this, but it's the curly brackets that got me.
Not experienced enough in python yet.
My life will be so much easier now, being able to quickly do:
var('z')
solz=[]
for i in range(len(sols)):
dummy_var = sols[i].rhs().variables()[0]
solz.append(sols[i].rhs().subs({dummy_var:z}))
(as long as there are not other variables obviously.)Mon, 06 Dec 2021 13:29:01 +0100https://ask.sagemath.org/question/60108/manipulate-dummy-variable-after-solving-trig/?comment=60114#post-id-60114