glucat  0.8.4
index_set.h
Go to the documentation of this file.
1 #ifndef _GLUCAT_INDEX_SET_H
2 #define _GLUCAT_INDEX_SET_H
3 /***************************************************************************
4  GluCat : Generic library of universal Clifford algebra templates
5  index_set.h : Declare a class for a set of non-zero integer indices
6  -------------------
7  begin : Sun 2001-12-09
8  copyright : (C) 2001-2012 by Paul C. Leopardi
9  ***************************************************************************
10 
11  This library is free software: you can redistribute it and/or modify
12  it under the terms of the GNU Lesser General Public License as published
13  by the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public License
22  along with this library. If not, see <http://www.gnu.org/licenses/>.
23 
24  ***************************************************************************
25  This library is based on a prototype written by Arvind Raja and was
26  licensed under the LGPL with permission of the author. See Arvind Raja,
27  "Object-oriented implementations of Clifford algebras in C++: a prototype",
28  in Ablamowicz, Lounesto and Parra (eds.)
29  "Clifford algebras with numeric and symbolic computations", Birkhauser, 1996.
30  ***************************************************************************
31  See also Arvind Raja's original header comments in glucat.h
32  ***************************************************************************/
33 
34 #include "glucat/global.h"
35 #include "glucat/errors.h"
36 
37 #include <boost/static_assert.hpp>
38 
39 #include <bitset>
40 #include <utility>
41 
42 namespace glucat
43 {
44  template<const index_t LO, const index_t HI>
45  class index_set; // forward
46 
48  template<const index_t LO, const index_t HI>
49  const index_set<LO,HI>
50  operator^ (const index_set<LO,HI>& lhs,
51  const index_set<LO,HI>& rhs);
52 
54  template<const index_t LO, const index_t HI>
55  const index_set<LO,HI>
56  operator& (const index_set<LO,HI>& lhs,
57  const index_set<LO,HI>& rhs);
58 
60  template<const index_t LO, const index_t HI>
61  const index_set<LO,HI>
62  operator| (const index_set<LO,HI>& lhs,
63  const index_set<LO,HI>& rhs);
64 
66  // -1 if a<b, +1 if a>b, 0 if a==b
67  template<const index_t LO, const index_t HI>
68  int
69  compare(const index_set<LO,HI>& a, const index_set<LO,HI>& b);
70 
72  template<const index_t LO, const index_t HI>
73  class index_set :
74  private std::bitset<HI-LO>
75  {
76  private:
77  BOOST_STATIC_ASSERT((LO <= 0) && (0 <= HI) && (LO < HI) && \
78  (-LO < _GLUCAT_BITS_PER_ULONG) && \
79  ( HI < _GLUCAT_BITS_PER_ULONG) && \
80  ( HI-LO <= _GLUCAT_BITS_PER_ULONG));
81  typedef std::bitset<HI-LO> bitset_t;
82  typedef error<index_set> error_t;
83  public:
84  typedef index_set index_set_t;
85  typedef std::pair<index_t,index_t> index_pair_t;
86 
87  static const index_t v_lo = LO;
88  static const index_t v_hi = HI;
89 
90  static const std::string classname();
92  index_set () { }
94  index_set (const bitset_t bst);
96  index_set (const index_t idx);
98  index_set (const set_value_t folded_val, const index_set_t frm, const bool prechecked = false);
100  index_set (const index_pair_t& range, const bool prechecked = false);
102  index_set (const std::string& str);
103 
105  bool operator== (const index_set_t rhs) const;
107  bool operator!= (const index_set_t rhs) const;
109  index_set_t operator~ () const;
115  index_set_t& operator|= (const index_set_t rhs);
117  bool operator[] (const index_t idx) const;
119  bool test(const index_t idx) const;
123  index_set_t& set(const index_t idx);
125  index_set_t& set(const index_t idx, const int val);
127  index_set_t& reset();
129  index_set_t& reset(const index_t idx);
131  index_set_t& flip();
133  index_set_t& flip(const index_t idx);
135  index_t count() const;
137  index_t count_neg() const;
139  index_t count_pos() const;
141  index_t min() const;
143  index_t max() const;
144 
145  // Functions which support Clifford algebra operations
147  bool operator< (const index_set_t rhs) const;
149  bool is_contiguous () const;
151  const index_set_t fold () const;
153  const index_set_t fold (const index_set_t frm, const bool prechecked = false) const;
155  const index_set_t unfold (const index_set_t frm, const bool prechecked = false) const;
157  set_value_t value_of_fold (const index_set_t frm) const;
159  int sign_of_mult (const index_set_t ist) const;
161  int sign_of_square() const;
162 
164  size_t hash_fn () const;
165 
166  // Friends
167  friend const index_set_t operator^<> (const index_set_t& lhs, const index_set_t& rhs);
168  friend const index_set_t operator&<> (const index_set_t& lhs, const index_set_t& rhs);
169  friend const index_set_t operator|<> (const index_set_t& lhs, const index_set_t& rhs);
170  friend int compare<> (const index_set_t& lhs, const index_set_t& rhs);
171 
172  // Member reference:
173  class reference;
174  friend class reference;
175 
177  class reference {
178  friend class index_set;
179 
181  reference();
182  public:
184  ~reference () {}
186  reference& operator= (const bool x);
188  reference& operator= (const reference& j);
190  bool operator~ () const;
192  operator bool () const;
194  reference& flip();
195 
196  private:
199  };
202  private:
204  bool lex_less_than (const index_set_t rhs) const;
205  };
206 
208  _GLUCAT_CTAssert(sizeof(set_value_t) >= sizeof(std::bitset<DEFAULT_HI-DEFAULT_LO>),
209  Default_index_set_too_big_for_value)
210 
211  // non-members
212 
213 
214  template<const index_t LO, const index_t HI>
215  std::ostream&
216  operator<< (std::ostream& os, const index_set<LO,HI>& ist);
217 
219  template<const index_t LO, const index_t HI>
220  std::istream&
221  operator>> (std::istream& s, index_set<LO,HI>& ist);
222 
223  // Functions which support Clifford algebra operations
225  int sign_of_square(index_t j);
226 
228  template<const index_t LO, const index_t HI>
229  index_t
230  min_neg(const index_set<LO,HI>& ist);
231 
233  template<const index_t LO, const index_t HI>
234  index_t
235  max_pos(const index_set<LO,HI>& ist);
236 }
237 #endif // _GLUCAT_INDEX_SET_H
global.h
glucat::index_set::bitset_t
std::bitset< HI-LO > bitset_t
Definition: index_set.h:110
glucat::min_neg
index_t min_neg(const index_set< LO, HI > &ist)
Minimum negative index, or 0 if none.
Definition: index_set_imp.h:996
glucat::index_set::reference::operator~
bool operator~() const
Flips a bit.
Definition: index_set_imp.h:1050
glucat::index_set::operator^=
index_set_t & operator^=(const index_set_t rhs)
Symmetric set difference: exclusive or.
Definition: index_set_imp.h:178
glucat::index_set::count_pos
index_t count_pos() const
Number of positive indices included in set.
Definition: index_set_imp.h:405
glucat::index_set::unfold
const index_set_t unfold(const index_set_t frm, const bool prechecked=false) const
Unfold this index set within the given frame.
Definition: index_set_imp.h:823
glucat::index_set::test
bool test(const index_t idx) const
Test idx for membership: test value of bit idx.
Definition: index_set_imp.h:269
glucat::index_set::min
index_t min() const
Minimum member.
Definition: index_set_imp.h:490
glucat::index_set::index_set
index_set()
Default constructor creates an empty set.
Definition: index_set.h:121
glucat::index_set::v_lo
static const index_t v_lo
Definition: index_set.h:116
glucat::index_set::reference::m_pst
index_set_t * m_pst
Definition: index_set.h:226
glucat::operator|
const Multivector< Scalar_T, LO, HI > operator|(const Multivector< Scalar_T, LO, HI > &lhs, const RHS< Scalar_T, LO, HI > &rhs)
Transformation via twisted adjoint action.
Definition: clifford_algebra_imp.h:339
glucat::index_set::count
index_t count() const
Cardinality: Number of indices included in set.
Definition: index_set_imp.h:373
PyClical.ist
ist
Definition: PyClical.pyx:1878
errors.h
glucat::max_pos
index_t max_pos(const index_set< LO, HI > &ist)
Maximum positive index, or 0 if none.
Definition: index_set_imp.h:1003
glucat::index_t
int index_t
Size of index_t should be enough to represent LO, HI.
Definition: global.h:106
glucat::index_set::operator==
bool operator==(const index_set_t rhs) const
Equality.
Definition: index_set_imp.h:148
glucat::index_set::operator~
index_set_t operator~() const
Set complement: not.
Definition: index_set_imp.h:170
glucat::index_set::operator<
bool operator<(const index_set_t rhs) const
Less than operator used for comparisons, map, etc.
Definition: index_set_imp.h:626
glucat::index_set::operator|=
index_set_t & operator|=(const index_set_t rhs)
Set union: or.
Definition: index_set_imp.h:228
glucat::set_value_t
unsigned long set_value_t
Size of set_value_t should be enough to contain index_set<LO,HI>
Definition: global.h:108
glucat::index_set::operator[]
bool operator[](const index_t idx) const
Subscripting: Test idx for membership: test value of bit idx.
Definition: index_set_imp.h:261
glucat::index_set
Index set class based on std::bitset<> in Gnu standard C++ library.
Definition: index_set.h:74
glucat::index_set::reset
index_set_t & reset()
Make set empty: Set all bits to 0.
Definition: index_set_imp.h:323
glucat::index_set::reference::reference
reference()
Private default constructor is left undefined.
glucat::index_set::reference::m_idx
index_t m_idx
Definition: index_set.h:227
glucat::index_set::fold
const index_set_t fold() const
Fold this index set within itself as a frame.
Definition: index_set_imp.h:777
glucat::index_set::v_hi
static const index_t v_hi
Definition: index_set.h:117
glucat::index_set::index_pair_t
std::pair< index_t, index_t > index_pair_t
Definition: index_set.h:114
glucat::index_set::is_contiguous
bool is_contiguous() const
Determine if the index set is contiguous, ie. has no gaps.
Definition: index_set_imp.h:761
glucat::index_set::classname
static const std::string classname()
Definition: index_set_imp.h:78
glucat::index_set::reference::operator=
reference & operator=(const bool x)
for b[i] = x;
Definition: index_set_imp.h:1022
glucat::compare
int compare(const index_set< LO, HI > &a, const index_set< LO, HI > &b)
"lexicographic compare" eg. {3,4,5} is less than {3,7,8}
Definition: index_set_imp.h:602
glucat::index_set::operator!=
bool operator!=(const index_set_t rhs) const
Inequality.
Definition: index_set_imp.h:159
glucat::index_set::reference::~reference
~reference()
Definition: index_set.h:213
glucat::sign_of_square
int sign_of_square(index_t j)
Square of generator {j}.
Definition: index_set_imp.h:989
glucat::error
Specific exception class.
Definition: errors.h:87
glucat::index_set::reference
friend class reference
Definition: index_set.h:202
glucat::operator&
const Multivector< Scalar_T, LO, HI > operator&(const Multivector< Scalar_T, LO, HI > &lhs, const RHS< Scalar_T, LO, HI > &rhs)
Inner product.
Definition: clifford_algebra_imp.h:257
glucat::index_set::set
index_set_t & set()
Include all indices except 0: set all bits except 0.
Definition: index_set_imp.h:284
glucat::index_set::count_neg
index_t count_neg() const
Number of negative indices included in set.
Definition: index_set_imp.h:393
glucat::index_set::reference::flip
reference & flip()
for b[i].flip();
Definition: index_set_imp.h:1065
std
Definition: framed_multi.h:371
glucat
Definition: clifford_algebra.h:39
glucat::index_set::error_t
error< index_set > error_t
Definition: index_set.h:111
glucat::_GLUCAT_CTAssert
_GLUCAT_CTAssert(std::numeric_limits< unsigned char >::radix==2, CannotDetermineBitsPerChar) const index_t BITS_PER_CHAR
If radix of unsigned char is not 2, we can't easily determine number of bits from sizeof.
glucat::index_set::index_set_t
index_set index_set_t
Definition: index_set.h:113
glucat::index_set::hash_fn
size_t hash_fn() const
Hash function.
Definition: index_set_imp.h:977
glucat::index_set::sign_of_square
int sign_of_square() const
Sign of geometric square of a Clifford basis element.
Definition: index_set_imp.h:957
glucat::index_set::value_of_fold
set_value_t value_of_fold(const index_set_t frm) const
The set value of the fold of this index set within the given frame.
Definition: index_set_imp.h:857
glucat::index_set::lex_less_than
bool lex_less_than(const index_set_t rhs) const
Lexicographic ordering of two sets: *this < rhs.
Definition: index_set_imp.h:617
glucat::index_set::flip
index_set_t & flip()
Set complement, except 0: flip all bits, except 0.
Definition: index_set_imp.h:348
glucat::index_set::operator&=
index_set_t & operator&=(const index_set_t rhs)
Set intersection: and.
Definition: index_set_imp.h:203
glucat::index_set::sign_of_mult
int sign_of_mult(const index_set_t ist) const
Sign of geometric product of two Clifford basis elements.
Definition: index_set_imp.h:908
glucat::operator^
const Multivector< Scalar_T, LO, HI > operator^(const Multivector< Scalar_T, LO, HI > &lhs, const RHS< Scalar_T, LO, HI > &rhs)
Outer product.
Definition: clifford_algebra_imp.h:242
glucat::index_set::BOOST_STATIC_ASSERT
BOOST_STATIC_ASSERT((LO<=0) &&(0<=HI) &&(LO< HI) &&(-LO< _GLUCAT_BITS_PER_ULONG) &&(HI< _GLUCAT_BITS_PER_ULONG) &&(HI-LO<=_GLUCAT_BITS_PER_ULONG))
glucat::index_set::max
index_t max() const
Maximum member.
Definition: index_set_imp.h:579