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.Mon, 04 Jul 2011 06:21:37 -0500Finding complex roots numerically using sagehttp://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/Can sage find complex roots numerically for equations like
cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))
if so, how?Sat, 02 Jul 2011 14:04:50 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/Answer by Mike Hansen for <p>Can sage find complex roots numerically for equations like </p>
<p>cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))</p>
<p>if so, how?</p>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12488#post-id-12488You can use `scipy.optimize.fsolve` to find a solution, but you'll need to set up things slightly differently. You'll want to consider your function as a map from `RR^2 -> RR^2` as opposed to `CC -> CC`.
sage: x, y = var('x,y')
sage: g = cos(cos(cos(cos(x+i*y)))) - sin(sin(sin(sin(x+i*y))))
sage: f(x,y) = (real(g), imag(g))
sage: from scipy.optimize import fsolve
sage: fsolve(lambda v: f(*v), (1.0,1.0))
array([ 0.75688714, 0.610155 ])
sage: f(*_)
(1.87627691162e-13, -4.80504525058e-13)
You'll have to be careful with your initial guess as the procedure may not converge.
sage: fsolve(lambda v: f(*v), (0.0,0.0))
array([ 1.01756188e+00, 1.17664027e-08])
sage: f(*_)
(0.165849713543, -9.91343156065e-13)
SciPy should give a warning the first time this happens, but it's best to use the `full_output` option to be sure:
sage: fsolve(lambda v: f(*v), (0.0,0.0), full_output=1)
(array([ 1.01756188e+00, 1.17664027e-08]), {'qtf': array([ -1.65849759e-01, -2.24024475e-06]), 'nfev': 26, 'fjac': array([[ -1.00000000e+00, 1.35076898e-05],
[ -1.35076898e-05, -1.00000000e+00]]), 'r': array([ 3.14707173e-04, 3.50982324e-09, -2.59845204e-04]), 'fvec': array([ 1.65849714e-01, -9.91343156e-13])}, 5, 'The iteration is not making good progress, as measured by the \n improvement from the last ten iterations.')
(Note the "The iteration is not making good progress" comment. See [the SciPy documentation](http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html) for the specifics on the output given by `full_output`.)
Looking at [question 500](http://ask.sagemath.org/question/500/using-sage-symbolic-functions-in-scipy-fsolve) will also be useful. It probably wouldn't be too difficult to make a something that does this behind the scenes.Sun, 03 Jul 2011 06:53:35 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12488#post-id-12488Comment by kcrisman for <p>You can use <code>scipy.optimize.fsolve</code> to find a solution, but you'll need to set up things slightly differently. You'll want to consider your function as a map from <code>RR^2 -> RR^2</code> as opposed to <code>CC -> CC</code>. </p>
<pre><code>sage: x, y = var('x,y')
sage: g = cos(cos(cos(cos(x+i*y)))) - sin(sin(sin(sin(x+i*y))))
sage: f(x,y) = (real(g), imag(g))
sage: from scipy.optimize import fsolve
sage: fsolve(lambda v: f(*v), (1.0,1.0))
array([ 0.75688714, 0.610155 ])
sage: f(*_)
(1.87627691162e-13, -4.80504525058e-13)
</code></pre>
<p>You'll have to be careful with your initial guess as the procedure may not converge.</p>
<pre><code>sage: fsolve(lambda v: f(*v), (0.0,0.0))
array([ 1.01756188e+00, 1.17664027e-08])
sage: f(*_)
(0.165849713543, -9.91343156065e-13)
</code></pre>
<p>SciPy should give a warning the first time this happens, but it's best to use the <code>full_output</code> option to be sure:</p>
<pre><code>sage: fsolve(lambda v: f(*v), (0.0,0.0), full_output=1)
(array([ 1.01756188e+00, 1.17664027e-08]), {'qtf': array([ -1.65849759e-01, -2.24024475e-06]), 'nfev': 26, 'fjac': array([[ -1.00000000e+00, 1.35076898e-05],
[ -1.35076898e-05, -1.00000000e+00]]), 'r': array([ 3.14707173e-04, 3.50982324e-09, -2.59845204e-04]), 'fvec': array([ 1.65849714e-01, -9.91343156e-13])}, 5, 'The iteration is not making good progress, as measured by the \n improvement from the last ten iterations.')
</code></pre>
<p>(Note the "The iteration is not making good progress" comment. See <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html">the SciPy documentation</a> for the specifics on the output given by <code>full_output</code>.)</p>
<p>Looking at <a href="http://ask.sagemath.org/question/500/using-sage-symbolic-functions-in-scipy-fsolve">question 500</a> will also be useful. It probably wouldn't be too difficult to make a something that does this behind the scenes.</p>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21508#post-id-21508Thanks, Mike. I did not know about fsolve. There are a lot of Scipy things that need better wrapping/use, see also #4942 and #2607.Mon, 04 Jul 2011 06:21:37 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21508#post-id-21508Comment by ebs for <p>You can use <code>scipy.optimize.fsolve</code> to find a solution, but you'll need to set up things slightly differently. You'll want to consider your function as a map from <code>RR^2 -> RR^2</code> as opposed to <code>CC -> CC</code>. </p>
<pre><code>sage: x, y = var('x,y')
sage: g = cos(cos(cos(cos(x+i*y)))) - sin(sin(sin(sin(x+i*y))))
sage: f(x,y) = (real(g), imag(g))
sage: from scipy.optimize import fsolve
sage: fsolve(lambda v: f(*v), (1.0,1.0))
array([ 0.75688714, 0.610155 ])
sage: f(*_)
(1.87627691162e-13, -4.80504525058e-13)
</code></pre>
<p>You'll have to be careful with your initial guess as the procedure may not converge.</p>
<pre><code>sage: fsolve(lambda v: f(*v), (0.0,0.0))
array([ 1.01756188e+00, 1.17664027e-08])
sage: f(*_)
(0.165849713543, -9.91343156065e-13)
</code></pre>
<p>SciPy should give a warning the first time this happens, but it's best to use the <code>full_output</code> option to be sure:</p>
<pre><code>sage: fsolve(lambda v: f(*v), (0.0,0.0), full_output=1)
(array([ 1.01756188e+00, 1.17664027e-08]), {'qtf': array([ -1.65849759e-01, -2.24024475e-06]), 'nfev': 26, 'fjac': array([[ -1.00000000e+00, 1.35076898e-05],
[ -1.35076898e-05, -1.00000000e+00]]), 'r': array([ 3.14707173e-04, 3.50982324e-09, -2.59845204e-04]), 'fvec': array([ 1.65849714e-01, -9.91343156e-13])}, 5, 'The iteration is not making good progress, as measured by the \n improvement from the last ten iterations.')
</code></pre>
<p>(Note the "The iteration is not making good progress" comment. See <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html">the SciPy documentation</a> for the specifics on the output given by <code>full_output</code>.)</p>
<p>Looking at <a href="http://ask.sagemath.org/question/500/using-sage-symbolic-functions-in-scipy-fsolve">question 500</a> will also be useful. It probably wouldn't be too difficult to make a something that does this behind the scenes.</p>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21512#post-id-21512This is a good idea. ThanksSun, 03 Jul 2011 12:50:04 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21512#post-id-21512Answer by kcrisman for <p>Can sage find complex roots numerically for equations like </p>
<p>cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))</p>
<p>if so, how?</p>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12485#post-id-12485Sorry - I didn't read the "complex" part! Ignore the answer below.
Even in Maxima this seems to mostly be available for polynomials only, which is also true in Sage. I'd be interested in any answer to this. See my other answer for a poor, but cool-looking, substitute.
+++++
Usually the `find_root` is pretty useful here - see the example below. However, this particular situation has no (real) roots, I think.
sage: f = cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))
sage: f.find_root(-100,100)
------------------------------------------
RuntimeError: f appears to have no zero on the interval
Plotting it makes this clear, plus the obvious periodicity:
sage: plot(f,-100,100)
Sat, 02 Jul 2011 16:34:15 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12485#post-id-12485Comment by ebs for <p>Sorry - I didn't read the "complex" part! Ignore the answer below.</p>
<p>Even in Maxima this seems to mostly be available for polynomials only, which is also true in Sage. I'd be interested in any answer to this. See my other answer for a poor, but cool-looking, substitute.</p>
<p>+++++</p>
<p>Usually the <code>find_root</code> is pretty useful here - see the example below. However, this particular situation has no (real) roots, I think.</p>
<pre><code>sage: f = cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))
sage: f.find_root(-100,100)
------------------------------------------
RuntimeError: f appears to have no zero on the interval
</code></pre>
<p>Plotting it makes this clear, plus the obvious periodicity:</p>
<pre><code>sage: plot(f,-100,100)
</code></pre>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21516#post-id-21516Yes, no real roots!Sat, 02 Jul 2011 19:01:43 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21516#post-id-21516Answer by parzan for <p>Can sage find complex roots numerically for equations like </p>
<p>cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))</p>
<p>if so, how?</p>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12487#post-id-12487Here is one way to find a solution, bypassing the complex nature of the problem:
sage: var('z')
z
sage: f = cos(cos(cos(cos(z)))) - sin(sin(sin(sin(z))))
sage: var('a,b', domain=RR)
(a, b)
sage: M = f(z=a+b*i).real()^2+f(z=a+b*i).imag()^2
sage: minimize(M,[0,0],algorithm='powell',disp=0)
(0.756887137348, 0.61015499692)
sage: M(a=_[0],b=_[1])
6.39826537371e-26
You can try various starting points and other algorithms (ncg,bfgs) - they give different results.
In Maple things are much simpler:
> fsolve(cos(cos(cos(cos(x)))) = sin(sin(sin(sin(x)))),x,complex);
> -0.7401150735 - 1.364789582 I
so it is possible...
Sun, 03 Jul 2011 01:54:59 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12487#post-id-12487Comment by ebs for <p>Here is one way to find a solution, bypassing the complex nature of the problem:</p>
<pre><code>sage: var('z')
z
sage: f = cos(cos(cos(cos(z)))) - sin(sin(sin(sin(z))))
sage: var('a,b', domain=RR)
(a, b)
sage: M = f(z=a+b*i).real()^2+f(z=a+b*i).imag()^2
sage: minimize(M,[0,0],algorithm='powell',disp=0)
(0.756887137348, 0.61015499692)
sage: M(a=_[0],b=_[1])
6.39826537371e-26
</code></pre>
<p>You can try various starting points and other algorithms (ncg,bfgs) - they give different results.</p>
<p>In Maple things are much simpler:</p>
<blockquote>
<p>fsolve(cos(cos(cos(cos(x)))) = sin(sin(sin(sin(x)))),x,complex);</p>
<p>-0.7401150735 - 1.364789582 I</p>
</blockquote>
<p>so it is possible...</p>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21513#post-id-21513This is a good idea. ThanksSun, 03 Jul 2011 12:49:59 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21513#post-id-21513Answer by kcrisman for <p>Can sage find complex roots numerically for equations like </p>
<p>cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))</p>
<p>if so, how?</p>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12486#post-id-12486Here is something that doesn't answer your question, but could at least help you narrow it down some - and perhaps some code in it could help.
sage: f = cos(cos(cos(cos(x)))) - sin(sin(sin(sin(x))))
sage: complex_plot(f,(-pi,pi),(-pi,pi),plot_points=500)
This will show the results of `f` on this square. The darker the area, the smaller the modulus (color indicates the argument of the image of that point). You can compare with the identity map to see what is what.
sage: complex_plot(x,(-pi,pi),(-pi,pi),plot_points=500)
Sat, 02 Jul 2011 16:50:44 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?answer=12486#post-id-12486Comment by ebs for <p>Here is something that doesn't answer your question, but could at least help you narrow it down some - and perhaps some code in it could help.</p>
<pre><code>sage: f = cos(cos(cos(cos(x)))) - sin(sin(sin(sin(x))))
sage: complex_plot(f,(-pi,pi),(-pi,pi),plot_points=500)
</code></pre>
<p>This will show the results of <code>f</code> on this square. The darker the area, the smaller the modulus (color indicates the argument of the image of that point). You can compare with the identity map to see what is what.</p>
<pre><code>sage: complex_plot(x,(-pi,pi),(-pi,pi),plot_points=500)
</code></pre>
http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21515#post-id-21515the complex plot shows regions in the complex plane where a root may be found. And its beautiful too. But, I am looking for a way to calculate numerical values of roots.Sat, 02 Jul 2011 19:45:51 -0500http://ask.sagemath.org/question/8205/finding-complex-roots-numerically-using-sage/?comment=21515#post-id-21515