2

I have a data structure that looks like a list values and I am trying to compute the (x,y) 2d hermite functions from them using numpy. I'm trying to use as many numpy arrays as possible due to the performance boost you get from getting to Fortran as quickly as possible (I'm expecting x to be in practice many thousands of 3-arrays). Specifically, my code looks like this:

x = np.array([[1., 2., 3.], [4., 5., 6.]])
coefs = np.array([[[1., 0.],[0., 1.]], [[0., 1.], [1., 0.]]])

z = np.array([0., 0.])

z[:] = hermval2d(x[:,0], x[:,1], coefs[:])

This returns an error about the shape of hermval2d, which according to just running the hermval2d function instead of assigning it:

In [XX]: hermval2d(x[:,0], x[:,1], coefs[:])
Out[XX]:
array([[  9.,  81.],
       [  6.,  18.]])

I would expect the hermval2d to be a scalar for every x, y, and coefficient matrix, which is what you would expect from the documentation. So what am I missing here? What's the score?

webb
  • 450
  • 1
  • 3
  • 12

1 Answers1

3

It's right there in the docs :)

hermval2d(x, y, c)

[...]

The shape of the result will be c.shape[2:] + x.shape

In your case this seems to return the Hermite values for x and y evaluated for each ith 2d array in c[:,:,i].

  • Okay so let me revise my question: what exactly is it computing if it's doing that? Is it making a grid out of the x- and y- values? I guess the question is actually: what is hermval2d returning? – webb Aug 11 '14 at 20:36
  • @webb, see edit. The [source code](http://github.com/numpy/numpy/blob/master/numpy/polynomial/hermite.py#L947) is interesting. Apparently the thing with a grid of x- and y-values is what happens inside the `hermgrid2d` function. –  Aug 11 '14 at 20:55
  • my conclusion from that source code is I should just compute my own series using hermval directly to have the best control over the output. – webb Aug 11 '14 at 21:07
  • With your example, hermval2d is computing the values for the the (x,y) pairs `(1,2)` and `(4,5)` for each of the coefficient arrays `coefs[...,0]` and `coefs[...,1]` and the result is a 2x2 array. This follows from `x[:,0] = (1,4)` and `x[:,1] = (2,5)` and you are computing a vector valued function because of the 3d coefficient array. It sounds like you want a 2d coefficient array here. – Charles Harris Aug 11 '14 at 23:06