def apply_list(self, zmin, zmax, ns, evaluation): 'RandomComplex[{zmin_, zmax_}, ns_]' expr = Expression('RandomComplex', Expression('List', zmin, zmax), ns) min_value, max_value = self.to_complex(zmin, evaluation), self.to_complex( zmax, evaluation) if min_value is None or max_value is None: return evaluation.message('RandomComplex', 'unifr', Expression('List', zmin, zmax)) py_ns = ns.to_python() if not isinstance(py_ns, list): py_ns = [py_ns] if not all([isinstance(i, int) and i >= 0 for i in py_ns]): return evaluation.message('RandomComplex', 'array', ns, expr) with RandomEnv(evaluation) as rand: real = rand.randreal(min_value.real, max_value.real, py_ns) imag = rand.randreal(min_value.imag, max_value.imag, py_ns) return instantiate_elements( stack(real, imag), lambda c: Complex(Real(c[0]), Real(c[1])), d=2)
def apply(self, domain, size, evaluation): '''%(name)s[domain_, size_]''' if domain.get_head_name() == 'System`Rule': # elements and weights err, py_weights = self._weights_to_python(domain.leaves[0], evaluation) if py_weights is None: return err elements = domain.leaves[1].leaves if domain.leaves[1].get_head_name() != 'System`List' or len( py_weights) != len(elements): return evaluation.message(self.get_name(), 'wghtv', domain) elif domain.get_head_name() == 'System`List': # only elements py_weights = None elements = domain.leaves else: return evaluation.message(self.get_name(), 'lrwl', domain) err, py_size = self._size_to_python(domain, size, evaluation) if py_size is None: return err if not self._replace: # i.e. RandomSample? n_chosen = reduce(operator_mul, py_size, 1) if len(elements) < n_chosen: return evaluation.message('smplen', size, domain), None with RandomEnv(evaluation) as rand: return instantiate_elements( rand.randchoice(len(elements), size=py_size, replace=self._replace, p=py_weights), lambda i: elements[i])
def apply_list(self, zmin, zmax, ns, evaluation): 'RandomComplex[{zmin_, zmax_}, ns_]' expr = Expression('RandomComplex', Expression('List', zmin, zmax), ns) if Expression('RealNumberQ', zmin).evaluate(evaluation): zmin = Complex(zmin, 0.0) if Expression('RealNumberQ', zmax).evaluate(evaluation): zmax = Complex(zmax, 0.0) if not (isinstance(zmin, Complex) and isinstance(zmax, Complex)): return evaluation.message('RandomComplex', 'unifr', Expression('List', zmin, zmax)) min_value, max_value = zmin.to_python(), zmax.to_python() py_ns = ns.to_python() if not isinstance(py_ns, list): py_ns = [py_ns] if not all([isinstance(i, int) and i >= 0 for i in py_ns]): return evaluation.message('RandomComplex', 'array', ns, expr) with RandomEnv(evaluation) as rand: real = rand.randreal(min_value.real, max_value.real, py_ns) imag = rand.randreal(min_value.imag, max_value.imag, py_ns) return instantiate_elements(stack_along_inner_axis([real, imag]), lambda c: Complex(*c), d=2)
def apply(self, domain, size, evaluation): '''%(name)s[domain_, size_]''' if domain.get_head_name() == 'System`Rule': # elements and weights err, py_weights = self._weights_to_python(domain.leaves[0], evaluation) if py_weights is None: return err elements = domain.leaves[1].leaves if domain.leaves[1].get_head_name() != 'System`List' or len(py_weights) != len(elements): return evaluation.message(self.get_name(), 'wghtv', domain) elif domain.get_head_name() == 'System`List': # only elements py_weights = None elements = domain.leaves else: return evaluation.message(self.get_name(), 'lrwl', domain) err, py_size = self._size_to_python(domain, size, evaluation) if py_size is None: return err if not self._replace: # i.e. RandomSample? n_chosen = reduce(operator_mul, py_size, 1) if len(elements) < n_chosen: return evaluation.message('smplen', size, domain), None with RandomEnv(evaluation) as rand: return instantiate_elements( rand.randchoice(len(elements), size=py_size, replace=self._replace, p=py_weights), lambda i: elements[i])
def apply_list(self, rmin, rmax, ns, evaluation): 'RandomInteger[{rmin_, rmax_}, ns_?ListQ]' if not isinstance(rmin, Integer) or not isinstance(rmax, Integer): return evaluation.message('RandomInteger', 'unifr', Expression('List', rmin, rmax)) rmin, rmax = rmin.value, rmax.value result = ns.to_python() with RandomEnv(evaluation) as rand: return instantiate_elements(rand.randint(rmin, rmax, result), Integer)
def apply_list(self, xmin, xmax, ns, evaluation): 'RandomReal[{xmin_, xmax_}, ns_?ListQ]' if not (isinstance(xmin, (Real, Integer)) and isinstance(xmax, (Real, Integer))): return evaluation.message('RandomReal', 'unifr', Expression('List', xmin, xmax)) min_value, max_value = xmin.to_python(), xmax.to_python() result = ns.to_python() if not all([isinstance(i, int) and i >= 0 for i in result]): expr = Expression('RandomReal', Expression('List', xmin, xmax), ns) return evaluation.message('RandomReal', 'array', expr, ns) assert all([isinstance(i, int) for i in result]) with RandomEnv(evaluation) as rand: return instantiate_elements(rand.randreal(min_value, max_value, result), Real)
def apply_list(self, zmin, zmax, ns, evaluation): 'RandomComplex[{zmin_, zmax_}, ns_]' expr = Expression('RandomComplex', Expression('List', zmin, zmax), ns) min_value, max_value = self.to_complex(zmin, evaluation), self.to_complex(zmax, evaluation) if min_value is None or max_value is None: return evaluation.message('RandomComplex', 'unifr', Expression('List', zmin, zmax)) py_ns = ns.to_python() if not isinstance(py_ns, list): py_ns = [py_ns] if not all([isinstance(i, six.integer_types) and i >= 0 for i in py_ns]): return evaluation.message('RandomComplex', 'array', ns, expr) with RandomEnv(evaluation) as rand: real = rand.randreal(min_value.real, max_value.real, py_ns) imag = rand.randreal(min_value.imag, max_value.imag, py_ns) return instantiate_elements(stack_along_inner_axis([real, imag]), lambda c: Complex(*c), d=2)