integral of piecewise function: error

I get a weird error when trying to take an integral of a very simple piecewise function:

blah = piecewise([((0, 0.01), 0.0001), ([0.01, 0.02], 0.0002), ((0.02, 0.03), 0.0003)])
blah.integral(x, 0.01, 0.025)


gives:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-132-73d5f18533d2> in <module>()
1 blah = piecewise([((Integer(0), RealNumber('0.01')), RealNumber('0.0001')), ([RealNumber('0.01'), RealNumber('0.02')], RealNumber('0.0002')), ((RealNumber('0.02'), RealNumber('0.03')), RealNumber('0.0003')), ([RealNumber('0.03'), RealNumber('0.04')], RealNumber('0.0004'))])
----> 2 blah.integral(x, RealNumber('0.01'), RealNumber('0.025'))

400         new_args = list(ex._unpack_operands())
401         new_args.extend(args)
--> 402         return f(ex, *new_args, **kwds)
403     return new_f

/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in integral(cls, self, parameters, variable, x, a, b, definite)
793             """
794             if a != None and b != None:
--> 795                 F = self.integral(x)
796                 return F(b) - F(a)
797

400         new_args = list(ex._unpack_operands())
401         new_args.extend(args)
--> 402         return f(ex, *new_args, **kwds)
403     return new_f

/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/functions/piecewise.pyc in integral(cls, self, parameters, variable, x, a, b, definite)
828                     else:
829                         try:
--> 830                             assume(start < x)
831                         except ValueError: # Assumption is redundant
832                             pass

513         else:
514             try:
--> 515                 x.assume()
516             except KeyError:
517                 raise TypeError("assume not defined for objects of type '%s'"%type(x))

AttributeError: 'numpy.bool_' object has no attribute 'assume'


Is this a bug or did I make a mistake?

UPDATE: I uploaded an example to SMC: https://cloud.sagemath.com/projects/3...

edit retag close merge delete

I don't get this error. Posting the whole traceback may help. Can you try this in a brand-new session of Sage?

( 2017-02-10 16:43:52 +0200 )edit

No error in this live example either...

( 2017-02-11 03:13:17 +0200 )edit

Thanks for checking!! Well, if I restart the notebook and run the above code, I also don't get an error message. Only if I run the whole worksheet and execute the same code towards the end, I get the error message. Maybe there is a memory problem or I re-defined something somewhere... I will edit my above comment to include the full error message, perhaps you see something there that I don't.

( 2017-02-12 23:30:33 +0200 )edit

Sort by ยป oldest newest most voted

You must have done something to mess with the preparser earlier in your worksheet, or perhaps with x. Is there anywhere in the worksheet where x is now a Numpy object instead of a Sage variable? (Apparently a Numpy boolean object? I haven't encountered those.)

more

Thanks, your second suggestion was easily verified, but even if I define x as a symbolic variable just before the code above, the error does not change. The first one is a bit more tricky, as I can't see how this would have happened. I actually found what line causes the error, but I don't understand why. I uploaded an example to SMC here: https://cloud.sagemath.com/projects/3...

( 2017-02-13 16:47:00 +0200 )edit

Wow, this is really baffling. Somehow your using Numpy floats in the piecewise function is messing it up. Unfortunately I don't have more time to debug this right now. If you could create a minimal example where one didn't have to worry about the loading of the other stuff, that would be very useful - right now there is the whole loaded session black box which probably is superfluous to triggering the bug. I did try Numpy floats and that doesn't seem to automatically trigger it, though.

( 2017-02-14 03:47:43 +0200 )edit

Thanks, Karl! At least I know how to avoid the bug now. I updated the SMC worksheet, showing that the bug is not triggered if I preparse the numpy floats with float(). Interestingly, after doing this once, I cannot trigger the bug at all any more, even if I paste the code that originally triggered the bug into cells below. No idea how to create a minimal example here or how to debug this.

( 2017-02-14 11:50:05 +0200 )edit