/* * matlab_norm.cpp * * Copyright (c) 2010 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include #include #include // cmath might only be needed on some systems (gcc versions?), but it shouldn't cause any problems by being here #include #ifdef __APPLE_CC__ #include "Accelerate/Accelerate.h" #endif #ifdef WIN32 #ifdef INTELMKL extern "C" { #include "mkl_cblas.h" #include "mkl_lapack.h" } #else extern "C" { #include "cblas.h" #include "clapack.h" } #include "mpt_svd.h" #endif typedef int __CLPK_integer; typedef float __CLPK_doublereal; #endif WIN32 #ifdef linux extern "C" { #include //typedef int integer; #include #include } typedef integer __CLPK_integer; typedef float __CLPK_doublereal; #endif #include "rimage.h" using namespace std; /* Try to recreate Matlab's "norm" method for complex matrixes. "NORM(X) is the largest singular value of X, max(svd(X))." */ double matlab_norm( RImage< std::complex > &input ) { // http://www.netlib.org/lapack/complex/cgesvd.f // detailed description of LAPACK cgesvd algorithm. // initialize input parameters for LPACK dgesvd function char jobu, jobvt; int rtnval; __CLPK_integer rows, cols, lda, ldu, ldvt, lwork, lrwork, info; jobu = 'A'; jobvt = 'A'; rows = (__CLPK_integer)input.width; cols = (__CLPK_integer)input.height; lda = max((__CLPK_integer)1, rows); ldu = rows; ldvt = cols; // generally should make lwork value large. // 2*N+2*N*NB or N*N+N+N*NB or N*N+2*N+2*N*NB or N*N+M*N // LWORK >= MAX(1,2*MIN(M,N)+MAX(M,N)) lwork = max(3*min(rows,cols)+max(rows,cols), 5*min(rows,cols)-4) * 3 ; // greater of 5*N or 5*M lrwork = max(5*rows, 5*cols); #ifdef WIN32 __CLPK_doublecomplex *a = new __CLPK_doublecomplex[ rows*cols]; __CLPK_doublereal *s = new __CLPK_doublereal[ min(rows, cols) ]; __CLPK_doublecomplex *u = NULL; __CLPK_doublecomplex *vt = new __CLPK_doublecomplex[ ldvt * cols ]; #ifdef INTELMKL u = new __CLPK_doublereal[ ldu * rows ]; __CLPK_doublecomplex *work = new __CLPK_doublecomplex[ lwork ]; __CLPK_doublereal *rwork = new __CLPK_doublereal[ lrwork ]; #endif #else __CLPK_doublecomplex a[ rows*cols]; __CLPK_doublereal s[ min(rows, cols) ]; __CLPK_doublecomplex u[ ldu * rows ]; __CLPK_doublecomplex vt[ ldvt * cols ]; __CLPK_doublecomplex work[ lwork ]; __CLPK_doublereal rwork[ lrwork ]; #endif for(int i = 0; i < cols; ++i) { for(int j = 0; j < rows; ++j) { a[ ( i*rows + j ) ].r = input[j][i].real(); a[ ( i*rows + j ) ].i = input[j][i].imag(); } } //make LAPACK dgesvd call or call local version if LAPACK is not available #ifdef WIN32 #ifdef INTELMKL rtnval = sgesvd_( &jobu, &jobvt, &rows, &cols, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, &info); #else info = 0; rtnval = dsvd( a, rows, cols, s, vt ); // The LAPACK based svd methods return the results in a separate array ('u'), but this one changes 'a' itself. u = a; // Try rearranging the results to match the lapack routine. std::sort( s, s+min(rows,cols), greater( ) ); if( rows == ldu ) { for ( int i = rows-1; i >= 0; i-- ) for ( int j = 0; j < i; j++ ) { std::swap( vt[i*cols+j] , vt[j*rows+i] ); } } #endif #else /* int zgesvd_(char *jobu, char *jobvt, __CLPK_integer *m, __CLPK_integer *n, __CLPK_doublecomplex *a, __CLPK_integer *lda, __CLPK_doublereal *s, __CLPK_doublecomplex *u, __CLPK_integer *ldu, __CLPK_doublecomplex *vt, __CLPK_integer *ldvt, __CLPK_doublecomplex *work, __CLPK_integer *lwork, __CLPK_doublereal *rwork, __CLPK_integer *info); */ rtnval = zgesvd_( &jobu, &jobvt, &rows, &cols, a, &lda, s, u, &ldu, vt, &ldvt, work, &lwork, rwork, &info); #endif if( info < 0 ) cerr << "the i-th argument of had an illegal value" << endl; if( info > 0 ) cout << "CBDSQR did not converge." << endl; double norm = std::numeric_limits::min(); for(int i = 0; i < min(rows, cols); ++i) { if( s[i] > norm ) norm = s[i]; } #ifdef WIN32 delete[] vt; delete[] s; delete[] a; #ifdef INTELMKL delete[] u; delete[] work; delete[] rwork; #endif #endif return norm; }