Chris@16: // Chris@16: // Copyright (c) 2000-2002 Chris@16: // Joerg Walter, Mathias Koch Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // The authors gratefully acknowledge the support of Chris@16: // GeNeSys mbH & Co. KG in producing this work. Chris@16: // Chris@16: Chris@16: #ifndef _BOOST_UBLAS_OPERATION_ Chris@16: #define _BOOST_UBLAS_OPERATION_ Chris@16: Chris@16: #include Chris@16: Chris@16: /** \file operation.hpp Chris@16: * \brief This file contains some specialized products. Chris@16: */ Chris@16: Chris@16: // axpy-based products Chris@16: // Alexei Novakov had a lot of ideas to improve these. Thanks. Chris@16: // Hendrik Kueck proposed some new kernel. Thanks again. Chris@16: Chris@16: namespace boost { namespace numeric { namespace ublas { Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const compressed_matrix &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, row_major_tag) { Chris@16: typedef typename V::size_type size_type; Chris@16: typedef typename V::value_type value_type; Chris@16: Chris@16: for (size_type i = 0; i < e1.filled1 () -1; ++ i) { Chris@16: size_type begin = e1.index1_data () [i]; Chris@16: size_type end = e1.index1_data () [i + 1]; Chris@16: value_type t (v (i)); Chris@16: for (size_type j = begin; j < end; ++ j) Chris@16: t += e1.value_data () [j] * e2 () (e1.index2_data () [j]); Chris@16: v (i) = t; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const compressed_matrix &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, column_major_tag) { Chris@16: typedef typename V::size_type size_type; Chris@16: Chris@16: for (size_type j = 0; j < e1.filled1 () -1; ++ j) { Chris@16: size_type begin = e1.index1_data () [j]; Chris@16: size_type end = e1.index1_data () [j + 1]; Chris@16: for (size_type i = begin; i < end; ++ i) Chris@16: v (e1.index2_data () [i]) += e1.value_data () [i] * e2 () (j); Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: // Dispatcher Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const compressed_matrix &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, bool init = true) { Chris@16: typedef typename V::value_type value_type; Chris@16: typedef typename L1::orientation_category orientation_category; Chris@16: Chris@16: if (init) Chris@16: v.assign (zero_vector (e1.size1 ())); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: vector cv (v); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_vector_assign (cv, prod (e1, e2)); Chris@16: #endif Chris@16: axpy_prod (e1, e2, v, orientation_category ()); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits::epsilon () * verrorbound, internal_logic ()); Chris@16: #endif Chris@16: return v; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V Chris@16: axpy_prod (const compressed_matrix &e1, Chris@16: const vector_expression &e2) { Chris@16: typedef V vector_type; Chris@16: Chris@16: vector_type v (e1.size1 ()); Chris@16: return axpy_prod (e1, e2, v, true); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const coordinate_matrix &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, bool init = true) { Chris@16: typedef typename V::size_type size_type; Chris@16: typedef typename V::value_type value_type; Chris@16: typedef L1 layout_type; Chris@16: Chris@16: size_type size1 = e1.size1(); Chris@16: size_type size2 = e1.size2(); Chris@16: Chris@16: if (init) { Chris@16: noalias(v) = zero_vector(size1); Chris@16: } Chris@16: Chris@16: for (size_type i = 0; i < e1.nnz(); ++i) { Chris@16: size_type row_index = layout_type::index_M( e1.index1_data () [i], e1.index2_data () [i] ); Chris@16: size_type col_index = layout_type::index_m( e1.index1_data () [i], e1.index2_data () [i] ); Chris@16: v( row_index ) += e1.value_data () [i] * e2 () (col_index); Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, packed_random_access_iterator_tag, row_major_tag) { Chris@16: typedef const E1 expression1_type; Chris@16: typedef typename V::size_type size_type; Chris@16: Chris@16: typename expression1_type::const_iterator1 it1 (e1 ().begin1 ()); Chris@16: typename expression1_type::const_iterator1 it1_end (e1 ().end1 ()); Chris@16: while (it1 != it1_end) { Chris@16: size_type index1 (it1.index1 ()); Chris@16: #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION Chris@16: typename expression1_type::const_iterator2 it2 (it1.begin ()); Chris@16: typename expression1_type::const_iterator2 it2_end (it1.end ()); Chris@16: #else Chris@16: typename expression1_type::const_iterator2 it2 (boost::numeric::ublas::begin (it1, iterator1_tag ())); Chris@16: typename expression1_type::const_iterator2 it2_end (boost::numeric::ublas::end (it1, iterator1_tag ())); Chris@16: #endif Chris@16: while (it2 != it2_end) { Chris@16: v (index1) += *it2 * e2 () (it2.index2 ()); Chris@16: ++ it2; Chris@16: } Chris@16: ++ it1; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, packed_random_access_iterator_tag, column_major_tag) { Chris@16: typedef const E1 expression1_type; Chris@16: typedef typename V::size_type size_type; Chris@16: Chris@16: typename expression1_type::const_iterator2 it2 (e1 ().begin2 ()); Chris@16: typename expression1_type::const_iterator2 it2_end (e1 ().end2 ()); Chris@16: while (it2 != it2_end) { Chris@16: size_type index2 (it2.index2 ()); Chris@16: #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION Chris@16: typename expression1_type::const_iterator1 it1 (it2.begin ()); Chris@16: typename expression1_type::const_iterator1 it1_end (it2.end ()); Chris@16: #else Chris@16: typename expression1_type::const_iterator1 it1 (boost::numeric::ublas::begin (it2, iterator2_tag ())); Chris@16: typename expression1_type::const_iterator1 it1_end (boost::numeric::ublas::end (it2, iterator2_tag ())); Chris@16: #endif Chris@16: while (it1 != it1_end) { Chris@16: v (it1.index1 ()) += *it1 * e2 () (index2); Chris@16: ++ it1; Chris@16: } Chris@16: ++ it2; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, sparse_bidirectional_iterator_tag) { Chris@16: typedef const E2 expression2_type; Chris@16: Chris@16: typename expression2_type::const_iterator it (e2 ().begin ()); Chris@16: typename expression2_type::const_iterator it_end (e2 ().end ()); Chris@16: while (it != it_end) { Chris@16: v.plus_assign (column (e1 (), it.index ()) * *it); Chris@16: ++ it; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: // Dispatcher Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, packed_random_access_iterator_tag) { Chris@16: typedef typename E1::orientation_category orientation_category; Chris@16: return axpy_prod (e1, e2, v, packed_random_access_iterator_tag (), orientation_category ()); Chris@16: } Chris@16: Chris@16: Chris@16: /** \brief computes v += A x or v = A x in an Chris@16: optimized fashion. Chris@16: Chris@16: \param e1 the matrix expression \c A Chris@16: \param e2 the vector expression \c x Chris@16: \param v the result vector \c v Chris@16: \param init a boolean parameter Chris@16: Chris@16: axpy_prod(A, x, v, init) implements the well known Chris@16: axpy-product. Setting \a init to \c true is equivalent to call Chris@16: v.clear() before axpy_prod. Currently \a init Chris@16: defaults to \c true, but this may change in the future. Chris@16: Chris@16: Up to now there are some specialisation for compressed Chris@16: matrices that give a large speed up compared to prod. Chris@16: Chris@16: \ingroup blas2 Chris@16: Chris@16: \internal Chris@16: Chris@16: template parameters: Chris@16: \param V type of the result vector \c v Chris@16: \param E1 type of a matrix expression \c A Chris@16: \param E2 type of a vector expression \c x Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const vector_expression &e2, Chris@16: V &v, bool init = true) { Chris@16: typedef typename V::value_type value_type; Chris@16: typedef typename E2::const_iterator::iterator_category iterator_category; Chris@16: Chris@16: if (init) Chris@16: v.assign (zero_vector (e1 ().size1 ())); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: vector cv (v); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_vector_assign (cv, prod (e1, e2)); Chris@16: #endif Chris@16: axpy_prod (e1, e2, v, iterator_category ()); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits::epsilon () * verrorbound, internal_logic ()); Chris@16: #endif Chris@16: return v; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const vector_expression &e2) { Chris@16: typedef V vector_type; Chris@16: Chris@16: vector_type v (e1 ().size1 ()); Chris@16: return axpy_prod (e1, e2, v, true); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const compressed_matrix &e2, Chris@16: V &v, column_major_tag) { Chris@16: typedef typename V::size_type size_type; Chris@16: typedef typename V::value_type value_type; Chris@16: Chris@16: for (size_type j = 0; j < e2.filled1 () -1; ++ j) { Chris@16: size_type begin = e2.index1_data () [j]; Chris@16: size_type end = e2.index1_data () [j + 1]; Chris@16: value_type t (v (j)); Chris@16: for (size_type i = begin; i < end; ++ i) Chris@16: t += e2.value_data () [i] * e1 () (e2.index2_data () [i]); Chris@16: v (j) = t; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const compressed_matrix &e2, Chris@16: V &v, row_major_tag) { Chris@16: typedef typename V::size_type size_type; Chris@16: Chris@16: for (size_type i = 0; i < e2.filled1 () -1; ++ i) { Chris@16: size_type begin = e2.index1_data () [i]; Chris@16: size_type end = e2.index1_data () [i + 1]; Chris@16: for (size_type j = begin; j < end; ++ j) Chris@16: v (e2.index2_data () [j]) += e2.value_data () [j] * e1 () (i); Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: // Dispatcher Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const compressed_matrix &e2, Chris@16: V &v, bool init = true) { Chris@16: typedef typename V::value_type value_type; Chris@16: typedef typename L2::orientation_category orientation_category; Chris@16: Chris@16: if (init) Chris@16: v.assign (zero_vector (e2.size2 ())); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: vector cv (v); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_vector_assign (cv, prod (e1, e2)); Chris@16: #endif Chris@16: axpy_prod (e1, e2, v, orientation_category ()); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits::epsilon () * verrorbound, internal_logic ()); Chris@16: #endif Chris@16: return v; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const compressed_matrix &e2) { Chris@16: typedef V vector_type; Chris@16: Chris@16: vector_type v (e2.size2 ()); Chris@16: return axpy_prod (e1, e2, v, true); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: V &v, packed_random_access_iterator_tag, column_major_tag) { Chris@16: typedef const E2 expression2_type; Chris@16: typedef typename V::size_type size_type; Chris@16: Chris@16: typename expression2_type::const_iterator2 it2 (e2 ().begin2 ()); Chris@16: typename expression2_type::const_iterator2 it2_end (e2 ().end2 ()); Chris@16: while (it2 != it2_end) { Chris@16: size_type index2 (it2.index2 ()); Chris@16: #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION Chris@16: typename expression2_type::const_iterator1 it1 (it2.begin ()); Chris@16: typename expression2_type::const_iterator1 it1_end (it2.end ()); Chris@16: #else Chris@16: typename expression2_type::const_iterator1 it1 (boost::numeric::ublas::begin (it2, iterator2_tag ())); Chris@16: typename expression2_type::const_iterator1 it1_end (boost::numeric::ublas::end (it2, iterator2_tag ())); Chris@16: #endif Chris@16: while (it1 != it1_end) { Chris@16: v (index2) += *it1 * e1 () (it1.index1 ()); Chris@16: ++ it1; Chris@16: } Chris@16: ++ it2; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: V &v, packed_random_access_iterator_tag, row_major_tag) { Chris@16: typedef const E2 expression2_type; Chris@16: typedef typename V::size_type size_type; Chris@16: Chris@16: typename expression2_type::const_iterator1 it1 (e2 ().begin1 ()); Chris@16: typename expression2_type::const_iterator1 it1_end (e2 ().end1 ()); Chris@16: while (it1 != it1_end) { Chris@16: size_type index1 (it1.index1 ()); Chris@16: #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION Chris@16: typename expression2_type::const_iterator2 it2 (it1.begin ()); Chris@16: typename expression2_type::const_iterator2 it2_end (it1.end ()); Chris@16: #else Chris@16: typename expression2_type::const_iterator2 it2 (boost::numeric::ublas::begin (it1, iterator1_tag ())); Chris@16: typename expression2_type::const_iterator2 it2_end (boost::numeric::ublas::end (it1, iterator1_tag ())); Chris@16: #endif Chris@16: while (it2 != it2_end) { Chris@16: v (it2.index2 ()) += *it2 * e1 () (index1); Chris@16: ++ it2; Chris@16: } Chris@16: ++ it1; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: V &v, sparse_bidirectional_iterator_tag) { Chris@16: typedef const E1 expression1_type; Chris@16: Chris@16: typename expression1_type::const_iterator it (e1 ().begin ()); Chris@16: typename expression1_type::const_iterator it_end (e1 ().end ()); Chris@16: while (it != it_end) { Chris@16: v.plus_assign (*it * row (e2 (), it.index ())); Chris@16: ++ it; Chris@16: } Chris@16: return v; Chris@16: } Chris@16: Chris@16: // Dispatcher Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: V &v, packed_random_access_iterator_tag) { Chris@16: typedef typename E2::orientation_category orientation_category; Chris@16: return axpy_prod (e1, e2, v, packed_random_access_iterator_tag (), orientation_category ()); Chris@16: } Chris@16: Chris@16: Chris@16: /** \brief computes v += AT x or v = AT x in an Chris@16: optimized fashion. Chris@16: Chris@16: \param e1 the vector expression \c x Chris@16: \param e2 the matrix expression \c A Chris@16: \param v the result vector \c v Chris@16: \param init a boolean parameter Chris@16: Chris@16: axpy_prod(x, A, v, init) implements the well known Chris@16: axpy-product. Setting \a init to \c true is equivalent to call Chris@16: v.clear() before axpy_prod. Currently \a init Chris@16: defaults to \c true, but this may change in the future. Chris@16: Chris@16: Up to now there are some specialisation for compressed Chris@16: matrices that give a large speed up compared to prod. Chris@16: Chris@16: \ingroup blas2 Chris@16: Chris@16: \internal Chris@16: Chris@16: template parameters: Chris@16: \param V type of the result vector \c v Chris@16: \param E1 type of a vector expression \c x Chris@16: \param E2 type of a matrix expression \c A Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V & Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: V &v, bool init = true) { Chris@16: typedef typename V::value_type value_type; Chris@16: typedef typename E1::const_iterator::iterator_category iterator_category; Chris@16: Chris@16: if (init) Chris@16: v.assign (zero_vector (e2 ().size2 ())); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: vector cv (v); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_vector_assign (cv, prod (e1, e2)); Chris@16: #endif Chris@16: axpy_prod (e1, e2, v, iterator_category ()); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits::epsilon () * verrorbound, internal_logic ()); Chris@16: #endif Chris@16: return v; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: V Chris@16: axpy_prod (const vector_expression &e1, Chris@16: const matrix_expression &e2) { Chris@16: typedef V vector_type; Chris@16: Chris@16: vector_type v (e2 ().size2 ()); Chris@16: return axpy_prod (e1, e2, v, true); Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, TRI, Chris@16: dense_proxy_tag, row_major_tag) { Chris@101: Chris@16: typedef typename M::size_type size_type; Chris@16: Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@101: typedef typename M::value_type value_type; Chris@16: matrix cm (m); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_matrix_assign (cm, prod (e1, e2), row_major_tag ()); Chris@16: #endif Chris@16: size_type size1 (e1 ().size1 ()); Chris@16: size_type size2 (e1 ().size2 ()); Chris@16: for (size_type i = 0; i < size1; ++ i) Chris@16: for (size_type j = 0; j < size2; ++ j) Chris@16: row (m, i).plus_assign (e1 () (i, j) * row (e2 (), j)); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits::epsilon () * merrorbound, internal_logic ()); Chris@16: #endif Chris@16: return m; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, TRI, Chris@16: sparse_proxy_tag, row_major_tag) { Chris@101: Chris@16: typedef TRI triangular_restriction; Chris@16: typedef const E1 expression1_type; Chris@16: typedef const E2 expression2_type; Chris@16: Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@101: typedef typename M::value_type value_type; Chris@16: matrix cm (m); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_matrix_assign (cm, prod (e1, e2), row_major_tag ()); Chris@16: #endif Chris@16: typename expression1_type::const_iterator1 it1 (e1 ().begin1 ()); Chris@16: typename expression1_type::const_iterator1 it1_end (e1 ().end1 ()); Chris@16: while (it1 != it1_end) { Chris@16: #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION Chris@16: typename expression1_type::const_iterator2 it2 (it1.begin ()); Chris@16: typename expression1_type::const_iterator2 it2_end (it1.end ()); Chris@16: #else Chris@16: typename expression1_type::const_iterator2 it2 (boost::numeric::ublas::begin (it1, iterator1_tag ())); Chris@16: typename expression1_type::const_iterator2 it2_end (boost::numeric::ublas::end (it1, iterator1_tag ())); Chris@16: #endif Chris@16: while (it2 != it2_end) { Chris@16: // row (m, it1.index1 ()).plus_assign (*it2 * row (e2 (), it2.index2 ())); Chris@16: matrix_row mr (e2 (), it2.index2 ()); Chris@16: typename matrix_row::const_iterator itr (mr.begin ()); Chris@16: typename matrix_row::const_iterator itr_end (mr.end ()); Chris@16: while (itr != itr_end) { Chris@16: if (triangular_restriction::other (it1.index1 (), itr.index ())) Chris@16: m (it1.index1 (), itr.index ()) += *it2 * *itr; Chris@16: ++ itr; Chris@16: } Chris@16: ++ it2; Chris@16: } Chris@16: ++ it1; Chris@16: } Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits::epsilon () * merrorbound, internal_logic ()); Chris@16: #endif Chris@16: return m; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, TRI, Chris@16: dense_proxy_tag, column_major_tag) { Chris@16: typedef typename M::size_type size_type; Chris@16: Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@101: typedef typename M::value_type value_type; Chris@16: matrix cm (m); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_matrix_assign (cm, prod (e1, e2), column_major_tag ()); Chris@16: #endif Chris@16: size_type size1 (e2 ().size1 ()); Chris@16: size_type size2 (e2 ().size2 ()); Chris@16: for (size_type j = 0; j < size2; ++ j) Chris@16: for (size_type i = 0; i < size1; ++ i) Chris@16: column (m, j).plus_assign (e2 () (i, j) * column (e1 (), i)); Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits::epsilon () * merrorbound, internal_logic ()); Chris@16: #endif Chris@16: return m; Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, TRI, Chris@16: sparse_proxy_tag, column_major_tag) { Chris@16: typedef TRI triangular_restriction; Chris@16: typedef const E1 expression1_type; Chris@16: typedef const E2 expression2_type; Chris@101: Chris@16: Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@101: typedef typename M::value_type value_type; Chris@16: matrix cm (m); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_matrix_assign (cm, prod (e1, e2), column_major_tag ()); Chris@16: #endif Chris@16: typename expression2_type::const_iterator2 it2 (e2 ().begin2 ()); Chris@16: typename expression2_type::const_iterator2 it2_end (e2 ().end2 ()); Chris@16: while (it2 != it2_end) { Chris@16: #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION Chris@16: typename expression2_type::const_iterator1 it1 (it2.begin ()); Chris@16: typename expression2_type::const_iterator1 it1_end (it2.end ()); Chris@16: #else Chris@16: typename expression2_type::const_iterator1 it1 (boost::numeric::ublas::begin (it2, iterator2_tag ())); Chris@16: typename expression2_type::const_iterator1 it1_end (boost::numeric::ublas::end (it2, iterator2_tag ())); Chris@16: #endif Chris@16: while (it1 != it1_end) { Chris@16: // column (m, it2.index2 ()).plus_assign (*it1 * column (e1 (), it1.index1 ())); Chris@16: matrix_column mc (e1 (), it1.index1 ()); Chris@16: typename matrix_column::const_iterator itc (mc.begin ()); Chris@16: typename matrix_column::const_iterator itc_end (mc.end ()); Chris@16: while (itc != itc_end) { Chris@16: if(triangular_restriction::other (itc.index (), it2.index2 ())) Chris@16: m (itc.index (), it2.index2 ()) += *it1 * *itc; Chris@16: ++ itc; Chris@16: } Chris@16: ++ it1; Chris@16: } Chris@16: ++ it2; Chris@16: } Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits::epsilon () * merrorbound, internal_logic ()); Chris@16: #endif Chris@16: return m; Chris@16: } Chris@16: Chris@16: // Dispatcher Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, TRI, bool init = true) { Chris@16: typedef typename M::value_type value_type; Chris@16: typedef typename M::storage_category storage_category; Chris@16: typedef typename M::orientation_category orientation_category; Chris@16: typedef TRI triangular_restriction; Chris@16: Chris@16: if (init) Chris@16: m.assign (zero_matrix (e1 ().size1 (), e2 ().size2 ())); Chris@16: return axpy_prod (e1, e2, m, triangular_restriction (), storage_category (), orientation_category ()); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: TRI) { Chris@16: typedef M matrix_type; Chris@16: typedef TRI triangular_restriction; Chris@16: Chris@16: matrix_type m (e1 ().size1 (), e2 ().size2 ()); Chris@16: return axpy_prod (e1, e2, m, triangular_restriction (), true); Chris@16: } Chris@16: Chris@16: /** \brief computes M += A X or M = A X in an Chris@16: optimized fashion. Chris@16: Chris@16: \param e1 the matrix expression \c A Chris@16: \param e2 the matrix expression \c X Chris@16: \param m the result matrix \c M Chris@16: \param init a boolean parameter Chris@16: Chris@16: axpy_prod(A, X, M, init) implements the well known Chris@16: axpy-product. Setting \a init to \c true is equivalent to call Chris@16: M.clear() before axpy_prod. Currently \a init Chris@16: defaults to \c true, but this may change in the future. Chris@16: Chris@16: Up to now there are no specialisations. Chris@16: Chris@16: \ingroup blas3 Chris@16: Chris@16: \internal Chris@16: Chris@16: template parameters: Chris@16: \param M type of the result matrix \c M Chris@16: \param E1 type of a matrix expression \c A Chris@16: \param E2 type of a matrix expression \c X Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, bool init = true) { Chris@16: typedef typename M::value_type value_type; Chris@16: typedef typename M::storage_category storage_category; Chris@16: typedef typename M::orientation_category orientation_category; Chris@16: Chris@16: if (init) Chris@16: m.assign (zero_matrix (e1 ().size1 (), e2 ().size2 ())); Chris@16: return axpy_prod (e1, e2, m, full (), storage_category (), orientation_category ()); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M Chris@16: axpy_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2) { Chris@16: typedef M matrix_type; Chris@16: Chris@16: matrix_type m (e1 ().size1 (), e2 ().size2 ()); Chris@16: return axpy_prod (e1, e2, m, full (), true); Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: opb_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, Chris@16: dense_proxy_tag, row_major_tag) { Chris@16: typedef typename M::size_type size_type; Chris@16: typedef typename M::value_type value_type; Chris@16: Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: matrix cm (m); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_matrix_assign (cm, prod (e1, e2), row_major_tag ()); Chris@16: #endif Chris@16: size_type size (BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size1 ())); Chris@16: for (size_type k = 0; k < size; ++ k) { Chris@16: vector ce1 (column (e1 (), k)); Chris@16: vector re2 (row (e2 (), k)); Chris@16: m.plus_assign (outer_prod (ce1, re2)); Chris@16: } Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits::epsilon () * merrorbound, internal_logic ()); Chris@16: #endif Chris@16: return m; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: opb_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, Chris@16: dense_proxy_tag, column_major_tag) { Chris@16: typedef typename M::size_type size_type; Chris@16: typedef typename M::value_type value_type; Chris@16: Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: matrix cm (m); Chris@16: typedef typename type_traits::real_type real_type; Chris@16: real_type merrorbound (norm_1 (m) + norm_1 (e1) * norm_1 (e2)); Chris@16: indexing_matrix_assign (cm, prod (e1, e2), column_major_tag ()); Chris@16: #endif Chris@16: size_type size (BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size1 ())); Chris@16: for (size_type k = 0; k < size; ++ k) { Chris@16: vector ce1 (column (e1 (), k)); Chris@16: vector re2 (row (e2 (), k)); Chris@16: m.plus_assign (outer_prod (ce1, re2)); Chris@16: } Chris@16: #if BOOST_UBLAS_TYPE_CHECK Chris@16: BOOST_UBLAS_CHECK (norm_1 (m - cm) <= 2 * std::numeric_limits::epsilon () * merrorbound, internal_logic ()); Chris@16: #endif Chris@16: return m; Chris@16: } Chris@16: Chris@16: // Dispatcher Chris@16: Chris@16: /** \brief computes M += A X or M = A X in an Chris@16: optimized fashion. Chris@16: Chris@16: \param e1 the matrix expression \c A Chris@16: \param e2 the matrix expression \c X Chris@16: \param m the result matrix \c M Chris@16: \param init a boolean parameter Chris@16: Chris@16: opb_prod(A, X, M, init) implements the well known Chris@16: axpy-product. Setting \a init to \c true is equivalent to call Chris@16: M.clear() before opb_prod. Currently \a init Chris@16: defaults to \c true, but this may change in the future. Chris@16: Chris@16: This function may give a speedup if \c A has less columns than Chris@16: rows, because the product is computed as a sum of outer Chris@16: products. Chris@16: Chris@16: \ingroup blas3 Chris@16: Chris@16: \internal Chris@16: Chris@16: template parameters: Chris@16: \param M type of the result matrix \c M Chris@16: \param E1 type of a matrix expression \c A Chris@16: \param E2 type of a matrix expression \c X Chris@16: */ Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M & Chris@16: opb_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2, Chris@16: M &m, bool init = true) { Chris@16: typedef typename M::value_type value_type; Chris@16: typedef typename M::storage_category storage_category; Chris@16: typedef typename M::orientation_category orientation_category; Chris@16: Chris@16: if (init) Chris@16: m.assign (zero_matrix (e1 ().size1 (), e2 ().size2 ())); Chris@16: return opb_prod (e1, e2, m, storage_category (), orientation_category ()); Chris@16: } Chris@16: template Chris@16: BOOST_UBLAS_INLINE Chris@16: M Chris@16: opb_prod (const matrix_expression &e1, Chris@16: const matrix_expression &e2) { Chris@16: typedef M matrix_type; Chris@16: Chris@16: matrix_type m (e1 ().size1 (), e2 ().size2 ()); Chris@16: return opb_prod (e1, e2, m, true); Chris@16: } Chris@16: Chris@16: }}} Chris@16: Chris@16: #endif