Developer documentation

extended.hpp
Go to the documentation of this file.
1 /********************************************************************
2  * This file is part of the source code of the realroot library.
3  * Author(s): P. Dobrowolski, Warsaw University of Technology, Poland
4  * $Id: extended.hpp,v 1.1 2012/02/22 20:00:00 pdobrowolski Exp $
5  ********************************************************************/
6 #ifndef REALROOT_EXTENDED_H_
7 #define REALROOT_EXTENDED_H_
8 
9 #include <cassert>
10 #include <realroot/scalar.hpp>
11 
12 namespace mmx {
13 
14 // this must hold for two extended types to be operable
15 #define extended_check(A, B) \
16  assert((A.c == 0 || B.c == 0 || A.c == B.c) && \
17  (A.c != 0 || A.b == 0) && \
18  (B.c != 0 || B.b == 0))
19 
20 // return a common root extension of two potentially extended types
21 #define common_root(A, B) \
22  (A.c != 0 ? A.c : B.c)
23 
24 template<class NT_>
25 struct extended
26 {
27  // a + b sqrt(c)
28  // note: c must not be a square!
29  typedef NT_ NT;
30  typedef scalar<NT> T;
31  typedef extended<NT> Self;
32 
33  T a, b, c;
34 
36  {
37  }
38 
39  extended(const T &a_)
40  : a(a_),
41  b(T(0)),
42  c(T(0))
43  {
44  }
45 
46  extended(const T &a_, const T &b_, const T &c_)
47  : a(a_),
48  b(b_),
49  c(c_)
50  {
51  }
52 
53  static int signof(const T &x)
54  {
55  if (x > 0)
56  return 1;
57  else if (x < 0)
58  return -1;
59  else
60  return 0;
61  }
62 
63  static int compare(const Self &lhs, const Self &rhs)
64  {
65  extended_check(lhs, rhs);
66  Self delta = lhs - rhs;
67  // equal
68  if (delta.a == 0 && delta.b == 0) return 0;
69  // simple
70  if (delta.a == 0) return signof(delta.b);
71  if (delta.b == 0) return signof(delta.a);
72  // less
73  if (delta.a < 0 && delta.b < 0) return -1;
74  // greater
75  if (delta.a > 0 && delta.b > 0) return 1;
76  // less or greater, but mixed signs
77  T aa = delta.a * delta.a;
78  T bbc = delta.b * delta.b * delta.c;
79  if (aa > bbc)
80  return signof(delta.a);
81  else
82  return signof(delta.b);
83  return 0;
84  }
85 };
86 
87 template<class NT> inline
88 bool operator == (const extended<NT> &lhs, const extended<NT> &rhs)
89 {
90  extended_check(lhs, rhs);
91  return lhs.a == rhs.a && lhs.b == rhs.b;
92 }
93 
94 template<class NT> inline
95 bool operator != (const extended<NT> &lhs, const extended<NT> &rhs)
96 {
97  return !(lhs == rhs);
98 }
99 
100 template<class NT> inline
102 {
103  extended_check(lhs, rhs);
104  typename extended<NT>::T root = common_root(lhs, rhs);
105  return extended<NT>(lhs.a * rhs.a + lhs.b * rhs.b * root,
106  lhs.a * rhs.b + lhs.b * rhs.a,
107  root);
108 }
109 
110 template<class NT> inline
112 {
113  extended_check(lhs, rhs);
114  typename extended<NT>::T root = common_root(lhs, rhs);
115  typename extended<NT>::T denominator = rhs.a * rhs.a - rhs.b * rhs.b * root;
116  return extended<NT>((lhs.a * rhs.a - lhs.b * rhs.b * lhs.c) / denominator,
117  (lhs.b * rhs.a - lhs.a * rhs.b) / denominator,
118  root);
119 }
120 
121 template<class NT> inline
123 {
124  extended_check(lhs, rhs);
125  typename extended<NT>::T root = common_root(lhs, rhs);
126  return extended<NT>(lhs.a + rhs.a,
127  lhs.b + rhs.b,
128  root);
129 }
130 
131 template<class NT> inline
133 {
134  extended_check(lhs, rhs);
135  typename extended<NT>::T root = common_root(lhs, rhs);
136  return extended<NT>(lhs.a - rhs.a,
137  lhs.b - rhs.b,
138  root);
139 }
140 
141 template<class NT> inline
143 {
144  return extended<NT>(-lhs.a,
145  -lhs.b,
146  lhs.c);
147 }
148 
149 template<class NT> inline
150 extended<NT> &operator <<= (extended<NT> &x, long int s)
151 {
152  x.a <<= s;
153  x.b <<= s;
154  return x;
155 }
156 
157 template<class NT> inline
159 {
160  x = x + other;
161  return x;
162 }
163 
164 template<class NT> inline
166 {
167  x = x - other;
168  return x;
169 }
170 
171 template<class NT> inline
173 {
174  x = x * other;
175  return x;
176 }
177 
178 template<class NT> inline
180 {
181  x = x / other;
182  return x;
183 }
184 
185 template<class NT> inline
186 bool operator <(const extended<NT> &lhs, const extended<NT> &rhs)
187 {
188  return extended<NT>::compare(lhs, rhs) < 0;
189 }
190 
191 template<class NT> inline
192 bool operator <=(const extended<NT> &lhs, const extended<NT> &rhs)
193 {
194  return extended<NT>::compare(lhs, rhs) <= 0;
195 }
196 
197 template<class NT> inline
198 bool operator >(const extended<NT> &lhs, const extended<NT> &rhs)
199 {
200  return extended<NT>::compare(lhs, rhs) > 0;
201 }
202 
203 template<class NT> inline
204 bool operator >=(const extended<NT> &lhs, const extended<NT> &rhs)
205 {
206  return extended<NT>::compare(lhs, rhs) >= 0;
207 }
208 
209 #undef extended_check
210 #undef common_root
211 } //namespace mmx
212 
213 #endif // REALROOT_EXTENDED_H_
extended< NT > & operator+=(extended< NT > &x, const extended< NT > &other)
Definition: extended.hpp:158
bool operator==(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:88
#define common_root(A, B)
Definition: extended.hpp:21
extended< NT > Self
Definition: extended.hpp:31
Definition: extended.hpp:25
extended< NT > operator-(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:132
extended< NT > operator/(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:111
bool operator>=(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:204
extended()
Definition: extended.hpp:35
scalar< NT > T
Definition: extended.hpp:30
T c
Definition: extended.hpp:33
extended< NT > & operator/=(extended< NT > &x, const extended< NT > &other)
Definition: extended.hpp:179
extended< NT > operator+(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:122
static int compare(const Self &lhs, const Self &rhs)
Definition: extended.hpp:63
NT_ NT
Definition: extended.hpp:29
extended< NT > & operator*=(extended< NT > &x, const extended< NT > &other)
Definition: extended.hpp:172
extended(const T &a_)
Definition: extended.hpp:39
extended(const T &a_, const T &b_, const T &c_)
Definition: extended.hpp:46
bool operator>(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:198
#define extended_check(A, B)
Definition: extended.hpp:15
ZZ denominator(const QQ &a)
Definition: GMPXX.hpp:96
static int signof(const T &x)
Definition: extended.hpp:53
T b
Definition: extended.hpp:33
T a
Definition: extended.hpp:33
bool operator!=(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:95
Definition: array.hpp:12
extended< NT > & operator-=(extended< NT > &x, const extended< NT > &other)
Definition: extended.hpp:165
extended< NT > operator*(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:101
Home