source: NonGTP/Boost/boost/numeric/ublas/expression_types.hpp @ 857

Revision 857, 18.3 KB checked in by igarcia, 18 years ago (diff)
Line 
1//
2//  Copyright (c) 2000-2002
3//  Joerg Walter, Mathias Koch
4//
5//  Permission to use, copy, modify, distribute and sell this software
6//  and its documentation for any purpose is hereby granted without fee,
7//  provided that the above copyright notice appear in all copies and
8//  that both that copyright notice and this permission notice appear
9//  in supporting documentation.  The authors make no representations
10//  about the suitability of this software for any purpose.
11//  It is provided "as is" without express or implied warranty.
12//
13//  The authors gratefully acknowledge the support of
14//  GeNeSys mbH & Co. KG in producing this work.
15//
16#ifndef _BOOST_UBLAS_EXPRESSION_TYPE_
17#define _BOOST_UBLAS_EXPRESSION_TYPE_
18
19#include <boost/numeric/ublas/exception.hpp>
20#include <boost/numeric/ublas/traits.hpp>
21#include <boost/numeric/ublas/functional.hpp>
22
23
24// Expression templates based on ideas of Todd Veldhuizen and Geoffrey Furnish
25// Iterators based on ideas of Jeremy Siek
26
27namespace boost { namespace numeric { namespace ublas {
28
29    // Base class for uBLAS staticaly derived expressions - see the Barton Nackman trick
30    //  Provides numeric properties for linear algebra
31    template<class E>
32    class ublas_expression {
33    public:
34        typedef E expression_type;
35        /* E can be an incomplete type - to define the following we would need more template arguments
36        typedef typename E::type_category type_category;
37        typedef typename E::value_type value_type;
38        */
39       
40        // Directly implement nonassignable - simplifes debugging call trace!
41    protected:
42        ublas_expression () {}
43        ~ublas_expression () {}
44    private:
45        const ublas_expression& operator= (const ublas_expression &);
46    };
47
48
49    // Base class for Scalar Expression models -
50    //  it does not model the Scalar Expression concept but all derived types should.
51    // The class defines a common base type and some common interface for all
52    // statically derived Scalar Expression classes
53    // We implement the casts to the statically derived type.
54    template<class E>
55    class scalar_expression:
56        public ublas_expression<E> {
57    public:
58        typedef E expression_type;
59        typedef scalar_tag type_category;
60
61        BOOST_UBLAS_INLINE
62        const expression_type &operator () () const {
63            return *static_cast<const expression_type *> (this);
64        }
65        BOOST_UBLAS_INLINE
66        expression_type &operator () () {
67            return *static_cast<expression_type *> (this);
68        }
69    };
70
71    template<class T>
72    class scalar_reference:
73        public scalar_expression<scalar_reference<T> > {
74
75        typedef scalar_reference<T> self_type;
76    public:
77        typedef T value_type;
78        typedef const value_type &const_reference;
79        typedef typename boost::mpl::if_<boost::is_const<T>,
80                                          const_reference,
81                                          value_type &>::type reference;
82        typedef const self_type const_closure_type;
83        typedef const_closure_type closure_type;
84
85        // Construction and destruction
86        BOOST_UBLAS_INLINE
87        explicit scalar_reference (reference t):
88            t_ (t) {}
89
90        // Conversion
91        BOOST_UBLAS_INLINE
92        operator value_type () const {
93            return t_;
94        }
95
96        // Assignment
97        BOOST_UBLAS_INLINE
98        scalar_reference &operator = (const scalar_reference &s) {
99            t_ = s.t_;
100            return *this;
101        }
102        template<class AE>
103        BOOST_UBLAS_INLINE
104        scalar_reference &operator = (const scalar_expression<AE> &ae) {
105            t_ = ae;
106            return *this;
107        }
108
109        // Closure comparison
110        BOOST_UBLAS_INLINE
111        bool same_closure (const scalar_reference &sr) const {
112            return &t_ == &sr.t_;
113        }
114
115    private:
116        reference t_;
117    };
118
119    template<class T>
120    class scalar_value:
121        public scalar_expression<scalar_value<T> > {
122
123        typedef scalar_value<T> self_type;
124    public:
125        typedef T value_type;
126        typedef const value_type &const_reference;
127        typedef typename boost::mpl::if_<boost::is_const<T>,
128                                          const_reference,
129                                          value_type &>::type reference;
130        typedef const scalar_reference<const self_type> const_closure_type;
131        typedef scalar_reference<self_type> closure_type;
132
133        // Construction and destruction
134        BOOST_UBLAS_INLINE
135        scalar_value ():
136            t_ () {}
137        BOOST_UBLAS_INLINE
138        scalar_value (const value_type &t):
139            t_ (t) {}
140
141        BOOST_UBLAS_INLINE
142        operator value_type () const {
143            return t_;
144        }
145
146        // Assignment
147        BOOST_UBLAS_INLINE
148        scalar_value &operator = (const scalar_value &s) {
149            t_ = s.t_;
150            return *this;
151        }
152        template<class AE>
153        BOOST_UBLAS_INLINE
154        scalar_value &operator = (const scalar_expression<AE> &ae) {
155            t_ = ae;
156            return *this;
157        }
158
159        // Closure comparison
160        BOOST_UBLAS_INLINE
161        bool same_closure (const scalar_value &sv) const {
162            return this == &sv;    // self closing on instances value
163        }
164
165    private:
166        value_type t_;
167    };
168
169
170    // Base class for Vector Expression models -
171    //  it does not model the Vector Expression concept but all derived types should.
172    // The class defines a common base type and some common interface for all
173    // statically derived Vector Expression classes
174    // We implement the casts to the statically derived type.
175    template<class E>
176    class vector_expression:
177        public ublas_expression<E> {
178    public:
179        static const unsigned complexity = 0;
180        typedef E expression_type;
181        typedef vector_tag type_category;
182        /* E can be an incomplete type - to define the following we would need more template arguments
183        typedef typename E::size_type size_type;
184        */
185 
186        BOOST_UBLAS_INLINE
187        const expression_type &operator () () const {
188            return *static_cast<const expression_type *> (this);
189        }
190        BOOST_UBLAS_INLINE
191        expression_type &operator () () {
192            return *static_cast<expression_type *> (this);
193        }
194
195#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
196    private:
197        // projection types
198        typedef vector_range<E> vector_range_type;
199        typedef vector_range<const E> const_vector_range_type;
200        typedef vector_slice<E> vector_slice_type;
201        typedef vector_slice<const E> const_vector_slice_type;
202        // vector_indirect_type will depend on the A template parameter
203        typedef basic_range<> default_range;    // required to avoid range/slice name confusion
204        typedef basic_slice<> default_slice;
205   public:
206        BOOST_UBLAS_INLINE
207        const_vector_range_type operator () (const default_range &r) const {
208            return const_vector_range_type (operator () (), r);
209        }
210        BOOST_UBLAS_INLINE
211        vector_range_type operator () (const default_range &r) {
212            return vector_range_type (operator () (), r);
213        }
214        BOOST_UBLAS_INLINE
215        const_vector_slice_type operator () (const default_slice &s) const {
216            return const_vector_slice_type (operator () (), s);
217        }
218        BOOST_UBLAS_INLINE
219        vector_slice_type operator () (const default_slice &s) {
220            return vector_slice_type (operator () (), s);
221        }
222        template<class A>
223        BOOST_UBLAS_INLINE
224        const vector_indirect<const E, A> operator () (const indirect_array<A> &ia) const {
225            return vector_indirect<const E, A>  (operator () (), ia);
226        }
227        template<class A>
228        BOOST_UBLAS_INLINE
229        vector_indirect<E, A> operator () (const indirect_array<A> &ia) {
230            return vector_indirect<E, A> (operator () (), ia);
231        }
232
233        BOOST_UBLAS_INLINE
234        const_vector_range_type project (const default_range &r) const {
235            return const_vector_range_type (operator () (), r);
236        }
237        BOOST_UBLAS_INLINE
238        vector_range_type project (const default_range &r) {
239            return vector_range_type (operator () (), r);
240        }
241        BOOST_UBLAS_INLINE
242        const_vector_slice_type project (const default_slice &s) const {
243            return const_vector_slice_type (operator () (), s);
244        }
245        BOOST_UBLAS_INLINE
246        vector_slice_type project (const default_slice &s) {
247            return vector_slice_type (operator () (), s);
248        }
249        template<class A>
250        BOOST_UBLAS_INLINE
251        const vector_indirect<const E, A> project (const indirect_array<A> &ia) const {
252            return vector_indirect<const E, A> (operator () (), ia);
253        }
254        template<class A>
255        BOOST_UBLAS_INLINE
256        vector_indirect<E, A> project (const indirect_array<A> &ia) {
257            return vector_indirect<E, A> (operator () (), ia);
258        }
259#endif
260    };
261
262    // Base class for Vector container models -
263    //  it does not model the Vector concept but all derived types should.
264    // The class defines a common base type and some common interface for all
265    // statically derived Vector classes
266    // We implement the casts to the statically derived type.
267    template<class C>
268    class vector_container:
269        public vector_expression<C> {
270    public:
271        static const unsigned complexity = 0;
272        typedef C container_type;
273        typedef vector_tag type_category;
274 
275        BOOST_UBLAS_INLINE
276        const container_type &operator () () const {
277            return *static_cast<const container_type *> (this);
278        }
279        BOOST_UBLAS_INLINE
280        container_type &operator () () {
281            return *static_cast<container_type *> (this);
282        }
283
284#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
285        using vector_expression<C>::operator ();
286#endif
287    };
288
289
290    // Base class for Matrix Expression models -
291    //  it does not model the Matrix Expression concept but all derived types should.
292    // The class defines a common base type and some common interface for all
293    // statically derived Matrix Expression classes
294    // We implement the casts to the statically derived type.
295    template<class E>
296    class matrix_expression:
297        public ublas_expression<E> {
298    public:
299        static const unsigned complexity = 0;
300        typedef E expression_type;
301        typedef matrix_tag type_category;
302        /* E can be an incomplete type - to define the following we would need more template arguments
303        typedef typename E::size_type size_type;
304        */
305
306        BOOST_UBLAS_INLINE
307        const expression_type &operator () () const {
308            return *static_cast<const expression_type *> (this);
309        }
310        BOOST_UBLAS_INLINE
311        expression_type &operator () () {
312            return *static_cast<expression_type *> (this);
313        }
314
315#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
316    private:
317        // projection types
318        typedef vector_range<E> vector_range_type;
319        typedef const vector_range<const E> const_vector_range_type;
320        typedef vector_slice<E> vector_slice_type;
321        typedef const vector_slice<const E> const_vector_slice_type;
322        typedef matrix_row<E> matrix_row_type;
323        typedef const matrix_row<const E> const_matrix_row_type;
324        typedef matrix_column<E> matrix_column_type;
325        typedef const  matrix_column<const E> const_matrix_column_type;
326        typedef matrix_range<E> matrix_range_type;
327        typedef const matrix_range<const E> const_matrix_range_type;
328        typedef matrix_slice<E> matrix_slice_type;
329        typedef const matrix_slice<const E> const_matrix_slice_type;
330        // matrix_indirect_type will depend on the A template parameter
331        typedef basic_range<> default_range;    // required to avoid range/slice name confusion
332        typedef basic_slice<> default_slice;
333
334    public:
335        BOOST_UBLAS_INLINE
336        const_matrix_row_type operator [] (std::size_t i) const {
337            return const_matrix_row_type (operator () (), i);
338        }
339        BOOST_UBLAS_INLINE
340        matrix_row_type operator [] (std::size_t i) {
341            return matrix_row_type (operator () (), i);
342        }
343        BOOST_UBLAS_INLINE
344        const_matrix_row_type row (std::size_t i) const {
345            return const_matrix_row_type (operator () (), i);
346        }
347        BOOST_UBLAS_INLINE
348        matrix_row_type row (std::size_t i) {
349            return matrix_row_type (operator () (), i);
350        }
351        BOOST_UBLAS_INLINE
352        const_matrix_column_type column (std::size_t j) const {
353            return const_matrix_column_type (operator () (), j);
354        }
355        BOOST_UBLAS_INLINE
356        matrix_column_type column (std::size_t j) {
357            return matrix_column_type (operator () (), j);
358        }
359
360        BOOST_UBLAS_INLINE
361        const_matrix_range_type operator () (const default_range &r1, const default_range &r2) const {
362            return const_matrix_range_type (operator () (), r1, r2);
363        }
364        BOOST_UBLAS_INLINE
365        matrix_range_type operator () (const default_range &r1, const default_range &r2) {
366            return matrix_range_type (operator () (), r1, r2);
367        }
368        BOOST_UBLAS_INLINE
369        const_matrix_slice_type operator () (const default_slice &s1, const default_slice &s2) const {
370            return const_matrix_slice_type (operator () (), s1, s2);
371        }
372        BOOST_UBLAS_INLINE
373        matrix_slice_type operator () (const default_slice &s1, const default_slice &s2) {
374            return matrix_slice_type (operator () (), s1, s2);
375        }
376        template<class A>
377        BOOST_UBLAS_INLINE
378        const matrix_indirect<const E, A> operator () (const indirect_array<A> &ia1, const indirect_array<A> &ia2) const {
379            return matrix_indirect<const E, A> (operator () (), ia1, ia2);
380        }
381        template<class A>
382        BOOST_UBLAS_INLINE
383        matrix_indirect<E, A> operator () (const indirect_array<A> &ia1, const indirect_array<A> &ia2) {
384            return matrix_indirect<E, A> (operator () (), ia1, ia2);
385        }
386
387        BOOST_UBLAS_INLINE
388        const_matrix_range_type project (const default_range &r1, const default_range &r2) const {
389            return const_matrix_range_type (operator () (), r1, r2);
390        }
391        BOOST_UBLAS_INLINE
392        matrix_range_type project (const default_range &r1, const default_range &r2) {
393            return matrix_range_type (operator () (), r1, r2);
394        }
395        BOOST_UBLAS_INLINE
396        const_matrix_slice_type project (const default_slice &s1, const default_slice &s2) const {
397            return const_matrix_slice_type (operator () (), s1, s2);
398        }
399        BOOST_UBLAS_INLINE
400        matrix_slice_type project (const default_slice &s1, const default_slice &s2) {
401            return matrix_slice_type (operator () (), s1, s2);
402        }
403        template<class A>
404        BOOST_UBLAS_INLINE
405        const matrix_indirect<const E, A> project (const indirect_array<A> &ia1, const indirect_array<A> &ia2) const {
406            return matrix_indirect<const E, A> (operator () (), ia1, ia2);
407        }
408        template<class A>
409        BOOST_UBLAS_INLINE
410        matrix_indirect<E, A> project (const indirect_array<A> &ia1, const indirect_array<A> &ia2) {
411            return matrix_indirect<E, A> (operator () (), ia1, ia2);
412        }
413#endif
414    };
415
416#ifdef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
417    struct iterator1_tag {};
418    struct iterator2_tag {};
419
420    template<class I>
421    BOOST_UBLAS_INLINE
422    typename I::dual_iterator_type begin (const I &it, iterator1_tag) {
423        return it ().find2 (1, it.index1 (), 0);
424    }
425    template<class I>
426    BOOST_UBLAS_INLINE
427    typename I::dual_iterator_type end (const I &it, iterator1_tag) {
428        return it ().find2 (1, it.index1 (), it ().size2 ());
429    }
430    template<class I>
431    BOOST_UBLAS_INLINE
432    typename I::dual_reverse_iterator_type rbegin (const I &it, iterator1_tag) {
433        return typename I::dual_reverse_iterator_type (end (it, iterator1_tag ()));
434    }
435    template<class I>
436    BOOST_UBLAS_INLINE
437    typename I::dual_reverse_iterator_type rend (const I &it, iterator1_tag) {
438        return typename I::dual_reverse_iterator_type (begin (it, iterator1_tag ()));
439    }
440
441    template<class I>
442    BOOST_UBLAS_INLINE
443    typename I::dual_iterator_type begin (const I &it, iterator2_tag) {
444        return it ().find1 (1, 0, it.index2 ());
445    }
446    template<class I>
447    BOOST_UBLAS_INLINE
448    typename I::dual_iterator_type end (const I &it, iterator2_tag) {
449        return it ().find1 (1, it ().size1 (), it.index2 ());
450    }
451    template<class I>
452    BOOST_UBLAS_INLINE
453    typename I::dual_reverse_iterator_type rbegin (const I &it, iterator2_tag) {
454        return typename I::dual_reverse_iterator_type (end (it, iterator2_tag ()));
455    }
456    template<class I>
457    BOOST_UBLAS_INLINE
458    typename I::dual_reverse_iterator_type rend (const I &it, iterator2_tag) {
459        return typename I::dual_reverse_iterator_type (begin (it, iterator2_tag ()));
460    }
461#endif
462
463    // Base class for Matrix container models -
464    //  it does not model the Matrix concept but all derived types should.
465    // The class defines a common base type and some common interface for all
466    // statically derived Matrix classes
467    // We implement the casts to the statically derived type.
468    template<class C>
469    class matrix_container:
470        public matrix_expression<C> {
471    public:
472        static const unsigned complexity = 0;
473        typedef C container_type;
474        typedef matrix_tag type_category;
475
476        BOOST_UBLAS_INLINE
477        const container_type &operator () () const {
478            return *static_cast<const container_type *> (this);
479        }
480        BOOST_UBLAS_INLINE
481        container_type &operator () () {
482            return *static_cast<container_type *> (this);
483        }
484
485#ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
486        using matrix_expression<C>::operator ();
487#endif
488    };
489
490}}}
491
492#endif
Note: See TracBrowser for help on using the repository browser.