IT++ Logo

matfunc.cpp

Go to the documentation of this file.
00001 
00031 #include <itpp/base/matfunc.h>
00032 #include <itpp/base/algebra/schur.h>
00033 #include <itpp/base/converters.h>
00034 #include <limits>
00035 
00037 
00038 namespace itpp {
00039 
00040   // Square root of a square matrix (based on Octave sqrtm implementation)
00041   cmat sqrtm(const mat& A)
00042   {
00043     return sqrtm(to_cmat(A));
00044   }
00045 
00046   // Square root of the complex square matrix A
00047   cmat sqrtm(const cmat& A)
00048   {
00049     cmat U, T;
00050     schur(A, U, T);
00051 
00052     int n = U.rows();
00053     cmat R(n, n);
00054 
00055     R.zeros();
00056     for (int j = 0; j < n; j++)
00057       R(j, j) = std::sqrt(T(j, j));
00058 
00059     const double fudge = std::sqrt(std::numeric_limits<double>::min());
00060 
00061     for (int p = 0; p < n - 1; p++) {
00062       for (int i = 0; i < n - (p + 1); i++) {
00063         const int j = i + p + 1;
00064         std::complex<double> s = T(i, j);
00065 
00066         for (int k = i + 1; k < j; k++)
00067           s -= R(i, k) * R(k, j);
00068 
00069         const std::complex<double> d = R(i, i) + R(j, j) + fudge;
00070         const std::complex<double> conj_d = conj(d);
00071 
00072         R(i, j) = (s * conj_d) / (d * conj_d);
00073       }
00074     }
00075 
00076     return U * R * U.H();
00077   }
00078 
00079 
00080   bool all(const bvec &testvec)
00081   {
00082     for (int i=0; i<testvec.length(); i++)
00083       if (!testvec(i)) return false;
00084     return true;
00085   }
00086 
00087   bool any(const bvec &testvec)
00088   {
00089     for (int i=0; i<testvec.length(); i++)
00090       if (testvec(i)) return true;
00091     return false;
00092   }
00093 
00094   // ----------------------------------------------------------------------
00095   // Instantiations
00096   // ----------------------------------------------------------------------
00097 
00098   template int length(const vec &v);
00099   template int length(const cvec &v);
00100   template int length(const svec &v);
00101   template int length(const ivec &v);
00102   template int length(const bvec &v);
00103 
00104   template double sum(const vec &v);
00105   template std::complex<double> sum(const cvec &v);
00106   template short sum(const svec &v);
00107   template int sum(const ivec &v);
00108   template bin sum(const bvec &v);
00109 
00110   template double sum_sqr(const vec &v);
00111   template std::complex<double> sum_sqr(const cvec &v);
00112   template short sum_sqr(const svec &v);
00113   template int sum_sqr(const ivec &v);
00114   template bin sum_sqr(const bvec &v);
00115 
00116   template vec cumsum(const vec &v);
00117   template cvec cumsum(const cvec &v);
00118   template svec cumsum(const svec &v);
00119   template ivec cumsum(const ivec &v);
00120   template bvec cumsum(const bvec &v);
00121 
00122   template double prod(const vec &v);
00123   template std::complex<double> prod(const cvec &v);
00124   template short prod(const svec &v);
00125   template int prod(const ivec &v);
00126   template bin prod(const bvec &v);
00127 
00128   template vec cross(const vec &v1, const vec &v2);
00129   template cvec cross(const cvec &v1, const cvec &v2);
00130   template ivec cross(const ivec &v1, const ivec &v2);
00131   template svec cross(const svec &v1, const svec &v2);
00132   template bvec cross(const bvec &v1, const bvec &v2);
00133 
00134   template vec reverse(const vec &in);
00135   template cvec reverse(const cvec &in);
00136   template svec reverse(const svec &in);
00137   template ivec reverse(const ivec &in);
00138   template bvec reverse(const bvec &in);
00139 
00140   template vec zero_pad(const vec &v, int n);
00141   template cvec zero_pad(const cvec &v, int n);
00142   template ivec zero_pad(const ivec &v, int n);
00143   template svec zero_pad(const svec &v, int n);
00144   template bvec zero_pad(const bvec &v, int n);
00145 
00146   template vec zero_pad(const vec &v);
00147   template cvec zero_pad(const cvec &v);
00148   template ivec zero_pad(const ivec &v);
00149   template svec zero_pad(const svec &v);
00150   template bvec zero_pad(const bvec &v);
00151 
00152   template mat  zero_pad(const mat &, int, int);
00153   template cmat zero_pad(const cmat &, int, int);
00154   template imat zero_pad(const imat &, int, int);
00155   template smat zero_pad(const smat &, int, int);
00156   template bmat zero_pad(const bmat &, int, int);
00157 
00158   template vec sum(const mat &m, int dim);
00159   template cvec sum(const cmat &m, int dim);
00160   template svec sum(const smat &m, int dim);
00161   template ivec sum(const imat &m, int dim);
00162   template bvec sum(const bmat &m, int dim);
00163 
00164   template double sumsum(const mat &X);
00165   template std::complex<double> sumsum(const cmat &X);
00166   template short sumsum(const smat &X);
00167   template int sumsum(const imat &X);
00168   template bin sumsum(const bmat &X);
00169 
00170   template vec sum_sqr(const mat & m, int dim);
00171   template cvec sum_sqr(const cmat &m, int dim);
00172   template svec sum_sqr(const smat &m, int dim);
00173   template ivec sum_sqr(const imat &m, int dim);
00174   template bvec sum_sqr(const bmat &m, int dim);
00175 
00176   template mat cumsum(const mat &m, int dim);
00177   template cmat cumsum(const cmat &m, int dim);
00178   template smat cumsum(const smat &m, int dim);
00179   template imat cumsum(const imat &m, int dim);
00180   template bmat cumsum(const bmat &m, int dim);
00181 
00182   template vec prod(const mat &m, int dim);
00183   template cvec prod(const cmat &v, int dim);
00184   template svec prod(const smat &m, int dim);
00185   template ivec prod(const imat &m, int dim);
00186   template bvec prod(const bmat &m, int dim);
00187 
00188   template vec diag(const mat &in);
00189   template cvec diag(const cmat &in);
00190 
00191   template void diag(const vec &in, mat &m);
00192   template void diag(const cvec &in, cmat &m);
00193 
00194   template mat diag(const vec &v, const int K);
00195   template cmat diag(const cvec &v, const int K);
00196 
00197   template mat bidiag(const vec &, const vec &);
00198   template cmat bidiag(const cvec &, const cvec &);
00199 
00200   template void bidiag(const vec &, const vec &, mat &);
00201   template void bidiag(const cvec &, const cvec &, cmat &);
00202 
00203   template void bidiag(const mat &, vec &, vec &);
00204   template void bidiag(const cmat &, cvec &, cvec &);
00205 
00206   template mat tridiag(const vec &main, const vec &, const vec &);
00207   template cmat tridiag(const cvec &main, const cvec &, const cvec &);
00208 
00209   template void tridiag(const vec &main, const vec &, const vec &, mat &);
00210   template void tridiag(const cvec &main, const cvec &, const cvec &, cmat &);
00211 
00212   template void tridiag(const mat &m, vec &, vec &, vec &);
00213   template void tridiag(const cmat &m, cvec &, cvec &, cvec &);
00214 
00215   template double trace(const mat &in);
00216   template std::complex<double> trace(const cmat &in);
00217   template short trace(const smat &in);
00218   template int trace(const imat &in);
00219   template bin trace(const bmat &in);
00220 
00221   template void transpose(const mat &m, mat &out);
00222   template void transpose(const cmat &m, cmat &out);
00223   template void transpose(const smat &m, smat &out);
00224   template void transpose(const imat &m, imat &out);
00225   template void transpose(const bmat &m, bmat &out);
00226 
00227   template mat transpose(const mat &m);
00228   template cmat transpose(const cmat &m);
00229   template smat transpose(const smat &m);
00230   template imat transpose(const imat &m);
00231   template bmat transpose(const bmat &m);
00232 
00233   template void hermitian_transpose(const mat &m, mat &out);
00234   template void hermitian_transpose(const cmat &m, cmat &out);
00235   template void hermitian_transpose(const smat &m, smat &out);
00236   template void hermitian_transpose(const imat &m, imat &out);
00237   template void hermitian_transpose(const bmat &m, bmat &out);
00238 
00239   template mat hermitian_transpose(const mat &m);
00240   template cmat hermitian_transpose(const cmat &m);
00241   template smat hermitian_transpose(const smat &m);
00242   template imat hermitian_transpose(const imat &m);
00243   template bmat hermitian_transpose(const bmat &m);
00244 
00245   template vec rvectorize(const mat &m);
00246   template cvec rvectorize(const cmat &m);
00247   template ivec rvectorize(const imat &m);
00248   template svec rvectorize(const smat &m);
00249   template bvec rvectorize(const bmat &m);
00250 
00251   template vec cvectorize(const mat &m);
00252   template cvec cvectorize(const cmat &m);
00253   template ivec cvectorize(const imat &m);
00254   template svec cvectorize(const smat &m);
00255   template bvec cvectorize(const bmat &m);
00256 
00257   template mat reshape(const mat &m, int rows, int cols);
00258   template cmat reshape(const cmat &m, int rows, int cols);
00259   template imat reshape(const imat &m, int rows, int cols);
00260   template smat reshape(const smat &m, int rows, int cols);
00261   template bmat reshape(const bmat &m, int rows, int cols);
00262 
00263   template mat reshape(const vec &m, int rows, int cols);
00264   template cmat reshape(const cvec &m, int rows, int cols);
00265   template imat reshape(const ivec &m, int rows, int cols);
00266   template smat reshape(const svec &m, int rows, int cols);
00267   template bmat reshape(const bvec &m, int rows, int cols);
00268 
00269   template mat kron(const mat &X, const mat &Y);
00270   template cmat kron(const cmat &X, const cmat &Y);
00271   template imat kron(const imat &X, const imat &Y);
00272   template smat kron(const smat &X, const smat &Y);
00273   template bmat kron(const bmat &X, const bmat &Y);
00274 
00275   template vec repmat(const vec &v, int n);
00276   template cvec repmat(const cvec &v, int n);
00277   template ivec repmat(const ivec &v, int n);
00278   template svec repmat(const svec &v, int n);
00279   template bvec repmat(const bvec &v, int n);
00280 
00281   template mat repmat(const vec &v, int m, int n, bool transpose);
00282   template cmat repmat(const cvec &v, int m, int n, bool transpose);
00283   template imat repmat(const ivec &v, int m, int n, bool transpose);
00284   template smat repmat(const svec &v, int m, int n, bool transpose);
00285   template bmat repmat(const bvec &v, int m, int n, bool transpose);
00286 
00287   template mat repmat(const mat &data, int m, int n);
00288   template cmat repmat(const cmat &data, int m, int n);
00289   template imat repmat(const imat &data, int m, int n);
00290   template smat repmat(const smat &data, int m, int n);
00291   template bmat repmat(const bmat &data, int m, int n);
00292 
00293 } // namespace itpp
00294 
SourceForge Logo

Generated on Sat Apr 19 10:43:52 2008 for IT++ by Doxygen 1.5.5