[Numpy-discussion] Evaluate bivariate polynomials

Charles R Harris charlesr.harris@gmail....
Wed Oct 19 09:00:27 CDT 2011

On Wed, Oct 19, 2011 at 5:58 AM, Nils Wagner

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111019/e6ba53c1/attachment.html 

More information about the NumPy-Discussion mailing list