Hello,

I implemented the idea that Bill Hart suggested this morning.  In the
next version of SAGE the following will work:

sage: y = 5
sage: y
5
sage: QQ['x,y,z']
Polynomial Ring in x, y, z over Rational Field
sage: f = x^3 + y^2 - z
sage: f^2
z^2 - 2*y^2*z + y^4 - 2*x^3*z + 2*x^3*y^2 + x^6
sage: type(y)
<class 'sage.rings.multi_polynomial_element.MPolynomial_polydict'>
sage: parent(y)
Polynomial Ring in x, y, z over Rational Field

Notice how x,y,z are automatically injected into the namespace of the  
interpreter.
This will automatically work for any ring when you create the polynomial  
ring
over it using the R['...'] notation.

Moreover, if you use R['...'] notation anywhere in library code it doesn't  
affect
the interpreter's variables at all -- it only affects the scope where  
R['...']
is used.

This was surprisingly easy to implement, and doesn't involve the SAGE  
preparser
at all.  Python is an amazing language!   Here's the udiff (I just added
a few lines to sage/rings/ring.pyx):

diff -r 16127b77db9a sage/rings/ring.pyx
--- a/sage/rings/ring.pyx       Fri Oct 27 13:32:36 2006 -0700
+++ b/sage/rings/ring.pyx       Fri Oct 27 19:56:57 2006 -0500
@@ -102,11 +102,18 @@ cdef class Ring(sage.structure.gens.Gene
              v = x.split(',')

          if len(v) > 1:
-            return P(self, len(v), names=v)
-        else:
-            return P(self, x)
-
-
+            R = P(self, len(v), names=v)
+        else:
+            R = P(self, x)
+
+        # Inject the newly defined variables names into the global scope.
+        # TODO: could use Python/C api for fast iteration through list  
below.
+        G = globals()
+        cdef int i
+        for i from 0 <= i < len(v):
+            G[v[i]] = R.gen(i)
+
+        return R


--------------------


> Since y is not defined as a variable in the trace above but only as a
> text string, there is no way to set y to be a variable without making
> adjustments to the preprocessor.

Actually, it turns out Python is very powerful, and one can in some cases
change variables in the calling scope of a function defined in a Python
extension module.  The preprocessor and IPython are irrelevant to this.

William



--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to