📄 vector_sparse.hpp
字号:
//
// Copyright (c) 2000-2002
// Joerg Walter, Mathias Koch
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation. The authors make no representations
// about the suitability of this software for any purpose.
// It is provided "as is" without express or implied warranty.
//
// The authors gratefully acknowledge the support of
// GeNeSys mbH & Co. KG in producing this work.
//
#ifndef BOOST_UBLAS_VECTOR_SPARSE_H
#define BOOST_UBLAS_VECTOR_SPARSE_H
#include <boost/numeric/ublas/config.hpp>
#include <boost/numeric/ublas/storage_sparse.hpp>
#include <boost/numeric/ublas/vector.hpp>
// Iterators based on ideas of Jeremy Siek
namespace boost { namespace numeric { namespace ublas {
#ifdef BOOST_UBLAS_STRICT_VECTOR_SPARSE
template<class V>
class sparse_vector_element:
public container_reference<V> {
public:
typedef V vector_type;
typedef typename V::size_type size_type;
typedef typename V::value_type value_type;
// typedef const value_type &const_reference;
typedef typename type_traits<value_type>::const_reference const_reference;
typedef value_type &reference;
typedef value_type *pointer;
// Construction and destruction
BOOST_UBLAS_INLINE
sparse_vector_element (const value_type &d):
container_reference<vector_type> (), it_ (), i_ (), d_ (d), dirty_ (false) {
external_logic ().raise ();
}
BOOST_UBLAS_INLINE
sparse_vector_element (vector_type &v, pointer it, size_type i):
container_reference<vector_type> (v), it_ (it), i_ (i), d_ (*it), dirty_ (false) {}
BOOST_UBLAS_INLINE
sparse_vector_element (vector_type &v, size_type i):
container_reference<vector_type> (v), it_ (), i_ (i), d_ (), dirty_ (false) {
pointer it = (*this) ().find_element (i_);
if (it)
d_ = *it;
}
BOOST_UBLAS_INLINE
sparse_vector_element (const sparse_vector_element &p):
container_reference<vector_type> (p), it_ (p.it_), i_ (p.i_), d_ (p.d_), dirty_ (p.dirty_) {}
BOOST_UBLAS_INLINE
~sparse_vector_element () {
if (dirty_) {
if (! it_) {
it_ = (*this) ().find_element (i_);
if (! it_)
(*this) ().insert (i_, d_);
else
*it_ = d_;
} else
*it_ = d_;
}
}
// Assignment
template<class D>
BOOST_UBLAS_INLINE
sparse_vector_element &operator = (const D &d) {
d_ = d;
dirty_ = true;
return *this;
}
BOOST_UBLAS_INLINE
sparse_vector_element &operator = (const sparse_vector_element &p) {
d_ = p.d_;
dirty_ = true;
return *this;
}
template<class OV>
BOOST_UBLAS_INLINE
sparse_vector_element &operator = (const sparse_vector_element<OV> &p) {
d_ = p.d_;
dirty_ = true;
return *this;
}
template<class D>
BOOST_UBLAS_INLINE
sparse_vector_element &operator += (const D &d) {
d_ += d;
dirty_ = true;
return *this;
}
BOOST_UBLAS_INLINE
sparse_vector_element &operator += (const sparse_vector_element &p) {
d_ += p.d_;
dirty_ = true;
return *this;
}
template<class OV>
BOOST_UBLAS_INLINE
sparse_vector_element &operator += (const sparse_vector_element<OV> &p) {
d_ += p.d_;
dirty_ = true;
return *this;
}
template<class D>
BOOST_UBLAS_INLINE
sparse_vector_element &operator -= (const D &d) {
d_ -= d;
dirty_ = true;
return *this;
}
BOOST_UBLAS_INLINE
sparse_vector_element &operator -= (const sparse_vector_element &p) {
d_ -= p.d_;
dirty_ = true;
return *this;
}
template<class OV>
BOOST_UBLAS_INLINE
sparse_vector_element &operator -= (const sparse_vector_element<OV> &p) {
d_ -= p.d_;
dirty_ = true;
return *this;
}
template<class D>
BOOST_UBLAS_INLINE
sparse_vector_element &operator *= (const D &d) {
d_ *= d;
dirty_ = true;
return *this;
}
BOOST_UBLAS_INLINE
sparse_vector_element &operator *= (const sparse_vector_element &p) {
d_ *= p.d_;
dirty_ = true;
return *this;
}
template<class OV>
BOOST_UBLAS_INLINE
sparse_vector_element &operator *= (const sparse_vector_element<OV> &p) {
d_ *= p.d_;
dirty_ = true;
return *this;
}
template<class D>
BOOST_UBLAS_INLINE
sparse_vector_element &operator /= (const D &d) {
d_ /= d;
dirty_ = true;
return *this;
}
BOOST_UBLAS_INLINE
sparse_vector_element &operator /= (const sparse_vector_element &p) {
d_ /= p.d_;
dirty_ = true;
return *this;
}
template<class OV>
BOOST_UBLAS_INLINE
sparse_vector_element &operator /= (const sparse_vector_element<OV> &p) {
d_ /= p.d_;
dirty_ = true;
return *this;
}
// Conversion
BOOST_UBLAS_INLINE
operator const_reference () const {
return d_;
}
#ifdef BOOST_UBLAS_DEPRECATED
BOOST_UBLAS_INLINE
operator reference () {
dirty_ = true;
return d_;
}
#endif
// Swapping
BOOST_UBLAS_INLINE
void swap (sparse_vector_element p) {
if (this != &p) {
dirty_ = true;
p.dirty_ = true;
std::swap (d_, p.d_);
}
}
#ifndef BOOST_UBLAS_NO_MEMBER_FRIENDS
BOOST_UBLAS_INLINE
friend void swap (sparse_vector_element p1, sparse_vector_element p2) {
p1.swap (p2);
}
#endif
private:
pointer it_;
size_type i_;
value_type d_;
bool dirty_;
};
template<class V>
struct type_traits<sparse_vector_element<V> > {
typedef typename V::value_type element_type;
typedef type_traits<sparse_vector_element<V> > self_type;
typedef typename type_traits<element_type>::value_type value_type;
typedef typename type_traits<element_type>::const_reference const_reference;
typedef sparse_vector_element<V> reference;
typedef typename type_traits<element_type>::real_type real_type;
typedef typename type_traits<element_type>::precision_type precision_type;
BOOST_STATIC_CONSTANT (std::size_t, plus_complexity = type_traits<element_type>::plus_complexity);
BOOST_STATIC_CONSTANT (std::size_t, multiplies_complexity = type_traits<element_type>::multiplies_complexity);
static
BOOST_UBLAS_INLINE
real_type real (const_reference t) {
return type_traits<element_type>::real (t);
}
static
BOOST_UBLAS_INLINE
real_type imag (const_reference t) {
return type_traits<element_type>::imag (t);
}
static
BOOST_UBLAS_INLINE
value_type conj (const_reference t) {
return type_traits<element_type>::conj (t);
}
static
BOOST_UBLAS_INLINE
real_type abs (const_reference t) {
return type_traits<element_type>::abs (t);
}
static
BOOST_UBLAS_INLINE
value_type sqrt (const_reference t) {
return type_traits<element_type>::sqrt (t);
}
static
BOOST_UBLAS_INLINE
real_type norm_1 (const_reference t) {
return type_traits<element_type>::norm_1 (t);
}
static
BOOST_UBLAS_INLINE
real_type norm_2 (const_reference t) {
return type_traits<element_type>::norm_2 (t);
}
static
BOOST_UBLAS_INLINE
real_type norm_inf (const_reference t) {
return type_traits<element_type>::norm_inf (t);
}
static
BOOST_UBLAS_INLINE
bool equals (const_reference t1, const_reference t2) {
return type_traits<element_type>::equals (t1, t2);
}
};
template<class V1, class T2>
struct promote_traits<sparse_vector_element<V1>, T2> {
typedef typename promote_traits<typename sparse_vector_element<V1>::value_type, T2>::promote_type promote_type;
};
template<class T1, class V2>
struct promote_traits<T1, sparse_vector_element<V2> > {
typedef typename promote_traits<T1, typename sparse_vector_element<V2>::value_type>::promote_type promote_type;
};
template<class V1, class V2>
struct promote_traits<sparse_vector_element<V1>, sparse_vector_element<V2> > {
typedef typename promote_traits<typename sparse_vector_element<V1>::value_type,
typename sparse_vector_element<V2>::value_type>::promote_type promote_type;
};
#endif
// Array based sparse vector class
template<class T, class A>
class sparse_vector:
public vector_expression<sparse_vector<T, A> > {
public:
#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS
BOOST_UBLAS_USING vector_expression<sparse_vector<T, A> >::operator ();
#endif
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
// typedef const T &const_reference;
typedef typename type_traits<T>::const_reference const_reference;
#if ! defined (BOOST_UBLAS_STRICT_STORAGE_SPARSE) && ! defined (BOOST_UBLAS_STRICT_VECTOR_SPARSE)
typedef T &reference;
#elif defined (BOOST_UBLAS_STRICT_VECTOR_SPARSE)
typedef sparse_vector_element<sparse_vector<T, A> > reference;
#elif defined (BOOST_UBLAS_STRICT_STORAGE_SPARSE)
typedef typename map_traits<A>::reference reference;
#endif
typedef const T *const_pointer;
typedef T *pointer;
typedef A array_type;
typedef const A const_array_type;
typedef const sparse_vector<T, A> const_self_type;
typedef sparse_vector<T, A> self_type;
#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS
typedef const vector_const_reference<const_self_type> const_closure_type;
#else
typedef const vector_reference<const_self_type> const_closure_type;
#endif
typedef vector_reference<self_type> closure_type;
typedef typename A::const_iterator const_iterator_type;
typedef typename A::iterator iterator_type;
typedef sparse_tag storage_category;
// Construction and destruction
BOOST_UBLAS_INLINE
sparse_vector ():
vector_expression<self_type> (),
size_ (0), non_zeros_ (0), data_ () {}
BOOST_UBLAS_INLINE
sparse_vector (size_type size, size_type non_zeros = 0):
vector_expression<self_type> (),
size_ (size), non_zeros_ (non_zeros), data_ () {
reserve (non_zeros_);
}
BOOST_UBLAS_INLINE
sparse_vector (const sparse_vector &v):
vector_expression<self_type> (),
size_ (v.size_), non_zeros_ (v.non_zeros_), data_ (v.data_) {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -