Possibly, this is not a bug in Ring. For the class of Piecewise functions, __mul__ is implemented in a way that allows you to multiply two elements in that class. If you try to multiply a piecewise times a rational (in that order) then it detects this and creats on the fly a piecewise function equal to the rational on the range of the first function, then multiplies them. However, if you multiply a rational c times a piecewise f (in that order), then Python applies __mul__ from QQ and tries to carry out the multiplication. When writing piecewise.py, could not figure out how to implement that.
sage: x = PolynomialRing(QQ, 'x').gen() sage: f = Piecewise([[(0,1),1*x^0]]) sage: r = f*2 sage: P1 = f.plot() sage: P2 = r.plot() sage: show(P1+P2) sage: two = Piecewise([[(0,1),2*x^0]]) sage: f*two Piecewise defined function with 1 parts, [[(0, 1), 2]] sage: two*f Piecewise defined function with 1 parts, [[(0, 1), 2]] sage: f*two == f*2 True Worst, if a piecewise is defined using lambda functions then either 2*f nor f*2 works! sage: one = lambda x:1 sage: f = Piecewise([[(0,1),one]]) sage: f*2 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /home/wdj/sagefiles/sage-3.0.4.rc0/<ipython console> in <module>() /home/wdj/sagefiles/sage-3.0.4.rc0/local/lib/python2.5/site-packages/sage/functions/piecewise.py in __mul__(self, other) 1403 for j in range(N-1): 1404 x0 = endpts[j+1] -> 1405 fcn.append([(endpts[j],endpts[j+1]),R(other)*self.which_function(x0)]) 1406 return Piecewise(fcn) 1407 self_endpts = self.end_points() ## we assume these start /home/wdj/sagefiles/sage-3.0.4.rc0/element.pyx in sage.structure.element.RingElement.__mul__ (sage/structure/element.c:8814)() /home/wdj/sagefiles/sage-3.0.4.rc0/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op_c (sage/structure/coerce.c:5582)() TypeError: unsupported operand parent(s) for '*': 'Univariate Polynomial Ring in x over Rational Field' and '<type 'function'>' ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ On Fri, Jul 11, 2008 at 9:55 PM, cgb <[EMAIL PROTECTED]> wrote: > > Hi, I'm brand new to sage, just installed it on my mac, and was going > through some of the examples and found a bug when I tried to multiply > a piecewise function by a constant: > > sage: x = PolynomialRing(QQ, 'x').gen() > sage: f = Piecewise([[(0,1),1*x^0]]) > sage: r = f*2 > sage: r = 2*f > --------------------------------------------------------------------------- > TypeError Traceback (most recent call > last) > > /Users/boncelet/<ipython console> in <module>() > > /Users/boncelet/element.pyx in > sage.structure.element.RingElement.__mul__ (sage/structure/element.c: > 8545)() > > /Users/boncelet/coerce.pyx in > sage.structure.coerce.CoercionModel_cache_maps.bin_op_c (sage/ > structure/coerce.c:5338)() > > TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and > '<type 'instance'>' > -------- > > > Multiplication should be communicative. I suspect the .__mul__ method > is set incorrectly. (If memory serves, python can have separate > methods for left and right multiplication.) > > ---CGB > > > > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---