def __new__(cls, label, shape=None, **kw_args): from sympy import MatrixBase, NDimArray if isinstance(label, string_types): label = Symbol(label) elif isinstance(label, Symbol): pass elif isinstance(label, (MatrixBase, NDimArray)): return label elif isinstance(label, Iterable): return _sympify(label) else: label = _sympify(label) if is_sequence(shape): shape = Tuple(*shape) elif shape is not None: shape = Tuple(shape) offset = kw_args.pop('offset', S.Zero) strides = kw_args.pop('strides', None) if shape is not None: obj = Expr.__new__(cls, label, shape) else: obj = Expr.__new__(cls, label) obj._shape = shape obj._offset = offset obj._strides = strides obj._name = str(label) return obj
def __new__(cls, label, shape=None, *, offset=S.Zero, strides=None, **kw_args): from sympy.matrices.matrices import MatrixBase from sympy.tensor.array.ndim_array import NDimArray assumptions, kw_args = _filter_assumptions(kw_args) if isinstance(label, str): label = Symbol(label, **assumptions) elif isinstance(label, Symbol): assumptions = label._merge(assumptions) elif isinstance(label, (MatrixBase, NDimArray)): return label elif isinstance(label, Iterable): return _sympify(label) else: label = _sympify(label) if is_sequence(shape): shape = Tuple(*shape) elif shape is not None: shape = Tuple(shape) if shape is not None: obj = Expr.__new__(cls, label, shape) else: obj = Expr.__new__(cls, label) obj._shape = shape obj._offset = offset obj._strides = strides obj._name = str(label) IndexedBase._set_assumptions(obj, assumptions) return obj
def __new__(cls, label, shape=None, **kw_args): from sympy import MatrixBase, NDimArray assumptions, kw_args = _filter_assumptions(kw_args) if isinstance(label, string_types): label = Symbol(label) elif isinstance(label, Symbol): assumptions = label._merge(assumptions) label = Symbol(label.name) elif isinstance(label, (MatrixBase, NDimArray)): return label elif isinstance(label, Iterable): return _sympify(label) else: label = _sympify(label) if is_sequence(shape): shape = Tuple(*shape) elif shape is not None: shape = Tuple(shape) offset = kw_args.pop('offset', S.Zero) strides = kw_args.pop('strides', None) if shape is not None: obj = Expr.__new__(cls, label, shape) else: obj = Expr.__new__(cls, label) obj._shape = shape obj._offset = offset obj._strides = strides obj._name = str(label) IndexedBase._set_assumptions(obj, assumptions) return obj
def __new__(cls, label, range=None, **kw_args): if isinstance(label, basestring): label = Symbol(label, integer=True) label, range = map(sympify, (label, range)) if not label.is_integer: raise TypeError("Idx object requires an integer label") elif ordered_iter(range, include=Tuple): assert len(range) == 2, "Idx got range tuple with wrong length" for bound in range: if not (bound.is_integer or abs(bound) is S.Infinity): raise TypeError("Idx object requires integer bounds") args = label, Tuple(*range) elif isinstance(range, Expr): if not (range.is_integer or range is S.Infinity): raise TypeError("Idx object requires an integer dimension") args = label, Tuple(S.Zero, range - S.One) elif range: raise TypeError("range must be tuple or integer sympy expression") else: args = label, obj = Expr.__new__(cls, *args, **kw_args) return obj
def __new__(cls, e, z, z0, dir="+"): e = sympify(e) z = sympify(z) z0 = sympify(z0) if z0 is S.Infinity: dir = "-" elif z0 is S.NegativeInfinity: dir = "+" if (z0.has(z)): raise NotImplementedError("Limits approaching a variable point are" " not supported (%s -> %s)" % (z, z0)) if isinstance(dir, str): dir = Symbol(dir) elif not isinstance(dir, Symbol): raise TypeError("direction must be of type basestring or " "Symbol, not %s" % type(dir)) if str(dir) not in ('+', '-', '+-'): raise ValueError("direction must be one of '+', '-' " "or '+-', not %s" % dir) obj = Expr.__new__(cls) obj._args = (e, z, z0, dir) return obj
def __new__(cls, name, abbrev, **assumptions): obj = Expr.__new__(cls, **assumptions) assert isinstance(name, str), ` type(name) ` assert isinstance(abbrev, str), ` type(abbrev) ` obj.name = name obj.abbrev = abbrev return obj
def __new__(cls, function, *symbols, **assumptions): from sympy.integrals.integrals import _process_limits # Any embedded piecewise functions need to be brought out to the # top level so that integration can go into piecewise mode at the # earliest possible moment. function = piecewise_fold(sympify(function)) if function is S.NaN: return S.NaN if not symbols: raise ValueError("Summation variables must be given") limits, sign = _process_limits(*symbols) # Only limits with lower and upper bounds are supported; the indefinite Sum # is not supported if any(len(l) != 3 or None in l for l in limits): raise ValueError('Sum requires values for lower and upper bounds.') obj = Expr.__new__(cls, **assumptions) arglist = [sign*function] arglist.extend(limits) obj._args = tuple(arglist) return obj
def __new__(cls, label, range=None, **kw_args): from sympy.utilities.misc import filldedent if isinstance(label, basestring): label = Symbol(label, integer=True) label, range = map(sympify, (label, range)) if not label.is_integer: raise TypeError("Idx object requires an integer label.") elif is_sequence(range): if len(range) != 2: raise ValueError(filldedent(""" Idx range tuple must have length 2, but got %s""" % len(range))) for bound in range: if not (bound.is_integer or abs(bound) is S.Infinity): raise TypeError("Idx object requires integer bounds.") args = label, Tuple(*range) elif isinstance(range, Expr): if not (range.is_integer or range is S.Infinity): raise TypeError("Idx object requires an integer dimension.") args = label, Tuple(0, range - 1) elif range: raise TypeError(filldedent(""" The range must be an ordered iterable or integer SymPy expression.""")) else: args = label, obj = Expr.__new__(cls, *args, **kw_args) return obj
def __new__(cls, e, z, z0, dir="+"): e = sympify(e) z = sympify(z) z0 = sympify(z0) obj = Expr.__new__(cls) obj._args = (e, z, z0, dir) return obj
def __new__(cls, function, *symbols, **assumptions): # Any embedded piecewise functions need to be brought out to the # top level so that integration can go into piecewise mode at the # earliest possible moment. function = piecewise_fold(sympify(function)) if function is S.NaN: return S.NaN if symbols: limits, sign = _process_limits(*symbols) else: # no symbols provided -- let's compute full anti-derivative limits, sign = [Tuple(s) for s in function.free_symbols], 1 if len(limits) != 1: raise ValueError("specify integration variables to integrate %s" % function) while isinstance(function, Integral): # denest the integrand limits = list(function.limits) + limits function = function.function obj = Expr.__new__(cls, **assumptions) arglist = [sign*function] arglist.extend(limits) obj._args = tuple(arglist) obj.is_commutative = all(s.is_commutative for s in obj.free_symbols) return obj
def __new__(cls, name, abbrev, **assumptions): obj = Expr.__new__(cls, **assumptions) assert isinstance(name, str),`type(name)` assert isinstance(abbrev, str),`type(abbrev)` obj.name = name obj.abbrev = abbrev return obj
def __new__(cls, base, *args, **kw_args): from sympy.utilities.misc import filldedent from sympy.tensor.array.ndim_array import NDimArray from sympy.matrices.matrices import MatrixBase if not args: raise IndexException("Indexed needs at least one index.") if isinstance(base, (str, Symbol)): base = IndexedBase(base) elif not hasattr(base, '__getitem__') and not isinstance( base, IndexedBase): raise TypeError( filldedent(""" The base can only be replaced with a string, Symbol, IndexedBase or an object with a method for getting items (i.e. an object with a `__getitem__` method). """)) args = list(map(sympify, args)) if isinstance(base, (NDimArray, Iterable, Tuple, MatrixBase)) and all( [i.is_number for i in args]): if len(args) == 1: return base[args[0]] else: return base[args] obj = Expr.__new__(cls, base, *args, **kw_args) try: IndexedBase._set_assumptions(obj, base.assumptions0) except AttributeError: IndexedBase._set_assumptions(obj, {}) return obj
def __new__(cls, function, *symbols, **assumptions): # Any embedded piecewise functions need to be brought out to the # top level so that integration can go into piecewise mode at the # earliest possible moment. function = piecewise_fold(sympify(function)) if function is S.NaN: return S.NaN if symbols: limits, sign = _process_limits(*symbols) else: # no symbols provided -- let's compute full anti-derivative limits, sign = [Tuple(s) for s in function.free_symbols], 1 if len(limits) != 1: raise ValueError( "specify integration variables to integrate %s" % function) while isinstance(function, Integral): # denest the integrand limits = list(function.limits) + limits function = function.function obj = Expr.__new__(cls, **assumptions) arglist = [sign * function] arglist.extend(limits) obj._args = tuple(arglist) obj.is_commutative = all(s.is_commutative for s in obj.free_symbols) return obj
def __new__(cls, label, range=None, **kw_args): if isinstance(label, basestring): label = Symbol(label, integer=True) label, range = map(sympify, (label, range)) if not label.is_integer: raise TypeError("Idx object requires an integer label") elif is_sequence(range): assert len(range) == 2, "Idx got range tuple with wrong length" for bound in range: if not (bound.is_integer or abs(bound) is S.Infinity): raise TypeError("Idx object requires integer bounds") args = label, Tuple(*range) elif isinstance(range, Expr): if not (range.is_integer or range is S.Infinity): raise TypeError("Idx object requires an integer dimension") args = label, Tuple(S.Zero, range-S.One) elif range: raise TypeError("range must be ordered iterable or integer sympy expression") else: args = label, obj = Expr.__new__(cls, *args, **kw_args) return obj
def __new__(cls, term, *symbols, **assumptions): term = sympify(term) if term is S.NaN: return S.NaN if len(symbols) == 1: symbol = symbols[0] if isinstance(symbol, C.Equality): k = symbol.lhs a = symbol.rhs.start n = symbol.rhs.end elif is_sequence(symbol): k, a, n = symbol else: raise ValueError("Invalid arguments") k, a, n = map(sympify, (k, a, n)) else: raise NotImplementedError obj = Expr.__new__(cls, **assumptions) obj._args = (term, Tuple(k, a, n)) return obj
def __new__(cls, function, *symbols, **assumptions): # Any embedded piecewise functions need to be brought out to the # top level so that integration can go into piecewise mode at the # earliest possible moment. function = piecewise_fold(sympify(function)) if function.is_Number: if function is S.NaN: return S.NaN elif function is S.Infinity: return S.Infinity elif function is S.NegativeInfinity: return S.NegativeInfinity if symbols: limits = [] for V in symbols: if isinstance(V, Symbol): limits.append(Tuple(V)) continue elif isinstance(V, (tuple, list, Tuple)): V = flatten(V) newsymbol = sympify(V[0]) if len(V) == 3: if isinstance(newsymbol, Symbol): nlim = map(sympify, V[1:]) if V[1] is None and V[2] is not None: nlim = [V[2]] if V[2] is None and V[1] is not None: function = -function nlim = [V[1]] if V[1] is None and V[2] is None: nlim = [] limits.append( Tuple(newsymbol, *nlim )) continue elif len(V) == 1 or (len(V) == 2 and V[1] is None): if isinstance(newsymbol, Symbol): limits.append(Tuple(newsymbol)) continue elif len(V) == 2: if isinstance(newsymbol, Symbol): limits.append(Tuple(newsymbol,V[1])) continue raise ValueError("Invalid integration variable or limits: %s" % str(symbols)) else: # no symbols provided -- let's compute full anti-derivative limits = [Tuple(symb) for symb in function.atoms(Symbol)] if not limits: return function obj = Expr.__new__(cls, **assumptions) arglist = [function] arglist.extend(limits) obj._args = tuple(arglist) return obj
def __new__(cls, base, *args, **kw_args): if not args: raise IndexException("Indexed needs at least one index") if isinstance(base, (basestring, Symbol)): base = IndexedBase(base) elif not isinstance(base, IndexedBase): raise TypeError("Indexed expects string, Symbol or IndexedBase as base") return Expr.__new__(cls, base, *args, **kw_args)
def __new__(cls, function, *symbols, **assumptions): from sympy.integrals.integrals import _process_limits # Any embedded piecewise functions need to be brought out to the # top level so that integration can go into piecewise mode at the # earliest possible moment. function = piecewise_fold(sympify(function)) if function is S.NaN: return S.NaN if not symbols: raise ValueError("Summation variables must be given") limits, sign = _process_limits(*symbols) # Only limits with lower and upper bounds are supported; the indefinite Sum # is not supported if any(len(l) != 3 or None in l for l in limits): raise ValueError('Sum requires values for lower and upper bounds.') obj = Expr.__new__(cls, **assumptions) arglist = [sign * function] arglist.extend(limits) obj._args = tuple(arglist) return obj
def __new__(cls, base, *args, **kw_args): assert (isinstance(base, VectorField)) if not args: raise IndexException("Indexed needs at least one index.") return Expr.__new__(cls, base, *args, **kw_args)
def __new__(cls, space, name): if not isinstance(space, VectorFunctionSpace): raise ValueError('Expecting a VectorFunctionSpace') obj = Expr.__new__(cls) obj._space = space obj._name = name return obj
def __new__(cls, base, *args, **kw_args): if isinstance(base, VectorTestFunction): pass elif isinstance(base, Add): return Add(*[cls(b, *args, **kw_args) for b in base.args]) elif isinstance(base, Mul): scalar_types = (*_coeffs_registery, ScalarTestFunction) scalars = [s for s in base.args if isinstance(s, scalar_types)] others = [s for s in base.args if s not in scalars] return Mul(*scalars) * Mul( *[cls(b, *args, **kw_args) for b in others]) elif isinstance(base, Trace): expr = cls(base.expr, *args, **kw_args) boundary = base.boundary order = base.order return Trace(expr, boundary, order) else: raise ValueError( 'Expecting VectorTestFunction, Trace, or Add/Mul object') if not args: raise IndexException("Indexed needs at least one index.") return Expr.__new__(cls, base, *args, **kw_args)
def __new__(cls, arg): if hasattr(arg, 'adjoint'): obj = arg.adjoint() elif hasattr(arg, 'conjugate') and hasattr(arg, 'transpose'): obj = arg.conjugate().transpose() if obj is not None: return obj return Expr.__new__(cls, arg)
def _new(cls, poly, index): """Construct new ``RootOf`` object from raw data. """ obj = Expr.__new__(cls) obj.poly = poly obj.index = index return obj
def __new__(cls, arg): if hasattr(arg, "adjoint"): obj = arg.adjoint() elif hasattr(arg, "conjugate") and hasattr(arg, "transpose"): obj = arg.conjugate().transpose() if obj is not None: return obj return Expr.__new__(cls, arg)
def eval_all(expr: Expr): all_sym = expr.free_symbols replacements = [] for sym in all_sym: if isinstance(sym, DummyVariable): replacements.append((sym, sym.value)) if len(replacements): expr = expr.subs(replacements) return expr
def _new(cls, poly, func, auto=True): """Construct new raw ``RootSum`` instance. """ obj = Expr.__new__(cls) obj.poly = poly obj.fun = func obj.auto = auto return obj
def __new__(cls, name, n, m): n, m = map(sympify, (n, m)) from sympy import MatrixBase if isinstance(name, (MatrixBase,)): if n.is_Integer and m.is_Integer: return name[n, m] name = sympify(name) obj = Expr.__new__(cls, name, n, m) return obj
def __new__(cls, name, n, m): n, m = map(sympify, (n, m)) from sympy import MatrixBase if isinstance(name, (MatrixBase, )): if n.is_Integer and m.is_Integer: return name[n, m] name = sympify(name) obj = Expr.__new__(cls, name, n, m) return obj
def __new__(cls, label, shape=None, **kw_args): if isinstance(label, string_types): label = Symbol(label) elif isinstance(label, Symbol): pass else: raise TypeError("Base label should be a string or Symbol.") if is_sequence(shape): shape = Tuple(*shape) else: shape = sympify(shape) if shape is not None: obj = Expr.__new__(cls, label, shape, **kw_args) else: obj = Expr.__new__(cls, label, **kw_args) obj._shape = shape return obj
def __new__(cls, f, x=None): if not isinstance(f, Poly): f = Poly(f, x) elif x is not None: raise SymbolsError("Redundant symbols were given") if f.is_multivariate: raise ValueError('multivariate polynomial') return Expr.__new__(cls, f)
def __new__(cls, label, shape=None, **kw_args): if isinstance(label, basestring): label = Symbol(label) obj = Expr.__new__(cls, label, **kw_args) if is_sequence(shape): obj._shape = Tuple(*shape) else: obj._shape = shape return obj
def __new__(cls, label, shape=None, **kw_args): if isinstance(label, string_types): label = Symbol(label) elif isinstance(label, Symbol): pass else: label = _sympify(label) if is_sequence(shape): shape = Tuple(*shape) elif shape is not None: shape = Tuple(shape) if shape is not None: obj = Expr.__new__(cls, label, shape, **kw_args) else: obj = Expr.__new__(cls, label, **kw_args) obj._shape = shape return obj
def __new__(cls, label, shape=None, **kw_args): if isinstance(label, basestring): label = Symbol(label) obj = Expr.__new__(cls, label, **kw_args) if isinstance(shape, (tuple, list)): obj._shape = Tuple(*shape) else: obj._shape = shape return obj
def __new__(cls, base, *args, **kw_args): if not args: raise IndexException("Indexed needs at least one index") if isinstance(base, (basestring, Symbol)): base = IndexedBase(base) elif not isinstance(base, IndexedBase): raise TypeError("Indexed expects string, Symbol or IndexedBase as base") # FIXME: 2.4 compatibility args = map(_ensure_Idx, args) # args = tuple([ a if isinstance(a, Idx) else Idx(a) for a in args ]) return Expr.__new__(cls, base, *args, **kw_args)
def __new__(cls, function, *symbols, **assumptions): # Any embedded piecewise functions need to be brought out to the # top level so that integration can go into piecewise mode at the # earliest possible moment. function = piecewise_fold(sympify(function)) if function is S.NaN: return S.NaN symbols = list(symbols) if not symbols: # no symbols provided -- let's compute full anti-derivative symbols = sorted(function.free_symbols, Basic.compare) if not symbols: raise ValueError('An integration variable is required.') while isinstance(function, Integral): # denest the integrand symbols = list(function.limits) + symbols function = function.function limits = [] for V in symbols: if isinstance(V, Symbol): limits.append(Tuple(V)) continue elif isinstance(V, (tuple, list, Tuple)): V = sympify(flatten(V)) if V[0].is_Symbol: newsymbol = V[0] if len(V) == 3: if V[1] is None and V[2] is not None: nlim = [V[2]] elif V[1] is not None and V[2] is None: function = -function nlim = [V[1]] elif V[1] is None and V[2] is None: nlim = [] else: nlim = V[1:] limits.append(Tuple(newsymbol, *nlim )) continue elif len(V) == 1 or (len(V) == 2 and V[1] is None): limits.append(Tuple(newsymbol)) continue elif len(V) == 2: limits.append(Tuple(newsymbol, V[1])) continue raise ValueError("Invalid integration variable or limits: %s" % str(symbols)) obj = Expr.__new__(cls, **assumptions) obj._args = tuple([function] + limits) obj.is_commutative = all(s.is_commutative for s in obj.free_symbols) return obj
def __new__(cls, label, shape=None, **kw_args): if not isinstance(label, (basestring, Symbol)): raise TypeError("Base label should be a string or Symbol.") label = sympify(label) obj = Expr.__new__(cls, label, **kw_args) if is_sequence(shape): obj._shape = Tuple(*shape) else: obj._shape = sympify(shape) return obj
def __new__(cls, base, *args, **kw_args): from sympy.utilities.misc import filldedent if not args: raise IndexException("Indexed needs at least one index.") if isinstance(base, (basestring, Symbol)): base = IndexedBase(base) elif not isinstance(base, IndexedBase): raise TypeError(filldedent(""" Indexed expects string, Symbol or IndexedBase as base.""")) return Expr.__new__(cls, base, *args, **kw_args)
def __new__(cls, base, *args, **kw_args): from sympy.solvers.solvers import _filldedent if not args: raise IndexException("Indexed needs at least one index.") if isinstance(base, (basestring, Symbol)): base = IndexedBase(base) elif not isinstance(base, IndexedBase): raise TypeError(_filldedent(""" Indexed expects string, Symbol or IndexedBase as base.""")) return Expr.__new__(cls, base, *args, **kw_args)
def __new__(cls, name, n, m): n, m = map(_sympify, (n, m)) from sympy import MatrixBase if isinstance(name, (MatrixBase,)): if n.is_Integer and m.is_Integer: return name[n, m] if isinstance(name, string_types): name = Symbol(name) name = _sympify(name) obj = Expr.__new__(cls, name, n, m) return obj
def __new__(cls, base, *args, **kw_args): from sympy.utilities.misc import filldedent if not args: raise IndexException("Indexed needs at least one index.") if isinstance(base, (string_types, Symbol)): base = IndexedBase(base) elif not hasattr(base, '__getitem__') and not isinstance(base, IndexedBase): raise TypeError(filldedent(""" Indexed expects string, Symbol, or IndexedBase as base.""")) args = list(map(sympify, args)) return Expr.__new__(cls, base, *args, **kw_args)
def __new__(cls, f, *args, **flags): if not hasattr(f, '__call__'): raise TypeError("%s is not a callable object" % f) roots = RootsOf(*args) if not flags.get('evaluate', True): return Expr.__new__(cls, f, roots) else: if roots.count == 0: return S.Zero else: result = [] for root in roots.exact_roots(): result.append(f(root)) if len(result) < roots.count: result.append(Expr.__new__(cls, f, roots)) return Add(*result)
def __new__(cls, e, z, z0, dir="+"): e = sympify(e) z = sympify(z) z0 = sympify(z0) if isinstance(dir, basestring): dir = Symbol(dir) elif not isinstance(dir, Symbol): raise TypeError("direction must be of type basestring or Symbol, not %s" % type(dir)) if str(dir) not in ("+", "-"): raise ValueError("direction must be either '+' or '-', not %s" % dir) obj = Expr.__new__(cls) obj._args = (e, z, z0, dir) return obj
def __new__(cls, expr, *symbols, **assumptions): expr = sympify(expr).expand() if expr is S.NaN: return S.NaN if symbols: symbols = map(sympify, symbols) if not all(isinstance(s, Symbol) for s in symbols): raise NotImplementedError( 'Order at points other than 0 not supported.') else: symbols = list(expr.free_symbols) if expr.is_Order: new_symbols = list(expr.variables) for s in symbols: if s not in new_symbols: new_symbols.append(s) if len(new_symbols) == len(expr.variables): return expr symbols = new_symbols elif symbols: if expr.is_Add: lst = expr.extract_leading_order(*symbols) expr = Add(*[f.expr for (e, f) in lst]) elif expr: if len(symbols) > 1 or expr.is_commutative is False: # TODO # We cannot use compute_leading_term because that only # works in one symbol. expr = expr.as_leading_term(*symbols) else: expr = expr.compute_leading_term(symbols[0]) terms = expr.as_coeff_mul(*symbols)[1] s = set(symbols) expr = Mul(*[t for t in terms if s & t.free_symbols]) if expr is S.Zero: return expr elif not expr.has(*symbols): expr = S.One # create Order instance: symbols.sort(key=cmp_to_key(Basic.compare)) obj = Expr.__new__(cls, expr, *symbols, **assumptions) return obj
def __new__(cls, function, argument, **kwargs): evaluate = kwargs.pop('evaluate', global_evaluate[0]) argument = repack_if_can(sympify(unpack_if_can(argument))) if not is_Function(function): raise TypeError('function is not a FunctionClass, Functor or Lambda: %s'%function) if evaluate: function, argument = Apply.reduce(function, argument) if function == Id: return argument else: return Expr.__new__(cls, function, argument, **kwargs)
def _new(cls, poly, index): """Construct new ``CRootOf`` object from raw data. """ obj = Expr.__new__(cls) obj.poly = PurePoly(poly) obj.index = index try: _reals_cache[obj.poly] = _reals_cache[poly] _complexes_cache[obj.poly] = _complexes_cache[poly] except KeyError: pass return obj