GetFEM++  5.3
gmm_dense_sylvester.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2017 Yves Renard
5 
6  This file is a part of GetFEM++
7 
8  GetFEM++ is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /** @file gmm_dense_sylvester.h
33  @author Yves Renard <Yves.Renard@insa-lyon.fr>
34  @date June 5, 2003.
35  @brief Sylvester equation solver.
36 */
37 #ifndef GMM_DENSE_SYLVESTER_H
38 #define GMM_DENSE_SYLVESTER_H
39 
40 #include "gmm_kernel.h"
41 
42 namespace gmm {
43 
44  /* ********************************************************************* */
45  /* Kronecker system matrix. */
46  /* ********************************************************************* */
47  template <typename MAT1, typename MAT2, typename MAT3>
48  void kron(const MAT1 &m1, const MAT2 &m2, const MAT3 &m3_,
49  bool init = true) {
50  MAT3 &m3 = const_cast<MAT3 &>(m3_);
51  size_type m = mat_nrows(m1), n = mat_ncols(m1);
52  size_type l = mat_nrows(m2), k = mat_ncols(m2);
53 
54  GMM_ASSERT2(mat_nrows(m3) == m*l && mat_ncols(m3) == n*k,
55  "dimensions mismatch");
56 
57  for (size_type i = 0; i < m; ++i)
58  for (size_type j = 0; j < m; ++j)
59  if (init)
60  gmm::copy(gmm::scaled(m2, m1(i,j)),
61  gmm::sub_matrix(m3, sub_interval(l*i, l),
62  sub_interval(k*j, k)));
63  else
64  gmm::add(gmm::scaled(m2, m1(i,j)),
65  gmm::sub_matrix(m3, sub_interval(l*i, l),
66  sub_interval(k*j, k)));
67  }
68 
69 
70  /* ********************************************************************* */
71  /* Copy a matrix into a vector. */
72  /* ********************************************************************* */
73 
74  template <typename MAT, typename VECT>
75  colmatrix_to_vector(const MAT &A, VECT &v, col_major) {
76  size_type m = mat_nrows(A), n = mat_ncols(A);
77  GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
78  for (size_type i = 0; i < n; ++i)
79  gmm::copy(mat_col(A, i), sub_vector(v, sub_interval(i*m, m)));
80  }
81 
82  template <typename MAT, typename VECT>
83  colmatrix_to_vector(const MAT &A, VECT &v, row_and_col)
84  { colmatrix_to_vector(A, v, col_major()); }
85 
86  template <typename MAT, typename VECT>
87  colmatrix_to_vector(const MAT &A, VECT &v, col_and_row)
88  { colmatrix_to_vector(A, v, col_major()); }
89 
90  template <typename MAT, typename VECT>
91  colmatrix_to_vector(const MAT &A, VECT &v, row_major) {
92  size_type m = mat_nrows(mat), n = mat_ncols(A);
93  GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
94  for (size_type i = 0; i < m; ++i)
95  gmm::copy(mat_row(A, i), sub_vector(v, sub_slice(i, n, m)));
96  }
97 
98  template <typename MAT, typename VECT> inline
99  colmatrix_to_vector(const MAT &A, const VECT &v_) {
100  VECT &v = const_cast<VECT &>(v_);
101  colmatrix_to_vector(A, v, typename linalg_traits<MAT>::sub_orientation());
102  }
103 
104 
105  /* ********************************************************************* */
106  /* Copy a vector into a matrix. */
107  /* ********************************************************************* */
108 
109  template <typename MAT, typename VECT>
110  vector_to_colmatrix(const VECT &v, MAT &A, col_major) {
111  size_type m = mat_nrows(A), n = mat_ncols(A);
112  GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
113  for (size_type i = 0; i < n; ++i)
114  gmm::copy(sub_vector(v, sub_interval(i*m, m)), mat_col(A, i));
115  }
116 
117  template <typename MAT, typename VECT>
118  vector_to_colmatrix(const VECT &v, MAT &A, row_and_col)
119  { vector_to_colmatrix(v, A, col_major()); }
120 
121  template <typename MAT, typename VECT>
122  vector_to_colmatrix(const VECT &v, MAT &A, col_and_row)
123  { vector_to_colmatrix(v, A, col_major()); }
124 
125  template <typename MAT, typename VECT>
126  vector_to_colmatrix(const VECT &v, MAT &A, row_major) {
127  size_type m = mat_nrows(mat), n = mat_ncols(A);
128  GMM_ASSERT2(m*n == vect_size(v), "dimensions mismatch");
129  for (size_type i = 0; i < m; ++i)
130  gmm::copy(sub_vector(v, sub_slice(i, n, m)), mat_row(A, i));
131  }
132 
133  template <typename MAT, typename VECT> inline
134  vector_to_colmatrix(const VECT &v, const MAT &A_) {
135  MAT &A = const_cast<MAT &>(A_);
136  vector_to_colmatrix(v, A, typename linalg_traits<MAT>::sub_orientation());
137  }
138 
139  /* ********************************************************************* */
140  /* Solve sylvester equation. */
141  /* ********************************************************************* */
142 
143  // very prohibitive solver, to be replaced ...
144  template <typename MAT1, typename MAT2, typename MAT3, typename MAT4 >
145  void sylvester(const MAT1 &m1, const MAT2 &m2, const MAT3 &m3,
146  const MAT4 &m4_) {
147  typedef typename linalg_traits<Mat>::value_type T;
148 
149  MAT3 &m4 = const_cast<MAT4 &>(m4_);
150  size_type m = mat_nrows(m1), n = mat_ncols(m1);
151  size_type l = mat_nrows(m2), k = mat_ncols(m2);
152 
153  GMM_ASSERT2(m == n && l == k && m == mat_nrows(m3) &&
154  l == mat_ncols(m3) && m == mat_nrows(m4) && l == mat_ncols(m4),
155  "dimensions mismatch");
156 
157  gmm::dense_matrix<T> akronb(m*l, m*l);
158  gmm::dense_matrix<T> idm(m, m), idl(l,l);
159  gmm::copy(identity_matrix(), idm);
160  gmm::copy(identity_matrix(), idl);
161  std::vector<T> x(m*l), c(m*l);
162 
163  kron(idl, m1, akronb);
164  kron(gmm::transposed(m2), idm, akronb, false);
165 
166  colmatrix_to_vector(m3, c);
167  lu_solve(akronb, c, x);
168  vector_to_colmatrix(x, m4);
169 
170  }
171 }
172 
173 #endif
174 
void lu_solve(const DenseMatrix &LU, const Pvector &pvector, VectorX &x, const VectorB &b)
LU Solve : Solve equation Ax=b, given an LU factored matrix.
Definition: gmm_dense_lu.h:163
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
Include the base gmm files.