I just had a hard to find bug in my program. poly1d treats numpy scalars differently than python numbers when left or right multiplication is used.
Essentially, if the first term is the numpy scalar, multiplied by a polynomial, then the result is an np.array. If the order is reversed, then the result is an instance of np.poly1d. The return types are also the same for numpy arrays, which is at least understandable, although a warning would be good) When using plain (python) numbers, then both left and right multiplication of the number with the polynomial returns a polynomial. Is this a bug or a feature? I didn't see it mentioned in the docs. My problem for debugging was that in the examples I used python numbers while the program used numpy scalars, and it took me a while to figure out that this is the source of my bugs. examples below Josef >>> polys [poly1d([1]), poly1d([-1., 0.]), poly1d([ 1., 0., -1.]), poly1d([ 1., 0., -3., 0.]), poly1d([ 1., 0., -6., 0., 3.])] np.array on left is fine >>> (polys[2]*np.array(0.5/6.0) + polys[3]*np.array(0.5/24.0)) poly1d([ 0.02083333, 0.08333333, -0.0625 , -0.08333333]) >>> (polys[2]*0.5/6.0 + polys[3]*0.5/24.0) poly1d([ 0.02083333, 0.08333333, -0.0625 , -0.08333333]) >>> (0.5/6.0*polys[2] + 0.5/24.0*polys[3]) poly1d([ 0.02083333, 0.08333333, -0.0625 , -0.08333333]) problems with np.array on left >>> (np.array(0.5/6.0)*polys[2] + np.array(0.5/24.0)*polys[3]) Traceback (most recent call last): File "<pyshell#722>", line 1, in <module> (np.array(0.5/6.0)*polys[2] + np.array(0.5/24.0)*polys[3]) ValueError: shape mismatch: objects cannot be broadcast to a single shape >>> np.array(0.5/6.0)*polys[2] array([ 0.08333333, 0. , -0.08333333]) >>> polys[2]*np.array(0.5/6.0) poly1d([ 0.08333333, 0. , -0.08333333]) >>> 0.5/6.0*polys[2] poly1d([ 0.08333333, 0. , -0.08333333]) >>> np.array(0.5/6.0) array(0.083333333333333329) same with numpy scalar >>> np.array([0.5/6.0])[0]*polys[2] array([ 0.08333333, 0. , -0.08333333]) >>> np.array([0.5/6.0])[0] 0.083333333333333329 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion