# HG changeset patch
# User ncalexan@gmail.com
# Date 1228443463 28800
# Node ID 8bfa940112f9f441a5ee05dd931565463c123405
# Parent  f0a6a5c413e30b6bf2f5695484145c1ab1bf7678
trac #4702 -- rebase against #4701 (in sage-3.2.1) and fix all broken doctests.

diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/modules/free_module.py
--- a/sage/modules/free_module.py	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/modules/free_module.py	Thu Dec 04 18:17:43 2008 -0800
@@ -1562,14 +1562,75 @@
             Full RSpace of degree 2 over Univariate Polynomial Ring in x over Rational Field
 
             sage: A = matrix([[1,0],[0,-1]])
-            sage: M = FreeModule(ZZ,2,inner_product_matrix=A)
+            sage: M = FreeModule(ZZ,2,inner_product_matrix=A); M
+            Ambient free quadratic module of rank 2 over the principal ideal domain Integer Ring
+            Inner product matrix:
+            [ 1  0]
+            [ 0 -1]
             sage: M._magma_init_(magma)                         # optional - magma
             'RSpace(_sage_[...],2,_sage_ref...)'
-            sage: magma(M)                                      # optional - magma
+            sage: m = magma(M); m                               # optional - magma
             Full RSpace of degree 2 over Integer Ring
             Inner Product Matrix:
             [ 1  0]
             [ 0 -1]
