Debian Codegraph @

wxgeometrie/mathlib/universal_functions.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------#######
4 # Mathlib 2 (sympy powered) #
5 ##--------------------------------------#######
6 #WxGeometrie
7 #Dynamic geometry, graph plotter, and more for french mathematic teachers.
8 #Copyright (C) 2005-2013 Nicolas Pourcelot
9 #
10 #This program is free software; you can redistribute it and/or modify
11 #it under the terms of the GNU General Public License as published by
12 #the Free Software Foundation; either version 2 of the License, or
13 #(at your option) any later version.
14 #
15 #This program is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU General Public License for more details.
19 #
20 #You should have received a copy of the GNU General Public License
21 #along with this program; if not, write to the Free Software
22 #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24
25 __doc__ = """
26 Cette librairie contient des versions modifiées des fonctions courantes,
27 pour qu'elles puissent être appliquées aussi bien à des nombres
28 qu'à des variables formelles ou encore des listes...
29
30 Le dictionnaire fonctions_mathematiques contient les infos suivantes :
31 {<nom de la fonction>: [<nom dans math>, <nom dans numpy>, <nom dans sympy>]}
32
33 Il est générée via le code suivant::
34
35 import math
36 liste = math.__dict__.items()
37 dico = {}
38 ignore_liste = ("ldexp", "frexp", "pow", "degrees", "radians", "atan2", "hypot", "modf", "fmod", "fabs")
39 dico.update([(nom,[nom,nom,nom]) for nom, objet in liste if not nom.startswith("_") and hasattr(objet, "__call__") and not nom in ignore_liste])
40 # fonctions ayant un nom différent dans numpy :
41 dico["asin"][1] = "arcsin"; dico["acos"][1] = "arccos"; dico["atan"][1] = "arctan"
42 # fonctions ayant un nom différent dans sympy :
43 dico["ceil"][2] = "ceiling"
44 # fonctions à renommer :
45 dico["ln"] = dico.pop("log")
46 dico["log"] = dico.pop("log10")
47 liste = dico.items()
48 liste.sort()
49 s = "_fonctions_mathematiques = " + repr(liste).replace("[(", "{\n").replace(")]", "}").replace(", [", ": [").replace("), (", ",\n")
50 print s
51 exec(s)
52
53 # Vérification du contenu du dictionnaire :
54 import math, sympy, numpy
55 sympy.log10 = lambda x: sympy.log(x, 10)
56 for elt in _fonctions_mathematiques.items():
57 m, n, s = elt[1]
58 getattr(math, m)
59 getattr(numpy, n)
60 getattr(sympy, s)
61
62 # Génération du code :
63
64 # Nom de la fonction dans les modules math, numpy et sympy respectivement.
65 _fonctions_mathematiques = {
66 'acos': ['acos', 'arccos', 'acos'],
67 'asin': ['asin', 'arcsin', 'asin'],
68 'atan': ['atan', 'arctan', 'atan'],
69 'ceil': ['ceil', 'ceil', 'ceiling'],
70 'cos': ['cos', 'cos', 'cos'],
71 'cosh': ['cosh', 'cosh', 'cosh'],
72 'exp': ['exp', 'exp', 'exp'],
73 'floor': ['floor', 'floor', 'floor'],
74 'ln': ['log', 'log', 'log'],
75 'log': ['log10', 'log10', 'log10'],
76 'sin': ['sin', 'sin', 'sin'],
77 'sinh': ['sinh', 'sinh', 'sinh'],
78 'sqrt': ['sqrt', 'sqrt', 'sqrt'],
79 'tan': ['tan', 'tan', 'tan'],
80 'tanh': ['tanh', 'tanh', 'tanh']}
81
82 for _nom, _noms in _fonctions_mathematiques.items():
83 _nom_math, _nom_numpy, _nom_sympy = _noms
84 print '''\n\ndef %s(*args, **kw):
85 arg0 = args[0]
86 if isinstance(arg0, (int, float)):
87 return _math.%s(*args,**kw)
88 elif isinstance(arg0, complex):
89 return _cmath.%s(*args,**kw)
90 elif isinstance(arg0, _sympy.Basic):
91 return _sympy.%s(*args,**kw)
92 else:
93 return _numpy.%s(*args,**kw)''' %(_nom, _nom_math, _nom_math, _nom_sympy, _nom_numpy)
94
95 """
96
97
98 # Nom de la fonction dans les modules math, numpy et sympy respectivement.
99 _fonctions_mathematiques = {
100 'abs':['abs', 'abs', 'abs'],
101 'acos': ['acos', 'arccos', 'acos'],
102 'asin': ['asin', 'arcsin', 'asin'],
103 'atan': ['atan', 'arctan', 'atan'],
104 'ceil': ['ceil', 'ceil', 'ceiling'],
105 'cos': ['cos', 'cos', 'cos'],
106 'cosh': ['cosh', 'cosh', 'cosh'],
107 'exp': ['exp', 'exp', 'exp'],
108 'floor': ['floor', 'floor', 'floor'],
109 'ln': ['log', 'log', 'log'],
110 'log': ['log10', 'log10', 'log10'],
111 'sin': ['sin', 'sin', 'sin'],
112 'sinh': ['sinh', 'sinh', 'sinh'],
113 'sqrt': ['sqrt', 'sqrt', 'sqrt'],
114 'tan': ['tan', 'tan', 'tan'],
115 'tanh': ['tanh', 'tanh', 'tanh']}
116
117 import numpy as _numpy, sympy as _sympy, math as _math, cmath as _cmath
118 _sympy.log10 = lambda x: _sympy.log(x, 10)
119
120 _cmath.floor = _math.floor # afin de renvoyer une erreur si floor est appelé avec un complexe
121
122 _math.abs = _cmath.abs = _sympy.abs = abs
123
124 # Le code suivant est généré automatiquement (voir plus haut)
125
126
127 def sinh(*args, **kw):
128 arg0 = args[0]
129 if isinstance(arg0, (int, float)):
130 return _math.sinh(*args,**kw)
131 elif isinstance(arg0, complex):
132 return _cmath.sinh(*args,**kw)
133 elif isinstance(arg0, _sympy.Basic):
134 return _sympy.sinh(*args,**kw)
135 else:
136 return _numpy.sinh(*args,**kw)
137
138
139 def asin(*args, **kw):
140 arg0 = args[0]
141 if isinstance(arg0, (int, float)):
142 return _math.asin(*args,**kw)
143 elif isinstance(arg0, complex):
144 return _cmath.asin(*args,**kw)
145 elif isinstance(arg0, _sympy.Basic):
146 return _sympy.asin(*args,**kw)
147 else:
148 return _numpy.arcsin(*args,**kw)
149
150
151 def cos(*args, **kw):
152 arg0 = args[0]
153 if isinstance(arg0, (int, float)):
154 return _math.cos(*args,**kw)
155 elif isinstance(arg0, complex):
156 return _cmath.cos(*args,**kw)
157 elif isinstance(arg0, _sympy.Basic):
158 return _sympy.cos(*args,**kw)
159 else:
160 return _numpy.cos(*args,**kw)
161
162
163 def log(*args, **kw):
164 arg0 = args[0]
165 if isinstance(arg0, (int, float)):
166 return _math.log10(*args,**kw)
167 elif isinstance(arg0, complex):
168 return _cmath.log10(*args,**kw)
169 elif isinstance(arg0, _sympy.Basic):
170 return _sympy.log10(*args,**kw)
171 else:
172 return _numpy.log10(*args,**kw)
173
174
175 def atan(*args, **kw):
176 arg0 = args[0]
177 if isinstance(arg0, (int, float)):
178 return _math.atan(*args,**kw)
179 elif isinstance(arg0, complex):
180 return _cmath.atan(*args,**kw)
181 elif isinstance(arg0, _sympy.Basic):
182 return _sympy.atan(*args,**kw)
183 else:
184 return _numpy.arctan(*args,**kw)
185
186
187 def floor(*args, **kw):
188 arg0 = args[0]
189 if isinstance(arg0, (int, float)):
190 return _math.floor(*args,**kw)
191 elif isinstance(arg0, complex):
192 return _cmath.floor(*args,**kw)
193 elif isinstance(arg0, _sympy.Basic):
194 return _sympy.floor(*args,**kw)
195 else:
196 return _numpy.floor(*args,**kw)
197
198
199 def ln(*args, **kw):
200 arg0 = args[0]
201 if isinstance(arg0, (int, float)):
202 return _math.log(*args,**kw)
203 elif isinstance(arg0, complex):
204 return _cmath.log(*args,**kw)
205 elif isinstance(arg0, _sympy.Basic):
206 return _sympy.log(*args,**kw)
207 else:
208 return _numpy.log(*args,**kw)
209
210
211 def tanh(*args, **kw):
212 arg0 = args[0]
213 if isinstance(arg0, (int, float)):
214 return _math.tanh(*args,**kw)
215 elif isinstance(arg0, complex):
216 return _cmath.tanh(*args,**kw)
217 elif isinstance(arg0, _sympy.Basic):
218 return _sympy.tanh(*args,**kw)
219 else:
220 return _numpy.tanh(*args,**kw)
221
222
223 def sqrt(*args, **kw):
224 arg0 = args[0]
225 if isinstance(arg0, (int, float)):
226 return _math.sqrt(*args,**kw)
227 elif isinstance(arg0, complex):
228 return _cmath.sqrt(*args,**kw)
229 elif isinstance(arg0, _sympy.Basic):
230 return _sympy.sqrt(*args,**kw)
231 else:
232 return _numpy.sqrt(*args,**kw)
233
234
235 def cosh(*args, **kw):
236 arg0 = args[0]
237 if isinstance(arg0, (int, float)):
238 return _math.cosh(*args,**kw)
239 elif isinstance(arg0, complex):
240 return _cmath.cosh(*args,**kw)
241 elif isinstance(arg0, _sympy.Basic):
242 return _sympy.cosh(*args,**kw)
243 else:
244 return _numpy.cosh(*args,**kw)
245
246
247 def exp(*args, **kw):
248 arg0 = args[0]
249 if isinstance(arg0, (int, float)):
250 return _math.exp(*args,**kw)
251 elif isinstance(arg0, complex):
252 return _cmath.exp(*args,**kw)
253 elif isinstance(arg0, _sympy.Basic):
254 return _sympy.exp(*args,**kw)
255 else:
256 return _numpy.exp(*args,**kw)
257
258
259 def acos(*args, **kw):
260 arg0 = args[0]
261 if isinstance(arg0, (int, float)):
262 return _math.acos(*args,**kw)
263 elif isinstance(arg0, complex):
264 return _cmath.acos(*args,**kw)
265 elif isinstance(arg0, _sympy.Basic):
266 return _sympy.acos(*args,**kw)
267 else:
268 return _numpy.arccos(*args,**kw)
269
270
271 def ceil(*args, **kw):
272 arg0 = args[0]
273 if isinstance(arg0, (int, float)):
274 return _math.ceil(*args,**kw)
275 elif isinstance(arg0, complex):
276 return _cmath.ceil(*args,**kw)
277 elif isinstance(arg0, _sympy.Basic):
278 return _sympy.ceil(*args,**kw)
279 else:
280 return _numpy.ceiling(*args,**kw)
281
282
283 def sin(*args, **kw):
284 arg0 = args[0]
285 if isinstance(arg0, (int, float)):
286 return _math.sin(*args,**kw)
287 elif isinstance(arg0, complex):
288 return _cmath.sin(*args,**kw)
289 elif isinstance(arg0, _sympy.Basic):
290 return _sympy.sin(*args,**kw)
291 else:
292 return _numpy.sin(*args,**kw)
293
294
295 def tan(*args, **kw):
296 arg0 = args[0]
297 if isinstance(arg0, (int, float)):
298 return _math.tan(*args,**kw)
299 elif isinstance(arg0, complex):
300 return _cmath.tan(*args,**kw)
301 elif isinstance(arg0, _sympy.Basic):
302 return _sympy.tan(*args,**kw)
303 else:
304 return _numpy.tan(*args,**kw)
305
306
307 def abs(*args, **kw):
308 arg0 = args[0]
309 if isinstance(arg0, (int, float)):
310 return _math.abs(*args,**kw)
311 elif isinstance(arg0, complex):
312 return _cmath.abs(*args,**kw)
313 elif isinstance(arg0, _sympy.Basic):
314 return _sympy.abs(*args,**kw)
315 else:
316 return _numpy.abs(*args,**kw)
317
318
319 # Code écrit à la main (ne pas effacer donc !)
320
321 def arg(complexe):
322 if isinstance(complexe, (int, complex, float)):
323 return _cmath.log(complexe).imag
324 elif isinstance(complexe, _sympy.Basic):
325 return _sympy.arg(complexe)
326 else:
327 return _numpy.imag(_numpy.log(complex))