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.LGPL ** 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 <ICLUtils/CompatMacros.h> 00034 #include <ICLMath/DynMatrix.h> 00035 #include <ICLUtils/BasicTypes.h> 00036 #include <algorithm> 00037 00038 namespace icl{ 00039 namespace math{ 00040 00043 00044 00060 template<class T, class Init> 00061 inline DynMatrix<T> &matrix_init(DynMatrix<T> &m, Init init){ 00062 std::fill(m.begin(),m.end(),init); 00063 return m; 00064 } 00065 00067 00068 template<class T> ICLMath_IMP 00069 DynMatrix<T> &matrix_abs(DynMatrix<T> &m); 00070 00072 00073 template<class T> ICLMath_IMP 00074 DynMatrix<T> &matrix_abs(const DynMatrix<T> &m, DynMatrix<T> &dst); 00075 00077 00078 template<class T> ICLMath_IMP 00079 DynMatrix<T> &matrix_log(DynMatrix<T> &m); 00080 00082 00083 template<class T> ICLMath_IMP 00084 DynMatrix<T> &matrix_log(const DynMatrix<T> &m, DynMatrix<T> &dst); 00085 00087 00088 template<class T> ICLMath_IMP 00089 DynMatrix<T> &matrix_exp(DynMatrix<T> &m); 00090 00092 00093 template<class T> ICLMath_IMP 00094 DynMatrix<T> &matrix_exp(const DynMatrix<T> &m, DynMatrix<T> &dst); 00095 00097 00098 template<class T> ICLMath_IMP 00099 DynMatrix<T> &matrix_sqrt(DynMatrix<T> &m); 00100 00102 00103 template<class T> ICLMath_IMP 00104 DynMatrix<T> &matrix_sqrt(const DynMatrix<T> &m, DynMatrix<T> &dst); 00105 00107 00108 template<class T> ICLMath_IMP 00109 DynMatrix<T> &matrix_sqr(DynMatrix<T> &m); 00110 00112 00113 template<class T> ICLMath_IMP 00114 DynMatrix<T> &matrix_sqr(const DynMatrix<T> &m, DynMatrix<T> &dst); 00115 00117 00118 template<class T> ICLMath_IMP 00119 DynMatrix<T> &matrix_sin(DynMatrix<T> &m); 00120 00122 00123 template<class T> ICLMath_IMP 00124 DynMatrix<T> &matrix_sin(const DynMatrix<T> &m, DynMatrix<T> &dst); 00125 00127 00128 template<class T> ICLMath_IMP 00129 DynMatrix<T> &matrix_cos(DynMatrix<T> &m); 00130 00132 00133 template<class T> ICLMath_IMP 00134 DynMatrix<T> &matrix_cos(const DynMatrix<T> &m, DynMatrix<T> &dst); 00135 00137 00138 template<class T> ICLMath_IMP 00139 DynMatrix<T> &matrix_tan(DynMatrix<T> &m); 00140 00142 00143 template<class T> ICLMath_IMP 00144 DynMatrix<T> &matrix_tan(const DynMatrix<T> &m, DynMatrix<T> &dst); 00145 00147 00148 template<class T> ICLMath_IMP 00149 DynMatrix<T> &matrix_arcsin(DynMatrix<T> &m); 00150 00152 00153 template<class T> ICLMath_IMP 00154 DynMatrix<T> &matrix_arcsin(const DynMatrix<T> &m, DynMatrix<T> &dst); 00155 00157 00158 template<class T> ICLMath_IMP 00159 DynMatrix<T> &matrix_arccos(DynMatrix<T> &m); 00160 00162 00163 template<class T> ICLMath_IMP 00164 DynMatrix<T> &matrix_arccos(const DynMatrix<T> &m, DynMatrix<T> &dst); 00165 00167 00168 template<class T> ICLMath_IMP 00169 DynMatrix<T> &matrix_arctan(DynMatrix<T> &m); 00170 00172 00173 template<class T> ICLMath_IMP 00174 DynMatrix<T> &matrix_arctan(const DynMatrix<T> &m, DynMatrix<T> &dst); 00175 00177 00178 template<class T> ICLMath_IMP 00179 DynMatrix<T> &matrix_reciprocal(DynMatrix<T> &m); 00180 00182 00183 template<class T> ICLMath_IMP 00184 DynMatrix<T> &matrix_reciprocal(const DynMatrix<T> &m, DynMatrix<T> &dst); 00185 00188 00189 00190 template<class T> ICLMath_IMP 00191 DynMatrix<T> &matrix_powc(DynMatrix<T> &m, T exponent); 00192 00194 00195 template<class T> ICLMath_IMP 00196 DynMatrix<T> &matrix_powc(const DynMatrix<T> &m, T exponent, DynMatrix<T> &dst); 00197 00199 00200 template<class T> ICLMath_IMP 00201 DynMatrix<T> &matrix_addc(DynMatrix<T> &m, T val); 00202 00204 00205 template<class T> ICLMath_IMP 00206 DynMatrix<T> &matrix_addc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00207 00209 00210 template<class T> ICLMath_IMP 00211 DynMatrix<T> &matrix_subc(DynMatrix<T> &m, T val); 00212 00214 00215 template<class T> ICLMath_IMP 00216 DynMatrix<T> &matrix_subc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00217 00219 00220 template<class T> ICLMath_IMP 00221 DynMatrix<T> &matrix_divc(DynMatrix<T> &m, T val); 00222 00224 00225 template<class T> ICLMath_IMP 00226 DynMatrix<T> &matrix_divc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00227 00229 00230 template<class T> ICLMath_IMP 00231 DynMatrix<T> &matrix_mulc(DynMatrix<T> &m, T val); 00232 00234 00235 template<class T> ICLMath_IMP 00236 DynMatrix<T> &matrix_mulc(const DynMatrix<T> &m, T val, DynMatrix<T> &dst); 00237 00240 00241 00242 template<class T> ICLMath_IMP 00243 DynMatrix<T> &matrix_arctan2(const DynMatrix<T> &my, const DynMatrix<T> &mx, DynMatrix<T> &dst) 00244 throw (IncompatibleMatrixDimensionException); 00245 00247 00248 template<class T> ICLMath_IMP 00249 DynMatrix<T> &matrix_add(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00250 throw (IncompatibleMatrixDimensionException); 00251 00253 00254 template<class T> ICLMath_IMP 00255 DynMatrix<T> &matrix_sub(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00256 throw (IncompatibleMatrixDimensionException); 00257 00259 00260 template<class T> ICLMath_IMP 00261 DynMatrix<T> &matrix_mul(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00262 throw (IncompatibleMatrixDimensionException); 00263 00265 00266 template<class T> ICLMath_IMP 00267 DynMatrix<T> &matrix_div(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00268 throw (IncompatibleMatrixDimensionException); 00269 00271 00272 template<class T> ICLMath_IMP 00273 DynMatrix<T> &matrix_pow(const DynMatrix<T> &m1, const DynMatrix<T> &m2, DynMatrix<T> &dst) 00274 throw (IncompatibleMatrixDimensionException); 00275 00279 00280 00283 template<class T> ICLMath_IMP 00284 T matrix_distance(const DynMatrix<T> &m1, const DynMatrix<T> &m2, T norm = 2) 00285 throw (IncompatibleMatrixDimensionException); 00286 00288 00291 template<class T> ICLMath_IMP 00292 T matrix_divergence(const DynMatrix<T> &m1, const DynMatrix<T> &m2) 00293 throw (IncompatibleMatrixDimensionException); 00294 00295 00299 00300 00305 template<class T> ICLMath_IMP 00306 T matrix_min(const DynMatrix<T> &m, int *x = 0, int *y = 0); 00307 00309 00314 template<class T> ICLMath_IMP 00315 T matrix_max(const DynMatrix<T> &m, int *x = 0, int *y = 0); 00316 00318 00327 template<class T> ICLMath_IMP 00328 void matrix_minmax(const DynMatrix<T> &m, T dst[2], 00329 int *minx=0, int *miny=0, 00330 int *maxx=0, int *maxy=0); 00331 00332 00334 00335 template<class T> ICLMath_IMP 00336 T matrix_mean(const DynMatrix<T> &m); 00337 00339 00340 template<class T> ICLMath_IMP 00341 T matrix_var(const DynMatrix<T> &m); 00342 00344 00354 template<class T> ICLMath_IMP 00355 T matrix_var(const DynMatrix<T> &m, T mean, bool empiricalMean = true); 00356 00358 00361 template<class T> ICLMath_IMP 00362 void matrix_meanvar(const DynMatrix<T> &m, T *mean, T*var); 00363 00365 00366 template<class T> ICLMath_IMP 00367 T matrix_stddev(const DynMatrix<T> &m); 00368 00370 00380 template<class T> ICLMath_IMP 00381 T matrix_stddev(const DynMatrix<T> &m, T mean, bool empiricalMean = true); 00382 00386 00387 00388 template<class T> ICLMath_IMP 00389 DynMatrix<T> &matrix_muladd(const DynMatrix<T> &a, T alpha, const DynMatrix<T> &b, T beta, T gamma, DynMatrix<T> &dst) 00390 throw (IncompatibleMatrixDimensionException); 00391 00393 00394 template<class T> ICLMath_IMP 00395 DynMatrix<T> &matrix_muladd(const DynMatrix<T> &a, T alpha, T gamma, DynMatrix<T> &dst); 00396 00398 00399 template<class T> ICLMath_IMP 00400 DynMatrix<T> &matrix_mask(const DynMatrix<unsigned char> &mask, DynMatrix<T> &m) 00401 throw (IncompatibleMatrixDimensionException); 00402 00404 00405 template<class T> ICLMath_IMP 00406 DynMatrix<T> &matrix_mask(const DynMatrix<unsigned char> &mask, const DynMatrix<T> &m, DynMatrix<T> &dst) 00407 throw (IncompatibleMatrixDimensionException); 00408 00414 00415 enum transposedDef{ 00416 NONE_T=0, 00417 SRC1_T=1<<0, 00418 SRC2_T=1<<1, 00419 BOTH_T=SRC1_T | SRC2_T, 00420 }; 00421 00423 00434 template<class T> ICLMath_IMP 00435 DynMatrix<T> &matrix_mult_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00436 throw (IncompatibleMatrixDimensionException); 00437 00439 00450 template<class T> ICLMath_IMP 00451 DynMatrix<T> &big_matrix_mult_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00452 throw (IncompatibleMatrixDimensionException); 00453 00455 template<class T> ICLMath_IMP 00456 DynMatrix<T> &matrix_add_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00457 throw (IncompatibleMatrixDimensionException); 00458 00460 template<class T> ICLMath_IMP 00461 DynMatrix<T> &matrix_sub_t(const DynMatrix<T> &src1, const DynMatrix<T> &src2, DynMatrix<T> &dst, int transpDef) 00462 throw (IncompatibleMatrixDimensionException); 00463 00466 00467 00473 template<class T> ICLMath_IMP 00474 void svd_dyn(const DynMatrix<T> &A, DynMatrix<T> &U, DynMatrix<T> &S, DynMatrix<T> &V) throw (utils::ICLException); 00475 00476 00477 #if 0 00478 U.setBounds(A.cols(), A.rows()); 00479 V.setBounds(A.cols(), A.cols()); 00480 s.setBounds(1,A.cols()); 00481 DynMatrix<icl64f> A64f(A.cols(),A.rows()),U64f(U.cols(),U.rows()),s64f(1,s.rows()),V64f(V.cols(),V.rows()); 00482 std::copy(A.begin(),A.end(),A64f.begin()); 00483 00484 svd_cpp_64f(A64f,U64f,s64f,V64f); 00485 00486 std::copy(U64f.begin(),U64f.end(),U.begin()); 00487 std::copy(V64f.begin(),V64f.end(),V.begin()); 00488 std::copy(s64f.begin(),s64f.end(),s.begin()); 00489 } 00490 00492 // SVD specialization for direct call to 64f function in order to avoid unnecessary double to double conversion before hand 00493 template<> inline void svd_dyn<icl64f>(const DynMatrix<icl64f> &A, DynMatrix<icl64f> &U, DynMatrix<icl64f> &s, DynMatrix<icl64f> &V) { 00494 U.setBounds(A.cols(), A.rows()); 00495 V.setBounds(A.cols(), A.cols()); 00496 s.setBounds(1,A.cols()); 00497 00498 svd_cpp_64f(A,U,s,V); 00499 } 00502 #endif 00503 00506 } // namespace math 00507 }