+            sage: m.Type()                                      # optional - magma
+            ModTupRng
+            sage: m._sage_()                                    # optional - magma
+            Ambient free quadratic module of rank 2 over the principal ideal domain Integer Ring
+            Inner product matrix:
+            [ 1  0]
+            [ 0 -1]
+            sage: m._sage_() is M                               # optional - magma
+            True
+
+            Now over a field:
+
+            sage: N = FreeModule(QQ,2,inner_product_matrix=A); N
+            Ambient quadratic space of dimension 2 over Rational Field
+            Inner product matrix:
+            [ 1  0]
+            [ 0 -1]
+            sage: n = magma(N); n                                      # optional - magma
+            Full Vector space of degree 2 over Rational Field
+            Inner Product Matrix:
+            [ 1  0]
+            [ 0 -1]
+            sage: n.Type()                                             # optional - magma
+            ModTupFld
+            sage: n._sage_()                                           # optional - magma
+            Ambient quadratic space of dimension 2 over Rational Field
+            Inner product matrix:
+            [ 1  0]
+            [ 0 -1]
+            sage: n._sage_() is N                                      # optional - magma
+            True
+
+            How about some inexact fields:
+            sage: v = vector(RR, [1, pi, 5/6])
+            sage: F = v.parent()
+            sage: M = magma(F); M # optional - magma
+            Full Vector space of degree 3 over Real field of precision 15
+            sage: M.Type() # optional - magma
+            ModTupFld
+            sage: m = M._sage_(); m # optional - magma
+            Vector space of dimension 3 over Real Field with 49 bits of precision
+            sage: m is F # optional - magma
+            False
+
+            For interval fields, we can convert to Magma but there is no
+            interval field in Magma so we cannot convert back:
+
+            sage: v = vector(RealIntervalField(100), [1, pi, 0.125])
+            sage: F = v.parent()
+            sage: M = magma(v.parent()); M # optional - magma
+            Full Vector space of degree 3 over Real field of precision 30
+            sage: M.Type() # optional - magma
+            ModTupFld
+            sage: m = M._sage_(); m # optional - magma
+            Vector space of dimension 3 over Real Field with 99 bits of precision
+            sage: m is F # optional - magma
+            False
         """
         K = magma(self.base_ring())
         if not self._inner_product_is_dot_product():
@@ -1579,7 +1640,7 @@
             return "RSpace(%s,%s)"%(K.name(), self.__rank)
 
     def _macaulay2_(self, macaulay2=None):
-        """
+        r"""
         EXAMPLES:
             sage: R = QQ^2
             sage: macaulay2(R) # optional
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/modules/free_module_element.pyx
--- a/sage/modules/free_module_element.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/modules/free_module_element.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -290,6 +290,49 @@
         self._parent = parent
         self._degree = parent.degree()
         self._is_mutable = 1
+
+    def _magma_init_(self, magma):
+        r"""
+        Convert self to Magma.
+
+        EXAMPLES:
+            sage: F = FreeModule(ZZ, 2, inner_product_matrix=matrix(ZZ, 2, 2, [1, 0, 0, -1]))
+            sage: v = F([1, 2])
+            sage: M = magma(v); M # optional - magma
+            (1 2)
+            sage: M.Type() # optional - magma
+            ModTupRngElt
+            sage: M.Parent() # optional - magma
+            Full RSpace of degree 2 over Integer Ring
+            Inner Product Matrix:
+            [ 1  0]
+            [ 0 -1]
+            sage: M._sage_() # optional - magma
+            (1, 2)
+            sage: M._sage_() == v # optional - magma
+            True
+            sage: M._sage_().parent() is v.parent() # optional - magma
+            True
+
+            sage: v = vector(QQ, [1, 2, 5/6])
+            sage: M = magma(v); M # optional - magma
+            (  1   2 5/6)
+            sage: M.Type() # optional - magma
+            ModTupFldElt
+            sage: M.Parent() # optional - magma
+            Full Vector space of degree 3 over Rational Field
+            sage: M._sage_() # optional - magma
+            (1, 2, 5/6)
+            sage: M._sage_() == v # optional - magma
+            True
+            sage: M._sage_().parent() is v.parent() # optional - magma
+            True
+        """
+        # Get a reference to Magma version of parent.   
+        R = magma(self.parent())
+        # Get list of coefficients.
+        v = ','.join([a._magma_init_(magma) for a in self.list()])
+        return '%s![%s]' % (R.name(), v)
 
     def __hash__(self):
         if self._is_mutable:
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/complex_double.pyx
--- a/sage/rings/complex_double.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/complex_double.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -354,6 +354,20 @@
         elif CC.has_coerce_map_from(S):
             return CCtoCDF(CC, self) * CC.coerce_map_from(S)
 
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: magma(CDF) # optional - magma
+            Complex field of precision 15
+            sage: floor(RR(log(2**53, 10)))
+            15
+            sage: magma(CDF)._sage_() # optional - magma
+            Complex Field with 49 bits of precision
+        """
+        return "ComplexField(%s : Bits := true)" % self.prec()
+
     def prec(self):
         """
         Return the precision of this complex double field (to be more
@@ -638,6 +652,18 @@
             return self._complex.dat[n]
         raise IndexError, "index n must be 0 or 1"
 
+    def _magma_init_(self, magma):
+        r"""
+        EXAMPLES:
+            sage: magma(CDF(1.2, 0.3)) # optional - magma
+            1.20000000000000 + 0.300000000000000*$.1
+            sage: s = magma(CDF(1.2, 0.3))._sage_(); s # optional - magma
+            1.2000000000000 + 0.30000000000000*I
+            sage: s.parent() # optinonal - magma
+            Complex Field with 49 bits of precision
+        """
+        return "%s![%s, %s]" % (self.parent()._magma_init_(magma), self.real(), self.imag())
+
     def prec(self):
         """
         Returns the precision of this number (to be more similar to
@@ -647,7 +673,6 @@
             sage: CDF(0).prec()
             53
         """
-
         return 53
 
     #######################################################################
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/complex_field.py
--- a/sage/rings/complex_field.py	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/complex_field.py	Thu Dec 04 18:17:43 2008 -0800
@@ -168,6 +168,24 @@
 
     def prec(self):
         return self._prec
+
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: magma(ComplexField(200)) # optional - magma
+            Complex field of precision 60
+            sage: 10^60 < 2^200 < 10^61
+            True
+            sage: s = magma(ComplexField(200))._sage_(); s # optional - magma
+            Complex Field with 199 bits of precision
+            sage: 2^199 < 10^60 < 2^200
+            True
+            sage: s is ComplexField(199) # optional - magma
+            True
+        """
+        return "ComplexField(%s : Bits := true)" % self.prec()
 
     precision = prec
 
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/complex_interval.pyx
--- a/sage/rings/complex_interval.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/complex_interval.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -431,6 +431,20 @@
             return sage.rings.ring_element.RingElement.__pow__(self, right)
         return (self.log() * right).exp()
     
+    def _magma_init_(self, magma):
+        r"""
+        EXAMPLES:
+            sage: t = CIF((1, 1.1), 2.5); t
+            1.1? + 2.5000000000000000?*I
+            sage: magma(t) # optional - magma
+            1.05000000000000 + 2.50000000000000*$.1
+            sage: t = ComplexIntervalField(100)((1, 4/3), 2.5); t
+            2.? + 2.5000000000000000000000000000000?*I
+            sage: magma(t) # optional - magma
+            1.16666666666666666666666666670 + 2.50000000000000000000000000000*$.1
+        """
+        return "%s![%s, %s]" % (self.parent()._magma_init_(magma), self.center().real(), self.center().imag())
+
     def prec(self):
         """
         Return precision of this complex number. 
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/complex_interval_field.py
--- a/sage/rings/complex_interval_field.py	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/complex_interval_field.py	Thu Dec 04 18:17:43 2008 -0800
@@ -146,6 +146,18 @@
 
     def prec(self):
         return self._prec
+
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: magma(ComplexIntervalField(100)) # optional - magma
+            Complex field of precision 30
+            sage: floor(RR(log(2**100, 10)))
+            30
+        """
+        return "ComplexField(%s : Bits := true)" % self.prec()
 
     precision = prec
 
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/complex_number.pyx
--- a/sage/rings/complex_number.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/complex_number.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -511,6 +511,17 @@
         except AttributeError:
             raise TypeError
 
+    def _magma_init_(self, magma):
+        r"""
+        EXAMPLES:
+            sage: magma(CC([1, 2])) # optional - magma
+            1.00000000000000 + 2.00000000000000*$.1
+            sage: v = magma(CC([1, 2]))._sage_(); v # optional - magma
+            1.0000000000000 + 2.0000000000000*I
+            sage: v.parent() # optional - magma
+            Complex Field with 49 bits of precision
+        """
+        return "%s![%s, %s]" % (self.parent()._magma_init_(magma), self.real(), self.imag())
 
     def __nonzero__(self):
         """
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/real_double.pyx
--- a/sage/rings/real_double.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/real_double.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -227,10 +227,32 @@
         if connecting is not None:
             return ToRDF(RR) * connecting
 
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            Magma handles precision in decimal digits, so we lose a bit:
+
+            sage: 10^15 < 2^53 < 10^16
+            True
+            sage: magma(RDF) # optional - magma
+            Real field of precision 15
+
+            When we convert back from Magma, we convert to a generic real
+            field that happens to have (close to) 53 bits:
+
+            2^49 < 10^15 < 2^50
+            sage: magma(RDF)._sage_() # optional - magma
+            Real Field with 49 bits of precision
+        """
+        return "RealField(%s : Bits := true)" % self.prec()
+
     def prec(self):
         """
-        Return the precision of this real double field (to be more
-        similar to RealField).  Always returns 53.
+        Return the precision of this real double field in bits.
+
+        Always returns 53.
 
         EXAMPLES:
             sage: RDF.prec()
@@ -423,6 +445,18 @@
         """
         self._value = float(x)
 
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: RDF(10.5)
+            10.5
+            sage: magma(RDF(10.5)) # optional - magma
+            10.5000000000000
+        """
+        return "%s!%s" % (self.parent()._magma_init_(magma), self)
+
     def __reduce__(self):
         """
         EXAMPLES:
@@ -440,8 +474,9 @@
 
     def prec(self):
         """
-        Returns the precision of this number (to be more similar to
-        RealNumber).  Always returns 53.
+        Returns the precision of this number in bits.
+
+        Always returns 53.
 
         EXAMPLES:
             sage: RDF(0).prec()
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/real_mpfi.pyx
--- a/sage/rings/real_mpfi.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/real_mpfi.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -590,7 +590,19 @@
 
     def prec(self):
         return self.__prec
-    
+
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: magma(RealIntervalField(80)) # optional - magma
+            Real field of precision 24
+            sage: floor(RR(log(2**80, 10)))
+            24
+        """
+        return "RealField(%s : Bits := true)" % self.prec()
+
     def pi(self):
         """
         Returns pi to the precision of this field.
@@ -875,6 +887,18 @@
 
     def _latex_(self):
         return str(self)
+
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: t = RIF(10, 10.5); t
+            11.?
+            sage: magma(t) # optional - magma
+            10.2500000000000
+        """
+        return "%s!%s" % (self.parent()._magma_init_(magma), self.center())
 
     def _interface_init_(self):
         """
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/real_mpfr.pyx
--- a/sage/rings/real_mpfr.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/real_mpfr.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -536,6 +536,22 @@
 
     def prec(self):
         return self.__prec
+
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: magma(RealField(70)) # optional - magma
+            Real field of precision 21
+            sage: 10^21 < 2^70 < 10^22
+            True
+            sage: s = magma(RealField(70))._sage_(); s # optional - magma
+            Real Field with 69 bits of precision
+            sage: 2^69 < 10^21 < 2^70
+            True
+        """
+        return "RealField(%s : Bits := true)" % self.prec()
     
     # int mpfr_const_pi (mpfr_t rop, mp_rnd_t rnd)
     def pi(self):
@@ -795,6 +811,18 @@
         self.init = 1
         if x is None: return
         self._set(x, base)
+
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: magma(RR(10.5)) # optional - magma
+            10.5000000000000
+            sage: RealField(200)(10.5) # optional - magma
+            10.500000000000000000000000000000000000000000000000000000000
+        """
+        return "%s!%s" % (self.parent()._magma_init_(magma), self)
 
     cdef _set(self, x, int base):
         # This should not be called except when the number is being created.
diff -r f0a6a5c413e3 -r 8bfa940112f9 sage/rings/real_rqdf.pyx
--- a/sage/rings/real_rqdf.pyx	Thu Dec 04 17:54:54 2008 -0800
+++ b/sage/rings/real_rqdf.pyx	Thu Dec 04 18:17:43 2008 -0800
@@ -211,6 +211,29 @@
         elif hasattr(x,'_real_rqdf_'):
             return x._real_rqdf_(self)
         return QuadDoubleElement(x)
+
+    def _magma_init_(self, magma):
+        r"""
+        Return a string representation of self in the Magma language.
+
+        EXAMPLES:
+            sage: magma(RQDF) # optional
+            Real field of precision 63
+            sage: floor(RR(log(2**212, 10)))
+            63
+        """
+        return "RealField(%s : Bits := true)" % self.prec()
+
+    def prec(self):
+        """
+        Return the precision of this real quad double field (to be more
+        similar to RealField).  Always returns 212.
+
+        EXAMPLES:
+            sage: RQDF.prec()
+            212
+        """
+        return 212
 
     def gen(self, i=0):
         """
@@ -541,6 +564,16 @@
         """
         return (self.initptr.x[0], self.initptr.x[1],
                 self.initptr.x[2], self.initptr.x[3])
+
+    def prec(self):
+        """
+        Return the precision of this real quad double element.  Always returns 212.
+
+        EXAMPLES:
+            sage: RQDF(0).prec()
+            212
+        """
+        return 212
 
     def real(self):
         """
