examples/test_linalg/test_linalg.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_poly.c Test program for linear algebra         */
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/mat.h>
00030 #include <it/vec.h>
00031 #include <it/io.h>
00032 #include <it/linalg.h>
00033 
00034 int main ()
00035 {
00036   vec  v0, v1, v2; 
00037   int  j;
00038 
00039   cvec eval; 
00040   cmat evec; 
00041   mat m1, m2, m3; 
00042 
00043   m1 = mat_new( 3, 3 ); 
00044 
00045   m1[0][0] = 1;   m1[0][1] = 5;   m1[0][2] = 2; 
00046   m1[1][0] = 2;   m1[1][1] = 3;   m1[1][2] = 5; 
00047   m1[2][0] = 3;   m1[2][1] = 7;   m1[2][2] = 3; 
00048 
00049   fprintf( stderr, "\n\nACKNOWLEDGEMENT: Various linear\nalgebra routines (EIG, SVD) are\nborrowed from the JAMA Java\npackage in the public domain.\nSee linalg.h for details.\n\nThese routines were written by:\nJoe Hicklin        (MathWorks)\nCleve Moler        (MathWorks)\nPeter Webb         (MathWorks)\nRonald F. Boisvert (NIST)\nBruce Miller       (NIST)\nRoldan Pozo        (NIST)\nKarin Remington    (NIST)\n\n" );
00050 
00051   it_printf( "m1=\n#m\n", m1 ); 
00052 
00053   m2 = mat_new_inv( m1 ); 
00054 
00055   printf( "\n\nTesting matrix inversion (LU + AX=B)\n\n" );
00056 
00057   it_printf( "m2=inv(m1)=\n#m\n", m2 ); 
00058 
00059   m3 = mat_new_mul( m2, m1 ); 
00060 
00061   it_printf( "Computing m1*m2=\n#m\n", m3 );
00062 
00063   printf( "\n\nTesting eigenvalue decomposition\n\n" );
00064 
00065   evec = cmat_new( 3, 3 ); 
00066 
00067   eval = mat_eig( m1, evec );
00068 
00069   it_printf( "Eigenvalues of m1= $z\n", eval );
00070 
00071   it_printf( "Eigenvectors of m1=\n#z\n", evec );
00072 
00073 
00074   printf( "\n\nTesting singular value decomposition\n\n" );
00075 
00076   v0 = mat_svd( m1, m2, m3 );
00077 
00078   it_printf( "m1=\n#m\n", m1 );
00079 
00080   it_printf( "Singular values of m1= $v\n", v0 ); 
00081 
00082   it_printf( "Corresponding U matrix:\n#m\n", m2 );
00083   it_printf( "Corresponding V matrix:\n#m\n", m3 );
00084 
00085   mat_copy( m2, m1 ); 
00086 
00087 
00088   printf( "\n\nTesting Gram-Schmidt orthogonalisation\n\n" );
00089 
00090   mat_gs( m2 ); 
00091 
00092   it_printf( "m2 is Gram-Schmidt(m1):\n#m\n", m2 ); 
00093 
00094   for ( j= 0; j< mat_width(m2); j++ ) 
00095     {
00096       v1 = mat_get_col( m2, j ); 
00097       v2 = mat_get_col( m2, (j+1)%mat_width(m2) ); 
00098 
00099       printf( "<m2(:,%d)|m2(:,%d)>=%e\tnorm(m2(:,%d))=%e\n", j, (j+1)%mat_width(m2), vec_inner_product(v1, v2), j, vec_norm(v1, 2.) );
00100 
00101       vec_delete( v1 );
00102       vec_delete( v2 );
00103     }
00104   
00105 
00106   printf( "\n\nTesting some matrix-related numbers\n\n" );
00107 
00108   printf( "Some values for m1:\n" ); 
00109 
00110   printf( "det(m1)=%f\n", mat_det(m1) );
00111   printf( "rank(m1)=%d\n", mat_rank(m1) ); 
00112   printf( "cond(m1)=%f\n", mat_cond(m1) ); 
00113   printf( "norm2(m1)=%f\n", mat_norm2(m1) ); 
00114 
00115   cvec_delete( eval );
00116   cmat_delete( evec );
00117 
00118   mat_delete( m1 );
00119   mat_delete( m2 );
00120   mat_delete( m3 ); 
00121 
00122   vec_delete( v0 );
00123 
00124   return 0;
00125 }

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