Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
matrix.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2006-05-22
7 
8  Copyright (C) 2006 EPFL
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 3.0 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
29 #if !defined(FEELPP_MATRIX_HPP)
30 #define FEELPP_MATRIX_HPP 1
31 
32 #include <boost/numeric/ublas/vector.hpp>
33 #include <boost/numeric/ublas/matrix_expression.hpp>
34 #include <boost/numeric/ublas/detail/matrix_assign.hpp>
35 
36 namespace boost
37 {
38 namespace numeric
39 {
40 namespace ublas
41 {
42 
44 // Identity matrix class
45 template<class T>
46 class anti_identity_matrix:
47  public matrix_container<anti_identity_matrix<T> >
48 {
49 
50  typedef const T *const_pointer;
51  typedef anti_identity_matrix<T> self_type;
52 public:
53 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
54  using matrix_container<self_type>::operator ();
55 #endif
56  typedef std::size_t size_type;
57  typedef std::ptrdiff_t difference_type;
58  typedef T value_type;
59  typedef const T &const_reference;
60  typedef T &reference;
61  typedef const matrix_reference<const self_type> const_closure_type;
62  typedef matrix_reference<self_type> closure_type;
63  typedef sparse_tag storage_category;
64  typedef unknown_orientation_tag orientation_category;
65 
66  // Construction and destruction
67  BOOST_UBLAS_INLINE
68  anti_identity_matrix ():
69  matrix_container<self_type> (),
70  size1_ ( 0 ), size2_ ( 0 ), size_common_ ( 0 ) {}
71  BOOST_UBLAS_INLINE
72  anti_identity_matrix ( size_type size ):
73  matrix_container<self_type> (),
74  size1_ ( size ), size2_ ( size ), size_common_ ( ( std::min ) ( size1_, size2_ ) ) {}
75  BOOST_UBLAS_INLINE
76  anti_identity_matrix ( size_type size1, size_type size2 ):
77  matrix_container<self_type> (),
78  size1_ ( size1 ), size2_ ( size2 ), size_common_ ( ( std::min ) ( size1_, size2_ ) ) {}
79  BOOST_UBLAS_INLINE
80  anti_identity_matrix ( const anti_identity_matrix &m ):
81  matrix_container<self_type> (),
82  size1_ ( m.size1_ ), size2_ ( m.size2_ ), size_common_ ( ( std::min ) ( size1_, size2_ ) ) {}
83 
84  // Accessors
85  BOOST_UBLAS_INLINE
86  size_type size1 () const
87  {
88  return size1_;
89  }
90  BOOST_UBLAS_INLINE
91  size_type size2 () const
92  {
93  return size2_;
94  }
95 
96  // Resizing
97  BOOST_UBLAS_INLINE
98  void resize ( size_type size, bool preserve = true )
99  {
100  size1_ = size;
101  size2_ = size;
102  }
103  BOOST_UBLAS_INLINE
104  void resize ( size_type size1, size_type size2, bool /*preserve*/ = true )
105  {
106  size1_ = size1;
107  size2_ = size2;
108  }
109 
110  // Element access
111  BOOST_UBLAS_INLINE
112  const_reference operator () ( size_type i, size_type j ) const
113  {
114  if ( i == size2_-( j+1 ) )
115  return one_;
116 
117  else
118  return zero_;
119  }
120 
121  // Assignment
122  BOOST_UBLAS_INLINE
123  anti_identity_matrix &operator = ( const anti_identity_matrix &m )
124  {
125  size1_ = m.size1_;
126  size2_ = m.size2_;
127  return *this;
128  }
129  BOOST_UBLAS_INLINE
130  anti_identity_matrix &assign_temporary ( anti_identity_matrix &m )
131  {
132  swap ( m );
133  return *this;
134  }
135 
136  // Swapping
137  BOOST_UBLAS_INLINE
138  void swap ( anti_identity_matrix &m )
139  {
140  if ( this != &m )
141  {
142  std::swap ( size1_, m.size1_ );
143  std::swap ( size2_, m.size2_ );
144  }
145  }
146  BOOST_UBLAS_INLINE
147  friend void swap ( anti_identity_matrix &m1, anti_identity_matrix &m2 )
148  {
149  m1.swap ( m2 );
150  }
151 
152  // Iterator types
153 private:
154  // Use an index
155  typedef size_type const_subiterator_type;
156 
157 public:
158  class const_iterator1;
159  class const_iterator2;
160  typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
161  typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
162 
163  // Element lookup
164  BOOST_UBLAS_INLINE
165  const_iterator1 find1 ( int rank, size_type i, size_type j ) const
166  {
167  if ( rank == 1 )
168  {
169  i = ( std::max ) ( i, j );
170  i = ( std::min ) ( i, j + 1 );
171  }
172 
173  return const_iterator1 ( *this, i );
174  }
175  BOOST_UBLAS_INLINE
176  const_iterator2 find2 ( int rank, size_type i, size_type j ) const
177  {
178  if ( rank == 1 )
179  {
180  j = ( std::max ) ( j, i );
181  j = ( std::min ) ( j, i + 1 );
182  }
183 
184  return const_iterator2 ( *this, j );
185  }
186 
187 
188  class const_iterator1:
189  public container_const_reference<anti_identity_matrix>,
190  public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
191  const_iterator1, value_type>
192  {
193  public:
194  typedef typename anti_identity_matrix::value_type value_type;
195  typedef typename anti_identity_matrix::difference_type difference_type;
196  typedef typename anti_identity_matrix::const_reference reference;
197  typedef typename anti_identity_matrix::const_pointer pointer;
198 
199  typedef const_iterator2 dual_iterator_type;
200  typedef const_reverse_iterator2 dual_reverse_iterator_type;
201 
202  // Construction and destruction
203  BOOST_UBLAS_INLINE
204  const_iterator1 ():
205  container_const_reference<self_type> (), it_ () {}
206  BOOST_UBLAS_INLINE
207  const_iterator1 ( const self_type &m, const const_subiterator_type &it ):
208  container_const_reference<self_type> ( m ), it_ ( it ) {}
209 
210  // Arithmetic
211  BOOST_UBLAS_INLINE
212  const_iterator1 &operator ++ ()
213  {
214  BOOST_UBLAS_CHECK ( it_ < ( *this ) ().size1 (), bad_index () );
215  ++it_;
216  return *this;
217  }
218  BOOST_UBLAS_INLINE
219  const_iterator1 &operator -- ()
220  {
221  BOOST_UBLAS_CHECK ( it_ > 0, bad_index () );
222  --it_;
223  return *this;
224  }
225 
226  // Dereference
227  BOOST_UBLAS_INLINE
228  const_reference operator * () const
229  {
230  return one_;
231  }
232 
233 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
234  BOOST_UBLAS_INLINE
235 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
236  typename self_type::
237 #endif
238  const_iterator2 begin () const
239  {
240  return const_iterator2 ( ( *this ) (), it_ );
241  }
242  BOOST_UBLAS_INLINE
243 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
244  typename self_type::
245 #endif
246  const_iterator2 end () const
247  {
248  return const_iterator2 ( ( *this ) (), it_ + 1 );
249  }
250  BOOST_UBLAS_INLINE
251 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
252  typename self_type::
253 #endif
254  const_reverse_iterator2 rbegin () const
255  {
256  return const_reverse_iterator2 ( end () );
257  }
258  BOOST_UBLAS_INLINE
259 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
260  typename self_type::
261 #endif
262  const_reverse_iterator2 rend () const
263  {
264  return const_reverse_iterator2 ( begin () );
265  }
266 #endif
267 
268  // Indices
269  BOOST_UBLAS_INLINE
270  size_type index1 () const
271  {
272  return it_;
273  }
274  BOOST_UBLAS_INLINE
275  size_type index2 () const
276  {
277  return it_;
278  }
279 
280  // Assignment
281  BOOST_UBLAS_INLINE
282  const_iterator1 &operator = ( const const_iterator1 &it )
283  {
284  container_const_reference<self_type>::assign ( &it () );
285  it_ = it.it_;
286  return *this;
287  }
288 
289  // Comparison
290  BOOST_UBLAS_INLINE
291  bool operator == ( const const_iterator1 &it ) const
292  {
293  BOOST_UBLAS_CHECK ( &( *this ) () == &it (), external_logic () );
294  return it_ == it.it_;
295  }
296 
297  private:
298  const_subiterator_type it_;
299  };
300 
301  typedef const_iterator1 iterator1;
302 
303  BOOST_UBLAS_INLINE
304  const_iterator1 begin1 () const
305  {
306  return const_iterator1 ( *this, 0 );
307  }
308  BOOST_UBLAS_INLINE
309  const_iterator1 end1 () const
310  {
311  return const_iterator1 ( *this, size_common_ );
312  }
313 
314  class const_iterator2:
315  public container_const_reference<anti_identity_matrix>,
316  public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
317  const_iterator2, value_type>
318  {
319  public:
320  typedef typename anti_identity_matrix::value_type value_type;
321  typedef typename anti_identity_matrix::difference_type difference_type;
322  typedef typename anti_identity_matrix::const_reference reference;
323  typedef typename anti_identity_matrix::const_pointer pointer;
324 
325  typedef const_iterator1 dual_iterator_type;
326  typedef const_reverse_iterator1 dual_reverse_iterator_type;
327 
328  // Construction and destruction
329  BOOST_UBLAS_INLINE
330  const_iterator2 ():
331  container_const_reference<self_type> (), it_ () {}
332  BOOST_UBLAS_INLINE
333  const_iterator2 ( const self_type &m, const const_subiterator_type &it ):
334  container_const_reference<self_type> ( m ), it_ ( it ) {}
335 
336  // Arithmetic
337  BOOST_UBLAS_INLINE
338  const_iterator2 &operator ++ ()
339  {
340  BOOST_UBLAS_CHECK ( it_ < ( *this ) ().size_common_, bad_index () );
341  ++it_;
342  return *this;
343  }
344  BOOST_UBLAS_INLINE
345  const_iterator2 &operator -- ()
346  {
347  BOOST_UBLAS_CHECK ( it_ > 0, bad_index () );
348  --it_;
349  return *this;
350  }
351 
352  // Dereference
353  BOOST_UBLAS_INLINE
354  const_reference operator * () const
355  {
356  return one_;
357  }
358 
359 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
360  BOOST_UBLAS_INLINE
361 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
362  typename self_type::
363 #endif
364  const_iterator1 begin () const
365  {
366  return const_iterator1 ( ( *this ) (), it_ );
367  }
368  BOOST_UBLAS_INLINE
369 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
370  typename self_type::
371 #endif
372  const_iterator1 end () const
373  {
374  return const_iterator1 ( ( *this ) (), it_ + 1 );
375  }
376  BOOST_UBLAS_INLINE
377 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
378  typename self_type::
379 #endif
380  const_reverse_iterator1 rbegin () const
381  {
382  return const_reverse_iterator1 ( end () );
383  }
384  BOOST_UBLAS_INLINE
385 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
386  typename self_type::
387 #endif
388  const_reverse_iterator1 rend () const
389  {
390  return const_reverse_iterator1 ( begin () );
391  }
392 #endif
393 
394  // Indices
395  BOOST_UBLAS_INLINE
396  size_type index1 () const
397  {
398  return it_;
399  }
400  BOOST_UBLAS_INLINE
401  size_type index2 () const
402  {
403  return it_;
404  }
405 
406  // Assignment
407  BOOST_UBLAS_INLINE
408  const_iterator2 &operator = ( const const_iterator2 &it )
409  {
410  container_const_reference<self_type>::assign ( &it () );
411  it_ = it.it_;
412  return *this;
413  }
414 
415  // Comparison
416  BOOST_UBLAS_INLINE
417  bool operator == ( const const_iterator2 &it ) const
418  {
419  BOOST_UBLAS_CHECK ( &( *this ) () == &it (), external_logic () );
420  return it_ == it.it_;
421  }
422 
423  private:
424  const_subiterator_type it_;
425  };
426 
427  typedef const_iterator2 iterator2;
428 
429  BOOST_UBLAS_INLINE
430  const_iterator2 begin2 () const
431  {
432  return const_iterator2 ( *this, 0 );
433  }
434  BOOST_UBLAS_INLINE
435  const_iterator2 end2 () const
436  {
437  return const_iterator2 ( *this, size_common_ );
438  }
439 
440  // Reverse iterators
441 
442  BOOST_UBLAS_INLINE
443  const_reverse_iterator1 rbegin1 () const
444  {
445  return const_reverse_iterator1 ( end1 () );
446  }
447  BOOST_UBLAS_INLINE
448  const_reverse_iterator1 rend1 () const
449  {
450  return const_reverse_iterator1 ( begin1 () );
451  }
452 
453  BOOST_UBLAS_INLINE
454  const_reverse_iterator2 rbegin2 () const
455  {
456  return const_reverse_iterator2 ( end2 () );
457  }
458  BOOST_UBLAS_INLINE
459  const_reverse_iterator2 rend2 () const
460  {
461  return const_reverse_iterator2 ( begin2 () );
462  }
463 
464 private:
465  size_type size1_;
466  size_type size2_;
467  size_type size_common_;
468  static const value_type zero_;
469  static const value_type one_;
470 };
471 
472 template<class T>
473 const typename anti_identity_matrix<T>::value_type anti_identity_matrix<T>::zero_ ( 0 );
474 template<class T>
475 const typename anti_identity_matrix<T>::value_type anti_identity_matrix<T>::one_ ( 1 );
477 
478 }
479 }
480 }
481 
482 #endif /* FEELPP_MATRIX_HPP */
483 
bool operator==(const BareEdge &p1, const BareEdge &p2)
Definition: bareitems.hpp:360
size_t size_type
Indices (starting from 0)
Definition: feelcore/feel.hpp:319
Elements & operator=(Elements const &e)
Definition: elements.hpp:335

Generated on Sun Dec 22 2013 13:11:06 for Feel++ by doxygen 1.8.5