def __new__(cls, *args, **options): components = {} # Check each arg and simultaneously learn the components for i, arg in enumerate(args): if not isinstance(arg, cls._expr_type): if isinstance(arg, Mul): arg = cls._mul_func(*(arg.args)) elif isinstance(arg, Add): arg = cls._add_func(*(arg.args)) else: raise TypeError( str(arg) + " cannot be interpreted correctly") # If argument is zero, ignore if arg == cls.zero: continue # Else, update components accordingly if hasattr(arg, "components"): for x in arg.components: components[x] = components.get(x, 0) + arg.components[x] temp = list(components.keys()) for x in temp: if components[x] == 0: del components[x] # Handle case of zero vector if len(components) == 0: return cls.zero # Build object newargs = [x * components[x] for x in components] obj = super().__new__(cls, *newargs, **options) if isinstance(obj, Mul): return cls._mul_func(*obj.args) assumptions = {'commutative': True} obj._assumptions = StdFactKB(assumptions) obj._components = components obj._sys = (list(components.keys()))[0]._sys return obj
def __new_stage2__(cls, name, **assumptions): if not isinstance(name, string_types): raise TypeError("name should be a string, not %s" % repr(type(name))) obj = Expr.__new__(cls) obj.name = name # TODO: Issue #8873: Forcing the commutative assumption here means # later code such as ``srepr()`` cannot tell whether the user # specified ``commutative=True`` or omitted it. To workaround this, # we keep a copy of the assumptions dict, then create the StdFactKB, # and finally overwrite its ``._generator`` with the dict copy. This # is a bit of a hack because we assume StdFactKB merely copies the # given dict as ``._generator``, but future modification might, e.g., # compute a minimal equivalent assumption set. tmp_asm_copy = assumptions.copy() # be strict about commutativity is_commutative = fuzzy_bool(assumptions.get('commutative', True)) assumptions['commutative'] = is_commutative obj._assumptions = StdFactKB(assumptions) obj._assumptions._generator = tmp_asm_copy # Issue #8873 return obj
def __new_stage2__(cls, name, **assumptions): assert isinstance(name, str), repr(type(name)) obj = Expr.__new__(cls) obj.name = name obj._assumptions = StdFactKB(assumptions) return obj