Ask Your Question

integral of piecewise function: error

asked 2017-02-10 14:06:56 +0100

stan gravatar image

updated 2017-02-13 16:47:57 +0100

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)


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

/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/symbolic/function_factory.pyc in new_f(ex, *args, **kwds)
    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)

/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/symbolic/function_factory.pyc in new_f(ex, *args, **kwds)
    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

/home/sschyman/Programs/sage-upgrade/local/lib/python2.7/site-packages/sage/symbolic/assumptions.pyc in assume(*args)
    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:

edit retag flag offensive 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?

kcrisman gravatar imagekcrisman ( 2017-02-10 16:43:52 +0100 )edit

No error in this live example either...

paulmasson gravatar imagepaulmasson ( 2017-02-11 03:13:17 +0100 )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.

stan gravatar imagestan ( 2017-02-12 23:30:33 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-02-13 15:08:28 +0100

kcrisman gravatar image

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

edit flag offensive delete link 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:

stan gravatar imagestan ( 2017-02-13 16:47:00 +0100 )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.

kcrisman gravatar imagekcrisman ( 2017-02-14 03:47:43 +0100 )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.

stan gravatar imagestan ( 2017-02-14 11:50:05 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2017-02-10 14:06:56 +0100

Seen: 927 times

Last updated: Feb 13 '17