00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef BOOST_NUMERIC_BINDINGS_LAPACK_HSEQR_HPP
00012 #define BOOST_NUMERIC_BINDINGS_LAPACK_HSEQR_HPP
00013
00014 #include <iostream>
00015 #include <complex>
00016 #include <boost/numeric/bindings/traits/traits.hpp>
00017 #include <boost/numeric/bindings/traits/matrix_traits.hpp>
00018 #include <boost/numeric/bindings/traits/type_traits.hpp>
00019 #include <boost/numeric/bindings/lapack/lapack.h>
00020 #include <boost/numeric/bindings/lapack/workspace.hpp>
00021 #include <boost/numeric/bindings/traits/detail/array.hpp>
00022
00023 #ifndef BOOST_NUMERIC_BINDINGS_NO_STRUCTURE_CHECK
00024 # include <boost/static_assert.hpp>
00025 # include <boost/type_traits.hpp>
00026 #endif
00027
00028
00029 namespace boost { namespace numeric { namespace bindings {
00030
00031 namespace lapack {
00032
00034
00035
00036
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 namespace detail {
00095
00096 inline
00097 int hseqr_backend(const char* job, const char* compz, const int* n,
00098 const int ilo, const int ihi, float* H, const int ldH,
00099 float* wr, float* wi, float* Z, const int ldz, float* work,
00100 const int* lwork){
00101 int info;
00102
00103
00104 LAPACK_SHSEQR(job, compz, n, &ilo, &ihi, H, &ldH, wr, wi,
00105 Z, &ldz, work, lwork, &info);
00106 return info;
00107 }
00108
00109
00110 inline
00111 int hseqr_backend(const char* job, const char* compz, const int* n,
00112 const int ilo, const int ihi, double* H, const int ldH,
00113 double* wr, double* wi, double* Z, const int ldz, double* work,
00114 const int* lwork){
00115 int info;
00116
00117
00118 LAPACK_DHSEQR(job, compz, n, &ilo, &ihi, H, &ldH, wr, wi,
00119 Z, &ldz, work, lwork, &info);
00120 return info;
00121 }
00122
00123
00124 inline
00125 int hseqr_backend(const char* job, const char* compz, int* n,
00126 const int ilo, const int ihi, traits::complex_f* H, const int ldH,
00127 traits::complex_f* w, traits::complex_f* Z, int ldz,
00128 traits::complex_f* work, const int* lwork){
00129 int info;
00130
00131
00132 LAPACK_CHSEQR(job, compz, n, &ilo, &ihi,
00133 traits::complex_ptr(H), &ldH,
00134 traits::complex_ptr(w),
00135 traits::complex_ptr(Z), &ldz,
00136 traits::complex_ptr(work), lwork, &info);
00137 return info;
00138 }
00139
00140
00141 inline
00142 int hseqr_backend(const char* job, const char* compz, int* n,
00143 const int ilo, const int ihi, traits::complex_d* H, const int ldH,
00144 traits::complex_d* w, traits::complex_d* Z, int ldz,
00145 traits::complex_d* work, const int* lwork){
00146 int info;
00147
00148
00149 LAPACK_ZHSEQR(job, compz, n, &ilo, &ihi,
00150 traits::complex_ptr(H), &ldH,
00151 traits::complex_ptr(w),
00152 traits::complex_ptr(Z), &ldz,
00153 traits::complex_ptr(work), lwork, &info);
00154 return info;
00155 }
00156
00157 template <int N>
00158 struct Hseqr{};
00159
00160 template <>
00161 struct Hseqr< 1 >{
00162 template < typename A, typename W, typename V>
00163 int operator() ( const char job, const char compz, A& H, W& w, V& Z ){
00164
00165
00166 int n = traits::matrix_size1(H);
00167 typedef typename A::value_type value_type;
00168 traits::detail::array<value_type> wr(n);
00169 traits::detail::array<value_type> wi(n);
00170
00171
00172 int lwork = -1;
00173 value_type work_temp;
00174 int result = detail::hseqr_backend(&job, &compz, &n, 1, n,
00175 traits::matrix_storage(H),
00176 traits::leading_dimension(H),
00177 wr.storage(), wi.storage(),
00178 traits::matrix_storage(Z),
00179 traits::leading_dimension(Z),
00180 &work_temp, &lwork);
00181
00182 if( result !=0 ) return result;
00183
00184 lwork = (int) work_temp;
00185 traits::detail::array<value_type> work(lwork);
00186 result = detail::hseqr_backend(&job, &compz, &n, 1, n,
00187 traits::matrix_storage(H),
00188 traits::leading_dimension(H),
00189 wr.storage(), wi.storage(),
00190 traits::matrix_storage(Z),
00191 traits::leading_dimension(Z),
00192 work.storage(), &lwork);
00193
00194 for (int i = 0; i < n; i++)
00195 w[i] = std::complex<value_type>(wr[i], wi[i]);
00196
00197 return result;
00198 }
00199 };
00200
00201 template <>
00202 struct Hseqr< 2 >{
00203 template < typename A, typename W, typename V>
00204 int operator() ( const char job, const char compz, A& H, W& w, V& Z ){
00205
00206
00207 int n = traits::matrix_size1(H);
00208 typedef typename A::value_type value_type;
00209
00210
00211 int lwork = -1;
00212 value_type work_temp;
00213 int result = detail::hseqr_backend(&job, &compz, &n, 1, n,
00214 traits::matrix_storage(H),
00215 traits::leading_dimension(H),
00216 traits::vector_storage(w),
00217 traits::matrix_storage(Z), traits::leading_dimension(Z),
00218 &work_temp, &lwork);
00219
00220 if( result !=0 ) return result;
00221
00222 lwork = (int) std::real(work_temp);
00223 traits::detail::array<value_type> work(lwork);
00224 result = detail::hseqr_backend(&job, &compz, &n, 1, n,
00225 traits::matrix_storage(H),
00226 traits::leading_dimension(H),
00227 traits::vector_storage(w),
00228 traits::matrix_storage(Z), traits::leading_dimension(Z),
00229 work.storage(), &lwork);
00230
00231 return result;
00232 }
00233 };
00234
00235 template < typename A, typename W, typename V>
00236 int hseqr( const char job, const char compz, A& H, W& w, V& Z ){
00237
00238
00239 assert ( job == 'E' || job == 'S' );
00240 assert ( compz == 'N' || compz == 'I' || compz == 'V' );
00241
00242 typedef typename A::value_type value_type;
00243
00244 int result = detail::Hseqr< n_workspace_args<value_type>::value >()(
00245 job, compz, H, w, Z);
00246
00247 return result;
00248 }
00249 }
00250
00251
00252 template < typename A, typename W>
00253 int hseqr( const char job, A& H, W& w){
00254
00255 #ifndef BOOST_NUMERIC_BINDINGS_NO_STRUCTURE_CHECK
00256 BOOST_STATIC_ASSERT((boost::is_same<
00257 typename traits::matrix_traits<A>::matrix_structure,
00258 traits::general_t
00259 >::value));
00260 #endif
00261
00262 #ifndef NDEBUG
00263 int const n = traits::matrix_size1(H);
00264 #endif
00265
00266 typedef typename A::value_type value_type;
00267 typedef typename W::value_type complex_value_type;
00268
00269 assert(traits::matrix_size2(H) == n);
00270 assert(traits::vector_size(w) == n);
00271
00272 ublas::matrix<value_type, ublas::column_major> Z(1,1);
00273 return detail::hseqr( job, 'N', H, w, Z );
00274 }
00275
00276
00277 template < typename A, typename W, typename Z>
00278 int hseqr( const char job, const char compz, A& H, W& w, Z& z){
00279
00280 #ifndef BOOST_NUMERIC_BINDINGS_NO_STRUCTURE_CHECK
00281 BOOST_STATIC_ASSERT((boost::is_same<
00282 typename traits::matrix_traits<A>::matrix_structure,
00283 traits::general_t
00284 >::value));
00285 #endif
00286
00287 #ifndef NDEBUG
00288 int const n = traits::matrix_size1(H);
00289 #endif
00290
00291 typedef typename A::value_type value_type;
00292 assert(traits::matrix_size2(H) == n);
00293 assert(traits::vector_size(w) == n);
00294 assert(traits::matrix_size2(z) == n);
00295
00296 return detail::hseqr( job, compz, H, w, z );
00297 }
00298
00299 }
00300 }}}
00301
00302 #endif