Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit ba171aa

Browse files
committed
22024: symbolic placeholder for complex root
1 parent 7f79a2d commit ba171aa

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

src/sage/functions/all.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
abs_symbolic, sqrt, log_gamma,
2626
gamma_inc, incomplete_gamma, gamma_inc_lower,
2727
arg, real_part, real, frac,
28-
imag_part, imag, imaginary, conjugate, cases)
28+
imag_part, imag, imaginary, conjugate, cases,
29+
complex_root_of)
2930

3031
from .log import (exp, exp_polar, log, ln, polylog, dilog, lambert_w, harmonic_number)
3132

src/sage/functions/other.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,3 +2696,82 @@ def _sympy_(self, l):
26962696

26972697
cases = Function_cases()
26982698

2699+
2700+
class Function_crootof(BuiltinFunction):
2701+
"""
2702+
Formal function holding ``(polynomial, index)`` pairs.
2703+
2704+
The expression evaluates to a floating point value that is an
2705+
approximation to a specific complex root of the polynomial. The
2706+
ordering is fixed so you always get the same root.
2707+
2708+
The functionality is imported from SymPy, see
2709+
http://docs.sympy.org/latest/_modules/sympy/polys/rootoftools.html
2710+
2711+
EXAMPLES::
2712+
2713+
sage: c = complex_root_of(x^6 + x + 1, 1); c
2714+
complex_root_of(x^6 + x + 1, 1)
2715+
sage: c.n()
2716+
-0.790667188814418 + 0.300506920309552*I
2717+
sage: c.n(100)
2718+
-0.79066718881441764449859281847 + 0.30050692030955162512001002521*I
2719+
sage: (c^6 + c + 1).n(100) < 1e-25
2720+
True
2721+
"""
2722+
def __init__(self):
2723+
"""
2724+
EXAMPLES::
2725+
2726+
sage: loads(dumps(complex_root_of))
2727+
complex_root_of
2728+
"""
2729+
BuiltinFunction.__init__(self, "complex_root_of", nargs=2,
2730+
conversions=dict(sympy='CRootOf'))
2731+
2732+
def _eval_(self, poly, index):
2733+
"""
2734+
TESTS::
2735+
2736+
sage: _ = var('y')
2737+
sage: complex_root_of(1, 1)
2738+
Traceback (most recent call last):
2739+
...
2740+
ValueError: polynomial in one variable required
2741+
sage: complex_root_of(x+y, 1)
2742+
Traceback (most recent call last):
2743+
...
2744+
ValueError: polynomial in one variable required
2745+
sage: complex_root_of(sin(x), 1)
2746+
Traceback (most recent call last):
2747+
...
2748+
ValueError: polynomial in one variable required
2749+
"""
2750+
try:
2751+
vars = poly.variables()
2752+
except AttributeError:
2753+
raise ValueError('polynomial in one variable required')
2754+
if len(vars) != 1 or not poly.is_polynomial(vars[0]):
2755+
raise ValueError('polynomial in one variable required')
2756+
2757+
def _evalf_(self, poly, index, parent=None, algorithm=None):
2758+
"""
2759+
EXAMPLES::
2760+
2761+
sage: complex_root_of(x^2-2, 1).n()
2762+
1.41421356237310
2763+
sage: complex_root_of(x^2-2, 3).n()
2764+
Traceback (most recent call last):
2765+
...
2766+
IndexError: root index out of [-2, 1] range, got 3
2767+
"""
2768+
from sympy.polys import CRootOf, Poly
2769+
try:
2770+
prec = parent.precision()
2771+
except AttributeError:
2772+
prec = 53
2773+
sobj = CRootOf(Poly(poly._sympy_()), int(index))
2774+
return sobj.n(ceil(prec*3/10))._sage_()
2775+
2776+
complex_root_of = Function_crootof()
2777+

src/sage/interfaces/sympy.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,24 @@ def _sympysage_abs(self):
578578
from sage.functions.other import abs_symbolic
579579
return abs_symbolic(self.args[0]._sage_())
580580

581+
def _sympysage_crootof(self):
582+
"""
583+
EXAMPLES::
584+
585+
sage: from sympy import Symbol, CRootOf
586+
sage: sobj = CRootOf(Symbol('x')**2 - 2, 1)
587+
sage: assert complex_root_of(x^2-2, 1)._sympy_() == sobj
588+
sage: assert complex_root_of(x^2-2, 1) == sobj._sage_()
589+
590+
sage: from sympy import solve as ssolve
591+
sage: sols = ssolve(x^6+x+1, x)
592+
sage: (sols[0]+1)._sage_().n()
593+
0.209332811185582 - 0.300506920309552*I
594+
"""
595+
from sage.functions.other import complex_root_of
596+
from sage.symbolic.ring import SR
597+
return complex_root_of(self.args[0]._sage_(), SR(self.args[1]))
598+
581599
def _sympysage_relational(self):
582600
"""
583601
EXAMPLES::
@@ -672,6 +690,7 @@ def sympy_init():
672690
from sympy.functions.special.tensor_functions import KroneckerDelta
673691
from sympy.logic.boolalg import BooleanTrue, BooleanFalse
674692
from sympy.integrals.integrals import Integral
693+
from sympy.polys.rootoftools import CRootOf
675694
from sympy.series.order import Order
676695

677696
Float._sage_ = _sympysage_float
@@ -715,6 +734,7 @@ def sympy_init():
715734
re._sage_ = _sympysage_re
716735
im._sage_ = _sympysage_im
717736
Abs._sage_ = _sympysage_abs
737+
CRootOf._sage_ = _sympysage_crootof
718738
BooleanFalse._sage_ = _sympysage_false
719739
BooleanTrue._sage_ = _sympysage_true
720740
ceiling._sage_ = _sympysage_ceiling

0 commit comments

Comments
 (0)