home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / numana01.zip / DEF / POLY.DEF < prev    next >
Text File  |  1996-07-31  |  7KB  |  132 lines

  1. DEFINITION MODULE Poly;
  2.  
  3.         (********************************************************)
  4.         (*                                                      *)
  5.         (*              Polynomial arithmetic                   *)
  6.         (*      This version uses a vector representation       *)
  7.         (*                                                      *)
  8.         (*  Programmer:         P. Moylan                       *)
  9.         (*  Last edited:        29 May 1995                     *)
  10.         (*  Status:             OK                              *)
  11.         (*                                                      *)
  12.         (********************************************************)
  13.  
  14. (************************************************************************)
  15. (*                      IMPORTANT CONVENTION                            *)
  16. (*                                                                      *)
  17. (* Most of the procedures in this module use "INOUT" parameters for     *)
  18. (* their results.  It is assumed that you are likely to want to re-use  *)
  19. (* variables (especially where a calculation involves temporary         *)
  20. (* variables for intermediate results), so it's likely that the         *)
  21. (* variable that will receive the result already has a valid value      *)
  22. (* before the call.  In such a case there is an implicit "Destroy"      *)
  23. (* operation that recovers the space occupied by the old value, before  *)
  24. (* that value is replaced by the new result.                            *)
  25. (*                                                                      *)
  26. (* The internal calculations are ordered in such a way that the new     *)
  27. (* result is calculated before the old value is destroyed.  This allows *)
  28. (* you to use operations like Add(A,B,A) safely; the old value of A     *)
  29. (* is fetched correctly before it is overwritten by the result.         *)
  30. (*                                                                      *)
  31. (* The price to be paid for this flexibility is that you _must_ do an   *)
  32. (* "Init" operation on every variable before the first time you use it. *)
  33. (* If you forget the "Init", this module might try to do a "DISPOSE"    *)
  34. (* using an invalid pointer.                                            *)
  35. (*                                                                      *)
  36. (************************************************************************)
  37.  
  38. TYPE
  39.     Polynomial;         (* is private *)
  40.     CoeffType = LONGREAL;
  41.  
  42. (************************************************************************)
  43. (*              CREATING AND DESTROYING POLYNOMIALS                     *)
  44. (************************************************************************)
  45.  
  46. PROCEDURE Init (VAR (*OUT*) P: Polynomial);
  47.  
  48.     (* This should be the first operation performed on P, since this    *)
  49.     (* module needs to keep track of which polynomials have already had *)
  50.     (* space allocated for them.  It creates the zero polynomial.       *)
  51.  
  52. PROCEDURE Assign (VAR (*INOUT*) P: Polynomial;
  53.                                 coeffs: ARRAY OF CoeffType);
  54.  
  55.     (* Creates a polynomial with specified coefficients.  The previous  *)
  56.     (* value, if any, is lost.  The coefficients are specified from     *)
  57.     (* low to high degree; for example, the coefficient set specified   *)
  58.     (* by the array (1.0, 2.0, 3.0) gives the second-degree polynomial  *)
  59.     (* 1.0 + 2.0*x + 3.0*x^2.                                           *)
  60.  
  61. PROCEDURE Destroy (VAR (*INOUT*) P: Polynomial);
  62.  
  63.     (* Deallocates the space occupied by P.  P is still considered to   *)
  64.     (* exist, and its value is the zero polynomial.  The difference     *)
  65.     (* between Init and Destroy is that Init assumes that the input     *)
  66.     (* value of P is random rubbish, whereas Destroy assumes that P     *)
  67.     (* is properly structured as a polynomial (i.e. that an Init has    *)
  68.     (* previously been done on it).                                     *)
  69.  
  70. (************************************************************************)
  71. (*                      THE BASIC OPERATIONS                            *)
  72. (************************************************************************)
  73.  
  74. PROCEDURE Degree (P: Polynomial): INTEGER;
  75.  
  76.     (* Returns the degree of P, i.e. the power of the most significant  *)
  77.     (* term.  The degree of a constant is 0, but the degree of the      *)
  78.     (* constant 0.0 is defined to be -1.                                *)
  79.  
  80. PROCEDURE Negate (P: Polynomial);
  81.  
  82.     (* P := -P.  This is an in-place operation, i.e. the original       *)
  83.     (* value of P is overwritten.                                       *)
  84.  
  85. PROCEDURE Add (A, B: Polynomial;  VAR (*INOUT*) C: Polynomial);
  86.  
  87.     (* Computes C := A + B. *)
  88.  
  89. PROCEDURE Sub (A, B: Polynomial;  VAR (*INOUT*) C: Polynomial);
  90.  
  91.     (* Computes C := A - B. *)
  92.  
  93. PROCEDURE Mul (A, B: Polynomial;  VAR (*INOUT*) C: Polynomial);
  94.  
  95.     (* Computes C := A*B. *)
  96.  
  97. PROCEDURE Div (A, B: Polynomial;  VAR (*INOUT*) Q, R: Polynomial);
  98.  
  99.     (* Computes A/B.  On return the quotient is Q and the       *)
  100.     (* remainder is R.                                          *)
  101.  
  102. (************************************************************************)
  103. (*                      ROOTS OF POLYNOMIALS                            *)
  104. (************************************************************************)
  105.  
  106. PROCEDURE Mueller (P: Polynomial;  VAR (*OUT*) root: LONGCOMPLEX);
  107.  
  108.     (* Finds one root of the equation P(x) = 0 by Mu"llers method.      *)
  109.  
  110. PROCEDURE Newton (P: Polynomial;  VAR (*INOUT*) root: LONGCOMPLEX);
  111.  
  112.     (* Improves an initial guess at a root of the equation P(x) = 0 by  *)
  113.     (* Newton's method.  We assume that the input value of root is      *)
  114.     (* close enough to make Newton's method appropriate.                *)
  115.  
  116. PROCEDURE FindRoots (P: Polynomial;  VAR (*OUT*) roots: ARRAY OF LONGCOMPLEX);
  117.  
  118.     (* Finds all (we hope) the solutions to P(x) = 0. *)
  119.  
  120. (************************************************************************)
  121. (*                          SCREEN OUTPUT                               *)
  122. (************************************************************************)
  123.  
  124. PROCEDURE Write (P: Polynomial;  places, linesize: CARDINAL);
  125.  
  126.     (* Writes P to the screen, where each coefficient is allowed to be  *)
  127.     (* up to "places" characters wide, and "linesize" is the number of  *)
  128.     (* characters allowed before we have to wrap onto a new line.       *)
  129.  
  130. END Poly.
  131.  
  132.