Solve/find_root for miscellaneous special function elliptic_kc?

I am looking to use solve() or find_root() with a function defined using elliptic_kc (sage.functions.special.EllipticKC), however Sage produces an error. The context of this is for use in a coplanar waveguide impedance calculation.

Example:

sage: find_root(elliptic_kc(x)==2, x, 0, 0.5)

[...]

'TypeError: unable to simplify to float approximation'.

and

sage: solve(2.0==elliptic_kc(x), x, to_poly_solve=True)

[...]

TypeError: 'sage.symbolic.expression.Expression' object is not subscriptable

Is there a convenient way to perform a solve/numerical root for these special functions?

edit retag close merge delete

Oh huh, I found a solution using scipy.optimize.root_scalar. It's not the cleanest, but I suppose it works.

sage: def el(x): return elliptic_kc(x) - 2.0

sage: root_scalar(el, bracket=(0,1)).root

( 2023-05-11 05:13:22 +0100 )edit

To display code blocks, indent them by four spaces.

( 2023-05-19 22:00:28 +0100 )edit

Sort by ยป oldest newest most voted

Defining a function instead worked for me:

sage: f = lambda x: elliptic_kc(x) - 2
sage: find_root(f, 0, 0.9)
0.6438562191479369


Alternatively, one can use the solve functionality from pari/gp:

sage: gp("solve(x=0, 0.7, ellK(x^(1/2)) - 2)")
0.64385621914775464686672115765108324989


Note that the pari/gp function ellK is not the same (complete) elliptic $K$ function from sage. For instance, the values in $1/4$ differ:

sage: elliptic_kc(1/4.)
1.68575035481260


and

? ellK(1/4)
%12 = 1.5962422221317835101489690714979498795


However:

? ellK(1/2)
%13 = 1.6857503548125960428712036577990769895


Just as a note: pari/gp can give us more decimal places:

? \p 150
realprecision = 154 significant digits (150 digits displayed)
? solve(x=0, 0.7, ellK(x^(1/2)) - 2)
%14 = 0.643856219147754646866721157651083249845248156171320703143872009317482774610540686232984439485823088513555922361027026184004141998761703004038574916462
?

more