The ChainCon Software (Release 0.03)
ringz2.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file
4 ///
5 /// Elements of the ring Z_2.
6 ///
7 /////////////////////////////////////////////////////////////////////////////
8 
9 // Copyright (C) 2009-2016 by Pawel Pilarczyk.
10 //
11 // This file is part of my research software package. This is free software:
12 // you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation,
14 // either version 3 of the License, or (at your option) any later version.
15 //
16 // This software 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 General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this software; see the file "license.txt". If not,
23 // please, see <http://www.gnu.org/licenses/>.
24 
25 // Started on March 24, 2009. Last revision: January 4, 2013.
26 
27 
28 #ifndef _CHAINCON_RINGZ2_H_
29 #define _CHAINCON_RINGZ2_H_
30 
31 
32 // include some standard C++ header files
33 #include <istream>
34 #include <ostream>
35 #include <sstream>
36 #include <string>
37 
38 // include selected header files from the CHomP library
39 #include "chomp/system/config.h"
40 
41 
42 // --------------------------------------------------
43 // ------------ elements of the ring Z_2 ------------
44 // --------------------------------------------------
45 
46 /// An element of the ring Z_2.
47 class tZ2
48 {
49 public:
50  /// Default constructor of a ring element.
51  /// Note: The default copy constructor, destructor,
52  /// and assignment operators are fine.
53  tZ2 ();
54 
55  /// Constructor from an integer number
56  /// (to be used mainly for 0 or 1).
57  explicit tZ2 (int n);
58 
59  /// Conversion to an integer.
60  /// Note that in some rings this conversion may not be valid.
61  operator int () const;
62 
63  /// The delta function.
64  int delta () const;
65 
66  /// Negates the ring element.
67  tZ2 &negate ();
68 
69  /// Inverts the ring element.
70  tZ2 &invert ();
71 
72  /// Adds another ring element.
73  tZ2 &operator += (const tZ2 &another);
74 
75  /// Multiplies by another ring element.
76  tZ2 &operator *= (const tZ2 &another);
77 
78  /// Swaps the internal data with another ring element.
79  void swap (tZ2 &another);
80 
81  /// Returns the symbol of the ring.
82  static std::string ringsymbol ();
83 
84  /// Performs the division with remainder in the ring.
85  /// Returns true if the remainer is zero, false otherwise.
86  friend bool divide
87  (const tZ2 &a, const tZ2 &b,
88  tZ2 &quotient, tZ2 &remainder);
89 
90 private:
91  /// The number corresponding to the ring element.
92  bool _n;
93 
94 }; /* class tZ2 */
95 
96 // --------------------------------------------------
97 
98 inline tZ2::tZ2 ()
99 {
100  return;
101 } /* tZ2::tZ2 */
102 
103 inline tZ2::tZ2 (int n)
104 {
105  _n = (n & 1) ? true : false;
106  return;
107 } /* tZ2::tZ2 */
108 
109 
110 inline tZ2::operator int () const
111 {
112  return (_n ? 1 : 0);
113 } /* tZ2::operator int */
114 
115 inline int tZ2::delta () const
116 {
117  return (_n ? 1 : 0);
118 } /* tZ2::delta */
119 
120 inline tZ2 &tZ2::negate ()
121 {
122  return *this;
123 } /* tZ2::negate */
124 
125 inline tZ2 &tZ2::invert ()
126 {
127  if (!_n)
128  throw "Trying to invert the zero element of Z_2.";
129  return *this;
130 } /* tZ2::invert */
131 
132 inline tZ2 &tZ2::operator += (const tZ2 &another)
133 {
134  if (another. _n)
135  _n = !_n;
136  return *this;
137 } /* tZ2::operator += */
138 
139 inline tZ2 &tZ2::operator *= (const tZ2 &another)
140 {
141  if (!another. _n)
142  _n = false;
143  return *this;
144 } /* tZ2::operator *= */
145 
146 inline void tZ2::swap (tZ2 &another)
147 {
148  bool tmp (_n);
149  _n = another. _n;
150  another. _n = tmp;
151  return;
152 } /* tZ2::swap */
153 
154 // --------------------------------------------------
155 
156 inline std::string tZ2::ringsymbol ()
157 {
158  return std::string ("Z_2");
159 } /* tZ2::ringsymbol */
160 
161 // --------------------------------------------------
162 
163 /// Generates a hashing key no. 1 for an element of the Zp ring.
164 /// This key is to be used in a hashed set.
165 inline int_t hashkey1 (const tZ2 &n)
166 {
167  return (static_cast<int> (n) ? 1 : 0);
168 } /* hashkey1 */
169 
170 /// Generates a hashing key no. 2 for a general pair of elements,
171 /// based on hashing keys of the elements.
172 /// This key is to be used in a hashed set.
173 inline int_t hashkey2 (const tZ2 &n)
174 {
175  return (static_cast<int> (n) ? 0 : 1);
176 } /* hashkey2 */
177 
178 // --------------------------------------------------
179 
180 /// Reads an element from an input stream.
181 inline std::istream &operator >> (std::istream &in, tZ2 &n)
182 {
183  int number;
184  in >> number;
185  n = tZ2 (number);
186  return in;
187 } /* operator >> */
188 
189 /// Writes an element to an output stream.
190 inline std::ostream &operator << (std::ostream &out, const tZ2 &n)
191 {
192  out << static_cast<int> (n);
193  return out;
194 } /* operator << */
195 
196 // --------------------------------------------------
197 
198 /// Operator == for checking whether a ring element is equivalent
199 /// to an integer number. Intended to compare with 0 and 1 only.
200 template <class intType>
201 inline bool operator == (const tZ2 &n1, const intType &n2)
202 {
203  return (static_cast<intType> (static_cast<int> (n1)) == n2);
204 } /* operator == */
205 
206 /// Operator == for checking whether a ring element is equivalent
207 /// to an integer number. Intended to compare with 0 and 1 only.
208 template <class intType>
209 inline bool operator == (const intType &n1, const tZ2 &n2)
210 {
211  return (n1 == static_cast<intType> (static_cast<int> (n2)));
212 } /* operator == */
213 
214 
215 #endif // _CHAINCON_RINGZ2_H_
216 
std::istream & operator>>(std::istream &in, tZ2 &n)
Reads an element from an input stream.
Definition: ringz2.h:181
tZ2 & operator+=(const tZ2 &another)
Adds another ring element.
Definition: ringz2.h:132
int delta() const
The delta function.
Definition: ringz2.h:115
An element of the ring Z_2.
Definition: ringz2.h:47
int_t hashkey1(const tZ2 &n)
Generates a hashing key no.
Definition: ringz2.h:165
int_t hashkey2(const tZ2 &n)
Generates a hashing key no.
Definition: ringz2.h:173
tZ2 & operator*=(const tZ2 &another)
Multiplies by another ring element.
Definition: ringz2.h:139
static std::string ringsymbol()
Returns the symbol of the ring.
Definition: ringz2.h:156
tZ2 & negate()
Negates the ring element.
Definition: ringz2.h:120
void swap(tZ2 &another)
Swaps the internal data with another ring element.
Definition: ringz2.h:146
bool operator==(const tZ2 &n1, const intType &n2)
Operator == for checking whether a ring element is equivalent to an integer number.
Definition: ringz2.h:201
std::ostream & operator<<(std::ostream &out, const tZ2 &n)
Writes an element to an output stream.
Definition: ringz2.h:190
tZ2()
Default constructor of a ring element.
Definition: ringz2.h:98
bool _n
The number corresponding to the ring element.
Definition: ringz2.h:92
friend bool divide(const tZ2 &a, const tZ2 &b, tZ2 &quotient, tZ2 &remainder)
Performs the division with remainder in the ring.
tZ2 & invert()
Inverts the ring element.
Definition: ringz2.h:125