📄 hermitian.hpp
字号:
// FIXME use matrix_resize_preserve on conformant compilers // detail::matrix_resize_preserve<functor_type> (*this, temporary, size_, size_); assign_temporary (temporary); } else data ().resize (functor1_type::packed_size (size_, size_)); } BOOST_UBLAS_INLINE void resize (size_type size1, size_type size2, bool preserve = true) { resize (BOOST_UBLAS_SAME (size1, size2), preserve); } BOOST_UBLAS_INLINE void resize_packed_preserve (size_type size) { size_ = BOOST_UBLAS_SAME (size, size); data ().resize (functor1_type::packed_size (size_, size_), value_type (0)); } // Element access BOOST_UBLAS_INLINE const_reference at_element (size_type i, size_type j) const { BOOST_UBLAS_CHECK (i < size_, bad_index ()); BOOST_UBLAS_CHECK (j < size_, bad_index ()); // if (i == j) // return type_traits<value_type>::real (data () [functor1_type::element (functor2_type (), i, size_, i, size_)]); // else if (functor1_type::other (i, j)) return data () [functor1_type::element (functor2_type (), i, size_, j, size_)]; else return type_traits<value_type>::conj (data () [functor1_type::element (functor2_type (), j, size_, i, size_)]); } BOOST_UBLAS_INLINE true_reference at_element (size_type i, size_type j) { BOOST_UBLAS_CHECK (i < size_, bad_index ()); BOOST_UBLAS_CHECK (j < size_, bad_index ()); if (functor1_type::other (i, j)) return data () [functor1_type::element (functor2_type (), i, size_, j, size_)]; else { external_logic ().raise (); return conj_ = type_traits<value_type>::conj (data () [functor1_type::element (functor2_type (), j, size_, i, size_)]); } } BOOST_UBLAS_INLINE void at (size_type i, size_type j, value_type t) { BOOST_UBLAS_CHECK (i < size_, bad_index ()); BOOST_UBLAS_CHECK (j < size_, bad_index ()); // if (i == j) // data () [functor1_type::element (functor2_type (), i, size_, i, size_)] = type_traits<value_type>::real (t); // else if (functor1_type::other (i, j)) data () [functor1_type::element (functor2_type (), i, size_, j, size_)] = t; else data () [functor1_type::element (functor2_type (), j, size_, i, size_)] = type_traits<value_type>::conj (t); } BOOST_UBLAS_INLINE const_reference operator () (size_type i, size_type j) const { return at_element (i, j); } BOOST_UBLAS_INLINE reference operator () (size_type i, size_type j) {#ifndef BOOST_UBLAS_STRICT_MATRIX_SPARSE return at_element (i, j);#else if (functor1_type::other (i, j)) return reference (*this, i, j, data () [functor1_type::element (functor2_type (), i, size_, j, size_)]); else return reference (*this, i, j, type_traits<value_type>::conj (data () [functor1_type::element (functor2_type (), j, size_, i, size_)]));#endif } // Assignment BOOST_UBLAS_INLINE hermitian_matrix &operator = (const hermitian_matrix &m) { size_ = m.size_; data () = m.data (); return *this; } BOOST_UBLAS_INLINE hermitian_matrix &assign_temporary (hermitian_matrix &m) { swap (m); return *this; } template<class AE> BOOST_UBLAS_INLINE hermitian_matrix &operator = (const matrix_expression<AE> &ae) { // return assign (self_type (ae)); self_type temporary (ae); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE hermitian_matrix &assign (const matrix_expression<AE> &ae) { matrix_assign (scalar_assign<true_reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae); return *this; } template<class AE> BOOST_UBLAS_INLINE hermitian_matrix& operator += (const matrix_expression<AE> &ae) { // return assign (self_type (*this + ae)); self_type temporary (*this + ae); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE hermitian_matrix &plus_assign (const matrix_expression<AE> &ae) { matrix_assign (scalar_plus_assign<true_reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae); return *this; } template<class AE> BOOST_UBLAS_INLINE hermitian_matrix& operator -= (const matrix_expression<AE> &ae) { // return assign (self_type (*this - ae)); self_type temporary (*this - ae); return assign_temporary (temporary); } template<class AE> BOOST_UBLAS_INLINE hermitian_matrix &minus_assign (const matrix_expression<AE> &ae) { matrix_assign (scalar_minus_assign<true_reference, BOOST_UBLAS_TYPENAME AE::value_type> (), *this, ae); return *this; } template<class AT> BOOST_UBLAS_INLINE hermitian_matrix& operator *= (const AT &at) { // Multiplication is only allowed for real scalars, // otherwise the resulting matrix isn't hermitian. // Thanks to Peter Schmitteckert for spotting this. BOOST_UBLAS_CHECK (type_traits<value_type>::imag (at) == 0, non_real ()); matrix_assign_scalar (scalar_multiplies_assign<true_reference, AT> (), *this, at); return *this; } template<class AT> BOOST_UBLAS_INLINE hermitian_matrix& operator /= (const AT &at) { // Multiplication is only allowed for real scalars, // otherwise the resulting matrix isn't hermitian. // Thanks to Peter Schmitteckert for spotting this. BOOST_UBLAS_CHECK (type_traits<value_type>::imag (at) == 0, non_real ()); matrix_assign_scalar (scalar_divides_assign<true_reference, AT> (), *this, at); return *this; } // Swapping BOOST_UBLAS_INLINE void swap (hermitian_matrix &m) { if (this != &m) { std::swap (size_, m.size_); data ().swap (m.data ()); } }#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS BOOST_UBLAS_INLINE friend void swap (hermitian_matrix &m1, hermitian_matrix &m2) { m1.swap (m2); }#endif // Element insertion and erasure // These functions should work with std::vector. // Thanks to Kresimir Fresl for spotting this. BOOST_UBLAS_INLINE void insert (size_type i, size_type j, const_reference t) { BOOST_UBLAS_CHECK (i < size_, bad_index ()); BOOST_UBLAS_CHECK (j < size_, bad_index ()); if (functor1_type::other (i, j)) { size_type k = functor1_type::element (functor2_type (), i, size_, j, size_); BOOST_UBLAS_CHECK (type_traits<value_type>::equals (data () [k], value_type (0)) || type_traits<value_type>::equals (data () [k], t), bad_index ()); // data ().insert (data ().begin () + k, t); data () [k] = t; } else { size_type k = functor1_type::element (functor2_type (), j, size_, i, size_); BOOST_UBLAS_CHECK (type_traits<value_type>::equals (data () [k], value_type (0)) || type_traits<value_type>::equals (data () [k], type_traits<value_type>::conj (t)), bad_index ()); // data ().insert (data ().begin () + k, type_traits<value_type>::conj (t)); data () [k] = type_traits<value_type>::conj (t); } } BOOST_UBLAS_INLINE void erase (size_type i, size_type j) { BOOST_UBLAS_CHECK (i < size_, bad_index ()); BOOST_UBLAS_CHECK (j < size_, bad_index ()); if (functor1_type::other (i, j)) { size_type k = functor1_type::element (functor2_type (), i, size_, j, size_); // data ().erase (data ().begin () + k); data () [k] = value_type (0); } else { size_type k = functor1_type::element (functor2_type (), j, size_, i, size_); // data ().erase (data ().begin () + k); data () [k] = value_type (0); } } BOOST_UBLAS_INLINE void clear () { // data ().clear (); std::fill (data ().begin (), data ().end (), value_type (0)); } // Iterator types#ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1; typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2; typedef indexed_const_iterator1<self_type, packed_random_access_iterator_tag> const_iterator1; typedef indexed_const_iterator2<self_type, packed_random_access_iterator_tag> const_iterator2;#else class const_iterator1; class iterator1; class const_iterator2; class iterator2;#endif#ifdef BOOST_MSVC_STD_ITERATOR typedef reverse_iterator_base1<const_iterator1, value_type, const_reference> const_reverse_iterator1; typedef reverse_iterator_base1<iterator1, value_type, reference> reverse_iterator1; typedef reverse_iterator_base2<const_iterator2, value_type, const_reference> const_reverse_iterator2; typedef reverse_iterator_base2<iterator2, value_type, reference> reverse_iterator2;#else typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1; typedef reverse_iterator_base1<iterator1> reverse_iterator1; typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2; typedef reverse_iterator_base2<iterator2> reverse_iterator2;#endif // Element lookup BOOST_UBLAS_INLINE const_iterator1 find1 (int /* rank */, size_type i, size_type j) const { return const_iterator1 (*this, i, j); } BOOST_UBLAS_INLINE iterator1 find1 (int rank, size_type i, size_type j) { if (rank == 1) i = functor1_type::restrict1 (i, j); return iterator1 (*this, i, j); } BOOST_UBLAS_INLINE const_iterator2 find2 (int /* rank */, size_type i, size_type j) const { return const_iterator2 (*this, i, j); } BOOST_UBLAS_INLINE iterator2 find2 (int rank, size_type i, size_type j) { if (rank == 1) j = functor1_type::restrict2 (i, j); return iterator2 (*this, i, j); } // Iterators simply are indices.#ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR class const_iterator1: public container_const_reference<hermitian_matrix>, public random_access_iterator_base<packed_random_access_iterator_tag, const_iterator1, value_type> { public: typedef packed_random_access_iterator_tag iterator_category;#ifdef BOOST_MSVC_STD_ITERATOR typedef const_reference reference;#else typedef typename hermitian_matrix::value_type value_type; typedef typename hermitian_matrix::difference_type difference_type; typedef typename hermitian_matrix::const_reference reference; typedef const typename hermitian_matrix::pointer pointer;#endif typedef const_iterator2 dual_iterator_type; typedef const_reverse_iterator2 dual_reverse_iterator_type; // Construction and destruction BOOST_UBLAS_INLINE const_iterator1 (): container_const_reference<self_type> (), it1_ (), it2_ () {} BOOST_UBLAS_INLINE const_iterator1 (const self_type &m, size_type it1, size_type it2): container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {} BOOST_UBLAS_INLINE const_iterator1 (const iterator1 &it): container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {} // Arithmetic BOOST_UBLAS_INLINE const_iterator1 &operator ++ () { ++ it1_; return *this; } BOOST_UBLAS_INLINE const_iterator1 &operator -- () { -- it1_; return *this; } BOOST_UBLAS_INLINE const_iterator1 &operator += (difference_type n) { it1_ += n; return *this; } BOOST_UBLAS_INLINE const_iterator1 &operator -= (difference_type n) { it1_ -= n; return *this; } BOOST_UBLAS_INLINE difference_type operator - (const const_iterator1 &it) const { BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ()); BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ()); return it1_ - it.it1_; } // Dereference BOOST_UBLAS_INLINE const_reference operator * () const { return (*this) ().at_element (it1_, it2_); }#ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION BOOST_UBLAS_INLINE#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION typename self_type::#endif const_iterator2 begin () const { return (*this) ().find2 (1, it1_, 0); } BOOST_UBLAS_INLINE#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION typename self_type::#endif const_iterator2 end () const { return (*this) ().find2 (1, it1_, (*this) ().size2 ()); } BOOST_UBLAS_INLINE#ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION typename self_type::#endif const_reverse_iterator2 rbegin () const { return const_reverse_iterator2 (end ());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -