# Problems with symbolic vector arithmetic

36 views

### Joris Vankerschaver

Jun 25, 2011, 3:56:36 PM6/25/11
to sage-devel
Dear all,

I'm playing around with #11335 which was included in Sage
4.7.1.alpha3. In this patch, I added a symbolic vector class, with
the aim of providing simplification methods that act elementwise on
symbolic vectors. The symbolic vector class derives from
sage.modules.free_module_element.FreeModuleElement_generic_dense and
so far only provides methods for simplification, so it's pretty
simple.

However, it looks like this simple design is not sufficient to ensure
that the result of simple arithmetic operators stays in the same
vector space:

sage: v = vector(SR, [1])
sage: w = vector(SR, [x])
sage: type(v)
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
sage: type(w)
<class 'sage.modules.vector_symbolic_dense.Vector_symbolic_dense'>
sage: type(v + w)
<type
'sage.modules.free_module_element.FreeModuleElement_generic_dense'>

I have no idea what the matter is, though. The coercion model reports
that arithmetic should be performed directly:

sage: cm = sage.structure.element.get_coercion_model()
sage: cm.explain(v, w, operator.add)
Identical parents, arithmetic performed immediately.
Result lives in Vector space of dimension 1 over Symbolic Ring
Vector space of dimension 1 over Symbolic Ring

Any idea where things went wrong?

Thanks!
Joris

### David Roe

Jun 26, 2011, 3:15:06 PM6/26/11
The implementation in sage/modules/vector_symbolic_dense just inherits from FreeModuleElement_generic_dense, without overriding any methods like _add_ or _new_c.  In particular, the arithmetic operations in FreeModuleElement_generic_dense hard-code the class, so that arithmetic always creates a new FreeModuleElement_generic_dense (as you're seeing).

The simplest solution may be to change

cdef _new_c(self, object v):
# Create a new dense free module element with minimal overhead and
# no type checking.
cdef FreeModuleElement_generic_dense x
x = PY_NEW(FreeModuleElement_generic_dense)

in FreeModuleElement_generic_dense to

cdef _new_c(self, object v):
# Create a new dense free module element with minimal overhead and
# no type checking.
cdef FreeModuleElement_generic_dense x
x = <FreeModuleElement_generic_dense>PY_NEW(<object>PY_TYPE(self))

David

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

### Joris Vankerschaver

Jun 27, 2011, 4:26:59 PM6/27/11
to sage-devel
Hi David,

Thanks for pointing out the problem and at the same time providing a
very elegant solution!

I've gone ahead and made this into #11549, and I've taken the liberty
of cc-ing you and Robert Bradshaw on this (since he reviewed the
original vector class).

All the best,
Joris