00001
00002
00003
00004
00005
00006 #ifndef REALROOT_SCALAR_EXTENDED_INTEGER_H_
00007 #define REALROOT_SCALAR_EXTENDED_INTEGER_H_
00008
00009 #include <string>
00010 #include <iostream>
00011 #include <realroot/extended.hpp>
00012 #include <realroot/scalar_integer.hpp>
00013 #include <cassert>
00014
00015 namespace mmx {
00016
00017 typedef extended<MPZ> EMPZ;
00018
00019 std::ostream& operator <<(std::ostream& os, const scalar<EMPZ>& b);
00020
00021 template<> inline
00022 void scalar<EMPZ>::init() {}
00023
00024 template<> inline
00025 scalar<EMPZ>::scalar() : data() {}
00026
00027 template<> inline
00028 scalar<EMPZ>::~scalar() {}
00029
00030 template<> inline
00031 scalar<EMPZ>::scalar(int si) : data(scalar<MPZ>(si)) {}
00032
00033 template<> inline
00034 scalar<EMPZ>::scalar(unsigned si) : data(scalar<MPZ>(si)) {}
00035
00036 template<> inline
00037 scalar<EMPZ>::scalar(const char *string, unsigned int base) : data(scalar<MPZ>(string, base)) {}
00038
00039 template<> template<class U>
00040 inline scalar<EMPZ>::scalar(const scalar<U> &a, const scalar<U> &b, const scalar<U> &c) : data(a, b, c) {}
00041
00042 template<> inline
00043 scalar<EMPZ>::scalar(signed long int sl) : data(scalar<MPZ>(sl)) {}
00044
00045 template<> inline
00046 scalar<EMPZ>::scalar(unsigned long int ul) : data(scalar<MPZ>(ul)) {}
00047
00048 template<> inline
00049 scalar<EMPZ>::scalar(const scalar<EMPZ>& rhs) : data(rhs.rep()) {}
00050
00051 template<> inline
00052 scalar<EMPZ>::scalar(double rhs) : data(scalar<MPZ>(rhs)) {}
00053
00054 template<> inline
00055 scalar<EMPZ>& scalar<EMPZ>::operator =(const scalar<EMPZ>& rhs) { rep() = rhs.rep(); return *this; }
00056
00057 template<> inline
00058 bool scalar<EMPZ>::operator ==(const scalar<EMPZ>& rhs) const { return rep() == rhs.rep(); }
00059
00060 template<> inline
00061 bool scalar<EMPZ>::operator ==(long sl) const { return rep() == EMPZ(sl); }
00062
00063 template<> inline
00064 bool scalar<EMPZ>::operator ==(int si) const { return rep() == EMPZ(si); }
00065
00066 template<> inline
00067 bool scalar<EMPZ>::operator ==(unsigned long ul) const { return rep() == EMPZ(ul); }
00068
00069
00070 template<> inline
00071 bool scalar<EMPZ>::operator !=(const scalar<EMPZ>& rhs) const { return rep() != rhs.rep(); }
00072
00073 template<> inline
00074 bool scalar<EMPZ>::operator !=(long sl) const { return rep() != EMPZ(sl); }
00075
00076 template<> inline
00077 bool scalar<EMPZ>::operator !=(int si) const { return rep() != EMPZ(si); }
00078
00079 template<> inline
00080 bool scalar<EMPZ>::operator !=(unsigned long ul) const { return rep() != EMPZ(ul); }
00081
00082
00083 template<> inline
00084 bool scalar<EMPZ>::operator >(const scalar<EMPZ>& rhs) const { return rep() > rhs.rep(); }
00085
00086 template<> inline
00087 bool scalar<EMPZ>::operator >(long sl) const { return rep() > EMPZ(sl); }
00088
00089 template<> inline
00090 bool scalar<EMPZ>::operator >(int si) const { return rep() > EMPZ(si); }
00091
00092 template<> inline
00093 bool scalar<EMPZ>::operator >(unsigned long ul) const { return rep() > EMPZ(ul); }
00094
00095
00096 template<> inline
00097 bool scalar<EMPZ>::operator >=(const scalar<EMPZ>& rhs) const { return rep() >= rhs.rep(); }
00098
00099 template<> inline
00100 bool scalar<EMPZ>::operator >=(long sl) const { return rep() >= EMPZ(sl); }
00101
00102 template<> inline
00103 bool scalar<EMPZ>::operator >=(int si) const { return rep() >= EMPZ(si); }
00104
00105 template<> inline
00106 bool scalar<EMPZ>::operator >=(unsigned long ul) const { return rep() >= EMPZ(ul); }
00107
00108
00109 template<> inline
00110 bool scalar<EMPZ>::operator <(const scalar<EMPZ>& rhs) const { return rep() < rhs.rep(); }
00111
00112 template<> inline
00113 bool scalar<EMPZ>::operator <(long sl) const { return rep() < EMPZ(sl); }
00114
00115 template<> inline
00116 bool scalar<EMPZ>::operator <(int si) const { return rep() < EMPZ(si); }
00117
00118 template<> inline
00119 bool scalar<EMPZ>::operator <(unsigned long ul) const { return rep() < EMPZ(ul); }
00120
00121
00122 template<> inline
00123 bool scalar<EMPZ>::operator <=(const scalar<EMPZ>& rhs) const { return rep() <= rhs.rep(); }
00124
00125 template<> inline
00126 bool scalar<EMPZ>::operator <=(long sl) const { return rep() <= EMPZ(sl); }
00127
00128 template<> inline
00129 bool scalar<EMPZ>::operator <=(int si) const { return rep() <= EMPZ(si); }
00130
00131 template<> inline
00132 bool scalar<EMPZ>::operator <=(unsigned long ul) const { return rep() <= EMPZ(ul); }
00133
00134
00135 template<> inline
00136 scalar<EMPZ>& scalar<EMPZ>::operator =(unsigned long ul) { rep() = EMPZ(ul); return *this;}
00137
00138 template<> inline
00139 scalar<EMPZ>& scalar<EMPZ>::operator =(long sl) { rep() = EMPZ(sl); return *this;}
00140
00141 template<> inline
00142 scalar<EMPZ>& scalar<EMPZ>::operator =(int si) { rep() = EMPZ(si); return *this;}
00143
00144
00145 template<> inline
00146 scalar<EMPZ>& scalar<EMPZ>::operator +=(const scalar<EMPZ>& rhs) { rep() += rhs.rep(); return *this; }
00147
00148 template<> inline
00149 scalar<EMPZ>& scalar<EMPZ>::operator +=(unsigned long ul) { rep() += EMPZ(ul); return *this;}
00150
00151 template<> inline
00152 scalar<EMPZ>& scalar<EMPZ>::operator +=(long sl) { rep() += EMPZ(sl); return *this;}
00153
00154 template<> inline
00155 scalar<EMPZ>& scalar<EMPZ>::operator +=(int si) { rep() += EMPZ(si); return *this;}
00156
00157
00158 template<> inline
00159 scalar<EMPZ>& scalar<EMPZ>::operator -=(const scalar<EMPZ>& rhs) { rep() -= rhs.rep(); return *this; }
00160
00161 template<> inline
00162 scalar<EMPZ>& scalar<EMPZ>::operator -=(unsigned long ul) { rep() -= EMPZ(ul); return *this;}
00163
00164 template<> inline
00165 scalar<EMPZ>& scalar<EMPZ>::operator -=(long sl) { rep() -= EMPZ(sl); return *this;}
00166
00167 template<> inline
00168 scalar<EMPZ>& scalar<EMPZ>::operator -=(int si) { rep() -= EMPZ(si); return *this;}
00169
00170
00171 template<> inline
00172 scalar<EMPZ>& scalar<EMPZ>::operator *=(const scalar<EMPZ>& rhs) { rep() *= rhs.rep(); return *this; }
00173
00174 template<> inline
00175 scalar<EMPZ>& scalar<EMPZ>::operator *=(unsigned long ul) { rep() *= EMPZ(ul); return *this;}
00176
00177 template<> inline
00178 scalar<EMPZ>& scalar<EMPZ>::operator *=(long sl) { rep() *= EMPZ(sl); return *this;}
00179
00180 template<> inline
00181 scalar<EMPZ>& scalar<EMPZ>::operator *=(int si) { rep() *= EMPZ(si); return *this;}
00182
00183
00184 template<> inline
00185 scalar<EMPZ>& scalar<EMPZ>::operator /=(const scalar<EMPZ>& rhs) { rep() /= rhs.rep(); return *this; }
00186
00187 template<> inline
00188 scalar<EMPZ>& scalar<EMPZ>::operator /=(unsigned long ul) { rep() /= EMPZ(ul); return *this;}
00189
00190 template<> inline
00191 scalar<EMPZ>& scalar<EMPZ>::operator /=(long sl) { rep() /= EMPZ(sl); return *this;}
00192
00193 template<> inline
00194 scalar<EMPZ>& scalar<EMPZ>::operator /=(int si) { rep() /= EMPZ(si); return *this;}
00195
00196
00197
00198
00199 inline
00200 scalar<EMPZ> operator +(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2)
00201 {
00202 scalar<EMPZ> result;
00203 result.rep() = a1.rep() + a2.rep();
00204 return result;
00205 }
00206
00207 inline
00208 scalar<EMPZ> operator -(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2)
00209 {
00210 scalar<EMPZ> result;
00211 result.rep() = a1.rep() - a2.rep();
00212 return result;
00213 }
00214
00215 inline
00216 scalar<EMPZ> operator -(const scalar<EMPZ>& a1)
00217 {
00218 scalar<EMPZ> result;
00219 result.rep() = -a1.rep();
00220 return result;
00221 }
00222
00223 inline
00224 scalar<EMPZ> operator *(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2)
00225 {
00226 scalar<EMPZ> result;
00227 result.rep() = a1.rep() * a2.rep();
00228 return result;
00229 }
00230
00231 inline
00232 scalar<EMPZ> operator *(const scalar<EMPZ>& a1, const scalar<MPZ>& a2)
00233 {
00234 scalar<EMPZ> result;
00235 result.rep() = a1.rep() * EMPZ(a2);
00236 return result;
00237 }
00238
00239 inline scalar<EMPZ> operator *(const scalar<MPZ>& a1, const scalar<EMPZ>& a2)
00240 {
00241 scalar<EMPZ> result;
00242 result.rep() = EMPZ(a1) * a2.rep();
00243 return result;
00244 }
00245
00246 inline
00247 scalar<EMPZ> operator *(const scalar<EMPZ>& a2, int a1)
00248 {
00249 return a2 * scalar<EMPZ>(a1);
00250 }
00251
00252 inline
00253 scalar<EMPZ> operator *(int a1, const scalar<EMPZ>& a2)
00254 {
00255 scalar<EMPZ> result;
00256 result.rep() = EMPZ(scalar<MPZ>(a1)) * a2.rep();
00257 return result;
00258 }
00259
00260 inline
00261 scalar<EMPZ> operator /(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2)
00262 {
00263 scalar<EMPZ> result;
00264 result.rep() = a1.rep() / a2.rep();
00265 return result;
00266 }
00267
00268 inline
00269 scalar<EMPZ> operator /(const scalar<EMPZ>& a1, const scalar<MPZ>& a2)
00270 {
00271 scalar<EMPZ> result;
00272 result.rep() = a1.rep() / EMPZ(a2);
00273 return result;
00274 }
00275
00277 template<> inline
00278 void scalar<EMPZ>::operator ++()
00279 {
00280 ++rep().a;
00281 }
00282
00284 template<> inline
00285 void scalar<EMPZ>::operator --()
00286 {
00287 --rep().a;
00288 }
00289
00290
00291
00292 inline
00293 std::ostream& operator <<(std::ostream& os, const scalar<EMPZ>& b)
00294 {
00295 os << b.rep().a << " + " << b.rep().b << " * sqrt( " << b.rep().c << " )";
00296 return os;
00297 }
00298
00299 template<class OSTREAM> inline void
00300 print(OSTREAM& os, const scalar<EMPZ>& b) {
00301 os << b.rep().a << " + " << b.rep().b << " * sqrt( " << b.rep().c << " )";
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 template<> inline void
00315 scalar<EMPZ>::abs()
00316 {
00317 if (*this < 0)
00318 *this = -*this;
00319 }
00320
00321 template<> inline
00322 scalar<EMPZ>& scalar<EMPZ>::negate()
00323 {
00324 *this = -*this;
00325 return *this;
00326 }
00327
00328 inline
00329 scalar<EMPZ> operator << (const scalar<EMPZ>& x, long int s) {
00330 scalar<EMPZ> r;
00331 r.rep().a <<= s;
00332 r.rep().b <<= s;
00333 return r;
00334 }
00335
00336 inline
00337 scalar<EMPZ>& operator <<= (scalar<EMPZ>& x, long int s)
00338 {
00339 x.rep().a <<= s;
00340 x.rep().b <<= s;
00341 return x;
00342 }
00343
00344
00345 inline
00346 int compare(const scalar<EMPZ>& b1, const scalar<EMPZ>& b2) { return EMPZ::compare(b1.rep(), b2.rep()); }
00347
00348 inline
00349 int compare(const scalar<EMPZ>& b, unsigned long ul) { return EMPZ::compare(b.rep(), EMPZ(scalar<MPZ>(ul))); }
00350
00351 inline
00352 int compare(const scalar<EMPZ>& b, long sl) { return EMPZ::compare(b.rep(), EMPZ(scalar<MPZ>(sl))); }
00353
00354 inline
00355 int compare(const scalar<EMPZ>& b, int si) { return EMPZ::compare(b.rep(), EMPZ(scalar<MPZ>(si))); }
00356
00357 inline
00358 scalar<EMPZ> min(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2) { return a1 < a2 ? a1 : a2; }
00359
00360 inline
00361 scalar<EMPZ> max(const scalar<EMPZ>& a1, const scalar<EMPZ>& a2) { return a1 > a2 ? a1 : a2; }
00362
00363
00364 inline long int bit_size(const scalar<EMPZ>& z) { return std::max(bit_size(z.rep().a), bit_size(z.rep().b)); }
00365
00366 namespace let
00367 {
00368 inline void assign(scalar<MPZ>& x, const scalar<EMPZ>& r) { assert(0); }
00369 }
00370
00371 template<typename T,typename F> struct as_helper;
00372
00373 template<> struct as_helper<double,scalar<EMPZ> > {
00374 static inline double cv(const scalar<EMPZ>& x) {double r; let::assign(r,x); return r;}
00375 };
00376 }
00377
00378 #endif // REALROOT_SCALAR_EXTENDED_INTEGER_H_