POLY, by Wayne Scott [upgrade to POLY on Goodies Disk #1. -jkh-] Here it is, my polynomial routines version 3.2 Changes from version 3.1: - Faster PMUL - RT now works with complex cooeffients - various bug fixes These routines are in the public domain, but I ask that if you use any of them in one of your programs you give me credit. I am also not responsible for any damage caused by these programs. This package include the following programs. TRIM Strip leading zeros from polynomial object. IRT Invert root program. Given n roots it return a nth degree polynomial. PDER Derivative of a polynomial. RDER Derivative of a rational function. PF Partial Fractions. (Handles multiple roots!) FCTP Factor polynomial RT Find roots of any polynomial L2A Convert a list to an array and back. PADD Add two polynomials PMUL Multiply two polynomials. PDIV Divide two polynomials. EVPLY Evaluate a polynomial at a point. COEF Given an equation return a polynomial list. These programs are for the HP-48sx. I have a version of them that works correctly on the HP-28. Send me mail if you want it. I think people will find these very useful and work as I say, but if you find any bugs please send me E-Mail. Comments are also welcome. Some of these routines could be faster (PF, PMUL, ...) tell me if you know how to speed them up. Wayne Scott | INTERNET: wscott@en.ecn.purdue.edu Electrical Engineering | BITNET: wscott%ea.ecn.purdue.edu@purccvm Purdue University | UUCP: {purdue,pur-ee}!en.ecn.purdue.edu!wscott These programs all work on polynomials in the following form: 3*X^3-7*X^2+5 is entered as { 3 -7 0 5 } Reasons why I use lists instead of arrays include: * lists look better on the stack. (multi-line) * The interactive stack can quickly put serveral items in a list. * Hitting EVAL will quickly return the elements on a list. * the '+' key will add a element to the end or beginning of a list or concatenate two lists * Internally the main program that needs to be fast (BAIRS) does 100% stack maniplations so speed of arrays vs. lists was not a major issue. * It would be easier for later releases to include symbolic polynomials. ÚÄÄÄÄÄÄ¿ ³ FCTP ³ (FaCTor Polynomial) ÀÄÄÄÄÄÄÙ When it is passed the cooeficients of a polynomial in a list it returns the factor of that polynomial. Example: 1: { 1 -17.8 99.41 -261.218 352.611 -134.106 } FCTP 3: { 1 -4.2 2.1 } 2: { 1 -3.3 6.2 } 1: { 1 -10.3 } This tells us that X^5-17.8*X^4+99.41*X^3-261.218*X^2+352.611*X-134.106 factors to (X^2-4.2*X+2.1)*(X^2-3.3*X+6.2)*(X-10.3) ÚÄÄÄÄ¿ ³ RT ³ (RooTs) ÀÄÄÄÄÙ If given a polynomial it return its roots. Example: 1: { 1 -17.8 99.41 -261.218 352.611 -134.106 } RT 5: 3.61986841536 4: .58013158464 3: (1.65, 1.8648056199) 2: (1.65, -1.8648056199) 1: 10.3 RT will work with complex cooeffients in the polynomial. These programs use the BAIRS program which performs Bairstow's method of quadratic factors and QUD which does the quadratic equation. ÚÄÄÄÄÄÄ¿ ³ TRIM ³is used to strip the leading zeros from a polynomial list. ÀÄÄÄÄÄÄÙ Example: { 0 0 3 0 7 } TRIM => { 3 0 7 } ÚÄÄÄÄÄÄ¿ ³ RDER ³will give the DERivative of a Rational function. Example: ÀÄÄÄÄÄÄÙ d x + 4 -X^2 - 8*x + 31 -- ------------- = -------------------------------- dx x^2 - 7*x + 3 x^4 - 14*x^3 + 55*x^2 - 42*x + 9 2: { 1 4 } 1: { 1 -7 3 } RDER 2: { -1 -8 31 } 1: { 1 -14 55 -42 9 } ÚÄÄÄÄÄ¿ ³ IRT ³(Inverse RT) will return a polynomial whose roots you specify. ÀÄÄÄÄÄÙ Example: If a transfer function has zeros at -1, 1 and 5 the function is x^3 - 5*x^2 - x + 5 1: { -1 1 5 } IRT 1: { 1 -5 -1 5 } ÚÄÄÄÄÄÄ¿ ³ PDER ³will return the DERivative of a Polynomial. Example: ÀÄÄÄÄÄÄÙ The d/dx (x^3 - 5*x^2 - x + 5) = 3*x^2 - 10*x - 1 1: { 1 -5 -1 5 } PDER 1: { 3 -10 -1 } ÚÄÄÄÄ¿ ³ PF ³will do a Partial Fraction expansion on a transfer function. ÀÄÄÄÄÙ Example: s + 5 1/18 5/270 2/3 1/9 2/27 ----------------- = ----- + ----- - ------- - ------- - ----- (s-4)(s+2)(s-1)^3 (s-4) (s+2) (s-1)^3 (s-1)^2 (s-1) 2: { 1 5 } 1: { 4 -2 1 1 1 } PF 1: { 5.5555e-2 1.85185e-2 -.6666 -.11111 -.074074 } This program expects the polynomial of the numerator to be on level 2 and a list with the poles to be on level 1. Repeated poles are suported but they must be listed in order. The output is a list of the values of the fraction in the same order as the poles were entered. ÚÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄ¿ ³ PADD ³ PMUL ³ PDIV ³are all obvious, they take two polynomial lists off ÀÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÙ the stack and perform the operation on them. PDIV returns the quotient polynomial and the remainder polynomial. ÚÄÄÄÄÄ¿ ³ L2A ³converts a list to an array. (and back) ÀÄÄÄÄÄÙ 1: { 1 2 3 } L2A 1: [ 1 2 3 ] L2A 1: { 1 2 3 } ÚÄÄÄÄÄÄÄ¿ ³ EVPLY ³evaluates any polynomial at a point. ÀÄÄÄÄÄÄÄÙ x^3 - 3*x^2 +10*x - 5 | x=2.5 = 16.875 2: { 1 -3 10 -5 } 1: 2.5 EVPLY 1: 16.875 P.S. Many thanks to Mattias Dahl & Henrik Johansson for fixs they have made.