def monomial_variance_uniform_variables(indices, coeffs): mean = monomial_mean_uniform_variables(indices, coeffs) squared_indices, squared_coeffs = multiply_multivariate_polynomials( indices, coeffs, indices, coeffs) variance = monomial_mean_uniform_variables( squared_indices, squared_coeffs)-mean**2 return variance
def coeffs_of_active_subspace_polynomials(W1_trans, as_poly_indices): num_active_vars, num_vars = W1_trans.shape num_as_moments = as_poly_indices.shape[1] moments = np.zeros((num_as_moments), float) monomial_power_indices = [] monomial_power_coeffs = [] for var_num in range(num_active_vars): monomial_power_indices.append([]) monomial_power_coeffs.append([]) for degree in range(as_poly_indices[var_num, :].max() + 1): coeffs, indices = coeffs_of_power_of_nd_linear_polynomial( num_vars, degree, W1_trans[var_num, :]) monomial_power_coeffs[var_num].append(coeffs) monomial_power_indices[var_num].append(indices) indices_list = [] coeffs_list = [] for ii in range(as_poly_indices.shape[1]): as_index = as_poly_indices[:, ii] activated_vars = np.where(as_index > 0)[0] num_activated_vars = activated_vars.shape[0] if num_activated_vars > 0: var_num = activated_vars[0] degree = as_index[var_num] else: degree = 0 var_num = 0 coeffs = monomial_power_coeffs[var_num][degree] indices = monomial_power_indices[var_num][degree] for dd in range(1, num_activated_vars): var_num = activated_vars[dd] degree = as_index[var_num] coeffs_dd = monomial_power_coeffs[var_num][degree] indices_dd = monomial_power_indices[var_num][degree] # TODO Extension does not groud like terms,but pure python function # multiply_multivariate_polynomials does. # This is only a valid substitution if this function is # only called by intergrate moments try: from pyapprox.cython.manipulate_polynomials import \ multiply_multivariate_polynomials_pyx indices, coeffs = multiply_multivariate_polynomials_pyx( indices, coeffs, indices_dd, coeffs_dd) except: print('multiply_multivariate_polynomials extension failed') indices, coeffs = multiply_multivariate_polynomials( indices, coeffs, indices_dd, coeffs_dd) indices_list.append(indices) coeffs_list.append(coeffs) return indices_list, coeffs_list
def coeffs_of_active_subspace_polynomial(W1_trans, as_index): """ Evaluate the coefficients of a polynomial in an active subspace in terms of a polynomial in the original variable space. In active subspace with a monomial basis we may have a polynomial with terms [1,y1,y2,y1**2,y1*y2,y2**2,y1**3,y1**2*y2,y1*y2**2,y2**3] We want to compute a polynomial in terms of the original variables x. E.g. y1**2*y2 = W_11*x1+W_12*x2+W_13*x3)**2*(W_21*x1+W_22*x2+W_23*x3) where the native function space (x) has dimension num_vars=3 Parameters ---------- W1_trans : np.ndarray (num_active_vars, num_vars) The active subspace active variable transformation such that the active variable y is obtained from the full dimension variable x via y = W1_trans*x as_index : np.ndarray (num_active_vars) The multivariate index of the polynomial in terms of the active subspace variables y Returns ------- coeff : np.ndarray (num_moments) The coefficients of the polynomial in terms of the original variables x indices : np.ndarray (num_terms) The set of multivariate indices that define equivalent polynomial (to the active subapce polynomial) in terms of the original variables x """ num_vars = W1_trans.shape[1] activated_vars = np.where(as_index > 0)[0] num_activated_vars = activated_vars.shape[0] if num_activated_vars > 0: var_num = activated_vars[0] degree = as_index[var_num] else: degree = 0 var_num = 0 coeffs, indices = coeffs_of_power_of_nd_linear_polynomial( num_vars, degree, W1_trans[var_num, :]) for dd in range(1, num_activated_vars): var_num = activated_vars[dd] degree = as_index[var_num] coeffs_d, indices_d = coeffs_of_power_of_nd_linear_polynomial( num_vars, degree, W1_trans[var_num, :]) indices, coeffs = multiply_multivariate_polynomials( indices, coeffs, indices_d, coeffs_d) return coeffs, indices
def coeffs_of_active_subspace_polynomials(W1_trans, as_poly_indices): num_active_vars, num_vars = W1_trans.shape monomial_power_indices = [[] for ii in range(num_active_vars)] monomial_power_coeffs = [[] for ii in range(num_active_vars)] for var_num in range(num_active_vars): for degree in range(as_poly_indices[var_num, :].max()+1): coeffs, indices = coeffs_of_power_of_nd_linear_monomial( num_vars, degree, W1_trans[var_num, :]) monomial_power_coeffs[var_num].append(coeffs[:, None].copy()) monomial_power_indices[var_num].append(indices) indices_list = [] coeffs_list = [] for ii in range(as_poly_indices.shape[1]): as_index = as_poly_indices[:, ii] activated_vars = np.where(as_index > 0)[0] num_activated_vars = activated_vars.shape[0] if num_activated_vars > 0: var_num = activated_vars[0] degree = as_index[var_num] else: degree = 0 var_num = 0 coeffs = monomial_power_coeffs[var_num][degree] indices = monomial_power_indices[var_num][degree] for dd in range(1, num_activated_vars): var_num = activated_vars[dd] degree = as_index[var_num] coeffs_dd = monomial_power_coeffs[var_num][degree] indices_dd = monomial_power_indices[var_num][degree] # # TODO Extension does not group like terms, but pure python function # # multiply_multivariate_polynomials does. # # This is only a valid substitution if this function is # # only called by intergrate moments # try: # from pyapprox.cython.manipulate_polynomials import \ # multiply_multivariate_polynomials_pyx # indices, coeffs = multiply_multivariate_polynomials_pyx( # indices, coeffs, indices_dd, coeffs_dd) # except (ImportError, TypeError) as e: # from pyapprox.sys_utilities import trace_error_with_msg # trace_error_with_msg('multiply_multivariate_polynomials extension failed', e) # print(coeffs.shape, coeffs_dd.shape) indices, coeffs = multiply_multivariate_polynomials( indices, coeffs, indices_dd, coeffs_dd) indices_list.append(indices) coeffs_list.append(coeffs[:, 0]) return indices_list, coeffs_list