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.

233 lines
5.5 KiB

  1. //=====================================================
  2. // File : action_hessenberg.hh
  3. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  4. //=====================================================
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. //
  19. #ifndef ACTION_HESSENBERG
  20. #define ACTION_HESSENBERG
  21. #include "utilities.h"
  22. #include "STL_interface.hh"
  23. #include <string>
  24. #include "init/init_function.hh"
  25. #include "init/init_vector.hh"
  26. #include "init/init_matrix.hh"
  27. using namespace std;
  28. template<class Interface>
  29. class Action_hessenberg {
  30. public :
  31. // Ctor
  32. Action_hessenberg( int size ):_size(size)
  33. {
  34. MESSAGE("Action_hessenberg Ctor");
  35. // STL vector initialization
  36. init_matrix<pseudo_random>(X_stl,_size);
  37. init_matrix<null_function>(C_stl,_size);
  38. init_matrix<null_function>(resu_stl,_size);
  39. // generic matrix and vector initialization
  40. Interface::matrix_from_stl(X_ref,X_stl);
  41. Interface::matrix_from_stl(X,X_stl);
  42. Interface::matrix_from_stl(C,C_stl);
  43. _cost = 0;
  44. for (int j=0; j<_size-2; ++j)
  45. {
  46. double r = std::max(0,_size-j-1);
  47. double b = std::max(0,_size-j-2);
  48. _cost += 6 + 3*b + r*r*4 + r*_size*4;
  49. }
  50. }
  51. // invalidate copy ctor
  52. Action_hessenberg( const Action_hessenberg & )
  53. {
  54. INFOS("illegal call to Action_hessenberg Copy Ctor");
  55. exit(1);
  56. }
  57. // Dtor
  58. ~Action_hessenberg( void ){
  59. MESSAGE("Action_hessenberg Dtor");
  60. // deallocation
  61. Interface::free_matrix(X_ref,_size);
  62. Interface::free_matrix(X,_size);
  63. Interface::free_matrix(C,_size);
  64. }
  65. // action name
  66. static inline std::string name( void )
  67. {
  68. return "hessenberg_"+Interface::name();
  69. }
  70. double nb_op_base( void ){
  71. return _cost;
  72. }
  73. inline void initialize( void ){
  74. Interface::copy_matrix(X_ref,X,_size);
  75. }
  76. inline void calculate( void ) {
  77. Interface::hessenberg(X,C,_size);
  78. }
  79. void check_result( void ){
  80. // calculation check
  81. Interface::matrix_to_stl(C,resu_stl);
  82. // STL_interface<typename Interface::real_type>::hessenberg(X_stl,C_stl,_size);
  83. //
  84. // typename Interface::real_type error=
  85. // STL_interface<typename Interface::real_type>::norm_diff(C_stl,resu_stl);
  86. //
  87. // if (error>1.e-6){
  88. // INFOS("WRONG CALCULATION...residual=" << error);
  89. // exit(0);
  90. // }
  91. }
  92. private :
  93. typename Interface::stl_matrix X_stl;
  94. typename Interface::stl_matrix C_stl;
  95. typename Interface::stl_matrix resu_stl;
  96. typename Interface::gene_matrix X_ref;
  97. typename Interface::gene_matrix X;
  98. typename Interface::gene_matrix C;
  99. int _size;
  100. double _cost;
  101. };
  102. template<class Interface>
  103. class Action_tridiagonalization {
  104. public :
  105. // Ctor
  106. Action_tridiagonalization( int size ):_size(size)
  107. {
  108. MESSAGE("Action_tridiagonalization Ctor");
  109. // STL vector initialization
  110. init_matrix<pseudo_random>(X_stl,_size);
  111. for(int i=0; i<_size; ++i)
  112. {
  113. for(int j=0; j<i; ++j)
  114. X_stl[i][j] = X_stl[j][i];
  115. }
  116. init_matrix<null_function>(C_stl,_size);
  117. init_matrix<null_function>(resu_stl,_size);
  118. // generic matrix and vector initialization
  119. Interface::matrix_from_stl(X_ref,X_stl);
  120. Interface::matrix_from_stl(X,X_stl);
  121. Interface::matrix_from_stl(C,C_stl);
  122. _cost = 0;
  123. for (int j=0; j<_size-2; ++j)
  124. {
  125. double r = std::max(0,_size-j-1);
  126. double b = std::max(0,_size-j-2);
  127. _cost += 6. + 3.*b + r*r*8.;
  128. }
  129. }
  130. // invalidate copy ctor
  131. Action_tridiagonalization( const Action_tridiagonalization & )
  132. {
  133. INFOS("illegal call to Action_tridiagonalization Copy Ctor");
  134. exit(1);
  135. }
  136. // Dtor
  137. ~Action_tridiagonalization( void ){
  138. MESSAGE("Action_tridiagonalization Dtor");
  139. // deallocation
  140. Interface::free_matrix(X_ref,_size);
  141. Interface::free_matrix(X,_size);
  142. Interface::free_matrix(C,_size);
  143. }
  144. // action name
  145. static inline std::string name( void ) { return "tridiagonalization_"+Interface::name(); }
  146. double nb_op_base( void ){
  147. return _cost;
  148. }
  149. inline void initialize( void ){
  150. Interface::copy_matrix(X_ref,X,_size);
  151. }
  152. inline void calculate( void ) {
  153. Interface::tridiagonalization(X,C,_size);
  154. }
  155. void check_result( void ){
  156. // calculation check
  157. Interface::matrix_to_stl(C,resu_stl);
  158. // STL_interface<typename Interface::real_type>::tridiagonalization(X_stl,C_stl,_size);
  159. //
  160. // typename Interface::real_type error=
  161. // STL_interface<typename Interface::real_type>::norm_diff(C_stl,resu_stl);
  162. //
  163. // if (error>1.e-6){
  164. // INFOS("WRONG CALCULATION...residual=" << error);
  165. // exit(0);
  166. // }
  167. }
  168. private :
  169. typename Interface::stl_matrix X_stl;
  170. typename Interface::stl_matrix C_stl;
  171. typename Interface::stl_matrix resu_stl;
  172. typename Interface::gene_matrix X_ref;
  173. typename Interface::gene_matrix X;
  174. typename Interface::gene_matrix C;
  175. int _size;
  176. double _cost;
  177. };
  178. #endif