La déformation de Gauß fonctionne !

This commit is contained in:
Mysaa 2021-06-05 12:45:29 +02:00
parent 20b45e9071
commit 5bcae43999
4 changed files with 185 additions and 14 deletions

View File

@ -7,7 +7,9 @@ type t = {
h : Math.matrice; }
type code_lineaire = t
exception PasDansLeCodeException
exception IndecodableException
val antecedent : code_lineaire -> Math.vecteur -> Math.vecteur
val encoder : code_lineaire -> Math.vecteur -> Math.vecteur
val systematiqueFromRedondance : int -> int -> Math.matrice -> code_lineaire
val distance_minimale : code_lineaire -> int

View File

@ -1,3 +1,4 @@
.PHONY: ascii tipe ocshell runall clean
%.ascii.txt: %.txt
iconv -f utf-8 -t ascii//TRANSLIT $< -o $@
@ -20,6 +21,9 @@ tipe: tipe.cmx
%.txt.test: %.ascii.txt tipe
./tipe $<
ocshell:
rlwrap ocaml graphics.cma images.cmo Math.cmo Code.cmo
runall: $(shell find textes/ -type f -iname "*.txt" ! -iname "*.ascii.txt" | sed "s/\.txt/\.txt\.test/")
clean:

106
Test.ml
View File

