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.

222 lines
8.7 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2012 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_tri_solve.h
  27. @author Yves Renard
  28. @date October 13, 2002.
  29. @brief Solve triangular linear system for dense matrices.
  30. */
  31. #ifndef GMM_TRI_SOLVE_H__
  32. #define GMM_TRI_SOLVE_H__
  33. #include "gmm_interface.h"
  34. namespace gmm {
  35. template <typename TriMatrix, typename VecX>
  36. void upper_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  37. col_major, abstract_sparse, bool is_unit) {
  38. typename linalg_traits<TriMatrix>::value_type x_j;
  39. for (int j = int(k) - 1; j >= 0; --j) {
  40. typedef typename linalg_traits<TriMatrix>::const_sub_col_type COL;
  41. COL c = mat_const_col(T, j);
  42. typename linalg_traits<COL>::const_iterator
  43. it = vect_const_begin(c), ite = vect_const_end(c);
  44. if (!is_unit) x[j] /= c[j];
  45. for (x_j = x[j]; it != ite ; ++it)
  46. if (int(it.index()) < j) x[it.index()] -= x_j * (*it);
  47. }
  48. }
  49. template <typename TriMatrix, typename VecX>
  50. void upper_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  51. col_major, abstract_dense, bool is_unit) {
  52. typename linalg_traits<TriMatrix>::value_type x_j;
  53. for (int j = int(k) - 1; j >= 0; --j) {
  54. typedef typename linalg_traits<TriMatrix>::const_sub_col_type COL;
  55. COL c = mat_const_col(T, j);
  56. typename linalg_traits<COL>::const_iterator
  57. it = vect_const_begin(c), ite = it + j;
  58. typename linalg_traits<VecX>::iterator itx = vect_begin(x);
  59. if (!is_unit) x[j] /= c[j];
  60. for (x_j = x[j]; it != ite ; ++it, ++itx) *itx -= x_j * (*it);
  61. }
  62. }
  63. template <typename TriMatrix, typename VecX>
  64. void lower_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  65. col_major, abstract_sparse, bool is_unit) {
  66. typename linalg_traits<TriMatrix>::value_type x_j;
  67. // cout << "(lower col)The Tri Matrix = " << T << endl;
  68. // cout << "k = " << endl;
  69. for (int j = 0; j < int(k); ++j) {
  70. typedef typename linalg_traits<TriMatrix>::const_sub_col_type COL;
  71. COL c = mat_const_col(T, j);
  72. typename linalg_traits<COL>::const_iterator
  73. it = vect_const_begin(c), ite = vect_const_end(c);
  74. if (!is_unit) x[j] /= c[j];
  75. for (x_j = x[j]; it != ite ; ++it)
  76. if (int(it.index()) > j && it.index() < k) x[it.index()] -= x_j*(*it);
  77. }
  78. }
  79. template <typename TriMatrix, typename VecX>
  80. void lower_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  81. col_major, abstract_dense, bool is_unit) {
  82. typename linalg_traits<TriMatrix>::value_type x_j;
  83. for (int j = 0; j < int(k); ++j) {
  84. typedef typename linalg_traits<TriMatrix>::const_sub_col_type COL;
  85. COL c = mat_const_col(T, j);
  86. typename linalg_traits<COL>::const_iterator
  87. it = vect_const_begin(c) + (j+1), ite = vect_const_begin(c) + k;
  88. typename linalg_traits<VecX>::iterator itx = vect_begin(x) + (j+1);
  89. if (!is_unit) x[j] /= c[j];
  90. for (x_j = x[j]; it != ite ; ++it, ++itx) *itx -= x_j * (*it);
  91. }
  92. }
  93. template <typename TriMatrix, typename VecX>
  94. void upper_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  95. row_major, abstract_sparse, bool is_unit) {
  96. typedef typename linalg_traits<TriMatrix>::const_sub_row_type ROW;
  97. typename linalg_traits<TriMatrix>::value_type t;
  98. typename linalg_traits<TriMatrix>::const_row_iterator
  99. itr = mat_row_const_end(T);
  100. for (int i = int(k) - 1; i >= 0; --i) {
  101. --itr;
  102. ROW c = linalg_traits<TriMatrix>::row(itr);
  103. typename linalg_traits<ROW>::const_iterator
  104. it = vect_const_begin(c), ite = vect_const_end(c);
  105. for (t = x[i]; it != ite; ++it)
  106. if (int(it.index()) > i && it.index() < k) t -= (*it) * x[it.index()];
  107. if (!is_unit) x[i] = t / c[i]; else x[i] = t;
  108. }
  109. }
  110. template <typename TriMatrix, typename VecX>
  111. void upper_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  112. row_major, abstract_dense, bool is_unit) {
  113. typename linalg_traits<TriMatrix>::value_type t;
  114. for (int i = int(k) - 1; i >= 0; --i) {
  115. typedef typename linalg_traits<TriMatrix>::const_sub_row_type ROW;
  116. ROW c = mat_const_row(T, i);
  117. typename linalg_traits<ROW>::const_iterator
  118. it = vect_const_begin(c) + (i + 1), ite = vect_const_begin(c) + k;
  119. typename linalg_traits<VecX>::iterator itx = vect_begin(x) + (i+1);
  120. for (t = x[i]; it != ite; ++it, ++itx) t -= (*it) * (*itx);
  121. if (!is_unit) x[i] = t / c[i]; else x[i] = t;
  122. }
  123. }
  124. template <typename TriMatrix, typename VecX>
  125. void lower_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  126. row_major, abstract_sparse, bool is_unit) {
  127. typename linalg_traits<TriMatrix>::value_type t;
  128. for (int i = 0; i < int(k); ++i) {
  129. typedef typename linalg_traits<TriMatrix>::const_sub_row_type ROW;
  130. ROW c = mat_const_row(T, i);
  131. typename linalg_traits<ROW>::const_iterator
  132. it = vect_const_begin(c), ite = vect_const_end(c);
  133. for (t = x[i]; it != ite; ++it)
  134. if (int(it.index()) < i) t -= (*it) * x[it.index()];
  135. if (!is_unit) x[i] = t / c[i]; else x[i] = t;
  136. }
  137. }
  138. template <typename TriMatrix, typename VecX>
  139. void lower_tri_solve__(const TriMatrix& T, VecX& x, size_t k,
  140. row_major, abstract_dense, bool is_unit) {
  141. typename linalg_traits<TriMatrix>::value_type t;
  142. for (int i = 0; i < int(k); ++i) {
  143. typedef typename linalg_traits<TriMatrix>::const_sub_row_type ROW;
  144. ROW c = mat_const_row(T, i);
  145. typename linalg_traits<ROW>::const_iterator
  146. it = vect_const_begin(c), ite = it + i;
  147. typename linalg_traits<VecX>::iterator itx = vect_begin(x);
  148. for (t = x[i]; it != ite; ++it, ++itx) t -= (*it) * (*itx);
  149. if (!is_unit) x[i] = t / c[i]; else x[i] = t;
  150. }
  151. }
  152. // Triangular Solve: x <-- T^{-1} * x
  153. template <typename TriMatrix, typename VecX> inline
  154. void upper_tri_solve(const TriMatrix& T, VecX &x_, bool is_unit = false)
  155. { upper_tri_solve(T, x_, mat_nrows(T), is_unit); }
  156. template <typename TriMatrix, typename VecX> inline
  157. void lower_tri_solve(const TriMatrix& T, VecX &x_, bool is_unit = false)
  158. { lower_tri_solve(T, x_, mat_nrows(T), is_unit); }
  159. template <typename TriMatrix, typename VecX> inline
  160. void upper_tri_solve(const TriMatrix& T, VecX &x_, size_t k,
  161. bool is_unit) {
  162. VecX& x = const_cast<VecX&>(x_);
  163. GMM_ASSERT2(mat_nrows(T) >= k && vect_size(x) >= k
  164. && mat_ncols(T) >= k && !is_sparse(x_), "dimensions mismatch");
  165. upper_tri_solve__(T, x, k,
  166. typename principal_orientation_type<typename
  167. linalg_traits<TriMatrix>::sub_orientation>::potype(),
  168. typename linalg_traits<TriMatrix>::storage_type(),
  169. is_unit);
  170. }
  171. template <typename TriMatrix, typename VecX> inline
  172. void lower_tri_solve(const TriMatrix& T, VecX &x_, size_t k,
  173. bool is_unit) {
  174. VecX& x = const_cast<VecX&>(x_);
  175. GMM_ASSERT2(mat_nrows(T) >= k && vect_size(x) >= k
  176. && mat_ncols(T) >= k && !is_sparse(x_), "dimensions mismatch");
  177. lower_tri_solve__(T, x, k,
  178. typename principal_orientation_type<typename
  179. linalg_traits<TriMatrix>::sub_orientation>::potype(),
  180. typename linalg_traits<TriMatrix>::storage_type(),
  181. is_unit);
  182. }
  183. }
  184. #endif // GMM_TRI_SOLVE_H__