Hi,

I discovered that units have the same flaw. This all look good:

sage: units.length.meter^2
meter^2
sage: u = units.length.meter^2
sage: u.op[0]
meter
sage: type(units.length.meter)
<class 'sage.symbolic.units.UnitExpression'>

However, getting back meter from u give not a unit but a plain Expression:

sage: type(u.op[0])
<type 'sage.symbolic.expression.Expression'>
sage: units.length.meter._sage_doc_()
'SI base unit of length.\nDefined to be the distance light travels in vacuum
in 1/299792458 of a second.'

As a consequence:

sage: u.op[0]._sage_doc_()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/florent/recherche/Operades/Chapoton/<ipython console> in <module>()

/home/florent/src/Sage/sage/local/lib/python2.6/site-packages/sage/structure/element.so
 in sage.structure.element.Element.__getattr__ (sage/structure/element.c:2840)()

AttributeError: 'sage.symbolic.expression.Expression' object has no attribute
'_sage_doc_'

I would be happy to fix it and to finish my IndexedVariable code if someone
explain me how to do so. It is reasonable to mangle the name passed to
Pynac/Gynac (say "meter#units") and to rewrite new_Expression_from_GEx to care
about the unmangling ? Is it possible to pass tagged variable to Gynac/Pynac ?

Cheers,

Florent


On Tue, Nov 15, 2011 at 05:29:38PM +0100, Florent Hivert wrote:
>       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

-- 
Florent Hivert
  ---
   Il y a trois sortes de gens dans le monde : ceux qui savent compter et
ceux qui ne savent pas.
   There are three kinds of people in the world: those who can count,
and those who cannot.
  ---
Professeur, LRI, Univ. Paris Sud 11, CNRS.
Bureau 38, Laboratoire de Recherche en Informatique (UMR CNRS 8623)
Bâtiment 650, Université Paris Sud 11, 91405 ORSAY CEDEX
Tél: 01-69-15-65-99
http://www.lri.fr/~hivert

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