examples/test_vec/test_vec.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_vec.c Test program for vectors types           */
00022 /*------------------------------------------------------------*/
00023 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <stdarg.h>
00027 #include <math.h>
00028 #include <assert.h>
00029 #include <it/vec.h>
00030 #include <it/io.h>
00031 
00032 int main ()
00033 {
00034   vec  v0, v1, v2, v3, v4, v5, v6, v7, vs;
00035   ivec v8, uv8, v9, uv8v9, iv8v9, v10, v11;
00036   int  j;
00037 
00038   /* Alloc a vector and set all elements to 9                                */
00039   v0 = vec_new (10);
00040   vec_set (v0, 9);
00041 
00042   /* Alloc a vector with elements initialized to zero                        */
00043   v1 = vec_new_zeros (10);
00044   v1[5] = 5;
00045   _vec (v1, 5) = 5;   /* same thing with type and bound checking */
00046 
00047   /* Constructions with various functions (allocation processed in the function) */
00048   v2 = vec_new_ones (5);
00049   v3 = vec_new_arithm (1, -2, 15);
00050   v4 = vec_new_geom (1, 2, 8);
00051 
00052   /* Generic function test     */
00053   v5 = vec_new (8);
00054   vec_1N (v5);
00055   //  vec_eval(v5, IT_FUNCTION(tan), NULL );
00056 
00057   v6 = vec_new (15);
00058   v7 = v5;
00059 
00060   /* Process various operations on vectors    */
00061   it_printf
00062     ("Length(v1) = %d\nv0=$v\nv1=$.3v\nv2=$.3v\nv3=$.3v\nv4=$.3v\nv5=$.3v\nMax( v5 ) = %g\nMin( v5 ) = %g\nMean( v5 ) = %g\nSum( v5 ) = %g\nSumSqr( v5 ) = %g\nNorm(v,2) = %g\n",
00063      Vec_length (v1), v0, v1, v2, v3, v4, v5, vec_max (v7),
00064      vec_min (v7), vec_mean (v7), vec_sum (v7), vec_sum_sqr (v7),
00065      vec_norm (v7, 2));
00066 
00067   /* Sort a vector and retrieve the permutation corresponding to this sorting */
00068   v8 = vec_sort_index (v7);
00069   vec_sort (v7);
00070   it_printf ("v7=$.2f (max_length = %d)\nv8=$d\n",
00071        v7, vec_length_max (v7), v8);
00072 
00073   /* Set operations             */
00074   vec_del (v7, 0);
00075   ivec_del (v8, 5);
00076   vec_pop (v7);
00077   ivec_pop (v8);
00078 
00079   it_printf ("v7=$.3v (max_length = %d)\nv8=$i\n\n",
00080        v7, vec_length_max (v7), v8);
00081 
00082   vs = vec_cum_sum (v7);
00083   it_printf ("cum_sum(v7)=$.3v\n\n", vs);
00084 
00085   /* Now test vectors as stack  */
00086 
00087   it_printf ("v7=$.3v (max_length = %d)\nv8=$d\n\n",
00088        v7, vec_length_max (v7), v8);
00089 
00090   printf ("Modification of the vector max length\n");
00091   fflush (stdout);
00092   Vec_set_length_max (v7, 9);
00093   Vec_set_length_max (v8, 7);
00094 
00095   it_printf ("v7=$.3v (max_length = %d)\nv8=$d (max_length = %d)\n\n",
00096        v7, vec_length_max (v7), v8, ivec_length_max (v8));
00097 
00098   for (j = 1; j < 8; j++) {
00099     vec_push (v7, j * 1.2);
00100     ivec_push (v8, j);
00101 
00102     it_printf ("v7=$g (max_length = %d)\nv8=$d (max_length = %d)\n\n",
00103          v7, vec_length_max (v7), v8, ivec_length_max (v8));
00104   }
00105 
00106   v9 = ivec_new_string ("4 1 9 4");
00107   uv8 = ivec_new_unique (v8);
00108   it_printf ("Unique( v8 ) = $d\n", uv8);
00109 
00110   ivec_sort (v8);
00111   it_printf ("Sort( v8 ) = $d\n\n", v8);
00112 
00113   it_printf ("v8 = $d\n", v8);
00114   it_printf ("v9 = $d\n", v9);
00115 
00116   uv8v9 = ivec_new_union (v8, v9);
00117   iv8v9 = ivec_new_intersection (v8, v9);
00118   it_printf ("Union(v8,v9) = $d\n", uv8v9);
00119   it_printf ("Inter(v8,v9) = $d\n", iv8v9);
00120 
00121   printf ("Median( Union(v8,v9) ) = %d\n\n", ivec_median (uv8v9));
00122 
00123   /* Compute the maximum and their index */
00124   it_printf ("v8=$d\n", v8);
00125   v10 = ivec_k_max (v8, 5);
00126   v11 = ivec_k_max_index_between (v8, 5, 0, ivec_length (v8) - 1);
00127   it_printf ("v10=$d\nv11=$d\n\n", v10, v11);
00128 
00129   /* Free the memory associated with the vectors */
00130   vec_delete (v0);
00131   vec_delete (v1);
00132   vec_delete (v2);
00133   vec_delete (v3);
00134   vec_delete (v4);
00135   vec_delete (v6);
00136   vec_delete (v7);
00137   vec_delete (vs);
00138   ivec_delete (v8);
00139   ivec_delete (uv8);
00140   ivec_delete (v9);
00141   ivec_delete (uv8v9);
00142   ivec_delete (iv8v9);
00143   ivec_delete (v10);
00144   ivec_delete (v11);
00145 
00146   return 0;
00147 }

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