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 | from random import randrange |
---|
24 | |
---|
25 | def genere_points(xmin, xmax, ymin, ymax, simple=False): |
---|
26 | |
---|
27 | if simple: # génère 3 intervalles de variations /\/ ou \/\ |
---|
28 | if randrange(1): |
---|
29 | variation = [0, 1, 0] |
---|
30 | else: |
---|
31 | variation = [1, 0, 1] |
---|
32 | longueur = xmax - xmin |
---|
33 | hauteur = ymax - ymin |
---|
34 | x_variation = [xmin] |
---|
35 | x_variation.append(x_variation[0] + randrange(int(0.25 * longueur), int(0.4 * longueur))) |
---|
36 | x_variation.append(x_variation[1] + randrange(int(0.25 * longueur), int(0.4 * longueur))) |
---|
37 | x_variation.append(xmax) |
---|
38 | liste_points = [(x_variation[0], randrange(ymin + (1 - variation[0]) * int(0.4 * hauteur), ymax - variation[0] * int(0.4 * hauteur)))] |
---|
39 | for i in [1, 2, 3]: |
---|
40 | if variation[i - 1]: |
---|
41 | liste_points.append((x_variation[i], randrange(liste_points[i - 1][1], ymax))) |
---|
42 | else: |
---|
43 | liste_points.append((x_variation[i], randrange(ymin, liste_points[i - 1][1]))) |
---|
44 | else: |
---|
45 | liste_points = [(xmin, 0)] |
---|
46 | for i in range(10): |
---|
47 | liste_points.append((liste_points[i][0] + randrange(2, 5), randrange(ymin, ymax))) |
---|
48 | return liste_points |
---|
49 | |
---|
50 | def bezier(simple=False, xmin=-15, xmax=15, ymin=-12, ymax=12): |
---|
51 | liste_points = genere_points(simple=simple, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) |
---|
52 | variation = [] |
---|
53 | largeur = liste_points[-1][0] - liste_points[0][0] |
---|
54 | points = [liste_points[0]] |
---|
55 | for i in range(1, len(liste_points)): |
---|
56 | if liste_points[i - 1][1] < liste_points[i][1]: |
---|
57 | points.append(liste_points[i]) |
---|
58 | elif liste_points[i - 1][1] > liste_points[i][1]: |
---|
59 | points.append(liste_points[i]) |
---|
60 | deltaxx = 0.3 * (points[1][0] - points[0][0]) |
---|
61 | nderiv = float(points[1][1] - points[0][1]) / (points[1][0] - points[0][0]) |
---|
62 | apres = [(points[0][0] + 0.5, points[0][1] + deltaxx * nderiv)] |
---|
63 | avant = [] |
---|
64 | if points[0][1] < points[1][1]: |
---|
65 | variation += '+' |
---|
66 | else: |
---|
67 | variation += '-' |
---|
68 | x_variation = [points[0][0]] |
---|
69 | y_variation = [points[0][1]] |
---|
70 | |
---|
71 | for i in range(1, len(points) - 1): |
---|
72 | if (points[i][1] <= points[i - 1][1] and points[i][1] <= points[i + 1][1]): |
---|
73 | nderiv = 0 |
---|
74 | x_variation.append(points[i][0]) |
---|
75 | y_variation.append(points[i][1]) |
---|
76 | variation += '+' |
---|
77 | elif (points[i][1] >= points[i - 1][1] and points[i][1] >= points[i + 1][1]): |
---|
78 | nderiv = 0 |
---|
79 | x_variation.append(points[i][0]) |
---|
80 | y_variation.append(points[i][1]) |
---|
81 | variation += '-' |
---|
82 | else: |
---|
83 | nderiv = float(points[i - 1][1] - points[i + 1][1]) / (points[i - 1][0] - points[i + 1][0]) |
---|
84 | deltax = 0.3 * (points[i - 1][0] - points[i][0]) |
---|
85 | deltaxx = 0.3 * (points[i + 1][0] - points[i][0]) |
---|
86 | avant.append((points[i][0] + deltax, points[i][1] + deltax * nderiv)) |
---|
87 | apres.append((points[i][0] + deltaxx, points[i][1] + deltaxx * nderiv)) |
---|
88 | avant.append((points[len(points) - 1][0] - 0.5, points[len(points) - 1][1] - 0)) |
---|
89 | string = str(points[0]) |
---|
90 | for i in range(len(points) - 1): |
---|
91 | string += str(apres[i]) + str(avant[i]) + str(points[i + 1]) |
---|
92 | textbezier = "\\input{tabvar}\n\ |
---|
93 | \psset{unit=" + str(14.0 / largeur) + "cm}\n\ |
---|
94 | \\begin{pspicture}" |
---|
95 | textbezier += "(" + str(xmin) + "," + str(ymin) + ")(" + str(liste_points[-1][0]) + "," + str(ymax) + ")\n" |
---|
96 | textbezier += "\psgrid[subgriddiv=0,griddots=10,gridlabels=0pt]\n\ |
---|
97 | \psaxes[linewidth=1pt,%\n\ |
---|
98 | ticks=none,%\n\ |
---|
99 | %labels=none\n\ |
---|
100 | Dx=2\n\ |
---|
101 | ]{->}(0,0)" |
---|
102 | textbezier += "(" + str(xmin) + "," + str(ymin) + ")(" + str(liste_points[-1][0]) + "," + str(ymax) + ")\n" |
---|
103 | impression = textbezier + "\psbezier" + string + "\n" + "\\end{pspicture}\n\\vspace{2cm}\n\n" |
---|
104 | impression += "\\begin{tikzpicture}\n\\tkzTabInit[espcl=1]\n{$x$ / .5,$f'\\,(x)$ / .5}\n" |
---|
105 | impression += "{$" + "$,$".join([str(i) for i in x_variation]) + "$}\n" |
---|
106 | impression += "\\tkzTabLine{," + ",".join(y_variation) + "}\n\end{tikzpicture}\n" |
---|
107 | #=============================================================================== |
---|
108 | # {$x$ / 1,$g(x)$ / 1}% |
---|
109 | # {$0$,$1$,$2$}% |
---|
110 | # \tkzTabLine{d,+,0,-,d} |
---|
111 | # \end{tikzpicture}" |
---|
112 | #=============================================================================== |
---|
113 | impression += "$$\n\ |
---|
114 | \\tabvar{%\n\ |
---|
115 | \\tx{x}&\\tx{" + str(x_variation[0]) + "}" |
---|
116 | for i in range(1, len(x_variation)): |
---|
117 | impression += "&& \\tx{" + str(x_variation[i]) + "}" |
---|
118 | impression += "&& \\tx{" + str(points[-1][0]) + "}\\cr\n" |
---|
119 | impression += "\\tx{f(x)}" |
---|
120 | for i in range(len(variation)): |
---|
121 | if variation[i] == '+': |
---|
122 | impression += "&\\txb{" + str(y_variation[i]) + "}&\\fm" |
---|
123 | else: |
---|
124 | impression += "&\\txh{" + str(y_variation[i]) + "}&\\fd" |
---|
125 | if variation[-1] == '+': |
---|
126 | impression += "&\\txh{" + str(points[-1][1]) + "}" |
---|
127 | else: |
---|
128 | impression += "&\\txb{" + str(points[-1][1]) + "}" |
---|
129 | return impression + "\\cr\n}$$\n\n" |
---|
130 | |
---|
131 | |
---|
132 | def tab_var(fonc, Intervalle=["-\\infty", "+\\infty"]): |
---|
133 | from pyromaths.classes.Polynome import Polynome |
---|
134 | if isinstance(fonc, Polynome): |
---|
135 | return tab_var_poly(fonc, Intervalle[0], Intervalle[1]) |
---|
136 | else: |
---|
137 | return _(u"Je ne sais pas étudier les variations de cette fonction.") |
---|
138 | |
---|
139 | def tab_var_poly(P, borneinf="-\\infty" , bornesup=u"+\\infty"): |
---|
140 | Intervalle = [borneinf, bornesup] |
---|
141 | # print"borneinf=",borneinf |
---|
142 | tab_var = "\\input{tabvar}\n" |
---|
143 | tab_var += "$$\\tabvar{%\n" |
---|
144 | if P.deg == 0: |
---|
145 | tab_var += tabvar_x(Intervalle, P.var) |
---|
146 | Cste = str(P(0)) |
---|
147 | tab_var += "\\tx{f(" + P.var + ")}&\\tx{" + Cste + "}&\\fhm&\\tx{" + Cste + "}\\cr\n" |
---|
148 | return tab_var + "}$$\n" |
---|
149 | elif P.deg == 1: |
---|
150 | tab_var += tabvar_x(Intervalle, P.var) |
---|
151 | if P.dictio[1] > 0: |
---|
152 | tab_var += "\\tx{f(" + P.var + ")}& &\\fm& \\cr\n" |
---|
153 | else: |
---|
154 | tab_var += "\\tx{f(" + P.var + ")}& &\\fd&\\cr\n" |
---|
155 | return tab_var + "}$$\n" |
---|
156 | elif P.deg == 2: |
---|
157 | alpha = -P.dictio[1] / (2 * P.dictio[2]) |
---|
158 | extremum = P(alpha) |
---|
159 | if (borneinf == "-\\infty" or Intervalle[0] < alpha) and\ |
---|
160 | (Intervalle[1] == "+\\infty" or Intervalle[1] > alpha): |
---|
161 | listex = [Intervalle[0], alpha, Intervalle[1]] |
---|
162 | if P.dictio[0] > 0: |
---|
163 | tab_var_y = "\\tx{f(" + P.var + ")}& &\\fd&\\txb{" + nombre_TeX(extremum) + "}&\\fm&\\cr\n" |
---|
164 | else: |
---|
165 | tab_var_y = "\\tx{f(" + P.var + ")}& &\\fm&\\txh{" + nombre_TeX(extremum) + "}&\\fd&\\cr\n" |
---|
166 | else: |
---|
167 | listex = Intervalle |
---|
168 | if P.dictio > 0: |
---|
169 | if not(borneinf == "-\\infty") and Intervalle[0] > alpha: |
---|
170 | tab_var_y = "\\tx{f(" + P.var + ")}& &\\fm& \\cr\n" |
---|
171 | else: |
---|
172 | tab_var_y = "\\tx{f(" + P.var + ")}& &\\fd&\\cr\n" |
---|
173 | else: |
---|
174 | if not(borneinf == "-\\infty") and Intervalle[0] > alpha: |
---|
175 | tab_var_y = "\\tx{f(" + P.var + ")}& &\\fd&\\cr\n" |
---|
176 | else: |
---|
177 | tab_var_y = "\\tx{f(" + P.var + ")}& &\\fm& \\cr\n" |
---|
178 | tab_var += tabvar_x(listex, P.var) |
---|
179 | tab_var += tab_var_y |
---|
180 | return tab_var + "}$$\n" |
---|
181 | elif P.deg == 3: |
---|
182 | return _(u"attends un peu pour le degré 3") |
---|
183 | # #Les fonctions développées pour les équations du second degré doivent aider |
---|
184 | else: |
---|
185 | return _(u"Degré trop élevé") |
---|
186 | # #Cas particulier où P' a une racine évidente, ou polynome bicarré |
---|
187 | |
---|
188 | |
---|
189 | def tabvar_x(listex, var): |
---|
190 | '''imprime la premiere ligne du tabvar''' |
---|
191 | var_x = "\\tx{" + var + "}&\\tx{" + nombre_TeX(listex[0]) + "}" |
---|
192 | for i in range(1, len(listex)): |
---|
193 | var_x += "&&\\tx{" + nombre_TeX(listex[i]) + "}" |
---|
194 | return var_x + "\\cr\n" |
---|
195 | |
---|
196 | def nombre_TeX(nombre): # A remplacer par sepmillier |
---|
197 | from pyromaths.classes.Fractions import Fraction |
---|
198 | if type(nombre) in [type(int), type(float)]: |
---|
199 | return str(nombre) |
---|
200 | elif isinstance(nombre, Fraction): |
---|
201 | if nombre.denominateur == 1: |
---|
202 | return nombre_TeX(nombre.numerateur) |
---|
203 | else: |
---|
204 | return "\\dfrac{" + str(nombre.numerateur) + "}{" + str(nombre.denominateur) + "}" |
---|
205 | else: |
---|
206 | return str(nombre) |
---|