00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_DIAGONAL_INCLUDE
00013 #define MTL_DIAGONAL_INCLUDE
00014
00015
00016 #include <boost/numeric/mtl/utility/enable_if.hpp>
00017 #include <boost/numeric/mtl/matrix/compressed2D.hpp>
00018 #include <boost/numeric/mtl/matrix/inserter.hpp>
00019 #include <boost/numeric/mtl/vector/dense_vector.hpp>
00020 #include <boost/numeric/mtl/concept/collection.hpp>
00021
00022 namespace mtl {
00023
00024 namespace vector {
00025
00027 template <typename Vector>
00028 compressed2D<typename Collection<Vector>::value_type>
00029
00030 inline diagonal(const Vector& v)
00031 {
00032 typedef compressed2D<typename Collection<Vector>::value_type> matrix_type;
00033 matrix_type D(size(v), size(v));
00034 D= 0;
00035 mtl::matrix::inserter<matrix_type> ins(D, 1);
00036 for (typename Collection<Vector>::size_type i= 0; i < size(v); ++i)
00037 ins[i][i] << v[i];
00038
00039 return D;
00040 }
00041 }
00042
00043 namespace matrix {
00044
00046 template <typename Matrix>
00047
00048 dense_vector<typename Collection<Matrix>::value_type>
00049 inline diagonal(const Matrix& A)
00050 {
00051 using std::min;
00052 typedef typename Collection<Matrix>::size_type size_type;
00053 size_type n= min(num_rows(A), num_cols(A));
00054 dense_vector<typename Collection<Matrix>::value_type> v(n);
00055
00056 for (size_type i= 0; i < n; ++i)
00057 v[i]= A[i][i];
00058 return v;
00059 }
00060 }
00061
00062 }
00063
00064 #endif // MTL_DIAGONAL_INCLUDE