Debian Codegraph @

wxgeometrie/modules/probabilites/repetition.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------#######
4 # Probabilités #
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 from PyQt5.QtWidgets import QDialog
25
26 from sympy import S
27 from sympy.core.sympify import SympifyError
28
29 from .repetition_ui import Ui_DialogRepetition
30 from wxgeometrie.geolib.routines import nice_str
31
32
33 def repetition_experiences(_profondeur=3, _numeroter=True, evts=[], probas=[]):
34 """Génère le code d'un arbre de probabilités correspondant à la répétition
35 d'expériences aléatoires identiques et indépendantes.
36 Typiquement, un schéma de Bernoulli.
37
38 >>> from wxgeometrie.modules.probabilites import repetition_experiences
39 >>> print(repetition_experiences())
40 >A_1:0.5
41 >>A_2:0.5
42 >>>A_3:0.5
43 >>>&A_3:0.5
44 >>&A_2:0.5
45 >>>A_3:0.5
46 >>>&A_3:0.5
47 >&A_1:0.5
48 >>A_2:0.5
49 >>>A_3:0.5
50 >>>&A_3:0.5
51 >>&A_2:0.5
52 >>>A_3:0.5
53 >>>&A_3:0.5
54 """
55 #FIXME: rajouter des tests unitaires.
56 if not evts:
57 evts = ['A']
58 if len(evts) == 1:
59 # On rajoute automatiquement l'évènement contraire.
60 evt = evts[0]
61 evts.append(evt[1:] if evt.startswith('&') else '&' + evt)
62
63 # On complète pour que la liste des évènements et celle des probabilités
64 # aient la même taille.
65 if len(probas) > len(evts):
66 evts += (len(probas) - len(evts))*['']
67 else:
68 if len(probas) == len(evts) - 1:
69 try:
70 probas.append(nice_str(1 - sum(S(proba.replace(',', '.')) for proba in probas)))
71 except SympifyError:
72 pass
73 if len(probas) < len(evts):
74 probas += (len(evts) - len(probas))*['']
75
76 # On génère le code.
77 lines = ['']
78 for niveau in range(1, _profondeur + 1):
79 prefixe = niveau*'>'
80 suffixe = ('_' + str(niveau) if _numeroter else '')
81 for i in range(len(lines), 0, -1):
82 if lines[i - 1].startswith((niveau - 1)*'>'):
83 for evt, proba in reversed(list(zip(evts, probas))):
84 #~ proba = nice_str(proba) if proba != '' else ''
85 lines.insert(i, prefixe + evt + suffixe + ':' + proba)
86 assert len(lines) < 10000
87 return '\n'.join(lines).strip()
88
89
90
91 class DialogRepetition(QDialog, Ui_DialogRepetition):
92 def __init__(self, parent):
93 QDialog.__init__(self, parent)
94
95 # Set up the user interface from Designer.
96 self.setupUi(self)
97
98 def accept(self):
99 n = self.niveaux.value()
100 num = self.numeroter.isChecked()
101 evts = [evt.strip() for evt in self.evenements.text().split(';')]
102 probas = [proba.strip() for proba in self.probas.text().split(';')]
103
104 code = repetition_experiences(n, num, evts, probas)
105 self.parent().instructions.setPlainText(code)
106 self.parent().appliquer.click()