> SynDiv := proc()

  local i, j, n, a, filename, fp, nd, s, b, B, I, r;

  ########## Prepare f(x) ###########################################

  printf("***** Author: Mengnien Wu ****************************\n");

  printf("* Given a polynomial                                 *\n");

  printf("*      f(x)=a[1]+a[2]*x+...+a[n]*x^(n-1)             *\n");

  printf("* Divide it by (x-b) using \"Synthetic Division\"    *\n");

  printf("* and do the same thing to the quotient repeatedly.  *\n");

  printf("* A file contains the coefficents of                 *\n");

  printf("*        n a[1] a[2] ... a[n]                        *\n");

  printf("* will be read.                                      *\n");

  printf("******************************************************\n");

  printf("\nEnter your filename:\n");

  filename := scnf("%a")[1];

  fp:=fopen(filename,Read);

  n := fscnf(fp,"%d")[1];

  a := array(1..n);

  for i from 1 to n do

    a[i] := fscnf(fp,"%f")[1];

  od;

  fclose(fp);

  ########## Showing the given f(x) #################################

  printf("f(x) =%f",a[1]);

  for i from 2 to n do

    if a[i]>0 then printf(" +%f*x^%d",a[i],i-1);

    elif a[i]<0 then printf(" %f*x^%d",a[i],i-1);

    fi;

  od; printf("\n");

  ########## Divide nd many times ###################################

  printf("Enter b:\n");

  b:=scanf("%f")[1];

  printf("Enter # of divisions:\n");

  nd:=scnf("%d")[1];            

  for i from 1 to nd do          

    for j from n-1 to i by -1 do   # Only this part is the core

      a[j]:=a[j]+b*a[j+1];         # of synthetic division

    od;                            #

  od;

                            

  ########## For showing the rewrite of f(x) only : #################

  B := b;

  if B<0 then B := -B; s:=`+`; else s:=`-`; fi;

  printf("f(x) =");

  ########## Print the remainder of divisions #######################

  I:=0; r:=false;

  for i from 1 to nd do

    if a[i]<>0 then I:=i; r:=true; break; fi;

  od;

  for i from 1 to nd do

    if a[i]>0 and i>I then printf(" +"); fi;

    if a[i]<>0 then

        printf("%3.0f",a[i]);

    if i=2 then printf("*(x%c%f)",s,B); fi;

    if i>2 then printf("*(x%c%f)^%d",s,B,i-1); fi;

    fi

  od;

  ########## Print the final quotient of divisions ##################

  I:=nd;

  for i from nd+1 to n do

    if a[i]<>0 then I:=i; break; fi;

  od;

  if nd<=n then

    if r=true then printf(" + "); fi;

    printf("(x%c%f)^%d * [",s,B,nd);

    for i from nd+1 to n do

      if a[i]>0 and i>I then printf("+"); fi;

      if a[i]<>0 then

      printf("%f",a[i]);

      if i-nd=2 then printf("*x"); fi;

      if i-nd>2 then printf("*x^%d",i-nd-1); fi;

    fi;

    od;

    printf("]");

  fi;

  printf("\n");

  ########## Print f(b) and f'(b) ###################################

  printf("f(%4.0f) =%3.0f, f'(%3.0f) = %3.0f\n",b,a[1],b,a[2]);

  end:

 

> SynDiv();

***** Author: Mengnien Wu ****************************

* Given a polynomial                                 *

*      f(x)=a[1]+a[2]*x+...+a[n]*x^(n-1)             *

* Divide it by (x-b) using "Synthetic Division"      *

* and do the same thing to the remainder repeatedly. *

* A file contains the coefficents of                 *

*        n a[1] a[2] ... a[n]                        *

* will be read.                                      *

******************************************************

 

Enter your filename:

> "D:/Mathematics/2001fall/NumericalAnalysis/coeff.txt"

f(x) = -4 +  3*x^1  -3*x^2 +  2*x^4

Enter b:

> 1

Enter # of divisions:

> 2

f(x) = -2 + 5*(x-1) + (x-1)^2 * [ 3+ 4*x+ 2*x^2]

f(1) = -2, f'(1) = 5