Ask Your Question

Finding complex roots numerically using sage

asked 2011-07-02 14:04:50 -0500

ebs gravatar image

Can sage find complex roots numerically for equations like

cos(cos(cos(cos(x)))) == sin(sin(sin(sin(x))))

if so, how?

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted

answered 2011-07-03 06:53:35 -0500

Mike Hansen gravatar image

You 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 for the specifics on the output given by full_output.)

Looking at question 500 will also be useful. It probably wouldn't be too difficult to make a something that does this behind the scenes.

edit flag offensive delete link more


This is a good idea. Thanks

ebs gravatar imageebs ( 2011-07-03 12:50:04 -0500 )edit

Thanks, Mike. I did not know about fsolve. There are a lot of Scipy things that need better wrapping/use, see also #4942 and #2607.

kcrisman gravatar imagekcrisman ( 2011-07-04 06:21:37 -0500 )edit

answered 2011-07-03 01:54:59 -0500

parzan gravatar image

Here is one way to find a solution, bypassing the complex nature of the problem:

sage: var('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])                           

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...

edit flag offensive delete link more


This is a good idea. Thanks

ebs gravatar imageebs ( 2011-07-03 12:49:59 -0500 )edit

answered 2011-07-02 16:50:44 -0500

kcrisman gravatar image

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.

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)
edit flag offensive delete link more


the 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.

ebs gravatar imageebs ( 2011-07-02 19:45:51 -0500 )edit

answered 2011-07-02 16:34:15 -0500

kcrisman gravatar image

updated 2011-07-02 16:45:27 -0500

Sorry - 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)
edit flag offensive delete link more


Yes, no real roots!

ebs gravatar imageebs ( 2011-07-02 19:01:43 -0500 )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


Asked: 2011-07-02 14:04:50 -0500

Seen: 1,529 times

Last updated: Jul 03 '11