TIPE2021/Test.ml

156 lines
3.6 KiB
OCaml

#cd "/home/mysaa/Documents/Arbeiten/TIPE2021";;
#use "Codes.ml";;
(* Test du produit de matrice *)
let matest = [0b01110; 0b00101; 0b10111];;
print_matrice 5 matest;;
produit matest 0b110;; (* -> 0b10010 = 8*)
(* Tests des polynomes *)
let pol1 = 13 and pol2 = 67;;
print_polynome pol1;;
print_polynome pol2;;
print_polynome (polmul pol1 pol2);;
let qt,rst=(poldiveuc pol2 pol1) in
print_polynome qt;
print_polynome rst;;
(* Test des fonctions de base *)
deux_puissance 11;;
identite 3;;
changer_bit 2 6;;
respecter 7 [3];;
(* Test des Codes *)
(* Un classique : code de Hamming (4, 7)
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 1 1 0 1 0 0
1 1 0 1 0 1 0
1 0 1 1 0 0 1
-> les 4 premières colonnes : G
-> les 3 dernières lignes : H
*)
let code_hamming =
construire_code_lineaire_systematique 4 7 [7; 3; 5; 6]
;;
distance_minimale code_hamming;;
exception GTROUVE of matrice;;
for i=0 to 20
do
Random.self_init ();
let matPapa = matriceAuPif 9 12 in
let code_paparfait = construire_code_lineaire_systematique 12 21 matPapa in
let dst = distance_minimale code_paparfait in
if dst>1
then raise (GTROUVE matPapa)
done;;
print_vecteur 21 (encoder code_paparfait 0b011011011001);;
print_matrice 3 (suivants 3 (suivants 3 (suivants 3 (suivants 3 [0b000]))));;
print_vecteur 7 (encoder code_hamming 0b0100);;
decoder code_hamming 0b1010100;;
decoder code_hamming 0b0010100;;
decoder code_hamming 0b1110000;;
print_vecteur 7 21;;
(* Tests des codes cycliques *)
let cocycl = {ncyc=7;kcyc=4;pol=13};;
print_polynome cocycl.pol;;
poldiveuc ((deux_puissance 7) +1) cocycl.pol;;
print_polynome ((deux_puissance 7) +1);;
print_polynome (poldiv ((deux_puissance 7) +1) cocycl.pol);;
let cocylined = cycliqueVersLineaire cocycl;;
print_matrice 7 cocylined.g;;
print_matrice 4 cocylined.h;;
distance_minimale cocylined;;
print_vecteur 7 (cyclencode cocycl 0b1010);;
(* Essayons de générer une table d'addition *)
type element = Zero | Ap of int;;
exception NotApException;;
let getap a = match a with
| Zero -> raise NotApException
| Ap(i) -> i;;
let add tble i j =
let n = (Array.length tble) +1 in
if i=j then Zero else
match i,j with
| Zero,x | x,Zero -> x
| (Ap(0),Ap(k)) -> tble.(k-1)
| (Ap(k),Ap(0)) -> tble.(k-1)
| (Ap(ii),Ap(jj)) -> let tt = getap (tble.(((jj-ii+n) mod n)-1)) in
Ap((tt+ii) mod n);;
let rangearray i j =
let out = Array.make (j-i+1) 0 in
for k=i to j do
out.(k-i) <- k
done;
out;;
let randtabl n =
let tab = rangearray 1 (n-1) in
Random.self_init ();
for i=n-2 downto 1 do
let j = Random.int (i+1) in
let a,b=tab.(i),tab.(j) in
tab.(i) <- b;
tab.(j) <- a
done;
let rout = Array.make (n-1) Zero in
for i=0 to n-1-1 do
rout.(i) <- Ap(tab.(i))
done;
rout;;
randtabl n;;
exception PasTransitifException;;
let estTransitif tble =
let n = Array.length tble +1 in
try
for i=1 to (n-1) do
for j=1 to (i-1) do (* i et j distincts et non nuls (transititivité évidente)*)
(* On teste si (0+i)+j = (0+j)+i *)
let sa = add tble (add tble (Ap(0)) (Ap(i))) (Ap(j)) in
let sb = add tble (add tble (Ap(0)) (Ap(j))) (Ap(i)) in
if sa<>sb then raise PasTransitifException
done
done;
true
with PasTransitifException -> false;;
let printalp a = match a with
| Zero -> print_string "o"
| Ap(i) -> print_int i;;
let arr=[|Ap(3);Ap(6);Ap(1);Ap(5);Ap(4);Ap(2)|] in
for i=0 to n-2 do printalp arr.(i) done;
print_endline"";
estTransitif arr;;
let arr=randtabl n in
for i=0 to n-2 do printalp arr.(i) done;
print_endline"";
estTransitif arr;;
let n = 7 (* alpha est racine neme de l'unité *);;
for i=0 to 100_000 do
let arr=randtabl n in
if estTransitif arr;
then (for i=0 to n-2 do printalp arr.(i) done;
print_endline"")
done;;