From 76e39c826f58120c902f1209549353cde9c3a127 Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Sun, 16 Feb 2014 15:43:50 +0100 Subject: [PATCH] arrays: add make_unit_matrix and norm2, norm2_sqr --- triqs/arrays/matrix.hpp | 8 ++++++++ triqs/arrays/vector.hpp | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/triqs/arrays/matrix.hpp b/triqs/arrays/matrix.hpp index 0b50b15b..34d1aeb6 100644 --- a/triqs/arrays/matrix.hpp +++ b/triqs/arrays/matrix.hpp @@ -208,6 +208,14 @@ namespace triqs { namespace arrays { #undef _IMPL_MATRIX_COMMON #undef IMPL_TYPE + + template + matrix make_unit_matrix(int dim) { + matrix r(dim, dim); + r() = 1; + return r; + } + template matrix_view make_matrix_view(ArrayType const & a) { diff --git a/triqs/arrays/vector.hpp b/triqs/arrays/vector.hpp index e5009355..74b530d2 100644 --- a/triqs/arrays/vector.hpp +++ b/triqs/arrays/vector.hpp @@ -223,6 +223,20 @@ namespace triqs { namespace arrays { #include "./blas_lapack/axpy.hpp" namespace triqs { namespace arrays { + // norm2 squared + template typename std::enable_if::value, typename V::value_type>::type norm2_sqr(V const& a) { + int dim = a.size(); + auto r = typename V::value_type{}; + for (int i = 0; i < dim; ++i) r += a(i) * a(i); + return r; + } + + // norm2 + template typename std::enable_if::value, typename V::value_type>::type norm2(V const& a) { + using std::sqrt; + return sqrt(norm2(a)); + } + // lexicographical comparison operators template typename std::enable_if< ImmutableVector::value && ImmutableVector::value , bool>::type