examples/test_distance/test_distance.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 /*------------------------------------------------------------*/
00021 /** @file test_distance.c Test program for distances          */
00022 /*------------------------------------------------------------*/
00023 
00024 #include <it/vec.h>
00025 #include <it/io.h>
00026 #include <it/distance.h>
00027 
00028 int main ()
00029 {
00030   int  i, j;
00031 
00032   int  dh[5][5];
00033   int  dl[3][3];
00034 
00035   /* Initialisation of some vector */
00036   vec  v1 = vec_new_arithm (1.1, 1.1, 7);
00037   vec  v2 = vec_new_arithm (0, 1.1, 8);
00038   vec  v3 = vec_clone (v2);
00039   vec  v4 = vec_clone (v1);
00040   vec  v5 = vec_clone (v2);
00041 
00042   ivec iv1 = ivec_new_geom (1, 2, 8);
00043   ivec iv2 = ivec_new_ones (4);
00044   ivec iv3 = ivec_new (5);
00045 
00046   v3[2] = 3;
00047   v3[5] = 7;
00048   v4[2] = 2.3;
00049   v4[6] = -3;
00050   v5[0] = 1.1;
00051   v5[2] = 2.3;
00052   v5[6] = -3;
00053 
00054   // it_set_vec_default_fmt( "%8.3" );
00055 
00056   it_printf ("v1 = $v\nv2 = $v\nv3 = $v\nv4 = $v\nv5 = $v\n\n", v1, v2, v3,
00057        v4, v5);
00058 
00059   ivec_set (iv3, 7);
00060 
00061   it_printf ("iv1=$d\niv2=$d\niv3=$d\n\n", iv1, iv2, iv3);
00062 
00063   /* Process distance between vectors */
00064   dh[0][0] = vec_distance_hamming (v1, v1);
00065   dh[0][1] = vec_distance_hamming (v1, v2);
00066   dh[0][2] = vec_distance_hamming (v1, v3);
00067   dh[0][3] = vec_distance_hamming (v1, v4);
00068   dh[0][4] = vec_distance_hamming (v1, v5);
00069 
00070   dh[1][0] = vec_distance_hamming (v2, v1);
00071   dh[1][1] = vec_distance_hamming (v2, v2);
00072   dh[1][2] = vec_distance_hamming (v2, v3);
00073   dh[1][3] = vec_distance_hamming (v2, v4);
00074   dh[1][4] = vec_distance_hamming (v2, v5);
00075 
00076   dh[2][0] = vec_distance_hamming (v3, v1);
00077   dh[2][1] = vec_distance_hamming (v3, v2);
00078   dh[2][2] = vec_distance_hamming (v3, v3);
00079   dh[2][3] = vec_distance_hamming (v3, v4);
00080   dh[2][4] = vec_distance_hamming (v3, v5);
00081 
00082   dh[3][0] = vec_distance_hamming (v4, v1);
00083   dh[3][1] = vec_distance_hamming (v4, v2);
00084   dh[3][2] = vec_distance_hamming (v4, v3);
00085   dh[3][3] = vec_distance_hamming (v4, v4);
00086   dh[3][4] = vec_distance_hamming (v4, v5);
00087 
00088   dh[4][0] = vec_distance_hamming (v5, v1);
00089   dh[4][1] = vec_distance_hamming (v5, v2);
00090   dh[4][2] = vec_distance_hamming (v5, v3);
00091   dh[4][3] = vec_distance_hamming (v5, v4);
00092   dh[4][4] = vec_distance_hamming (v5, v5);
00093 
00094   dl[0][0] = ivec_distance_levenshtein (iv1, iv1, 1, 1, 1);
00095   dl[0][1] = ivec_distance_levenshtein (iv1, iv2, 1, 1, 1);
00096   dl[0][2] = ivec_distance_levenshtein (iv1, iv3, 1, 1, 1);
00097 
00098   dl[1][0] = ivec_distance_levenshtein (iv2, iv1, 1, 1, 1);
00099   dl[1][1] = ivec_distance_levenshtein (iv2, iv2, 1, 1, 1);
00100   dl[1][2] = ivec_distance_levenshtein (iv2, iv3, 1, 1, 1);
00101 
00102   dl[2][0] = ivec_distance_levenshtein (iv3, iv1, 1, 1, 1);
00103   dl[2][1] = ivec_distance_levenshtein (iv3, iv2, 1, 1, 1);
00104   dl[2][2] = ivec_distance_levenshtein (iv3, iv3, 1, 1, 1);
00105 
00106 
00107   for (i = 0; i < 5; i++) {
00108     for (j = 0; j < 5; j++) {
00109       printf ("%5d\t", dh[i][j]);
00110     }
00111     printf ("\n");
00112   }
00113 
00114   for (i = 0; i < 3; i++) {
00115     for (j = 0; j < 3; j++) {
00116       printf ("%5d\t", dl[i][j]);
00117     }
00118     printf ("\n");
00119   }
00120 
00121 
00122   it_printf ("vec_distance_mse(v2,v3) = %f\n", vec_distance_mse (v2, v3, 0));
00123 
00124 
00125   vec_delete (v1);
00126   vec_delete (v2);
00127   vec_delete (v3);
00128   vec_delete (v4);
00129   vec_delete (v5);
00130 
00131   ivec_delete (iv1);
00132   ivec_delete (iv2);
00133   ivec_delete (iv3);
00134 
00135   return 0;
00136 }

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