pwtools.num.poly_powers

pwtools.num.poly_powers(ndim, deg)[source]

Powers for building a n-dim polynomial and columns of the n-dim Vandermonde matrix.

Parameters:
  • ndim (number of dimensions of the poly (e.g. 2 for f(x1,x2)))

  • deg (degree of the poly)

Returns:

powers

Return type:

2d array ((deg+1)**ndim, ndim)

Examples

For one dim, we have data points (x_i,y_i) and the to-be-fitted poly of order k is:

f(x) = a0*x^0 + a1*x^1 + a2*x^2 + ... + ak*x^k

The Vandermonde matrix A consists of all powers of x (cols) for all data points (rows) and each row has the form of the poly:

[[x_0^0 x_0^1 ... x_0^k],
 [x_1^0 x_1^1 ... x_1^k],
 ...
 [x_n^0 x_n^1 ... x_n^k]]

To fit, we solve A . a = y, where a = [a0,…,ak].

The returned array powers has k rows, where each row holds the powers for one term in the poly. For ndim=1 and poly order k, we have:

[[0],
 [1],
 ...
 [k]]

and:

[0] -> x^0
[1] -> x^1
...
[k] -> x^k

Now, suppose we have 2 dims, thus data points (x0_i,x1_i,y_i) and a poly of order 2:

f(x0,x1) = a0*x0^0*x1^0 + a1*x0^0*x1^1 + a2*x0^0*x1^2 + a3*x0^1*x1^0 +
           a4*x0^1*x1^1 + a5*x0^1*x1^2 + a6*x0^2*x1^0 + a7*x0^2*x1^1 +
           a8*x0^2*x1^2

with 9 coeffs a = [a0,…,a8]. Therefore, powers.shape = (9,2):

[[0, 0],
 [0, 1],
 [0, 2],
 [1, 0],
 [1, 1],
 [1, 2],
 [2, 0],
 [2, 1],
 [2, 2]]

and:

[0,0] -> x0^0*x1^0
[1,2] -> x0^1*x1^2
...