예제 #1
0
    def cancelation(self, term_to_cancel, **defaults_config):
        '''
        Deduce and return an equality between self and a form in which
        the given operand has been canceled on the numerator and
        denominator.  For example,
        [(a*b)/(b*c)].cancelation(b) would return
        (a*b)/(b*c) = a / c.
        Assumptions or previous work might be required to establish
        that the term_to_cancel is non-zero.
        '''
        from proveit.numbers import Mult, one
        expr = self
        eq = TransRelUpdater(expr)

        if self.numerator == self.denominator == term_to_cancel:
            # x/x = 1
            from . import frac_cancel_complete
            return frac_cancel_complete.instantiate({x: term_to_cancel})

        if term_to_cancel != self.numerator:
            # try to catch Exp objects here as well?
            # after all, Exp(term_to_cancel, n) has factors!
            if (not isinstance(self.numerator, Mult)
                    or term_to_cancel not in self.numerator.operands):
                raise ValueError("%s not in the numerator of %s" %
                                 (term_to_cancel, self))
            # Factor the term_to_cancel from the numerator to the left.
            expr = eq.update(expr.inner_expr().numerator.factorization(
                term_to_cancel,
                group_factors=True,
                group_remainder=True,
                preserve_all=True))
        if term_to_cancel != self.denominator:
            if (not isinstance(self.denominator, Mult)
                    or term_to_cancel not in self.denominator.operands):
                raise ValueError("%s not in the denominator of %s" %
                                 (term_to_cancel, self))
            # Factor the term_to_cancel from the denominator to the left.
            expr = eq.update(expr.inner_expr().denominator.factorization(
                term_to_cancel,
                group_factors=True,
                group_remainder=True,
                preserve_all=True))
        if expr.numerator == expr.denominator == term_to_cancel:
            # Perhaps it reduced to the trivial x/x = 1 case via
            # auto-simplification.
            expr = eq.update(expr.cancelation(term_to_cancel))
            return eq.relation
        else:
            # (x*y) / (x*z) = y/z with possible automatic reductions
            # via 1 eliminations.
            from . import frac_cancel_left
            replacements = list(defaults.replacements)
            if expr.numerator == term_to_cancel:
                numer_prod = Mult(term_to_cancel, one)
                _y = one
                replacements.append(
                    numer_prod.one_elimination(1,
                                               preserve_expr=term_to_cancel))
            else:
                _y = expr.numerator.operands[1]
            if expr.denominator == term_to_cancel:
                denom_prod = Mult(term_to_cancel, one)
                _z = one
                replacements.append(
                    denom_prod.one_elimination(1,
                                               preserve_expr=term_to_cancel))
            else:
                _z = expr.denominator.operands[1]
            expr = eq.update(
                frac_cancel_left.instantiate({
                    x: term_to_cancel,
                    y: _y,
                    z: _z
                },
                                             replacements=replacements,
                                             preserve_expr=expr))
            return eq.relation
