(self, factors=None)
Initialize Factors from dict or expr.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x
        >>> from sympy import I
        >>> e = 2*x**3
        >>> Factors(e)
        Factors({2: 1, x: 3})
        >>> Factors(e.as_powers_dict())
        Factors({2: 1, x: 3})
        >>> f = _
        >>> f.factors  # underlying dictionary
        {2: 1, x: 3}
        >>> f.gens  # base of each factor
        frozenset([2, x])
        >>> Factors(0)
        Factors({0: 1})
        >>> Factors(I)
        Factors({I: 1})

        Notes
        =====

        Although a dictionary can be passed, only minimal checking is
        performed: powers of -1 and I are made canonical.
Initialize Factors from dict or expr.
Initialize Factors from dict or expr .
def __init__(self, factors=None):  # Factors
        """Initialize Factors from dict or expr.

        Examples
        ========

        >>> from sympy.core.exprtools import Factors
        >>> from sympy.abc import x
        >>> from sympy import I
        >>> e = 2*x**3
        >>> Factors(e)
        Factors({2: 1, x: 3})
        >>> Factors(e.as_powers_dict())
        Factors({2: 1, x: 3})
        >>> f = _
        >>> f.factors  # underlying dictionary
        {2: 1, x: 3}
        >>> f.gens  # base of each factor
        frozenset([2, x])
        >>> Factors(0)
        Factors({0: 1})
        >>> Factors(I)
        Factors({I: 1})

        Notes
        =====

        Although a dictionary can be passed, only minimal checking is
        performed: powers of -1 and I are made canonical.

        """
        if isinstance(factors, (SYMPY_INTS, float)):
            factors = S(factors)

        if isinstance(factors, Factors):
            factors = factors.factors.copy()
        elif factors is None or factors is S.One:
            factors = {}
        elif factors is S.Zero or factors == 0:
            factors = {S.Zero: S.One}
        elif isinstance(factors, Number):
            n = factors
            factors = {}
            if n < 0:
                factors[S.NegativeOne] = S.One
                n = -n
            if n is not S.One:
                if n.is_Float or n.is_Integer or n is S.Infinity:
                    factors[n] = S.One
                elif n.is_Rational:
                    # since we're processing Numbers, the denominator is
                    # stored with a negative exponent; all other factors
                    # are left .
                    if n.p != 1:
                        factors[Integer(n.p)] = S.One
                    factors[Integer(n.q)] = S.NegativeOne
                else:
                    raise ValueError('Expected Float|Rational|Integer, not %s' % n)
        elif isinstance(factors, Basic) and not factors.args:
            factors = {factors: S.One}
        elif isinstance(factors, Expr):
            c, nc = factors.args_cnc()
            i = c.count(I)
            for _ in range(i):
                c.remove(I)
            factors = dict(Mul._from_args(c).as_powers_dict())
            if i:
                factors[I] = S.One*i
            if nc:
                factors[Mul(*nc, evaluate=False)] = S.One
        else:
            factors = factors.copy()  # /!\ should be dict-like

            # tidy up -/+1 and I exponents if Rational

            handle = []
            for k in factors:
                if k is I or k in (-1, 1):
                    handle.append(k)
            if handle:
                i1 = S.One
                for k in handle:
                    if not _isnumber(factors[k]):
                        continue
                    i1 *= k**factors.pop(k)
                if i1 is not S.One:
                    for a in i1.args if i1.is_Mul else [i1]:  # at worst, -1.0*I*(-1)**e
                        if a is S.NegativeOne:
                            factors[a] = S.One
                        elif a is I:
                            factors[I] = S.One
                        elif a.is_Pow:
                            if S.NegativeOne not in factors:
                                factors[S.NegativeOne] = S.Zero
                            factors[S.NegativeOne] += a.exp
                        elif a == 1:
                            factors[a] = S.One
                        elif a == -1:
                            factors[-a] = S.One
                            factors[S.NegativeOne] = S.One
                        else:
                            raise ValueError('unexpected factor in i1: %s' % a)

        self.factors = factors
        try:
            self.gens = frozenset(factors.keys())
        except AttributeError:
            raise TypeError('expecting Expr or dictionary')
def __init__ ( self , factors = None ) : # Factors if isinstance ( factors , ( SYMPY_INTS , float ) ) : factors = S ( factors ) if isinstance ( factors , Factors ) : factors = factors . factors . copy ( ) elif factors is None or factors is S . One : factors = { } elif factors is S . Zero or factors == 0 : factors = { S . Zero : S . One } elif isinstance ( factors , Number ) : n = factors factors = { } if n < 0 : factors [ S . NegativeOne ] = S . One n = - n if n is not S . One : if n . is_Float or n . is_Integer or n is S . Infinity : factors [ n ] = S . One elif n . is_Rational : # since we're processing Numbers, the denominator is # stored with a negative exponent; all other factors # are left . if n . p != 1 : factors [ Integer ( n . p ) ] = S . One factors [ Integer ( n . q ) ] = S . NegativeOne else : raise ValueError ( 'Expected Float|Rational|Integer, not %s' % n ) elif isinstance ( factors , Basic ) and not factors . args : factors = { factors : S . One } elif isinstance ( factors , Expr ) : c , nc = factors . args_cnc ( ) i = c . count ( I ) for _ in range ( i ) : c . remove ( I ) factors = dict ( Mul . _from_args ( c ) . as_powers_dict ( ) ) if i : factors [ I ] = S . One * i if nc : factors [ Mul ( * nc , evaluate = False ) ] = S . One else : factors = factors . copy ( ) # /!\ should be dict-like # tidy up -/+1 and I exponents if Rational handle = [ ] for k in factors : if k is I or k in ( - 1 , 1 ) : handle . append ( k ) if handle : i1 = S . One for k in handle : if not _isnumber ( factors [ k ] ) : continue i1 *= k ** factors . pop ( k ) if i1 is not S . One : for a in i1 . args if i1 . is_Mul else [ i1 ] : # at worst, -1.0*I*(-1)**e if a is S . NegativeOne : factors [ a ] = S . One elif a is I : factors [ I ] = S . One elif a . is_Pow : if S . NegativeOne not in factors : factors [ S . NegativeOne ] = S . Zero factors [ S . NegativeOne ] += a . exp elif a == 1 : factors [ a ] = S . One elif a == - 1 : factors [ - a ] = S . One factors [ S . NegativeOne ] = S . One else : raise ValueError ( 'unexpected factor in i1: %s' % a ) self . factors = factors try : self . gens = frozenset ( factors . keys ( ) ) except AttributeError : raise TypeError ( 'expecting Expr or dictionary' )