home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / sonderh1 / complex.inc < prev    next >
Text File  |  1987-02-03  |  10KB  |  335 lines

  1. (*****************************************************************************)
  2. (*                                                                           *)
  3. (*         COMPLEX.INC - Modul zur Verarbeitung von komplexen Zahlen         *)
  4. (*                                                                           *)
  5. (*                           ---  Vers 4.1  ---                              *)
  6. (*                                                                           *)
  7. (*****************************************************************************)
  8.  
  9.  
  10. Type Complex   = String [12];
  11.      CString   = String [40];
  12.      Dummy     = Record
  13.                     StrLen    :Byte;
  14.                     RealPart,
  15.                     ImagPart  :Real
  16.                  End;
  17.  
  18.  
  19. Const i1 :Complex = #0#0#0#0#0#0#129#0#0#0#0#0;       (* imaginaere Einheit  *)
  20.       r1 :Complex = #129#0#0#0#0#0#0#0#0#0#0#0;       (* die reelle Zahl "1" *)
  21.  
  22.  
  23. (***********************  Komplexe Transferroutinen  *************************)
  24.  
  25. (* ------------------------------------------------------------------------- *)
  26. (*                      Realteil einer komplexen Zahl                        *)
  27. (* ------------------------------------------------------------------------- *)
  28.  
  29. Function Re (z :Complex) :Real;
  30.  
  31.    Var ComplStr :Complex;
  32.        Rec      :Dummy absolute ComplStr;
  33.  
  34.    Begin
  35.    ComplStr := z;
  36.    Re := Rec.RealPart
  37.    End;
  38.  
  39.  
  40. (* ------------------------------------------------------------------------- *)
  41. (*                    Imaginaerteil einer komplexen Zahl                     *)
  42. (* ------------------------------------------------------------------------- *)
  43.  
  44. Function Im (z :Complex) :Real;
  45.  
  46.    Var ComplStr :Complex;
  47.        Rec      :Dummy absolute ComplStr;
  48.  
  49.    Begin
  50.    ComplStr := z;
  51.    Im := Rec.ImagPart
  52.    End;
  53.  
  54.  
  55. (* ------------------------------------------------------------------------- *)
  56. (*          Zusammenfuegen zweier reeller Zahlen zu einer komplexen          *)
  57. (* ------------------------------------------------------------------------- *)
  58.  
  59. Function cval (Re,Im :Real) :Complex;
  60.  
  61.    Var Rec      :Dummy;
  62.        ComplStr :Complex absolute Rec;
  63.  
  64.    Begin
  65.    Rec.StrLen   := SizeOf (Real) shl 1;
  66.    Rec.RealPart := Re;
  67.    Rec.ImagPart := Im;
  68.    cval := ComplStr
  69.    End;
  70.  
  71.  
  72. (* ------------------------------------------------------------------------- *)
  73. (*           Umwandlung eines Eingabe-Strings ins COMPLEX-Format             *)
  74. (* ------------------------------------------------------------------------- *)
  75.  
  76. Function Value (InputString :CString) :Complex;
  77.  
  78.    Var Re,Im       :Real;
  79.        p,
  80.        RealResult,
  81.        ImagResult  :Integer;
  82.        valid       :Boolean;
  83.        c           :CString;
  84.  
  85.    Begin
  86.    c := InputString;
  87.    While Pos(' ',c) > 0 do Delete (c,Pos(' ',c),1);
  88.    If c[Length(c)]<>'i' then
  89.       Begin
  90.       Im := 0;
  91.       Val (c,Re,RealResult);
  92.       valid := (RealResult=0)
  93.       End
  94.    else
  95.       Begin
  96.       Re := 0;
  97.       Delete (c,Length(c),1);
  98.       If (c='') or (c[Length(c)] in ['+','-'])
  99.          then c := c + '1';
  100.       Val (c,Im,p);
  101.       valid := (p=0);
  102.       If not valid then
  103.          Begin
  104.          Val (Copy(c,1,pred(p)), Re, RealResult);
  105.          If c[p]='+' then p := succ (p);
  106.          Val (Copy(c,p,Length(c)), Im, ImagResult);
  107.          valid := (RealResult + ImagResult = 0)
  108.          End
  109.       End;
  110.    If valid then
  111.       Value := cval (Re,Im)
  112.    else
  113.       Begin
  114.       WriteLn (^M^J'Error:  invalid complex number "',InputString,'"');
  115.       Halt
  116.       End
  117.    End;
  118.  
  119.  
  120. (*************************  Komplexe IO-Routinen  ****************************)
  121.  
  122. (* ------------------------------------------------------------------------- *)
  123. (*                      Ausgeben einer komplexen Zahl                        *)
  124. (* ------------------------------------------------------------------------- *)
  125.  
  126. Procedure WriteC (Var Medium :Text; z :Complex; m,n :Byte);
  127.  
  128.    Var OutStr,ImagStr :CString;
  129.        k              :Integer;
  130.  
  131.    Begin
  132.    Str (Re(z):m:n,OutStr);
  133.    While OutStr[1]=' ' do Delete (OutStr,1,1);
  134.    If Im(z) >= 0 then
  135.       OutStr := OutStr + '+'
  136.    else
  137.       OutStr := OutStr + '-';
  138.    Str (Abs(Im(z)):m:n,ImagStr);
  139.    While ImagStr[1]=' ' do Delete (ImagStr,1,1);
  140.    OutStr := OutStr + ImagStr + 'i';
  141.    For k:=1 to (m-Length(OutStr)) do
  142.        OutStr := ' ' + OutStr;
  143.    Write (Medium,OutStr)
  144.    End;
  145.  
  146.  
  147. Procedure WriteLnC (Var Medium :Text; z :Complex; m,n :Byte);
  148.  
  149.    Begin
  150.    WriteC (Medium,z,m,n);
  151.    WriteLn (Medium);
  152.    End;
  153.  
  154.  
  155. (* ------------------------------------------------------------------------- *)
  156. (*                      Einlesen einer komplexen Zahl                        *)
  157. (* ------------------------------------------------------------------------- *)
  158.  
  159. Procedure ReadC (Var Medium :Text; Var z :Complex);
  160.  
  161.    Var InputString :CString;
  162.        Character   :Char;
  163.        TermChars   :Set of Char;
  164.  
  165.    Begin
  166.    InputString := '';
  167.    If Addr(Medium) = Addr(CON) then        (* Ist Eingabemedium die Konsole? *)
  168.       TermChars := [^M,^Z,#33..#127]
  169.    else
  170.       TermChars := [^Z,#33..#127];
  171.    Repeat
  172.       Read (Medium,Character)
  173.    until Character in TermChars;
  174.    While ord (Character) in [33..127] do
  175.       Begin
  176.       InputString := InputString + Character;
  177.       Read (Medium,Character)
  178.       End;
  179.    If InputString<>'' then z := Value (InputString)
  180.    End;
  181.  
  182.  
  183. Procedure ReadLnC (Var Medium :Text; Var z :Complex);
  184.  
  185.    Begin
  186.    ReadC (Medium,z);
  187.    ReadLn (Medium);
  188.    End;
  189.  
  190.  
  191. (************************  Komplexe Rechenroutinen  **************************)
  192.  
  193. (* ------------------------------------------------------------------------- *)
  194. (*                Komplex konjugiertes einer komplexen Zahl                  *)
  195. (* ------------------------------------------------------------------------- *)
  196.  
  197. Function conj (z :Complex) :Complex;
  198.  
  199.    Begin
  200.    conj := cval (Re(z),-Im(z));
  201.    End;
  202.  
  203.  
  204. (* ------------------------------------------------------------------------- *)
  205. (*                   Absolutbetrag einer komplexen Zahl                      *)
  206. (* ------------------------------------------------------------------------- *)
  207.  
  208. Function cabs (z :Complex) :Real;
  209.  
  210.    Begin
  211.    cabs := sqrt (sqr(Re(z)) + sqr(Im(z)))
  212.    End;
  213.  
  214.  
  215. (* ------------------------------------------------------------------------- *)
  216. (*          Transformation:   z = x + iy   --->   z = r*exp(i*phi)           *)
  217. (* ------------------------------------------------------------------------- *)
  218.  
  219. Procedure polar (z :Complex; Var r,phi :Real);
  220.  
  221.    Begin
  222.    r   := cabs (z);
  223.    If r<>0 then
  224.       If abs(Im(z)/r)=1 then
  225.          phi := pi/2 * (Im(z)/r)/abs(Im(z)/r)
  226.       else
  227.          phi := arctan ((Im(z))/Re(z))
  228.    else
  229.       phi := 0;
  230.    If Re(z)<0 then
  231.       If Im(z)<>0 then
  232.          phi := phi + pi * abs(Im(z))/Im(z)
  233.       else
  234.          phi := pi
  235.    End;
  236.  
  237.  
  238. (* ------------------------------------------------------------------------- *)
  239. (*          Transformation:   z = r*exp(i*phi)   --->   z = x + iy           *)
  240. (* ------------------------------------------------------------------------- *)
  241.  
  242. Function rect (r,phi :Real) :Complex;
  243.  
  244.    Begin
  245.    rect := cval (r*cos(phi), r*sin(phi))
  246.    End;
  247.  
  248.  
  249. (* ------------------------------------------------------------------------- *)
  250. (*         Division einer komplexen Zahl durch eine reelle Konstante         *)
  251. (* ------------------------------------------------------------------------- *)
  252.  
  253. Function divk (z :Complex; k :Real) :Complex;
  254.  
  255.    Begin
  256.    divk := cval (Re(z)/k, Im(z)/k)
  257.    End;
  258.  
  259.  
  260. (* ------------------------------------------------------------------------- *)
  261. (*     Multiplikation einer komplexen Zahl mit einer reellen Konstanten      *)
  262. (* ------------------------------------------------------------------------- *)
  263.  
  264. Function multk (z :Complex; k :Real) :Complex;
  265.  
  266.    Begin
  267.    multk := cval (Re(z)*k, Im(z)*k)
  268.    End;
  269.  
  270.  
  271. (* ------------------------------------------------------------------------- *)
  272. (*                           Komplexe Addition                               *)
  273. (* ------------------------------------------------------------------------- *)
  274.  
  275. Function cadd (a,b :Complex) :Complex;
  276.  
  277.    Begin
  278.    cadd := cval (Re(a)+Re(b), Im(a)+Im(b))
  279.    End;
  280.  
  281.  
  282. (* ------------------------------------------------------------------------- *)
  283. (*                         Komplexe Subtraktion                              *)
  284. (* ------------------------------------------------------------------------- *)
  285.  
  286. Function csub (a,b :Complex) :Complex;
  287.  
  288.    Begin
  289.    csub := cval (Re(a)-Re(b), Im(a)-Im(b))
  290.    End;
  291.  
  292.  
  293. (* ------------------------------------------------------------------------- *)
  294. (*                        Komplexe Multiplikation                            *)
  295. (* ------------------------------------------------------------------------- *)
  296.  
  297. Function cmult (a,b :Complex) :Complex;
  298.  
  299.    Begin
  300.    cmult := cval (Re(a)*Re(b)-Im(a)*Im(b), Re(a)*Im(b)+Im(a)*Re(b))
  301.    End;
  302.  
  303.  
  304. (* ------------------------------------------------------------------------- *)
  305. (*                           Komplexe Division                               *)
  306. (* ------------------------------------------------------------------------- *)
  307.  
  308. Function cdiv (a,b :Complex) :Complex;
  309.  
  310.    Begin
  311.    cdiv := divk (cmult(a,conj(b)), sqr(cabs(b)));
  312.    End;
  313.  
  314.  
  315. (* ------------------------------------------------------------------------- *)
  316. (*                      Kehrwert einer komplexen Zahl                        *)
  317. (* ------------------------------------------------------------------------- *)
  318.  
  319. Function cinv (z :Complex) :Complex;
  320.  
  321.    Begin
  322.    cinv := divk (conj(z), sqr(cabs(z)))
  323.    End;
  324.  
  325.  
  326. (* ------------------------------------------------------------------------- *)
  327. (*                  Negieren einer komplexen Zahl (z := -z)                  *)
  328. (* ------------------------------------------------------------------------- *)
  329.  
  330. Function cneg (z :Complex) :Complex;
  331.  
  332.    Begin
  333.    cneg := cval (-Re(z),-Im(z))
  334.    End;
  335.