37 #ifndef GMM_DENSE_MATRIX_FUNCTIONS_H 38 #define GMM_DENSE_MATRIX_FUNCTIONS_H 50 typedef typename number_traits<T>::magnitude_type R;
51 bool singular =
false;
67 const size_type n = mat_nrows(A);
68 for (
int j=0; j < int(n); j++) {
69 typename dense_matrix<T>::iterator colj = A.begin() + j*n;
70 if (gmm::abs(colj[j]) > tol)
71 colj[j] = gmm::sqrt(colj[j]);
75 for (
int i=j-1; i >= 0; i--) {
76 typename dense_matrix<T>::const_iterator coli = A.begin() + i*n;
77 T colji = colj[i] = safe_divide(colj[i], (coli[i] + colj[j]));
78 for (
int k = 0; k < i; k++)
79 colj[k] -= coli[k] * colji;
84 GMM_WARNING1(
"Matrix is singular, may not have a square root");
89 void sqrtm(
const dense_matrix<std::complex<T> >& A,
90 dense_matrix<std::complex<T> >& SQRTMA)
92 GMM_ASSERT1(gmm::mat_nrows(A) == gmm::mat_ncols(A),
93 "Matrix square root requires a square matrix");
94 gmm::resize(SQRTMA, gmm::mat_nrows(A), gmm::mat_ncols(A));
95 dense_matrix<std::complex<T> > S(A), Q(A), TMP(A);
96 #if defined(GMM_USES_LAPACK) 99 GMM_ASSERT1(
false,
"Please recompile with lapack and blas librairies " 100 "to use sqrtm matrix function.");
103 gmm::mult(Q, S, TMP);
104 gmm::mult(TMP, gmm::transposed(Q), SQRTMA);
107 template <
typename T>
108 void sqrtm(
const dense_matrix<T>& A,
109 dense_matrix<std::complex<T> >& SQRTMA)
111 dense_matrix<std::complex<T> > cA(mat_nrows(A), mat_ncols(A));
112 gmm::copy(A, gmm::real_part(cA));
116 template <
typename T>
117 void sqrtm(
const dense_matrix<T>& A, dense_matrix<T>& SQRTMA)
119 dense_matrix<std::complex<T> > cA(mat_nrows(A), mat_ncols(A));
120 gmm::copy(A, gmm::real_part(cA));
121 dense_matrix<std::complex<T> > cSQRTMA(cA);
123 gmm::resize(SQRTMA, gmm::mat_nrows(A), gmm::mat_ncols(A));
124 gmm::copy(gmm::real_part(cSQRTMA), SQRTMA);
136 template <
typename T>
139 typedef typename number_traits<T>::magnitude_type R;
141 size_type n = gmm::mat_nrows(S);
142 GMM_ASSERT1(n == gmm::mat_ncols(S),
143 "Matrix logarithm is not defined for non-square matrices");
144 for (size_type i=0; i < n-1; ++i)
145 if (gmm::abs(S(i+1,i)) > default_tol(T())) {
146 GMM_ASSERT1(
false,
"An upper triangular matrix is expected");
149 for (size_type i=0; i < n-1; ++i)
150 if (gmm::real(S(i,i)) <= -default_tol(R()) &&
151 gmm::abs(gmm::imag(S(i,i))) <= default_tol(R())) {
152 GMM_ASSERT1(
false,
"Principal matrix logarithm is not defined " 153 "for matrices with negative eigenvalues");
158 R theta[] = { R(0),R(0),R(1.61e-2),R(5.38e-2),R(1.13e-1),R(1.86e-1),R(2.6429608311114350e-1) };
161 size_type p(0), m(6), opt_iters(100);
162 for (size_type k=0; k < opt_iters; ++k, scaling *= R(2)) {
163 dense_matrix<T> auxS(S);
164 for (size_type i = 0; i < n; ++i) auxS(i,i) -= R(1);
166 if (tau <= theta[6]) {
168 size_type j1(6), j2(6);
169 for (size_type j=0; j < 6; ++j)
170 if (tau <= theta[j]) { j1 = j;
break; }
171 for (size_type j=0; j < j1; ++j)
172 if (tau <= 2*theta[j]) { j2 = j;
break; }
173 if (j1 - j2 <= 1 || p == 2) { m = j1;
break; }
176 if (k == opt_iters-1)
177 GMM_WARNING1 (
"Maximum number of square roots exceeded; " 178 "the calculated matrix logarithm may still be accurate");
181 for (size_type i = 0; i < n; ++i) S(i,i) -= R(1);
185 std::vector<R> nodes, wts;
188 R nodes_[] = { R(0.5) };
190 nodes.assign(nodes_, nodes_+m+1);
191 wts.assign(wts_, wts_+m+1);
194 R nodes_[] = { R(0.211324865405187),R(0.788675134594813) };
195 R wts_[] = { R(0.5),R(0.5) };
196 nodes.assign(nodes_, nodes_+m+1);
197 wts.assign(wts_, wts_+m+1);
200 R nodes_[] = { R(0.112701665379258),R(0.500000000000000),R(0.887298334620742) };
201 R wts_[] = { R(0.277777777777778),R(0.444444444444444),R(0.277777777777778) };
202 nodes.assign(nodes_, nodes_+m+1);
203 wts.assign(wts_, wts_+m+1);
206 R nodes_[] = { R(0.0694318442029737),R(0.3300094782075718),R(0.6699905217924281),R(0.9305681557970263) };
207 R wts_[] = { R(0.173927422568727),R(0.326072577431273),R(0.326072577431273),R(0.173927422568727) };
208 nodes.assign(nodes_, nodes_+m+1);
209 wts.assign(wts_, wts_+m+1);
212 R nodes_[] = { R(0.0469100770306681),R(0.2307653449471584),R(0.5000000000000000),R(0.7692346550528415),R(0.9530899229693319) };
213 R wts_[] = { R(0.118463442528095),R(0.239314335249683),R(0.284444444444444),R(0.239314335249683),R(0.118463442528094) };
214 nodes.assign(nodes_, nodes_+m+1);
215 wts.assign(wts_, wts_+m+1);
218 R nodes_[] = { R(0.0337652428984240),R(0.1693953067668678),R(0.3806904069584015),R(0.6193095930415985),R(0.8306046932331322),R(0.9662347571015761) };
219 R wts_[] = { R(0.0856622461895853),R(0.1803807865240693),R(0.2339569672863452),R(0.2339569672863459),R(0.1803807865240693),R(0.0856622461895852) };
220 nodes.assign(nodes_, nodes_+m+1);
221 wts.assign(wts_, wts_+m+1);
224 R nodes_[] = { R(0.0254460438286208),R(0.1292344072003028),R(0.2970774243113015),R(0.4999999999999999),R(0.7029225756886985),R(0.8707655927996973),R(0.9745539561713792) };
225 R wts_[] = { R(0.0647424830844348),R(0.1398526957446384),R(0.1909150252525594),R(0.2089795918367343),R(0.1909150252525595),R(0.1398526957446383),R(0.0647424830844349) };
226 nodes.assign(nodes_, nodes_+m+1);
227 wts.assign(wts_, wts_+m+1);
231 dense_matrix<T> auxS1(S), auxS2(S);
232 std::vector<T> auxvec(n);
234 for (size_type j=0; j <= m; ++j) {
235 gmm::copy(gmm::scaled(auxS1, nodes[j]), auxS2);
236 gmm::add(gmm::identity_matrix(), auxS2);
238 for (size_type i=0; i < n; ++i) {
239 gmm::copy(gmm::mat_row(auxS1, i), auxvec);
240 gmm::lower_tri_solve(gmm::transposed(auxS2), auxvec,
false);
241 gmm::add(gmm::scaled(auxvec, wts[j]), gmm::mat_row(S, i));
245 gmm::scale(S, scaling);
251 template <
typename T>
252 void logm(
const dense_matrix<T>& A, dense_matrix<T>& LOGMA)
254 typedef typename number_traits<T>::magnitude_type R;
255 size_type n = gmm::mat_nrows(A);
256 GMM_ASSERT1(n == gmm::mat_ncols(A),
257 "Matrix logarithm is not defined for non-square matrices");
258 dense_matrix<T> S(A), Q(A);
259 #if defined(GMM_USES_LAPACK) 262 GMM_ASSERT1(
false,
"Please recompile with lapack and blas librairies " 263 "to use logm matrix function.");
266 bool convert_to_complex(
false);
267 if (!is_complex(T()))
268 for (size_type i=0; i < n-1; ++i)
269 if (gmm::abs(S(i+1,i)) > default_tol(T())) {
270 convert_to_complex =
true;
275 if (convert_to_complex) {
276 dense_matrix<std::complex<R> > cS(n,n), cQ(n,n), auxmat(n,n);
277 gmm::copy(gmm::real_part(S), gmm::real_part(cS));
278 gmm::copy(gmm::real_part(Q), gmm::real_part(cQ));
279 block2x2_reduction(cS, cQ, default_tol(R())*R(3));
280 for (size_type j=0; j < n-1; ++j)
281 for (size_type i=j+1; i < n; ++i)
284 gmm::mult(cQ, cS, auxmat);
285 gmm::mult(auxmat, gmm::transposed(cQ), cS);
287 gmm::copy(gmm::real_part(cS), LOGMA);
291 dense_matrix<T> auxmat(n,n);
293 gmm::mult(Q, S, auxmat);
294 gmm::mult(auxmat, gmm::transposed(Q), LOGMA);
void logm(const dense_matrix< T > &A, dense_matrix< T > &LOGMA)
Matrix logarithm (from GNU/Octave)
void logm_utri_inplace(dense_matrix< T > &S)
Matrix logarithm for upper triangular matrices (from GNU/Octave)
void sqrtm_utri_inplace(dense_matrix< T > &A)
Matrix square root for upper triangular matrices (from GNU Octave).
number_traits< typename linalg_traits< M >::value_type >::magnitude_type mat_norm1(const M &m)
*/
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void resize(M &v, size_type m, size_type n)
*/