@@ -2696,3 +2696,82 @@ def _sympy_(self, l):
2696
2696
2697
2697
cases = Function_cases ()
2698
2698
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
+
0 commit comments