#------------------------------------
# ES203 Maple initialization file
#
# Instructions:
# 1. Copy the contents of this file to a file called "maple.ini"
# 2. Locate the "LIB" folder in your Maple installation folder, e.g.,
#       C:\Program Files\Maple 8\LIB
# 3. Place the "maple.ini" file in the LIB folder
# 4. The "maple.ini" file will be processed when you launch Maple, and
#       also when you type "restart;"
#
#
# Created 19 Dec 2002 by Ed Doering
# Updates:
# 02 Jan 2003 - eng(): display prefix when unit is specified
# 24 Jan      - added 'parz' procedure (parallel impedance)
#------------------------------------

# Use 'j' to represent square root of -1
interface(imaginaryunit=j):

# Need 'StringTools' library
interface(warnlevel=0): 	# suppress warning message
with(StringTools):
interface(warnlevel=3):		# revert to default warning level


# Procedure to create a string for engineering prefix notation
epns := proc (x,f) local n,m,e;

   # Convert all numerical values to floating point
   n := evalf(x);

   # Only process non-zero values
   if (n <> 0) then

      # Round to 'f' significant figures
      e := floor(log10(abs(n)));
      n := round(n/10^(e-(f-1)))*10^(e-(f-1));
      
      # Determine exponent and mantissa
      e := 3*floor(log10(abs(evalf(n)))/3);
      m := evalf(n/10^e);

   else

      e := 0;
      m := 0;

   end if;

   SubstituteAll(sprintf("%gE%+d",m,e)," ","")
end:

# Procedure to create a string for engineering prefix notation mantissa
epnms := proc (x,f) local n,m,e;

   # Convert all numerical values to floating point
   n := evalf(x);

   # Only process non-zero values
   if (n <> 0) then

      # Round to 'f' significant figures
      e := floor(log10(abs(n)));
      n := round(n/10^(e-(f-1)))*10^(e-(f-1));
      
      # Determine exponent and mantissa
      e := 3*floor(log10(abs(evalf(n)))/3);
      m := evalf(n/10^e);

   else

      e := 0;
      m := 0;

   end if;

   SubstituteAll(sprintf("%g",m)," ","")
end:

# Procedure to create a string for engineering prefix notation prefix
epnps := proc (x,f) local n,e,ps;

   # Prefix strings
   ps := table([(-15)="f",(-12)="p",(-9)="n",
      (-6)=sprintf("%c",181),(-3)="m",(0)="",
      (3)="k",(6)="M",(9)="G",(12)="T",(15)="P"]);

   # Convert all numerical values to floating point
   n := evalf(x);

   # Only process non-zero values
   if (n <> 0) then

     # Determine exponent and mantissa
      e := 3*floor(log10(abs(evalf(n)))/3);

   else

      e := 0;

   end if;

   # Determine which prefix string to report
   if (e < -15 or e > 15) then
      sprintf("x 10^%d ",e)
   else
      ps[e]
   end if;

end:

# Procedure to create a string for fixed-point notation
fixs := proc (x,f) local n,e;

   # Convert all numerical values to floating point
   n := evalf(x);

   # Only process non-zero values
   if (n <> 0) then

      # Round to 'f' significant figures
      e := floor(log10(abs(n)));
      n := round(n/10^(e-(f-1)))*10^(e-(f-1));

   end if;

   SubstituteAll(sprintf("%g",n)," ","")
end:

# Procedure to print a numerical value in engineering prefix notation
eng := proc (x,u,f);
   if (nargs = 1) then
      printf("%20s",epns(x,3)):
   elif (nargs = 2) then
      printf("%20s %s%s",epnms(x,3),epnps(x,3),u):
   else
      printf("%20s %s%s",epnms(x,f),epnps(x,f),u):
   end if;
end:

# Procedure to display a complex value in polar form; angle is displayed in degrees
pol := proc (x,u,f) local m,p;
   m := abs(x);
   p := argument(x)*(180/Pi);
   if (nargs = 1) then
      printf("%20s at %5s%c\n",epns(m,3),fixs(p,3),186);   
   elif (nargs = 2) then
      printf("%20s at %5s%c %s%s\n",epnms(m,3),fixs(p,3),186,epnps(x,3),u);   
   else
      printf("%20s at %5s%c %s%s\n",epnms(m,f),fixs(p,f),186,epnps(x,f),u);   
   end if;
end:

# Procedure to calculate equivalent impedance of two parallel impedances
parz := (za,zb) -> 1/(1/za + 1/zb):

# Procedure to summarize usage instructions for all procedures in this initialization file
es203help := proc ();
   printf("--- eng(n)     - display numerical value 'n' in engineering prefix notation\n");
   printf("--- eng(n,u)   - include string 'u' as units, e.g., ""volts""\n");
   printf("--- eng(n,u,f) - use 'f' significant figures\n");
   printf("--- \n");
   printf("--- parz(za,zb)- returns equivalent impedance of parallel impedances Za and Zb\n");
   printf("--- \n");
   printf("--- pol(z)     - display complex value 'z' in polar format\n");
   printf("--- pol(z,u)   - include string 'u' as units, e.g., ""volts""\n");
   printf("--- pol(z,u,f) - use 'f' significant figures\n");
end:

# Describe actions of this initialization file
printf("--- Using ES203 'maple.ini' initialization file (last update: 24 Jan 2003)\n"); 
printf("--- 'j' = sqrt(-1)\n");
printf("--- Procedures: eng, parz, pol. Type 'es203help();' for usage details.\n");