[IPOL discuss] SVD for IPOL: which library, and how?

David.Tschumperle at greyc.ensicaen.fr David.Tschumperle at greyc.ensicaen.fr
Sun Apr 3 15:46:11 CEST 2011


Just a note about how the SVD can be used in CImg : Either it uses the
Lapack routines (if '#define cimg_use_lapack') is defined
before including the "CImg.h" header, either it uses his own routine
(inspired from the NR).
Here is an example of such an use (here, Lapack is not mandatory) :

#include "CImg.h"
using namespace cimg_library;

int main() {

  // Create one 'noisy' 5x5 identity matrix (uniform noise with amplitude
0.1).
  const CImg<> I = CImg<>::identity_matrix(5).noise(0.1,1);

  // Compute the SVD of the matrix I.
  CImg<> U,S,V;
  I.SVD(U,S,V);

  // Print diagonal coefficient (in vector) S.
  // (should all be around 1).
  S.print("Diag coefs.");

  // Rebuild a version J of I from U,S,V (J and I should be equal !).
  const CImg<> J = U*S.get_diagonal()*V.get_transpose();

  // Print the mean squared error between I and J
  // (should be very close to 0).
  std::fprintf(stderr,"Error(I,J) = %g\n",I.MSE(J));

  return 0;
}

To compile this with minimal dependencies (on Linux) :

g++ -o toto toto.cpp -Dcimg_display=0

and the execution gives :

$ ./toto
Diag coefs.: this = 0xbf8fd200, size = (1,5,1,1) [20 b], data =
(float*)0x9a100d8..0x9a100eb (non-shared) = [ 1.15535 ; 1.05855 ; 1.02251 ;
0.9813 ; 0.86202 ], min = 0.86202, max = 1.15535, mean = 1.01595, std =
0.107453, coords(min) = (0,4,0,0), coords(max) = (0,0,0,0).
Error(I,J) = 3.95106e-14

That's it, you see this is quite easy to use, and this doesn't require any
external libraries to link with (just a header to include).

David.



-- 
-----------------------------------------------------------------------------
David Tschumperlé
CNRS Researcher
GREYC (UMR-CNRS 6072)         E-mail: David.Tschumperle at greyc.ensicaen.fr
6, Bd du Marechal Juin        Tel: +33 (0)2-31-45-29-24
F-14050 CAEN Cedex            Fax: +33 (0)2-31-45-26-98
France                        http://www.greyc.ensicaen.fr/~dtschump
-----------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tools.ipol.im/mailman/archive/discuss/attachments/20110403/1a7fa21f/attachment.htm>


More information about the discuss mailing list