mathmaker  0.6(alpha)
mamk_misc/doc/mathmaker4doxygen/maintenance/autotest/obj_test/calc_test/products_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 import decimal
00027 
00028 from core import *
00029 from core.base_calculus import *
00030 
00031 from maintenance.autotest import common
00032 
00033 check = common.check
00034 
00035 
00036 def action():
00037     if common.verbose:
00038         os.write(common.output, bytes("--- PRODUCTS\n", 'utf-8'))
00039 
00040     item_1 = Item(1)
00041     item_minus_1 = Item(-1)
00042     item_a = Item('a')
00043     item_b = Item('b')
00044 
00045     product_minus_a = Product([Item("-a")])
00046     product_minus_a_bis = Product([Item(('+', "-a", 1))])
00047     product_1_times_1 = Product([item_1, item_1])
00048     product_1_times_1_times_a = Product([item_1, item_1, item_a])
00049     product_2_times_minus_2 = Product([Item(2), Item(-2)])
00050     product_minus_2_times_2 = Product([Item(-2), Item(2)])
00051 
00052     product_8_times_minus_6 = Product([Monomial((8, 0)), Monomial((-6, 0))])
00053 
00054     product_2_times_a_times_b = Product([Item(2), item_a, item_b])
00055     product_1_times_minus_1 = Product([item_1, item_minus_1])
00056     product_1_times_minus_b_times_1_times_4 = Product([item_1,
00057                                                        Item('-b'),
00058                                                        item_1,
00059                                                        Item(4)])
00060     product_minus_1_times_minus_4 = Product([Item(-1), Item(-4)])
00061 
00062     product_minus_1_times_4 = Product([Item(-1), Item(4)])
00063 
00064     product_minus_1_times_1_times_1_times_1 = Product([Item(-1),
00065                                                        Item(1),
00066                                                        Item(1),
00067                                                        Item(1)])
00068 
00069     product_a_times_minus_b = Product([Item('a'), (Item('-b'))])
00070     product_a_times_minus_b.set_compact_display(False)
00071 
00072     product_a_times_ebd_minus_b = Product([Item('a'),
00073                                            Item(('+', "-b", 1))
00074                                           ])
00075 
00076     product_a_times_ebd_minus_b.set_compact_display(False)
00077 
00078     product_2_times_1 = Product([Item(2), (Item(1))])
00079     product_2_times_1.set_compact_display(False)
00080 
00081     product_1_times_7x = Product([Item(1), (Monomial((7, 1)))])
00082     product_1_times_7x.set_compact_display(False)
00083 
00084     product_minus_1_squared = Product(Item(-1))
00085     product_minus_1_squared.set_exponent(2)
00086 
00087     product_minus_3_times_minus_5_exponent_squared_minus_1 = \
00088                                                       Product([Item(-3),
00089                                                                Item(-5)])
00090 
00091     product_minus_3_times_minus_5_exponent_squared_minus_1.set_exponent(
00092                                                                   Item(('+',
00093                                                                         -1,
00094                                                                         2
00095                                                                        ))
00096                                                                         )
00097 
00098     product_4_times_product_minus_2_times_7 = Product([Item(4),
00099                                                        Product([Item(-2),
00100                                                                 Item(7)])
00101                                                       ])
00102 
00103     product_4_times_product_minus_2_times_7_BIS = Product([Item(4),
00104                                                            Product([Item(-2),
00105                                                                     Item(7)
00106                                                                     ])
00107                                                           ])
00108 
00109     squared_product_minus_2_times_7 = Product([Item(-2), Item(7)])
00110     squared_product_minus_2_times_7.set_exponent(2)
00111 
00112     product_4_times_squared_product_minus_2_times_7 = Product([Item(4),
00113                                             squared_product_minus_2_times_7])
00114 
00115     sum_4_plus_2 = Sum([Item(4), Item(2)])
00116     product_7_times_sum_4_plus_2 = Product([Item(7), sum_4_plus_2])
00117     product_minus_1_times_sum_4_plus_2 = Product([Item(-1), sum_4_plus_2])
00118 
00119     product_sum_1_plus_2 = Product([Sum([1, 2])])
00120 
00121     product_1_by_sum_3_plus_4 = Product([1, Sum([3, 4])])
00122 
00123     product_sum_3_plus_4_by_1 = Product([Sum([3, 4]), 1])
00124 
00125     product_rubbish = Product([Item(('+', 1, 1)),
00126                                     Item(('+', "x", 1))])
00127 
00128     product_monom0_times_monom0 = Product([Monomial((7,0)),
00129                                            (Monomial((8,0))) ])
00130     product_monom0_times_monom0.set_compact_display(False)
00131 
00132     product_sum_3_plus_4_times_monom_minus8x = Product([Sum([Item(3),
00133                                                              Item(4)]),
00134                                                         Monomial((-8, 1))])
00135 
00136     product_4x_times_minus3x = Product([Monomial((4, 1)),
00137                                         Monomial((-3, 1))])
00138 
00139     product_nc_monomials_4_times_minus3 = Product([Monomial((4, 0)),
00140                                                    Monomial((-3, 0))])
00141 
00142     product_nc_monomials_4_times_minus3.set_compact_display(False)
00143 
00144     square_product_monom_6 = Product(Monomial(('+', 6, 0)))
00145     square_product_monom_6.set_exponent(2)
00146 
00147     square_product_square_item_6 = Product(Item(('+', 6, 2)))
00148     square_product_square_item_6.set_exponent(2)
00149 
00150     square_product_monom_x = Product(Monomial(('+', 1, 1)))
00151     square_product_monom_x.set_exponent(2)
00152 
00153     square_product_square_item_x = Product(Item(('+', 'x', 2)))
00154     square_product_square_item_x.set_exponent(2)
00155 
00156     product_minus1_fraction_2over3_fraction_3over4 = Product([Item(-1),
00157                                                               Fraction(('+',
00158                                                                         2,
00159                                                                         3)),
00160                                                               Fraction(('+',
00161                                                                         3,
00162                                                                         4))
00163                                                              ])
00164 
00165     product_fraction_6over2_times_fraction_8overminus5 = \
00166                                          Product([Fraction(('+', 6, 2)),
00167                                                   Fraction(('+', 8, -5))])
00168 
00169     product_fraction_minus3overminus2_times_fraction_minus1over5 = \
00170                                          Product([Fraction(('+', -3, -2)),
00171                                                   Fraction(('+', -1, 5))])
00172 
00173     product_3timesminusx = Product([Item(3), Item(('-', "x"))])
00174 
00175     product_0timesx = Product([Item(0), Item("x")])
00176 
00177     product_1square = Product(Item(1))
00178     product_1square.set_exponent(2)
00179 
00180     product_monom_minus1_times_minus7x = Product([Item(-1),
00181                                                   Product([
00182                                                            Monomial(('-', 7, 1))
00183                                                           ])
00184                                                 ])
00185     product_monom_minus1_times_minus7x.set_compact_display(False)
00186 
00187     temp = Product([Item(('-', "a")),
00188                     Item(('+', "b"))
00189                   ])
00190     product_7_times_product_minusa_minusb = Product([Item(7),
00191                                                      temp
00192                                                     ])
00193 
00194     temp.set_compact_display(False)
00195     product_7_times_product_minusa_minusb_BIS \
00196                                           = Product([Item(7),
00197                                                      temp
00198                                                     ])
00199 
00200     temp = Product([Item(-2),
00201                     Item(7),
00202                     Item('a'),
00203                     Item('b')
00204                   ])
00205     product_9_times_minus2_times_7ab = Product([ Item(9),
00206                                                  Product([Item(-2),
00207                                                           Item(7),
00208                                                           Item('a'),
00209                                                           Item('b')
00210                                                           ])
00211                                                 ])
00212 
00213     temp.set_compact_display(False)
00214     product_9_times_minus2_times_7ab_BIS = Product([ Item(9),
00215                                                      temp
00216                                                     ])
00217 
00218 
00219     temp = Product([Item(-2),
00220                     Item('a'),
00221                     Item(4),
00222                     Item('b')
00223                   ])
00224     product_9_times_minus2a_times_4b = Product([ Item(9),
00225                                                  temp
00226                                                 ])
00227 
00228     temp.set_compact_display(False)
00229     product_9_times_minus2a_times_4b_BIS = Product([ Item(9),
00230                                                      temp
00231                                                     ])
00232 
00233     product_sum_minus1_plus_4_times_x = Product([
00234                                                  Sum([Item(-1),
00235                                                       Item(3)
00236                                                      ]),
00237                                                  Item('x')
00238                                                 ])
00239 
00240     product_9_times_Monomial_minusx = Product([Item(9),
00241                                                Monomial(('-', 1, 1))
00242                                                ])
00243 
00244     product_10_times_minusminus4 = Product([Item(10),
00245                                             Product([Item(-1), Item(-4)])
00246                                            ])
00247 
00248     product_15_times_Monomial_3x = Product([Item(15),
00249                                             Sum([Item(0),
00250                                                  Monomial(('+', 3, 1))
00251                                                ])
00252                                           ])
00253 
00254     product_15_times_Monomial_3x_BIS = Product([Item(15), Sum([Item(0),
00255                                                        Monomial(('+', 3, 1))
00256                                                      ])
00257                                               ])
00258 
00259     product_15_times_Monomial_3x_TER = Product([Product([Item(15)]),
00260                                                 Sum([Item(0),
00261                                                      Monomial(('+', 3, 1))
00262                                                    ])
00263                                               ])
00264 
00265 
00266     product_sum_2_3x_times_sum_minus4_6x = Product([Sum([Item(2),
00267                                                          Monomial(('+', 3, 1))
00268                                                          ]),
00269                                                     Sum([Item(-4),
00270                                                          Monomial(('+', 6, 1))
00271                                                          ])
00272                                                     ])
00273 
00274     product_item_minus0_times_item_x = Product([Item(('-', 0, 1)),
00275                                                 Item(('+', 'x', 1))
00276                                               ])
00277 
00278 
00279 
00280 
00281     #1
00282     check(product_minus_a,
00283          ["-a"])
00284 
00285     check(product_minus_a.is_reducible(),
00286          ["False"])
00287 
00288     check(product_minus_a_bis.is_reducible(),
00289          ["False"])
00290 
00291     check(product_1_times_1,
00292          ["1"])
00293 
00294     #5
00295     check(product_1_times_1.is_reducible(),
00296          ["False"])
00297 
00298     product_1_times_1.set_compact_display(False)
00299     check(product_1_times_1,
00300          ["1\\times 1"])
00301 
00302     check(product_1_times_1_times_a,
00303          ["a"])
00304 
00305 
00306     check(product_1_times_1_times_a.is_reducible(),
00307          ["False"])
00308 
00309     check(product_2_times_minus_2,
00310          ["2\\times (-2)"])
00311 
00312     #10
00313     check(product_minus_2_times_2,
00314          ["-2\\times 2"])
00315 
00316     check(product_8_times_minus_6,
00317          ["8\\times (-6)"])
00318 
00319     check(product_2_times_a_times_b,
00320          ["2ab"])
00321 
00322     check(product_2_times_a_times_b.is_reducible(),
00323          ["False"])
00324 
00325     check(product_1_times_minus_1,
00326          ["-1"])
00327 
00328     product_1_times_minus_1.set_compact_display(False)
00329 
00330     #15
00331     check(product_1_times_minus_1,
00332          ["1\\times (-1)"])
00333 
00334     check(product_1_times_minus_b_times_1_times_4,
00335          ["-b\\times 4"])
00336 
00337     check(product_1_times_minus_b_times_1_times_4.is_reducible(),
00338          ["True"])
00339 
00340     product_1_times_minus_b_times_1_times_4.set_compact_display(False)
00341     check(product_1_times_minus_b_times_1_times_4,
00342          ["1\\times (-b)\\times 1\\times 4"])
00343 
00344     check(product_minus_1_times_minus_4,
00345          ["-(-4)"])
00346 
00347     #20
00348     check(product_minus_1_times_4,
00349          ["-4"])
00350 
00351     check(product_minus_1_times_minus_4.is_reducible(),
00352          ["True"])
00353 
00354     check(product_minus_1_times_minus_4.evaluate(),
00355          ["4"])
00356 
00357     check(product_minus_1_times_1_times_1_times_1,
00358          ["-1"])
00359 
00360     check(product_minus_1_times_1_times_1_times_1.is_reducible(),
00361          ["False"])
00362 
00363     #25
00364     check(product_a_times_minus_b,
00365          ["a\\times (-b)"])
00366 
00367     check(product_a_times_minus_b.is_reducible(),
00368          ["True"])
00369 
00370     check(product_a_times_ebd_minus_b,
00371          ["a\\times (-b)"])
00372 
00373     check(product_a_times_ebd_minus_b.is_reducible(),
00374          ["True"])
00375 
00376     check(product_2_times_1.is_reducible(),
00377          ["True"])
00378 
00379     #30
00380     check(product_1_times_7x.is_reducible(),
00381          ["True"])
00382 
00383 
00384     check(product_minus_1_squared,
00385          ["(-1)^{2}"])
00386 
00387     check(product_minus_1_squared.is_displ_as_a_single_1(),
00388          ["False"])
00389 
00390     check(product_minus_1_squared.is_displ_as_a_single_minus_1(),
00391          ["False"])
00392 
00393     check(product_minus_1_squared.is_reducible(),
00394          ["True"])
00395 
00396     #35
00397     check(product_minus_3_times_minus_5_exponent_squared_minus_1,
00398          ["(-3\\times (-5))^{(-1)^{2}}"])
00399 
00400     check(product_minus_3_times_minus_5_exponent_squared_minus_1.evaluate(),
00401          ["15"])
00402 
00403     check(product_4_times_product_minus_2_times_7,
00404          ["4\\times (-2)\\times 7"])
00405 
00406     check(product_4_times_product_minus_2_times_7_BIS,
00407          ["4\\times (-2)\\times 7"])
00408 
00409     check(product_4_times_product_minus_2_times_7.evaluate(),
00410          ["-56"])
00411 
00412     #40
00413     check(product_4_times_squared_product_minus_2_times_7,
00414          ["4\\times (-2\\times 7)^{2}"])
00415 
00416     check(product_4_times_squared_product_minus_2_times_7.is_reducible(),
00417          ["True"])
00418 
00419     check(product_4_times_squared_product_minus_2_times_7.evaluate(),
00420          ["784"])
00421 
00422     check(product_7_times_sum_4_plus_2,
00423          ["7(4+2)"])
00424 
00425     product_7_times_sum_4_plus_2.set_compact_display(False)
00426     check(product_7_times_sum_4_plus_2,
00427          ["7\\times (4+2)"])
00428 
00429     #45
00430     check(product_7_times_sum_4_plus_2.evaluate(),
00431          ["42"])
00432 
00433     check(product_minus_1_times_sum_4_plus_2,
00434          ["-(4+2)"])
00435 
00436     check(product_minus_1_times_sum_4_plus_2.evaluate(),
00437          ["-6"])
00438 
00439     check(product_sum_1_plus_2,
00440          ["1+2"])
00441 
00442     check(product_1_by_sum_3_plus_4,
00443          ["3+4"])
00444 
00445     #50
00446     check(product_sum_3_plus_4_by_1,
00447          ["3+4"])
00448 
00449     check(product_sum_3_plus_4_by_1.is_reducible(),
00450          ["False"])
00451 
00452     check(product_rubbish,
00453          ["x"])
00454 
00455     check(product_rubbish.is_reducible(),
00456          ["False"])
00457 
00458     check(product_monom0_times_monom0,
00459          ["7\\times 8"])
00460 
00461     #55
00462     check(product_sum_3_plus_4_times_monom_minus8x,
00463          ["(3+4)\\times (-8x)"])
00464 
00465     check(product_4x_times_minus3x,
00466          ["4x\\times (-3x)"])
00467 
00468     check(product_nc_monomials_4_times_minus3,
00469          ["4\\times (-3)"])
00470 
00471     check(square_product_monom_6,
00472          ["6^{2}"])
00473 
00474     check(square_product_square_item_6,
00475          ["(6^{2})^{2}"])
00476 
00477     #60
00478     check(square_product_monom_x,
00479          ["x^{2}"])
00480 
00481     check(square_product_square_item_x,
00482          ["(x^{2})^{2}"])
00483 
00484     check(product_minus1_fraction_2over3_fraction_3over4,
00485          ["-\\frac{2}{3}\\times \\frac{3}{4}"])
00486 
00487     check(product_fraction_6over2_times_fraction_8overminus5,
00488          ["\\frac{6}{2}\\times \\frac{8}{-5}"])
00489 
00490     check( \
00491     product_fraction_minus3overminus2_times_fraction_minus1over5
00492                                                     .calculate_next_step(),
00493          ["-\\frac{3\\times 1}{2\\times 5}"])
00494 
00495     #65
00496     check(product_3timesminusx,
00497          ["3\\times (-x)"])
00498 
00499     check(product_3timesminusx.is_reducible(),
00500          ["True"])
00501 
00502     check(product_0timesx,
00503          ["0x"])
00504 
00505     check(product_0timesx.is_reducible(),
00506          ["True"])
00507 
00508     check(product_1square.is_displ_as_a_single_1(),
00509          ["False"])
00510 
00511     #70
00512     check(product_1square.is_reducible(),
00513          ["True"])
00514 
00515 
00516     check(product_monom_minus1_times_minus7x,
00517          ["-1\\times (-7x)"])
00518 
00519     check(product_7_times_product_minusa_minusb,
00520          ["7\\times (-ab)"])
00521 
00522     check(product_7_times_product_minusa_minusb_BIS,
00523          ["7\\times (-a)\\times b"])
00524 
00525     check(product_9_times_minus2_times_7ab,
00526          ["9\\times (-2)\\times 7ab"])
00527 
00528     #75
00529     check(product_9_times_minus2_times_7ab_BIS,
00530          ["9\\times (-2)\\times 7\\times a\\times b"])
00531 
00532     check(product_9_times_minus2a_times_4b,
00533          ["9\\times (-2a)\\times 4b"])
00534 
00535     check(product_9_times_minus2a_times_4b_BIS,
00536          ["9\\times (-2)\\times a\\times 4\\times b"])
00537 
00538     check(product_sum_minus1_plus_4_times_x,
00539          ["(-1+3)x"])
00540 
00541     check(product_9_times_Monomial_minusx,
00542          ["9\\times (-x)"])
00543 
00544     #80
00545     check(product_10_times_minusminus4,
00546          ["10\\times (-(-4))"])
00547 
00548     check(product_15_times_Monomial_3x,
00549          ["15\\times 3x"])
00550 
00551     check(product_15_times_Monomial_3x_BIS,
00552          ["15\\times 3x"])
00553 
00554     check(product_15_times_Monomial_3x_TER,
00555          ["15\\times 3x"])
00556 
00557     check(product_sum_2_3x_times_sum_minus4_6x.is_reducible(),
00558          ["False"])
00559 
00560     #check(product_item_minus0_times_item_x.is_displ_as_a_single_0(),
00561     #     ["True"])
00562 
00563     p = Product([Monomial(('-', 10, 1))])
00564     p.set_exponent(2)
00565 
00566     check(p,
00567          ["(-10x)^{2}"])
00568 
00569     check(p.calculate_next_step(),
00570          ["100x^{2}"])
00571 
00572     fraction_and_item = Product([Fraction((Item(5), Item(7))),
00573                                  Item(8)
00574                                 ])
00575 
00576     check(fraction_and_item.calculate_next_step(),
00577          ["\\frac{5\\times 8}{7}"])
00578 
00579     fraction_and_item = Product([Item(8),
00580                                  Fraction((Item(5), Item(7)))
00581                                 ])
00582 
00583     check(fraction_and_item.calculate_next_step(),
00584          ["\\frac{8\\times 5}{7}"])
00585 
00586     fractions_and_items = Product([Fraction((Item(3), Item(5))),
00587                                    Item(8),
00588                                    Fraction((Item(7), Item(11))),
00589                                    Item(4),
00590                                    Fraction((Item(13), Item(17)))
00591                                   ])
00592 
00593     check(fractions_and_items.calculate_next_step(),
00594          ["\\frac{3\\times 8\\times 7\\times 4\\times 13}"\
00595           + "{5\\times 11\\times 17}"])
00596 
00597 
00598 
00599     if common.verbose:
00600         os.write(common.output, bytes("\n--- PRODUCTS - evaluate\n", 'utf-8'))
00601 
00602     a = Item(2.5)
00603     b = Item(3.5)
00604 
00605     check(isinstance(a.evaluate(), decimal.Decimal),
00606          ["True"])
00607 
00608     c = Product([a, b]).evaluate()
00609 
00610     check(isinstance(c, decimal.Decimal),
00611          ["True"])
00612 
00613     check(c,
00614          ["8.75"])
00615 
00616     d = Fraction((Item(3), Item(8)))
00617 
00618     check(d.evaluate(),
00619          ["0.375"])
00620 
00621     e = Fraction((Item(3), Item(7)))
00622 
00623     check(e.evaluate(),
00624          ["0.4285714285714285714285714286"])
00625 
00626     check(e.evaluate(keep_not_decimal_nb_as_fractions=True),
00627          ["\\frac{3}{7}"])
00628 
00629     f = Product([Fraction((Item(3), Item(7))),
00630                  Fraction((Item(7), Item(4)))])
00631 
00632     check(f.evaluate(),
00633          ["0.75"])
00634 
00635     check(f.evaluate(keep_not_decimal_nb_as_fractions=True),
00636          ["0.75"])
00637 
00638     g = Product([Item(6),
00639                  Fraction((Item(5), Item(3)))])
00640 
00641     check(g.evaluate(),
00642          ["10"])