def __init__(self, config=None, **kwargs): """Perform initialization""" super(DependentSampler, self).__init__(config, **kwargs) # Construct the 'depends' list (overwrites whatever was provided, as this does it better!) try: parsed = parse(self.config['formula']) self.config['depends'] = list(parsed.variables_used) except CalcError: raise ConfigError("Formula error in dependent sampling formula: " + self.config["formula"])
def get_used_vars(expressions): """ Get the variables used in expressions Arguments: expressions: an iterable collection of expressions Returns: vars_used ({str}): set of variables used """ is_empty = lambda x: x is None or x.strip() == '' expressions = [expr for expr in expressions if not is_empty(expr)] # Pre-parse all expressions (these all get cached) parsed_expressions = [parse(expr) for expr in expressions] # Create a list of all variables used in the expressions vars_used = set().union( *[p.variables_used for p in parsed_expressions]) return vars_used
def get_limits_and_funcs(self, expression, lower_str, upper_str, varscope, funcscope): """ Evals lower/upper limits and gets the functions used in limits and integrand/summand. """ lower, lower_used = evaluator(lower_str, variables=varscope, functions=funcscope, suffixes=self.suffixes, allow_inf=True) upper, upper_used = evaluator(upper_str, variables=varscope, functions=funcscope, suffixes=self.suffixes, allow_inf=True) expression_used = parse(expression) used_funcs = lower_used.functions_used.union( upper_used.functions_used, expression_used.functions_used) return lower, upper, used_funcs