Index: branch/gresset/sp4a3/sp4a3_kalman.c
===================================================================
--- branch/gresset/sp4a3/sp4a3_kalman.c	(revision 285)
+++ branch/gresset/sp4a3/sp4a3_kalman.c	(revision 286)
@@ -7,12 +7,21 @@
 #include "sp4a3_kalman_extra.h"
 
 
-void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
-
+void Add_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double out[na][ma]){
+    int i=0, j=0;
+    for(i=0; i<na; i++){
+        for(j=0; j<ma; j++){
+            out[i][j]=A[i][j]+B[i][j];
+        }
+    }
 }
 
 void Inverse_Mat_22(int n,int m,double A[n][m],double B[n][m]){
-
+    double det=A[0][0]*A[1][1]-A[0][1]*A[1][0];
+    B[0][0]=(A[1][1])/det;
+    B[0][1]=(-A[0][1])/det;
+    B[1][0]=(-A[1][0])/det;
+    B[1][1]=(A[0][0])/det;
 }
 
 void Transpose_Mat(int n,int m,double A[n][m],double R[m][n]){
@@ -22,12 +31,36 @@
 			R[j][i]=A[i][j];
 }
 
-void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double b[nb][mb], double out[na][ma]){
-
+void Sub_Mat_Mat(int na,int ma,double A[na][ma],int nb,int mb,double B[nb][mb], double out[na][ma]){
+    int i=0, j=0;
+    for(i=0; i<na; i++){
+        for(j=0; j<ma; j++){
+            out[i][j]=A[i][j]-B[i][j];
+        }
+    }
 }
 
-void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double out[na][mb]){
-
+void Mul_Mat_Mat(int na,int ma,double A[na][ma], int nb,int mb,double B[nb][mb], double out[na][mb]){
+    int i=0, j=0, k=0;
+    double sum;
+    for (i=0; i<na; i++){
+        for (j=0; j<mb; j++){
+        out[i][j]=0;
+        sum=0;
+            for(k=0; k<ma; k++){
+                sum= sum+A[i][k]*B[k][j];
+            }
+        out[i][j]=sum;
+        }
+    }
+}
+
+void Init_Mat(int na,int ma,double A[na][ma], double out[na][ma]){
+    int i=0, j=0;
+    for (i=0; i<na; i++)
+        for (j=0; j<ma; j++){
+            out[i][j]=A[i][j];
+        }
 }
 
 
@@ -65,8 +98,9 @@
 	double RMT44T42[4][2]={{-308,-268},{-611,-99},{-816,118},{-432,122}};
 	double RAT44[4][4]={{13,15,21,19},{27,27,30,48},{87,53,19,9},{59,14,15,49}};
 	double RST44[4][4]={{-11,-11,-7,-11},{-15,-17,-16,-32},{-69,-37,-5,3},{-49,-6,-9,-45}};
-	double RMT44T44[4][4]={{816,412,192,304},{1155,583,379,687},{1146,668,466,758},{486,308,222,338}};
+	double RMT44T44[4][4]={{816,412,192,304},{1155,583,379,687},{1146,668,466,758},{486,308,222,338}};
 
+
 	printf("Execution des tests unitaires.\n");
 	Transpose_Mat(2,4,T24,R42);		if (!Equal_Mat_Mat(RTT24,R42)) 		error("Erreur calcul Transposition 2x4");
 	Transpose_Mat(4,4,T44a,R44);	if (!Equal_Mat_Mat(RTT44,R44)) 		error("Erreur calcul Transposition 4x4");
@@ -84,7 +118,9 @@
 	Mul_Mat_Mat(4,2,T42a,2,4,T24,R44);	if (!Equal_Mat_Mat(RMT42T24,R44)) 	error("Erreur calcul Multiplication 4x2 2x4");
 	Mul_Mat_Mat(2,4,T24,4,1,T41a,R21);	if (!Equal_Mat_Mat(RMT24T41,R21)) 	error("Erreur calcul Multiplication 2x4 4x1");
 	Mul_Mat_Mat(2,4,T24,4,2,T42a,R22);	if (!Equal_Mat_Mat(RMT24T42,R22)) 	error("Erreur calcul Multiplication 2x4 4x2");
-	Mul_Mat_Mat(2,4,T24,4,4,T44a,R24);	if (!Equal_Mat_Mat(RMT24T44,R24)) 	error("Erreur calcul Multiplication 2x4 4x4");
+	Mul_Mat_Mat(2,4,T24,4,4,T44a,R24);	if (!Equal_Mat_Mat(RMT24T44,R24)) 	error("Erreur calcul Multiplication 2x4 4x4");
+	Init_Mat(2, 4, T24, R24);   if (!Equal_Mat_Mat(T24, R24))   error("Erreur initialisation 2x4");
+	Init_Mat(4, 4, T44a, R44);   if (!Equal_Mat_Mat(T44a, R44))   error("Erreur initialisation 4x4");
 	printf("Test unitaires OK.\n");
 }
 
