avoiding error in findroot (mpmath)

111 views
Skip to first unread message

Mike

unread,
Dec 31, 2012, 2:01:00 AM12/31/12
to sage-s...@googlegroups.com
For varying values of n, I'd like to find values of a so that int_0^z (x-a)^n dx has a root on the unit circle.

As I'd like high precision, mpmath seems right.

I can set things up as follows:

sage: from mpmath import *; mp.dps=30; mp.pretty=true
sage: var("a b c", domain="real")
sage: p=integral((x-a)^2, x)
sage: print p.subs(x=b+c*I).real()
sage: print p.subs(x=b+c*I).imag()
a^2*b - a*b^2 + a*c^2 + 1/3*b^3 - b*c^2
a^2*c - 2*a*b*c + b^2*c - 1/3*c^3

and then copy-and-paste the equations into a function
and solve:

sage: def f(a, b, c):
...       return (a^2*b - a*b^2 + a*c^2 + 1/3*b^3 - b*c^2,
...               a^2*c - 2*a*b*c + b^2*c - 1/3*c^3,
...               b^2+c^2-1 )
...         
sage: findroot(f, (0.57, 0.85, 0.49))
...      
[0.577350269189625764509148780502]
[0.866025403784438646763723170753]
[                             0.5]

which works, but I need to eliminate the manual copy-and-paste.

What can I do to avoid the following error?

sage: def g(a, b, c):
...       return (p.subs(x=b+c*I).real(),
...               p.subs(x=b+c*I).imag(),
...               b^2+c^2-1 )
...         
sage: findroot(g, (0.57, 0.85, 0.49))
Traceback (most recent call last):
...
TypeError: g() takes exactly 3 arguments (1 given)

Marshall Hampton

unread,
Jan 9, 2013, 4:25:39 PM1/9/13
to sage-s...@googlegroups.com
I'm bumping this up since I can't figure it out and I'm talking to Mike at the Sage booth at JMMS.  Can anyone answer this?

-Marshall

kcrisman

unread,
Jan 9, 2013, 10:55:00 PM1/9/13
to sage-s...@googlegroups.com
This is really an mpmath question...


On Wednesday, January 9, 2013 4:25:39 PM UTC-5, Marshall Hampton wrote:
I'm bumping this up since I can't figure it out and I'm talking to Mike at the Sage booth at JMMS.  Can anyone answer this?


    926             multidimensional = isinstance(fx, (list, tuple, ctx.matrix))
    927         except TypeError:
--> 928             fx = f(x0[0])
    929             multidimensional = False
    930         if 'multidimensional' in kwargs:

TypeError: g() takes exactly 3 arguments (1 given)


So the problem is that we are raising a TypeError and then of course g does *not* take just one argument x0[0] = .57 in this case.  Hmm.

        try:
            fx = f(*x0)
            multidimensional = isinstance(fx, (list, tuple, ctx.matrix))

But this assignment should work (and does "by hand").  The only thing that changes x0 is

        if isinstance(x0, (list, tuple)):
            x0 = [ctx.convert(x) for x in x0]


Got it.

sage: Z = (0.57, 0.85, 0.49)
sage: Z = [mpmath.convert(z) for z in Z]
sage: Z
[mpf('0.56999999999999995'), mpf('0.84999999999999998'), mpf('0.48999999999999999')]
sage: fx = g(*Z)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)


TypeError: no canonical coercion from <type 'sage.libs.mpmath.ext_main.mpc'> to Symbolic Ring



So what you need to do is ... tricky.  Can someone take it from here?   I tried several things (changes to g) to get those mpmath things to behave well (converting back to Sage) but the fact that g is symbolic makes it hard.  It should be possible, though!

- kcrisman
Reply all
Reply to author
Forward
0 new messages