Esempio n. 1
0
def test_mon_deg0_5_squared_printed():
    """Is this squared Monomial printed correctly?"""
    m = Monomial(('+', 5, 0))
    m.set_exponent(2)
    assert m.printed == wrap_nb('5^{2}')
Esempio n. 2
0
def level_03(q_subkind, **options):

    a = randomly.integer(1, 10)
    b = randomly.integer(1, 10)

    steps = []

    if q_subkind in [
            'sum_square', 'sum_square_mixed', 'difference_square',
            'difference_square_mixed'
    ]:
        # __
        first_term = Monomial(('+', Item(('+', a, 2)).evaluate(), 2))

        second_term = Monomial(
            ('+', Item(('+', Product([2, a, b]).evaluate(), 1)).evaluate(), 1))

        third_term = Monomial(('+', Item(('+', b, 2)).evaluate(), 0))

        if q_subkind in ['difference_square', 'difference_square_mixed']:
            second_term.set_sign('-')

        if q_subkind in ['sum_square_mixed', 'difference_square_mixed']:
            ordered_expression = Polynomial(
                [first_term, second_term, third_term])

            [first_term, second_term,
             third_term] = randomly.mix([first_term, second_term, third_term])

        steps.append(Polynomial([first_term, second_term, third_term]))

        if q_subkind in ['sum_square_mixed', 'difference_square_mixed']:
            steps.append(ordered_expression)

        sq_a_monom = Monomial(('+', a, 1))
        sq_b_monom = Monomial(('+', b, 0))

        let_a_eq = Equality([Item('a'), sq_a_monom])

        let_b_eq = Equality([Item('b'), sq_b_monom])

        steps.append(
            _("Let") + " " + let_a_eq.into_str(force_expression_markers=True) +
            " " + _("and") + " " +
            let_b_eq.into_str(force_expression_markers=True))

        sq_a_monom.set_exponent(2)
        sq_b_monom.set_exponent(2)

        a_square_eq = Equality(
            [Item(('+', 'a', 2)), sq_a_monom,
             sq_a_monom.reduce_()])

        b_square_eq = Equality(
            [Item(('+', 'b', 2)), sq_b_monom,
             sq_b_monom.reduce_()])

        steps.append(
            _("then") + " " +
            a_square_eq.into_str(force_expression_markers=True))

        steps.append(
            _("and") + " " +
            b_square_eq.into_str(force_expression_markers=True))

        two_times_a_times_b_numeric = Product(
            [Item(2), Monomial(('+', a, 1)),
             Item(b)])

        two_times_a_times_b_reduced = two_times_a_times_b_numeric.reduce_()

        two_times_a_times_b_eq = Equality([
            Product([Item(2), Item('a'), Item('b')]),
            two_times_a_times_b_numeric, two_times_a_times_b_reduced
        ])

        steps.append(
            _("and") + " " +
            two_times_a_times_b_eq.into_str(force_expression_markers=True))

        steps.append(_("So it is possible to factorize:"))

        if q_subkind in ['difference_square', 'difference_square_mixed']:
            b = -b

        factorized_expression = Sum([Monomial(('+', a, 1)), Item(b)])
        factorized_expression.set_exponent(2)

        steps.append(factorized_expression)

    elif q_subkind in ['squares_difference', 'squares_difference_mixed']:
        # To have some (ax)² - b² but also sometimes b² - (ax)²:
        degrees = [2, 0, 1, 0]

        if randomly.integer(1, 10) >= 8:
            degrees = [0, 2, 0, 1]

        first_term = Monomial(('+', Item(('+', a, 2)).evaluate(), degrees[0]))

        second_term = Monomial(('-', Item(('+', b, 2)).evaluate(), degrees[1]))

        sq_first_term = Monomial(('+', Item(
            ('+', a, 1)).evaluate(), degrees[2]))

        sq_second_term = Monomial(('-', Item(
            ('+', b, 1)).evaluate(), degrees[3]))

        # The 'mixed' cases are: -b² + (ax)² and -(ax)² + b²
        if q_subkind == 'squares_difference_mixed':
            [first_term, second_term] = randomly.mix([first_term, second_term])
            [sq_first_term,
             sq_second_term] = randomly.mix([sq_first_term, sq_second_term])

        positive_sq_first = sq_first_term.clone()
        positive_sq_first.set_sign('+')
        positive_sq_second = sq_second_term.clone()
        positive_sq_second.set_sign('+')

        steps.append(Polynomial([first_term, second_term]))

        first_inter = None
        second_inter = None

        if sq_second_term.is_negative():
            first_inter = positive_sq_first.clone()
            first_inter.set_exponent(2)
            temp_second_inter = positive_sq_second.clone()
            temp_second_inter.set_exponent(2)
            second_inter = Product([-1, temp_second_inter])
        else:
            temp_first_inter = positive_sq_first.clone()
            temp_first_inter.set_exponent(2)
            first_inter = Product([-1, temp_first_inter])
            second_inter = positive_sq_second.clone()
            second_inter.set_exponent(2)

        steps.append(Sum([first_inter, second_inter]))

        if q_subkind == 'squares_difference_mixed':
            steps.append(Sum([second_inter, first_inter]))

        steps.append(_("So, this expression can be factorized:"))

        sum1 = None
        sum2 = None

        if sq_second_term.is_negative():
            sum1 = Sum([sq_first_term, sq_second_term])
            sq_second_term.set_sign('+')
            sum2 = Sum([sq_first_term, sq_second_term])

        else:
            sum1 = Sum([sq_second_term, sq_first_term])
            sq_first_term.set_sign('+')
            sum2 = Sum([sq_second_term, sq_first_term])

        lil_box = [sum1, sum2]

        steps.append(Product([randomly.pop(lil_box), randomly.pop(lil_box)]))

    elif q_subkind in [
            'fake_01', 'fake_01_mixed', 'fake_02', 'fake_02_mixed', 'fake_03',
            'fake_03_mixed', 'fake_04_A', 'fake_04_A_mixed', 'fake_04_B',
            'fake_04_B_mixed', 'fake_04_C', 'fake_04_C_mixed', 'fake_04_D',
            'fake_04_D_mixed'
    ]:
        # __
        straight_cases = [
            'fake_01', 'fake_02', 'fake_03', 'fake_04_A', 'fake_04_B',
            'fake_04_C', 'fake_04_D'
        ]
        match_pb_cases = [
            'fake_01', 'fake_02', 'fake_01_mixed', 'fake_02_mixed'
        ]
        sign_pb_cases = [
            'fake_03', 'fake_03_mixed', 'fake_04_A', 'fake_04_B', 'fake_04_C',
            'fake_04_D', 'fake_04_A_mixed', 'fake_04_B_mixed',
            'fake_04_C_mixed', 'fake_04_D_mixed'
        ]

        ax = Monomial(('+', a, 1))
        b_ = Monomial(('+', b, 0))

        ax_2 = ax.clone()
        ax_2.set_exponent(2)
        a2x2 = Monomial(('+', a * a, 2))

        b_2 = Monomial(('+', b, 0))
        b_2.set_exponent(2)

        b2 = Monomial(('+', b * b, 0))

        two_ax_b = Product([Item(2), Monomial(('+', a, 1)), Item(b)])

        twoabx = Monomial(('+', 2 * a * b, 1))

        fake_twoabx = Monomial(('+', a * b, 1))

        if randomly.integer(1, 10) >= 8:
            fake_twoabx = Monomial(
                ('+',
                 2 * a * b + randomly.pop([-1, 1]) * randomly.integer(1, 5),
                 1))
        first_term = None
        second_term = None
        third_term = None

        ordered_expression = None
        mixed_expression = None

        if q_subkind == 'fake_03' or q_subkind == 'fake_03_mixed':
            first_term = a2x2.clone()
            second_term = b2.clone()
            ordered_expression = Polynomial([first_term, second_term])
            mixed_expression = Polynomial([second_term, first_term])

        else:
            first_term = a2x2.clone()
            third_term = b2.clone()

            if q_subkind in [
                    'fake_01', 'fake_01_mixed', 'fake_02', 'fake_02_mixed'
            ]:
                # __
                second_term = fake_twoabx.clone()

            else:
                second_term = twoabx.clone()

            if q_subkind == 'fake_02' or q_subkind == 'fake_02_mixed':
                second_term.set_sign('-')

            elif q_subkind == 'fake_04_A' or q_subkind == 'fake_04_A_mixed':
                third_term.set_sign('-')

            elif q_subkind == 'fake_04_B' or q_subkind == 'fake_04_B_mixed':
                first_term.set_sign('-')

            elif q_subkind == 'fake_04_C' or q_subkind == 'fake_04_C_mixed':
                second_term.set_sign('-')
                third_term.set_sign('-')

            elif q_subkind == 'fake_04_D' or q_subkind == 'fake_04_D_mixed':
                first_term.set_sign('-')
                second_term.set_sign('-')

            ordered_expression = Polynomial(
                [first_term, second_term, third_term])

            mixed_expression = Polynomial(
                randomly.mix([first_term, second_term, third_term]))
        if q_subkind in straight_cases:
            steps.append(ordered_expression)

        elif q_subkind == 'fake_03_mixed':
            steps.append(mixed_expression)

        else:
            steps.append(mixed_expression)
            steps.append(ordered_expression)

        if q_subkind in match_pb_cases:
            let_a_eq = Equality([Item('a'), ax])

            let_b_eq = Equality([Item('b'), b_])

            steps.append(
                _("Let") + " " +
                let_a_eq.into_str(force_expression_markers=True) + " " +
                _("and") + " " +
                let_b_eq.into_str(force_expression_markers=True))

            a_square_eq = Equality([Item(('+', 'a', 2)), ax_2, a2x2])

            b_square_eq = Equality([Item(('+', 'b', 2)), b_2, b2])

            steps.append(
                _("then") + " " +
                a_square_eq.into_str(force_expression_markers=True))

            steps.append(
                _("and") + " " +
                b_square_eq.into_str(force_expression_markers=True))

            two_times_a_times_b_eq = Equality([
                Product([Item(2), Item('a'), Item('b')]), two_ax_b, twoabx,
                fake_twoabx
            ],
                                              equal_signs=['=', '=', 'neq'])

            steps.append(
                _("but") + " " +
                two_times_a_times_b_eq.into_str(force_expression_markers=True))

            steps.append(_("So it does not match a binomial identity."))
            steps.append(_("This expression cannot be factorized."))

        elif q_subkind in sign_pb_cases:
            steps.append(_("Because of the signs,"))
            steps.append(_("it does not match a binomial identity."))
            steps.append(_("This expression cannot be factorized."))

    return steps
Esempio n. 3
0
def test_3x_squared_printed():
    """Is this squared Monomial printed correctly?"""
    m = Monomial(('+', 3, 1))
    m.set_exponent(2)
    assert m.printed == wrap_nb('(3x)^{2}')