#
#	Polar, implementation of complex numbers in polar form for Darwin
#
#				Gaston H. Gonnet (August 1998)
#
#	An example of Object oriented programming in Darwin
#

Polar := proc( Rho:numeric, Theta:numeric )
option polymorphic, NormalizeOnAssign;
description
'Data structure Polar( Rho, Theta )
Representation of complex numbers in polar form.  The number is
Rho * exp( i*Theta ).

- Operations:
    Initialization:      a := Polar(1,Pi/2);
                         b := Polar(0,1);
    All arithmetic operations:
			 a+b, a-b, a*b, a/b, a^b, |a|
    Special functions	 exp(a), ln(a), sin(a), cos(a), tan(a)
    Printing:            print(a);
			 printf( ''%.3f'', a );
    Type testing:	 type(a,Polar);

- Conversions:
    To string	: string(a)
       Complex  : Complex(a)
       Polar    : Polar(Complex(...))
 
- Selectors:
    a[Re] : real part
    a[Im] : imaginary part
    a[Rho] : radius or absolute value
    a[Theta] : angle, (-Pi < a[Theta] <= Pi)';

if nargs <> 2 then error('Polar must use two arguments')
elif Rho=0 then noeval(Polar(0,0))
elif Rho<0 then
     n := round( Theta/(2*Pi) - 0.5 );
     # Do the subtraction in two steps to maximize accuracy
     Polar(-Rho, Theta - 6.283203125*(n+0.5) + 0.0000178178204135230747*(n+0.5))
elif Theta > Pi or Theta < -Pi then
     n := round( Theta/(2*Pi) );
     # Do the subtraction in two steps to maximize accuracy
     noeval(Polar( Rho, Theta - 6.283203125*n  + 0.0000178178204135230747*n ))
elif Theta = -Pi then noeval(Polar(Rho,Pi))
else noeval(Polar(Rho,Theta)) fi
end:


Polar_print := proc( p:Polar ) option internal; printf( '%g\n', p ) end:

Polar_printf := proc( fmt:string, L:Polar ) option internal;
if L[1]=0 then sprintf(fmt,0)
elif L[2]=0 then sprintf(fmt,L[1])
elif L[2]=Pi then sprintf(fmt,-L[1])
elif L[1]=1 then sprintf( 'exp(' . fmt . 'i)', L[2] )
else sprintf( fmt . '*exp(' . fmt . 'i)', L[1], L[2] ) fi
end:

Polar_type := noeval( Polar(numeric,numeric) ):

Polar_select := proc( a:Polar, s, val ) option internal;
sel := uppercase(s);
if sel = 'RE' then
     if nargs=3 then error('cannot assign real part in Polar form') fi;
     a[1] * cos(a[2])
elif sel = 'IM' then
     if nargs=3 then error('cannot assign imaginary part in Polar form') fi;
     a[1] * sin(a[2])
else error(s,'is an invalid selector for Polar') fi
end:


Polar_string := proc( ) option internal; Polar_printf( '%g', args ) end:


Polar_times := proc( a, b ) option internal;
if type(a,Polar) then
     if type(b,Polar) then Polar( a[1]*b[1], a[2]+b[2] )
     elif type(b,numeric) then Polar( a[1]*b, a[2] )
     else error(niy) fi
elif type(a,numeric) then Polar(a*b[1],b[2])
else error(niy) fi;
end:


Polar_power := proc(a, b) option internal;
	if type(a,Polar) then
             if type(b,numeric) then
                  Polar(a[1]^b,a[2]*b);
             elif type(b,Polar) then
                  x := b[1]*cos(b[2]);
                  y := b[1]*sin(b[2]);
                  Polar(a[1]^x*exp(-a[2]*y),(y*ln(a[1]) +a[2]*x));
             else error(niy) fi;
        elif a=0 then
	     x := b[1]*cos(b[2]);
	     if b[1]=0 or abs(b[2])=Pi/2 then error('undefined')
	     elif x > 0 then 0
	     else error('division by zero') fi
        elif type(a,numeric) then Polar_power( Polar(a,0), b )
        else error(niy) fi;
end:

Polar_Complex := proc(p:Polar) option internal;
  Complex(p[Rho]*cos(p[Theta]), p[Rho]*sin(p[Theta]));
end:

Complex_Polar := proc(c:Complex) option internal;
  Polar(sqrt(c[Re]^2 + c[Im]^2),arctan(c[Im],c[Re]));
end:

Polar_exp := proc(p:Polar) option internal;
     x := p[Rho]*cos(p[Theta]);
     y := p[Rho]*sin(p[Theta]);
     Polar(exp(x),y);
end:

Polar_abs := proc(p:Polar) -> numeric; option internal; p[Rho] end:

Polar_Rand := proc() -> Polar; option internal;
    Polar(exp(10*Rand()-5),Pi*(2*Rand()-1)) end:

Polar_ln := proc(p:Polar) -> Polar; option internal;
    Complex_Polar( Complex(ln(p[Rho]),p[Theta]) ) end:

#printlevel := 40;
Inherit( Polar, Complex );

