You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
11 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2014-2017 Konstantinos Poulios
  4. This file is a part of GetFEM++
  5. GetFEM++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_dense_matrix_functions.h
  27. @author Konstantinos Poulios <poulios.konstantinos@gmail.com>
  28. @date December 10, 2014.
  29. @brief Common matrix functions for dense matrices.
  30. */
  31. #ifndef GMM_DENSE_MATRIX_FUNCTIONS_H
  32. #define GMM_DENSE_MATRIX_FUNCTIONS_H
  33. namespace gmm {
  34. /**
  35. Matrix square root for upper triangular matrices (from GNU Octave).
  36. */
  37. template <typename T>
  38. void sqrtm_utri_inplace(dense_matrix<T>& A)
  39. {
  40. typedef typename number_traits<T>::magnitude_type R;
  41. bool singular = false;
  42. // The following code is equivalent to this triple loop:
  43. //
  44. // n = rows (A);
  45. // for j = 1:n
  46. // A(j,j) = sqrt (A(j,j));
  47. // for i = j-1:-1:1
  48. // A(i,j) /= (A(i,i) + A(j,j));
  49. // k = 1:i-1;
  50. // t storing a A(k,j) -= A(k,i) * A(i,j);
  51. // endfor
  52. // endfor
  53. R tol = R(0); // default_tol(R()) * gmm::mat_maxnorm(A);
  54. const size_type n = mat_nrows(A);
  55. for (int j=0; j < int(n); j++) {
  56. typename dense_matrix<T>::iterator colj = A.begin() + j*n;
  57. if (gmm::abs(colj[j]) > tol)
  58. colj[j] = gmm::sqrt(colj[j]);
  59. else
  60. singular = true;
  61. for (int i=j-1; i >= 0; i--) {
  62. typename dense_matrix<T>::const_iterator coli = A.begin() + i*n;
  63. T colji = colj[i] = safe_divide(colj[i], (coli[i] + colj[j]));
  64. for (int k = 0; k < i; k++)
  65. colj[k] -= coli[k] * colji;
  66. }
  67. }
  68. if (singular)
  69. GMM_WARNING1("Matrix is singular, may not have a square root");
  70. }
  71. template <typename T>
  72. void sqrtm(const dense_matrix<std::complex<T> >& A,
  73. dense_matrix<std::complex<T> >& SQRTMA)
  74. {
  75. GMM_ASSERT1(gmm::mat_nrows(A) == gmm::mat_ncols(A),
  76. "Matrix square root requires a square matrix");
  77. gmm::resize(SQRTMA, gmm::mat_nrows(A), gmm::mat_ncols(A));
  78. dense_matrix<std::complex<T> > S(A), Q(A), TMP(A);
  79. #if defined(GMM_USES_LAPACK)
  80. schur(TMP, S, Q);
  81. #else
  82. GMM_ASSERT1(false, "Please recompile with lapack and blas librairies "
  83. "to use sqrtm matrix function.");
  84. #endif
  85. sqrtm_utri_inplace(S);
  86. gmm::mult(Q, S, TMP);
  87. gmm::mult(TMP, gmm::transposed(Q), SQRTMA);
  88. }
  89. template <typename T>
  90. void sqrtm(const dense_matrix<T>& A,
  91. dense_matrix<std::complex<T> >& SQRTMA)
  92. {
  93. dense_matrix<std::complex<T> > cA(mat_nrows(A), mat_ncols(A));
  94. gmm::copy(A, gmm::real_part(cA));
  95. sqrtm(cA, SQRTMA);
  96. }
  97. template <typename T>
  98. void sqrtm(const dense_matrix<T>& A, dense_matrix<T>& SQRTMA)
  99. {
  100. dense_matrix<std::complex<T> > cA(mat_nrows(A), mat_ncols(A));
  101. gmm::copy(A, gmm::real_part(cA));
  102. dense_matrix<std::complex<T> > cSQRTMA(cA);
  103. sqrtm(cA, cSQRTMA);
  104. gmm::resize(SQRTMA, gmm::mat_nrows(A), gmm::mat_ncols(A));
  105. gmm::copy(gmm::real_part(cSQRTMA), SQRTMA);
  106. // dense_matrix<std::complex<T1> >::const_reference
  107. // it = cSQRTMA.begin(), ite = cSQRTMA.end();
  108. // dense_matrix<std::complex<T1> >::reference
  109. // rit = SQRTMA.begin();
  110. // for (; it != ite; ++it, ++rit) *rit = it->real();
  111. }
  112. /**
  113. Matrix logarithm for upper triangular matrices (from GNU/Octave)
  114. */
  115. template <typename T>
  116. void logm_utri_inplace(dense_matrix<T>& S)
  117. {
  118. typedef typename number_traits<T>::magnitude_type R;
  119. size_type n = gmm::mat_nrows(S);
  120. GMM_ASSERT1(n == gmm::mat_ncols(S),
  121. "Matrix logarithm is not defined for non-square matrices");
  122. for (size_type i=0; i < n-1; ++i)
  123. if (gmm::abs(S(i+1,i)) > default_tol(T())) {
  124. GMM_ASSERT1(false, "An upper triangular matrix is expected");
  125. break;
  126. }
  127. for (size_type i=0; i < n-1; ++i)
  128. if (gmm::real(S(i,i)) <= -default_tol(R()) &&
  129. gmm::abs(gmm::imag(S(i,i))) <= default_tol(R())) {
  130. GMM_ASSERT1(false, "Principal matrix logarithm is not defined "
  131. "for matrices with negative eigenvalues");
  132. break;
  133. }
  134. // Algorithm 11.9 in "Function of matrices", by N. Higham
  135. 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) };
  136. R scaling(1);
  137. size_type p(0), m(6), opt_iters(100);
  138. for (size_type k=0; k < opt_iters; ++k, scaling *= R(2)) {
  139. dense_matrix<T> auxS(S);
  140. for (size_type i = 0; i < n; ++i) auxS(i,i) -= R(1);
  141. R tau = gmm::mat_norm1(auxS);
  142. if (tau <= theta[6]) {
  143. ++p;
  144. size_type j1(6), j2(6);
  145. for (size_type j=0; j < 6; ++j)
  146. if (tau <= theta[j]) { j1 = j; break; }
  147. for (size_type j=0; j < j1; ++j)
  148. if (tau <= 2*theta[j]) { j2 = j; break; }
  149. if (j1 - j2 <= 1 || p == 2) { m = j1; break; }
  150. }
  151. sqrtm_utri_inplace(S);
  152. if (k == opt_iters-1)
  153. GMM_WARNING1 ("Maximum number of square roots exceeded; "
  154. "the calculated matrix logarithm may still be accurate");
  155. }
  156. for (size_type i = 0; i < n; ++i) S(i,i) -= R(1);
  157. if (m > 0) {
  158. std::vector<R> nodes, wts;
  159. switch(m) {
  160. case 0: {
  161. R nodes_[] = { R(0.5) };
  162. R wts_[] = { R(1) };
  163. nodes.assign(nodes_, nodes_+m+1);
  164. wts.assign(wts_, wts_+m+1);
  165. } break;
  166. case 1: {
  167. R nodes_[] = { R(0.211324865405187),R(0.788675134594813) };
  168. R wts_[] = { R(0.5),R(0.5) };
  169. nodes.assign(nodes_, nodes_+m+1);
  170. wts.assign(wts_, wts_+m+1);
  171. } break;
  172. case 2: {
  173. R nodes_[] = { R(0.112701665379258),R(0.500000000000000),R(0.887298334620742) };
  174. R wts_[] = { R(0.277777777777778),R(0.444444444444444),R(0.277777777777778) };
  175. nodes.assign(nodes_, nodes_+m+1);
  176. wts.assign(wts_, wts_+m+1);
  177. } break;
  178. case 3: {
  179. R nodes_[] = { R(0.0694318442029737),R(0.3300094782075718),R(0.6699905217924281),R(0.9305681557970263) };
  180. R wts_[] = { R(0.173927422568727),R(0.326072577431273),R(0.326072577431273),R(0.173927422568727) };
  181. nodes.assign(nodes_, nodes_+m+1);
  182. wts.assign(wts_, wts_+m+1);
  183. } break;
  184. case 4: {
  185. R nodes_[] = { R(0.0469100770306681),R(0.2307653449471584),R(0.5000000000000000),R(0.7692346550528415),R(0.9530899229693319) };
  186. R wts_[] = { R(0.118463442528095),R(0.239314335249683),R(0.284444444444444),R(0.239314335249683),R(0.118463442528094) };
  187. nodes.assign(nodes_, nodes_+m+1);
  188. wts.assign(wts_, wts_+m+1);
  189. } break;
  190. case 5: {
  191. R nodes_[] = { R(0.0337652428984240),R(0.1693953067668678),R(0.3806904069584015),R(0.6193095930415985),R(0.8306046932331322),R(0.9662347571015761) };
  192. R wts_[] = { R(0.0856622461895853),R(0.1803807865240693),R(0.2339569672863452),R(0.2339569672863459),R(0.1803807865240693),R(0.0856622461895852) };
  193. nodes.assign(nodes_, nodes_+m+1);
  194. wts.assign(wts_, wts_+m+1);
  195. } break;
  196. case 6: {
  197. R nodes_[] = { R(0.0254460438286208),R(0.1292344072003028),R(0.2970774243113015),R(0.4999999999999999),R(0.7029225756886985),R(0.8707655927996973),R(0.9745539561713792) };
  198. R wts_[] = { R(0.0647424830844348),R(0.1398526957446384),R(0.1909150252525594),R(0.2089795918367343),R(0.1909150252525595),R(0.1398526957446383),R(0.0647424830844349) };
  199. nodes.assign(nodes_, nodes_+m+1);
  200. wts.assign(wts_, wts_+m+1);
  201. } break;
  202. }
  203. dense_matrix<T> auxS1(S), auxS2(S);
  204. std::vector<T> auxvec(n);
  205. gmm::clear(S);
  206. for (size_type j=0; j <= m; ++j) {
  207. gmm::copy(gmm::scaled(auxS1, nodes[j]), auxS2);
  208. gmm::add(gmm::identity_matrix(), auxS2);
  209. // S += wts[i] * auxS1 * inv(auxS2)
  210. for (size_type i=0; i < n; ++i) {
  211. gmm::copy(gmm::mat_row(auxS1, i), auxvec);
  212. gmm::lower_tri_solve(gmm::transposed(auxS2), auxvec, false);
  213. gmm::add(gmm::scaled(auxvec, wts[j]), gmm::mat_row(S, i));
  214. }
  215. }
  216. }
  217. gmm::scale(S, scaling);
  218. }
  219. /**
  220. Matrix logarithm (from GNU/Octave)
  221. */
  222. template <typename T>
  223. void logm(const dense_matrix<T>& A, dense_matrix<T>& LOGMA)
  224. {
  225. typedef typename number_traits<T>::magnitude_type R;
  226. size_type n = gmm::mat_nrows(A);
  227. GMM_ASSERT1(n == gmm::mat_ncols(A),
  228. "Matrix logarithm is not defined for non-square matrices");
  229. dense_matrix<T> S(A), Q(A);
  230. #if defined(GMM_USES_LAPACK)
  231. schur(A, S, Q); // A = Q * S * Q^T
  232. #else
  233. GMM_ASSERT1(false, "Please recompile with lapack and blas librairies "
  234. "to use logm matrix function.");
  235. #endif
  236. bool convert_to_complex(false);
  237. if (!is_complex(T()))
  238. for (size_type i=0; i < n-1; ++i)
  239. if (gmm::abs(S(i+1,i)) > default_tol(T())) {
  240. convert_to_complex = true;
  241. break;
  242. }
  243. gmm::resize(LOGMA, n, n);
  244. if (convert_to_complex) {
  245. dense_matrix<std::complex<R> > cS(n,n), cQ(n,n), auxmat(n,n);
  246. gmm::copy(gmm::real_part(S), gmm::real_part(cS));
  247. gmm::copy(gmm::real_part(Q), gmm::real_part(cQ));
  248. block2x2_reduction(cS, cQ, default_tol(R())*R(3));
  249. for (size_type j=0; j < n-1; ++j)
  250. for (size_type i=j+1; i < n; ++i)
  251. cS(i,j) = T(0);
  252. logm_utri_inplace(cS);
  253. gmm::mult(cQ, cS, auxmat);
  254. gmm::mult(auxmat, gmm::transposed(cQ), cS);
  255. // Remove small complex values which may have entered calculation
  256. gmm::copy(gmm::real_part(cS), LOGMA);
  257. // GMM_ASSERT1(gmm::mat_norm1(gmm::imag_part(cS)) < n*default_tol(T()),
  258. // "Internal error, imag part should be zero");
  259. } else {
  260. dense_matrix<T> auxmat(n,n);
  261. logm_utri_inplace(S);
  262. gmm::mult(Q, S, auxmat);
  263. gmm::mult(auxmat, gmm::transposed(Q), LOGMA);
  264. }
  265. }
  266. }
  267. #endif