examples/test_mat/test_mat.c

Go to the documentation of this file.
00001 /*
00002    libit - Library for basic source and channel coding functions
00003    Copyright (C) 2005-2005 Vivien Chappelier, Herve Jegou
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /** @file test_mat.c Test program for Matrices */
00021 
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <stdarg.h>
00025 #include <math.h>
00026 #include <assert.h>
00027 #include <it/io.h>
00028 #include <it/mat.h>
00029 #include <it/linalg.h>
00030 
00031 int main ()
00032 {
00033   int  i, j;
00034   const int K = 100;
00035   mat  m1 = mat_new (2, 5);
00036   mat  m2 = mat_new_alloc (5, 3, 6, 6);
00037   mat  m3;
00038   mat  m4 = mat_new (3, 3);
00039   mat  m5, m6, m7, m8;
00040   m4[0][0] = 0.3;
00041   m4[0][1] = 0.2;
00042   m4[0][2] = 0.5;
00043   m4[1][0] = 0.1;
00044   m4[1][1] = 0.1;
00045   m4[1][2] = 0.8;
00046   m4[2][0] = 0.7;
00047   m4[2][1] = 0.2;
00048   m4[2][2] = 0.1;
00049 
00050   printf
00051     ("height(m1) = %d\tmaxheight(m1) = %d\nwidth(m1) = %d\tmaxwidth(m1) = %d\n",
00052      mat_height (m1), mat_height_max (m1), mat_width (m1),
00053      mat_width_max (m1));
00054 
00055   printf
00056     ("height(m1) = %d\tmaxheight(m1) = %d\nwidth(m1) = %d\tmaxwidth(m1) = %d\n",
00057      mat_height (m2), mat_height_max (m2), mat_width (m2),
00058      mat_width_max (m2));
00059 
00060   mat_set (m1, 5);
00061   mat_set (m2, 4);
00062 
00063   mat_mul_by (m1, 3);
00064   mat_decr (m1, 1);
00065 
00066   for (i = 0; i < mat_height (m1); i++) {
00067     for (j = 0; j < mat_width (m1); j++)
00068       printf ("%.4f\t", m1[i][j]);
00069     printf ("\n");
00070   }
00071   printf ("\n");
00072 
00073 
00074   for (i = 0; i < mat_height_max (m2); i++) {
00075     for (j = 0; j < mat_width_max (m2); j++)
00076       printf ("%.4f\t", m2[i][j]);
00077     printf ("\n");
00078   }
00079 
00080   m1[1][1] = 7;
00081   m2[4][2] = -3;
00082 
00083   /* Inverse and display m4 */
00084   m3 = mat_new_inv (m4);
00085   it_printf ("\nm4      = \n#.4f\n", m4);
00086   it_printf ("\ninv(m4) = \n#.6f\n", m3);
00087 
00088   m5 = mat_new_mul (m4, m3);
00089 
00090   /* display Matrices */
00091   it_printf ("\nm1 = #.4f\n\nm2 = #.4f\n\nm4 * m3 = \n#m\n", m1, m2, m5);
00092 
00093   it_printf ("\nm4 = \n#.6f\n", m4);
00094 
00095   m6 = mat_new_transpose (m4);
00096   it_printf ("\ntranspose( m4 ) = \n#.6f\n", m6);
00097 
00098   /* Gram-schmidt orthogonalisation */
00099   mat_gs (m4);
00100   it_printf ("\ngram_schmidt    : \n#.6f\n", m4);
00101 
00102   /* Normalization of columns */
00103   mat_cols_normalize (m4, 2);
00104   it_printf ("\nnormalize       : \n#.6f\n", m4);
00105 
00106   /* Max */
00107   it_printf ("\nmax             : %f\n", mat_max (m4));
00108 
00109   /* Test inversion of a "large" matrix */
00110   m7 = mat_new (K, K);
00111   for (i = 0; i < K; i++)
00112     for (j = 0; j < K; j++)
00113       m7[i][j] = pow (0.5, fabs (j - i));
00114 
00115   m8 = mat_new_inv (m7);
00116 
00117   mat_delete (m1);
00118   mat_delete (m2);
00119   mat_delete (m3);
00120   mat_delete (m4);
00121   mat_delete (m5);
00122   mat_delete (m6);
00123   mat_delete (m7);
00124   mat_delete (m8);
00125   return 0;
00126 }

Hosted by
Copyright (C) 2005-2006 Hervé Jégou
Vivien Chappelier
Francois Cayre
libit logo courtesy of Jonathan Delhumeau