def orthonormal_polynomials(self, *max_degree): ''' Return the max_degree-1 first orthonormal polynomials associated with the NormalRandomVariable. Parameters ---------- max_degree : int, optional The maximum degree of the returned polynomials. The default is None, choosing the default maximum degree associated with the constructor of the polynomials. Returns ------- poly : tensap.OrthonormalPolynomials The generated orthonormal polynomials. ''' poly = tensap.HermitePolynomials(*max_degree) if self != NormalRandomVariable(0, 1): # print('ShiftedOrthonormalPolynomials are created.') poly = tensap.ShiftedOrthonormalPolynomials(poly, self.mu, self.sigma) return poly
# %% Projection on polynomial space through quadrature def FUN(x): # Function to approximate return x**2/2 FUN = tensap.UserDefinedFunction(FUN, 1) FUN.evaluation_at_multiple_points = True X = tensap.NormalRandomVariable() # Integration rule I = X.gauss_integration_rule(5) # Approximation basis P = 3 H = tensap.PolynomialFunctionalBasis(tensap.HermitePolynomials(), range(P+1)) # Computation of the approximation F = H.projection(FUN, I) # Derivative and second derivative of F through projection DF = H.projection(lambda x, F=F: F.eval_derivative(1, x), I) DDF = H.projection(lambda x, DF=DF: DF.eval_derivative(1, x), I) # Displays and error print('\nProjection on polynomial space through quadrature') N = 100 ERR_L2, ERR_L_INF = F.test_error(FUN, N, X) print('Mean squared error = %2.5e' % ERR_L2) plt.figure() X_PLOT = np.linspace(-1, 1, 100)
''' import sys import numpy as np sys.path.insert(0, './../../../') import tensap # %% Approximation of a function F(X) DIM = 6 FUN = tensap.UserDefinedFunction('x0 + x0*x1 + x0*x2**2 + x3**3 + x4 + x5', DIM) FUN.evaluationAtMultiplePoints = True DEGREE = 3 X = tensap.RandomVector(tensap.NormalRandomVariable(0, 1), DIM) P = tensap.PolynomialFunctionalBasis(tensap.HermitePolynomials(), range(DEGREE+1)) BASES = tensap.FunctionalBases.duplicate(P, DIM) GRIDS = X.random(1000) G = tensap.FullTensorGrid([np.reshape(GRIDS[:, i], [-1, 1]) for i in range(DIM)]) H = tensap.FullTensorProductFunctionalBasis(BASES) F, OUTPUT = H.tensor_product_interpolation(FUN, G) X_TEST = X.random(1000) F_X_TEST = F(X_TEST) Y_TEST = FUN(X_TEST) ERR = np.linalg.norm(Y_TEST-F_X_TEST) / np.linalg.norm(Y_TEST) print('Mean squared error = %2.5e\n' % ERR)