@@ -98,7 +134,7 @@
 
     FILE * Fout = Fout = fopen("output.dat","w");
 	if (fichier == NULL)
-        error("Impossible d'ouvrir le fichier output.dat");
+        error("Impossible d'ouvrir le fichier output.dat");
 
 	printf("Kalman\n");
 	double t = 0;
@@ -151,7 +187,8 @@
 			Plot_Mat(R,"R = ");
 		}
 		else
-		{
+		{
+		    //getchar();
 			t -= t0;x -= x0;y -= y0;
 
 			debug=0; ///Mettre à 1 pour afficher les matrices.
@@ -158,23 +195,58 @@
 			///Ajouter votre code ci-dessous///
 			// Kalman
 
-			// X = F*X
+			// X = F*X
+			double matx[4][1];
+			Init_Mat(4, 1, X, matx);
+			Mul_Mat_Mat(4, 4, F, 4, 1, matx, X);
 			Plot_Mat(X," X(k+1|k) = ");
 
-			//P = F*P*F'+Q;
+			//P = F*P*F'+Q;
+			double out[4][4], out1[4][4];
+			Mul_Mat_Mat(4, 4, F, 4, 4, P, out);
+			Mul_Mat_Mat(4, 4, out, 4, 4, FT, out1);
+			Add_Mat_Mat(4, 4, out1, 4, 4, Q, P);
 			Plot_Mat(P,"P(k+1|k) = F.P(k|k).FT + Q = ");
 
-			// K = P*H' / ( H*P*H' + R);
-			Plot_Mat(K,"K = ");
+			// K = P*H' / ( H*P*H' + R);
+
+            double mat[4][2];
+            Mul_Mat_Mat(4, 4,  P, 4, 2, HT, mat);
+            Plot_Mat(mat,"P(k+1|k).HT = ");
+
+            double mat1[2][2], mat2[2][2];
+            Mul_Mat_Mat(2, 4, H, 4, 2, mat, mat1);
+            Add_Mat_Mat(2, 2, mat1, 2, 2, R, mat2);
+            Plot_Mat(mat2, "H.P(k+1|k).HT + R = ");
+
+            double mat3[2][2];
+            Inverse_Mat_22(2, 2, mat2, mat3);
+            Plot_Mat(mat3, "INV(H.P(k+1|k).HT + R) = ");
+
+            Mul_Mat_Mat(4, 2, mat, 2, 2, mat3, K);
+            Plot_Mat(K, "K = ");
 
-			//X = X + K*([xb(i);yb(i)]-H*X);
-			//Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+			//X = X + K*([xb(i);yb(i)]-H*X);
+			double Obs[2][1]={{x}, {y}}, Delta[2][1], mat4[2][1];
+			Mul_Mat_Mat(2, 4, H, 4, 1, X, mat4);
+			Sub_Mat_Mat(2, 1, Obs, 2, 1, mat4, Delta);
+			Plot_Mat(Delta,"DELTA = Obs - H.X(k+1|k)");
+
+            double mat5[4][1];
+            Mul_Mat_Mat(4, 2, K, 2, 1, Delta, mat5);
+            Add_Mat_Mat(4, 1, X, 4, 1, mat5, X);
             Plot_Mat(X," X(k+1|k+1) = X(k+1|k) + K.Delta = ");
 
-			// P = P - K*H*P;
+			// P = P - K*H*P;
+			double mat6[4][4], mat7[4][4], matp[4][4];
+			Mul_Mat_Mat(4, 2, K, 2, 4, H, mat6);
+			Mul_Mat_Mat(4, 4, mat6, 4, 4, P, mat7);
+			Init_Mat(4, 4, P, matp);
+			Sub_Mat_Mat(4, 4, matp, 4, 4, mat7, P);
             Plot_Mat(P," P(k+1|k+1) = P(k+1|k) - K.H.P(k+1|k) = ");
 
-			/// La matrice X doit contenir la position filtrée ///
+			/// La matrice X doit contenir la position filtrée ///
+
 		}
 		t = cpt * dt;
 		dx = (x - oldx)/dt;
