def _get_results(self): '''Estimate the mean value function given the randomization pattern. ''' e_orth = orthogonalize(self.evar_list) self.mu_q_method start_time = sysclock() mu_q_arr, var_q_arr = self.mu_q_method(*e_orth) exec_time = sysclock() - start_time return mu_q_arr, var_q_arr, exec_time
def power(): a = np.linspace(0, 1, 5) a0 = a.copy() par = 0.5 mask = a > par a0[mask] = 0 a_m = np.ma.array(a, mask=mask) res1a = a0.copy() start = sysclock() res1a **= 2 print sysclock() - start, 'zeroed array, inplace, without logical operator (single pass) - res1a **= 2' start = sysclock() res1b = a0 ** 2 print sysclock() - start, 'zeroed array, (single pass) allocation of additional array - res1b = a ** 2' res2a = a_m.copy() start = sysclock() res2a **= 2 print sysclock() - start, 'implicit mask, inplace, access indirection through mask - res2a **= 2' start = sysclock() res2b = a_m ** 2 print sysclock() - start, 'implicit mask, assigned, access indirection through mask - res2b = a_m ** 2' res3 = a.copy() start = sysclock() res3[a <= par] **= 2 print sysclock() - start, 'explicit mask, inplace, two passes through array - res3[res3 > 0] **= 2' start = sysclock() res4 = (a * Heaviside(par - a)) ** 2 print sysclock() - start, 'Heaviside, two passes - res4 = (a * Heaviside(-a + 0.5)) ** 2' print 'all arrays are equal -', (np.array_equal(res1a[~mask], res1b[~mask]) and np.array_equal(res2a.data[~mask], res2b.data[~mask]) and np.array_equal(res1a[~mask], res2a.data[~mask]) and np.array_equal(res2b.data[~mask], res3[~mask]) and np.array_equal(res4[~mask], res3[~mask]))
def multip(): a = np.linspace(0, 1, 1000000) b = np.linspace(0, 1, 1000000) a[a > 0.5] = 0 b[b > 0.5] = 0 a_m = np.ma.array(a, mask=a > 0.5) b_m = np.ma.array(b, mask=b > 0.5) res1a = a.copy() start = sysclock() res1a *= b print sysclock() - start, 'full array - res1a *= b' start = sysclock() res1b = a * b print sysclock() - start, 'full array - res1b = a * b' res2a = a_m.copy() start = sysclock() res2a *= b_m print sysclock() - start, 'numpy masked array - res2a *= b_m' start = sysclock() res2b = a_m * b_m print sysclock() - start, 'numpy masked array - res2b = a_m * b_m' res3 = a.copy() start = sysclock() res3[res3 > 0] *= b[res3 > 0] print sysclock() - start, 'mask explicit' start = sysclock() res4 = a * Heaviside(-a + 0.5) * b * Heaviside(-b + 0.5) print sysclock() - start, 'Heaviside' print 'all arrays are equal -', np.array_equal(res1a, res1b) and np.array_equal(res2a.data, res2b.data)\ and np.array_equal(res1a, res2a.data) and np.array_equal(res2b.data, res3)\ and np.array_equal(res4, res3)
a = np.linspace(0, 1, 5000)[:, None] b = np.linspace(0, 1, 5000)[None, :] a_m = np.ma.array(a, mask = a > 0.5) b_m = np.ma.array(b, mask = b > 0.5) def f(a, b): return a * b * np.cos(a) * np.sin(b) def f_H(a, b): return a * b * np.cos(a) * np.sin(b) * H(0.5 - a) def f_m(a, b): return a * b * np.ma.cos(a) * np.ma.sin(b) start = sysclock() res = f(a, b) print sysclock() - start, 'full array' del res start = sysclock() res = f_H(a, b) print sysclock() - start, 'heaviside' del res start = sysclock() res = f(a, b) * H(0.5 - a) print sysclock() - start, 'heaviside, alt 2' del res start = sysclock()
import scipy.weave as weave import platform if platform.system() == 'Linux': from time import time as sysclock elif platform.system() == 'Windows': from time import clock as sysclock l = 5000 x = np.linspace(0., 10., l) y = np.linspace(0., 10., l) def f(x, y): return np.sum(x[:, None] * y[None, :]) start = sysclock() print f(x, y) print sysclock() - start, 'numpy' c_code = ''' double r = 0; for( int i_x=0; i_x < l; i_x++){ for( int i_y=0; i_y < l; i_y++){ r += x(i_x) * y(i_y);// nefunguje *(x + i_x) * *(y + i_y); }; }; return_val = r; ''' conv = weave.converters.blitz #conv = weave.converters.default
def power(): a = np.linspace(0, 1, 5) a0 = a.copy() par = 0.5 mask = a > par a0[mask] = 0 a_m = np.ma.array(a, mask=mask) res1a = a0.copy() start = sysclock() res1a **= 2 print sysclock( ) - start, 'zeroed array, inplace, without logical operator (single pass) - res1a **= 2' start = sysclock() res1b = a0**2 print sysclock( ) - start, 'zeroed array, (single pass) allocation of additional array - res1b = a ** 2' res2a = a_m.copy() start = sysclock() res2a **= 2 print sysclock( ) - start, 'implicit mask, inplace, access indirection through mask - res2a **= 2' start = sysclock() res2b = a_m**2 print sysclock( ) - start, 'implicit mask, assigned, access indirection through mask - res2b = a_m ** 2' res3 = a.copy() start = sysclock() res3[a <= par] **= 2 print sysclock( ) - start, 'explicit mask, inplace, two passes through array - res3[res3 > 0] **= 2' start = sysclock() res4 = (a * Heaviside(par - a))**2 print sysclock( ) - start, 'Heaviside, two passes - res4 = (a * Heaviside(-a + 0.5)) ** 2' print 'all arrays are equal -', ( np.array_equal(res1a[~mask], res1b[~mask]) and np.array_equal(res2a.data[~mask], res2b.data[~mask]) and np.array_equal(res1a[~mask], res2a.data[~mask]) and np.array_equal(res2b.data[~mask], res3[~mask]) and np.array_equal(res4[~mask], res3[~mask]))
def main(): a = np.linspace(0, 1, 5000)[:, None] b = np.linspace(0, 1, 5000)[None, :] a_m = np.ma.array(a, mask = a > 0.5) b_m = np.ma.array(b, mask = b > 0.5) start = sysclock() res = f(a, b) print sysclock() - start, 'full array' del res start = sysclock() res = f_H(a, b) print sysclock() - start, 'heaviside' del res start = sysclock() res = f(a, b) * H(0.5 - a) print sysclock() - start, 'heaviside, alt 2' del res start = sysclock() res = f(a_m, b_m) print sysclock() - start, 'masked array, numpy function' del res start = sysclock() res = f_m(a_m, b_m) print sysclock() - start, 'masked array, mask function' del res
def run1(): a = 0. b = 0. c = np.zeros((1000, 1), dtype = float) d = np.zeros((1, 1000), dtype = float) for i in range(1000): f(a, b, c, d) def run2(): a = np.zeros((1, 1, 1, 1), dtype = float) b = np.zeros((1, 1, 1, 1), dtype = float) c = np.zeros((1, 1, 1000, 1), dtype = float) d = np.zeros((1, 1, 1, 1000), dtype = float) for i in range(1000): f(a, b, c, d) if __name__ == '__main__': from time import time as sysclock start_time = sysclock() run1() exec_time = sysclock() - start_time print '1', exec_time start_time = sysclock() run2() exec_time = sysclock() - start_time print '2', exec_time
def _eval( self ): '''Evaluate the integral based on the configuration of algorithm. ''' if self.cached_dG == False and self.compiled_QdG_loop == False: raise NotImplementedError, \ 'Configuration for pure Python integration is too slow and is not implemented' self._set_compiler() # prepare the array of the control variable discretization # eps_arr = self.eps_arr mu_q_arr = zeros_like( eps_arr ) # prepare the variable for the variance var_q_arr = None if self.implicit_var_eval: var_q_arr = zeros_like( eps_arr ) # prepare the parameters for the compiled function in # a separate dictionary c_params = {} if self.compiled_eps_loop: # for compiled eps_loop the whole input and output array must be passed to c # c_params['e_arr'] = eps_arr c_params['mu_q_arr'] = mu_q_arr #c_params['n_eps' ] = n_eps if self.compiled_QdG_loop: # prepare the lengths of the arrays to set the iteration bounds # for rv in self.rv_list: c_params[ '%s_flat' % rv.name ] = rv.theta_arr if len( self.rv_list ) > 0: if self.cached_dG: c_params[ 'dG_grid' ] = self.dG_grid else: for rv in self.rv_list: c_params['%s_pdf' % rv.name] = rv.dG_arr else: c_params[ 'dG_grid' ] = self.dG_grid if self.cached_dG: conv = converters.blitz else: conv = converters.default t = sysclock() if self.compiled_eps_loop: # C loop over eps, all inner loops must be compiled as well # if self.implicit_var_eval: raise NotImplementedError, 'calculation of variance not available in the compiled version' inline( self.C_code, self.arg_list, local_dict = c_params, type_converters = conv, compiler = self.compiler, verbose = self.compiler_verbose ) else: # Python loop over eps # for idx, e in enumerate( eps_arr ): if self.compiled_QdG_loop: if self.implicit_var_eval: raise NotImplementedError, 'calculation of variance not available in the compiled version' # C loop over random dimensions # c_params['e'] = e # prepare the parameter mu_q = inline( self.C_code, self.arg_list, local_dict = c_params, type_converters = conv, compiler = self.compiler, verbose = self.compiler_verbose ) else: # Numpy loops over random dimensions # # get the rf grid for all combinations of # parameter values # Q_grid = self.rf( e, *self.theta_ogrid ) # multiply the response grid with the contributions # of pdf distributions (weighted by the delta of the # random variable disretization) # if not self.implicit_var_eval: # only mean value needed, the multiplication can be done # in-place Q_grid *= self.dG_grid # sum all the values to get the integral mu_q = sum( Q_grid ) else: # get the square values of the grid Q_grid2 = Q_grid ** 2 # make an inplace product of Q_grid with the weights Q_grid *= self.dG_grid # make an inplace product of the squared Q_grid with the weights Q_grid2 *= self.dG_grid # sum all values to get the mean mu_q = sum( Q_grid ) # sum all squared values to get the variance var_q = sum( Q_grid2 ) - mu_q ** 2 # add the value to the return array mu_q_arr[idx] = mu_q if self.implicit_var_eval: var_q_arr[idx] = var_q duration = sysclock() - t return mu_q_arr, var_q_arr, duration
def power(): a = np.linspace(0, 1, 100000) a[a > 0.5] = 0 a_m = np.ma.array(a, mask=a > 0.5) res1a = a.copy() start = sysclock() res1a **= 2 print sysclock() - start, 'full array - res1a **= 2' start = sysclock() res1b = a ** 2 print sysclock() - start, 'full array - res1b = a ** 2' res2a = a_m.copy() start = sysclock() res2a **= 2 print sysclock() - start, 'numpy masked array - res2a **= 2' start = sysclock() res2b = a_m ** 2 print sysclock() - start, 'numpy masked array - res2b = a_m ** 2' res3 = a.copy() start = sysclock() res3[res3 > 0] **= 2 print sysclock() - start, 'mask explicit' print 'all arrays are equal -', np.array_equal(res1a, res1b) and np.array_equal(res2a.data, res2b.data)\ and np.array_equal(res1a, res2a.data) and np.array_equal(res2b.data, res3)