digiKam
matrixoperations.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 17-8-2016
7  * Description : Some matrix utility functions including singular value
8  * decomposition, inverse, and pseudo-inverse.
9  *
10  * Copyright (C) 2016 by Omar Amin <Omar dot moh dot amin at gmail dot com>
11  * Copyright (C) 2019 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
12  * Copyright (C) 2016-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
13  *
14  * This program is free software; you can redistribute it
15  * and/or modify it under the terms of the GNU General
16  * Public License as published by the Free Software Foundation;
17  * either version 2, or (at your option)
18  * any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * ============================================================ */
26 
27 #ifndef DIGIKAM_MATRIX_OPERATIONS_H
28 #define DIGIKAM_MATRIX_OPERATIONS_H
29 
30 // C++ includes
31 
32 #include <vector>
33 #include <iostream>
34 
35 // Local includes
36 
37 #include "digikam_opencv.h"
38 
39 namespace Digikam
40 {
41 
42 namespace MatrixOperations
43 {
44 
45 std::vector<std::vector<float> > inv2(const std::vector<std::vector<float> >& mat);
46 
47 std::vector<std::vector<float> > pinv(const std::vector<std::vector<float> >& mat);
48 
49 void stdmattocvmat(const std::vector<std::vector<float> >& src, cv::Mat& dst);
50 
51 void cvmattostdmat(const cv::Mat& dst, std::vector<std::vector<float> >& src);
52 
53 template <typename T>
54 inline T signdlib(const T& a, const T& b)
55 {
56  if (b < 0)
57  {
58  return -std::abs(a);
59  }
60  else
61  {
62  return std::abs(a);
63  }
64 }
65 
66 template <typename T>
67 inline T pythag(const T& a, const T& b)
68 {
69  T absa = std::abs(a);
70  T absb = std::abs(b);
71 
72  if (absa > absb)
73  {
74  T val = absb/absa;
75  val *= val;
76 
77  return (absa * std::sqrt(1.0F + val));
78  }
79  else
80  {
81  if (absb == 0.0)
82  {
83  return 0.0;
84  }
85  else
86  {
87  T val = absa/absb;
88  val *= val;
89 
90  return (absb * std::sqrt(1.0F + val));
91  }
92  }
93 }
94 
95 void transpose(std::vector<std::vector<float> >& src,
96  std::vector<std::vector<float> >& dst);
97 
98 float trace(const std::vector<std::vector<float> >& src);
99 
100 bool svd3(std::vector<std::vector<float> >& a,
101  std::vector<float >& w,
102  std::vector<std::vector<float> >& v,
103  std::vector<float >& rv1);
104 
105 void svd(const std::vector<std::vector<float> >& m,
106  std::vector<std::vector<float> >& u,
107  std::vector<std::vector<float> >& w,
108  std::vector<std::vector<float> >& v);
109 
110 float determinant(const std::vector<std::vector<float> >& u);
111 
112 } // namespace MatrixOperations
113 
114 } // namespace Digikam
115 
116 #endif // DIGIKAM_MATRIX_OPERATIONS_H
#define T
std::vector< std::vector< float > > pinv(const std::vector< std::vector< float > > &mat)
Definition: matrixoperations.cpp:58
std::vector< std::vector< float > > inv2(const std::vector< std::vector< float > > &mat)
Definition: matrixoperations.cpp:39
void cvmattostdmat(const cv::Mat &dst, std::vector< std::vector< float > > &src)
Definition: matrixoperations.cpp:96
void transpose(std::vector< std::vector< float > > &src, std::vector< std::vector< float > > &dst)
Definition: matrixoperations.cpp:107
T pythag(const T &a, const T &b)
Definition: matrixoperations.h:67
float trace(const std::vector< std::vector< float > > &src)
Definition: matrixoperations.cpp:120
T signdlib(const T &a, const T &b)
Definition: matrixoperations.h:54
float determinant(const std::vector< std::vector< float > > &u)
Definition: matrixoperations.cpp:551
void stdmattocvmat(const std::vector< std::vector< float > > &src, cv::Mat &dst)
Definition: matrixoperations.cpp:85
bool svd3(std::vector< std::vector< float > > &a, std::vector< float > &w, std::vector< std::vector< float > > &v, std::vector< float > &rv1)
Definition: matrixoperations.cpp:138
void svd(const std::vector< std::vector< float > > &m, std::vector< std::vector< float > > &u, std::vector< std::vector< float > > &w, std::vector< std::vector< float > > &v)
Definition: matrixoperations.cpp:504
Definition: datefolderview.cpp:43