@ -10,20 +10,22 @@ let print_boolean b = match b with
| true -> print_string "true"
| false -> print_string "false";;
let codeh = CLineaire.systematiqueFromRedondance 4 7 [7; 3; 5; 6];;
let vecteurs = matriceAuPif 9 (8-1);;
let codeh = CLineaire.systematiqueFromRedondance 4 7 [3;6;9;12];;
let vecteurs = matriceAuPif 99 (8-1);;
print_matrice 4 vecteurs;;
let classes = CLineaire.genererClasses codeh;;
let test v = try
CLineaire.encoder codeh (CLineaire.decoder codeh v) <> CLineaire.decoder2 classes v
with
Code.CLineaire.PasDansLeCodeException -> (print_int v;print_endline "*\n";false) in
Code.CLineaire.PasDansLeCodeException -> (print_int v;print_endline "*";false)
| CLineaire.IndecodableException -> (print_int v;print_endline "-";false) in
List.find_opt test vecteurs;;
CLineaire.decoder codeh 119;;
CLineaire.decoder codeh 49;;
CLineaire.encoder codeh 1;;
CLineaire.decoder2 classes 119;;
CLineaire.decoder2 classes 49;;
exit 0;;
(* Test du produit de matrice *)
let matest = [0b01110; 0b00101; 0b10111];;
print_matrice 5 matest;;
@ -98,6 +100,94 @@ CLineaire.distance_minimale cocylined;;
print_vecteur 9 203;;
(****** Construction des classes cyclotomiques pour un degré n ******)
let classes n =
let rech = Array.make n false in
let lsts = ref [] in
for i=1 to (n-1)
do
if not rech.(i)
then
begin
let v = ref (i lsl 1) in
let lst = ref [i] in
rech.(i) <- true;
while !v<>i
do
rech.(!v) <- true;
lst := !v::!lst;
v := (!v lsl 1) mod n(*Fois 2*)
done;
lsts := !lst::!lsts
end
done;
!lsts;;
let clzLst = let e::s = classes 31 in e;;
let tbleLst = let e::s = trouve 31 in e;;
let clz = List.map (fun x -> Ap(x)) clzLst;;
let tble = Array.map (fun x -> Ap(x)) tbleLst;;
(* Multiplie le polynome arr par chaque élément de la classe. On a deg(out)=d*)
let rec mulan tble arr clz d =
match clz with
| [] -> ()
| a::rr ->
mulan tble arr rr (d-1);
arr.(d) <- arr.(d-1);
for i=d-1 downto 1
do
arr.(i) <- add tble arr.(i-1) (mul tble a arr.(i))
done;
arr.(0) <- mul tble a arr.(0)
;;
let pols clz tble =
let len = List.length clz in
let poly = Array.make (len+1) Zero in
poly.(0) <- Ap(0);
mulan tble poly clz (len);
poly;;
let rec polof tble p a =
if p=0 then Zero else
let cst = if (p mod 2=0) then Zero else Ap(0) in
let q = p lsr 1 in
add tble cst (mul tble a (polof tble q a));;
let pomin tble a =
if a=Zero then 2 else
let i = ref 1 in
while polof tble !i a <> Zero
do
incr i;
incr i
done;
!i;;
polof tble 4 (Ap(2));;
for i=0 to 31 do
print_int i;
print_string "->";
print_polynome (pomin tble (Ap(i)))
done;;
pols clz tble;;
(* Essayons de générer une table d'addition *)
(**** Utilitaires ****)
@ -181,6 +271,12 @@ let add tble i j =
| (Ap(ii),Ap(jj)) -> let tt = getap (tble.(((jj-ii+n) mod n)-1)) in
Ap((tt+ii) mod n);;
let mul tble i j =
let n = (Array.length tble)+1 in
match i,j with
| Zero,_ | _,Zero -> Zero
| Ap(i),Ap(j) -> Ap((i+j) mod n);;
let randtabl n =
let tab = rangearray 1 (n-1) in
Random.self_init ();

View File

@ -3,26 +3,95 @@ open Images ;;
open Math;;
open Code;;
let img = lire_image "Documents/gauss.png";;
let img = lire_image "Documents/gauss_low.png";;
let alpha = 0.95;;
let n = 24;;
let nbreErr () =
let v = Random.float 1. in
if (0.<=v && v<0.9) then 0
else if (0.9<=v && v<0.99) then 1
else if (0.99<v && v<0.999) then 2
else 3;;
let rec bruiteur n a =
if Random.float 1. < alpha
then
let v = nbreErr () in
let x = ref a in
for i=1 to v
do
let erreur = 1 lsl (Random.int n) in
bruiteur n (a lxor erreur)
else a;;
x:=(!x lxor erreur)
done;
!x;;
let larg = Array.length img;;
let long = Array.length img.(0);;
let transmet code classes vv =
try
let y=CLineaire.encoder code vv in
let z=bruiteur 7 y in
let x=CLineaire.decoder code z in
x
with CLineaire.IndecodableException -> 0
| CLineaire.PasDansLeCodeException -> 0
| NoSuchKeyException -> 0xF;;
let transmettre code classes v =
let masque = 0b1111 in
let x0 = masque land v
and x1 = masque land (v lsr 4)
and x2 = masque land (v lsr 8)
and x3 = masque land (v lsr 12)
and x4 = masque land (v lsr 16)
and x5 = masque land (v lsr 20) in
let y0 = masque land (transmet code classes x0)
and y1 = masque land (transmet code classes x1)
and y2 = masque land (transmet code classes x2)
and y3 = masque land (transmet code classes x3)
and y4 = masque land (transmet code classes x4)
and y5 = masque land (transmet code classes x5) in
(y0) lor (y1 lsl 4) lor (y2 lsl 8) lor (y3 lsl 12) lor (y4 lsl 16) lor (y5 lsl 20);;
let transmetGros code classes v =
try
let y=CLineaire.encoder code (v land 0xFFFFFF) in
let z=bruiteur 31 y in
let x=CLineaire.decoder code y in
x land 0xFFFFFF
with CLineaire.IndecodableException -> 0
| CLineaire.PasDansLeCodeException -> 0
| NoSuchKeyException -> 0xF;;
(* classes de Hamming (4,7) *)
let code = CLineaire.systematiqueFromRedondance 4 7 [7; 3; 5; 6];;
let codeG = CLineaire.systematiqueFromRedondance 4 7 (matriceAuPif 3 4);;
let classes = CLineaire.genererClasses code;;
let classesG = CLineaire.genererClasses codeG;;
for i=0 to 177
do
print_int i;
print_string "->";
print_int (transmettre codeG classesG i);
print_endline ";"
done;;
for i=0 to larg-1 do
print_string "\r";
print_int i;
for j=0 to long-1 do
img.(i).(j) <- bruiteur n img.(i).(j)
img.(i).(j) <- transmettre code classes img.(i).(j)
done
done;;
(*for i=0 to larg-1 do
for j=0 to long-1 do
img.(i).(j) <- transmetGros codeG classesG img.(i).(j)
done
done;;*)
sauver_image img "gaussV2.png";;
sauver_image img "gaussV2-low.png";;