home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Utilities / Calc / lib / mod.cal < prev    next >
Encoding:
Text File  |  1992-02-24  |  3.5 KB  |  218 lines  |  [TEXT/????]

  1. /*
  2.  * Copyright (c) 1992 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Routines to handle numbers modulo a specified number.
  7.  *    a (mod N)
  8.  */
  9.  
  10. obj mod {a};            /* definition of the object */
  11.  
  12. global mod_value;        /* modulus value (value of N) */
  13.  
  14.  
  15. define mod(a)
  16. {
  17.     local x;
  18.  
  19.     obj mod x;
  20.     if (!isreal(a) || !isint(a))
  21.         quit "Bad argument for mod function";
  22.     x.a = a % mod_value;
  23.     return x;
  24. }
  25.  
  26.  
  27. define mod_print(a)
  28. {
  29.     if (digits(mod_value) <= 20)
  30.         print a.a, "(mod", mod_value : ")" :;
  31.     else
  32.         print a.a, "(mod N)" :;
  33. }
  34.  
  35.  
  36. define mod_one()
  37. {
  38.     return mod(1);
  39. }
  40.  
  41.  
  42. define mod_cmp(a, b)
  43. {
  44.     if (isnum(a))
  45.         return (a % mod_value) != b.a;
  46.     if (isnum(b))
  47.         return (b % mod_value) != a.a;
  48.     return a.a != b.a;
  49. }
  50.  
  51.  
  52. define mod_rel(a, b)
  53. {
  54.     if (isnum(a))
  55.         a = mod(a);
  56.     if (isnum(b))
  57.         b = mod(b);
  58.     if (a.a < b.a)
  59.         return -1;
  60.     return a.a != b.a;
  61. }
  62.  
  63.  
  64. define mod_add(a, b)
  65. {
  66.     local x;
  67.  
  68.     obj mod x;
  69.     if (isnum(b)) {
  70.         if (!isint(b))
  71.             quit "Adding non-integer";
  72.         x.a = (a.a + b) % mod_value;
  73.         return x;
  74.     }
  75.     if (isnum(a)) {
  76.         if (!isint(a))
  77.             quit "Adding non-integer";
  78.         x.a = (a + b.a) % mod_value;
  79.         return x;
  80.     }
  81.     x.a = (a.a + b.a) % mod_value;
  82.     return x;
  83. }
  84.  
  85.  
  86. define mod_sub(a, b)
  87. {
  88.     return a + (-b);
  89. }
  90.  
  91.  
  92. define mod_neg(a)
  93. {
  94.     local x;
  95.  
  96.     obj mod x;
  97.     x.a = mod_value - a.a;
  98.     return x;
  99. }
  100.  
  101.  
  102. define mod_mul(a, b)
  103. {
  104.     local x;
  105.  
  106.     obj mod x;
  107.     if (isnum(b)) {
  108.         if (!isint(b))
  109.             quit "Multiplying by non-integer";
  110.         x.a = (a.a * b) % mod_value;
  111.         return x;
  112.     }
  113.     if (isnum(a)) {
  114.         if (!isint(a))
  115.             quit "Multiplying by non-integer";
  116.         x.a = (a * b.a) % mod_value;
  117.         return x;
  118.     }
  119.     x.a = (a.a * b.a) % mod_value;
  120.     return x;
  121. }
  122.  
  123.  
  124. define mod_square(a)
  125. {
  126.     local x;
  127.  
  128.     obj mod x;
  129.     x.a = a.a^2 % mod_value;
  130.     return x;
  131. }
  132.  
  133.  
  134. define mod_inc(a)
  135. {
  136.     local x;
  137.  
  138.     x = a;
  139.     if (++x.a == mod_value)
  140.         x.a = 0;
  141.     return x;
  142. }
  143.  
  144.  
  145. define mod_dec(a)
  146. {
  147.     local x;
  148.  
  149.     x = a;
  150.     if (--x.a < 0)
  151.         x.a = mod_value - 1;
  152.     return x;
  153. }
  154.  
  155.  
  156. define mod_inv(a)
  157. {
  158.     local x;
  159.  
  160.     obj mod x;
  161.     x.a = minv(a.a, mod_value);
  162.     return x;
  163. }
  164.  
  165.  
  166. define mod_div(a, b)
  167. {
  168.     local c, x, y;
  169.  
  170.     obj mod x, y;
  171.     if (isnum(a))
  172.         a = mod(a);
  173.     if (isnum(b))
  174.         b = mod(b);
  175.     c = gcd(a.a, b.a);
  176.     x.a = a.a / c;
  177.     y.a = b.a / c;
  178.     return x * inverse(y);
  179. }
  180.  
  181.  
  182. define mod_pow(a, b)
  183. {
  184.     local x, y, z;
  185.  
  186.     obj mod x;
  187.     y = a;
  188.     z = b;
  189.     if (b < 0) {
  190.         y = inverse(a);
  191.         z = -b;
  192.     }
  193.     x.a = pmod(y.a, z, mod_value);
  194.     return x;
  195. }
  196.  
  197.  
  198. mod_value = 100;        /* default */
  199.  
  200. global lib_debug;
  201. if (!isnum(lib_debug) || lib_debug>0) print "obj mod {a} defined"
  202. if (!isnum(lib_debug) || lib_debug>0) print "mod(a) defined"
  203. if (!isnum(lib_debug) || lib_debug>0) print "mod_print(a) defined"
  204. if (!isnum(lib_debug) || lib_debug>0) print "mod_one(a) defined"
  205. if (!isnum(lib_debug) || lib_debug>0) print "mod_cmp(a, b) defined"
  206. if (!isnum(lib_debug) || lib_debug>0) print "mod_rel(a, b) defined"
  207. if (!isnum(lib_debug) || lib_debug>0) print "mod_add(a, b) defined"
  208. if (!isnum(lib_debug) || lib_debug>0) print "mod_sub(a, b) defined"
  209. if (!isnum(lib_debug) || lib_debug>0) print "mod_mod(a, b) defined"
  210. if (!isnum(lib_debug) || lib_debug>0) print "mod_square(a) defined"
  211. if (!isnum(lib_debug) || lib_debug>0) print "mod_inc(a) defined"
  212. if (!isnum(lib_debug) || lib_debug>0) print "mod_dec(a) defined"
  213. if (!isnum(lib_debug) || lib_debug>0) print "mod_inv(a) defined"
  214. if (!isnum(lib_debug) || lib_debug>0) print "mod_div(a, b) defined"
  215. if (!isnum(lib_debug) || lib_debug>0) print "mod_pow(a, b) defined"
  216. if (!isnum(lib_debug) || lib_debug>0) print "mod_value defined"
  217. if (!isnum(lib_debug) || lib_debug>0) print "set mod_value as needed"
  218.