mise en forme des algos de devoirs

This commit is contained in:
Pierre-antoine Comby 2019-04-30 18:28:33 +02:00
parent bb08d92f23
commit 45afaf066f
5 changed files with 18 additions and 44 deletions

View file

@ -2,13 +2,13 @@
\begin{document}
\section{Codage d'Huffman}
\subsection{version simple}
\inputminted{python}{../algo_code/huffman.py}
\subsection{version objet}
\inputminted{python}{../algo_code/huffman2.py}
\newpage
\section{Codage arithmétique}
\inputminted{python}{../algo_code/code_arithmetique.py}
\newpage
\section{Codage LZW}
\inputminted{python}{../algo_code/LZW.py}
\section{Quantification}
@ -25,7 +25,8 @@ Evaluer le KLT une restriction à $m$ composante pour un signal sonore. Tracer l
\end{document}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: main.tex
%%% TeX-master: "main"
%%% End:

View file

@ -5,7 +5,9 @@
\let\shaded\relax \let\endshaded\relax
\let\leftbar\relax \let\endleftbar\relax
\let\snugshade\relax \let\endsnugshade\relax
\usepackage{minted}
\usepackage{minted}
\setminted{frame=lines, framesep=2mm, baselinestretch=1, fontsize=\footnotesize,linenos}
% Mise en page
\renewcommand{\vec}{\mathbf}
@ -16,7 +18,8 @@
\begin{document}
\maketitle
\tableofcontents
\chapter*{Rappel de probabilité}
\setcounter{chapter}{-1}
\chapter{Rappel de probabilité}
\subfile{chap0_MAN.tex}
\chapter{Introduction - Motivation}
\subfile{chap0.tex}

View file

@ -2,28 +2,15 @@
import numpy as np
N=10
p = 0.2
P = np.random.rand(N)
X = np.zeros(N,dtype='int')
for i in range(N):
if P[i] > p:
X[i] = 1
X=[0,1,0,0,1,0,0,0,0,0]
print(X)
def binary(n,m,b=2):
"""
Convertie un nombre décimal en sa version binaire tronqué à m bits.
"""
# Convertie un nombre décimal en sa version binaire tronqué à m bits.
binaire= np.floor(n*b**m) # on se décale dans les entiers et on floor
return binaire,np.binary_repr(int(binaire))
def arithm(X,p):
l=[0]
h= [1]
l=[0]; h= [1]
for x in X:
if x == 0:
h.append(l[-1]+p*(h[-1]-l[-1]))
@ -31,18 +18,13 @@ def arithm(X,p):
else:
l.append(l[-1]+p*(h[-1]-l[-1]))
h.append(h[-1])
lmb = (l[-1]+h[-1])/2
mu = int(-np.log2(h[-1]-l[-1]))+1
code = binary(lmb,mu)
return code,lmb,mu
def arithm_pratique(X,p):
l = [0] # borne inférieur
h =[1] # borne supérieur
f = 0 # follow
c =[] # code
l = [0]; h =[1] ;f = 0;c =[] #inf, sup,follow, code
for k in range(len(X)):
print("for loop")
if X[k] == 0:
@ -51,28 +33,18 @@ def arithm_pratique(X,p):
else:
l.append(l[-1]+p*(h[-1]-l[-1]))
h.append(h[-1])
print(X[k])
print(l[-3:])
print(h[-3:])
while ((l[-1]>=0 and h[-1]<0.5)
or (l[-1]>=0.5 and h[-1]<1)
or (l[-1]>= 0.25 and h[-1]<0.75)):
print(" loop")
while ((l[-1]>=0 and h[-1]<0.5) or (l[-1]>=0.5 and h[-1]<1) or (l[-1]>= 0.25 and h[-1]<0.75)):
if (l[-1]>=0 and h[-1]<0.5):
print(" case 1")
c += [0]+[1]*f
l[-1] *=2
h[-1] *=2
elif (l[-1]>=0.5 and h[-1]<1):
print(" case 2")
c += [1]+[0]*f
l[-1] = 2*l[-1]-1
h[-1] = 2*h[-1]-1
elif (l[-1]>= 0.25 and h[-1]<0.75):
print(" case 3")
f +=1
l[-1] = 2*l[-1]-0.5
h[-1] = 2*h[-1]-0.5
return c
#print(arithm(X,p))
print(arithm_pratique(X,p))

View file

@ -1,7 +1,5 @@
#!/usr/bin/python3
import numpy as np
def get_2min(l):
min1 = 0
min2 = 1
@ -29,9 +27,7 @@ def huffman_rec(p):
C[min1] +='0'
p.insert(min2,p_save)
p[min1] -= p_save
print(p,C)
return C
p = [25,20,15,12,10,8,5,5]
print(huffman_rec(p))

View file

@ -17,11 +17,11 @@ class Noeud(object):
return self.p<other.p
def __repr__(self):
return self.name
def create_tree(table_noeud):
queue = table_noeud.copy()
while len(queue) > 2:
queue.sort()
print(queue)
l=queue.pop(0)
r=queue.pop(0)
queue.append(Noeud(left=l,right=r))
@ -41,10 +41,12 @@ def gen_code(node,prefix=''):
x = gen_code_rec(node,prefix)
return x
#affichage à l'aide de graphviz
def draw_tree(node):
if len(node.name) == 1: # feuille
desc ='N{} [label="{}:{}", fontcolor=blue, fontsize=16, width=2, shape=box];\n'.format(node.code, node.name, node.code)
desc ='N{} [label="{}:{}",\
fontcolor=blue, fontsize=16,\
width=2, shape=box];\n'.format(node.code, node.name, node.code)
else:
desc = 'N{} [label="{}"];\n'.format(node.code,node.code)
desc += draw_tree(node.left)