Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLMath/src/ICLMath/DynMatrixUtils.h ** 00010 ** Module : ICLMath ** 00011 ** Authors: Christof Elbrechter, Daniel Dornbusch ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.GPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLMath/DynMatrix.h> 00034 #include <ICLUtils/BasicTypes.h> 00035 #include <algorithm> 00036 00037 namespace icl{ 00038 namespace math{ 00039 00042 00043 00059 template<class T, class Init> 00060 inline DynMatrix<T> &matrix_init(DynMatrix<T> &m, Init init){ 00061 std::fill(m.begin(),m.end(),init); 00062 return m; 00063 } 00064 00066 00067 template<class T> 00068 DynMatrix<T> &matrix_abs(DynMatrix<T> &m); 00069 00071 00072 template<class T> 00073 DynMatrix<T> &matrix_abs(const DynMatrix<T> &m, DynMatrix<T> &dst); 00074 00076 00077 template<class T> 00078 DynMatrix<T> &matrix_log(DynMatrix<T> &m); 00079 00081 00082 template<class T> 00083 DynMatrix<T> &matrix_log(const DynMatrix<T> &m, DynMatrix<T> &dst); 00084 00086 00087 template<class T> 00088 DynMatrix<T> &matrix_exp(DynMatrix<T> &m); 00089 00091 00092 template<class T> 00093 DynMatrix<T> &matrix_exp(const DynMatrix<T> &m, DynMatrix<T> &dst); 00094 00096 00097 template<class T> 00098 DynMatrix<T> &matrix_sqrt(DynMatrix<T> &m); 00099 00101 00102 template<class T> 00103 DynMatrix<T> &matrix_sqrt(const DynMatrix<T> &m, DynMatrix<T> &dst); 00104 00106 00107 template<class T> 00108 DynMatrix<T> &matrix_sqr(DynMatrix<T> &m); 00109 00111 00112 template<class T> 00113 DynMatrix<T> &matrix_sqr(const DynMatrix<T> &m, DynMatrix<T> &dst); 00114 00116 00117 template<class T> 00118 DynMatrix<T> &matrix_sin(DynMatrix<T> &m); 00119 00121 00122 template<class T> 00123 DynMatrix<T> &matrix_sin(const DynMatrix<T> &m, DynMatrix<T> &dst); 00124 00126 00127 template<class T> 00128 DynMatrix<T> &matrix_cos(DynMatrix<T> &m); 00129 00131 00132 template<class T> 00133 DynMatrix<T> &matrix_cos(const DynMatrix<T> &m, DynMatrix<T> &dst); 00134 00136 00137 template<class T> 00138 DynMatrix<T> &matrix_tan(DynMatrix<T> &m); 00139 00141 00142 template<class T> 00143 DynMatrix<T> &matrix_tan(const DynMatrix<T> &m, DynMatrix<T> &dst); 00144 00146 00147 template<class T> 00148 DynMatrix<T> &matrix_arcsin(DynMatrix<T> &m); 00149 00151 00152 template<class T> 00153 DynMatrix<T> &matrix_arcsin(const DynMatrix<T> &m, DynMatrix<T> &dst); 00154 00156 00157 template<class T> 00158 DynMatrix<T> &matrix_arccos(DynMatrix<T> &m); 00159 00161 00162 template<class T> 00163 DynMatrix<T> &matrix_arccos(const DynMatrix<T> &m, DynMatrix<T> &dst); 00164 00166 00167 template<class T> 00168 DynMatrix<T> &matrix_arctan(DynMatrix<T> &m); 00169 00171 00172 template<class T> 00173 DynMatrix<T> &matrix_arctan(const DynMatrix<T> &m, DynMatrix<T> &dst); 00174 00176 00177 template<class T> 00178 DynMatrix<T> &matrix_reciprocal(DynMatrix<T> &m); 00179 00181 00182 template<class T> 00183 DynMatrix<T> &matrix_reciprocal(const DynMatrix<T> &m, DynMatrix<T> &dst); 00184 00187 00188 00189 template<class T> 00190 DynMatrix<T> &matrix_powc(DynMatrix<T> &m, T exponent); 00191 00193 00194 template<class T> 00195 DynMatrix<T> &matrix_powc(const DynMatrix<T> &m, T exponent, DynMatrix<T> &dst); 00196 00198 00199 template<class T> 00200 DynMatrix<T> &matrix_addc(DynMatrix<T> &m, T val); 00201 00203 00204 template<class T> 00205 DynMatrix<T> &matrix_addc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00206 00208 00209 template<class T> 00210 DynMatrix<T> &matrix_subc(DynMatrix<T> &m, T val); 00211 00213 00214 template<class T> 00215 DynMatrix<T> &matrix_subc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00216 00218 00219 template<class T> 00220 DynMatrix<T> &matrix_divc(DynMatrix<T> &m, T val); 00221 00223 00224 template<class T> 00225 DynMatrix<T> &matrix_divc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00226 00228 00229 template<class T> 00230 DynMatrix<T> &matrix_mulc(DynMatrix<T> &m, T val); 00231 00233 00234 template<class T> 00235 DynMatrix<T> &matrix_mulc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00236 00239 00240 00241 template<class T> 00242 DynMatrix<T> &matrix_arctan2(const DynMatrix<T> &my, const DynMatrix<T> &mx, DynMatrix<T> &dst) 00243 throw (IncompatibleMatrixDimensionException); 00244 00246 00247 template<class T> 00248 DynMatrix<T> &matrix_add(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00249 throw (IncompatibleMatrixDimensionException); 00250 00252 00253 template<class T> 00254 DynMatrix<T> &matrix_sub(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00255 throw (IncompatibleMatrixDimensionException); 00256 00258 00259 template<class T> 00260 DynMatrix<T> &matrix_mul(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00261 throw (IncompatibleMatrixDimensionException); 00262 00264 00265 template<class T> 00266 DynMatrix<T> &matrix_div(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00267 throw (IncompatibleMatrixDimensionException); 00268 00270 00271 template<class T> 00272 DynMatrix<T> &matrix_pow(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00273 throw (IncompatibleMatrixDimensionException); 00274 00278 00279 00282 template<class T> 00283 T matrix_distance(const DynMatrix<T> &m1, const DynMatrix<T> &m2, T norm=2) 00284 throw (IncompatibleMatrixDimensionException); 00285 00287 00290 template<class T> 00291 T matrix_divergence(const DynMatrix<T> &m1, const DynMatrix<T> &m2) 00292 throw (IncompatibleMatrixDimensionException); 00293 00294 00298 00299 00304 template<class T> 00305 T matrix_min(const DynMatrix<T> &m, int *x=0, int *y=0); 00306 00308 00313 template<class T> 00314 T matrix_max(const DynMatrix<T> &m, int *x=0, int *y=0); 00315 00317 00326 template<class T> 00327 void matrix_minmax(const DynMatrix<T> &m, T dst[2], 00328 int *minx=0, int *miny=0, 00329 int *maxx=0, int *maxy=0); 00330 00331 00333 00334 template<class T> 00335 T matrix_mean(const DynMatrix<T> &m); 00336 00338 00339 template<class T> 00340 T matrix_var(const DynMatrix<T> &m); 00341 00343 00353 template<class T> 00354 T matrix_var(const DynMatrix<T> &m, T mean, bool empiricalMean=true); 00355 00357 00360 template<class T> 00361 void matrix_meanvar(const DynMatrix<T> &m, T *mean, T*var); 00362 00364 00365 template<class T> 00366 T matrix_stddev(const DynMatrix<T> &m); 00367 00369 00379 template<class T> 00380 T matrix_stddev(const DynMatrix<T> &m, T mean, bool empiricalMean=true); 00381 00385 00386 00387 template<class T> 00388 DynMatrix<T> &matrix_muladd(const DynMatrix<T> &a,T alpha, const DynMatrix<T> &b, T beta, T gamma, DynMatrix<T> &dst) 00389 throw (IncompatibleMatrixDimensionException); 00390 00392 00393 template<class T> 00394 DynMatrix<T> &matrix_muladd(const DynMatrix<T> &a,T alpha, T gamma, DynMatrix<T> &dst); 00395 00397 00398 template<class T> 00399 DynMatrix<T> &matrix_mask(const DynMatrix<unsigned char> &mask, DynMatrix<T> &m) 00400 throw (IncompatibleMatrixDimensionException); 00401 00403 00404 template<class T> 00405 DynMatrix<T> &matrix_mask(const DynMatrix<unsigned char> &mask, const DynMatrix<T> &m, DynMatrix<T> &dst) 00406 throw (IncompatibleMatrixDimensionException); 00407 00413 00414 enum transposedDef{ 00415 NONE_T=0, 00416 SRC1_T=1<<0, 00417 SRC2_T=1<<1, 00418 BOTH_T=SRC1_T | SRC2_T, 00419 }; 00420 00422 00433 template<class T> 00434 DynMatrix<T> &matrix_mult_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00435 throw (IncompatibleMatrixDimensionException); 00436 00438 00449 template<class T> 00450 DynMatrix<T> &big_matrix_mult_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00451 throw (IncompatibleMatrixDimensionException); 00452 00454 template<class T> 00455 DynMatrix<T> &matrix_add_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00456 throw (IncompatibleMatrixDimensionException); 00457 00459 template<class T> 00460 DynMatrix<T> &matrix_sub_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00461 throw (IncompatibleMatrixDimensionException); 00462 00465 00466 00472 template<class T> 00473 void svd_dyn(const DynMatrix<T> &A, DynMatrix<T> &U, DynMatrix<T> &S, DynMatrix<T> &V) throw (utils::ICLException); 00474 00475 00476 #if 0 00477 U.setBounds(A.cols(), A.rows()); 00478 V.setBounds(A.cols(), A.cols()); 00479 s.setBounds(1,A.cols()); 00480 DynMatrix<icl64f> A64f(A.cols(),A.rows()),U64f(U.cols(),U.rows()),s64f(1,s.rows()),V64f(V.cols(),V.rows()); 00481 std::copy(A.begin(),A.end(),A64f.begin()); 00482 00483 svd_cpp_64f(A64f,U64f,s64f,V64f); 00484 00485 std::copy(U64f.begin(),U64f.end(),U.begin()); 00486 std::copy(V64f.begin(),V64f.end(),V.begin()); 00487 std::copy(s64f.begin(),s64f.end(),s.begin()); 00488 } 00489 00491 // SVD specialization for direct call to 64f function in order to avoid unnecessary double to double conversion before hand 00492 template<> inline void svd_dyn<icl64f>(const DynMatrix<icl64f> &A, DynMatrix<icl64f> &U, DynMatrix<icl64f> &s, DynMatrix<icl64f> &V) { 00493 U.setBounds(A.cols(), A.rows()); 00494 V.setBounds(A.cols(), A.cols()); 00495 s.setBounds(1,A.cols()); 00496 00497 svd_cpp_64f(A,U,s,V); 00498 } 00501 #endif 00502 00505 } // namespace math 00506 }