def nres(func): if isinstance(func, Lambda): return len(pack_if_not(func.expr)) elif isinstance(func, Functor): return func.nres elif isinstance(func, FunctionClass): return 1 else: raise TypeError
def __new__(cls, variable, expr, **kwargs): evaluate = kwargs.pop('evaluate', global_evaluate[0]) variable = repack_if_can(sympify(unpack_if_can(variable))) for v in pack_if_not(variable): if not is_Symbol(v): raise TypeError('variable is not a symbol or matrix symbol: %s' % v) if not is_Boolean(expr): raise TypeError('expression is not boolean or relational: %r' % expr) if evaluate: return Exist.eval(variable, expr) return Application.__new__(cls, variable, expr, **kwargs)
def variables(self): """get variable with tuple of set builder >>> from sympy import * >>> from symplus.strplus import init_mprinting >>> init_mprinting() >>> x, y = symbols('x y') >>> AbstractSet(x, abs(x)>1).variables (x,) >>> AbstractSet((x,), abs(x)>1).variables (x,) >>> AbstractSet((x,y), abs(x-y)>1).variables (x, y) >>> AbstractSet(x, x>y).variables (x,) """ return pack_if_not(self._args[0])
def solve_inv(func, *args): vars = symbols('a:%s'%narg(func)) vars = rename_variables_in(vars, free_symbols(func) | free_symbols(Tuple(*args))) exprs = pack_if_not(func(*vars)) if len(args) != len(exprs): raise ValueError try: solns = solve([expr - val for val, expr in zip(args, exprs)], vars) if isinstance(solns, list): solns = dict(zip(vars, solns[0])) if len(vars) == 1: return solns[vars[0]] else: return tuple(solns[var] for var in vars) except NotImplementedError: return None
def __new__(cls, variable, expr, **kwargs): """create AbstractSet by variable and expression >>> from sympy import * >>> from symplus.strplus import init_mprinting >>> init_mprinting() >>> x, y = symbols('x y') >>> AbstractSet(x, abs(x)>1) {x | |x| > 1} >>> AbstractSet((x,), abs(x)>1) {x | |x| > 1} >>> AbstractSet((x, y), abs(x-y)>1) {(x, y) | |x - y| > 1} >>> AbstractSet([x, y], abs(x-y)>1) {(x, y) | |x - y| > 1} >>> m, n = MatrixSymbol('m', 2, 2), MatrixSymbol('n', 2, 2) >>> AbstractSet(m, Eq(m[0,0]+m[1,1],0)) {m | 0 == m[0, 0] + m[1, 1]} >>> AbstractSet((m, x), Eq(det(m),x)) {(m, x) | x == ||m||} >>> AbstractSet(x, (x>1)&(x<3)) {x | (x < 3) /\ (x > 1)} >>> AbstractSet(x, x>y) {x | x > y} >>> AbstractSet(1, x>y) Traceback (most recent call last): ... TypeError: variable is not a symbol or matrix symbol: 1 >>> AbstractSet(x, x+y) Traceback (most recent call last): ... TypeError: expression is not boolean or relational: x + y """ variable = repack_if_can(sympify(unpack_if_can(variable))) for v in pack_if_not(variable): if not is_Symbol(v): raise TypeError("variable is not a symbol or matrix symbol: %s" % v) if not is_Boolean(expr): raise TypeError("expression is not boolean or relational: %r" % expr) return Set.__new__(cls, variable, expr, **kwargs)
def _contains(self, other): """ >>> from sympy import * >>> from symplus.strplus import init_mprinting >>> init_mprinting() >>> x, y, z = symbols('x y z') >>> AbstractSet(x, abs(x)>1)._contains(2) True >>> AbstractSet((x,y), abs(x-y)>1)._contains((3,1)) True >>> AbstractSet((x,y), abs(x-y)>1)._contains((x+3,x+1)) True >>> AbstractSet(x, x>y)._contains(z) z > y >>> AbstractSet(x, x>y)._contains(Matrix(2,2,[1,2,3,4])) False """ var = self.variables val = pack_if_not(other) if not type_match(var, val): return false return self.expr.xreplace(dict(zip(var, val)))
def reduce(funcs): i = 0 while i < len(funcs): if funcs[i] == Id: funcs = funcs[:i] + funcs[i+1:] elif isinstance(funcs[i], FunctionCompose): funcs = funcs[:i] + funcs[i].functions + funcs[i+1:] elif i-1 >= 0: if is_inverse_of(funcs[i-1], funcs[i]): funcs = funcs[:i-1] + funcs[i+1:] i = i - 2 elif hasattr(funcs[i-1], '_compose'): comp_funcs = funcs[i-1]._compose(funcs[i]) if comp_funcs is not None: funcs = funcs[:i-1] + (comp_funcs,) + funcs[i+1:] i = i - 1 elif isinstance(funcs[i-1], Lambda) and isinstance(funcs[i], Lambda): variables = rename_variables_in(funcs[i].variables, free_symbols(funcs[i-1])) expr = funcs[i].expr comp_funcs = Lambda(variables, funcs[i-1](*pack_if_not(expr))) funcs = funcs[:i-1] + (comp_funcs,) + funcs[i+1:] i = i - 1 i = i + 1 return funcs
def _contains(self, mem): if getattr(self.function, "is_invertible", False): mem_ = unpack_if_can(FunctionInverse(self.function)(*pack_if_not(mem))) return self.set._contains(mem_)
def variables(self): return pack_if_not(self._args[0])
def arguments(self): return pack_if_not(self._args[1])
def call(self, *args): apply_multivar = lambda a, f: f(*pack_if_not(a)) return reduce(apply_multivar, self.functions[::-1], args)