(Comp.sources.hp48) Item: 113 by mheiskan@hut.fi Author: [Mika Heiskanen] Subj: POLY and ARIT libs v2.0 Date: Mon Jun 01 1992 [Oooh! These are libraries that most students would KILL for! -jkh-] I have received several requests about the new version of my POLY routines (which I promised to publish). I have done lots of bug fixing and new routines, but the libs are still not ready. Since I'm not likely to finish it for some time to come, I'll publish this new version. ARIT library is finished (unless I get some new ideas :) POLY library doesn't have the unfinished routines, but it is still a lot better than the previous version. All routines are written in system rpl for speed. ARIT is independent of POLY but you need ARIT if you use POLY. ARIT contains programs for arithmetic manipulations : SCOLCT : symbolic colct, does lists too (To simplify polynomials ofcourse!) MCOLCT : above multiple times SEXPAN : still the usual expan, but does lists too EXCO : multiple SEXPAN and SCOLCT GCD : greatest common denominator LCM : least common multiple FACTOR : obvious FACS : gives each factor only once RVAL : simplifies expression which will be rational RADD : adds rational numbers RSUB : substracts ... RMUL : multiplies ... RDIV : divides ... RPOT : rational number to ... RINV : inverse of ... RABS : absolute of ... SSQRT : symbolic square root ... POLY contains programs for polymonial calculations : PADD : adds polynomials PMUL : multiplies ... PDIV : divides ... POWP : polynomial to integer power PDER : derivative of poly. PRIM : primitive ... SROOTS : symbolic roots of polynomial ROOTS : numerical ones SDG2 : symb. roots of 2nd (1st) degree polynomial DEG2 : roots of ... PVAL : value of polynomial at a point (Horner method) P->A : similar to above, use with symbolic arguments A->P : algebraic to polynomial A->F : algebraic to rational polynomial F->A : can you guess? :) DOAL : operates on each coef. SMPF : simplifies a rat. poly. FADD : adds ... FSUB : something unthinkable... FMUL : multiply and fill the earth... FDER : derivative of ... FVAL : value of ... at a point PGCD : greatest common ... of 2 polynomials PLCM : least ... TOG5 : toggles flag 5 + subroutines New ones: PF : Partial fractions IRT : Given roots gives the polynomial And now with more details: The routines recognize rational numbers of the form +-(A/B) where A and B are real (complex) integers. Polynonials are expressed in the usual form, as lists with the highest order coefficient first. POLY uses the routines in ARIT directory to enable calculations with rational coefficients. However POLY has 2 modes, if you don't want to use rational numbers (faster). Flag 5 is used to indicate corresponding modes with flag set meaning rational mode. You will see a little 5 in the status area to indicate this. TOG5 is supplied to help toggling modes. And now to each program: n1,n2... real integer r1,r2... rational numbers (Note: can be n1,n2 too!) ARIT directory: ~~~~~~~~~~~~~~~ GCD: Finds GCD of 2 real integers Stack: 2: n2 -> 1: n1 1: n3 Examples: 2: 9 -> 1: 6 1: 3 LCM: Finds LCM of 2 real integers Operation similar to above GCD FACTOR: Programmed by Klaus Kalb. Slightly modified to consider 2^20-1 to be always the highest possible factor, also tagged with the number to be factored. Stack: 1: n1 -> 1: n1:{ factors } Examples: 1: 1456 -> 1: 1456: {2 2 2 2 7 13} 1: 1 -> 1: 1: {} 1: n1<1 -> error FACS: Gives each factor once, tagging removed. Examples : 1: 1456 -> 1: {2 7 13} 1: 0 or 1 -> 1: {} RVAL: Evaluates algebraic expression, simplifies rational numbers as they appear. Uses programs RADD, RMUL ... SSQRT. Doesn't simplify algebraics. Stack: 1: Symbolic -> 1: Simplified 1: Other -> 1: Unchanged Examples: 1: '(5/6-2*INV(5/4/3))^3-3' -> 1: '-1766159/27000' 1: '2*4/5-A/B' -> 1: '(B*8-A*5)/(5*B)' 1: '(2,4)/6' -> 1: '(1,2)/3' 1: '2-4*SQRT(24)' -> 1: '2-2*SQRT(6)*4' (Since 4*SQRT(6) is considered symbolic) SCOLCT: Similar to above but does the usual COLCT too. Use this! Accepts lists of algebraics too. Compare RVAL and SCOLCT with the last example above and you'll see the difference. SIMP: Simplifies a rational number Stack: 1: r1 -> 1: r2 Examples: 1: '-4/2' -> 1: -2 1: 'A*6/3' -> 1: 'A*6/3' (Try SCOLCT instead) 1: '(25,24)/(2,3)' -> 1: '(122,-27)/13' 1: 4 -> 1: 4 RADD: Adds rational numbers. Stack: 2: r2 1: r1 -> 1: r3 Examples: 2: 4 1: '1/3' -> 1: '13/3' 2: '3/2' 1: '(12,14)/4' -> 1: '(9,7)/2' RSUB: Does NEG, then above. RMUL: Multiplies rational numbers Stack: 2: r2 1: r1 -> 1: r3 Examples: 2: 3 1: '3/4' -> 1: '9/4' RDIV Divides rational numbers. Stack: This is getting quite obvious, isn't it? RPOT Rational to integer power Stack: 2: r1 1: n1 -> 1: r2 Examples: 2: '5/6' 1: -2 -> 1: '36/25' RINV Inverse of rational RABS Absolute of rational Examples: 1: '-(5/4)' -> 1: '5/4' 1: '1/-6' -> 1: '1/6' SRE Real part of symbolic, number etc SIM Imaginary part of ... SSQRT: Calculates square root symbolically. Examples: 1: 1456 -> 1: '4*SQRT(91)' 1: (6,8) -> 1: '(2,1)*SQRT(2)' 1: 'A+B' -> 1: 'SQRT(A+B)' POLY directory: ~~~~~~~~~~~~~~~ 5 in front of stack level means rational mode is on. { 1 2 3 } = normal form = x^2+2x+3 {{ 1 2 3 }} = root form = (x-1)(x-2)(x-3) { { 1 2 3 } { 1 2 } {{ A B }} } = factored form = (x^2+2x+3)(x+2)(x-A)(x-B) All progs use normal form unless mentioned otherwise. PADD: Adds polynomials Examples: 2: { 1 2 3 4 } -> 1: { 1 2 4 6 } 1: { 1 2 } 2: { '1/3' '4/7' } -> 5 1: { '2/3' '4/3' '25/7' } 1: { '2/3' 1 3 } 1: { '2/3' 1.333... 3.5714 ...} PMUL: Examples: 2: { 1 2 3 4 } -> 1: { 1 4 7 10 8 } 1: { 1 2 } 2: { 4 3 2 '2/3' } 5 1: { 6 '25/2' 13 8 '10/3' '2/3' } 1: { '3/2' 2 1 } 1: { 6 12.5 13 ... } PDIV: Examples: 2: { 4 3 2 '2/3'} -> 5 2: { '8/3' '-14/9' } 1: { '3/2' 2 1 } 1: { '22/9' '20/9' } meaning 4x^3+3x^2+2x+2/3 22/9*x+20/9 ---------------- = 8/3*x-14/9 + ----------- 3/2x^2+2x+1 3/2x^2+2x+1 PMUL, PADD and PDIV accept reals etc too. Example: 2: { 2 3 } PMUL -> 1: { 6 9 } 1: 3 PADD -> 1: { 2 6 } POWP: Examples: 2: { 1 2 '1/2' } -> 5 1: { 1 6 '27/2' 14 '27/4' '3/2' '1/8 } 1: 3 meaning ( x^2+2x+1/2 )^3 = x^6+6x^5.... PDER: Derivative of a polynomial Examples: 1: { '3/2' 4 5 } -> 5 1: { 3 4 } PRIM: Primitive of a polynomial Examples: 1: { '3/2' 4 5 } -> 5 1: { '1/2' 2 4 0 } 1: { .5 2 4 0 } SROOTS: Finds roots of a polynomial Rational roots and roots of the form A+SQRT(B) --------- C are recognized. Sets flag 5 temporarily. Don't use symbolics except rationals with any root finding programs. Examples: 1: { 5 4 } -> 1: '-4/5' 1: { 3 (4,5) (1,1) } -> 2: '(-4,-5)/6+(1,2)/6*SQRT(7)' 1: '.........-...............' 1: { '4/3' '19/3' '47/2' 51 '250/3' '199/2' '200/3' '109/3' 10 } -> 8: '-1/2' 7: '-1+(0,1)*SQRT(5)' 6: '-1-.............' 5: (-0.23702...,-1.79456...) 4: -1.52595... 3: '-1/8+(0,1)/8*SQRT(31)' 2: '....-................' 1: (-0.23702...,1.79456...) ROOTS: Uses Laguerres method to find the numerical roots of a polynomial. ROOTS does ->NUM in the beginning. Examples: Look above, the results are just numbers. SDG2: Finds symbolic roots of a 2nd (1st) degree polynomial Usage is quite obvious. DEG2: Look above. Results numbers. New features: SROOTS and ROOTS also accept factored forms. Root forms will stay the same (I wonder why? :) Roots will be given in root form. (Funny, eh?) IRT makes no difference between root and normal forms (for convenience) PF: Partial fraction expansion. Doesn't support 2nd degree form yet. 2: Normal form 1: Normal, root or factored form Result will be in two parts. For multiple roots highest coef comes first. Level 2 has the coefs. Level 1 has the roots. Try this: 2: { 1 } PF -> 2: { '1/(-A+B)' '1/(A-B)' } 1: {{ A B }} 1: {{ B A }} Meaning: 1 1 ---- --- 1 -A+B A-B ---------- = ------ + ----- (X-A)(X-B) X-B X-A That is: B and A are different numbers!! This is because all algebraics are compared as strings. PVAL: Evaluates a polynomial at a point using Horner's method. NOT suitable when using symbolics. Examples: 2: { '1/2' '2/3' '3/4' } -> 5 1: '481/300' 1: '4/5' 1: 1.603333... P->A: Similar to above. Use with symbolics. Flag 5 has no effect. Examples: 2: { A B C } -> 1: 'AX^2+BX+C' 1: 'X' DOAL: Operates on each coeff. If level 1 isn't a program, poly. is multiplied with it. Flag 5 has effect only in that case. Examples: 2: { 1 2 3 } -> 1: { 1 .5 .333...} 1: << INV >> 2: { 1 2 3 } -> 1: { 1 '1/2' '1/3' } 1: << RINV >> 2: { 1 2 3 } -> 5 1: { '1/2' 1 '3/2' } 1: '1/2' -> 1: { .5 1 1.5 } A->P: Transforms an algebraic to a polynomial. Uses A->F (next) Examples: 2: '((x^2+2*x+1)/(x+1) - x^3)^2' -> 1: {1 0 -2 -2 1 2 1 } 1: 'x' A->F: Similar to above. Result is a rational polynomial and is simplified. Examples: 2: '1/x-x' -> 2: { -1 0 1 } 1: 'x' 1: { 1 0} Rational polynomial routines have the obvious normal form + denominator can be in the root form too. SMPF: Simplifies a rat. pol. Examples: 2: { 1 2 1 } -> 2: { 1 1 } 1 : { 1 1} 1: { 1 } FADD: Adds ... FMUL Multiplies ... FDER: Derivative of ... FVAL: See PVAL. PGCD: Grestest common denominator of 2 polynomials. Used by SMPF. Examples: 2: { 1 2 1 } -> 1: { 1 1 } 1: { 1 1 } PGCD doesn't normalize coefs at any point. This can lead to wrong results in SMPF if big integer coefs are used. I will fix this later. STRIP: Strips leading zeros Examples: 1: { 0 0 (0,0) 0 1 } -> 1: { 1 } 1: { 0 0 } -> 1: { 0 } And last but not least... TOG5: Toggles flag 5