Changeset 66 for include/fflas-ffpack/modular-balanced.h
- Timestamp:
- 06/05/08 14:58:48 (6 months ago)
- Files:
-
- 1 modified
-
include/fflas-ffpack/modular-balanced.h (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
include/fflas-ffpack/modular-balanced.h
r62 r66 17 17 #include <math.h> 18 18 #include "fflas-ffpack/modular-randiter.h" 19 #include "fflas-ffpack/nonzero-randiter.h" 19 20 20 21 template <class Element> … … 26 27 protected: 27 28 double modulus; 28 double inv_modulus;29 29 double half_mod; 30 30 unsigned long lmodulus; 31 31 32 32 public: … … 34 34 typedef unsigned long FieldInt; 35 35 typedef ModularBalancedRandIter<double> RandIter; 36 typedef NonzeroRandIter<ModularBalanced<double>, ModularBalancedRandIter<double> > NonZeroRandIter; 36 37 37 38 const bool balanced; 38 39 39 ModularBalanced<double> (int p) : modulus((double)p), inv_modulus(1./(double)p), half_mod( (p-1.)/2), balanced(true) {} 40 41 ModularBalanced<double>(const ModularBalanced<double>& mf) : modulus(mf.modulus), inv_modulus(mf.inv_modulus), half_mod(mf.half_mod), balanced(true){} 40 ModularBalanced<double> (int p) : modulus((double)p), 41 half_mod( (p-1.)/2), 42 balanced(true) {} 43 44 ModularBalanced<double>(const ModularBalanced<double>& mf) 45 : modulus(mf.modulus), half_mod(mf.half_mod), balanced(true){} 42 46 43 47 const ModularBalanced<double> &operator=(const ModularBalanced<double> &F) { 44 48 modulus = F.modulus; 45 inv_modulus = F.inv_modulus;46 49 half_mod = F.half_mod; 47 50 return *this; … … 70 73 71 74 std::ostream &write (std::ostream &os) const { 72 return os << " intmod " << (int)modulus;75 return os << "double mod " << (int)modulus; 73 76 } 74 77 … … 96 99 } 97 100 98 inline Element& init(Element& x, double y =0) const {101 inline Element& init(Element& x, const double y =0) const { 99 102 double tmp; 100 103 … … 131 134 if ( x > half_mod ) return x -= modulus; 132 135 if ( x < -half_mod ) return x += modulus; 133 elsereturn x;136 return x; 134 137 } 135 138 … … 137 140 x = y - z; 138 141 if (x > half_mod) return x -= modulus; 139 elseif (x < -half_mod) return x += modulus;140 elsereturn x;142 if (x < -half_mod) return x += modulus; 143 return x; 141 144 } 142 145 143 146 inline Element &mul (Element &x, const Element &y, const Element &z) const { 144 147 double tmp= y*z; 145 //x= tmp - floor(tmp*inv_modulus)*modulus;146 148 return init (x, tmp); 147 149 } … … 187 189 const Element &y) const { 188 190 double tmp= a*x+y; 189 //return r = tmp- floor(tmp*inv_modulus)*modulus;190 191 return init( r, tmp); 191 192 192 } 193 193 … … 196 196 if ( x > half_mod ) return x -= modulus; 197 197 if ( x < -half_mod ) return x += modulus; 198 elsereturn x;198 return x; 199 199 } 200 200 … … 203 203 if ( x > half_mod ) return x -= modulus; 204 204 if ( x < -half_mod ) return x += modulus; 205 elsereturn x;205 return x; 206 206 } 207 207 … … 224 224 225 225 inline Element &axpyin (Element &r, const Element &a, const Element &x) const { 226 double tmp=r+a*x; 227 //return r= tmp- floor(tmp*inv_modulus)*modulus; 228 return init( r, tmp ); 226 r += a * x; 227 return init( r, r); 229 228 } 230 229 … … 238 237 protected: 239 238 float modulus; 240 float inv_modulus;241 239 float half_mod; 242 240 … … 246 244 typedef unsigned long FieldInt; 247 245 typedef ModularBalancedRandIter<float> RandIter; 246 typedef NonzeroRandIter<ModularBalanced<float>, RandIter> NonZeroRandIter; 248 247 249 248 const bool balanced; 250 249 251 ModularBalanced<float> (int p) : modulus((float)p), inv_modulus(1./(float)p),half_mod( (p-1.)/2), balanced(true) {}252 253 ModularBalanced<float>(const ModularBalanced<float>& mf) : modulus(mf.modulus), inv_modulus(mf.inv_modulus),half_mod(mf.half_mod), balanced(true){}250 ModularBalanced<float> (int p) : modulus((float)p), half_mod( (p-1.)/2), balanced(true) {} 251 252 ModularBalanced<float>(const ModularBalanced<float>& mf) : modulus(mf.modulus), half_mod(mf.half_mod), balanced(true){} 254 253 255 254 const ModularBalanced<float> &operator=(const ModularBalanced<float> &F) { 256 255 modulus = F.modulus; 257 inv_modulus = F.inv_modulus;258 256 half_mod = F.half_mod; 259 257 return *this; … … 361 359 362 360 inline Element &mul (Element &x, const Element &y, const Element &z) const { 363 float tmp= y*z; 364 //x= tmp - floor(tmp*inv_modulus)*modulus; 365 return init (x, tmp); 361 x = y*z; 362 return init (x, x); 366 363 } 367 364 368 365 inline Element &div (Element &x, const Element &y, const Element &z) const { 369 Element temp; 370 inv (temp, z); 371 return mul (x, y, temp); 366 inv (x, z); 367 return mulin (x, y); 372 368 } 373 369 … … 405 401 const Element &x, 406 402 const Element &y) const { 407 float tmp= a*x+y; 408 //return r = tmp- floor(tmp*inv_modulus)*modulus; 409 return init( r, tmp); 403 r = a * x + y; 404 return init( r, r); 410 405 411 406 } … … 443 438 444 439 inline Element &axpyin (Element &r, const Element &a, const Element &x) const { 445 float tmp=r+a*x; 446 //return r= tmp- floor(tmp*inv_modulus)*modulus; 447 return init( r, tmp ); 440 r += a * x; 441 return init( r, r); 448 442 } 449 443 static inline float getMaxModulus()
