On Wed, Oct 19, 2011 at 5:58 AM, Nils Wagner
<[email protected]>wrote:

> Hi all,
>
> how do I evaluate a bivariate polynomial
>
> p(x,y)=c_0 + c_1 x + c_2 y +c_3 x**2 + c_4 x*y+ c_5 y**2 +
> c_6 x**3 + c_7 x**2*y + c_8 x*y**2+c_9*y**3 + \dots
>
> in numpy ?
>
> In case of univariate polynomials I can use np.polyval.
>
> Any pointer would be appreciated.
>

Here's a version for Bernstein polynomials that you could adapt. It's
possible to fool with the 2d version to have it evaluate on the grid defined
by x,y. As is, it evaluates on the x,y pairs. The coefficient c is a
rectangular array.

def bval(x, c):
    x = np.asarray(x)
    c = np.asarray(c)
    if len(c) == 1:
        c = c*np.ones(x.shape)
    else:
        t = 1 - x
        for i in range(len(c) - 1):
            c = c[:-1]*t + c[1:]*x
    return c[0]


def bval2d(x, y, c):
    f =  bval(y, bval(x, c[...,None]))
    return f


I use Bernstein polynomials for non-linear least squares for numerical
reasons and because they tend to work better with numerical differentiation.

Chuck
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to