Ignore:
Timestamp:
05/24/07 22:53:44 (6 years ago)
Author:
Robert Bradshaw <robertwb@…>
Branch:
default
Children:
4604:72093fbd8708, 4845:1d47d9a22376
Message:

number field sqrt (via pari)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/rings/number_field/number_field_element.pyx

    r4460 r4603  
    363363            return sage.rings.arith.generic_power(x, right, one=self.parent()(1)) 
    364364        return sage.rings.arith.generic_power(self, right, one=self.parent()(1)) 
     365         
     366    def is_square(self): 
     367        return len(self.sqrt(all=True)) > 0 
     368         
     369    def sqrt(self, all=False): 
     370        """ 
     371        Returns the square root of this number in the given number field.  
     372         
     373        EXAMPLES: 
     374            sage: K.<a> = NumberField(x^2 - 3) 
     375            sage: K(3).sqrt() 
     376            a 
     377            sage: K(3).sqrt(all=True) 
     378            [a, -a] 
     379            sage: K(a^10).sqrt() 
     380            9*a 
     381            sage: K(49).sqrt() 
     382            7 
     383            sage: K(1+a).sqrt() 
     384            Traceback (most recent call last): 
     385            ... 
     386            ValueError: a + 1 not a square in Number Field in a with defining polynomial x^2 - 3 
     387            sage: K(0).sqrt() 
     388            0 
     389            sage: K((7+a)^2).sqrt(all=True) 
     390            [a + 7, -a - 7] 
     391             
     392            sage: K.<a> = CyclotomicField(7) 
     393            sage: a.sqrt()   
     394            a^4 
     395             
     396            sage: K.<a> = NumberField(x^5 - x + 1) 
     397            sage: (a^4 + a^2 - 3*a + 2).sqrt() 
     398            a^3 - a^2 
     399 
     400        ALGORITHM:  
     401            Use Pari to factor $x^2$ - \code{self} in K.  
     402             
     403        """ 
     404        # For now, use pari's factoring abilities 
     405        R = sage.rings.polynomial.polynomial_ring.PolynomialRing(self._parent, 't') 
     406        f = R([-self, 0, 1]) 
     407        roots = f.roots() 
     408        if all: 
     409            return [r[0] for r in roots] 
     410        elif len(roots) > 0: 
     411            return roots[0][0] 
     412        else: 
     413            raise ValueError, "%s not a square in %s"%(self, self._parent) 
    365414 
    366415    cdef void _reduce_c_(self): 
     
    781830            0         
    782831        """ 
    783         # The minimal polynomial is square-free and 
    784         # divisible by same irreducible factors as 
    785         # the characteristic polynomial. 
    786         # TODO: factoring to find the square-free part is idiotic. 
    787         # Instead use a GCD algorithm! 
    788         f = sage.rings.polynomial.polynomial_ring.PolynomialRing(QQ, str(var))(1) 
    789         for g, _ in self.charpoly(var).factor(): 
    790             f *= g 
    791         return f 
     832        return self.charpoly(var).radical() # square free part of charpoly 
    792833 
    793834    def matrix(self): 
Note: See TracChangeset for help on using the changeset viewer.