glucat 0.12.0
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
42namespace 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 auto
50 operator^ (const index_set<LO,HI>& lhs,
51 const index_set<LO,HI>& rhs) -> const index_set<LO,HI>;
52
54 template<const index_t LO, const index_t HI>
55 auto
56 operator& (const index_set<LO,HI>& lhs,
57 const index_set<LO,HI>& rhs) -> const index_set<LO,HI>;
58
60 template<const index_t LO, const index_t HI>
61 auto
62 operator| (const index_set<LO,HI>& lhs,
63 const index_set<LO,HI>& rhs) -> const index_set<LO,HI>;
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 auto
69 compare(const index_set<LO,HI>& a, const index_set<LO,HI>& b) -> int;
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 using bitset_t = std::bitset<HI - LO>;
83 public:
85 using index_pair_t = std::pair<index_t, index_t>;
86
87 static const index_t v_lo = LO;
88 static const index_t v_hi = HI;
89
90 static auto classname() -> const std::string;
92 index_set () = default;
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 auto operator== (const index_set_t rhs) const -> bool;
107 auto operator!= (const index_set_t rhs) const -> bool;
109 auto operator~ () const -> index_set_t;
111 auto operator^= (const index_set_t rhs) -> index_set_t&;
113 auto operator&= (const index_set_t rhs) -> index_set_t&;
115 auto operator|= (const index_set_t rhs) -> index_set_t&;
117 auto operator[] (const index_t idx) const -> bool;
119 auto test(const index_t idx) const -> bool;
121 auto set() -> index_set_t&;
123 auto set(const index_t idx) -> index_set_t&;
125 auto set(const index_t idx, const int val) -> index_set_t&;
127 auto reset() -> index_set_t&;
129 auto reset(const index_t idx) -> index_set_t&;
131 auto flip() -> index_set_t&;
133 auto flip(const index_t idx) -> index_set_t&;
135 auto count() const -> index_t;
137 auto count_neg() const -> index_t;
139 auto count_pos() const -> index_t;
141 auto min() const -> index_t;
143 auto max() const -> index_t;
144
145 // Functions which support Clifford algebra operations
147 auto operator< (const index_set_t rhs) const -> bool;
149 auto is_contiguous () const -> bool;
151 auto fold () const -> const index_set_t;
153 auto fold (const index_set_t frm, const bool prechecked = false) const -> const index_set_t;
155 auto unfold (const index_set_t frm, const bool prechecked = false) const -> const index_set_t;
157 auto value_of_fold (const index_set_t frm) const -> set_value_t;
159 auto sign_of_mult (const index_set_t ist) const -> int;
161 auto sign_of_square() const -> int;
162
164 auto hash_fn () const -> size_t;
165
166 // Friends
167 friend auto operator^<> (const index_set_t& lhs, const index_set_t& rhs) -> const index_set_t;
168 friend auto operator&<> (const index_set_t& lhs, const index_set_t& rhs) -> const index_set_t;
169 friend auto operator|<> (const index_set_t& lhs, const index_set_t& rhs) -> const index_set_t;
170 friend auto compare<> (const index_set_t& lhs, const index_set_t& rhs) -> int;
171
172 // Member reference:
173 class reference;
174 friend class reference;
175
177 class reference {
178 friend class index_set;
179
180 public:
182 reference() = delete;
183 reference (index_set_t& ist, index_t idx);
184 ~reference () = default;
186 auto operator== (const reference& c_j) const -> bool;
188 auto operator= (const bool x) -> reference&;
190 auto operator= (const reference& c_j) -> reference&;
192 auto operator~ () const -> bool;
194 operator bool () const;
196 auto flip() -> reference&;
197
198 private:
200 index_t m_idx;
201 };
203 auto operator[](index_t idx) -> reference;
204 private:
206 auto lex_less_than (const index_set_t rhs) const -> bool;
207 };
208
210 _GLUCAT_CTAssert(sizeof(set_value_t) >= sizeof(std::bitset<DEFAULT_HI-DEFAULT_LO>),
211 Default_index_set_too_big_for_value)
212
213 // non-members
214
216 template<const index_t LO, const index_t HI>
217 auto
218 operator<< (std::ostream& os, const index_set<LO,HI>& ist) -> std::ostream&;
219
221 template<const index_t LO, const index_t HI>
222 auto
223 operator>> (std::istream& s, index_set<LO,HI>& ist) -> std::istream&;
224
225 // Functions which support Clifford algebra operations
227 auto sign_of_square(index_t j) -> int;
228
230 template<const index_t LO, const index_t HI>
231 auto
232 min_neg(const index_set<LO,HI>& ist) -> index_t;
233
235 template<const index_t LO, const index_t HI>
236 auto
237 max_pos(const index_set<LO,HI>& ist) -> index_t;
238}
239#endif // _GLUCAT_INDEX_SET_H
Specific exception class.
Definition: errors.h:57
Index set member reference.
Definition: index_set.h:177
reference()=delete
Default constructor is deleted.
Index set class based on std::bitset<> in Gnu standard C++ library.
Definition: index_set.h:75
auto value_of_fold(const index_set_t frm) const -> set_value_t
The set value of the fold of this index set within the given frame.
auto count() const -> index_t
Cardinality: Number of indices included in set.
std::bitset< HI - LO > bitset_t
Definition: index_set.h:81
auto flip() -> index_set_t &
Set complement, except 0: flip all bits, except 0.
auto lex_less_than(const index_set_t rhs) const -> bool
Lexicographic ordering of two sets: *this < rhs.
auto reset() -> index_set_t &
Make set empty: Set all bits to 0.
auto sign_of_square() const -> int
Sign of geometric square of a Clifford basis element.
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))
static const index_t v_lo
Definition: index_set.h:87
auto min() const -> index_t
Minimum member.
auto hash_fn() const -> size_t
Hash function.
auto set() -> index_set_t &
Include all indices except 0: set all bits except 0.
auto is_contiguous() const -> bool
Determine if the index set is contiguous, ie. has no gaps.
auto count_pos() const -> index_t
Number of positive indices included in set.
std::pair< index_t, index_t > index_pair_t
Definition: index_set.h:85
auto count_neg() const -> index_t
Number of negative indices included in set.
auto operator!=(const index_set_t rhs) const -> bool
Inequality.
static auto classname() -> const std::string
Definition: index_set_imp.h:50
auto operator~() const -> index_set_t
Set complement: not.
auto max() const -> index_t
Maximum member.
auto sign_of_mult(const index_set_t ist) const -> int
Sign of geometric product of two Clifford basis elements.
auto unfold(const index_set_t frm, const bool prechecked=false) const -> const index_set_t
Unfold this index set within the given frame.
index_set()=default
Default constructor creates an empty set.
auto operator==(const index_set_t rhs) const -> bool
Equality.
auto test(const index_t idx) const -> bool
Test idx for membership: test value of bit idx.
friend auto compare(const index_set_t &lhs, const index_set_t &rhs) -> int
auto fold() const -> const index_set_t
Fold this index set within itself as a frame.
static const index_t v_hi
Definition: index_set.h:88
#define _GLUCAT_CTAssert(expr, msg)
Definition: global.h:48
auto operator|(const Multivector< Scalar_T, LO, HI, Tune_P > &lhs, const RHS< Scalar_T, LO, HI, Tune_P > &rhs) -> const Multivector< Scalar_T, LO, HI, Tune_P >
Transformation via twisted adjoint action.
auto operator&(const Multivector< Scalar_T, LO, HI, Tune_P > &lhs, const RHS< Scalar_T, LO, HI, Tune_P > &rhs) -> const Multivector< Scalar_T, LO, HI, Tune_P >
Inner product.
auto compare(const index_set< LO, HI > &a, const index_set< LO, HI > &b) -> int
"lexicographic compare" eg. {3,4,5} is less than {3,7,8}
auto min_neg(const index_set< LO, HI > &ist) -> index_t
Minimum negative index, or 0 if none.
int index_t
Size of index_t should be enough to represent LO, HI.
Definition: global.h:77
unsigned long set_value_t
Size of set_value_t should be enough to contain index_set<LO,HI>
Definition: global.h:79
auto operator^(const Multivector< Scalar_T, LO, HI, Tune_P > &lhs, const RHS< Scalar_T, LO, HI, Tune_P > &rhs) -> const Multivector< Scalar_T, LO, HI, Tune_P >
Outer product.
auto max_pos(const index_set< LO, HI > &ist) -> index_t
Maximum positive index, or 0 if none.
const index_t DEFAULT_HI
Default highest index in an index set.
Definition: global.h:111