Re: [sage-devel] indexed symbolic variables
Hi Florent, On Thu, 17 Nov 2011 14:44:35 +0100 Florent Hivert wrote: > On Wed, Nov 16, 2011 at 03:48:10PM +0100, Burcin Erocal wrote: > > On Wed, 16 Nov 2011 15:10:54 +0100 > > Florent Hivert wrote: > > > > > > Pynac supports indexed expressions already. It just isn't > > > > wrapped in Sage yet. Again, there is an experimental patch > > > > available: > > > > > > > > http://sage.math.washington.edu/home/burcin/indexed_expression-20110727.patch > > > > > > > > Since you can wrap any hashable Python object in a symbolic > > > > expression with SR._force_pyobject(), this could replace your > > > > IndexedVarExpr class. > > > > > > > > I would appreciate any help with cleaning up these patches, > > > > adding doctests etc. so they can be merged. :) > > > > > > I'll try to cleanup this one, though I don't know anything about > > > Pynac's internal... Is there a lot more work to do than adding > > > doctests ? > > > > It should be only doctests, but you never know what comes up. :) > > > > I haven't looked at the patch in detail. I don't recall if any low > > level modification is needed. I'll be on IRC if you run into > > anything. > > I'm at a conference so I don't have that much time working on it right > now. I'll probably have more time next week. > > My two first attempts weren't successful: first of all, it seems that > you cannot index variables by any Python objects, only integers or > sequences of thereof. I typically want to put permutations, trees or > group elements in the index. Is that a limitation of Pynac or of your > wrapper ? This was a limitation of the wrapper. It tries to coerce the argument to SR. If Pynac doesn't need to do any arithmetic with these objects, one can skip the coercion rules with the SR._force_pyobject() function. I updated the patch to allow arbitrary (hashable) Python objects and omit the dimension argument. > Second, I got some Segfault so there is probably more work that only > doctest. I have to investigate more to get a simple reproducible > behavior. Can you check that I have the latest version of your patch: > it is called: indexed_expression-20110727.patch That was the latest version. :) Here is a new one: http://sage.math.washington.edu/home/burcin/indexed_expression_2018.patch Note that I never actually used this. It is possible that there are problems. I can try to fix these when you have a simple example. It's possible that your segfault is caused by #9880. We are trying to finish that ticket off for the next release. Unfortunately, I am really short on time these days. Cheers, Burcin -- 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
Re: [sage-devel] indexed symbolic variables
Hi Burcin, On Wed, Nov 16, 2011 at 03:48:10PM +0100, Burcin Erocal wrote: > On Wed, 16 Nov 2011 15:10:54 +0100 > Florent Hivert wrote: > > > > Pynac supports indexed expressions already. It just isn't wrapped in > > > Sage yet. Again, there is an experimental patch available: > > > > > > http://sage.math.washington.edu/home/burcin/indexed_expression-20110727.patch > > > > > > Since you can wrap any hashable Python object in a symbolic > > > expression with SR._force_pyobject(), this could replace your > > > IndexedVarExpr class. > > > > > > I would appreciate any help with cleaning up these patches, adding > > > doctests etc. so they can be merged. :) > > > > I'll try to cleanup this one, though I don't know anything about > > Pynac's internal... Is there a lot more work to do than adding > > doctests ? > > It should be only doctests, but you never know what comes up. :) > > I haven't looked at the patch in detail. I don't recall if any low > level modification is needed. I'll be on IRC if you run into anything. I'm at a conference so I don't have that much time working on it right now. I'll probably have more time next week. My two first attempts weren't successful: first of all, it seems that you cannot index variables by any Python objects, only integers or sequences of thereof. I typically want to put permutations, trees or group elements in the index. Is that a limitation of Pynac or of your wrapper ? Second, I got some Segfault so there is probably more work that only doctest. I have to investigate more to get a simple reproducible behavior. Can you check that I have the latest version of your patch: it is called: indexed_expression-20110727.patch Beginning by # HG changeset patch # User Burcin Erocal # Date 1278108459 -7200 # Node ID c0f30b1b122da619e859c8f99492ab89938de153 # Parent 57e08f919b352054d47c864bdcdaab3bc33692f2 With md5sum: edba1613640319fad746d8cf304f5f51 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
Re: [sage-devel] indexed symbolic variables
On Wed, 16 Nov 2011 15:10:54 +0100 Florent Hivert wrote: > > Pynac supports indexed expressions already. It just isn't wrapped in > > Sage yet. Again, there is an experimental patch available: > > > > http://sage.math.washington.edu/home/burcin/indexed_expression-20110727.patch > > > > Since you can wrap any hashable Python object in a symbolic > > expression with SR._force_pyobject(), this could replace your > > IndexedVarExpr class. > > > > I would appreciate any help with cleaning up these patches, adding > > doctests etc. so they can be merged. :) > > I'll try to cleanup this one, though I don't know anything about > Pynac's internal... Is there a lot more work to do than adding > doctests ? It should be only doctests, but you never know what comes up. :) I haven't looked at the patch in detail. I don't recall if any low level modification is needed. I'll be on IRC if you run into anything. Thanks a lot for looking into this. > By the way my buggy implementation revealed something that looks > strange to me: > > sage: Z = IndexedVar("Z") > sage: ex = (Z[1] + Z[2])/(Z[3] + Z[4]) > sage: ex > (Z[1] + Z[2])/(Z[3] + Z[4]) > sage: ex.numerator() > ERROR: An unexpected error occurred while tokenizing input > The following traceback may be corrupted or invalid > The error message is: ('EOF in multi-line statement', (647, 0)) > > --- > TypeError Traceback (most recent call > last) [...] > TypeError: unable to make sense of Maxima expression 'Z[2]+Z[1]' in > Sage > > Do we have to call Maxima to compute the numerator of a rational > fraction ? Is seems to me that Pynac/Ginac should be able to do it by > themselves. Unfortunately, we still call maxima for a lot of trivial operations. For example, pynac can easily handle the functions Expression.{coeff,poly}(). Feel free to open tickets for these issues. Cheers, Burcin -- 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
Re: [sage-devel] indexed symbolic variables
> Pynac supports indexed expressions already. It just isn't wrapped in > Sage yet. Again, there is an experimental patch available: > > http://sage.math.washington.edu/home/burcin/indexed_expression-20110727.patch > > Since you can wrap any hashable Python object in a symbolic expression > with SR._force_pyobject(), this could replace your IndexedVarExpr > class. > > I would appreciate any help with cleaning up these patches, adding > doctests etc. so they can be merged. :) I'll try to cleanup this one, though I don't know anything about Pynac's internal... Is there a lot more work to do than adding doctests ? By the way my buggy implementation revealed something that looks strange to me: sage: Z = IndexedVar("Z") sage: ex = (Z[1] + Z[2])/(Z[3] + Z[4]) sage: ex (Z[1] + Z[2])/(Z[3] + Z[4]) sage: ex.numerator() ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (647, 0)) --- TypeError Traceback (most recent call last) [...] TypeError: unable to make sense of Maxima expression 'Z[2]+Z[1]' in Sage Do we have to call Maxima to compute the numerator of a rational fraction ? Is seems to me that Pynac/Ginac should be able to do it by themselves. 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
Re: [sage-devel] indexed symbolic variables
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) However, getting back meter from u give not a unit but a plain Expression: sage: type(u.op[0]) 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_() --- AttributeErrorTraceback (most recent call last) /home/florent/recherche/Operades/Chapoton/ in () /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) > (, '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
Re: [sage-devel] indexed symbolic variables
Hi Florent, On Tue, 15 Nov 2011 17:29:38 +0100 Florent Hivert wrote: > 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) > (, 'sage.symbolic.expression.Expression'>) The Expression class hard codes the type of the resulting object after arithmetic, like many other element classes in Sage. This is done for speed of course. There have been many requests of this kind lately, I'm starting to think it is worth the speed penalty. See the discussion here for a patch that should fix the error and some timings: http://groups.google.com/group/sage-support/t/f749ba3cd079c6f4 We would also need this for #9556: http://trac.sagemath.org/sage_trac/ticket/9556 > 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. Pynac supports indexed expressions already. It just isn't wrapped in Sage yet. Again, there is an experimental patch available: http://sage.math.washington.edu/home/burcin/indexed_expression-20110727.patch Since you can wrap any hashable Python object in a symbolic expression with SR._force_pyobject(), this could replace your IndexedVarExpr class. I would appreciate any help with cleaning up these patches, adding doctests etc. so they can be merged. :) Cheers, Burcin -- 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
[sage-devel] indexed symbolic variables
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) (, ) 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