def test_chebvander2d(self): # also tests chebval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) van = cheb.chebvander2d(x1, x2, [1, 2]) tgt = cheb.chebval2d(x1, x2, c) res = np.dot(van, c.flat) assert_almost_equal(res, tgt) # check shape van = cheb.chebvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6))
def test_chebvander2d(self) : # also tests chebval2d for non-square coefficient array x1, x2, x3 = self.x c = np.random.random((2, 3)) van = cheb.chebvander2d(x1, x2, [1, 2]) tgt = cheb.chebval2d(x1, x2, c) res = np.dot(van, c.flat) assert_almost_equal(res, tgt) # check shape van = cheb.chebvander2d([x1], [x2], [1, 2]) assert_(van.shape == (1, 5, 6))
def norder2dcheb(nx, ny): ''' Purpose ------- Create the 2D nx th and ny th order Chebyshev polynomial using numpy.polynomial.chebyshev.chebvander2d(x, y, [nx, ny]) ''' x = Symbol('x') y = Symbol('y') funcarr = cheb.chebvander2d(x,y,[nx,ny]) funcarr = funcarr[0] # Because chebvander2d returns a 2d matrix funclist = funcarr.tolist() # lambdify only takes in lists and not arrays f = lambdify((x, y), funclist) # Note: lambdify looks at 1 as 1 and makes f[0] = 1 and not an array return funclist, f
def fit_2d_polynomial(x, y, z, order): """Fit z = f(x,y) as a 2d polynomial function Returns a function object f. """ # I seriously don't know why this isn't a first-level numpy function. # It required some sleuthing to find all the numpy primitives required, but once # I found them, it's almost trivial to put them together. from numpy.polynomial import chebyshev as cheby A = cheby.chebvander2d(x, y, (order, order)) coeff = np.linalg.lstsq(A, z, rcond=None)[0].reshape(order + 1, order + 1) fn = lambda x, y: cheby.chebval2d(x, y, coeff) return fn
def chebForwardTransform(orders, locations, functionVals): if len(locations.shape) == 1: return np.array(cheb.chebfit(locations, functionVals, orders[0])) else: if locations.shape[1] == 2: V = cheb.chebvander2d(locations[:, 0], locations[:, 1], orders) elif locations.shape[1] == 3: V = cheb.chebvander3d(locations[:, 0], locations[:, 1], locations[:, 2], orders) elif locations.shape[1] == 4: V = chebVander4d(locations, orders) elif locations.shape[1] == 5: V = chebVander5d(locations, orders) else: raise NotImplementedError # there's a bad startup joke about this being good enough for the paper. ret, _, _, _ = npl.lstsq(V, functionVals, rcond=None) return np.reshape(ret, (np.array(orders) + 1).flatten())
def __init__(self, fun, n, lower, upper, dim=1): self.chebpoints = np.asarray( [np.cos((2 * k - 1) * np.pi / (2 * n)) for k in range(1, n + 1)]) y = x = self.chebpoints XX, YY = np.meshgrid(x, y) XX = XX.flatten() YY = YY.flatten() M = chebvander2d(XX, YY, [n - 1, n - 1]) rhs = np.zeros((n * n, dim)) for i in range(n * n): rhs[i, :] = fun(self.scalefromdefault(XX[i], lower[0], upper[0]), self.scalefromdefault(YY[i], lower[1], upper[1])) c = np.linalg.solve(M, rhs) self.c = [] for i in range(dim): self.c.append(c[:, i].reshape((n, n))) self.lower = lower self.upper = upper self.fun = fun self.dim = dim
def splosno(n): return chebvander2d(iksos,thetos,[n,n])/(0.0573/164.102)
def A(n): return chebvander2d(x,th,[n,n])/(0.0573/164.102)
def vandermonde(self, points): """""" vander = chebvander2d(points[:, 0], points[:, 1], [self.degree, self.degree]) ulmask = CoeffPolynomial.upper_left_triangular_mask(self.degree) return vander[:, ulmask.ravel()]
def test_interpCheby2D_7(x, y): val = cheby.interpCheby2D_7(x, y, np.ones(36)) nm = chebvander2d(np.array(x), np.array(y), [7, 7])[:, index_map] assert (val == nm.sum(axis=1)).all()
def test_matrixCheby2D_7(x, y): m = cheby.matrixCheby2D_7(x, y) nm = chebvander2d(np.array(x), np.array(y), [7, 7]) for i, im in enumerate(index_map): assert (m[:, i] == nm[:, im]).all()