diff --git a/src/google.adb b/src/google.adb new file mode 100644 index 0000000..ddfc83b --- /dev/null +++ b/src/google.adb @@ -0,0 +1,213 @@ +package body Google is + + package body Naif is + + function "*"(left : Vector_Element.T_Vecteur; right : T_Google) return Vector_Element.T_Vecteur is + vec: Vector_Element.T_Vecteur; + c: T_Reel; + begin + for i in 0..N-1 loop + c := 0.0; + for j in 0..N-1 loop + c := c + left(j)*right(j,i); + end loop; + vec(i) := c; + end loop; + return vec; + end "*"; + + procedure initialize(mat: in out T_Google; value: in T_Reel) is + begin + for i in 0..N-1 loop + for j in 0..N-1 loop + mat(i,j) := value; + end loop; + end loop; + end initialize; + + procedure create_H(mat: in out T_Google; file: in Ada.Text_IO.File_Type) is + row, col: Natural; + nc: Natural := 0; + nl: Natural := 0; + begin + for i in 0..N_Links-1 loop + get(file, row); + get(file, col); + mat(row, col) := 1.0; + end loop; + + for i in 0..N-1 loop + for j in 0..N-1 loop + exit when mat(i, j) /= 0.0; + if j = N-1 then + nl := nl + 1; + end if; + end loop; + end loop; + put("lignes vides = "); put(nl, 1); + new_line; + + for j in 0..N-1 loop + for i in 0..N-1 loop + exit when mat(i, j) /= 0.0; + if i = N-1 then + nc := nc + 1; + end if; + end loop; + end loop; + put("colonnes vides = "); put(nc, 1); + new_line; + + end create_H; + + procedure create_S(mat: in out T_Google) is + sum: T_Reel; + begin + for i in 0..N-1 loop + sum := 0.0; + for j in 0..N-1 loop + sum := sum + mat(i,j); + end loop; + + if sum /= 0.0 then + for j in 0..N-1 loop + if mat(i,j) /= 0.0 then + mat(i,j) := 1.0/sum; + end if; + end loop; + else -- sum == 0 then + for j in 0..N-1 loop + mat(i,j) := 1.0/T_Reel(N); + end loop; + end if; + end loop; + end create_S; + + procedure create_G(mat: in out T_Google; alpha: in T_Reel) is + begin + for i in 0..N-1 loop + for j in 0..N-1 loop + mat(i,j) := alpha*mat(i,j) + (1.0-alpha)/T_Reel(N); + end loop; + end loop; + end create_G; + + procedure create_Google(mat: in out T_Google; alpha: in T_Reel; file: in Ada.Text_IO.File_Type) is + begin + + create_H(mat, file); + create_S(mat); + create_G(mat, alpha); + + end create_Google; + + procedure put(mat: in T_Google) is + begin + for i in 0..N-1 loop + for j in 0..N-1 loop + put(mat(i,j)); + end loop; + new_line; + end loop; + end put; + + end Naif; + + + + package body Creux is + + procedure create_Google(mat: in out T_Google; file: in out Ada.Text_IO.File_Type) is + row, col: Natural; + offset: Natural; + begin + + initialize(mat.Rows, 0); + initialize(mat.Cols, -1); + + for i in 0..N_Links-1 loop + get(file, row); + get(file, col); + mat.rows(row+1) := mat.rows(row+1) + 1; + end loop; + + repartition(mat.rows); + + reset(file, In_File); + skip_line(file); + + for i in 0..N_Links-1 loop + get(file, row); + get(file, col); + + if row = N then + offset := 0; + else + offset := mat.rows(row+1) - mat.rows(row); + end if; + + for j in 0..offset loop + + exit when mat.cols(mat.rows(row)+j) = col; -- anti-doublon + + if mat.cols(mat.rows(row)+j) = -1 then + mat.cols(mat.rows(row)+j) := col; + exit; + end if; + + end loop; + + end loop; + + end create_Google; + + procedure put(mat: in T_Google) is + begin + put("rows:"); + for i in 0..N loop + put(mat.rows(i)); + end loop; + new_line; new_line; + put("cols:"); + for i in 0..N_Links-1 loop + put(mat.cols(i)); + end loop; + end put; + + function calcul(vec: in Vector_Element.T_Vecteur; mat: in T_Google; alpha: in T_Reel) return Vector_Element.T_Vecteur is + new_vec: Vector_Element.T_Vecteur; + nb_col: Natural; + index_col: Integer; + begin + + initialize(new_vec, sum(vec)*(1.0-alpha)/T_Reel(N)); + + for i in 0..N-1 loop + + nb_col := mat.rows(i+1) - mat.rows(i); + + if nb_col = 0 then + + for j in 0..N-1 loop + new_vec(j) := new_vec(j) + alpha*vec(i)/T_Reel(N); + end loop; + + else + + for j in mat.rows(i)..mat.rows(i+1)-1 loop + index_col := mat.cols(j); + if index_col /= -1 then -- anti calcul d'un doublon + new_vec(index_col) := new_vec(index_col) + alpha*vec(i)/T_Reel(nb_col); + end if; + end loop; + + end if; + + end loop; + + return new_vec; + end calcul; + + end Creux; + +end Google; \ No newline at end of file diff --git a/src/google.ads b/src/google.ads new file mode 100644 index 0000000..cd73475 --- /dev/null +++ b/src/google.ads @@ -0,0 +1,80 @@ +with Vector; +with Ada.Text_IO; use Ada.Text_IO; +with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; + +-- tester de mettre le generic ici ? + +package Google is + + generic + + -- N peut être supprimé et recup direct grace vecteur_element + N: Positive; + N_Links: Positive; + + -- Vecteur contenant les élements du même type que la matrice + with package Vector_Element is new Vector.Reel(<>); + + package Naif is + + use Vector_Element; + use Vector_Element.Text_T_Reel; + + type T_Google is array (0..N-1, 0..N-1) of T_Reel; + + -- permet d'initialiser la matrice avec pour tout i,j, mat[i, j] = value + procedure initialize(mat: in out T_Google; value: in T_Reel); + + -- permet la création de la matrice de Google, voir l'énoncé + procedure create_Google(mat: in out T_Google; alpha: in T_Reel; file: in Ada.Text_IO.File_Type); + + -- permet le produit vecteur-matrice + function "*"(left: Vector_Element.T_Vecteur; right: T_Google) return Vector_Element.T_Vecteur; + + -- permet l'affiche de la matrice + procedure put(mat: in T_Google); + + end Naif; + + + + generic + + -- idem ici + N: Positive; + N_Links: Positive; + + -- Vecteur contenant les élements du même type que la matrice + with package Vector_Element is new Vector.Reel(<>); + + package Creux is + + use Vector_Element; + use Vector_Element.Text_T_Reel; + + package Vector_Rows is + new Vector.Entier(T_Entier => Natural, Capacite => N+1); + package Vector_Cols is + new Vector.Entier(T_Entier => Integer, Capacite => N_Links); + + use Vector_Rows; + use Vector_Cols; + + -- on définit le type T_Google correspondant à une matrice CSC + type T_Google is record + rows: Vector_Rows.T_Vecteur; + cols: Vector_Cols.T_Vecteur; + end record; + + -- permet de créer la matrice à partir du réseau + procedure create_Google(mat: in out T_Google; file: in out Ada.Text_IO.File_Type); + + -- permet faire le ~produit vecteur-matrice + function calcul(vec: in Vector_Element.T_Vecteur; mat: in T_Google; alpha: in T_Reel) return Vector_Element.T_Vecteur; + + -- permet l'affichage de la matrice, voir l'article wikiepdia pour comprendre format + procedure put(mat: in T_Google); + + end Creux; + +end Google; \ No newline at end of file diff --git a/src/google_creux.adb b/src/google_creux.adb deleted file mode 100644 index a9f4bcd..0000000 --- a/src/google_creux.adb +++ /dev/null @@ -1,65 +0,0 @@ -with Ada.Text_IO; use Ada.Text_IO; -with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; - -package body Google_Creux is - - procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur) is - row, col: Natural; - row_last: Natural; - nb: Integer := 1; - begin - - mat.rows(N) := N_links; - mat.rows(0) := network(0).from; - mat.cols(0) := network(0).to; - row_last := mat.rows(0); - - for i in 1..N_links-1 loop - row := network(i).from; - col := network(i).to; - mat.cols(i) := col; - for j in 1..row-row_last loop - mat.rows(nb) := i; - nb := nb + 1; - end loop; - row_last := row; - end loop; - - end create_H; - - procedure put(mat: in T_Google) is - begin - for i in 0..N_links-1 loop - put(mat.cols(i)); - end loop; - new_line; new_line; - for i in 0..N loop - put(mat.rows(i)); - end loop; - end put; - - function calcul(vec: in Vector_Element.T_Vecteur; mat: in T_Google; alpha: in T_Element) return Vector_Element.T_Vecteur is - new_vec: Vector_Element.T_Vecteur; - nb_col: Natural := 0; - col_CRS: Natural := 0; - col_vec: Natural := 0; - begin - Vector_Element.initialize(new_vec, Vector_Element.sum(vec)*(1.0-alpha)/T_Element(N)); - for i in 0..N-1 loop - nb_col := mat.rows(i+1) - mat.rows(i); - if nb_col = 0 then - for j in 0..N-1 loop - new_vec(j) := new_vec(j) + alpha*vec(i)/T_Element(N); - end loop; - else - for j in 1..nb_col loop - col_vec := mat.cols(col_CRS); - new_vec(col_vec) := new_vec(col_vec) + alpha*vec(i)/T_Element(nb_col); - col_CRS := col_CRS + 1; - end loop; - end if; - end loop; - return new_vec; - end calcul; - -end Google_Creux; diff --git a/src/google_creux.ads b/src/google_creux.ads deleted file mode 100644 index ccf07c1..0000000 --- a/src/google_creux.ads +++ /dev/null @@ -1,37 +0,0 @@ -with Vector; - -generic - - type T_Element is digits <>; - N: Positive; - N_Links: Positive; - - with package Vector_Natural is - new Vector.Entier(Capacite => N); - - with package Vector_Element is - new Vector.Digit(T_Digit => T_Element, Capacite => N, Vector_Entier => Vector_Natural); - - with package Vector_Link is - new Vector.Link(Capacite => N_Links); - -package Google_Creux is - - type T_Rows is array (0..N) of Natural; - type T_Cols is array (0..N_links-1) of Natural; - type T_Google is record - cols: T_Cols; - rows: T_Rows; - end record; - - -- permet de créer la matrice à partir du réseau - procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur); - - -- permet faire le produit vecteur-matrice - function calcul(vec: in Vector_Element.T_Vecteur; mat: in T_Google; alpha: in T_Element) return Vector_Element.T_Vecteur with - Pre => alpha > 0.0 and alpha < 1.0; - - -- permet l'affichage de la matrice, voir l'article wikiepdia pour comprendre format - procedure put(mat: in T_Google); - -end Google_Creux; diff --git a/src/google_naive.adb b/src/google_naive.adb deleted file mode 100644 index ca19dcb..0000000 --- a/src/google_naive.adb +++ /dev/null @@ -1,79 +0,0 @@ -package body Google_Naive is - - function "*"(left : Vector_Element.T_Vecteur; right : T_Google) return Vector_Element.T_Vecteur is - vec: Vector_Element.T_Vecteur; - c: T_Element; - begin - for i in 0..N-1 loop - c := 0.0; - for j in 0..N-1 loop - c := c + left(j)*right(j,i); - end loop; - vec(i) := c; - end loop; - return vec; - end "*"; - - procedure initialize(mat: in out T_Google) is - begin - for i in 0..N-1 loop - for j in 0..N-1 loop - mat(i,j) := 0.0; - end loop; - end loop; - end initialize; - - procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur) is - row, col: Integer; - begin - for i in 0..N_Links-1 loop - row := network(i).from; - col := network(i).to; - mat(row, col) := 1.0; - end loop; - end create_H; - - procedure create_S(mat: in out T_Google) is - sum: T_Element; - begin - for i in 0..N-1 loop - sum := 0.0; - for j in 0..N-1 loop - sum := sum + mat(i,j); - end loop; - - if sum /= 0.0 then - for j in 0..N-1 loop - if mat(i,j) /= 0.0 then - mat(i,j) := 1.0/sum; - end if; - end loop; - else -- sum == 0 then - for j in 0..N-1 loop - mat(i,j) := 1.0/T_Element(N); - end loop; - end if; - end loop; - end create_S; - - procedure create_G(mat: in out T_Google; alpha: in T_Element) is - begin - for i in 0..N-1 loop - for j in 0..N-1 loop - mat(i,j) := alpha*mat(i,j) + (1.0-alpha)/T_Element(N); - end loop; - end loop; - end create_G; - - procedure put(mat: in T_Google) is - use Text_T_Element; - begin - for i in 0..N-1 loop - for j in 0..N-1 loop - put(mat(i,j)); - end loop; - new_line; - end loop; - end put; - -end Google_Naive; diff --git a/src/google_naive.ads b/src/google_naive.ads deleted file mode 100644 index 8d802b0..0000000 --- a/src/google_naive.ads +++ /dev/null @@ -1,47 +0,0 @@ -with Ada.Text_IO; use Ada.Text_IO; -with Vector; - -generic - - type T_Element is digits <>; - N: Positive; - N_Links: Positive; - - -- on a besoin de ce type Vector_Element - with package Vector_Natural is - new Vector.Entier(Capacite => N); - - -- on a besoin de ce type pour le produit vecteur-matrice - with package Vector_Element is - new Vector.Digit(T_Digit => T_Element, Capacite => N, Vector_Entier => Vector_Natural); - - -- on a besoin de ce type pour la création de H - with package Vector_Link is - new Vector.Link(Capacite => N_Links); - -package Google_Naive is - - -- on permet l'affichage direct des T_Element - package Text_T_Element is - new Ada.Text_IO.Float_IO(Num => T_Element); - - type T_Google is array (0..N-1, 0..N-1) of T_Element; - - function "*"(left: Vector_Element.T_Vecteur; right: T_Google) return Vector_Element.T_Vecteur; - - procedure initialize(mat: in out T_Google); - - -- On place des 1 dans la matrice à partir du réseau. - procedure create_H(mat: in out T_Google; network: in Vector_Link.T_Vecteur); - - -- On transforme H pour obtenir S - procedure create_S(mat: in out T_Google); - - -- On transforme S pour obtenir G - procedure create_G(mat: in out T_Google; alpha: in T_Element) with - Pre => alpha > 0.0 and alpha < 1.0; - - -- permet l'affiche de la matrice - procedure put(mat: in T_Google); - -end Google_Naive; diff --git a/src/quicksort.adb b/src/quicksort.adb new file mode 100644 index 0000000..5848cde --- /dev/null +++ b/src/quicksort.adb @@ -0,0 +1,95 @@ +package body Quicksort is + + procedure sort(vec: in out Vector_Element.T_Vecteur; low: Natural := 0; high: Natural := Vector_Element.Capacite-1) is + + procedure swap(left, right: Natural) is + tmp : constant T_Reel := vec(left); + begin + vec(left) := vec(right); + vec(right) := tmp; + end swap; + + pivot : constant T_Reel := vec(low); + right : Natural := high; + left : Natural := low; + + begin + if high - low > 0 then + loop + while left < right and pivot >= vec(left) loop + left := left + 1; + end loop; + while pivot < vec(right) loop + right := right - 1; + end loop; + + exit when right <= left; + + swap(left, right); + left := left + 1; + right := right - 1; + end loop; + + if right = high then + right := right - 1; + swap(low, high); + end if; + + if left = low then + left := left - 1; + end if; + + sort(vec, low, right); + sort(vec, left, high); + end if; + end sort; + + procedure sort(vec: in out Vector_Element.T_Vecteur; vec_index: in out Vector_Index.T_Vecteur; + low: Natural := 0; high: Natural := Vector_Element.Capacite-1) is + + procedure swap(left, right: Natural) is + tmp : constant T_Reel := vec(left); + tmp_index : constant T_Entier := vec_index(left); + begin + vec(left) := vec(right); + vec(right) := tmp; + vec_index(left) := vec_index(right); + vec_index(right) := tmp_index; + end swap; + + pivot : constant T_Reel := vec(low); + right : Natural := high; + left : Natural := low; + + begin + if high - low > 0 then + loop + while left < right and pivot >= vec(left) loop + left := left + 1; + end loop; + while pivot < vec(right) loop + right := right - 1; + end loop; + + exit when right <= left; + + swap(left, right); + left := left + 1; + right := right - 1; + end loop; + + if right = high then + right := right - 1; + swap(low, high); + end if; + + if left = low then + left := left - 1; + end if; + + sort(vec, vec_index, low, right); + sort(vec, vec_index, left, high); + end if; + end sort; + +end Quicksort; \ No newline at end of file diff --git a/src/quicksort.ads b/src/quicksort.ads new file mode 100644 index 0000000..4a9bf56 --- /dev/null +++ b/src/quicksort.ads @@ -0,0 +1,22 @@ +with Vector; + +generic + + -- Vecteur contenant les élements du même type que la matrice + with package Vector_Element is new Vector.Reel(<>); + + -- Vecteur contenant des élements de type entier + with package Vector_Index is new Vector.Entier(<>); + +package Quicksort is + + use Vector_Element; + use Vector_Index; + + procedure sort(vec: in out Vector_Element.T_Vecteur; + low: Natural := 0; high: Natural := Vector_Element.Capacite-1); + + procedure sort(vec: in out Vector_Element.T_Vecteur; vec_index: in out Vector_Index.T_Vecteur; + low: Natural := 0; high: Natural := Vector_Element.Capacite-1); + +end Quicksort; \ No newline at end of file diff --git a/src/test_tri.adb b/src/test_tri.adb deleted file mode 100644 index d0d9aed..0000000 --- a/src/test_tri.adb +++ /dev/null @@ -1,147 +0,0 @@ -with Ada.Text_IO; use Ada.Text_IO; --- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; - -with Vector; - -procedure test_tri is - - --Type T_Double is digits 6; - N: constant Natural := 10; - - package Vector_Entier is - new Vector.Entier(Capacite => N); - use Vector_Entier; - - package Vector_Double is - new Vector.Digit(T_Digit => Float, - Capacite => N, - Vector_Entier => Vector_Entier); - use Vector_Double; - - package Vector_Link is - new Vector.Link(Capacite => N); - use Vector_Link; - - vec: Vector_Double.T_Vecteur; - vec_natural: Vector_Entier.T_Vecteur; - vec_index: Vector_Entier.T_Vecteur; - vec_link: Vector_Link.T_Vecteur; - -begin - - vec(0) := 10.0; - vec(1) := 1.0; - vec(2) := 21.0; - vec(3) := 3.0; - vec(4) := 13.0; - vec(5) := 56.0; - vec(6) := 3.0; - vec(7) := 5.0; - vec(8) := 6.0; - vec(9) := 6.0; - - new_line; - - put_line("avant:"); - put(vec); new_line; - - quicksort(vec); - - put_line("après:"); - put(vec); new_line; - - - - - - vec_natural(0) := 10; - vec_natural(1) := 1; - vec_natural(2) := 21; - vec_natural(3) := 3; - vec_natural(4) := 13; - vec_natural(5) := 2855059; - vec_natural(6) := 3; - vec_natural(7) := 5; - vec_natural(8) := 6; - vec_natural(9) := 6; - - new_line; - - put_line("avant:"); - put(vec_natural); new_line; - - quicksort(vec_natural); - - put_line("après:"); - put(vec_natural); new_line; - - - - - - vec(0) := 10.0; - vec(1) := 1.0; - vec(2) := 21.0; - vec(3) := 3.0; - vec(4) := 13.0; - vec(5) := 56.0; - vec(6) := 3.0; - vec(7) := 5.0; - vec(8) := 6.0; - vec(9) := 6.0; - identity(vec_index); - - new_line; - - put_line("double avant:"); - put(vec); new_line; - - put_line("index avant:"); - put(vec_index); new_line; - - quicksort(vec, vec_index); - - put_line("double après:"); - put(vec); new_line; - - put_line("index après:"); - put(vec_index); new_line; - - - vec_link(0) := T_Link'(10, 15); - vec_link(1) := T_Link'(1, 155); - vec_link(2) := T_Link'(0, 153); - vec_link(3) := T_Link'(32, 155); - vec_link(4) := T_Link'(0, 153); - vec_link(5) := T_Link'(15, 158); - vec_link(6) := T_Link'(0, 135); - vec_link(7) := T_Link'(0, 8); - vec_link(8) := T_Link'(40, 7); - vec_link(9) := T_Link'(0, 112); - - put_line("links avant:"); - put(vec_link); new_line; - - quicksort(vec_link); - - put_line("links après:"); - put(vec_link); new_line; - - declare - package Vector_Entier is - new Vector.Entier(Capacite => N); - package Vector_Double is - new Vector.Digit(T_Digit => Float, - Capacite => N, - Vector_Entier => Vector_Entier); - package Vector_Link is - new Vector.Link(Capacite => N); - - use Vector_Double; - use Vector_Entier; - use Vector_Link; - begin - null; - end; - -end test_tri;