I made a piecewise polynomial object based on numpy.poly1d. It has all the usual arithmetic methods plus deriv() and integ(). Create it with a list of breakpoints and a list of poly1ds.
I am stumped by this behavior:
>>> from numpy import poly1d
>>> from piecepoly import PiecePoly as pp
>>> f = poly1d([-1,-1])
>>> g = pp([-1,1], [poly1d([-1]),poly1d([1,0]),poly1d([1])])
>>> g(-2)
-1
>>> g(-0.7)
-0.69999999999999996
>>> g(0.3)
0.29999999999999999
>>> g(4)
1
>>> h = g*f
>>> h1 = h.integ()
>>> j = f*g
>>> j1 = j.integ()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/polynomial.py", line 1216, in integ
return poly1d(polyint(self.coeffs, m=m, k=k))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/polynomial.py", line 320, in polyint
y = NX.concatenate((p.__truediv__(NX.arange(len(p), 0, -1)), [k[0]]))
TypeError: unsupported operand type(s) for /: 'instance' and 'int'
The flaw would seem to be in __rmul__, but that is simply return self.__mul__(other).
As requested in comment, here is integ (which another project tested to my satisfaction):
def integ(self, n=1):
if n == 0: return self
if n < 0: return self.deriv(-n)
if self.integral == None:
intemp = [ f.integ() for f in self.func ]
fl = intemp.pop(0)
outf = [fl]
for t, fr in zip(self.breaks, intemp):
fr += fl(t) - fr(t)
outf.append(fr)
fl = fr
self.integral = PiecePoly(self.breaks, outf)
self.integral.derivative = self
if n==1: return self.integral
return self.integral.integ(n-1)