예제 #2
0
def complex_polar_coordinates(expr,
                              *,
                              radius_must_be_nonneg=True,
                              nonneg_radius_preferred=True,
                              do_include_unit_length_reduction=True,
                              reductions=None):
    '''
    Given an expression, expr, of the complex number polar form,
        r * exp(i * theta),
    or something obviously equivalent to this, where r and theta are 
    Real (and r is preferably RealNonNeg) under the given assumptions, 
    return
        (r, theta)
    as a tuple pair. If defaults.automation=False, the r and theta must
    already be known to be RealNonNeg and Real respectively. If
    defaults.automation=True, we may attempt to prove these through 
    automation.
    
    If radius_must_be_nonneg and nonneg_radius_preferred are False, 
    we won't worry about ensuring that r is non-negative (so the result
    can be ambiguous).  If radius_must_be_nonneg is True, a ValueError
    will be raised if we can't convert to a form where r is known to be
    non-negative.
    
    If expr is not exactly in this complex number polar form and
    'reductions' is provided as a set, add to the 'reductions' set
    an equation that equates the exact form on the left with the
    original form on the right.  This may be useful to use as
    'reductions' in instantiations of theorems that employ the
    complex number polar form so it may perform proper reductions
    to the desired form.  For example, if expr=5 is provided,
    the added reduction will be
        5 * exp(i * 0) = 5.
    
    If do_include_unit_length_reduction is True,  we will included
    reductions so that it will reduce from the unit length
    form as well.  For example, if expr=1 is provided, the added
    reductions will be
        exp(i * 0) = 1
        1 * 1 = 1.
    This also works in a way that cascades when reducing from the
    general polar form:
        1 * exp(i * 0) = 1 * 1 = 1
    
    Raise ValueError if the expr is not obviously equivalent to a
    complex number polar form.

    Also see unit_length_complex_polar_angle.
    '''
    from . import complex_polar_negation, complex_polar_radius_negation
    from proveit.logic import InSet, Equals
    from proveit.numbers import deduce_in_number_set, deduce_number_set
    from proveit.numbers import zero, one, e, i, pi
    from proveit.numbers import Real, RealNonPos, RealNonNeg, Complex
    from proveit.numbers import Add, LessEq, Neg, Mult, Exp
    orig_expr = expr
    automation = defaults.automation
    simplify = defaults.auto_simplify
    if reductions is None: reductions = set()

    def add_reduction(reduction, _radius, _theta):
        '''
        Add the given reduction.  First check that its left and
        rights sides are as expected: the left should be the polar
        form and the right should be the original expression.
        '''
        polar_form = Mult(_radius, Exp(e, Mult(i, _theta)))
        assert (isinstance(reduction, Judgment)
                and isinstance(reduction.expr, Equals)
                and reduction.lhs == polar_form and reduction.rhs
                == orig_expr), ("Reduction, %s, not a judgement "
                                "for %s = %s" %
                                (reduction, polar_form, orig_expr))
        if do_include_unit_length_reduction and _radius == one:
            # As a unit length complex number, let's include the
            # reduction from the unit length form in case a unit length
            # formula is applied (cover the bases).
            # The 'automation' allowed here is negligible (assuming
            # we have already proven appropriate set membership by this
            # point).
            reductions.add(reduction.inner_expr().lhs.eliminate_one(
                0, automation=True))
            # But prepare for a multi-stage reduction:
            # 1 * exp[i * theta] = 1 * orig_expr = orig_expr
            reductions.add(
                Mult(one, orig_expr).one_elimination(0, automation=True))
        elif reduction.lhs != reduction.rhs:
            reductions.add(reduction)

    def raise_not_valid_form(extra_msg=None):
        if extra_msg is None: extra_msg = ""
        raise ValueError("%s not in a form that is obviously "
                         "reducible from an r * exp(i*theta) form. %s" %
                         (orig_expr, extra_msg))

    if (isinstance(expr, Exp)
            or (isinstance(expr, Neg) and isinstance(expr.operand, Exp))):
        # exp(i * theta) reduced from 1 * exp(i * theta).
        # or exp(i * (theta + pi)) reduced from -exp(i * theta).
        inner_reductions = set()
        _theta = unit_length_complex_polar_angle(expr,
                                                 reductions=inner_reductions)
        deduce_in_number_set(_theta, Complex)
        deduce_in_number_set(Mult(i, _theta), Complex)
        deduce_in_number_set(Exp(e, Mult(i, _theta)), Complex)
        _r = one
        expr = Mult(_r, Exp(e, Mult(i, _theta)))
        # reduction: 1*exp(i * theta) = exp(i * theta)
        reduction = expr.one_elimination(0, preserve_all=True)
        # reduction: 1*exp(i * theta) = orig_expr
        if len(inner_reductions) > 0:
            reduction = reduction.inner_expr().rhs.substitute(
                inner_reductions.pop().rhs, preserve_all=True)
        # Add the reduction and return the coordinates.
        add_reduction(reduction, _r, _theta)
        return (_r, _theta)
    elif isinstance(expr, Neg):
        # expr = -(r*exp(i*theta0)) = r*exp(i*(theta0 + pi))
        inner_reductions = set()
        # obtain the theta of the negated expression.
        _r, _theta0 = complex_polar_coordinates(
            expr.operand,
            radius_must_be_nonneg=radius_must_be_nonneg,
            nonneg_radius_preferred=nonneg_radius_preferred,
            reductions=inner_reductions)
        # theta = theta0 + pi
        _theta = Add(_theta0, pi)
        if defaults.auto_simplify:
            # simplify theta
            theta_simplification = _theta.simplification()
            inner_reductions.add(theta_simplification)
            _theta = theta_simplification.rhs
        deduce_in_number_set(_theta, Complex)
        deduce_in_number_set(Mult(i, _theta), Complex)
        deduce_in_number_set(Exp(e, Mult(i, _theta)), Complex)
        # reduction: r*exp(i*theta) = orig_expr [via -(r*exp(i*theta0))]
        reduction = complex_polar_negation.instantiate(
            {
                r: _r,
                theta: _theta0
            },
            replacements=inner_reductions,
            auto_simplify=False)
        # Add the reduction and return the coordinates.
        add_reduction(reduction, _r, _theta)
        return (_r, _theta)

    # Search for an exponentiation factor with base of 'e' and an
    # imaginary number in the exponent.
    complex_exp_factor_idx = None
    if isinstance(expr, Mult):
        i_factor_idx = None
        for idx, factor in enumerate(expr.factors):
            if isinstance(factor, Exp) and factor.base == e:
                # exp(x) type factor; check for imaginary number in
                # exponent.
                contains_imaginary_factor = False
                sub_expr = factor.exponent
                if isinstance(sub_expr, Neg):
                    sub_expr = sub_expr.operand
                if isinstance(sub_expr, Mult):
                    if i in sub_expr.operands.entries:
                        contains_imaginary_factor = True
                else:
                    contains_imaginary_factor = (sub_expr == i)
                if contains_imaginary_factor:
                    # Found imaginary number in an exponent.
                    if ((complex_exp_factor_idx is not None)
                            or (i_factor_idx is not None)):
                        # We already have an imaginary number in
                        # an exponent.  We can only have one.
                        raise_not_valid_form()
                    complex_exp_factor_idx = idx
                    deduce_in_number_set(sub_expr, Complex)
    if complex_exp_factor_idx is None:
        # No exp(i theta) factor.  Let's multiply by exp(i * 0).
        exp_i0 = Exp(e, Mult(i, zero))
        expr = Mult(expr, exp_i0)
        inner_reductions = set()
        _r, _theta = complex_polar_coordinates(
            expr,
            radius_must_be_nonneg=radius_must_be_nonneg,
            nonneg_radius_preferred=nonneg_radius_preferred,
            do_include_unit_length_reduction=False,
            reductions=inner_reductions)
        assert _theta == zero
        deduce_in_number_set(exp_i0, Complex)
        # reduction: r * exp(i * theta) = orig_expr * exp(i * 0)
        if len(inner_reductions) > 0:
            reduction = inner_reductions.pop()
        else:
            reduction = Equals(expr, expr).conclude_via_reflexivity()
        # reduction: r * exp(i * theta) = orig_expr
        reduction = reduction.inner_expr().rhs.simplify(
            preserve_expr=orig_expr)
        add_reduction(reduction, _r, _theta)
        return (_r, _theta)

    # expr in ... * exp(... * i * ...) * ... form
    # Obtain the theta from exp(... * i * ...) = exp[i * theta0].
    inner_reductions = set()
    _theta0 = unit_length_complex_polar_angle(
        expr.factors[complex_exp_factor_idx], reductions=inner_reductions)
    expr = Mult(*expr.factors.entries[:complex_exp_factor_idx],
                Exp(e, Mult(i, _theta0)),
                *expr.factors.entries[complex_exp_factor_idx + 1:])
    # reduction: ... * expr[i * theta0] * ... = orig_expr
    if len(inner_reductions) > 0:
        reduction = expr.inner_expr().operands[1].substitution(
            inner_reductions.pop().rhs, preserve_all=True)
    else:
        reduction = Equals(expr, expr).conclude_via_reflexivity()
    if not expr.operands.is_double() or complex_exp_factor_idx != 1:
        # Pull the exp(i*theta) type factor to the right.
        # reduction: r0 * exp(i * theta0) = orig_expr
        for factor in expr.factors:
            # Deduce the factors are complex numbers ahead of time
            # in case automation is disabled.
            deduce_in_number_set(factor, Complex)
        reduction = reduction.inner_expr().lhs.factor(complex_exp_factor_idx,
                                                      pull='right',
                                                      group_remainder=True,
                                                      preserve_all=True)
        expr = reduction.lhs
    # expr: r0 * exp(i * theta0)
    assert expr.operands.is_double() and isinstance(expr.operands[1], Exp)
    # Check that r0 is real and that we know it's relation with zero.
    _r0 = expr.operands[0]
    _r0_ns = deduce_number_set(_r0).domain
    if Real.includes(_r0_ns):
        InSet(_r0, Real).prove()
    else:
        raise_not_valid_form("%s not known to be real." % _r0)
    is_known_nonneg = RealNonNeg.includes(_r0_ns)
    is_known_nonpos = RealNonPos.includes(_r0_ns)
    if radius_must_be_nonneg:
        # We must know the relationship between r0 and 0 so we
        # can ensure r is non-negative.
        if not nonneg_radius_preferred:
            ValueError("nonneg_radius_preferred must be True if "
                       "radius_must_be_nonneg is True.")
        if not (is_known_nonneg or is_known_nonpos):
            raise_not_valid_form("Relation of %s to 0 is unknown and "
                                 "radius_must_be_nonneg is True." % _r0)
    if nonneg_radius_preferred and is_known_nonpos:
        # r0 <= 0, so we must negate it and add pi to the angle.
        inner_reductions = {reduction}
        # theta: theta + pi
        _theta = Add(_theta0, pi)
        if simplify:
            # simplify theta
            theta_simplification = _theta.simplification()
            inner_reductions.add(theta_simplification)
            _theta = theta_simplification.rhs
        # r: -r0
        _r = Neg(_r0)
        if simplify:
            # simplify radius
            radius_simplification = _r.simplification()
            inner_reductions.add(radius_simplification)
            _r = radius_simplification.rhs
        # reduction: r*exp(i*theta) = orig_expr [via r0*exp(i*theta0))]
        reduction = complex_polar_radius_negation.instantiate(
            {
                r: _r0,
                theta: _theta0
            },
            replacements=inner_reductions,
            auto_simplify=False)
    else:
        _r, _theta = _r0, _theta0
    # Add the reduction and return the coordinates.
    add_reduction(reduction, _r, _theta)
    return (_r, _theta)