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.

174 lines
6.8 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2015 Yves Renard
  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_sylvester.h
  27. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  28. @date June 5, 2003.
  29. @brief Sylvester equation solver.
  30. */
  31. #ifndef GMM_DENSE_SYLVESTER_H
  32. #define GMM_DENSE_SYLVESTER_H
  33. #include "gmm_kernel.h"
  34. namespace gmm {
  35. /* ********************************************************************* */
  36. /* Kronecker system matrix. */
  37. /* ********************************************************************* */
  38. template <typename MAT1, typename MAT2, typename MAT3>
  39. void kron(const MAT1 &m1, const MAT2 &m2, const MAT3 &m3_,
  40. bool init = true) {
  41. MAT3 &m3 = const_cast<MAT3 &>(m3_);
  42. size_type m = mat_nrows(m1), n = mat_ncols(m1);
  43. size_type l = mat_nrows(m2), k = mat_ncols(m2);
  44. GMM_ASSERT2(mat_nrows(m3) == m*l && mat_ncols(m3) == n*k,
  45. "dimensions mismatch");
  46. for (size_type i = 0; i < m; ++i)
  47. for (size_type j = 0; j < m; ++j)
  48. if (init)
  49. gmm::copy(gmm::scaled(m2, m1(i,j)),
  50. gmm::sub_matrix(m3, sub_interval(l*i, l),
  51. sub_interval(k*j, k)));
  52. else
  53. gmm::add(gmm::scaled(m2, m1(i,j)),
  54. gmm::sub_matrix(m3, sub_interval(l*i, l),
  55. sub_interval(k*j, k)));
  56. }
  57. /* ********************************************************************* */
  58. /* Copy a matrix into a vector. */
  59. /* ********************************************************************* */
  60. template <typename MAT, typename VECT>
  61. colmatrix_to_vector(const MAT &A, VECT &v, col_major) {
  62. size_type m = mat_nrows(A), n = mat_ncols(A);
  63. GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
  64. for (size_type i = 0; i < n; ++i)
  65. gmm::copy(mat_col(A, i), sub_vector(v, sub_interval(i*m, m)));
  66. }
  67. template <typename MAT, typename VECT>
  68. colmatrix_to_vector(const MAT &A, VECT &v, row_and_col)
  69. { colmatrix_to_vector(A, v, col_major()); }
  70. template <typename MAT, typename VECT>
  71. colmatrix_to_vector(const MAT &A, VECT &v, col_and_row)
  72. { colmatrix_to_vector(A, v, col_major()); }
  73. template <typename MAT, typename VECT>
  74. colmatrix_to_vector(const MAT &A, VECT &v, row_major) {
  75. size_type m = mat_nrows(mat), n = mat_ncols(A);
  76. GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
  77. for (size_type i = 0; i < m; ++i)
  78. gmm::copy(mat_row(A, i), sub_vector(v, sub_slice(i, n, m)));
  79. }
  80. template <typename MAT, typename VECT> inline
  81. colmatrix_to_vector(const MAT &A, const VECT &v_) {
  82. VECT &v = const_cast<VECT &>(v_);
  83. colmatrix_to_vector(A, v, typename linalg_traits<MAT>::sub_orientation());
  84. }
  85. /* ********************************************************************* */
  86. /* Copy a vector into a matrix. */
  87. /* ********************************************************************* */
  88. template <typename MAT, typename VECT>
  89. vector_to_colmatrix(const VECT &v, MAT &A, col_major) {
  90. size_type m = mat_nrows(A), n = mat_ncols(A);
  91. GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
  92. for (size_type i = 0; i < n; ++i)
  93. gmm::copy(sub_vector(v, sub_interval(i*m, m)), mat_col(A, i));
  94. }
  95. template <typename MAT, typename VECT>
  96. vector_to_colmatrix(const VECT &v, MAT &A, row_and_col)
  97. { vector_to_colmatrix(v, A, col_major()); }
  98. template <typename MAT, typename VECT>
  99. vector_to_colmatrix(const VECT &v, MAT &A, col_and_row)
  100. { vector_to_colmatrix(v, A, col_major()); }
  101. template <typename MAT, typename VECT>
  102. vector_to_colmatrix(const VECT &v, MAT &A, row_major) {
  103. size_type m = mat_nrows(mat), n = mat_ncols(A);
  104. GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
  105. for (size_type i = 0; i < m; ++i)
  106. gmm::copy(sub_vector(v, sub_slice(i, n, m)), mat_row(A, i));
  107. }
  108. template <typename MAT, typename VECT> inline
  109. vector_to_colmatrix(const VECT &v, const MAT &A_) {
  110. MAT &A = const_cast<MAT &>(A_);
  111. vector_to_colmatrix(v, A, typename linalg_traits<MAT>::sub_orientation());
  112. }
  113. /* ********************************************************************* */
  114. /* Solve sylvester equation. */
  115. /* ********************************************************************* */
  116. // very prohibitive solver, to be replaced ...
  117. template <typename MAT1, typename MAT2, typename MAT3, typename MAT4 >
  118. void sylvester(const MAT1 &m1, const MAT2 &m2, const MAT3 &m3,
  119. const MAT4 &m4_) {
  120. typedef typename linalg_traits<Mat>::value_type T;
  121. MAT3 &m4 = const_cast<MAT4 &>(m4_);
  122. size_type m = mat_nrows(m1), n = mat_ncols(m1);
  123. size_type l = mat_nrows(m2), k = mat_ncols(m2);
  124. GMM_ASSERT2(m == n && l == k && m == mat_nrows(m3) &&
  125. l == mat_ncols(m3) && m == mat_nrows(m4) && l == mat_ncols(m4),
  126. "dimensions mismatch");
  127. gmm::dense_matrix<T> akronb(m*l, m*l);
  128. gmm::dense_matrix<T> idm(m, m), idl(l,l);
  129. gmm::copy(identity_matrix(), idm);
  130. gmm::copy(identity_matrix(), idl);
  131. std::vector<T> x(m*l), c(m*l);
  132. kron(idl, m1, akronb);
  133. kron(gmm::transposed(m2), idm, akronb, false);
  134. colmatrix_to_vector(m3, c);
  135. lu_solve(akronb, c, x);
  136. vector_to_colmatrix(x, m4);
  137. }
  138. }
  139. #endif