home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume27 / calc-2.9.0 / part18 < prev    next >
Text File  |  1993-12-07  |  61KB  |  2,060 lines

  1. Newsgroups: comp.sources.unix
  2. From: dbell@canb.auug.org.au (David I. Bell)
  3. Subject: v27i145: calc-2.9.0 - arbitrary precision C-like programmable calculator, Part18/19
  4. References: <1.755316719.21314@gw.home.vix.com>
  5. Sender: unix-sources-moderator@gw.home.vix.com
  6. Approved: vixie@gw.home.vix.com
  7.  
  8. Submitted-By: dbell@canb.auug.org.au (David I. Bell)
  9. Posting-Number: Volume 27, Issue 145
  10. Archive-Name: calc-2.9.0/part18
  11.  
  12. #!/bin/sh
  13. # this is part 18 of a multipart archive
  14. # do not concatenate these parts, unpack them in order with /bin/sh
  15. # file calc2.9.0/lib/lucas.cal continued
  16. #
  17. CurArch=18
  18. if test ! -r s2_seq_.tmp
  19. then echo "Please unpack part 1 first!"
  20.      exit 1; fi
  21. ( read Scheck
  22.   if test "$Scheck" != $CurArch
  23.   then echo "Please unpack part $Scheck next!"
  24.        exit 1;
  25.   else exit 0; fi
  26. ) < s2_seq_.tmp || exit 1
  27. echo "x - Continuing file calc2.9.0/lib/lucas.cal"
  28. sed 's/^X//' << 'SHAR_EOF' >> calc2.9.0/lib/lucas.cal
  29. X        quit "bad args: testval must be an integer";
  30. X    }
  31. X    if (!isint(v1)) {
  32. X        quit "bad args: v1 must be an integer";
  33. X    }
  34. X    if (testval <= 0) {
  35. X        quit "bogus arg: testval is <= 0";
  36. X    }
  37. X    if (v1 <= 0) {
  38. X        quit "bogus arg: v1 is <= 0";
  39. X    }
  40. X
  41. X    /*
  42. X     * enforce the h mod rules
  43. X     */
  44. X    if (h%2 == 0) {
  45. X        quit "h must not be even";
  46. X    }
  47. X
  48. X    /*
  49. X     * enforce the h > 0 and n >= 2 rules
  50. X     */
  51. X    if (h <= 0 || n < 1) {
  52. X        quit "reduced args violate the rule: 0 < h < 2^n";
  53. X    }
  54. X    hbits = highbit(h);
  55. X    if (hbits >= n) {
  56. X        quit "reduced args violate the rule: 0 < h < 2^n";
  57. X    }
  58. X
  59. X    /*
  60. X     * build up u2 based on the reversed bits of h
  61. X     */
  62. X    /* setup for bit loop */
  63. X    r = v1;
  64. X    s = (r^2 - 2);
  65. X
  66. X    /*
  67. X     * deal with small h as a special case
  68. X     *
  69. X     * The h value is odd > 0, and it needs to be
  70. X     * at least 2 bits long for the loop below to work.
  71. X     */
  72. X    if (h == 1) {
  73. X        ldebug("gen_u0", "quick h == 1 case");
  74. X        return r%testval;
  75. X    }
  76. X
  77. X    /* cycle from second highest bit to second lowest bit of h */
  78. X    for (i=hbits-1; i > 0; --i) {
  79. X
  80. X        /* bit(i) is 1 */
  81. X        if (isset(h,i)) {
  82. X
  83. X            /* compute v(2n+1) = v(r+1)*v(r)-v1 */
  84. X            r = (r*s - v1) % testval;
  85. X
  86. X            /* compute v(2n+2) = v(r+1)^2-2 */
  87. X            s = (s^2 - 2) % testval;
  88. X
  89. X        /* bit(i) is 0 */
  90. X        } else {
  91. X
  92. X            /* compute v(2n+1) = v(r+1)*v(r)-v1 */
  93. X            s = (r*s - v1) % testval;
  94. X
  95. X            /* compute v(2n) = v(r)^-2 */
  96. X            r = (r^2 - 2) % testval;
  97. X        }
  98. X    }
  99. X
  100. X    /* we know that h is odd, so the final bit(0) is 1 */
  101. X    r = (r*s - v1) % testval;
  102. X
  103. X    /* compute the final u2 return value */
  104. X    return r;
  105. X}
  106. X
  107. X/*
  108. X * Trial tables used by gen_v1()
  109. X *
  110. X * When h mod 3 == 0, one needs particular values of D, a and b (see gen_v1
  111. X * documentation) in order to find a value of v(1).
  112. X *
  113. X * This table defines 'quickmax' possible tests to be taken in ascending
  114. X * order.  The v1_qval[x] refers to a v(1) value from Ref1, Table 1.  A
  115. X * related D value is found in d_qval[x].  All D values expect d_qval[1]
  116. X * are also taken from Ref1, Table 1.  The case of D == 21 as listed in
  117. X * Ref1, Table 1 can be changed to D == 7 for the sake of the test because
  118. X * of {note 6}.
  119. X *
  120. X * It should be noted that the D values all satisfy the selection values
  121. X * as outlined in the gen_v1() function comments.  That is:
  122. X *
  123. X *       D == P*(2^f)*(3^g)
  124. X *
  125. X * where f == 0 and g == 0, P == D.  So we simply need to check that
  126. X * one of the following two cases are true:
  127. X *
  128. X *       P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  129. X *       P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  130. X *
  131. X * In all cases, the value of r is:
  132. X *
  133. X *       r == Q*(2^j)*(3^k)*(z^2)
  134. X *
  135. X * where Q == 1.  No further processing is needed to compute v(1) when r 
  136. X * is of this form.  
  137. X */
  138. Xquickmax = 8;
  139. Xmat d_qval[quickmax];
  140. Xmat v1_qval[quickmax];
  141. Xd_qval[0] = 5;        v1_qval[0] = 3;        /* a=1   b=1  r=4  */
  142. Xd_qval[1] = 7;        v1_qval[1] = 5;        /* a=3   b=1  r=12  D=21 */
  143. Xd_qval[2] = 13;        v1_qval[2] = 11;    /* a=3   b=1  r=4  */
  144. Xd_qval[3] = 11;        v1_qval[3] = 20;    /* a=3   b=1  r=2  */
  145. Xd_qval[4] = 29;        v1_qval[4] = 27;    /* a=5   b=1  r=4  */
  146. Xd_qval[5] = 53;        v1_qval[5] = 51;    /* a=53  b=1  r=4  */
  147. Xd_qval[6] = 17;        v1_qval[6] = 66;    /* a=17  b=1  r=1  */
  148. Xd_qval[7] = 19;        v1_qval[7] = 74;    /* a=38  b=1  r=2  */
  149. X
  150. X/*
  151. X * gen_v1 - compute the v(1) for a given h*2^n-1 if we can
  152. X *
  153. X * This function assumes:
  154. X *
  155. X *    n > 2            (n==2 has already been eliminated)
  156. X *    h mod 2 == 1
  157. X *    h < 2^n
  158. X *    h*2^n-1 mod 3 != 0    (h*2^n-1 has no small factors, such as 3)
  159. X *
  160. X * The generation of v(1) depends on the value of h.  There are two cases
  161. X * to consider, h mod 3 != 0, and h mod 3 == 0.
  162. X *
  163. X ***
  164. X *
  165. X * Case 1:    (h mod 3 != 0)
  166. X *
  167. X * This case is easy and always finds v(1).
  168. X *
  169. X * In Ref1, page 869, one finds that if:    (or see Ref2, page 131-132)
  170. X *
  171. X *    h mod 6 == +/-1
  172. X *    h*2^n-1 mod 3 != 0
  173. X *
  174. X * which translates, gives the functions assumptions, into the condition:
  175. X *
  176. X *    h mod 3 != 0
  177. X *
  178. X * If this case condition is true, then:
  179. X *
  180. X *    u(0) = (2+sqrt(3))^h + (2-sqrt(3))^h        (see Ref1, page 869)
  181. X *         = (2+sqrt(3))^h + (2+sqrt(3))^(-h)
  182. X *
  183. X * and since Ref1, Theorem 5 states:
  184. X *
  185. X *    u(0) = alpha^h + alpha^(-h)
  186. X *    r = abs(2^2 - 1^2*3) = 1
  187. X *
  188. X * and the bottom of Ref1, page 872 states:
  189. X *
  190. X *    v(x) = alpha^x + alpha^(-x)
  191. X *
  192. X * If we let:
  193. X *
  194. X *    alpha = (2+sqrt(3))
  195. X *
  196. X * then
  197. X *
  198. X *    u(0) = v(h)
  199. X *
  200. X * so we simply return
  201. X *
  202. X *    v(1) = alpha^1 + alpha^(-1)
  203. X *         = (2+sqrt(3)) + (2-sqrt(3))
  204. X *         = 4
  205. X *
  206. X ***
  207. X *
  208. X * Case 2:    (h mod 3 == 0)
  209. X *
  210. X * This case is not so easy and finds v(1) in most all cases.  In this
  211. X * version of this program, we will simply return -1 (failure) if we
  212. X * hit one of the cases that fall thru the cracks.  This does not happen
  213. X * often, so this is not too bad.
  214. X *
  215. X * Ref1, Theorem 5 contains the following definitions:
  216. X *
  217. X *        r = abs(a^2 - b^2*D)
  218. X *        alpha = (a + b*sqrt(D))^2/r
  219. X *
  220. X * where D is 'square free', and 'alpha = epsilon^s' (for some s>0) are units
  221. X * in the quadratic field K(sqrt(D)).
  222. X *
  223. X * One can find possible values for a, b and D in Ref1, Table 1 (page 872).
  224. X * (see the file lucas_tbl.cal)
  225. X *
  226. X * Now Ref1, Theorem 5 states that if:
  227. X *
  228. X *    L(D, h*2^n-1) = -1                [condition 1]
  229. X *    L(r, h*2^n-1) * (a^2 - b^2*D)/r = -1        [condition 2]
  230. X *
  231. X * where L(x,y) is the Legendre symbol (see below), then:
  232. X *
  233. X *    u(0) = alpha^h + alpha^(-h)
  234. X *
  235. X * The bottom of Ref1, page 872 states:
  236. X *
  237. X *    v(x) = alpha^x + alpha^(-x)
  238. X *
  239. X * thus since:
  240. X *
  241. X *    u(0) = v(h)
  242. X *
  243. X * so we want to return:
  244. X *
  245. X *    v(1) = alpha^1 + alpha^(-1)
  246. X *
  247. X * Therefore we need to take a given (D,a,b), determine if the two conditions
  248. X * are true, and return the related v(1).
  249. X *
  250. X * Before we address the two conditions, we need some background information
  251. X * on two symbols, Legendre and Jacobi.  In Ref 2, pp 278, 284-285, we find
  252. X * the following definitions of J(a,p) and L(a,n):
  253. X *
  254. X * The Legendre symbol L(a,p) takes the value:
  255. X *
  256. X *    L(a,p) == 1    => a is a quadratic residue of p
  257. X *    L(a,p) == -1    => a is NOT a quadratic residue of p
  258. X *
  259. X * when
  260. X *
  261. X *    p is prime
  262. X *    p mod 2 == 1
  263. X *    gcd(a,p) == 1
  264. X *
  265. X * The value x is a quadratic residue of y if there exists some integer z
  266. X * such that:
  267. X *
  268. X *    z^2 mod y == x
  269. X *
  270. X * The Jacobi symbol J(x,y) takes the value:
  271. X *
  272. X *    J(x,y) == 1    => y is not prime, or x is a quadratic residue of y
  273. X *    J(x,y) == -1    => x is NOT a quadratic residue of y
  274. X *
  275. X * when
  276. X *
  277. X *    y mod 2 == 1
  278. X *    gcd(x,y) == 1
  279. X *
  280. X * In the following comments on Legendre and Jacobi identities, we shall
  281. X * assume that the arguments to the symbolic are valid over the symbol
  282. X * definitions as stated above.
  283. X *
  284. X * In Ref2, pp 280-284, we find that:
  285. X *
  286. X *    L(a,p)*L(b,p) == L(a*b,p)                {A3.5}
  287. X *    J(x,y)*J(z,y) == J(x*z,y)                {A3.14}
  288. X *    L(a,p) == L(p,a) * (-1)^((a-1)*(p-1)/4)            {A3.8}
  289. X *    J(x,y) == J(y,x) * (-1)^((x-1)*(y-1)/4)            {A3.17}
  290. X *
  291. X * The equality L(a,p) == J(a,p) when:                {note 0}
  292. X *
  293. X *    p is prime
  294. X *    p mod 2 == 1
  295. X *    gcd(a,p) == 1
  296. X *
  297. X * It can be shown that (see Ref3):
  298. X *
  299. X *    L(a,p) == L(a mod p, p)                    {note 1}
  300. X *    L(z^2, p) == 1                        {note 2}
  301. X *
  302. X * From Ref2, table 32:
  303. X *
  304. X *    p mod 8 == +/-1   implies  L(2,p) == 1            {note 3}
  305. X *    p mod 12 == +/-1  implies  L(3,p) == 1            {note 4}
  306. X *
  307. X * Since h*2^n-1 mod 8 == -1, for n>2, note 3 implies:
  308. X *
  309. X *    L(2, h*2^n-1) == 1            (n>2)        {note 5}
  310. X *
  311. X * Since h=3*A, h*2^n-1 mod 12 == -1, for A>0, note 4 implies:
  312. X *
  313. X *    L(3, h*2^n-1) == 1                    {note 6}
  314. X *
  315. X * By use of {A3.5}, {note 2}, {note 5} and {note 6}, one can show:
  316. X *
  317. X *    L((2^g)*(3^l)*(z^2), h*2^n-1) == 1  (g>=0,l>=0,z>0,n>2)    {note 7}
  318. X *
  319. X * Returning to the testing of conditions, take condition 1:
  320. X *
  321. X *    L(D, h*2^n-1) == -1            [condition 1]
  322. X *
  323. X * In order for J(D, h*2^n-1) to be defined, we must ensure that D
  324. X * is not a factor of h*2^n-1.  This is done by pre-screening h*2^n-1 to
  325. X * not have small factors and selecting D less than that factor check limit.
  326. X *
  327. X * By use of {note 7}, we can show that when we choose D to be:
  328. X *
  329. X *    D is square free
  330. X *    D = P*(2^f)*(3^g)            (P is prime>2)
  331. X *
  332. X * The square free condition implies f = 0 or 1, g = 0 or 1.  If f and g
  333. X * are both 1, P must be a prime > 3.
  334. X *
  335. X * So given such a D value:
  336. X *
  337. X *    L(D, h*2^n-1) == L(P*(2^g)*(3^l), h*2^n-1)
  338. X *              == L(P, h*2^n-1) * L((2^g)*(3^l), h*2^n-1)       {A3.5}
  339. X *              == L(P, h*2^n-1) * 1                   {note 7}
  340. X *              == L(h*2^n-1, P)*(-1)^((h*2^n-2)*(P-1)/4)           {A3.8}
  341. X *              == L(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4)  {note 1}
  342. X *              == J(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4)  {note 0}
  343. X *
  344. X * When does J(h*2^n-1 mod P, P)*(-1)^((h*2^n-2)*(P-1)/4) take the value of -1,
  345. X * thus satisfy [condition 1]?  The answer depends on P.  Now P is a prime>2,
  346. X * thus P mod 4 == 1 or -1.
  347. X *
  348. X * Take P mod 4 == 1:
  349. X *
  350. X *    P mod 4 == 1  implies  (-1)^((h*2^n-2)*(P-1)/4) == 1
  351. X *
  352. X * Thus:
  353. X *
  354. X *    L(D, h*2^n-1) == L(h*2^n-1 mod P, P) * (-1)^((h*2^n-2)*(P-1)/4)
  355. X *              == L(h*2^n-1 mod P, P)
  356. X *              == J(h*2^n-1 mod P, P)
  357. X *
  358. X * Take P mod 4 == -1:
  359. X *
  360. X *    P mod 4 == -1  implies  (-1)^((h*2^n-2)*(P-1)/4) == -1
  361. X *
  362. X * Thus:
  363. X *
  364. X *    L(D, h*2^n-1) == L(h*2^n-1 mod P, P) * (-1)^((h*2^n-2)*(P-1)/4)
  365. X *              == L(h*2^n-1 mod P, P) * -1
  366. X *              == -J(h*2^n-1 mod P, P)
  367. X *
  368. X * Therefore [condition 1] is met if, and only if, one of the following
  369. X * to cases are true:
  370. X *
  371. X *    P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  372. X *    P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  373. X *
  374. X * Now consider [condition 2]:
  375. X *
  376. X *    L(r, h*2^n-1) * (a^2 - b^2*D)/r == -1    [condition 2]
  377. X *
  378. X * We select only a, b, r and D values where:
  379. X *
  380. X *    (a^2 - b^2*D)/r == -1
  381. X *
  382. X * Therefore in order for [condition 2] to be met, we must show that:
  383. X *
  384. X *    L(r, h*2^n-1) == 1
  385. X *
  386. X * If we select r to be of the form:
  387. X *
  388. X *    r == Q*(2^j)*(3^k)*(z^2)        (Q == 1, j>=0, k>=0, z>0)
  389. X *
  390. X * then by use of {note 7}:
  391. X *
  392. X *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  393. X *              == L((2^j)*(3^k)*(z^2), h*2^n-1)
  394. X *              == 1                           {note 2}
  395. X *
  396. X * and thus, [condition 2] is met.
  397. X *
  398. X * If we select r to be of the form:
  399. X *
  400. X *    r == Q*(2^j)*(3^k)*(z^2)        (Q is prime>2, j>=0, k>=0, z>0)
  401. X *
  402. X * then by use of {note 7}:
  403. X *
  404. X *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  405. X *              == L(Q, h*2^n-1) * L((2^j)*(3^k)*(z^2), h*2^n-1) {A3.5}
  406. X *              == L(Q, h*2^n-1) * 1                   {note 2}
  407. X *              == L(h*2^n-1, Q) * (-1)^((h*2^n-2)*(Q-1)/4)      {A3.8}
  408. X *              == L(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4)  {note 1}
  409. X *              == J(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4)  {note 0}
  410. X *
  411. X * When does J(h*2^n-1 mod Q, Q)*(-1)^((h*2^n-2)*(Q-1)/4) take the value of 1,
  412. X * thus satisfy [condition 2]?  The answer depends on Q.  Now Q is a prime>2,
  413. X * thus Q mod 4 == 1 or -1.
  414. X *
  415. X * Take Q mod 4 == 1:
  416. X *
  417. X *    Q mod 4 == 1  implies  (-1)^((h*2^n-2)*(Q-1)/4) == 1
  418. X *
  419. X * Thus:
  420. X *
  421. X *    L(D, h*2^n-1) == L(h*2^n-1 mod Q, Q) * (-1)^((h*2^n-2)*(Q-1)/4)
  422. X *              == L(h*2^n-1 mod Q, Q)
  423. X *              == J(h*2^n-1 mod Q, Q)
  424. X *
  425. X * Take Q mod 4 == -1:
  426. X *
  427. X *    Q mod 4 == -1  implies  (-1)^((h*2^n-2)*(Q-1)/4) == -1
  428. X *
  429. X * Thus:
  430. X *
  431. X *    L(D, h*2^n-1) == L(h*2^n-1 mod Q, Q) * (-1)^((h*2^n-2)*(Q-1)/4)
  432. X *              == L(h*2^n-1 mod Q, Q) * -1
  433. X *              == -J(h*2^n-1 mod Q, Q)
  434. X *
  435. X * Therefore [condition 2] is met by selecting  D = Q*(2^j)*(3^k)*(z^2),
  436. X * where Q is prime>2, j>=0, k>=0, z>0; if and only if one of the following
  437. X * to cases are true:
  438. X *
  439. X *    Q mod 4 ==  1  and  J(h*2^n-1 mod Q, Q) == 1
  440. X *    Q mod 4 == -1  and  J(h*2^n-1 mod Q, Q) == -1
  441. X *
  442. X ***
  443. X *
  444. X * In conclusion, we can compute v(1) by attempting to do the following:
  445. X *
  446. X * h mod 3 != 0
  447. X *
  448. X *     we return:
  449. X *
  450. X *       v(1) == 4
  451. X *
  452. X * h mod 3 == 0
  453. X *
  454. X *     define:
  455. X *
  456. X *       r == abs(a^2 - b^2*D)
  457. X *       alpha == (a + b*sqrt(D))^2/r
  458. X *
  459. X *     we return:
  460. X *
  461. X *       v(1) = alpha^1 + alpha^(-1)
  462. X *
  463. X *     if and only if we can find a given a, b, D that obey all the
  464. X *     following selection rules:
  465. X *
  466. X *       D is square free
  467. X *
  468. X *       D == P*(2^f)*(3^g)        (P is prime>2, f,g == 0 or 1)
  469. X *
  470. X *       (a^2 - b^2*D)/r == -1
  471. X *
  472. X *       r == Q*(2^j)*(3^k)*(z^2)    (Q==1 or Q is prime>2, j>=0, k>=0, z>0)
  473. X *
  474. X *         one of the following is true:
  475. X *           P mod 4 ==  1  and  J(h*2^n-1 mod P, P) == -1
  476. X *           P mod 4 == -1  and  J(h*2^n-1 mod P, P) ==  1
  477. X *
  478. X *       if Q is prime, then one of the following is true:
  479. X *           Q mod 4 ==  1  and  J(h*2^n-1 mod Q, Q) == 1
  480. X *           Q mod 4 == -1  and  J(h*2^n-1 mod Q, Q) == -1
  481. X *
  482. X *     If we cannot find a v(1) quickly enough, then we will give up
  483. X *     testing h*2^n-1.  This does not happen too often, so this hack
  484. X *     is not too bad.
  485. X *
  486. X ***
  487. X *
  488. X * input:
  489. X *    h    h as in h*2^n-1
  490. X *    n    n as in h*2^n-1
  491. X *
  492. X * output:
  493. X *    returns v(1), or -1 is there is no quick way
  494. X */
  495. Xdefine
  496. Xgen_v1(h, n)
  497. X{
  498. X    local d;    /* the 'D' value to try */
  499. X    local val_mod;    /* h*2^n-1 mod 'D' */
  500. X    local i;
  501. X
  502. X    /*
  503. X     * check for case 1
  504. X     */
  505. X    if (h % 3 != 0) {
  506. X        /* v(1) is easy to compute */
  507. X        return 4;
  508. X    }
  509. X
  510. X    /*
  511. X     * We will try all 'D' values until we find a proper v(1)
  512. X     * or run out of 'D' values.
  513. X     */
  514. X    for (i=0; i < quickmax; ++i) {
  515. X
  516. X        /* grab our 'D' value */
  517. X        d = d_qval[i];
  518. X
  519. X        /* compute h*2^n-1 mod 'D' quickly */
  520. X        val_mod = (h*pmod(2,n%(d-1),d)-1) % d;
  521. X
  522. X        /*
  523. X         * if 'D' mod 4 == 1, then
  524. X         *    (h*2^n-1) mod 'D' can not be a quadratic residue of 'D'
  525. X         * else
  526. X         *    (h*2^n-1) mod 'D' must be a quadratic residue of 'D'
  527. X         */
  528. X        if (d%4 == 1) {
  529. X            /* D mod 4 == 1, so check for J(D, h*2^n-1) == -1 */
  530. X            if (jacobi(val_mod, d) == -1) {
  531. X                /* it worked, return the related v(1) value */
  532. X                return v1_qval[i];
  533. X            }
  534. X        } else {
  535. X            /* D mod 4 == -1, so check for J(D, h*2^n-1) == 1 */
  536. X            if (jacobi(val_mod, d) == 1) {
  537. X                /* it worked, return the related v(1) value */
  538. X                return v1_qval[i];
  539. X            }
  540. X        }
  541. X    }
  542. X
  543. X    /*
  544. X     * This is an example of a more complex proof construction.
  545. X     * The code above will not be able to find the v(1) for:
  546. X     *
  547. X     *            81*2^81-1
  548. X     *
  549. X     * We will check with:
  550. X     *
  551. X     *    v(1)=81      D=6557      a=79      b=1      r=316
  552. X     *
  553. X     * Now, D==79*83 and r=79*2^2.  If we show that:
  554. X     *
  555. X     *    J(h*2^n-1 mod 79, 79) == -1
  556. X     *    J(h*2^n-1 mod 83, 83) == 1
  557. X     *
  558. X     * then we will satisfy [condition 1].  Observe:
  559. X     *
  560. X      *    79 mod 4 == -1  implies  (-1)^((h*2^n-2)*(79-1)/4) == -1
  561. X      *    83 mod 4 == -1  implies  (-1)^((h*2^n-2)*(83-1)/4) == -1
  562. X     *
  563. X     *    J(D, h*2^n-1) == J(83, h*2^n-1) * J(79, h*2^n-1)
  564. X     *              == J(h*2^n-1, 83) * (-1)^((h*2^n-2)*(83-1)/4) *
  565. X     *                 J(h*2^n-1, 79) * (-1)^((h*2^n-2)*(79-1)/4)
  566. X     *              == J(h*2^n-1 mod 83, 83) * -1 *
  567. X     *                 J(h*2^n-1 mod 79, 79) * -1
  568. X     *              ==  1 * -1 *
  569. X     *             -1 * -1
  570. X     *              == -1
  571. X     *
  572. X     * We will also satisfy [condition 2].  Observe:
  573. X     *
  574. X     *    (a^2 - b^2*D)/r == (79^2 - 1^1*6557)/316
  575. X     *            == -1
  576. X     *
  577. X     *    L(r, h*2^n-1) == L(Q*(2^j)*(3^k)*(z^2), h*2^n-1)
  578. X     *              == L(79, h*2^n-1) * L(2^2, h*2^n-1)
  579. X     *              == L(79, h*2^n-1) * 1
  580. X     *              == L(h*2^n-1, 79) * (-1)^((h*2^n-2)*(79-1)/4)
  581. X     *              == L(h*2^n-1, 79) * -1
  582. X     *              == L(h*2^n-1 mod 79, 79) * -1
  583. X     *              == J(h*2^n-1 mod 79, 79) * -1
  584. X     *              == -1 * -1
  585. X     *              == 1
  586. X     */
  587. X    if (jacobi( ((h*pmod(2,n%(79-1),79)-1)%79), 79 ) == -1 &&
  588. X        jacobi( ((h*pmod(2,n%(83-1),83)-1)%83), 83 ) == 1) {
  589. X        /* return the associated v(1)=81 */
  590. X        return 81;
  591. X    }
  592. X
  593. X    /* no quick and dirty v(1), so return -1 */
  594. X    return -1;
  595. X}
  596. X
  597. X/*
  598. X * ldebug - print a debug statement
  599. X *
  600. X * input:
  601. X *    funct    name of calling function
  602. X *    str    string to print
  603. X */
  604. Xdefine
  605. Xldebug(funct, str)
  606. X{
  607. X    if (lib_debug > 0) {
  608. X        print "DEBUG:", funct:":", str;
  609. X    }
  610. X    return;
  611. X}
  612. X
  613. Xglobal lib_debug;
  614. Xif (lib_debug >= 0) {
  615. X    print "lucas(h, n) defined";
  616. X}
  617. SHAR_EOF
  618. echo "File calc2.9.0/lib/lucas.cal is complete"
  619. chmod 0644 calc2.9.0/lib/lucas.cal || echo "restore of calc2.9.0/lib/lucas.cal fails"
  620. set `wc -c calc2.9.0/lib/lucas.cal`;Sum=$1
  621. if test "$Sum" != "27896"
  622. then echo original size 27896, current size $Sum;fi
  623. echo "x - extracting calc2.9.0/lib/lucas_chk.cal (Text)"
  624. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/lucas_chk.cal &&
  625. X/*
  626. X * Copyright (c) 1993 Landon Curt Noll
  627. X * Permission is granted to use, distribute, or modify this source,
  628. X * provided that this copyright notice remains intact.
  629. X *
  630. X * By: Landon Curt Noll
  631. X *     chongo@toad.com  -or-  ...!{pyramid,sun,uunet}!hoptoad!chongo
  632. X *
  633. X *
  634. X * primes of the form h*2^n-1 for 1<=h<200 and 1<=n<1000
  635. X *
  636. X * For all 0 <= i < prime_cnt, h_p[i]*2^n_p[i]-1 is prime.
  637. X *
  638. X * These values were taken from:
  639. X *
  640. X *    "Prime numbers and Computer Methods for Factorization", by Hans Riesel,
  641. X *    Birkhauser, 1985, pp 384-387.
  642. X *
  643. X * This routine assumes that the file "lucas.cal" has been loaded.
  644. X *
  645. X * NOTE: There are several errors in Riesel's table that have been corrected
  646. X *     in this file:
  647. X *
  648. X *        193*2^87-1 is prime
  649. X *        193*2^97-1 is NOT prime
  650. X *        199*2^211-1 is prime
  651. X *        199*2^221-1 is NOT prime
  652. X */
  653. X
  654. Xstatic prime_cnt = 1145;    /* number of primes in the list */
  655. X
  656. X/* h = prime parameters */
  657. Xstatic mat h_p[prime_cnt] = {
  658. X    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,            /* element 0 */
  659. X    1, 1, 1, 1, 3, 3, 3, 3, 3, 3,
  660. X    3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  661. X    3, 3, 3, 3, 3, 3, 3, 3, 3, 5,
  662. X    5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  663. X    5, 5, 5, 5, 5, 5, 7, 7, 7, 7,
  664. X    7, 7, 7, 7, 9, 9, 9, 9, 9, 9,
  665. X    9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
  666. X    9, 9, 9, 11, 11, 11, 11, 11, 11, 11,
  667. X    11, 11, 11, 13, 13, 13, 13, 13, 13, 15,
  668. X    15, 15, 15, 15, 15, 15, 15, 15, 15, 15,        /* 100 */
  669. X    15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  670. X    15, 15, 17, 17, 17, 17, 17, 17, 17, 17,
  671. X    17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
  672. X    17, 17, 19, 19, 19, 19, 19, 19, 19, 19,
  673. X    19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
  674. X    19, 19, 21, 21, 21, 21, 21, 21, 21, 21,
  675. X    21, 21, 21, 21, 21, 21, 21, 21, 23, 23,
  676. X    23, 23, 23, 23, 23, 23, 23, 25, 25, 25,
  677. X    25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  678. X    25, 25, 25, 27, 27, 27, 27, 27, 27, 27,        /* 200 */
  679. X    27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  680. X    27, 27, 27, 27, 27, 27, 27, 29, 29, 29,
  681. X    29, 29, 31, 31, 31, 31, 31, 31, 31, 31,
  682. X    31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
  683. X    33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
  684. X    33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
  685. X    33, 33, 33, 33, 35, 35, 35, 35, 35, 35,
  686. X    35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  687. X    35, 37, 39, 39, 39, 39, 39, 39, 39, 39,
  688. X    39, 41, 41, 41, 41, 41, 41, 41, 41, 41,        /* 300 */
  689. X    41, 41, 41, 41, 43, 43, 43, 43, 43, 45,
  690. X    45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  691. X    45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  692. X    45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  693. X    45, 45, 45, 45, 45, 47, 47, 47, 47, 49,
  694. X    49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
  695. X    49, 49, 49, 49, 49, 49, 51, 51, 51, 51,
  696. X    51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
  697. X    51, 53, 53, 53, 53, 53, 53, 53, 53, 53,
  698. X    53, 55, 55, 55, 55, 55, 55, 55, 55, 55,        /* 400 */
  699. X    55, 55, 55, 55, 55, 55, 55, 55, 55, 55,
  700. X    57, 57, 57, 57, 57, 57, 57, 57, 57, 57,
  701. X    57, 57, 57, 57, 57, 57, 57, 57, 59, 59,
  702. X    59, 59, 59, 59, 61, 61, 61, 61, 61, 61,
  703. X    61, 61, 61, 61, 61, 61, 61, 61, 61, 61,
  704. X    61, 63, 63, 63, 63, 63, 63, 63, 63, 63,
  705. X    63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
  706. X    63, 63, 63, 63, 65, 65, 65, 65, 65, 65,
  707. X    65, 65, 65, 65, 65, 65, 65, 65, 65, 65,
  708. X    65, 65, 67, 67, 67, 67, 67, 67, 67, 67,        /* 500 */
  709. X    69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
  710. X    69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
  711. X    69, 69, 69, 69, 69, 69, 69, 69, 69, 69,
  712. X    69, 69, 71, 71, 71, 73, 73, 73, 73, 73,
  713. X    73, 75, 75, 75, 75, 75, 75, 75, 75, 75,
  714. X    75, 75, 75, 75, 75, 75, 75, 75, 75, 75,
  715. X    75, 75, 75, 75, 75, 75, 75, 77, 77, 77,
  716. X    77, 77, 77, 77, 77, 77, 77, 77, 77, 79,
  717. X    79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
  718. X    81, 81, 81, 81, 81, 81, 81, 81, 81, 81,        /* 600 */
  719. X    81, 81, 81, 83, 83, 83, 83, 83, 83, 83,
  720. X    83, 83, 83, 83, 83, 83, 83, 83, 83, 83,
  721. X    83, 83, 83, 83, 83, 85, 85, 85, 85, 85,
  722. X    85, 85, 85, 85, 87, 87, 87, 87, 87, 87,
  723. X    87, 87, 87, 87, 87, 87, 87, 87, 87, 87,
  724. X    87, 87, 87, 87, 87, 87, 89, 89, 89, 89,
  725. X    89, 89, 89, 89, 89, 91, 91, 91, 91, 91,
  726. X    91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
  727. X    91, 91, 91, 91, 91, 91, 91, 93, 93, 93,
  728. X    93, 93, 93, 93, 93, 93, 93, 93, 93, 93,        /* 700 */
  729. X    93, 93, 93, 93, 93, 95, 95, 95, 95, 95,
  730. X    95, 95, 95, 95, 95, 97, 97, 97, 97, 97,
  731. X    99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
  732. X    99, 99, 99, 99, 99, 99, 101, 101, 101, 101,
  733. X    103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
  734. X    103, 103, 103, 105, 105, 105, 105, 105, 105, 105,
  735. X    105, 105, 105, 105, 105, 105, 105, 105, 105, 105,
  736. X    105, 105, 107, 107, 107, 107, 107, 107, 107, 107,
  737. X    107, 107, 107, 107, 107, 107, 109, 109, 109, 109,
  738. X    109, 113, 113, 113, 113, 113, 113, 113, 113, 113,    /* 800 */
  739. X    113, 115, 115, 115, 115, 115, 115, 115, 115, 115,
  740. X    115, 115, 115, 115, 115, 115, 115, 119, 119, 119,
  741. X    119, 119, 119, 119, 119, 121, 121, 121, 121, 121,
  742. X    121, 121, 121, 121, 121, 121, 121, 125, 125, 125,
  743. X    125, 125, 125, 127, 127, 131, 131, 131, 131, 131,
  744. X    131, 131, 131, 131, 131, 133, 133, 133, 133, 133,
  745. X    133, 133, 133, 133, 133, 133, 133, 133, 137, 137,
  746. X    137, 137, 139, 139, 139, 139, 139, 139, 139, 139,
  747. X    139, 139, 139, 139, 139, 139, 139, 139, 139, 139,
  748. X    139, 139, 139, 139, 139, 139, 139, 139, 139, 143,    /* 900 */
  749. X    143, 143, 143, 143, 143, 143, 143, 143, 143, 143,
  750. X    143, 143, 143, 143, 143, 143, 143, 143, 143, 143,
  751. X    143, 143, 143, 145, 145, 145, 145, 145, 145, 145,
  752. X    145, 145, 145, 145, 149, 149, 149, 149, 149, 149,
  753. X    149, 151, 151, 151, 155, 155, 155, 155, 155, 155,
  754. X    155, 155, 155, 155, 155, 155, 157, 157, 157, 157,
  755. X    157, 157, 157, 157, 157, 161, 161, 161, 161, 161,
  756. X    161, 161, 161, 161, 161, 161, 161, 161, 161, 161,
  757. X    163, 163, 163, 163, 167, 167, 167, 167, 167, 167,
  758. X    167, 167, 167, 167, 167, 167, 169, 169, 169, 169,    /* 1000 */
  759. X    169, 169, 169, 169, 169, 169, 169, 169, 173, 173,
  760. X    173, 173, 173, 173, 173, 173, 173, 173, 173, 173,
  761. X    173, 173, 173, 173, 175, 175, 175, 175, 175, 175,
  762. X    175, 175, 175, 175, 175, 175, 175, 175, 175, 175,
  763. X    179, 179, 179, 181, 181, 181, 181, 181, 181, 181,
  764. X    181, 181, 181, 181, 181, 181, 181, 181, 181, 181,
  765. X    181, 181, 181, 181, 181, 181, 181, 181, 185, 185,
  766. X    185, 185, 185, 185, 185, 185, 185, 185, 187, 187,
  767. X    187, 187, 187, 191, 193, 193, 193, 193, 193, 193,
  768. X    193, 197, 197, 197, 197, 197, 197, 197, 197, 197,    /* 1100 */
  769. X    197, 197, 197, 197, 197, 197, 197, 197, 197, 199,
  770. X    199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
  771. X    199, 199, 199, 199, 199, 199, 199, 199, 199, 199,
  772. X    199, 199, 199, 199, 199
  773. X};
  774. X
  775. X
  776. X/* n (exponent) prime parameters */
  777. Xstatic mat n_p[prime_cnt] = {
  778. X    2, 3, 5, 7, 13, 17, 19, 31, 61, 89,        /* element 0 */
  779. X    107, 127, 521, 607, 1, 2, 3, 4, 6, 7,
  780. X    11, 18, 34, 38, 43, 55, 64, 76, 94, 103,
  781. X    143, 206, 216, 306, 324, 391, 458, 470, 827, 2,
  782. X    4, 8, 10, 12, 14, 18, 32, 48, 54, 72,
  783. X    148, 184, 248, 270, 274, 420, 1, 5, 9, 17,
  784. X    21, 29, 45, 177, 1, 3, 7, 13, 15, 21,
  785. X    43, 63, 99, 109, 159, 211, 309, 343, 415, 469,
  786. X    781, 871, 939, 2, 26, 50, 54, 126, 134, 246,
  787. X    354, 362, 950, 3, 7, 23, 287, 291, 795, 1,
  788. X    2, 4, 5, 10, 14, 17, 31, 41, 73, 80,        /* 100 */
  789. X    82, 116, 125, 145, 157, 172, 202, 224, 266, 289,
  790. X    293, 463, 2, 4, 6, 16, 20, 36, 54, 60,
  791. X    96, 124, 150, 252, 356, 460, 612, 654, 664, 698,
  792. X    702, 972, 1, 3, 5, 21, 41, 49, 89, 133,
  793. X    141, 165, 189, 293, 305, 395, 651, 665, 771, 801,
  794. X    923, 953, 1, 2, 3, 7, 10, 13, 18, 27,
  795. X    37, 51, 74, 157, 271, 458, 530, 891, 4, 6,
  796. X    12, 46, 72, 244, 264, 544, 888, 3, 9, 11,
  797. X    17, 23, 35, 39, 75, 105, 107, 155, 215, 335,
  798. X    635, 651, 687, 1, 2, 4, 5, 8, 10, 14,        /* 200 */
  799. X    28, 37, 38, 70, 121, 122, 160, 170, 253, 329,
  800. X    362, 454, 485, 500, 574, 892, 962, 4, 16, 76,
  801. X    148, 184, 1, 5, 7, 11, 13, 23, 33, 35,
  802. X    37, 47, 115, 205, 235, 271, 409, 739, 837, 887,
  803. X    2, 3, 6, 8, 10, 22, 35, 42, 43, 46,
  804. X    56, 91, 102, 106, 142, 190, 208, 266, 330, 360,
  805. X    382, 462, 503, 815, 2, 6, 10, 20, 44, 114,
  806. X    146, 156, 174, 260, 306, 380, 654, 686, 702, 814,
  807. X    906, 1, 3, 24, 105, 153, 188, 605, 795, 813,
  808. X    839, 2, 10, 14, 18, 50, 114, 122, 294, 362,    /* 300 */
  809. X    554, 582, 638, 758, 7, 31, 67, 251, 767, 1,
  810. X    2, 3, 4, 5, 6, 8, 9, 14, 15, 16,
  811. X    22, 28, 29, 36, 37, 54, 59, 85, 93, 117,
  812. X    119, 161, 189, 193, 256, 308, 322, 327, 411, 466,
  813. X    577, 591, 902, 928, 946, 4, 14, 70, 78, 1,
  814. X    5, 7, 9, 13, 15, 29, 33, 39, 55, 81,
  815. X    95, 205, 279, 581, 807, 813, 1, 9, 10, 19,
  816. X    22, 57, 69, 97, 141, 169, 171, 195, 238, 735,
  817. X    885, 2, 6, 8, 42, 50, 62, 362, 488, 642,
  818. X    846, 1, 3, 5, 7, 15, 33, 41, 57, 69,        /* 400 */
  819. X    75, 77, 131, 133, 153, 247, 305, 351, 409, 471,
  820. X    1, 2, 4, 5, 8, 10, 20, 22, 25, 26,
  821. X    32, 44, 62, 77, 158, 317, 500, 713, 12, 16,
  822. X    72, 160, 256, 916, 3, 5, 9, 13, 17, 19,
  823. X    25, 39, 63, 67, 75, 119, 147, 225, 419, 715,
  824. X    895, 2, 3, 8, 11, 14, 16, 28, 32, 39,
  825. X    66, 68, 91, 98, 116, 126, 164, 191, 298, 323,
  826. X    443, 714, 758, 759, 4, 6, 12, 22, 28, 52,
  827. X    78, 94, 124, 162, 174, 192, 204, 304, 376, 808,
  828. X    930, 972, 5, 9, 21, 45, 65, 77, 273, 677,    /* 500 */
  829. X    1, 4, 5, 7, 9, 11, 13, 17, 19, 23,
  830. X    29, 37, 49, 61, 79, 99, 121, 133, 141, 164,
  831. X    173, 181, 185, 193, 233, 299, 313, 351, 377, 540,
  832. X    569, 909, 2, 14, 410, 7, 11, 19, 71, 79,
  833. X    131, 1, 3, 5, 6, 18, 19, 20, 22, 28,
  834. X    29, 39, 43, 49, 75, 85, 92, 111, 126, 136,
  835. X    159, 162, 237, 349, 381, 767, 969, 2, 4, 14,
  836. X    26, 58, 60, 64, 100, 122, 212, 566, 638, 1,
  837. X    3, 7, 15, 43, 57, 61, 75, 145, 217, 247,
  838. X    3, 5, 11, 17, 21, 27, 81, 101, 107, 327,    /* 600 */
  839. X    383, 387, 941, 2, 4, 8, 10, 14, 18, 22,
  840. X    24, 26, 28, 36, 42, 58, 64, 78, 158, 198,
  841. X    206, 424, 550, 676, 904, 5, 11, 71, 113, 115,
  842. X    355, 473, 563, 883, 1, 2, 8, 9, 10, 12,
  843. X    22, 29, 32, 50, 57, 69, 81, 122, 138, 200,
  844. X    296, 514, 656, 682, 778, 881, 4, 8, 12, 24,
  845. X    48, 52, 64, 84, 96, 1, 3, 9, 13, 15,
  846. X    17, 19, 23, 47, 57, 67, 73, 77, 81, 83,
  847. X    191, 301, 321, 435, 867, 869, 917, 3, 4, 7,
  848. X    10, 15, 18, 19, 24, 27, 39, 60, 84, 111,    /* 700 */
  849. X    171, 192, 222, 639, 954, 2, 6, 26, 32, 66,
  850. X    128, 170, 288, 320, 470, 1, 9, 45, 177, 585,
  851. X    1, 4, 5, 7, 8, 11, 19, 25, 28, 35,
  852. X    65, 79, 212, 271, 361, 461, 10, 18, 54, 70,
  853. X    3, 7, 11, 19, 63, 75, 95, 127, 155, 163,
  854. X    171, 283, 563, 2, 3, 5, 6, 8, 9, 25,
  855. X    32, 65, 113, 119, 155, 177, 299, 335, 426, 462,
  856. X    617, 896, 10, 12, 18, 24, 28, 40, 90, 132,
  857. X    214, 238, 322, 532, 858, 940, 9, 149, 177, 419,
  858. X    617, 8, 14, 74, 80, 274, 334, 590, 608, 614,    /* 800 */
  859. X    650, 1, 3, 11, 13, 19, 21, 31, 49, 59,
  860. X    69, 73, 115, 129, 397, 623, 769, 12, 16, 52,
  861. X    160, 192, 216, 376, 436, 1, 3, 21, 27, 37,
  862. X    43, 91, 117, 141, 163, 373, 421, 2, 4, 44,
  863. X    182, 496, 904, 25, 113, 2, 14, 34, 38, 42,
  864. X    78, 90, 178, 778, 974, 3, 11, 15, 19, 31,
  865. X    59, 75, 103, 163, 235, 375, 615, 767, 2, 18,
  866. X    38, 62, 1, 5, 7, 9, 15, 19, 21, 35,
  867. X    37, 39, 41, 49, 69, 111, 115, 141, 159, 181,
  868. X    201, 217, 487, 567, 677, 765, 811, 841, 917, 2,    /* 900 */
  869. X    4, 6, 8, 12, 18, 26, 32, 34, 36, 42,
  870. X    60, 78, 82, 84, 88, 154, 174, 208, 256, 366,
  871. X    448, 478, 746, 5, 13, 15, 31, 77, 151, 181,
  872. X    245, 445, 447, 883, 4, 16, 48, 60, 240, 256,
  873. X    304, 5, 221, 641, 2, 8, 14, 16, 44, 46,
  874. X    82, 172, 196, 254, 556, 806, 1, 5, 33, 121,
  875. X    125, 305, 445, 473, 513, 2, 6, 18, 22, 34,
  876. X    54, 98, 122, 146, 222, 306, 422, 654, 682, 862,
  877. X    3, 31, 63, 303, 4, 6, 8, 10, 16, 32,
  878. X    38, 42, 52, 456, 576, 668, 1, 5, 11, 17,    /* 1000 */
  879. X    67, 137, 157, 203, 209, 227, 263, 917, 2, 4,
  880. X    6, 16, 32, 50, 76, 80, 96, 104, 162, 212,
  881. X    230, 260, 480, 612, 1, 3, 9, 21, 23, 41,
  882. X    47, 57, 69, 83, 193, 249, 291, 421, 433, 997,
  883. X    8, 68, 108, 3, 5, 7, 9, 11, 17, 23,
  884. X    31, 35, 43, 47, 83, 85, 99, 101, 195, 267,
  885. X    281, 363, 391, 519, 623, 653, 673, 701, 2, 6,
  886. X    10, 18, 26, 40, 46, 78, 230, 542, 1, 17,
  887. X    21, 53, 253, 226, 3, 15, 27, 63, 87, 135,
  888. X    543, 2, 16, 20, 22, 40, 82, 112, 178, 230,    /* 1100 */
  889. X    302, 304, 328, 374, 442, 472, 500, 580, 694, 1,
  890. X    5, 7, 15, 19, 23, 25, 27, 43, 65, 99,
  891. X    125, 141, 165, 201, 211, 331, 369, 389, 445, 461,
  892. X    463, 467, 513, 583, 835
  893. X};
  894. X
  895. X
  896. Xglobal    lib_debug;    /* from lucas.cal */
  897. X
  898. X
  899. X/*
  900. X * lucas_chk - check the lucas function on known primes
  901. X *
  902. X * This function tests entries in the above h_p, n_p table
  903. X * when n_p is below a given limit.
  904. X *
  905. X * input:
  906. X *    high_n    skip tests on n_p[i] > high_n
  907. X *
  908. X * returns:
  909. X *    1    all is ok
  910. X *    0    something went wrong
  911. X */
  912. Xdefine
  913. Xlucas_chk(high_n)
  914. X{
  915. X    local i;    /* index */
  916. X    local result;    /* 0 => non-prime, 1 => prime, -1 => bad test */
  917. X    local error;    /* number of errors and bad tests found */
  918. X
  919. X    /*
  920. X     * firewall
  921. X     */
  922. X    if (!isint(high_n)) {
  923. X        ldebug("test_lucas", "high_n is non-int");
  924. X        quit "FATAL: bad args: high_n must be an integer";
  925. X    }
  926. X
  927. X    /*
  928. X     * scan thru the above prime table
  929. X     */
  930. X    error = 0;
  931. X    for (i=0; i < prime_cnt; ++i) {
  932. X
  933. X        /* skip primes where h>=2^n */
  934. X        if (highbit(h_p[i]) >= n_p[i]) {
  935. X            if (lib_debug > 0) {
  936. X                print "h>=2^n skip:", h_p[i]:"*2^":n_p[i]:"-1";
  937. X            }
  938. X            continue;
  939. X        }
  940. X
  941. X        /* test the prime if it is small enough */
  942. X        if (n_p[i] <= high_n) {
  943. X
  944. X            /* test the table value */
  945. X            result = lucas(h_p[i], n_p[i]);
  946. X
  947. X            /* report the test */
  948. X            if (result == 0) {
  949. X                print "ERROR, bad primality test of",\
  950. X                    h_p[i]:"*2^":n_p[i]:"-1";
  951. X                ++error;
  952. X            } else if (result == 1) {
  953. X                print h_p[i]:"*2^":n_p[i]:"-1 is prime";
  954. X            } else if (result == -1) {
  955. X                print "ERROR, failed to compute v(1) for",\
  956. X                    h_p[i]:"*2^":n_p[i]:"-1";
  957. X                ++error;
  958. X            } else {
  959. X                print "ERROR, bogus return value:", result;
  960. X                ++error;
  961. X            }
  962. X        }
  963. X    }
  964. X
  965. X    /* return the full status */
  966. X    if (error == 0) {
  967. X        print "lucas_chk(":high_n:") passed";
  968. X        return 1;
  969. X    } else if (error == 1) {
  970. X        print "lucas_chk(":high_n:") failed", error, "test";
  971. X        return 0;
  972. X    } else {
  973. X        print "lucas_chk(":high_n:") failed", error, "tests";
  974. X        return 0;
  975. X    }
  976. X}
  977. X
  978. Xglobal lib_debug;
  979. Xif (lib_debug >= 0) {
  980. X    print "lucas_chk(high_n) defined";
  981. X}
  982. SHAR_EOF
  983. chmod 0644 calc2.9.0/lib/lucas_chk.cal || echo "restore of calc2.9.0/lib/lucas_chk.cal fails"
  984. set `wc -c calc2.9.0/lib/lucas_chk.cal`;Sum=$1
  985. if test "$Sum" != "13089"
  986. then echo original size 13089, current size $Sum;fi
  987. echo "x - extracting calc2.9.0/lib/lucas_tbl.cal (Text)"
  988. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/lucas_tbl.cal &&
  989. X/*
  990. X * Copyright (c) 1993 Landon Curt Noll
  991. X * Permission is granted to use, distribute, or modify this source,
  992. X * provided that this copyright notice remains intact.
  993. X *
  994. X * By: Landon Curt Noll
  995. X *     chongo@toad.com  -or-  ...!{pyramid,sun,uunet}!hoptoad!chongo
  996. X *
  997. X *
  998. X * Lucasian criteria for primality
  999. X *
  1000. X * The following table is taken from:
  1001. X *
  1002. X *    "Lucasian Criteria for the Primality of N=h*2^n-1", by Hans Riesel,
  1003. X *    Mathematics of Computation, Vol 23 #108, p 872.
  1004. X *
  1005. X * The index of the *_val[] arrays correspond to the v(1) values found
  1006. X * in the table.  That is, for v(1) == x:
  1007. X *
  1008. X *    D == d_val[x]
  1009. X *    a == a_val[x]
  1010. X *    b == b_val[x]
  1011. X *    r == r_val[x]        (r == abs(a^2 - b^2*D))
  1012. X *
  1013. X *
  1014. X * Note that when *_val[i] is not a number, the related v(1) value
  1015. X * is not found in Table 1.
  1016. X */
  1017. X
  1018. Xtrymax = 100;
  1019. Xmat d_val[trymax+1];
  1020. Xmat a_val[trymax+1];
  1021. Xmat b_val[trymax+1];
  1022. Xmat r_val[trymax+1];
  1023. X/* v1= 0        INVALID */
  1024. X/* v1= 1        INVALID */
  1025. X/* v1= 2        INVALID */
  1026. Xd_val[ 3]=   5;  a_val[ 3]= 1;  b_val[ 3]=1;  r_val[ 3]=4;
  1027. Xd_val[ 4]=   3;  a_val[ 4]= 1;  b_val[ 4]=1;  r_val[ 4]=2;
  1028. Xd_val[ 5]=  21;  a_val[ 5]= 3;  b_val[ 5]=1;  r_val[ 5]=12;
  1029. Xd_val[ 6]=   2;  a_val[ 6]= 1;  b_val[ 6]=1;  r_val[ 6]=1;
  1030. X/* v1= 7        INVALID */
  1031. Xd_val[ 8]=  15;  a_val[ 8]= 3;  b_val[ 8]=1;  r_val[ 8]=6;
  1032. Xd_val[ 9]=  77;  a_val[ 9]= 7;  b_val[ 9]=1;  r_val[ 9]=28;
  1033. Xd_val[10]=   6;  a_val[10]= 2;  b_val[10]=1;  r_val[10]=2;
  1034. Xd_val[11]=  13;  a_val[11]= 3;  b_val[11]=1;  r_val[11]=4;
  1035. Xd_val[12]=  35;  a_val[12]= 5;  b_val[12]=1;  r_val[12]=10;
  1036. Xd_val[13]= 165;  a_val[13]=11;  b_val[13]=1;  r_val[13]=44;
  1037. X/* v1=14        INVALID */
  1038. Xd_val[15]= 221;  a_val[15]=13;  b_val[15]=1;  r_val[15]=52;
  1039. Xd_val[16]=   7;  a_val[16]= 3;  b_val[16]=1;  r_val[16]=2;
  1040. Xd_val[17]= 285;  a_val[17]=15;  b_val[17]=1;  r_val[17]=60;
  1041. X/* v1=18        INVALID */
  1042. Xd_val[19]= 357;  a_val[19]=17;  b_val[19]=1;  r_val[19]=68;
  1043. Xd_val[20]=  11;  a_val[20]= 3;  b_val[20]=1;  r_val[20]=2;
  1044. Xd_val[21]= 437;  a_val[21]=19;  b_val[21]=1;  r_val[21]=76;
  1045. Xd_val[22]=  30;  a_val[22]= 5;  b_val[22]=1;  r_val[22]=5;
  1046. X/* v1=23        INVALID */
  1047. Xd_val[24]= 143;  a_val[24]=11;  b_val[24]=1;  r_val[24]=22;
  1048. Xd_val[25]=  69;  a_val[25]= 9;  b_val[25]=1;  r_val[25]=12;
  1049. Xd_val[26]=  42;  a_val[26]= 6;  b_val[26]=1;  r_val[26]=6;
  1050. Xd_val[27]=  29;  a_val[27]= 5;  b_val[27]=1;  r_val[27]=4;
  1051. Xd_val[28]= 195;  a_val[28]=13;  b_val[28]=1;  r_val[28]=26;
  1052. Xd_val[29]=  93;  a_val[29]= 9;  b_val[29]=1;  r_val[29]=12;
  1053. Xd_val[30]=  14;  a_val[30]= 4;  b_val[30]=1;  r_val[30]=2;
  1054. Xd_val[31]= 957;  a_val[31]=29;  b_val[31]=1;  r_val[31]=116;
  1055. Xd_val[32]= 255;  a_val[32]=15;  b_val[32]=1;  r_val[32]=30;
  1056. Xd_val[33]=1085;  a_val[33]=31;  b_val[33]=1;  r_val[33]=124;
  1057. X/* v1=34        INVALID */
  1058. Xd_val[35]=1221;  a_val[35]=33;  b_val[35]=1;  r_val[35]=132;
  1059. Xd_val[36]= 323;  a_val[36]=17;  b_val[36]=1;  r_val[36]=34;
  1060. Xd_val[37]=1365;  a_val[37]=35;  b_val[37]=1;  r_val[37]=140;
  1061. Xd_val[38]=  10;  a_val[38]= 3;  b_val[38]=1;  r_val[38]=1;
  1062. Xd_val[39]=1517;  a_val[39]=37;  b_val[39]=1;  r_val[39]=148;
  1063. Xd_val[40]= 399;  a_val[40]=19;  b_val[40]=1;  r_val[40]=38;
  1064. Xd_val[41]=1677;  a_val[41]=39;  b_val[41]=1;  r_val[41]=156;
  1065. Xd_val[42]= 110;  a_val[42]=10;  b_val[42]=1;  r_val[42]=10;
  1066. Xd_val[43]= 205;  a_val[43]=15;  b_val[43]=1;  r_val[43]=20;
  1067. Xd_val[44]= 483;  a_val[44]=21;  b_val[44]=1;  r_val[44]=42;
  1068. Xd_val[45]=2021;  a_val[45]=43;  b_val[45]=1;  r_val[45]=172;
  1069. Xd_val[46]=  33;  a_val[46]= 6;  b_val[46]=1;  r_val[46]=3;
  1070. X/* v1=47        INVALID */
  1071. Xd_val[48]=  23;  a_val[48]= 5;  b_val[48]=1;  r_val[48]=2;
  1072. Xd_val[49]=2397;  a_val[49]=47;  b_val[49]=1;  r_val[49]=188;
  1073. Xd_val[50]=  39;  a_val[50]= 6;  b_val[50]=1;  r_val[50]=3;
  1074. Xd_val[51]=  53;  a_val[51]= 7;  b_val[51]=1;  r_val[51]=4;
  1075. X/* v1=52        INVALID */
  1076. Xd_val[53]=2805;  a_val[53]=51;  b_val[53]=1;  r_val[53]=204;
  1077. Xd_val[54]= 182;  a_val[54]=13;  b_val[54]=1;  r_val[54]=13;
  1078. Xd_val[55]=3021;  a_val[55]=53;  b_val[55]=1;  r_val[55]=212;
  1079. Xd_val[56]=  87;  a_val[56]= 9;  b_val[56]=1;  r_val[56]=6;
  1080. Xd_val[57]=3245;  a_val[57]=55;  b_val[57]=1;  r_val[57]=220;
  1081. Xd_val[58]= 210;  a_val[58]=14;  b_val[58]=1;  r_val[58]=14;
  1082. Xd_val[59]=3477;  a_val[59]=57;  b_val[59]=1;  r_val[59]=228;
  1083. Xd_val[60]= 899;  a_val[60]=29;  b_val[60]=1;  r_val[60]=58;
  1084. Xd_val[61]= 413;  a_val[61]=21;  b_val[61]=1;  r_val[61]=28;
  1085. X/* v1=62        INVALID */
  1086. Xd_val[63]=3965;  a_val[63]=61;  b_val[63]=1;  r_val[63]=244;
  1087. Xd_val[64]=1023;  a_val[64]=31;  b_val[64]=1;  r_val[64]=62;
  1088. Xd_val[65]= 469;  a_val[65]=21;  b_val[65]=1;  r_val[65]=28;
  1089. Xd_val[66]=  17;  a_val[66]= 4;  b_val[66]=1;  r_val[66]=1;
  1090. Xd_val[67]=4485;  a_val[67]=65;  b_val[67]=1;  r_val[67]=260;
  1091. Xd_val[68]=1155;  a_val[68]=33;  b_val[68]=1;  r_val[68]=66;
  1092. Xd_val[69]=4757;  a_val[69]=67;  b_val[69]=1;  r_val[69]=268;
  1093. Xd_val[70]=  34;  a_val[70]= 6;  b_val[70]=1;  r_val[70]=2;
  1094. Xd_val[71]=5037;  a_val[71]=69;  b_val[71]=1;  r_val[71]=276;
  1095. Xd_val[72]=1295;  a_val[72]=35;  b_val[72]=1;  r_val[72]=70;
  1096. Xd_val[73]= 213;  a_val[73]=15;  b_val[73]=1;  r_val[73]=12;
  1097. Xd_val[74]=  38;  a_val[74]= 6;  b_val[74]=1;  r_val[74]=2;
  1098. Xd_val[75]=5621;  a_val[75]=73;  b_val[75]=1;  r_val[75]=292;
  1099. Xd_val[76]=1443;  a_val[76]=37;  b_val[76]=1;  r_val[76]=74;
  1100. Xd_val[77]= 237;  a_val[77]=15;  b_val[77]=1;  r_val[77]=12;
  1101. Xd_val[78]=  95;  a_val[78]=10;  b_val[78]=1;  r_val[78]=5;
  1102. X/* v1=79        INVALID */
  1103. Xd_val[80]=1599;  a_val[80]=39;  b_val[80]=1;  r_val[80]=78;
  1104. Xd_val[81]=6557;  a_val[81]=79;  b_val[81]=1;  r_val[81]=316;
  1105. Xd_val[82]= 105;  a_val[82]=10;  b_val[82]=1;  r_val[82]=5;
  1106. Xd_val[83]=  85;  a_val[83]= 9;  b_val[83]=1;  r_val[83]=4;
  1107. Xd_val[84]=1763;  a_val[84]=41;  b_val[84]=1;  r_val[84]=82;
  1108. Xd_val[85]=7221;  a_val[85]=83;  b_val[85]=1;  r_val[85]=332;
  1109. Xd_val[86]= 462;  a_val[86]=21;  b_val[86]=1;  r_val[86]=21;
  1110. Xd_val[87]=7565;  a_val[87]=85;  b_val[87]=1;  r_val[87]=340;
  1111. Xd_val[88]= 215;  a_val[88]=15;  b_val[88]=1;  r_val[88]=10;
  1112. Xd_val[89]=7917;  a_val[89]=87;  b_val[89]=1;  r_val[89]=348;
  1113. Xd_val[90]= 506;  a_val[90]=22;  b_val[90]=1;  r_val[90]=22;
  1114. Xd_val[91]=8277;  a_val[91]=89;  b_val[91]=1;  r_val[91]=356;
  1115. Xd_val[92]= 235;  a_val[92]=15;  b_val[92]=1;  r_val[92]=10;
  1116. Xd_val[93]=8645;  a_val[93]=91;  b_val[93]=1;  r_val[93]=364;
  1117. Xd_val[94]= 138;  a_val[94]=12;  b_val[94]=1;  r_val[94]=6;
  1118. Xd_val[95]=9021;  a_val[95]=93;  b_val[95]=1;  r_val[95]=372;
  1119. Xd_val[96]=  47;  a_val[96]= 7;  b_val[96]=1;  r_val[96]=2;
  1120. Xd_val[97]=1045;  a_val[97]=33;  b_val[97]=1;  r_val[97]=44;
  1121. X/* v1=98        INVALID */
  1122. Xd_val[99]=9797;  a_val[99]=97;  b_val[99]=1;  r_val[99]=388;
  1123. Xd_val[100]=  51; a_val[100]= 7; b_val[100]=1; r_val[100]=2;
  1124. X
  1125. Xglobal lib_debug;
  1126. Xif (lib_debug >= 0) {
  1127. X    print "d_val[100] defined";
  1128. X    print "a_val[100] defined";
  1129. X    print "b_val[100] defined";
  1130. X    print "r_val[100] defined";
  1131. X}
  1132. SHAR_EOF
  1133. chmod 0644 calc2.9.0/lib/lucas_tbl.cal || echo "restore of calc2.9.0/lib/lucas_tbl.cal fails"
  1134. set `wc -c calc2.9.0/lib/lucas_tbl.cal`;Sum=$1
  1135. if test "$Sum" != "6646"
  1136. then echo original size 6646, current size $Sum;fi
  1137. echo "x - extracting calc2.9.0/lib/mersenne.cal (Text)"
  1138. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/mersenne.cal &&
  1139. X/*
  1140. X * Copyright (c) 1993 David I. Bell
  1141. X * Permission is granted to use, distribute, or modify this source,
  1142. X * provided that this copyright notice remains intact.
  1143. X *
  1144. X * Perform a primality test of 2^p-1, for prime p>1.
  1145. X */
  1146. X
  1147. Xdefine mersenne(p)
  1148. X{
  1149. X    local u, i, p_mask;
  1150. X
  1151. X    /* firewall */
  1152. X    if (! isint(p))
  1153. X        quit "p is not an integer";
  1154. X
  1155. X    /* two is a special case */
  1156. X    if (p == 2)
  1157. X        return 1;
  1158. X
  1159. X    /* if p is not prime, then 2^p-1 is not prime */
  1160. X    if (! ptest(p,10))
  1161. X        return 0;
  1162. X
  1163. X    /* calculate 2^p-1 for later mods */
  1164. X    p_mask = 2^p - 1;
  1165. X
  1166. X    /* lltest: u(i+1) = u(i)^2 - 2 mod 2^p-1 */
  1167. X    u = 4;
  1168. X    for (i = 2; i < p; ++i) {
  1169. X        u = u^2 - 2;
  1170. X        u = u&p_mask + u>>p;
  1171. X        if (u > p_mask)
  1172. X            u = u&p_mask + 1;
  1173. X    }
  1174. X
  1175. X    /* 2^p-1 is prime iff u(p) = 0 mod 2^p-1 */
  1176. X    return (u == p_mask);
  1177. X}
  1178. X
  1179. Xglobal lib_debug;
  1180. Xif (lib_debug >= 0) {
  1181. X    print "mersenne(p) defined";
  1182. X}
  1183. SHAR_EOF
  1184. chmod 0644 calc2.9.0/lib/mersenne.cal || echo "restore of calc2.9.0/lib/mersenne.cal fails"
  1185. set `wc -c calc2.9.0/lib/mersenne.cal`;Sum=$1
  1186. if test "$Sum" != "833"
  1187. then echo original size 833, current size $Sum;fi
  1188. echo "x - extracting calc2.9.0/lib/mod.cal (Text)"
  1189. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/mod.cal &&
  1190. X/*
  1191. X * Copyright (c) 1993 David I. Bell
  1192. X * Permission is granted to use, distribute, or modify this source,
  1193. X * provided that this copyright notice remains intact.
  1194. X *
  1195. X * Routines to handle numbers modulo a specified number.
  1196. X *    a (mod N)
  1197. X */
  1198. X
  1199. Xobj mod {a};            /* definition of the object */
  1200. X
  1201. Xglobal mod_value = 100;        /* modulus value (value of N) */
  1202. X
  1203. X
  1204. Xdefine mod(a)
  1205. X{
  1206. X    local obj mod    x;
  1207. X
  1208. X    if (!isreal(a) || !isint(a))
  1209. X        quit "Bad argument for mod function";
  1210. X    x.a = a % mod_value;
  1211. X    return x;
  1212. X}
  1213. X
  1214. X
  1215. Xdefine mod_print(a)
  1216. X{
  1217. X    if (digits(mod_value) <= 20)
  1218. X        print a.a, "(mod", mod_value : ")" :;
  1219. X    else
  1220. X        print a.a, "(mod N)" :;
  1221. X}
  1222. X
  1223. X
  1224. Xdefine mod_one()
  1225. X{
  1226. X    return mod(1);
  1227. X}
  1228. X
  1229. X
  1230. Xdefine mod_cmp(a, b)
  1231. X{
  1232. X    if (isnum(a))
  1233. X        return (a % mod_value) != b.a;
  1234. X    if (isnum(b))
  1235. X        return (b % mod_value) != a.a;
  1236. X    return a.a != b.a;
  1237. X}
  1238. X
  1239. X
  1240. Xdefine mod_rel(a, b)
  1241. X{
  1242. X    if (isnum(a))
  1243. X        a = mod(a);
  1244. X    if (isnum(b))
  1245. X        b = mod(b);
  1246. X    if (a.a < b.a)
  1247. X        return -1;
  1248. X    return a.a != b.a;
  1249. X}
  1250. X
  1251. X
  1252. Xdefine mod_add(a, b)
  1253. X{
  1254. X    local obj mod    x;
  1255. X
  1256. X    if (isnum(b)) {
  1257. X        if (!isint(b))
  1258. X            quit "Adding non-integer";
  1259. X        x.a = (a.a + b) % mod_value;
  1260. X        return x;
  1261. X    }
  1262. X    if (isnum(a)) {
  1263. X        if (!isint(a))
  1264. X            quit "Adding non-integer";
  1265. X        x.a = (a + b.a) % mod_value;
  1266. X        return x;
  1267. X    }
  1268. X    x.a = (a.a + b.a) % mod_value;
  1269. X    return x;
  1270. X}
  1271. X
  1272. X
  1273. Xdefine mod_sub(a, b)
  1274. X{
  1275. X    return a + (-b);
  1276. X}
  1277. X
  1278. X
  1279. Xdefine mod_neg(a)
  1280. X{
  1281. X    local obj mod    x;
  1282. X
  1283. X    x.a = mod_value - a.a;
  1284. X    return x;
  1285. X}
  1286. X
  1287. X
  1288. Xdefine mod_mul(a, b)
  1289. X{
  1290. X    local obj mod    x;
  1291. X
  1292. X    if (isnum(b)) {
  1293. X        if (!isint(b))
  1294. X            quit "Multiplying by non-integer";
  1295. X        x.a = (a.a * b) % mod_value;
  1296. X        return x;
  1297. X    }
  1298. X    if (isnum(a)) {
  1299. X        if (!isint(a))
  1300. X            quit "Multiplying by non-integer";
  1301. X        x.a = (a * b.a) % mod_value;
  1302. X        return x;
  1303. X    }
  1304. X    x.a = (a.a * b.a) % mod_value;
  1305. X    return x;
  1306. X}
  1307. X
  1308. X
  1309. Xdefine mod_square(a)
  1310. X{
  1311. X    local obj mod    x;
  1312. X
  1313. X    x.a = a.a^2 % mod_value;
  1314. X    return x;
  1315. X}
  1316. X
  1317. X
  1318. Xdefine mod_inc(a)
  1319. X{
  1320. X    local x;
  1321. X
  1322. X    x = a;
  1323. X    if (++x.a == mod_value)
  1324. X        x.a = 0;
  1325. X    return x;
  1326. X}
  1327. X
  1328. X
  1329. Xdefine mod_dec(a)
  1330. X{
  1331. X    local x;
  1332. X
  1333. X    x = a;
  1334. X    if (--x.a < 0)
  1335. X        x.a = mod_value - 1;
  1336. X    return x;
  1337. X}
  1338. X
  1339. X
  1340. Xdefine mod_inv(a)
  1341. X{
  1342. X    local obj mod    x;
  1343. X
  1344. X    x.a = minv(a.a, mod_value);
  1345. X    return x;
  1346. X}
  1347. X
  1348. X
  1349. Xdefine mod_div(a, b)
  1350. X{
  1351. X    local c, x, y;
  1352. X
  1353. X    obj mod x, y;
  1354. X    if (isnum(a))
  1355. X        a = mod(a);
  1356. X    if (isnum(b))
  1357. X        b = mod(b);
  1358. X    c = gcd(a.a, b.a);
  1359. X    x.a = a.a / c;
  1360. X    y.a = b.a / c;
  1361. X    return x * inverse(y);
  1362. X}
  1363. X
  1364. X
  1365. Xdefine mod_pow(a, b)
  1366. X{
  1367. X    local x, y, z;
  1368. X
  1369. X    obj mod x;
  1370. X    y = a;
  1371. X    z = b;
  1372. X    if (b < 0) {
  1373. X        y = inverse(a);
  1374. X        z = -b;
  1375. X    }
  1376. X    x.a = pmod(y.a, z, mod_value);
  1377. X    return x;
  1378. X}
  1379. X
  1380. X
  1381. Xglobal lib_debug;
  1382. Xif (lib_debug >= 0) {
  1383. X    print "obj mod {a} defined";
  1384. X    print "mod(a) defined";
  1385. X    print "mod_print(a) defined";
  1386. X    print "mod_one(a) defined";
  1387. X    print "mod_cmp(a, b) defined";
  1388. X    print "mod_rel(a, b) defined";
  1389. X    print "mod_add(a, b) defined";
  1390. X    print "mod_sub(a, b) defined";
  1391. X    print "mod_mod(a, b) defined";
  1392. X    print "mod_square(a) defined";
  1393. X    print "mod_inc(a) defined";
  1394. X    print "mod_dec(a) defined";
  1395. X    print "mod_inv(a) defined";
  1396. X    print "mod_div(a, b) defined";
  1397. X    print "mod_pow(a, b) defined";
  1398. X    print "mod_value defined";
  1399. X    print "set mod_value as needed";
  1400. X}
  1401. SHAR_EOF
  1402. chmod 0644 calc2.9.0/lib/mod.cal || echo "restore of calc2.9.0/lib/mod.cal fails"
  1403. set `wc -c calc2.9.0/lib/mod.cal`;Sum=$1
  1404. if test "$Sum" != "3005"
  1405. then echo original size 3005, current size $Sum;fi
  1406. echo "x - extracting calc2.9.0/lib/nextprim.cal (Text)"
  1407. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/nextprim.cal &&
  1408. X/*
  1409. X * Copyright (c) 1993 David I. Bell
  1410. X * Permission is granted to use, distribute, or modify this source,
  1411. X * provided that this copyright notice remains intact.
  1412. X *
  1413. X * Function to find the next prime (probably).
  1414. X */
  1415. X
  1416. Xdefine nextprime(n, tries)
  1417. X{
  1418. X    if (isnull(tries))
  1419. X        tries = 20;
  1420. X    if (iseven(n))
  1421. X        n++;
  1422. X    while (ptest(n, tries) == 0)
  1423. X        n += 2;
  1424. X    return n;
  1425. X}
  1426. X
  1427. Xglobal lib_debug;
  1428. Xif (lib_debug >= 0) {
  1429. X    print "nextprime(n, tries) defined";
  1430. X}
  1431. SHAR_EOF
  1432. chmod 0644 calc2.9.0/lib/nextprim.cal || echo "restore of calc2.9.0/lib/nextprim.cal fails"
  1433. set `wc -c calc2.9.0/lib/nextprim.cal`;Sum=$1
  1434. if test "$Sum" != "440"
  1435. then echo original size 440, current size $Sum;fi
  1436. echo "x - extracting calc2.9.0/lib/pell.cal (Text)"
  1437. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/pell.cal &&
  1438. X/*
  1439. X * Copyright (c) 1993 David I. Bell
  1440. X * Permission is granted to use, distribute, or modify this source,
  1441. X * provided that this copyright notice remains intact.
  1442. X *
  1443. X * Solve Pell's equation; Returns the solution X to: X^2 - D * Y^2 = 1.
  1444. X * Type the solution to pells equation for a particular D.
  1445. X */
  1446. X
  1447. Xdefine pell(D)
  1448. X{
  1449. X    local X, Y;
  1450. X
  1451. X    X = pellx(D);
  1452. X    if (isnull(X)) {
  1453. X        print "D=":D:" is square";
  1454. X        return;
  1455. X    }
  1456. X    Y = isqrt((X^2 - 1) / D);
  1457. X    print X : "^2 - " : D : "*" : Y : "^2 = " : X^2 - D*Y^2;
  1458. X}
  1459. X
  1460. X
  1461. X/*
  1462. X * Function to solve Pell's equation
  1463. X * Returns the solution X to:
  1464. X *    X^2 - D * Y^2 = 1
  1465. X */
  1466. Xdefine pellx(D)
  1467. X{
  1468. X    local R, Rp, U, Up, V, Vp, A, T, Q1, Q2, n;
  1469. X    local mat ans[2,2];
  1470. X    local mat tmp[2,2];
  1471. X
  1472. X    R = isqrt(D);
  1473. X    Vp = D - R^2;
  1474. X    if (Vp == 0)
  1475. X        return;
  1476. X    Rp = R + R;
  1477. X    U = Rp;
  1478. X    Up = U;
  1479. X    V = 1;
  1480. X    A = 0;
  1481. X    n = 0;
  1482. X    ans[0,0] = 1;
  1483. X    ans[1,1] = 1;
  1484. X    tmp[0,1] = 1;
  1485. X    tmp[1,0] = 1;
  1486. X    do {
  1487. X        T = V;
  1488. X        V = A * (Up - U) + Vp;
  1489. X        Vp = T;
  1490. X        A = U // V;
  1491. X        Up = U;
  1492. X        U = Rp - U % V;
  1493. X        tmp[0,0] = A;
  1494. X        ans *= tmp;
  1495. X        n++;
  1496. X    } while (A != Rp);
  1497. X    Q2 = ans[[1]];
  1498. X    Q1 = isqrt(Q2^2 * D + 1);
  1499. X    if (isodd(n)) {
  1500. X        T = Q1^2 + D * Q2^2;
  1501. X        Q2 = Q1 * Q2 * 2;
  1502. X        Q1 = T;
  1503. X    }
  1504. X    return Q1;
  1505. X}
  1506. X
  1507. Xglobal lib_debug;
  1508. Xif (lib_debug >= 0) {
  1509. X    print "pell(D) defined";
  1510. X    print "pellx(D) defined";
  1511. X}
  1512. SHAR_EOF
  1513. chmod 0644 calc2.9.0/lib/pell.cal || echo "restore of calc2.9.0/lib/pell.cal fails"
  1514. set `wc -c calc2.9.0/lib/pell.cal`;Sum=$1
  1515. if test "$Sum" != "1247"
  1516. then echo original size 1247, current size $Sum;fi
  1517. echo "x - extracting calc2.9.0/lib/pi.cal (Text)"
  1518. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/pi.cal &&
  1519. X/*
  1520. X * Copyright (c) 1993 David I. Bell
  1521. X * Permission is granted to use, distribute, or modify this source,
  1522. X * provided that this copyright notice remains intact.
  1523. X *
  1524. X * Calculate pi within the specified epsilon using the quartic convergence
  1525. X * iteration.
  1526. X */
  1527. X
  1528. Xdefine qpi(epsilon)
  1529. X{
  1530. X    local niter, yn, ym, tm, an, am, t, tn, sqrt2, epsilon2, count, digits;
  1531. X    local bits, bits2;
  1532. X
  1533. X    if (isnull(epsilon))
  1534. X        epsilon = epsilon();
  1535. X    digits = digits(1/epsilon);
  1536. X    if    (digits <=  8) { niter = 1; epsilon =    1e-8; }
  1537. X    else if (digits <= 40) { niter = 2; epsilon =  1e-40; }
  1538. X    else if (digits <= 170) { niter = 3; epsilon = 1e-170; }
  1539. X    else if (digits <= 693) { niter = 4; epsilon = 1e-693; }
  1540. X    else {
  1541. X        niter = 4;
  1542. X        t = 693;
  1543. X        while (t < digits) {
  1544. X            ++niter;
  1545. X            t *= 4;
  1546. X        }
  1547. X    }
  1548. X    epsilon2 = epsilon/(digits/10 + 1);
  1549. X    digits = digits(1/epsilon2);
  1550. X    sqrt2 = sqrt(2, epsilon2);
  1551. X    bits = abs(ilog2(epsilon)) + 1;
  1552. X    bits2 = abs(ilog2(epsilon2)) + 1;
  1553. X    yn = sqrt2 - 1;
  1554. X    an = 6 - 4 * sqrt2;
  1555. X    tn = 2;
  1556. X    for (count = 0; count < niter; count++) {
  1557. X        ym = yn;
  1558. X        am = an;
  1559. X        tn *= 4;
  1560. X        t = sqrt(sqrt(1-ym^4, epsilon2), epsilon2);
  1561. X        yn = (1-t)/(1+t);
  1562. X        an = (1+yn)^4*am-tn*yn*(1+yn+yn^2);
  1563. X        yn = bround(yn, bits2);
  1564. X        an = bround(an, bits2);
  1565. X    }
  1566. X    return (bround(1/an, bits));
  1567. X}
  1568. X
  1569. Xglobal lib_debug;
  1570. Xif (lib_debug >= 0) {
  1571. X    print "qpi(epsilon) defined";
  1572. X}
  1573. SHAR_EOF
  1574. chmod 0644 calc2.9.0/lib/pi.cal || echo "restore of calc2.9.0/lib/pi.cal fails"
  1575. set `wc -c calc2.9.0/lib/pi.cal`;Sum=$1
  1576. if test "$Sum" != "1311"
  1577. then echo original size 1311, current size $Sum;fi
  1578. echo "x - extracting calc2.9.0/lib/pollard.cal (Text)"
  1579. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/pollard.cal &&
  1580. X/*
  1581. X * Copyright (c) 1993 David I. Bell
  1582. X * Permission is granted to use, distribute, or modify this source,
  1583. X * provided that this copyright notice remains intact.
  1584. X *
  1585. X * Factor using Pollard's p-1 method.
  1586. X */
  1587. X
  1588. Xdefine factor(N, B, ai, af)
  1589. X{
  1590. X    local    a, k, i, d;
  1591. X
  1592. X    if (isnull(B))
  1593. X        B = 1000;
  1594. X    if (isnull(ai))
  1595. X        ai = 2;
  1596. X    if (isnull(af))
  1597. X        af = ai + 20;
  1598. X    k = lcmfact(B);
  1599. X    d = lfactor(N, B);
  1600. X    if (d > 1)
  1601. X        return d;
  1602. X    for (a = ai; a <= af; a++) {
  1603. X        i = pmod(a, k, N);
  1604. X        d = gcd(i - 1, N);
  1605. X        if ((d > 1) && (d != N))
  1606. X            return d;
  1607. X    }
  1608. X    return 1;
  1609. X}
  1610. X
  1611. Xglobal lib_debug;
  1612. Xif (lib_debug >= 0) {
  1613. X    print "factor(N, B, ai, af) defined";
  1614. X}
  1615. SHAR_EOF
  1616. chmod 0644 calc2.9.0/lib/pollard.cal || echo "restore of calc2.9.0/lib/pollard.cal fails"
  1617. set `wc -c calc2.9.0/lib/pollard.cal`;Sum=$1
  1618. if test "$Sum" != "620"
  1619. then echo original size 620, current size $Sum;fi
  1620. echo "x - extracting calc2.9.0/lib/poly.cal (Text)"
  1621. sed 's/^X//' << 'SHAR_EOF' > calc2.9.0/lib/poly.cal &&
  1622. X/* 
  1623. X * A collection of functions designed for calculations involving
  1624. X *     polynomials in one variable (by Ernest W. Bowen).
  1625. X *
  1626. X * On starting the program the independent variable has identifier x
  1627. X *    and name "x", i.e. the user can refer to it as x, the
  1628. X *    computer displays it as "x".  The name of the independent
  1629. X *    variable is stored as varname, so, for example, varname = "alpha"
  1630. X *    will change its name to "alpha".  At any time, the independent
  1631. X *    variable has only one name.  For some purposes, a name like
  1632. X *    "sin(t)" or "(a + b)" or "\lambda" might be useful;
  1633. X *    names like "*" or "-27" are legal but might give expressions
  1634. X *    that are difficult to intepret.
  1635. X *
  1636. X * Polynomial expressions may be constructed from numbers and the
  1637. X *    independent variable and other polynomials by the algebraic
  1638. X *    operations +, -, *, ^, and if the result is a polynomial /.
  1639. X *    The operations // and % are defined to have the quotient and
  1640. X *    remainder meanings as usually defined for polynomials.
  1641. X *
  1642. X * When polynomials are assigned to idenfifiers, it is convenient to
  1643. X *    think of the polynomials as values.  For example, p = (x - 1)^2
  1644. X *    assigns to p a polynomial value in the same way as q = (7 - 1)^2
  1645. X *    would assign to q a number value.  As with number expressions
  1646. X *    involving operations, the expression used to define the
  1647. X *    polynomial is usually lost; in the above example, the normal
  1648. X *    computer display for p will be  x^2 - 2x + 1.  Different
  1649. X *    identifiers may of course have the same polynomial value.
  1650. X * 
  1651. X * The polynomial we think of as a_0 + a_1 * x + ... + a_n * x^n,
  1652. X *    for number coefficients a_0, a_1, ... a_n may also be
  1653. X *    constructed as pol(a_0, a_1, ..., a_n).  Note that here the
  1654. X *    coefficients are to be in ascending power order.  The independent
  1655. X *    variable is pol(0,1), so to use t, say, as an identifier for
  1656. X *    this, one may assign  t = pol(0,1).  To simultaneously specify
  1657. X *    an identifier and a name for the independent variable, there is
  1658. X *    the instruction var, used as in identifier = var(name).  For
  1659. X *    example, to use "t" in the way "x" is initially, one may give
  1660. X *    the instruction  t = var("t").
  1661. X *
  1662. X * There are four parameters pmode, order, iod and ims for controlling
  1663. X *    the format in which polynomials are displayed.
  1664. X *    The parameter pmode may have values "alg" or "list": the
  1665. X *    former gives a display as an algebraic formula, while
  1666. X *    the latter only lists the coefficients.  Whether the terms or
  1667. X *    coefficients are in ascending or descending power order is
  1668. X *    controlled by order being "up" or "down".  If the
  1669. X *    parameter iod (for integer-only display), the polynomial
  1670. X *    is expressed in terms of a polynomial whose coefficients are
  1671. X *    integers with gcd = 1, the leading coefficient having positive
  1672. X *    real part, with where necessary a leading multiplying integer,
  1673. X *    a Gaussian integer multiplier if the coefficients are complex
  1674. X *    with a common complex factor, and a trailing divisor integer.
  1675. X *    If a non-zero value is assigned to the parameter ims,
  1676. X *    multiplication signs will be inserted where appropriate;
  1677. X *    this may be useful if the expression is to be copied to a
  1678. X *    program or a string to be used with eval. 
  1679. X *
  1680. X * For evaluation of polynomials the standard function is ev(p, t).
  1681. X *    If p is a polynomial and t anything for which the relevant
  1682. X *    operations can be performed, this returns the value of p
  1683. X *    at t.  The function ev(p, t) also accepts lists or matrices
  1684. X *    as possible values for p; each element of p is then evaluated
  1685. X *    at t.  For other p, t is ignored and the value of p is returned.
  1686. X *    If an identifier, a, say, is used for the polynomial, list or
  1687. X *    matrix p, the definition
  1688. X *            define a(t) = ev(a, t);
  1689. X *    permits a(t) to be used for the value of a at t as if the
  1690. X *    polynomial, list or matrix were a function.  For example,
  1691. X *    if a = 1 + x^2, a(2) will return the value 5, just as if
  1692. X *            define a(t) = 1 + t^2;
  1693. X *    had been used.   However, when the polynomial definition is
  1694. X *    used, changing the polynomial a will change a(t) to the value
  1695. X *    of the new polynomial at t.  For example,
  1696. X *    after 
  1697. X *        L = list(x, x^2, x^3, x^4);
  1698. X        define a(t) = ev(a, t);
  1699. X *    the loop
  1700. X *        for (i = 0; i < 4; i++)
  1701. X *            print ev(L[[i]], 5);
  1702. X *    may be replaced by
  1703. X *        for (i = 0; i < 4; i++) {
  1704. X *            a = L[[i]];
  1705. X *            print a(5);
  1706. X *        } 
  1707. X *      
  1708. X * Matrices with polynomial elements may be added, subtracted and
  1709. X *    multiplied as long as the usual rules for compatibility are
  1710. X *    observed.  Also, matrices may be multiplied by polynomials,
  1711. X *    i.e. if p is a     polynomial and A a matrix whose elements
  1712. X *    may be numbers or polynomials, p * A returns the matrix of
  1713. X *    the same shape as A with each element multiplied by p.
  1714. X *    Square matrices may also be 'substituted for the variable' in
  1715. X *    polynomials, e.g. if A is an m x m matrix, and
  1716. X *    p = x^2 + 3 * x + 2, ev(p, A) returns the same as
  1717. X *    A^2 + 3 * A + 2 * I, where I is the unit m x m matrix.  
  1718. X *    
  1719. X * On starting this program, three demonstration polynomials a, b, c
  1720. X *    have been defined.  The functions a(t), b(t), c(t) corresponding
  1721. X *    to a, b, c, and x(t) corresponding to x, have also been
  1722. X *    defined, so the usual function notation can be used for
  1723. X *    evaluations of a, b, c and x.  For x, as long as x identifies
  1724. X *    the independent variable, x(t) should return the value of t,
  1725. X *    i.e. it acts as an identity function.
  1726. X *    
  1727. X * Functions defined include:
  1728. X *
  1729. X *    monic(a) returns the monic multiple of a, i.e., if a != 0,
  1730. X *        the multiple of    a with leading coefficient 1    
  1731. X *    conj(a) returns the complex conjugate of a
  1732. X *    ispmult(a,b) returns 1 or 0 according as a is or is not
  1733. X *        a polynomial multiple of b
  1734. X *    pgcd(a,b) returns the monic gcd of a and b 
  1735. X *    pfgcd(a,b) returns a list of three polynomials (g, u, v)
  1736. X *        where g = pgcd(a,b) and g = u * a + v * b.
  1737. X *    plcm(a,b) returns the monic lcm of a and b
  1738. X *
  1739. X *    interp(X,Y,t) returns the value at t of the polynomial given
  1740. X *        by Newtonian divided difference interpolation, where
  1741. X *        X is a list of x-values, Y a list of corresponding
  1742. X *        y-values.  If t is omitted, the interpolating
  1743. X *        polynomial is returned.  A y-value may be replaced by
  1744. X *        list (y, y_1, y_2, ...), where y_1, y_2, ... are
  1745. X *        the reduced derivatives at the corresponding x;
  1746. X *        i.e. y_r is the r-th derivative divided by fact(r).
  1747. X *    mdet(A) returns the determinant of the square matrix A,
  1748. X *        computed by an algorithm that does not require
  1749. X *        inverses;  the built-in det function usually fails
  1750. X *        for matrices with polynomial elements.  
  1751. X *    D(a,n) returns the n-th derivative of a; if n is omitted,
  1752. X *        the first derivative is returned.
  1753. X *
  1754. X * A first-time user can see what the initially defined polynomials
  1755. X *    a, b and c are, and experiment with the algebraic operations
  1756. X *    and other functions that have been defined by giving
  1757. X *    instructions like:
  1758. X *            a
  1759. X *            b
  1760. X *            c
  1761. X *            (x^2 + 1) * a
  1762. X *            a^27
  1763. X *            a * b    
  1764. X *            a % b
  1765. X *            a // b
  1766. X *            a(1 + x)
  1767. X *            a(b)
  1768. X *            conj(c)
  1769. X *            g = pgcd(a, b)
  1770. X *            g
  1771. X *            a / g
  1772. X *            D(a)
  1773. X *            mat A[2,2] = {1 + x, x^2, 3, 4*x}
  1774. X *            mdet(A)
  1775. X *            D(A)
  1776. X *            A^2
  1777. X *            define A(t) = ev(A, t)
  1778. X *            A(2)
  1779. X *            A(1 + x)
  1780. X *            define L(t) = ev(L, t)
  1781. X *            L = list(x, x^2, x^3, x^4)
  1782. X *            L(5)
  1783. X *            a(L)
  1784. X *            interp(list(0,1,2,3), list(2,3,5,7))
  1785. X *            interp(list(0,1,2), list(0,list(1,0),2))
  1786. X *
  1787. X * One check on some of the functions is provided by the Cayley-Hamilton
  1788. X *    theorem:  if A is any m x m matrix and I the m x m unit matrix,
  1789. X *    and x is pol(0,1),
  1790. X *            ev(mdet(x * I - A), A)
  1791. X *    should return the zero m x m matrix.
  1792. X */
  1793. X
  1794. Xobj poly {p};
  1795. X
  1796. Xdefine pol() {
  1797. X    local u,i,s;
  1798. X    obj poly u;
  1799. X    s = list();
  1800. X    for (i=1; i<= param(0); i++) append (s,param(i));
  1801. X    i=size(s) -1;
  1802. X    while (i>=0 && s[[i]]==0) {i--; remove(s)}
  1803. X    u.p = s;
  1804. X    return u;
  1805. X}
  1806. X
  1807. Xdefine ispoly(a) {
  1808. X    local y;
  1809. X    obj poly y;
  1810. X    return istype(a,y);
  1811. X}
  1812. X
  1813. Xdefine findlist(a) {
  1814. X    if (ispoly(a)) return a.p;
  1815. X    if (a) return list(a);
  1816. X    return list();
  1817. X}
  1818. X
  1819. Xpmode = "alg";    /* The other acceptable pmode is "list" */
  1820. Xims = 0;    /* To be non-zero if multiplication signs to be inserted */
  1821. Xiod = 0;    /* To be non-zero for integer-only display */
  1822. Xorder = "down"    /* Determines order in which coefficients displayed */
  1823. X
  1824. Xdefine poly_print(a) {
  1825. X    local f, g, t;
  1826. X    if (size(a.p) == 0) {
  1827. X        print 0:;
  1828. X        return;
  1829. X    } 
  1830. X    if (iod) {
  1831. X        g = gcdcoeffs(a);
  1832. X        t = a.p[[size(a.p) - 1]] / g;
  1833. X        if (re(t) < 0) { t = -t; g = -g;}
  1834. X        if (g != 1) {
  1835. X            if (!isreal(t)) {
  1836. X                if (im(t) > re(t)) g *= 1i;
  1837. X                else if (im(t) <= -re(t)) g *= -1i;
  1838. X            }
  1839. X            if (isreal(g)) f = g;
  1840. X            else f = gcd(re(g), im(g));
  1841. X            if (num(f) != 1) {
  1842. X                print num(f):;
  1843. X                if (ims) print"*":;
  1844. X            }
  1845. X            if (!isreal(g)) {
  1846. X                printf("(%d)", g/f);
  1847. X                if (ims) print"*":;
  1848. X            }
  1849. X            if (pmode == "alg") print"(":;
  1850. X            polyprint(1/g * a);
  1851. X            if (pmode == "alg") print")":;
  1852. X            if (den(f) > 1) print "/":den(f):;
  1853. X            return;
  1854. X        }
  1855. X    }
  1856. X    polyprint(a);
  1857. X}
  1858. X
  1859. Xdefine polyprint(a) {
  1860. X    local s,n,i,c;
  1861. X    s = a.p;
  1862. X    n=size(s) - 1;
  1863. X    if (pmode=="alg") {
  1864. X        if (order == "up") {
  1865. X            i = 0;
  1866. X            while (!s[[i]]) i++;
  1867. X            pterm (s[[i]], i);
  1868. X            for (i++ ; i <= n; i++) {
  1869. X                c = s[[i]];
  1870. X                if (c) {
  1871. X                    if (isreal(c)) {
  1872. X                        if (c > 0) print" + ":;
  1873. X                        else {
  1874. X                            print" - ":;
  1875. X                            c = -c;
  1876. X                        }
  1877. X                    } 
  1878. X                    else print " + ":;
  1879. X                    pterm(c,i);
  1880. X                }
  1881. X            }
  1882. X            return;
  1883. X        }
  1884. X        if (order == "down") {
  1885. X            pterm(s[[n]],n);
  1886. X            for (i=n-1; i>=0; i--) {
  1887. X                c = s[[i]];
  1888. X                if (c) {
  1889. X                    if (isreal(c)) {
  1890. X                        if (c > 0) print" + ":;
  1891. X                        else {
  1892. X                            print" - ":;
  1893. X                            c = -c;
  1894. X                        }
  1895. X                    } 
  1896. X                    else print " + ":;
  1897. X                    pterm(c,i);
  1898. X                }
  1899. X            }
  1900. X            return;
  1901. X        }
  1902. X        quit "order to be up or down";
  1903. X    }
  1904. X    if (pmode=="list") {
  1905. X        plist(s);
  1906. X        return;
  1907. X    }
  1908. X    print pmode,:"is unknown mode";
  1909. X}
  1910. X        
  1911. X
  1912. Xdefine poly_neg(a) {
  1913. X    local s,i,y;
  1914. X    obj poly y;
  1915. X    s = a.p;
  1916. X    for (i=0; i< size(s); i++) s[[i]] = -s[[i]];
  1917. X    y.p = s;
  1918. X    return y;
  1919. X}
  1920. X
  1921. Xdefine poly_conj(a) {
  1922. X    local s,i,y;
  1923. X    obj poly y;
  1924. X    s = a.p;
  1925. X    for (i=0; i < size(s); i++) s[[i]] = conj(s[[i]]);
  1926. X    y.p = s;
  1927. X    return y;
  1928. X}
  1929. X
  1930. Xdefine poly_inv(a) = pol(1)/a;    /* This exists only for a of zero degree */
  1931. X
  1932. Xdefine poly_add(a,b) {
  1933. X    local sa, sb, i, y;
  1934. X    obj poly y;
  1935. X    sa=findlist(a); sb=findlist(b);
  1936. X    if (size(sa) > size(sb)) swap(sa,sb);
  1937. X    for (i=0; i< size(sa); i++) sa[[i]] += sb[[i]];    
  1938. X    while (i < size(sb)) append (sa, sb[[i++]]);
  1939. X    while (i > 0 && sa[[--i]]==0) remove (sa);
  1940. X    y.p = sa;
  1941. X    return y;
  1942. X}
  1943. X
  1944. Xdefine poly_sub(a,b) {
  1945. X     return a + (-b);
  1946. X}
  1947. X
  1948. Xdefine poly_cmp(a,b) {
  1949. X    local sa, sb;
  1950. X    sa = findlist(a);
  1951. X    sb=findlist(b);
  1952. X    return  (sa != sb);
  1953. X}
  1954. X
  1955. Xdefine poly_mul(a,b) {
  1956. X    local sa,sb,i, j, y;
  1957. X    if (ismat(a)) swap(a,b);
  1958. X    if (ismat(b)) {
  1959. X        y = b;
  1960. X        for (i=matmin(b,1); i <= matmax(b,1); i++)
  1961. X            for (j = matmin(b,2); j<= matmax(b,2); j++)
  1962. X                y[i,j] = a * b[i,j];
  1963. X        return y;    
  1964. X    }
  1965. X    obj poly y;
  1966. X    sa=findlist(a); sb=findlist(b);
  1967. X    y.p = listmul(sa,sb);
  1968. X    return y;
  1969. X} 
  1970. X
  1971. Xdefine listmul(a,b) {
  1972. X    local da,db, s, i, j, u;
  1973. X    da=size(a)-1; db=size(b)-1;
  1974. X    s=list();
  1975. X    if (da >= 0 && db >= 0) {
  1976. X        for (i=0; i<= da+db; i++) { u=0;
  1977. X            for (j = max(0,i-db); j <= min(i, da); j++)
  1978. X            u += a[[j]]*b[[i-j]]; append (s,u);}}
  1979. X    return s;
  1980. X}
  1981. X
  1982. Xdefine ev(a,t) {
  1983. X    local v, i, j;
  1984. X    if (ismat(a)) {
  1985. X        v = a;
  1986. X        for (i = matmin(a,1); i <= matmax(a,1); i++)
  1987. X            for (j = matmin(a,2); j <= matmax(a,2); j++)
  1988. X                v[i,j] = ev(a[i,j], t);
  1989. X        return v;
  1990. X    }
  1991. X    if (islist(a)) {
  1992. X        v = list();
  1993. X        for (i = 0; i < size(a); i++)
  1994. X            append(v, ev(a[[i]], t));
  1995. X        return v;
  1996. X    }
  1997. X    if (!ispoly(a)) return a;
  1998. X    if (islist(t)) {
  1999. X        v = list();
  2000. X        for (i = 0; i < size(t); i++)
  2001. X            append(v, ev(a, t[[i]]));
  2002. X        return v;
  2003. X    }    
  2004. X    if (ismat(t)) return evpm(a.p, t);
  2005. X    return evp(a.p, t); 
  2006. X}
  2007. X
  2008. Xdefine evp(s,t) {
  2009. X    local n,v,i;
  2010. X    n = size(s);
  2011. X    if (!n) return 0;
  2012. X    v = s[[n-1]];
  2013. X    for (i = n - 2; i >= 0; i--) v=t * v +s[[i]];
  2014. X    return v;
  2015. X}
  2016. X
  2017. Xdefine evpm(s,t) {
  2018. X    local m, n, V, i, I;
  2019. X    n = size(s);
  2020. X    m = matmax(t,1) - matmin(t,1);
  2021. X    if (matmax(t,2) - matmin(t,2) != m) quit "Non-square matrix";
  2022. X    mat V[m+1, m+1];
  2023. X    if (!n) return V;
  2024. X    mat I[m+1, m+1];
  2025. X    matfill(I, 0, 1);
  2026. X    V = s[[n-1]] * I;
  2027. X    for (i = n - 2; i >= 0; i--) V = t * V + s[[i]] * I;
  2028. X    return V;
  2029. X}
  2030. Xpzero = pol(0);
  2031. Xx = pol(0,1); 
  2032. Xvarname = "x";
  2033. Xdefine x(t) = ev(x, t);
  2034. X
  2035. Xdefine iszero(a) {
  2036. X    if (ispoly(a))
  2037. X        return !size(a.p);
  2038. X    return a == 0;
  2039. X}
  2040. X
  2041. Xdefine isstring(a) = istype(a, " ");
  2042. X
  2043. Xdefine var(name) {
  2044. X    if (!isstring(name)) quit "Argument of var is to be a string";
  2045. X    varname = name;
  2046. X    return pol(0,1);
  2047. X}
  2048. X
  2049. Xdefine pcoeff(a) {
  2050. X        if (isreal(a)) print a:;
  2051. X        else print "(":a:")":;
  2052. X}
  2053. X
  2054. Xdefine pterm(a,n) {
  2055. SHAR_EOF
  2056. echo "End of part 18"
  2057. echo "File calc2.9.0/lib/poly.cal is continued in part 19"
  2058. echo "19" > s2_seq_.tmp
  2059. exit 0
  2060.