home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / r / rlab / CTB / poly2str < prev    next >
Encoding:
Text File  |  1995-11-15  |  2.4 KB  |  86 lines

  1. //--------------------------------------------------------------------
  2. //
  3. // poly2str
  4. //
  5. // Syntax: a = poly2str(plynom,tvar)
  6. //
  7. // This routine returns a string (as part of a list) that is the
  8. // representation of the polynomial coefficients contained in the
  9. // variable plynom multipliedulitplied by the powers of the transform
  10. // variables in tvar (usually either "s" or "z").
  11. //
  12. // This routine returns the following list:
  13. //
  14. //       a.sout = string
  15. //       a.len = string length
  16. //
  17. // Copyright (C), by Jeffrey B. Layton, 1994
  18. // Version JBL 940513
  19. //--------------------------------------------------------------------
  20.  
  21. poly2str = function(plynom,tvar)
  22. {
  23.    local(polylen,lenout,first_flag,old,term,sout,j)
  24.  
  25. // Determine length of polynomial
  26.    polylen=length(plynom);
  27.  
  28. // Initialize string
  29.    sout="";
  30.  
  31. // Initialize junk variables
  32.    lenout=0;            // Length of output string
  33.    first_flag=1;     // First term in polynomial flag
  34.    old=0;            // flag
  35.  
  36. // Start looping for number of terms in polynomial
  37.    for (j in 1:polylen) {
  38.  
  39. // Extract current term in polynomial
  40.         term=plynom[j];
  41.  
  42. // If first term and term != 0 then add to output string
  43.         if ((first_flag == 1) && (term != 0)) {
  44.              if ((term == 1) && (j != polylen)) {
  45.                   sout="  ";
  46.              else
  47.                   sout="   "+num2str(term);
  48.              }
  49.              first_flag=0;
  50.  
  51. // Else, add sign and current term to output string
  52.         else
  53.              if ((term == 1) && (j != polylen)) {
  54.                   sout=sout+" + ";
  55.              else if (term == 0) {
  56. // In current version it does nothing if coeffiecient of
  57. // term is zero.
  58.              else if (term >= 0) {
  59.                   sout=sout+" + "+num2str(term);
  60.              else
  61.                   sout=sout+" - "+num2str(abs(term));
  62.              }}}
  63.         }
  64. // If term != 0 then add transform variable and sign for coefficient.
  65.         if (term != 0) {
  66.             if ((polylen-j) > 1) {
  67.                  sout=sout+" "+tvar+"^"+num2str(polylen-j);
  68.             else if ((polylen-j) == 1){
  69.                  sout=sout+" "+tvar;
  70.             }}
  71.         }
  72.         if (((length(sout)-old)>63) && (j != polylen)) {
  73.               lenout=max([lenout,length(sout)-old])
  74.               sout=sout;
  75.               old=length(sout)-2;
  76.         }
  77.    }
  78.  
  79.    if (isempty(sout)) {
  80.        sout="   0";
  81.    }
  82.    lenout=max([lenout,length(sout)-old]);
  83.  
  84.    return << sout=sout; lenout=lenout >>
  85. };
  86.