ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 28 Jan 2011 04:27:49 -0600Simplify, Solve Equation and use solutionhttp://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/Hi, I'm new to sage, and have the following question:
I have the following code:
> x,y=var('x y')
>
> f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
> f.full_simplify
> show(f(x,y))
>
>
>
> s1=solve([f(x,y)==1],x,y)
> show(s1)
Sage obiously doesn't simplify the function f... (simple trigonometric expression). And it doesn't solve it.
**1**) How can I simplify the function?
**2)** How can I use the result? For example: I have as an result:
solution=[x == -1]
How can I replace this x in an other function (example: sin(x)).
In mathematica I would do: sin(x) /. solution
or manually: sin(x) /. x->(-1)Fri, 28 Jan 2011 03:07:13 -0600http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/Answer by kcrisman for <p>Hi, I'm new to sage, and have the following question:
I have the following code:</p>
<blockquote>
<pre><code>x,y=var('x y')
f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
f.full_simplify
show(f(x,y))
s1=solve([f(x,y)==1],x,y)
show(s1)
</code></pre>
</blockquote>
<p>Sage obiously doesn't simplify the function f... (simple trigonometric expression). And it doesn't solve it.</p>
<p><strong>1</strong>) How can I simplify the function?</p>
<p><strong>2)</strong> How can I use the result? For example: I have as an result:
solution=[x == -1]
How can I replace this x in an other function (example: sin(x)).
In mathematica I would do: sin(x) /. solution
or manually: sin(x) /. x->(-1)</p>
http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?answer=12028#post-id-12028A few comments.
- If you use the `f(x,y)` syntax, you don't have to declare the variables ahead of time.
- Using results is not too bad, because you can ask for them as a dictionary. There are other examples in the doc, I think, but for example:
sage: B = (x^2-1).solve(x)
sage: G = x^3+3*x
sage: [G.subs(b) for b in B]
[-4, 4]
sage: B = (x^2-1).solve(x,solution_dict=True)
sage: [G.subs(b) for b in B]
[-4, 4]
- The actual first question is hard to find, and maybe should be part of our full simplification...:
sage: f(x,y)=sin(x)*cos(y)+sin(y)*cos(x)
sage: f.trig_reduce()
(x, y) |--> sin(x + y)
Note to Evgeny: I cannot get code formatted text after a bullet point. What gives? I asked this on the askbot website too.
Fri, 28 Jan 2011 03:33:45 -0600http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?answer=12028#post-id-12028Comment by DSM for <p>A few comments. </p>
<ul>
<li>If you use the <code>f(x,y)</code> syntax, you don't have to declare the variables ahead of time.</li>
<li><p>Using results is not too bad, because you can ask for them as a dictionary. There are other examples in the doc, I think, but for example:</p>
<p>sage: B = (x^2-1).solve(x)</p>
<p>sage: G = x^3+3*x</p>
<p>sage: [G.subs(b) for b in B]</p>
<p>[-4, 4]</p>
<p>sage: B = (x^2-1).solve(x,solution_dict=True)</p>
<p>sage: [G.subs(b) for b in B]</p>
<p>[-4, 4]</p></li>
<li><p>The actual first question is hard to find, and maybe should be part of our full simplification...:</p></li>
</ul>
<p>sage: f(x,y)=sin(x)<em>cos(y)+sin(y)</em>cos(x)</p>
<p>sage: f.trig_reduce()</p>
<p>(x, y) |--> sin(x + y)</p>
<p>Note to Evgeny: I cannot get code formatted text after a bullet point. What gives? I asked this on the askbot website too.</p>
http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?comment=22228#post-id-22228Beat me by three minutes. :^)Fri, 28 Jan 2011 03:37:57 -0600http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?comment=22228#post-id-22228Answer by manifold for <p>Hi, I'm new to sage, and have the following question:
I have the following code:</p>
<blockquote>
<pre><code>x,y=var('x y')
f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
f.full_simplify
show(f(x,y))
s1=solve([f(x,y)==1],x,y)
show(s1)
</code></pre>
</blockquote>
<p>Sage obiously doesn't simplify the function f... (simple trigonometric expression). And it doesn't solve it.</p>
<p><strong>1</strong>) How can I simplify the function?</p>
<p><strong>2)</strong> How can I use the result? For example: I have as an result:
solution=[x == -1]
How can I replace this x in an other function (example: sin(x)).
In mathematica I would do: sin(x) /. solution
or manually: sin(x) /. x->(-1)</p>
http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?answer=12030#post-id-12030Thanks it works!
Here for everyone the code to show it:
> # Solve an equation an use the solution
> mysol=solve([2+x^2==-1],x)
> show(mysol)
>
>
> G = x^3-i
> show(G)
>
> newG=G.subs(mysol[0])
> show(newG)
>
> [G.subs(j) for j in mysol]
But the trig reduce doesn't work with this code:
> f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
> f.trig_reduce()
> show(f)
>
>
>
> s1=solve([f(x,y)==1],x,y)
> show(s1)
I still get
(x, y) |--> sin(x)*cos(y) + sin(y)*cos(x)
Fri, 28 Jan 2011 03:59:18 -0600http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?answer=12030#post-id-12030Comment by manifold for <p>Thanks it works!</p>
<p>Here for everyone the code to show it:</p>
<blockquote>
<pre><code># Solve an equation an use the solution
mysol=solve([2+x^2==-1],x)
show(mysol)
G = x^3-i
show(G)
newG=G.subs(mysol[0])
show(newG)
[G.subs(j) for j in mysol]
</code></pre>
</blockquote>
<p>But the trig reduce doesn't work with this code:</p>
<blockquote>
<pre><code>f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
f.trig_reduce()
show(f)
s1=solve([f(x,y)==1],x,y)
show(s1)
</code></pre>
</blockquote>
<p>I still get
(x, y) |--> sin(x)<em>cos(y) + sin(y)</em>cos(x)</p>
http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?comment=22226#post-id-22226thanks! it works.Fri, 28 Jan 2011 04:27:49 -0600http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?comment=22226#post-id-22226Comment by DSM for <p>Thanks it works!</p>
<p>Here for everyone the code to show it:</p>
<blockquote>
<pre><code># Solve an equation an use the solution
mysol=solve([2+x^2==-1],x)
show(mysol)
G = x^3-i
show(G)
newG=G.subs(mysol[0])
show(newG)
[G.subs(j) for j in mysol]
</code></pre>
</blockquote>
<p>But the trig reduce doesn't work with this code:</p>
<blockquote>
<pre><code>f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
f.trig_reduce()
show(f)
s1=solve([f(x,y)==1],x,y)
show(s1)
</code></pre>
</blockquote>
<p>I still get
(x, y) |--> sin(x)<em>cos(y) + sin(y)</em>cos(x)</p>
http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?comment=22227#post-id-22227f.trig_reduce() returns a *copy* of f which has been trig_reduced. It doesn't affect f. Try f=f.trig_reduce().Fri, 28 Jan 2011 04:17:09 -0600http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?comment=22227#post-id-22227Answer by DSM for <p>Hi, I'm new to sage, and have the following question:
I have the following code:</p>
<blockquote>
<pre><code>x,y=var('x y')
f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
f.full_simplify
show(f(x,y))
s1=solve([f(x,y)==1],x,y)
show(s1)
</code></pre>
</blockquote>
<p>Sage obiously doesn't simplify the function f... (simple trigonometric expression). And it doesn't solve it.</p>
<p><strong>1</strong>) How can I simplify the function?</p>
<p><strong>2)</strong> How can I use the result? For example: I have as an result:
solution=[x == -1]
How can I replace this x in an other function (example: sin(x)).
In mathematica I would do: sin(x) /. solution
or manually: sin(x) /. x->(-1)</p>
http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?answer=12029#post-id-12029Sage's simplification facilities derive from Maxima, which is powerful but sometimes quirky. You often have to be a bit more specific about which transformations you want.
sage: x,y = var("x,y")
sage:
sage: f(x,y)= sin(x)*cos(y)+sin(y)*cos(x)
sage:
sage: f.reduce_trig()
(x, y) |--> sin(x + y)
sage: f = f.reduce_trig()
sage: solve(f(x,y)==1, y)
[y == 1/2*pi - x]
As for how to use the solutions, you want to read the help for the "subs" and "subs_expr" methods (substitute, substitute_expression): type help(Expression.subs) and help(Expression.subs_expr) (or Expression.subs?).
Here are a few examples, though:
sage: # substituting into expressions in different ways
sage: g = sin(x)+y
sage: g.subs(x=1,y=2) # using keyword arguments
sin(1) + 2
sage: g.subs({x:3, y:4}) # from a dictionary of values
sin(3) + 4
sage: g.subs(x==9) # from ONE equation
y + sin(9)
sage: g.subs_expr(*[x==5, y==6]) # from a list of equations
sin(5) + 6
I like solutions to be in dictionaries, so:
sage: solve([x+2*y==3,3*x-y==5],x,y,solution_dict=True)
[{y: 4/7, x: 13/7}]
sage: sols = _ # the underscore just refers to the last result
sage: g = sin(x)+y
sage: g.subs(sols[0])
sin(13/7) + 4/7
Fri, 28 Jan 2011 03:37:08 -0600http://ask.sagemath.org/question/7908/simplify-solve-equation-and-use-solution/?answer=12029#post-id-12029