Following up on the recent thread "coercion error: calling functions 
with vector inputs", I am trying to get SR(vector([1,2,])) to work.

In symbolic/ring.pyx, we have in the _element_constructor_ function, 
line 269 or so, the default case:

         elif isinstance(x, RingElement):
             GEx_construct_pyobject(exp, x)

so that explains why SR(matrix([[1,2]])) works.  However, 
isinstance(vector([1,2]), RingElement) is False, so a vector falls 
through to the type error.  What is the reason for insisting that the 
thing must be a RingElement (as opposed to, say, an Element)?

I've pasted below a patch to make SR(vector([1,2])) work.  However, I'm 
not sure that this is the right thing to do, compared to maybe making 
vectors really be pynac objects, rather than just wrapped python objects.

However, with this patch, this works:

sage: var('t')
t
sage: r=vector([cos(t),t^2])
sage: s=vector([sin(t),t])
sage: f(x,y)=x+2*y
sage: f(SR(r),SR(s))
(2*sin(t) + cos(t), t^2 + 2*t)
sage: f(r,s)
Coercion error



Thanks,

Jason

-- 
Jason Grout


diff -r 856e914baa43 sage/symbolic/ring.pyx
--- a/sage/symbolic/ring.pyx    Tue Oct 27 19:53:07 2009 -0500
+++ b/sage/symbolic/ring.pyx    Tue Oct 27 21:08:55 2009 -0500
@@ -26,6 +26,7 @@
  from sage.symbolic.expression import is_Expression

  from sage.structure.element cimport RingElement, Element
+from sage.structure.element import is_Vector
  from sage.structure.parent_base import ParentWithBase
  from sage.rings.ring cimport CommutativeRing
  from sage.categories.morphism cimport Morphism
@@ -193,6 +194,13 @@
              sage: SR(complex(2,-3))
              2.00000000000000 - 3.00000000000000*I

+        A vector::
+
+            sage: a=SR(vector([1,2]))
+            sage: t=var('t')
+            sage: b=SR(vector([t,t^2]))
+            sage: a+2*b
+
          TESTS::

              sage: SR._coerce_(int(5))
@@ -266,7 +274,7 @@
              return new_Expression_from_GEx(self, g_mInfinity)
          elif x is unsigned_infinity:
              return new_Expression_from_GEx(self, g_UnsignedInfinity)
-        elif isinstance(x, RingElement):
+        elif isinstance(x, RingElement) or is_Vector(x):
              GEx_construct_pyobject(exp, x)
          else:
              raise TypeError


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to