1 | #!/usr/bin/python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Pyromaths |
---|
5 | # Un programme en Python qui permet de créer des fiches d'exercices types de |
---|
6 | # mathématiques niveau collège ainsi que leur corrigé en LaTeX. |
---|
7 | # Copyright (C) 2006 -- Jérôme Ortais (jerome.ortais@pyromaths.org) |
---|
8 | # |
---|
9 | # This program is free software; you can redistribute it and/or modify |
---|
10 | # it under the terms of the GNU General Public License as published by |
---|
11 | # the Free Software Foundation; either version 2 of the License, or |
---|
12 | # (at your option) any later version. |
---|
13 | # |
---|
14 | # This program is distributed in the hope that it will be useful, |
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | # GNU General Public License for more details. |
---|
18 | # |
---|
19 | # You should have received a copy of the GNU General Public License |
---|
20 | # along with this program; if not, write to the Free Software |
---|
21 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | # |
---|
23 | |
---|
24 | from pyromaths.outils import Arithmetique |
---|
25 | from . import fractions |
---|
26 | from pyromaths.classes.PolynomesCollege import Polynome |
---|
27 | from pyromaths.outils.Affichage import TeX, tex_coef |
---|
28 | from pyromaths.outils.Priorites3 import texify |
---|
29 | |
---|
30 | # |
---|
31 | # ------------------- ÉQUATIONS ------------------- |
---|
32 | |
---|
33 | |
---|
34 | def valeurs(pyromax): # crée les valeurs aléatoires pour l'équation |
---|
35 | while True: |
---|
36 | coefs = [Arithmetique.valeur_alea(-pyromax, pyromax) for i in range(6)] |
---|
37 | sgn = Arithmetique.valeur_alea(-1, 1) |
---|
38 | if sgn > 0: |
---|
39 | signe = "+" |
---|
40 | else: |
---|
41 | signe = "-" |
---|
42 | while True: |
---|
43 | while True: |
---|
44 | dens = [Arithmetique.valeur_alea(2, 9) for i in range(3)] |
---|
45 | if dens[0] != dens[1] and dens[0] != dens[2] and dens[1] != \ |
---|
46 | dens[2]: |
---|
47 | break |
---|
48 | ppcm = Arithmetique.ppcm(dens[0], Arithmetique.ppcm(dens[1], dens[2])) |
---|
49 | densprim = [ppcm // dens[i] for i in range(3)] |
---|
50 | if densprim[0] < 10 and densprim[1] < 10 and densprim[2] < \ |
---|
51 | 10: |
---|
52 | break |
---|
53 | |
---|
54 | nvxcoefs = [coefs[i] * densprim[i // 2] for i in range(6)] |
---|
55 | if (nvxcoefs[0] + nvxcoefs[2] * sgn) - nvxcoefs[4] != 0: |
---|
56 | break |
---|
57 | return (tuple(coefs), tuple(dens), tuple(densprim), (signe, sgn), |
---|
58 | tuple(nvxcoefs)) |
---|
59 | |
---|
60 | |
---|
61 | def tex_quotient0(a, b, c): # renvoie l'ecriture d'un quotient de l'enonce |
---|
62 | return '\\cfrac{%s}{%s}' % (str(Polynome('%sx+%s' % (a, b))), c) |
---|
63 | |
---|
64 | |
---|
65 | def tex_quotient1(a, b, c, d): # renvoie l'ecriture de la mise au meme denominateur d'un quotient |
---|
66 | if d == 1: |
---|
67 | return tex_quotient0(a, b, c) |
---|
68 | else: |
---|
69 | return '\\cfrac{(%s)_{\\times%s}}{%s_{\\times%s}}' % (str(Polynome([[a, 1], [b, 0]])), d, c, d) |
---|
70 | |
---|
71 | |
---|
72 | def tex_equation0(valeurs): # renvoie l'ecriture des quotients de l'enonce |
---|
73 | texte = '' |
---|
74 | for i in range(3): |
---|
75 | texte = texte + tex_quotient0(valeurs[0][i * 2], valeurs[0][i * |
---|
76 | 2 + 1], valeurs[1][i]) |
---|
77 | if i == 0: |
---|
78 | texte = texte + valeurs[3][0] |
---|
79 | elif i == 1: |
---|
80 | texte = texte + '=' |
---|
81 | return texte |
---|
82 | |
---|
83 | |
---|
84 | def tex_equation1(valeurs): # renvoie l'ecriture de la mise au meme denominateur des quotients |
---|
85 | texte = '' |
---|
86 | for i in range(3): |
---|
87 | texte = texte + tex_quotient1(valeurs[0][i * 2], valeurs[0][i * |
---|
88 | 2 + 1], valeurs[1][i], valeurs[2][i]) |
---|
89 | if i == 0: |
---|
90 | texte = texte + valeurs[3][0] |
---|
91 | elif i == 1: |
---|
92 | texte = texte + '=' |
---|
93 | return texte |
---|
94 | |
---|
95 | |
---|
96 | def tex_equation2(valeurs): # renvoie l'ecriture des quotients au meme denominateur |
---|
97 | texte = '\\cfrac{' |
---|
98 | texte += texify([['Polynome([[%s, 1], [%s, 0]])' % (valeurs[4][0], valeurs[4][1]), valeurs[3][0], 'Polynome([[%s, 1], [%s, 0]])' % (valeurs[4][2], valeurs[4][3])]])[0] |
---|
99 | texte = texte + '}{\\cancel{%s}}=\cfrac{' % (valeurs[1][0] * valeurs[2][0]) |
---|
100 | texte += str(Polynome([[valeurs[4][4], 1] , [valeurs[4][5], 0]])) |
---|
101 | texte = texte + '}{\\cancel{%s}}' % (valeurs[1][0] * valeurs[2][0]) |
---|
102 | return texte |
---|
103 | |
---|
104 | |
---|
105 | def tex_equation2bis(valeurs): # renvoie l'ecriture des quotients au meme denominateur sans les parentheses eventuelles |
---|
106 | texte = str(Polynome('%sx+%s' % (valeurs[4][0], valeurs[4][1]))) |
---|
107 | texte = texte + str(Polynome('%sx+%s' % (valeurs[4][2] * valeurs[3][1], valeurs[4][3] * valeurs[3][1]))) |
---|
108 | texte = texte + '=' + str(Polynome('%sx+%s' % (valeurs[4][4], valeurs[4][5]))) |
---|
109 | return texte |
---|
110 | |
---|
111 | |
---|
112 | def tex_equation3(valeurs): # renvoie l'ecriture reduite de l'equation sans denominateur |
---|
113 | texte = str(Polynome('%sx+%s' % (valeurs[4][0] + valeurs[4][2] * valeurs[3][1], valeurs[4][1] + valeurs[4][3] * valeurs[3][1]))) |
---|
114 | texte = texte + '=' + str(Polynome('%sx+%s' % (valeurs[4][4], valeurs[4][5]))) |
---|
115 | return texte |
---|
116 | |
---|
117 | |
---|
118 | def tex_equation4(valeurs): # renvoie l'ecriture de l'equation avec l'inconnue d'un cote de l'egalite |
---|
119 | texte = tex_coef(valeurs[4][0] + valeurs[4][2] * valeurs[3][1], 'x') + tex_coef(-valeurs[4][4], |
---|
120 | 'x', bplus=1) |
---|
121 | texte = texte + '=' + tex_coef(valeurs[4][5], '') + \ |
---|
122 | tex_coef(-valeurs[4][1] - valeurs[4][3] * valeurs[3][1], |
---|
123 | '', bplus=1) |
---|
124 | return texte |
---|
125 | |
---|
126 | |
---|
127 | def tex_equation5(valeurs): # renvoie l'ecriture reduite de l'equation avec l'inconnue d'un cote de l'egalite |
---|
128 | texte = tex_coef((valeurs[4][0] + valeurs[4][2] * |
---|
129 | valeurs[3][1]) - valeurs[4][4], 'x') |
---|
130 | texte = texte + '=' + tex_coef((valeurs[4][5] - |
---|
131 | valeurs[4][1]) - valeurs[4][3] * valeurs[3][1], '') |
---|
132 | return texte |
---|
133 | |
---|
134 | |
---|
135 | def tex_equation6(valeurs): # renvoie la solution de l'equation |
---|
136 | frac = ((valeurs[4][5] - valeurs[4][1]) - valeurs[4][3] * valeurs[3][1], |
---|
137 | (valeurs[4][0] + valeurs[4][2] * valeurs[3][1]) - valeurs[4][4]) |
---|
138 | if (valeurs[4][0] + valeurs[4][2] * valeurs[3][1]) - valeurs[4][4] == \ |
---|
139 | 1: |
---|
140 | texte = '' |
---|
141 | else: |
---|
142 | texte = 'x=' + fractions.tex_frac(frac) |
---|
143 | simpl = fractions.simplifie(frac) |
---|
144 | if isinstance(simpl, tuple): |
---|
145 | texte = texte + '=' + fractions.tex_frac(simpl) |
---|
146 | return texte |
---|
147 | |
---|
148 | tex_eqs = [tex_equation0, tex_equation1, tex_equation2, tex_equation3, |
---|
149 | tex_equation4, tex_equation5, tex_equation6] |
---|
150 | |
---|
151 | def equations(exo, cor, valeurs): # resolution d'une equation |
---|
152 | exo.append(_(u"Résoudre l'équation : ")) |
---|
153 | exo.append(u'\\[ ' + tex_equation0(valeurs) + '\\] ') |
---|
154 | cor.append(_(u"Résoudre l'équation : ")) |
---|
155 | for i in range(7): |
---|
156 | cor.append(u"\\[%s\\]" % tex_eqs[i](valeurs)) |
---|
157 | if i == 2 and valeurs[3][1] < 0: |
---|
158 | cor.append(u'\\[ ' + tex_equation2bis(valeurs) + '\\] ') |
---|
159 | frac = ((valeurs[4][5] - valeurs[4][1]) - valeurs[4][3] * valeurs[3][1], |
---|
160 | (valeurs[4][0] + valeurs[4][2] * valeurs[3][1]) - valeurs[4][4]) |
---|
161 | simpl = fractions.simplifie(frac) |
---|
162 | if isinstance(simpl, tuple): |
---|
163 | sol = fractions.tex_frac(simpl) |
---|
164 | else: |
---|
165 | sol = fractions.tex_frac(frac) |
---|
166 | cor.append(_(u'\\fbox{La solution de cette équation est $%s$\\,.}') % |
---|
167 | sol) |
---|
168 | |
---|
169 | def tex_equations(): |
---|
170 | vals = valeurs(10) |
---|
171 | exo = ['\\exercice'] |
---|
172 | cor = ['\\exercice*'] |
---|
173 | equations(exo, cor, vals) |
---|
174 | return (exo, cor) |
---|
175 | |
---|
176 | tex_equations.description = _(u'Équation') |
---|