📄 matrix_expression.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_MATRIX_EXPRESSION_H
#define BOOST_UBLAS_MATRIX_EXPRESSION_H
#include <boost/numeric/ublas/config.hpp>
#include <boost/numeric/ublas/exception.hpp>
#include <boost/numeric/ublas/functional.hpp>
#include <boost/numeric/ublas/noalias.hpp>
// Expression templates based on ideas of Todd Veldhuizen and Geoffrey Furnish
// Iterators based on ideas of Jeremy Siek
namespace boost { namespace numeric { namespace ublas {
// Base class for the Barton Nackman trick
template<class E>
struct matrix_expression:
private boost::nonassignable {
BOOST_STATIC_CONSTANT (int, complexity = 0);
typedef E expression_type;
typedef matrix_tag type_category;
typedef abstract_tag simd_category;
// FIXME: Why doesn't this work?
// typedef typename E::size_type size_type;
typedef std::size_t size_type;
typedef noalias_proxy<E> noalias_proxy_type;
typedef const matrix_row<const E> const_matrix_row_type;
typedef matrix_row<E> matrix_row_type;
typedef const matrix_column<const E> const_matrix_column_type;
typedef matrix_column<E> matrix_column_type;
typedef const matrix_range<const E> const_matrix_range_type;
typedef matrix_range<E> matrix_range_type;
typedef const matrix_slice<const E> const_matrix_slice_type;
typedef matrix_slice<E> matrix_slice_type;
typedef const matrix_indirect<const E> const_matrix_indirect_type;
typedef matrix_indirect<E> matrix_indirect_type;
// This class could define an common interface for all
// statically derived expression type classes.
// Due to a compiler deficiency - one can not reference class typedefs of E
// on MSVC 6.0 (error C2027) - we only implement the casts.
BOOST_UBLAS_INLINE
const expression_type &operator () () const {
return *static_cast<const expression_type *> (this);
}
BOOST_UBLAS_INLINE
expression_type &operator () () {
return *static_cast<expression_type *> (this);
}
BOOST_UBLAS_INLINE
noalias_proxy_type noalias () {
return noalias_proxy_type (operator () ());
}
BOOST_UBLAS_INLINE
const_matrix_row_type operator [] (size_type i) const {
return const_matrix_row_type (operator () (), i);
}
BOOST_UBLAS_INLINE
matrix_row_type operator [] (size_type i) {
return matrix_row_type (operator () (), i);
}
BOOST_UBLAS_INLINE
const_matrix_row_type row (size_type i) const {
return const_matrix_row_type (operator () (), i);
}
BOOST_UBLAS_INLINE
matrix_row_type row (size_type i) {
return matrix_row_type (operator () (), i);
}
BOOST_UBLAS_INLINE
const_matrix_column_type column (size_type j) const {
return const_matrix_column_type (operator () (), j);
}
BOOST_UBLAS_INLINE
matrix_column_type column (size_type j) {
return matrix_column_type (operator () (), j);
}
#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS
BOOST_UBLAS_INLINE
const_matrix_range_type operator () (const range &r1, const range &r2) const {
return const_matrix_range_type (operator () (), r1, r2);
}
BOOST_UBLAS_INLINE
matrix_range_type operator () (const range &r1, const range &r2) {
return matrix_range_type (operator () (), r1, r2);
}
BOOST_UBLAS_INLINE
const_matrix_slice_type operator () (const slice &s1, const slice &s2) const {
return const_matrix_slice_type (operator () (), s1, s2);
}
BOOST_UBLAS_INLINE
matrix_slice_type operator () (const slice &s1, const slice &s2) {
return matrix_slice_type (operator () (), s1, s2);
}
template<class A>
BOOST_UBLAS_INLINE
const_matrix_indirect_type operator () (const indirect_array<A> &ia1, const indirect_array<A> &ia2) const {
return const_matrix_indirect_type (operator () (), ia1, ia2);
}
template<class A>
BOOST_UBLAS_INLINE
matrix_indirect_type operator () (const indirect_array<A> &ia1, const indirect_array<A> &ia2) {
return matrix_indirect_type (operator () (), ia1, ia2);
}
#else
BOOST_UBLAS_INLINE
const_matrix_range_type project (const range &r1, const range &r2) const {
return const_matrix_range_type (operator () (), r1, r2);
}
BOOST_UBLAS_INLINE
matrix_range_type project (const range &r1, const range &r2) {
return matrix_range_type (operator () (), r1, r2);
}
BOOST_UBLAS_INLINE
const_matrix_slice_type project (const slice &s1, const slice &s2) const {
return const_matrix_slice_type (operator () (), s1, s2);
}
BOOST_UBLAS_INLINE
matrix_slice_type project (const slice &s1, const slice &s2) {
return matrix_slice_type (operator () (), s1, s2);
}
template<class A>
BOOST_UBLAS_INLINE
const_matrix_indirect_type project (const indirect_array<A> &ia1, const indirect_array<A> &ia2) const {
return const_matrix_indirect_type (operator () (), ia1, ia2);
}
template<class A>
BOOST_UBLAS_INLINE
matrix_indirect_type project (const indirect_array<A> &ia1, const indirect_array<A> &ia2) {
return matrix_indirect_type (operator () (), ia1, ia2);
}
#endif
};
#ifdef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
struct iterator1_tag {};
struct iterator2_tag {};
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_iterator_type begin (const I &it, iterator1_tag) {
return it ().find2 (1, it.index1 (), 0);
}
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_iterator_type end (const I &it, iterator1_tag) {
return it ().find2 (1, it.index1 (), it ().size2 ());
}
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_reverse_iterator_type rbegin (const I &it, iterator1_tag) {
return typename I::dual_reverse_iterator_type (end (it, iterator1_tag ()));
}
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_reverse_iterator_type rend (const I &it, iterator1_tag) {
return typename I::dual_reverse_iterator_type (begin (it, iterator1_tag ()));
}
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_iterator_type begin (const I &it, iterator2_tag) {
return it ().find1 (1, 0, it.index2 ());
}
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_iterator_type end (const I &it, iterator2_tag) {
return it ().find1 (1, it ().size1 (), it.index2 ());
}
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_reverse_iterator_type rbegin (const I &it, iterator2_tag) {
return typename I::dual_reverse_iterator_type (end (it, iterator2_tag ()));
}
template<class I>
BOOST_UBLAS_INLINE
typename I::dual_reverse_iterator_type rend (const I &it, iterator2_tag) {
return typename I::dual_reverse_iterator_type (begin (it, iterator2_tag ()));
}
#endif
#ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS
template<class E>
class matrix_const_reference:
public matrix_expression<matrix_const_reference<E> > {
public:
#ifndef BOOST_UBLAS_NO_PROXY_SHORTCUTS
BOOST_UBLAS_USING matrix_expression<matrix_const_reference<E> >::operator ();
#endif
typedef E expression_type;
typedef typename E::simd_category simd_category;
typedef typename E::size_type size_type;
typedef typename E::difference_type difference_type;
typedef typename E::value_type value_type;
typedef typename E::const_reference const_reference;
typedef const_reference reference;
typedef typename E::const_pointer const_pointer;
typedef const_pointer pointer;
typedef typename E::orientation_category orientation_category;
typedef typename E::const_iterator1 const_iterator1_type;
typedef typename E::const_iterator2 const_iterator2_type;
typedef unknown_storage_tag storage_category;
// Construction and destruction
BOOST_UBLAS_INLINE
matrix_const_reference ():
e_ (nil_) {}
BOOST_UBLAS_INLINE
matrix_const_reference (const expression_type &e):
e_ (e) {}
// Accessors
BOOST_UBLAS_INLINE
size_type size1 () const {
return e_.size1 ();
}
BOOST_UBLAS_INLINE
size_type size2 () const {
return e_.size2 ();
}
BOOST_UBLAS_INLINE
const expression_type &expression () const {
return e_;
}
// Element access
BOOST_UBLAS_INLINE
const_reference operator () (size_type i, size_type j) const {
return expression () (i, j);
}
// Comparison
BOOST_UBLAS_INLINE
bool operator == (const matrix_const_reference &mr) const {
return &(*this).expression () == &mr.expression ();
}
typedef const_iterator1_type const_iterator1;
typedef const_iterator1 iterator1;
typedef const_iterator2_type const_iterator2;
typedef const_iterator2 iterator2;
// Element lookup
BOOST_UBLAS_INLINE
const_iterator1 find1 (int rank, size_type i, size_type j) const {
return expression ().find1 (rank, i, j);
}
BOOST_UBLAS_INLINE
const_iterator2 find2 (int rank, size_type i, size_type j) const {
return expression ().find2 (rank, i, j);
}
// Iterators are the iterators of the referenced expression.
BOOST_UBLAS_INLINE
const_iterator1 begin1 () const {
return expression ().begin1 ();
}
BOOST_UBLAS_INLINE
const_iterator1 end1 () const {
return expression ().end1 ();
}
BOOST_UBLAS_INLINE
const_iterator2 begin2 () const {
return expression ().begin2 ();
}
BOOST_UBLAS_INLINE
const_iterator2 end2 () const {
return expression ().end2 ();
}
// Reverse iterators
#ifdef BOOST_MSVC_STD_ITERATOR
typedef reverse_iterator_base1<const_iterator1, value_type, const_reference> const_reverse_iterator1;
#else
typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
#endif
BOOST_UBLAS_INLINE
const_reverse_iterator1 rbegin1 () const {
return const_reverse_iterator1 (end1 ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator1 rend1 () const {
return const_reverse_iterator1 (begin1 ());
}
#ifdef BOOST_MSVC_STD_ITERATOR
typedef reverse_iterator_base1<const_iterator1, value_type, const_reference> reverse_iterator1;
#else
typedef reverse_iterator_base1<const_iterator1> reverse_iterator1;
#endif
#ifdef BOOST_MSVC_STD_ITERATOR
typedef reverse_iterator_base2<const_iterator2, value_type, const_reference> const_reverse_iterator2;
#else
typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
#endif
BOOST_UBLAS_INLINE
const_reverse_iterator2 rbegin2 () const {
return const_reverse_iterator2 (end2 ());
}
BOOST_UBLAS_INLINE
const_reverse_iterator2 rend2 () const {
return const_reverse_iterator2 (begin2 ());
}
#ifdef BOOST_MSVC_STD_ITERATOR
typedef reverse_iterator_base2<const_iterator2, value_type, const_reference> reverse_iterator2;
#else
typedef reverse_iterator_base2<const_iterator2> reverse_iterator2;
#endif
private:
const expression_type &e_;
static expression_type nil_;
};
template<class E>
typename matrix_const_reference<E>::expression_type matrix_const_reference<E>::nil_;
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -