Hi there,

I'm trying to make sage aware of indexed variables. For the information, this
is a long standing Sage-Combinat wish. more precisely, basically I need a sage
object Y such that, for any object (say hashable) o:
 1 - the call Y[o] returns a variable which is legal in symbolic expressions.
 2 - the variable v = Y[o] has a method index such that v.index() return o
Since Expression doesn't have any __dict__ I can't use the quick hack
consisting in inserting the attribute index in it.

So I'm trying to inherits from Expression. Here is my code:

from sage.symbolic.expression import Expression
class IndexedVarExpr(Expression):
    def index(self):
        return self._index

class IndexedVar(SageObject):
    def __init__(self, name, parent = SR):
        self._name = name
        self._parent = parent

    @cached_method
    def __getitem__(self, i):
        res = IndexedVarExpr(self._parent,
                             self._parent.symbol("%s[%s]"%(self._name, i)))
        res._index = i
        return res

This seems to work as a first glance:

sage: Y = IndexedVar("Y")
sage: v = Y[1,2,3]
sage: v
Y[(1, 2, 3)]
sage: v^2+1
Y[(1, 2, 3)]^2 + 1
sage: vbis = (v^2+1).variables()[0]
sage: vbis
Y[(1, 2, 3)]
sage: vbis == v
Y[(1, 2, 3)] == Y[(1, 2, 3)]
sage: bool(vbis == v)
True

However, vbis doesn't return my exact object:

sage: type(v), type(vbis)
(<class '__main__.IndexedVarExpr'>, <type 
'sage.symbolic.expression.Expression'>)

I'd like to get some input or advice before pushing this further. Do you this
this way of working is robust ?

Cheers,

Florent

-- 
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