Debian Codegraph @

texmf-dist/tex/lualatex/sympycalc/SympyCalc.sty

raw
1 % Author : C. Pierquet
2 % licence : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txtf
3
4 \NeedsTeXFormat{LaTeX2e}
5 \ProvidesPackage{SympyCalc}[2023/05/08 v0.1.1 Sympy commands with TeX printing]
6 % 0.1.1 Ajout du nombre dérivé + calcul image
7 % 0.1 Version initiale
8
9 %------Packages utiles
10 \RequirePackage{xstring}
11
12 %------Scripts python
13 \begin{python}
14 #initialisations
15 from sympy import *
16 init_printing()
17 x, y, z, t = symbols('x y z t')
18
19 #commandes génériques
20 def latex_fr(expr) :
21 return latex(expr,imaginary_unit='ri',ln_notation=True,decimal_separator='comma').replace("e^","\\text{e}^")
22
23 def latex_frit(expr) :
24 return latex(expr,ln_notation=True,decimal_separator='comma')
25
26 def latex_fr_simpl(expr) :
27 return latex(simplify(expr),imaginary_unit='ri',ln_notation=True,decimal_separator='comma').replace("e^","\\text{e}^")
28
29 def latex_frit_simpl(expr) :
30 return latex(simplify(expr),ln_notation=True,decimal_separator='comma')
31
32 #commandes de résolutions
33 def latex_fr_resol(expr,var) :
34 if '=' in expr :
35 expr = expr.replace('=','-(') + ')'
36 res = solve(expr,var)
37 res = [simplify(element) for element in res if 'I' not in str(element)]
38 res = latex(res,imaginary_unit='ri',ln_notation=True,decimal_separator='comma')
39 res = res.replace("e^","\\text{e}^")
40 res = res.replace("\\ ","")
41 res = res.replace("[","\\{")
42 res = res.replace("]","\\}")
43 return res
44
45 def latex_frit_resol(expr,var) :
46 if '=' in expr :
47 expr = expr.replace('=','-+(') + ')'
48 res = solve(expr,var)
49 res = [simplify(element) for element in res if 'I' not in str(element)]
50 res = latex(res,ln_notation=True,decimal_separator='comma')
51 res = res.replace("e^","\\text{e}^")
52 res = res.replace("\\ ","")
53 res = res.replace("[","\\{")
54 res = res.replace("]","\\}")
55 return res
56
57 def latex_fr_resolC(expr,var) :
58 if '=' in expr :
59 expr = expr.replace('=','-(') + ')'
60 res = solve(expr,var)
61 res = [simplify(element) for element in res]
62 res = latex(res,imaginary_unit='ri',ln_notation=True,decimal_separator='comma')
63 res = res.replace("e^","\\text{e}^")
64 res = res.replace("\\ ","")
65 res = res.replace("[","\\{")
66 res = res.replace("]","\\}")
67 return res
68
69 def latex_frit_resolC(expr,var) :
70 if '=' in expr :
71 expr = expr.replace('=','-+(') + ')'
72 res = solve(expr,var)
73 res = [simplify(element) for element in res]
74 res = latex(res,ln_notation=True,decimal_separator='comma')
75 res = res.replace("e^","\\text{e}^")
76 res = res.replace("\\ ","")
77 res = res.replace("[","\\{")
78 res = res.replace("]","\\}")
79 return res
80
81 #commandes de nombres dérivés
82 def latex_fr_nbderiv(expr,var,pta,pos) :
83 tmpf = Lambda(var,expr)
84 res = (tmpf(var)-tmpf(pta))/(var-pta)
85 res = limit(res,var,pta,pos)
86 res = latex(res,ln_notation=True,decimal_separator='comma')
87 res = res.replace("e^","\\text{e}^")
88 return res
89
90 def latex_frit_nbderiv(expr,var,pta,pos) :
91 tmpf = Lambda(var,expr)
92 res = (tmpf(var)-tmpf(pta))/(var-pta)
93 res = limit(res,var,pta,pos)
94 res = latex(res,ln_notation=True,decimal_separator='comma')
95 return res
96
97 def latex_fr_nbderiv_simpl(expr,var,pta,pos) :
98 tmpf = Lambda(var,expr)
99 res = (tmpf(var)-tmpf(pta))/(var-pta)
100 res = simplify(limit(res,var,pta,pos))
101 res = latex(res,ln_notation=True,decimal_separator='comma')
102 res = res.replace("e^","\\text{e}^")
103 return res
104
105 def latex_frit_nbderiv_simpl(expr,var,pta,pos) :
106 tmpf = Lambda(var,expr)
107 res = (tmpf(var)-tmpf(pta))/(var-pta)
108 res = simplify(limit(res,var,pta,pos))
109 res = latex(res,ln_notation=True,decimal_separator='comma')
110 return res
111
112 #Patch pour +oo
113 def patch_oo(res) :
114 if res == '\\infty' :
115 return '+\\infty'
116 else :
117 return res
118
119 #Commande de forme exponentielle
120 def forme_expo(z) :
121 return abs(z)*exp(I*arg(z))
122
123 #Commande de calcul d'image
124 def imagefct(expr,var,pta) :
125 return expr.subs(var,pta)
126
127 \end{python}
128
129 %------Commandes de base
130 \NewDocumentCommand\sympycalc{ s D<>{Simplif} m }{%
131 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
132 \IfBooleanTF{#1}%*=frit, no*=fr
133 {%
134 \IfStrEq{#2}{Simplif}%
135 {%
136 \py{latex_frit_simpl(\argtmp)}%
137 }%
138 {%
139 \py{latex_frit(\argtmp)}%
140 }%
141 }%
142 {%
143 \IfStrEq{#2}{Simplif}%
144 {%
145 \py{latex_fr_simpl(\argtmp)}%
146 }%
147 {%
148 \py{latex_fr(\argtmp)}%
149 }%
150 }%
151 }
152 \NewDocumentCommand\dsympycalc{ s D<>{Simplif} m }{%
153 \IfBooleanTF{#1}%
154 {\displaystyle\sympycalc*<#2>{#3}}%
155 {\displaystyle\sympycalc<#2>{#3}}%
156 }
157
158 %------Image
159 \NewDocumentCommand\sympyimage{ s D<>{Simplif} m O{x} m }{%
160 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
161 \IfBooleanTF{#1}%*=frit, no*=fr
162 {%
163 \IfStrEq{#2}{Simplif}%
164 {%
165 \py{latex_frit_simpl(imagefct(\argtmp,#4,#5))}%
166 }%
167 {%
168 \py{latex_frit(imagefct(\argtmp,#4,#5))}%
169 }%
170 }%
171 {%
172 \IfStrEq{#2}{Simplif}%
173 {%
174 \py{latex_fr_simpl(imagefct(\argtmp,#4,#5))}%
175 }%
176 {%
177 \py{latex_fr(imagefct(\argtmp,#4,#5))}%
178 }%
179 }%
180 }
181 \NewDocumentCommand\dsympyimage{ s D<>{Simplif} m O{x} m }{%
182 \IfBooleanTF{#1}%
183 {\displaystyle\sympyimage*<#2>{#3}[#4]{#5}}%
184 {\displaystyle\sympyimage<#2>{#3}[#4]{#5}}%
185 }
186
187 %------Développer
188 \NewDocumentCommand\sympydev{ s D<>{Simplif} m O{x} }{%
189 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
190 \IfBooleanTF{#1}%*=frit, no*=fr
191 {%
192 \IfStrEq{#2}{Simplif}%
193 {%
194 \py{latex_frit_simpl(expand(\argtmp,#4))}%
195 }%
196 {%
197 \py{latex_frit(expand(\argtmp,#4))}%
198 }%
199 }%
200 {%
201 \IfStrEq{#2}{Simplif}%
202 {%
203 \py{latex_fr_simpl(expand(\argtmp,#4))}%
204 }%
205 {%
206 \py{latex_fr(expand(\argtmp,#4))}%
207 }%
208 }%
209 }
210 \NewDocumentCommand\dsympydev{ s D<>{Simplif} m O{x} }{%
211 \IfBooleanTF{#1}%
212 {\displaystyle\sympydev*<#2>{#3}[#4]}%
213 {\displaystyle\sympydev<#2>{#3}[#4]}%
214 }
215
216 %------Factoriser
217 \NewDocumentCommand\sympyfact{ s D<>{Simplif} m }{%
218 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
219 \IfBooleanTF{#1}%*=frit, no*=fr
220 {%
221 \IfStrEq{#2}{Simplif}%
222 {%
223 \py{latex_frit_simpl(factor(\argtmp))}%
224 }%
225 {%
226 \py{latex_frit(factor(\argtmp))}%
227 }%
228 }%
229 {%
230 \IfStrEq{#2}{Simplif}%
231 {%
232 \py{latex_fr_simpl(factor(\argtmp))}%
233 }%
234 {%
235 \py{latex_fr(factor(\argtmp))}%
236 }%
237 }%
238 }
239 \NewDocumentCommand\dsympyfact{ s D<>{Simplif} m }{%
240 \IfBooleanTF{#1}%
241 {\displaystyle\sympyfact*<#2>{#3}}%
242 {\displaystyle\sympyfact<#2>{#3}}%
243 }
244
245 %------Image
246
247 %------Dériver
248 \NewDocumentCommand\sympyderiv{ s D<>{Simplif} m O{x} }{%
249 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
250 \IfBooleanTF{#1}%*=frit, no*=fr
251 {%
252 \IfStrEq{#2}{Simplif}%
253 {%
254 \py{latex_frit_simpl(diff(\argtmp,#4))}%
255 }%
256 {%
257 \py{latex_frit(diff(\argtmp,#4))}%
258 }%
259 }%
260 {%
261 \IfStrEq{#2}{Simplif}%
262 {%
263 \py{latex_fr_simpl(diff(\argtmp,#4))}%
264 }%
265 {%
266 \py{latex_fr(diff(\argtmp,#4))}%
267 }%
268 }%
269 }
270 \NewDocumentCommand\dsympyderiv{ s D<>{Simplif} m O{x} }{%
271 \IfBooleanTF{#1}%
272 {\displaystyle\sympyderiv*<#2>{#3}[#4]}%
273 {\displaystyle\sympyderiv<#2>{#3}[#4]}%
274 }
275
276 %------Primitiver
277 \NewDocumentCommand\sympyprim{ s D<>{Simplif} m O{x} }{%
278 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
279 \IfBooleanTF{#1}%*=frit, no*=fr
280 {%
281 \IfStrEq{#2}{Simplif}%
282 {%
283 \py{latex_frit_simpl(integrate(\argtmp,#4))}%
284 }%
285 {%
286 \py{latex_frit(integrate(\argtmp,#4))}%
287 }%
288 }%
289 {%
290 \IfStrEq{#2}{Simplif}%
291 {%
292 \py{latex_fr_simpl(integrate(\argtmp,#4))}%
293 }%
294 {%
295 \py{latex_fr(integrate(\argtmp,#4))}%
296 }%
297 }%
298 }
299 \NewDocumentCommand\dsympyprim{ s D<>{Simplif} m O{x} }{%
300 \IfBooleanTF{#1}%
301 {\displaystyle\sympyprim*<#2>{#3}[#4]}%
302 {\displaystyle\sympyprim<#2>{#3}[#4]}%
303 }
304
305 %------Intégrer
306 \NewDocumentCommand\sympyintegr{ s D<>{Simplif} m O{x} m m }{%
307 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
308 \IfBooleanTF{#1}%*=frit, no*=fr
309 {%
310 \IfStrEq{#2}{Simplif}%
311 {%
312 \py{patch_oo(latex_frit_simpl(integrate(\argtmp,(#4,#5,#6))))}%
313 }%
314 {%
315 \py{patch_oo(latex_frit(integrate(\argtmp,(#4,#5,#6))))}%
316 }%
317 }%
318 {%
319 \IfStrEq{#2}{Simplif}%
320 {%
321 \py{patch_oo(latex_fr_simpl(integrate(\argtmp,(#4,#5,#6))))}%
322 }%
323 {%
324 \py{patch_oo(latex_fr(integrate(\argtmp,(#4,#5,#6))))}%
325 }%
326 }%
327 }
328 \NewDocumentCommand\dsympyintegr{ s D<>{Simplif} m O{x} m m }{%
329 \IfBooleanTF{#1}%
330 {\displaystyle\sympyintegr*<#2>{#3}[#4]{#5}{#6}}%
331 {\displaystyle\sympyintegr<#2>{#3}[#4]{#5}{#6}}%
332 }
333
334 %------Limite
335 \NewDocumentCommand\sympylim{ s D<>{Simplif} m O{x} m O{} }{%
336 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
337 \def\poslim{'+-'}
338 \IfStrEq{#6}{g}%
339 {\def\poslim{'-'}}{}
340 \IfStrEq{#6}{d}%
341 {\def\poslim{'+'}}{}
342 \IfBooleanTF{#1}%*=frit, no*=fr
343 {%
344 \IfStrEq{#2}{Simplif}%
345 {%
346 \py{patch_oo(latex_frit_simpl(limit(\argtmp,#4,#5,\poslim)))}%
347 }%
348 {%
349 \py{patch_oo(latex_frit(limit(\argtmp,#4,#5,\poslim)))}%
350 }%
351 }%
352 {%
353 \IfStrEq{#2}{Simplif}%
354 {%
355 \py{patch_oo(latex_fr_simpl(limit(\argtmp,#4,#5,\poslim)))}%
356 }%
357 {%
358 \py{patch_oo(latex_fr(limit(\argtmp,#4,#5,\poslim)))}%
359 }%
360 }%
361 }
362 \NewDocumentCommand\dsympylim{ s D<>{Simplif} m O{x} m O{} }{%
363 \IfBooleanTF{#1}%
364 {\displaystyle\sympylim*<#2>{#3}[#4]{#5}[#6]}%
365 {\displaystyle\sympylim<#2>{#3}[#4]{#5}[#6]}%
366 }
367
368 %------Nb deriv
369 \NewDocumentCommand\sympynbderiv{ s D<>{Simplif} m O{x} m O{} }{%
370 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
371 \def\poslim{'+-'}
372 \IfStrEq{#6}{g}%
373 {\def\poslim{'-'}}{}
374 \IfStrEq{#6}{d}%
375 {\def\poslim{'+'}}{}
376 \IfBooleanTF{#1}%*=frit, no*=fr
377 {%
378 \IfStrEq{#2}{Simplif}%
379 {%
380 \py{patch_oo(latex_fr_nbderiv_simpl(\argtmp,#4,#5,\poslim))}%
381 }%
382 {%
383 \py{patch_oo(latex_frit_nbderiv_simpl(\argtmp,#4,#5,\poslim))}%
384 }%
385 }%
386 {%
387 \IfStrEq{#2}{Simplif}%
388 {%
389 \py{patch_oo(latex_fr_nbderiv(\argtmp,#4,#5,\poslim))}%
390 }%
391 {%
392 \py{patch_oo(latex_frit_nbderiv(\argtmp,#4,#5,\poslim))}%
393 }%
394 }%
395 }
396 \NewDocumentCommand\dsympynbderiv{ s D<>{Simplif} m O{x} m O{} }{%
397 \IfBooleanTF{#1}%
398 {\displaystyle\sympynbderiv*<#2>{#3}[#4]{#5}[#6]}%
399 {\displaystyle\sympynbderiv<#2>{#3}[#4]{#5}[#6]}%
400 }
401
402 %------FormeExpo
403 \NewDocumentCommand\sympyfexpo{ s D<>{NoSimplif} m }{%
404 %\StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
405 \IfBooleanTF{#1}%*=frit, no*=fr
406 {%
407 \IfStrEq{#2}{Simplif}%
408 {%
409 \py{latex_frit_simpl(forme_expo(#3))}%
410 }%
411 {%
412 \py{latex_frit(forme_expo(#3))}%
413 }%
414 }%
415 {%
416 \IfStrEq{#2}{Simplif}%
417 {%
418 \py{latex_fr_simpl(forme_expo(#3))}%
419 }%
420 {%
421 \py{latex_fr(forme_expo(#3))}%
422 }%
423 }%
424 }
425 \NewDocumentCommand\dsympyfexpo{ s D<>{NoSimplif} m }{%
426 \IfBooleanTF{#1}%
427 {\displaystyle\sympyfexpo*<#2>{#3}}%
428 {\displaystyle\sympyfexpo<#2>{#3}}%
429 }
430
431 %------Solutions (R ou C)
432 \NewDocumentCommand\sympyresol{ s D<>{Simplif} m O{x} }{%
433 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
434 \IfBooleanTF{#1}%*=frit, no*=fr
435 {%
436 \py{latex_frit_resol('\argtmp',#4)}%
437 }%
438 {%
439 \py{latex_fr_resol('\argtmp',#4)}%
440 }%
441 }
442 \NewDocumentCommand\dsympyresol{ s D<>{Simplif} m O{x} }{%
443 \IfBooleanTF{#1}%
444 {\displaystyle\sympyresol*<#2>{#3}[#4]}%
445 {\displaystyle\sympyresol<#2>{#3}[#4]}%
446 }
447
448 \NewDocumentCommand\sympyresolC{ s D<>{Simplif} m O{x} }{%
449 \StrSubstitute{#3}{/}{*S.One/}[\argtmp]%
450 \IfBooleanTF{#1}%*=frit, no*=fr
451 {%
452 \py{latex_frit_resolC('\argtmp',#4)}%
453 }%
454 {%
455 \py{latex_fr_resolC('\argtmp',#4)}%
456 }%
457 }
458 \NewDocumentCommand\dsympyresolC{ s D<>{Simplif} m O{x} }{%
459 \IfBooleanTF{#1}%
460 {\displaystyle\sympyresolC*<#2>{#3}[#4]}%
461 {\displaystyle\sympyresolC<#2>{#3}[#4]}%
462 }
463
464 \endinput