def test_1V(): obj = test() obj.single_mode_evolution = False f_generalized = MB_dist(obj.q1_center, obj.q2_center, obj.p1, obj.p2, obj.p3, 1) C_f_hat_generalized = 2 * af.fft2( collision_operator.BGK( f_generalized, obj.q1_center, obj.q2_center, obj.p1, obj.p2, obj.p3, obj.compute_moments, obj.physical_system.params)) / (obj.N_q2 * obj.N_q1) # Background C_f_hat_generalized[0, 0, :] = 0 # Finding the indices of the mode excited: i_q1_max = np.unravel_index( af.imax(af.abs(C_f_hat_generalized))[1], (obj.N_q1, obj.N_q2, obj.N_p1 * obj.N_p2 * obj.N_p3), order='F')[0] i_q2_max = np.unravel_index( af.imax(af.abs(C_f_hat_generalized))[1], (obj.N_q1, obj.N_q2, obj.N_p1 * obj.N_p2 * obj.N_p3), order='F')[1] obj.p1 = np.array(af.reorder(obj.p1, 1, 2, 3, 0)) obj.p2 = np.array(af.reorder(obj.p2, 1, 2, 3, 0)) obj.p3 = np.array(af.reorder(obj.p3, 1, 2, 3, 0)) delta_f_hat = 0.01 * (1 / (2 * np.pi))**(1 / 2) \ * np.exp(-0.5 * obj.p1**2) obj.single_mode_evolution = True C_f_hat_single_mode = collision_operator.linearized_BGK( delta_f_hat, obj.p1, obj.p2, obj.p3, obj.compute_moments, obj.physical_system.params) assert (af.mean( af.abs( af.flat(C_f_hat_generalized[i_q1_max, i_q2_max]) - af.to_array(C_f_hat_single_mode.flatten()))) < 1e-14)
def argmax(a: ndarray, axis: tp.Optional[int] = None, out: tp.Optional[ndarray] = None) -> ndarray: if out: raise ValueError('out != None is not supported') val, idx = af.imax(a._af_array, dim=axis) return _wrap_af_array(idx)
def argmax(self, axis: tp.Optional[int] = None, out: tp.Optional['ndarray'] = None): """ Return indices of the maximum values along the given axis. """ val, idx = af.imax(self._af_array, dim=axis) return _wrap_af_array(idx)
def predict_proba1(self, X): near_locs, near_dists = af.vision.nearest_neighbour(X, self._data, self._dim, \ self._num_nearest, self._match_type) weights = self._get_neighbor_weights(near_dists) top_labels = af.moddims(self._labels[near_locs], \ get_dims(near_locs)[0], get_dims(near_locs)[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal probs, _ = af.imax(accum_weights, dim=0) return probs.T
def _predict_proba(self, query, train_feats, train_labels, k, dist_type, weight_by_dist): near_locs, near_dists = af.vision.nearest_neighbour(query, train_feats, 1, \ k, dist_type) weights = self._get_neighbor_weights(near_dists, weight_by_dist, k) top_labels = af.moddims(train_labels[near_locs], \ near_locs.dims()[0], near_locs.dims()[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal probs, _ = af.imax(accum_weights, dim=0) return probs.T
def argmax(self, axis=None): if axis is None: return self.flat.argmax(axis=0) if not isinstance(axis, numbers.Number): raise TypeError('an integer is required for the axis') val, idx = arrayfire.imax(self.d_array, pu.c2f(self.shape, axis)) shape = list(self.shape) shape.pop(axis) if(len(shape)): return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx) else: return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx)[()]
def argmax(self, axis=None): if axis is None: return self.flat.argmax(axis=0) if not isinstance(axis, numbers.Number): raise TypeError('an integer is required for the axis') val, idx = arrayfire.imax(self.d_array, pu.c2f(self.shape, axis)) shape = list(self.shape) shape.pop(axis) if (len(shape)): return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx) else: return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx)[()]
def predict(self, X): near_locs, near_dists = af.vision.nearest_neighbour(X, self._data, self._dim, \ self._num_nearest, self._match_type) weights = self._get_neighbor_weights(near_dists) top_labels = af.moddims(self._labels[near_locs], \ get_dims(near_locs)[0], get_dims(near_locs)[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal _, max_weight_locs = af.imax(accum_weights, dim=0) pred_idxs = af.range(get_dims(accum_weights)[1]) * get_dims( accum_weights)[0] + max_weight_locs.T top_labels_flat = af.flat(top_labels) pred_classes = top_labels_flat[pred_idxs] return pred_classes
def _predict(self, query, train_feats, train_labels, k, dist_type, weight_by_dist): near_locs, near_dists = af.vision.nearest_neighbour(query, train_feats, 1, \ k, dist_type) weights = self._get_neighbor_weights(near_dists, weight_by_dist, k) top_labels = af.moddims(train_labels[near_locs], \ near_locs.dims()[0], near_locs.dims()[1]) accum_weights = af.scan_by_key( top_labels, weights) # reduce by key would be more ideal _, max_weight_locs = af.imax(accum_weights, dim=0) pred_idxs = af.range(accum_weights.dims() [1]) * accum_weights.dims()[0] + max_weight_locs.T top_labels_flat = af.flat(top_labels) pred_classes = top_labels_flat[pred_idxs] return pred_classes
def predict(self, X): probs = self.predict_proba(X) _, classes = af.imax(probs, 1) return classes
def accuracy(predicted, target): _, tlabels = af.imax(target, 1) _, plabels = af.imax(predicted, 1) return 100 * af.count(plabels == tlabels) / tlabels.elements()
def predict_class(X, Weights): probs = predict_prob(X, Weights) _, classes = af.imax(probs, 1) return classes
def _predict(self, X: af.Array, Weights: af.Array) -> af.Array: probs = self._predict_proba(X, Weights) _, classes = af.imax(probs, 1) classes = classes + self._label_offset return classes
def _initialize(self, params): """ Called when the solver object is declared. This function is used to initialize the distribution function, and the field quantities using the options as provided by the user. The quantities are then mapped to the fourier basis by taking FFTs. The independant modes are then evolved by using the linear solver. """ # af.broadcast(function, *args) performs batched operations on # function(*args): f = af.broadcast(self.physical_system.initial_conditions.\ initialize_f, self.q1_center, self.q2_center, self.p1, self.p2, self.p3, params ) # Taking FFT: f_hat = af.fft2(f) # Since (k_q1, k_q2) = (0, 0) will give the background distribution: # The division by (self.N_q1 * self.N_q2) is performed since the FFT # at (0, 0) returns (amplitude * (self.N_q1 * self.N_q2)) self.f_background = af.abs(f_hat[0, 0, :])/ (self.N_q1 * self.N_q2) # Calculating derivatives of the background distribution function: self._calculate_dfdp_background() # Scaling Appropriately: # Except the case of (0, 0) the FFT returns # (0.5 * amplitude * (self.N_q1 * self.N_q2)): f_hat = 2 * f_hat / (self.N_q1 * self.N_q2) if(self.single_mode_evolution == True): # Background f_hat[0, 0, :] = 0 # Finding the indices of the perturbation introduced: i_q1_max = np.unravel_index(af.imax(af.abs(f_hat))[1], (self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3 ),order = 'F' )[0] i_q2_max = np.unravel_index(af.imax(af.abs(f_hat))[1], (self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3 ),order = 'F' )[1] # Taking sum to get a scalar value: params.k_q1 = af.sum(self.k_q1[i_q1_max, i_q2_max]) params.k_q2 = af.sum(self.k_q2[i_q1_max, i_q2_max]) self.f_background = np.array(self.f_background).\ reshape(self.N_p1, self.N_p2, self.N_p3) self.p1 = np.array(self.p1).\ reshape(self.N_p1, self.N_p2, self.N_p3) self.p2 = np.array(self.p2).\ reshape(self.N_p1, self.N_p2, self.N_p3) self.p3 = np.array(self.p3).\ reshape(self.N_p1, self.N_p2, self.N_p3) self._A_q1 = np.array(self._A_q1).\ reshape(self.N_p1, self.N_p2, self.N_p3) self._A_q2 = np.array(self._A_q2).\ reshape(self.N_p1, self.N_p2, self.N_p3) params.rho_background = self.compute_moments('density', self.f_background ) params.p1_bulk_background = self.compute_moments('mom_p1_bulk', self.f_background ) / params.rho_background params.p2_bulk_background = self.compute_moments('mom_p2_bulk', self.f_background ) / params.rho_background params.p3_bulk_background = self.compute_moments('mom_p3_bulk', self.f_background ) / params.rho_background params.T_background = ((1 / params.p_dim) * \ self.compute_moments('energy', self.f_background ) - params.rho_background * params.p1_bulk_background**2 - params.rho_background * params.p2_bulk_background**2 - params.rho_background * params.p3_bulk_background**2 ) / params.rho_background self.dfdp1_background = np.array(self.dfdp1_background).\ reshape(self.N_p1, self.N_p2, self.N_p3) self.dfdp2_background = np.array(self.dfdp2_background).\ reshape(self.N_p1, self.N_p2, self.N_p3) self.dfdp3_background = np.array(self.dfdp3_background).\ reshape(self.N_p1, self.N_p2, self.N_p3) # Unable to recover pert_real and pert_imag accurately from the input. # There seems to be some error in recovering these quantities which # is dependant upon the resolution. Using user-defined quantities instead delta_f_hat = params.pert_real * self.f_background \ + params.pert_imag * self.f_background * 1j self.Y = np.array([delta_f_hat]) compute_electrostatic_fields(self) self.Y = np.array([delta_f_hat, self.delta_E1_hat, self.delta_E2_hat, self.delta_E3_hat, self.delta_B1_hat, self.delta_B2_hat, self.delta_B3_hat ] ) else: # Using a vector Y to evolve the system: self.Y = af.constant(0, self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3, 7, dtype = af.Dtype.c64 ) # Assigning the 0th indice along axis 3 to the f_hat: self.Y[:, :, :, 0] = f_hat # Initializing the EM field quantities: self.E3_hat = af.constant(0, self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3, dtype = af.Dtype.c64 ) self.B1_hat = af.constant(0, self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3, dtype = af.Dtype.c64 ) self.B2_hat = af.constant(0, self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3, dtype = af.Dtype.c64 ) self.B3_hat = af.constant(0, self.N_q1, self.N_q2, self.N_p1 * self.N_p2 * self.N_p3, dtype = af.Dtype.c64 ) # Initializing EM fields using Poisson Equation: if(self.physical_system.params.fields_initialize == 'electrostatic' or self.physical_system.params.fields_initialize == 'fft' ): compute_electrostatic_fields(self) # If option is given as user-defined: elif(self.physical_system.params.fields_initialize == 'user-defined'): E1, E2, E3 = \ self.physical_system.initial_conditions.initialize_E(self.q1_center, self.q2_center, self.physical_system.params) B1, B2, B3 = \ self.physical_system.initial_conditions.initialize_B(self.q1_center, self.q2_center, self.physical_system.params) # Scaling Appropriately self.E1_hat = af.tile(2 * af.fft2(E1) / (self.N_q1 * self.N_q2), 1, 1, self.N_p1 * self.N_p2 * self.N_p3) self.E2_hat = af.tile(2 * af.fft2(E2) / (self.N_q1 * self.N_q2), 1, 1, self.N_p1 * self.N_p2 * self.N_p3) self.E3_hat = af.tile(2 * af.fft2(E3) / (self.N_q1 * self.N_q2), 1, 1, self.N_p1 * self.N_p2 * self.N_p3) self.B1_hat = af.tile(2 * af.fft2(B1) / (self.N_q1 * self.N_q2), 1, 1, self.N_p1 * self.N_p2 * self.N_p3) self.B2_hat = af.tile(2 * af.fft2(B2) / (self.N_q1 * self.N_q2), 1, 1, self.N_p1 * self.N_p2 * self.N_p3) self.B3_hat = af.tile(2 * af.fft2(B3) / (self.N_q1 * self.N_q2), 1, 1, self.N_p1 * self.N_p2 * self.N_p3) else: raise NotImplementedError('Method invalid/not-implemented') # Assigning other indices along axis 3 to be # the EM field quantities: self.Y[:, :, :, 1] = self.E1_hat self.Y[:, :, :, 2] = self.E2_hat self.Y[:, :, :, 3] = self.E3_hat self.Y[:, :, :, 4] = self.B1_hat self.Y[:, :, :, 5] = self.B2_hat self.Y[:, :, :, 6] = self.B3_hat af.eval(self.Y) return
def argmax(arr, axis=None): val, idx = af.imax(arr, axis) return idx