operation.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 852 行 · 第 1/3 页
HPP
852 行
//// Copyright (c) 2000-2002// Joerg Walter, Mathias Koch//// Distributed under the Boost Software License, Version 1.0. (See// accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)//// The authors gratefully acknowledge the support of// GeNeSys mbH & Co. KG in producing this work.//#ifndef _BOOST_UBLAS_OPERATION_#define _BOOST_UBLAS_OPERATION_#include <boost/numeric/ublas/matrix_proxy.hpp>/** \file operation.hpp * \brief This file contains some specialized products. */// axpy-based products// Alexei Novakov had a lot of ideas to improve these. Thanks.// Hendrik Kueck proposed some new kernel. Thanks again.namespace boost { namespace numeric { namespace ublas { template<class V, class T1, class L1, class IA1, class TA1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const compressed_matrix<T1, L1, 0, IA1, TA1> &e1, const vector_expression<E2> &e2, V &v, row_major_tag) { typedef typename V::size_type size_type; typedef typename V::value_type value_type; for (size_type i = 0; i < e1.filled1 () -1; ++ i) { size_type begin = e1.index1_data () [i]; size_type end = e1.index1_data () [i + 1]; value_type t (v (i)); for (size_type j = begin; j < end; ++ j) t += e1.value_data () [j] * e2 () (e1.index2_data () [j]); v (i) = t; } return v; } template<class V, class T1, class L1, class IA1, class TA1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const compressed_matrix<T1, L1, 0, IA1, TA1> &e1, const vector_expression<E2> &e2, V &v, column_major_tag) { typedef typename V::size_type size_type; for (size_type j = 0; j < e1.filled1 () -1; ++ j) { size_type begin = e1.index1_data () [j]; size_type end = e1.index1_data () [j + 1]; for (size_type i = begin; i < end; ++ i) v (e1.index2_data () [i]) += e1.value_data () [i] * e2 () (j); } return v; } // Dispatcher template<class V, class T1, class L1, class IA1, class TA1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const compressed_matrix<T1, L1, 0, IA1, TA1> &e1, const vector_expression<E2> &e2, V &v, bool init = true) { typedef typename V::value_type value_type; typedef typename L1::orientation_category orientation_category; if (init) v.assign (zero_vector<value_type> (e1.size1 ()));#if BOOST_UBLAS_TYPE_CHECK vector<value_type> cv (v); typedef typename type_traits<value_type>::real_type real_type; real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2)); indexing_vector_assign<scalar_plus_assign> (cv, prod (e1, e2));#endif axpy_prod (e1, e2, v, orientation_category ());#if BOOST_UBLAS_TYPE_CHECK BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits<real_type>::epsilon () * verrorbound, internal_logic ());#endif return v; } template<class V, class T1, class L1, class IA1, class TA1, class E2> BOOST_UBLAS_INLINE V axpy_prod (const compressed_matrix<T1, L1, 0, IA1, TA1> &e1, const vector_expression<E2> &e2) { typedef V vector_type; vector_type v (e1.size1 ()); return axpy_prod (e1, e2, v, true); } template<class V, class T1, class L1, class IA1, class TA1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const coordinate_matrix<T1, L1, 0, IA1, TA1> &e1, const vector_expression<E2> &e2, V &v, bool init = true) { typedef typename V::size_type size_type; typedef typename V::value_type value_type; typedef L1 layout_type; size_type size1 = e1.size1(); size_type size2 = e1.size2(); if (init) { noalias(v) = zero_vector<value_type>(size1); } for (size_type i = 0; i < e1.nnz(); ++i) { size_type row_index = layout_type::index_M( e1.index1_data () [i], e1.index2_data () [i] ); size_type col_index = layout_type::index_m( e1.index1_data () [i], e1.index2_data () [i] ); v( row_index ) += e1.value_data () [i] * e2 () (col_index); } return v; } template<class V, class E1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const matrix_expression<E1> &e1, const vector_expression<E2> &e2, V &v, packed_random_access_iterator_tag, row_major_tag) { typedef const E1 expression1_type; typedef const E2 expression2_type; typedef typename V::size_type size_type; typename expression1_type::const_iterator1 it1 (e1 ().begin1 ()); typename expression1_type::const_iterator1 it1_end (e1 ().end1 ()); while (it1 != it1_end) { size_type index1 (it1.index1 ());#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION typename expression1_type::const_iterator2 it2 (it1.begin ()); typename expression1_type::const_iterator2 it2_end (it1.end ());#else typename expression1_type::const_iterator2 it2 (boost::numeric::ublas::begin (it1, iterator1_tag ())); typename expression1_type::const_iterator2 it2_end (boost::numeric::ublas::end (it1, iterator1_tag ()));#endif while (it2 != it2_end) { v (index1) += *it2 * e2 () (it2.index2 ()); ++ it2; } ++ it1; } return v; } template<class V, class E1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const matrix_expression<E1> &e1, const vector_expression<E2> &e2, V &v, packed_random_access_iterator_tag, column_major_tag) { typedef const E1 expression1_type; typedef const E2 expression2_type; typedef typename V::size_type size_type; typename expression1_type::const_iterator2 it2 (e1 ().begin2 ()); typename expression1_type::const_iterator2 it2_end (e1 ().end2 ()); while (it2 != it2_end) { size_type index2 (it2.index2 ());#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION typename expression1_type::const_iterator1 it1 (it2.begin ()); typename expression1_type::const_iterator1 it1_end (it2.end ());#else typename expression1_type::const_iterator1 it1 (boost::numeric::ublas::begin (it2, iterator2_tag ())); typename expression1_type::const_iterator1 it1_end (boost::numeric::ublas::end (it2, iterator2_tag ()));#endif while (it1 != it1_end) { v (it1.index1 ()) += *it1 * e2 () (index2); ++ it1; } ++ it2; } return v; } template<class V, class E1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const matrix_expression<E1> &e1, const vector_expression<E2> &e2, V &v, sparse_bidirectional_iterator_tag) { typedef const E1 expression1_type; typedef const E2 expression2_type; typedef typename V::size_type size_type; typename expression2_type::const_iterator it (e2 ().begin ()); typename expression2_type::const_iterator it_end (e2 ().end ()); while (it != it_end) { v.plus_assign (column (e1 (), it.index ()) * *it); ++ it; } return v; } // Dispatcher template<class V, class E1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const matrix_expression<E1> &e1, const vector_expression<E2> &e2, V &v, packed_random_access_iterator_tag) { typedef typename E1::orientation_category orientation_category; return axpy_prod (e1, e2, v, packed_random_access_iterator_tag (), orientation_category ()); } /** \brief computes <tt>v += A x</tt> or <tt>v = A x</tt> in an optimized fashion. \param e1 the matrix expression \c A \param e2 the vector expression \c x \param v the result vector \c v \param init a boolean parameter <tt>axpy_prod(A, x, v, init)</tt> implements the well known axpy-product. Setting \a init to \c true is equivalent to call <tt>v.clear()</tt> before <tt>axpy_prod</tt>. Currently \a init defaults to \c true, but this may change in the future. Up to now there are some specialisation for compressed matrices that give a large speed up compared to prod. \ingroup blas2 \internal template parameters: \param V type of the result vector \c v \param E1 type of a matrix expression \c A \param E2 type of a vector expression \c x */ template<class V, class E1, class E2> BOOST_UBLAS_INLINE V & axpy_prod (const matrix_expression<E1> &e1, const vector_expression<E2> &e2, V &v, bool init = true) { typedef typename V::value_type value_type; typedef typename E2::const_iterator::iterator_category iterator_category; if (init) v.assign (zero_vector<value_type> (e1 ().size1 ()));#if BOOST_UBLAS_TYPE_CHECK vector<value_type> cv (v); typedef typename type_traits<value_type>::real_type real_type; real_type verrorbound (norm_1 (v) + norm_1 (e1) * norm_1 (e2)); indexing_vector_assign<scalar_plus_assign> (cv, prod (e1, e2));#endif axpy_prod (e1, e2, v, iterator_category ());#if BOOST_UBLAS_TYPE_CHECK BOOST_UBLAS_CHECK (norm_1 (v - cv) <= 2 * std::numeric_limits<real_type>::epsilon () * verrorbound, internal_logic ());#endif return v; } template<class V, class E1, class E2> BOOST_UBLAS_INLINE V axpy_prod (const matrix_expression<E1> &e1, const vector_expression<E2> &e2) { typedef V vector_type; vector_type v (e1 ().size1 ()); return axpy_prod (e1, e2, v, true); } template<class V, class E1, class T2, class IA2, class TA2> BOOST_UBLAS_INLINE V & axpy_prod (const vector_expression<E1> &e1, const compressed_matrix<T2, column_major, 0, IA2, TA2> &e2, V &v, column_major_tag) { typedef typename V::size_type size_type; typedef typename V::value_type value_type; for (size_type j = 0; j < e2.filled1 () -1; ++ j) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?