mathmaker  0.6(alpha)
mamk_misc/doc/mathmaker4doxygen/maintenance/autotest/obj_test/calc_test/expansion_and_reduction_test.py
00001 # -*- coding: utf-8 -*-
00002 
00003 # Mathmaker creates automatically maths exercises sheets
00004 # with their answers
00005 # Copyright 2006-2014 Nicolas Hainaux <nico_h@users.sourceforge.net>
00006 
00007 # This file is part of Mathmaker.
00008 
00009 # Mathmaker is free software; you can redistribute it and/or modify
00010 # it under the terms of the GNU General Public License as published by
00011 # the Free Software Foundation; either version 3 of the License, or
00012 # any later version.
00013 
00014 # Mathmaker is distributed in the hope that it will be useful,
00015 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 # GNU General Public License for more details.
00018 
00019 # You should have received a copy of the GNU General Public License
00020 # along with Mathmaker; if not, write to the Free Software
00021 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023 import os
00024 import sys
00025 
00026 from lib.common import latex
00027 
00028 from core import *
00029 from core.base_calculus import *
00030 from core.calculus import *
00031 
00032 from maintenance.autotest import common
00033 
00034 check = common.check
00035 
00036 
00037 def action():
00038     if common.verbose:
00039         os.write(common.output, bytes("--- --- EXPANSION & REDUCTION\n", 'utf-8'))
00040 
00041     expd_2_times_sum_of_1_and_5x = Expandable((Item(2),
00042                                                Sum([Item(1),
00043                                                     Monomial((5, 1))
00044                                                    ])
00045                                               ))
00046 
00047     expd_1_minus_11x_times_11_plus_7x = Expandable((Sum([Item(1),
00048                                                          Monomial((-11, 1))
00049                                                         ]),
00050                                                     Sum([Item(11),
00051                                                          Monomial((7, 1))
00052                                                         ])
00053                                                    ))
00054 
00055     expd_3x_minus_sum_xplus3_times_sum_6minus2x = \
00056     Sum([Monomial((3, 1)),
00057          Expandable((Item(-1),
00058                      Expandable((Sum([Item('x'), Item(3)]),
00059                                  Sum([Item(6), Monomial((2, 1))]) \
00060                                 ))
00061                      ))
00062         ])
00063 
00064     expd_minus1_times_sum_3x_minus_2 = Expandable((Item(-1),
00065                                                    Sum([Monomial((3, 1)),
00066                                                         Item(-2)
00067                                                        ])
00068                                                   ))
00069     red_1 = Sum([Product([Item(-3), Item(10)]),
00070                  Product([Monomial(('-', 10, 1)), Monomial(('-', 9, 1))]),
00071                  Product([Monomial(('+', 7, 1)), Monomial(('+', 8, 1))]),
00072                  Product([Item(8), Item(10)])
00073                  ])
00074 
00075     red_2 = Product([Sum([Item(146)]), Item(('+', "x", 2))])
00076 
00077     # -2×(-6) - 1 + 3 × (-x) - 8x × (-3)
00078     red_3 = Sum([Product([Item(-2), Item(-6)]),
00079                  Item(-1),
00080                  Product([Item(3), Item(('-', "x", 1))]),
00081                  Product([Monomial(('-', 8, 1)), Item(-3)])
00082                 ])
00083 
00084     # 5x + 7x × 8x + 5x × (-1) + 7×8
00085     red_4 = Sum([Monomial(('+', 5, 1)),
00086                  Product([Monomial(('+', 7, 1)), Monomial(('+', 8, 1))]),
00087                  Product([Monomial(('+', 5, 1)), Item(-1)]),
00088                  Product([Item(7), Item(8)])
00089                 ])
00090 
00091     # -30 + 80 + x²
00092     red_5 = Sum([Item(-30), Item(80), Monomial(('+', 1, 2))])
00093 
00094     # 4x + (-15x + 8 - 5x)
00095     red_6 = Sum([Monomial(('+', 4, 1)),
00096                  Expandable((Monomial(('+', 1, 0)),
00097                              Polynomial([Monomial(('-', 15, 1)),
00098                                          Monomial(('+', 8, 0)),
00099                                          Monomial(('-', 5, 1))
00100                                         ])
00101                             ))
00102                 ])
00103 
00104     # (3+3x)(3-3x)
00105     red_7 = BinomialIdentity((Item(3),
00106                              Monomial(('+', 3, 1))
00107                              ),
00108                             squares_difference='OK')
00109 
00110     # (1 - 10x)(1 + 10x)
00111     red_8 = BinomialIdentity((Item(1),
00112                              Monomial(('+', 10, 1))
00113                              ),
00114                             squares_difference='OK')
00115 
00116     # -2 -x + 8x² + x
00117     red_9 = Sum([Item(-2),
00118                  Monomial(('-', 1, 1)),
00119                  Monomial(('+', 8, 2)),
00120                  Monomial(('+', 1, 1))
00121                 ])
00122 
00123     # -15 + (10 + 14x - 10x²)
00124     red_10 = Sum([Monomial((-15, 0)),
00125                   Expandable((Item(1),
00126                               Sum([Item(10),
00127                                    Monomial((14, 1)),
00128                                    Monomial(('-', 10, 2))
00129                                   ])
00130                              ))
00131                  ])
00132 
00133     # -(2x+9)(-3x-7)+4(-3x+9)+13
00134     red_11 = Sum([Expandable((Monomial(('-', 1, 0)),
00135                               Expandable((Sum([Monomial((2, 1)),
00136                                                Monomial((9, 0))
00137                                               ]),
00138                                           Sum([Monomial((-3, 1)),
00139                                                Monomial((-7, 0))
00140                                               ])
00141                                          ))
00142                             )),
00143                   Expandable((Monomial((4, 0)),
00144                               Sum([Monomial((-3, 1)),
00145                                    Monomial((9, 0))
00146                                   ])
00147                             )),
00148                   Monomial((13, 0))
00149                  ])
00150 
00151 
00152     dev_1 = Sum([Expandable((Monomial(('+', 7, 0)),
00153                              Sum([Monomial(('-', 6, 1)),
00154                                   Monomial((6, 0))
00155                                  ])
00156                             )),
00157                  BinomialIdentity((Monomial(('-', 10, 1)),
00158                                    Monomial(('-', 3, 0))
00159                                   ),
00160                                   difference_square='OK'
00161                                  )
00162                 ])
00163 
00164 
00165 
00166 
00167 
00168 
00169     expr_1 = Expression("A", expd_2_times_sum_of_1_and_5x)
00170     check(expr_1.auto_expansion_and_reduction(),
00171          [  "$A=2(1+5x)$\\newline $A=2\\times 1+2\\times 5x$\\newline " \
00172           + "$A=2+10x$\\newline "])
00173 
00174     expr_2 = Expression("B", expd_1_minus_11x_times_11_plus_7x)
00175     check(expr_2.auto_expansion_and_reduction(),
00176          [ "$B=(1-11x)(11+7x)$\\newline $B=1\\times 11+1\\times 7x-11x\\times"\
00177           + " 11-11x\\times 7x$\\newline $B=11+7x-121x-77x^{2}$\\newline " \
00178           + "$B=11+(7-121)x-77x^{2}$\\newline $B=11-114x-77x^{2}$\\newline "])
00179 
00180     expr_3 = Expression("C", expd_3x_minus_sum_xplus3_times_sum_6minus2x)
00181     check(expr_3.auto_expansion_and_reduction(),
00182          [  "$C=3x-(x+3)(6+2x)$\\newline " \
00183           + "$C=3x-(x\\times 6+x\\times 2x+3\\times 6+3\\times 2x)$\\newline "\
00184           + "$C=3x-(6x+2x^{2}+18+6x)$\\newline " \
00185           + "$C=3x-6x-2x^{2}-18-6x$\\newline " \
00186           + "$C=(3-6-6)x-2x^{2}-18$\\newline " \
00187           + "$C=-9x-2x^{2}-18$\\newline "])
00188 
00189     expr_4 = Expression("D", expd_minus1_times_sum_3x_minus_2)
00190     check(expr_4.auto_expansion_and_reduction(),
00191          [  "$D=-(3x-2)$\\newline " \
00192           + "$D=-3x+2$\\newline "])
00193 
00194     expr_5 = Expression("E", red_1)
00195     check(expr_5.auto_expansion_and_reduction(),
00196          [  "$E=-3\\times 10-10x\\times (-9x)+7x\\times 8x+8\\times 10" \
00197           + "$\\newline $E=-30+90x^{2}+56x^{2}+80$\\newline " \
00198           + "$E=-30+80+(90+56)x^{2}$\\newline $E=50+146x^{2}$\\newline "])
00199 
00200     check(red_2.expand_and_reduce_next_step(),
00201          ["None"])
00202 
00203     expr_6 = Expression("F", red_3)
00204     check(expr_6.auto_expansion_and_reduction(),
00205          [  "$F=-2\\times (-6)-1+3\\times (-x)-8x\\times (-3)$\\newline " \
00206           + "$F=12-1-3x+24x$\\newline " \
00207           + "$F=11+(-3+24)x$\\newline " \
00208           + "$F=11+21x$\\newline "])
00209 
00210 
00211     # 5x + 7x × 8x + 5x × (-1) + 7×8
00212     expr_7 = Expression("G", red_4)
00213     check(expr_7.auto_expansion_and_reduction(),
00214          [  "$G=5x+7x\\times 8x+5x\\times (-1)+7\\times 8$\\newline " \
00215           + "$G=5x+56x^{2}-5x+56$\\newline " \
00216           + "$G=(5-5)x+56x^{2}+56$\\newline " \
00217           + "$G=0x+56x^{2}+56$\\newline " \
00218           + "$G=56x^{2}+56$\\newline "])
00219 
00220 
00221     expr_8 = Expression("H", red_5)
00222     check(expr_8.auto_expansion_and_reduction(),
00223          [  "$H=-30+80+x^{2}$\\newline " \
00224           + "$H=50+x^{2}$\\newline "])
00225 
00226     expr_9 = Expression("I", red_6)
00227     check(expr_9.auto_expansion_and_reduction(),
00228          [  "$I=4x+(-15x+8-5x)$\\newline " \
00229           + "$I=4x-15x+8-5x$\\newline " \
00230           + "$I=(4-15-5)x+8$\\newline " \
00231           + "$I=-16x+8$\\newline "])
00232 
00233     expr_10 = Expression("J", red_7)
00234     check(expr_10.auto_expansion_and_reduction(),
00235          [  "$J=(3+3x)(3-3x)$\\newline " \
00236           + "$J=3^{2}-(3x)^{2}$\\newline " \
00237           + "$J=9-9x^{2}$\\newline "])
00238 
00239     expr_11 = Expression("K", red_8)
00240     check(expr_11.auto_expansion_and_reduction(),
00241          [  "$K=(1+10x)(1-10x)$\\newline " \
00242           + "$K=1^{2}-(10x)^{2}$\\newline " \
00243           + "$K=1-100x^{2}$\\newline "])
00244 
00245     expr_12 = Expression("L", red_9)
00246     check(expr_12.auto_expansion_and_reduction(),
00247          [  "$L=-2-x+8x^{2}+x$\\newline " \
00248           + "$L=-2+(-1+1)x+8x^{2}$\\newline " \
00249           + "$L=-2+0x+8x^{2}$\\newline " \
00250           + "$L=-2+8x^{2}$\\newline "])
00251 
00252     expr_13 = Expression("M", red_10)
00253     check(expr_13.auto_expansion_and_reduction(),
00254          [  "$M=-15+(10+14x-10x^{2})$\\newline " \
00255           + "$M=-15+10+14x-10x^{2}$\\newline " \
00256           + "$M=-5+14x-10x^{2}$\\newline "])
00257 
00258     # -(2x+9)(-3x-7)+4(-3x+9)+13
00259     expr_14 = Expression("N", red_11)
00260     check(expr_14.auto_expansion_and_reduction(),
00261          [  "$N=-(2x+9)(-3x-7)+4(-3x+9)+13$\\newline " \
00262           + "$N=-(2x\\times (-3x)+2x\\times (-7)+9\\times (-3x)" \
00263                 + "+9\\times (-7))+4\\times (-3x)+4\\times 9+13$\\newline " \
00264           + "$N=-(-6x^{2}-14x-27x-63)-12x+36+13$\\newline " \
00265           + "$N=6x^{2}+14x+27x+63-12x+49$\\newline " \
00266           + "$N=6x^{2}+(14+27-12)x+63+49$\\newline " \
00267           + "$N=6x^{2}+29x+112$\\newline "])
00268 
00269     # 7(-6x + 6) + (-10x - 3)²
00270     expr_15 = Expression("P", dev_1)
00271     check(expr_15.auto_expansion_and_reduction(),
00272          [  "$P=7(-6x+6)+(-10x-3)^{2}$\\newline " \
00273           + "$P=7\\times (-6x)+7\\times 6+(-10x)^{2}-2\\times (-10x)\\times 3"\
00274                 + "+3^{2}$\\newline "\
00275           + "$P=-42x+42+100x^{2}+60x+9$\\newline "\
00276           + "$P=(-42+60)x+42+9+100x^{2}$\\newline "\
00277           + "$P=18x+51+100x^{2}$\\newline "
00278          ])
00279 
00280 
00281 
00282