home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Edition 9 / FreelogHS09.iso / FractalExplo / complex.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-05  |  27KB  |  937 lines

  1. // -----------------------------------------------------------------
  2. // Complex functions for Formulae Compilator 2.03 and greater
  3. // Fractal Explorer
  4. // <c> 2000, Sirotinsky A, Fedorenko O.
  5. //
  6. // =================================================================
  7. // Please, do not modify without authors permission !
  8. // =================================================================
  9. Unit Complex;
  10.  
  11. Interface
  12.  
  13.   Uses SysUtils, Math;
  14.  
  15.   Type
  16.     TComplex = record
  17.       real: Extended;
  18.       imag: Extended;
  19.     end;
  20.   Function  MakeComplex(const real, imag: Extended): TComplex;
  21.   Procedure SetResult(var x,y: Extended; const complex: TComplex);
  22.  
  23.   Procedure CAddV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z+A
  24.   Function  CAdd (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z+A
  25.   Procedure CSubV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z-A
  26.   Function  CSub (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z-A
  27.   Procedure CMulV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z*A
  28.   Function  CMul (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z*A
  29.   Procedure CDivV(var Cmp1: TComplex; const Cmp2: TComplex);     //  Z:=Z/A
  30.   Function  CDiv (const Cmp1, Cmp2: TComplex): TComplex;         //  V:=Z/A
  31.   // ++19/08/2000
  32.      Function  CAddR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z+var
  33.      Function  CSubR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z-var
  34.      Function  CMulR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z*var
  35.      Function  CDivR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z/var
  36.  
  37.  
  38.   Procedure CSqrV(var Cmp1: TComplex);                           //  Z:=Z*Z
  39.   Function  CSqr (const Cmp1: TComplex): TComplex;               //  V:=Z*Z
  40.   Procedure CTripleV(var Cmp1: TComplex);                        //  Z:=Z*Z*Z
  41.   Function  CTriple (const Cmp1: TComplex): TComplex;            //  V:=Z*Z*Z
  42.   Procedure CFourV(var Cmp1: TComplex);                          //  Z:=Z*Z*Z*Z
  43.   Function  CFour (const Cmp1: TComplex): TComplex;              //  V:=Z*Z*Z*Z
  44.  
  45.   Procedure CFlipV(var Cmp1: TComplex);
  46.   Function  CFlip (const Cmp1: TComplex): TComplex;
  47.   Procedure CRevV (var Cmp1: TComplex);                          //  Z:=1/Z
  48.   Function  CRev  (const Cmp1: TComplex): TComplex;              //  V:=1/Z
  49.   Procedure CRev2V(var Cmp1: TComplex; const Cmp2: TComplex);    //  Z:=1/(Z-A)
  50.   Function  CRev2 (const Cmp1,Cmp2: TComplex): TComplex;         //  V:=1/(Z-A)
  51.   Function  CConj (const Cmp1: TComplex): TComplex;
  52.   Procedure CConjV(var Cmp1: TComplex);
  53.  
  54.   Procedure CSqrtV(var Cmp1: TComplex);                          //  Z:=Sqrt(Z)
  55.   Function  CSqrt (const Cmp1: TComplex): TComplex;              //  V:=Sqrt(Z)
  56.   Procedure CExpV (var Cmp1: TComplex);                          //  Z:=Exp(Z)
  57.   Function  CExp  (const Cmp1: TComplex): TComplex;              //  V:=Exp(Z)
  58.   Procedure CLnV  (var Cmp1: TComplex);                          //  Z:=Ln(Z)
  59.   Function  CLn   (const Cmp1: TComplex): TComplex;              //  V:=Ln(Z)
  60.   Procedure CPowerV(var Cmp1: TComplex; Cmp2: TComplex);         //  Z:=Z^A
  61.   Function  CPower (const Cmp1,Cmp2: TComplex): TComplex;        //  V:=Z^A
  62.   // ++19/08/2000
  63.      Procedure CPowerRV(var Cmp1: TComplex; t: Extended);           //  Z:=Z^var
  64.      Function  CPowerR (const Cmp1:TComplex;t: Extended): TComplex; //  V:=Z^var
  65.  
  66.   Procedure CSinV  (var Cmp1: TComplex);                         //  Z:=Sin(Z)
  67.   Function  CSin   (const Cmp1: TComplex): TComplex;             //  V:=Sin(Z)
  68.   Procedure CCosV  (var Cmp1: TComplex);                         //  Z:=Cos(Z)
  69.   Function  CCos   (const Cmp1: TComplex): TComplex;             //  V:=Cos(Z)
  70.   Procedure CTanV  (var Cmp1: TComplex);                         //  Z:=Tan(Z)
  71.   Function  CTan   (const Cmp1: TComplex): TComplex;             //  V:=Tan(Z)
  72.   Procedure CSinhV (var Cmp1: TComplex);                         //  Z:=Sinh(Z)
  73.   Function  CSinh  (const Cmp1: TComplex): TComplex;             //  V:=Sinh(Z)
  74.   Procedure CCoshV (var Cmp1: TComplex);                         //  Z:=Cosh(Z)
  75.   Function  CCosh  (const Cmp1: TComplex): TComplex;             //  V:=Cosh(Z)
  76.  
  77.   // ++20/08/2000
  78.      Procedure CCotanV(var Cmp1: TComplex);                         //  Z:=Cotan(Z)
  79.      Function  CCotan (const Cmp1: TComplex): TComplex;             //  V:=Cotan(Z)
  80.      Procedure CTanhV (var Cmp1: TComplex);                         //  Z:=Tanh(Z)
  81.      Function  CTanh  (const Cmp1: TComplex): TComplex;             //  V:=Tanh(Z)
  82.      Procedure CCotanhV(var Cmp1: TComplex);                        //  Z:=Cotanh(Z)
  83.      Function  CCotanh(const Cmp1: TComplex): TComplex;             //  V:=Cotanh(Z)
  84.  
  85.   // ++19/08/2000
  86.      Procedure CASinV (var Cmp1: TComplex);                         //  Z:=ArcSin(Z)
  87.      Function  CASin  (const Cmp1: TComplex): TComplex;             //  V:=ArcSin(Z)
  88.      Procedure CACosV (var Cmp1: TComplex);                         //  Z:=ArcCos(Z)
  89.      Function  CACos  (const Cmp1: TComplex): TComplex;             //  V:=ArcCos(Z)
  90.      Procedure CATanV (var Cmp1: TComplex);                         //  Z:=ArcTan(Z)
  91.      Function  CATan  (const Cmp1: TComplex): TComplex;             //  V:=ArcTan(Z)
  92.  
  93.   // ++19/08/2000
  94.      Procedure CASinhV(var Cmp1: TComplex);                         //  Z:=ArcSinh(Z)
  95.      Function  CASinh (const Cmp1: TComplex): TComplex;             //  V:=ArcSinh(Z)
  96.      Procedure CACoshV(var Cmp1: TComplex);                         //  Z:=ArcCosh(Z)
  97.      Function  CACosh (const Cmp1: TComplex): TComplex;             //  V:=ArcCosh(Z)
  98.      Procedure CATanhV(var Cmp1: TComplex);                         //  Z:=ArcTanh(Z)
  99.      Function  CATanh (const Cmp1: TComplex): TComplex;             //  V:=ArcTanh(Z)
  100.  
  101.   // ++22/08/2000
  102.      Procedure FuncDisp(const Fn: Integer; var Cmp1: TComplex);     //  functions dispatcher
  103.  
  104. Implementation
  105.  
  106.   Const SmallTol: Extended = 1E-25;
  107.  
  108. Function  MakeComplex(const real, imag: Extended): TComplex;
  109. Begin
  110.   Result.real:=real;
  111.   Result.imag:=imag;
  112. End;
  113.  
  114. Procedure SetResult(var x,y: Extended; const complex: TComplex);
  115. Begin
  116.   x:=complex.real;
  117.   y:=complex.imag;
  118. End;
  119.  
  120. { ------------------------------------------------------------------- }
  121. Procedure CAddV(var Cmp1: TComplex; const Cmp2: TComplex);
  122. Begin
  123.   Cmp1.real:=Cmp1.real+Cmp2.real;
  124.   Cmp1.imag:=Cmp1.imag+Cmp2.imag;
  125. End;
  126.  
  127. Function  CAdd (const Cmp1, Cmp2: TComplex): TComplex;
  128. Begin
  129.   Result.real:=Cmp1.real+Cmp2.real;
  130.   Result.imag:=Cmp1.imag+Cmp2.imag;
  131. End;
  132.  
  133. Function  CAddR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z+var
  134. Begin
  135.   Result.real:=Cmp1.real+t;
  136.   Result.imag:=Cmp1.imag;
  137. End;
  138.  
  139.  
  140. Procedure CSubV(var Cmp1: TComplex; const Cmp2: TComplex);
  141. Begin
  142.   Cmp1.real:=Cmp1.real-Cmp2.real;
  143.   Cmp1.imag:=Cmp1.imag-Cmp2.imag;
  144. End;
  145.  
  146. Function  CSub (const Cmp1, Cmp2: TComplex): TComplex;
  147. Begin
  148.   Result.real:=Cmp1.real-Cmp2.real;
  149.   Result.imag:=Cmp1.imag-Cmp2.imag;
  150. End;
  151.  
  152. Function  CSubR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z-var
  153. Begin
  154.   Result.real:=Cmp1.real-t;
  155.   Result.imag:=Cmp1.imag;
  156. End;
  157.  
  158.  
  159. Procedure CMulV(var Cmp1: TComplex; const Cmp2: TComplex);
  160.   var tmp: Extended;
  161. Begin
  162.   tmp      :=Cmp1.real*Cmp2.real - Cmp1.imag*Cmp2.imag;
  163.   Cmp1.imag:=Cmp1.real*Cmp2.imag + Cmp1.imag*Cmp2.real;
  164.   Cmp1.real:=tmp;
  165. End;
  166.  
  167. Function  CMul (const Cmp1, Cmp2: TComplex): TComplex;
  168. Begin
  169.   Result.real:=Cmp1.real*Cmp2.real - Cmp1.imag*Cmp2.imag;
  170.   Result.imag:=Cmp1.real*Cmp2.imag + Cmp1.imag*Cmp2.real;
  171. End;
  172.  
  173. Function  CMulR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z*var
  174. Begin
  175.   Result.real:=Cmp1.real*t;
  176.   Result.imag:=Cmp1.imag*t;
  177. End;
  178.  
  179.  
  180. Procedure CDivV(var Cmp1: TComplex; const Cmp2: TComplex);
  181.   var tmp1,tmp2: Extended;
  182. Begin
  183.   tmp1       := Cmp2.real*Cmp2.real + Cmp2.imag*Cmp2.imag + SmallTol;
  184.   tmp2       :=(Cmp1.real*Cmp2.real + Cmp1.imag*Cmp2.imag)/tmp1;
  185.   Cmp1.imag  :=(Cmp1.imag*Cmp2.real - Cmp1.real*Cmp2.imag)/tmp1;
  186.   Cmp1.real  := tmp2;
  187. End;
  188.  
  189. Function  CDiv (const Cmp1, Cmp2: TComplex): TComplex;
  190.   var tmp: Extended;
  191. Begin
  192.   tmp        := Cmp2.real*Cmp2.real + Cmp2.imag*Cmp2.imag + SmallTol;
  193.   Result.real:=(Cmp1.real*Cmp2.real + Cmp1.imag*Cmp2.imag)/tmp;
  194.   Result.imag:=(Cmp1.imag*Cmp2.real - Cmp1.real*Cmp2.imag)/tmp;
  195. End;
  196.  
  197. Function  CDivR(const Cmp1: TComplex; t: Extended): TComplex;  //  V:=Z/var
  198. Begin
  199.   Result.real:=Cmp1.real/t;
  200.   Result.imag:=Cmp1.imag/t;
  201. End;
  202.  
  203. { ------------------------------------------------------------------- }
  204.  
  205. Procedure CSqrV(var Cmp1: TComplex);
  206.   var tmp: Extended;
  207. Begin
  208.   tmp      :=(Cmp1.real+Cmp1.imag)*(Cmp1.real-Cmp1.imag);
  209.   Cmp1.imag:= Cmp1.real*Cmp1.imag * 2;
  210.   Cmp1.real:=tmp;
  211. End;
  212.  
  213. Function  CSqr (const Cmp1: TComplex): TComplex;
  214. Begin
  215.   Result.real:=(Cmp1.real+Cmp1.imag)*(Cmp1.real-Cmp1.imag);
  216.   Result.imag:= Cmp1.real*Cmp1.imag * 2;
  217. End;
  218.  
  219.  
  220. Procedure CTripleV(var Cmp1: TComplex);
  221.   var tmp: Extended;
  222. Begin
  223.   tmp      :=Cmp1.real*(Cmp1.real*Cmp1.real - 3*Cmp1.imag*Cmp1.imag);
  224.   Cmp1.imag:=Cmp1.imag*(3*Cmp1.real*Cmp1.real - Cmp1.imag*Cmp1.imag);
  225.   Cmp1.real:=tmp;
  226. End;
  227.  
  228. Function  CTriple (const Cmp1: TComplex): TComplex;
  229. Begin
  230.   Result.real:=Cmp1.real*(Cmp1.real*Cmp1.real - 3*Cmp1.imag*Cmp1.imag);
  231.   Result.imag:=Cmp1.imag*(3*Cmp1.real*Cmp1.real - Cmp1.imag*Cmp1.imag);
  232. End;
  233.  
  234. Procedure CFourV(var Cmp1: TComplex);
  235.   var tmpR, tmpI: Extended;
  236. Begin
  237.   tmpR:=(Cmp1.real-Cmp1.imag)*(Cmp1.real+Cmp1.imag);
  238.   tmpI:= Cmp1.real*Cmp1.imag * 2;
  239.  
  240.   Cmp1.real:=(tmpR-tmpI)*(tmpR+tmpI);
  241.   Cmp1.imag:= tmpR*tmpI * 2;
  242. End;
  243.  
  244. Function  CFour (const Cmp1: TComplex): TComplex;
  245.   var tmpR, tmpI: Extended;
  246. Begin
  247.   tmpR:=(Cmp1.real-Cmp1.imag)*(Cmp1.real+Cmp1.imag);
  248.   tmpI:= Cmp1.real*Cmp1.imag * 2;
  249.  
  250.   Result.real:=(tmpR-tmpI)*(tmpR+tmpI);
  251.   Result.imag:= tmpR*tmpI * 2;
  252. End;
  253.  
  254. { ------------------------------------------------------------------- }
  255.  
  256. Procedure CFlipV(var Cmp1: TComplex);
  257.   var tmp: Extended;
  258. Begin
  259.   tmp:=Cmp1.real;
  260.   Cmp1.real:=Cmp1.imag;
  261.   Cmp1.imag:=tmp;
  262. End;
  263.  
  264. Function  CFlip (const Cmp1: TComplex): TComplex;
  265. Begin
  266.   Result.real:=Cmp1.imag;
  267.   Result.imag:=Cmp1.real;
  268. End;
  269.  
  270.  
  271. Procedure CRevV(var Cmp1: TComplex);
  272.   var tmp: Extended;
  273. Begin
  274.   tmp       := Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag + SmallTol;
  275.   Cmp1.real := Cmp1.real/tmp;
  276.   Cmp1.imag :=-Cmp1.imag/tmp;
  277. End;
  278.  
  279.  
  280. Function  CRev (const Cmp1: TComplex): TComplex;
  281.   var tmp: Extended;
  282. Begin
  283.   tmp        := Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag + SmallTol;
  284.   Result.real:= Cmp1.real/tmp;
  285.   Result.imag:=-Cmp1.imag/tmp;
  286. End;
  287.  
  288.  
  289. Procedure CRev2V(var Cmp1: TComplex; const Cmp2: TComplex);
  290.   var tmp: Extended;
  291. Begin
  292.   tmp       := (Cmp1.real-Cmp2.real)*(Cmp1.real-Cmp2.real) +
  293.                (Cmp1.imag-Cmp2.imag)*(Cmp1.imag-Cmp2.imag) + SmallTol;
  294.   Cmp1.real := (Cmp1.real-Cmp2.real)/tmp;
  295.   Cmp1.imag := (Cmp2.imag-Cmp1.imag)/tmp;
  296. End;
  297.  
  298. Function  CRev2 (const Cmp1,Cmp2: TComplex): TComplex;
  299.   var tmp: Extended;
  300. Begin
  301.   tmp        := (Cmp1.real-Cmp2.real)*(Cmp1.real-Cmp2.real) +
  302.                 (Cmp1.imag-Cmp2.imag)*(Cmp1.imag-Cmp2.imag) + SmallTol;
  303.   Result.real:= (Cmp1.real-Cmp2.real)/tmp;
  304.   Result.imag:= (Cmp2.imag-Cmp1.imag)/tmp;
  305. End;
  306.  
  307. { ------------------------------------------------------------------- }
  308.  
  309. Procedure CSqrtV(var Cmp1: TComplex);                          //  Z:=Sqrt(Z)
  310.   var a,b: Extended;
  311. Begin
  312.   With Cmp1 do begin
  313.     a:=Sqrt(Sqrt(real*real+imag*imag));
  314.     b:=ArcTan2(imag,real)/2;
  315.     real:=a*cos(b);
  316.     imag:=a*sin(b);
  317.   end;
  318. End;
  319.  
  320. Function  CSqrt (const Cmp1: TComplex): TComplex;              //  V:=Sqrt(Z)
  321.   var a,b: Extended;
  322. Begin
  323.   With Cmp1 do begin
  324.     a:=Sqrt(Sqrt(real*real+imag*imag));
  325.     b:=ArcTan2(imag,real)/2;
  326.     Result.real:=a*cos(b);
  327.     Result.imag:=a*sin(b);
  328.   end;
  329. End;
  330.  
  331.  
  332. Procedure CExpV (var Cmp1: TComplex);                          //  Z:=Exp(Z)
  333.   var tmp: Extended;
  334. Begin
  335.   tmp :=Exp(Cmp1.real);
  336.   Cmp1.real:=tmp*Cos(Cmp1.imag);
  337.   Cmp1.imag:=tmp*Sin(Cmp1.imag);
  338. End;
  339.  
  340. Function  CExp  (const Cmp1: TComplex): TComplex;              //  V:=Exp(Z)
  341.   var tmp: Extended;
  342. Begin
  343.   tmp :=Exp(Cmp1.real);
  344.   Result.real:=tmp*Cos(Cmp1.imag);
  345.   Result.imag:=tmp*Sin(Cmp1.imag);
  346. End;
  347.  
  348.  
  349. Procedure CLnV  (var Cmp1: TComplex);                          //  Z:=Ln(Z)
  350.   var tmp: Extended;
  351. Begin
  352.   tmp :=Log2(Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag)/2.7182818285;
  353.   Cmp1.imag:=ArcTan2(Cmp1.imag, Cmp1.real);
  354.   Cmp1.real:=tmp;
  355. End;
  356.  
  357. Function  CLn   (const Cmp1: TComplex): TComplex;              //  V:=Ln(Z)
  358. Begin
  359.   Result.real:=Log2(Cmp1.real*Cmp1.real + Cmp1.imag*Cmp1.imag)/2.7182818285;
  360.   Result.imag:=ArcTan2(Cmp1.imag, Cmp1.real);
  361. End;
  362.  
  363.  
  364. Procedure CPowerV(var Cmp1: TComplex; Cmp2: TComplex);         //  Z:=Z^A
  365.   var h1x,h1y: Extended;
  366.       h2x,h2y: Extended;
  367.       f      : Extended;
  368. Begin
  369.   h1x:=Log2(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag)/2.7182818285;
  370.   h1y:=ArcTan2(Cmp1.imag, Cmp1.real);
  371.  
  372.   h2x:=h1x*Cmp2.real-h1y*Cmp2.imag;
  373.   h2y:=h1y*Cmp2.real+h1x*Cmp2.imag;
  374.  
  375.   f  :=Exp(h2x);
  376.   Cmp1.real :=f*Cos(h2y);
  377.   Cmp1.imag :=f*Sin(h2y);
  378. End;
  379.  
  380. Function  CPower (const Cmp1,Cmp2: TComplex): TComplex;        //  V:=Z^A
  381.   var h1x,h1y: Extended;
  382.       h2x,h2y: Extended;
  383.       f      : Extended;
  384. Begin
  385.   h1x:=Log2(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag)/2.7182818285;
  386.   h1y:=ArcTan2(Cmp1.imag, Cmp1.real);
  387.  
  388.   h2x:=h1x*Cmp2.real-h1y*Cmp2.imag;
  389.   h2y:=h1y*Cmp2.real+h1x*Cmp2.imag;
  390.  
  391.   f  :=Exp(h2x);
  392.   Result.real :=f*Cos(h2y);
  393.   Result.imag :=f*Sin(h2y);
  394. End;
  395.  
  396.  
  397. Procedure CPowerRV(var Cmp1: TComplex; t: Extended);           //  Z:=Z^var
  398.   var tr,ti,f: Extended;
  399. Begin
  400.   tr:=t*Log2(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag)/2.7182818285;
  401.   ti:=t*ArcTan2(Cmp1.imag,Cmp1.real);
  402.   f  :=Exp(tr);
  403.   Cmp1.real:=f*Cos(ti);
  404.   Cmp1.imag:=f*Sin(ti);
  405. End;
  406.  
  407. Function  CPowerR (const Cmp1:TComplex;t: Extended): TComplex; //  V:=Z^var
  408.   var tr,ti,f: Extended;
  409. Begin
  410.   tr:=t*Log2(Cmp1.real*Cmp1.real+Cmp1.imag*Cmp1.imag)/2.7182818285;
  411.   ti:=t*ArcTan2(Cmp1.imag,Cmp1.real);
  412.   f  :=Exp(tr);
  413.   Result.real:=f*Cos(ti);
  414.   Result.imag:=f*Sin(ti);
  415. End;
  416.  
  417.  
  418.  
  419.  
  420. { ------------------------------------------------------------------- }
  421. Procedure CSinV (var Cmp1: TComplex);                          //  Z:=Sin(Z)
  422.   var tmp1,tmp2,tmp3: Extended;
  423. Begin
  424.   tmp1:=Exp(Cmp1.imag)/2;
  425.   tmp2:=0.25/tmp1;
  426.   tmp3:=Sin(Cmp1.real)*(tmp1+tmp2);
  427.   Cmp1.imag:=Cos(Cmp1.real)*(tmp1-tmp2);
  428.   Cmp1.real:=tmp3;
  429. End;
  430.  
  431. Function  CSin  (const Cmp1: TComplex): TComplex;              //  V:=Sin(Z)
  432.   var tmp1,tmp2: Extended;
  433. Begin
  434.   tmp1:=Exp(Cmp1.imag)/2;
  435.   tmp2:=0.25/tmp1;
  436.   Result.real:=Sin(Cmp1.real)*(tmp1+tmp2);
  437.   Result.imag:=Cos(Cmp1.real)*(tmp1-tmp2);
  438. End;
  439.  
  440.  
  441. Procedure CCosV (var Cmp1: TComplex);                          //  Z:=Cos(Z)
  442.   var tmp1: Extended;
  443. Begin
  444.   tmp1:=Cos(Cmp1.real)*Cosh(Cmp1.imag);
  445.   Cmp1.imag:=-Sin(Cmp1.real)*Sinh(Cmp1.imag);
  446.   Cmp1.real:=tmp1;
  447. End;
  448.  
  449. Function  CCos  (const Cmp1: TComplex): TComplex;              //  V:=Cos(Z)
  450. Begin
  451.   Result.real:=Cos(Cmp1.real)*Cosh(Cmp1.imag);
  452.   Result.imag:=-Sin(Cmp1.real)*Sinh(Cmp1.imag);
  453. End;
  454.  
  455.  
  456. Procedure CTanV (var Cmp1: TComplex);                          //  Z:=Tan(Z)
  457.   var b: Extended;
  458. Begin
  459.   b:=Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag);
  460.   If b=0 Then b:=SmallTol;
  461.   Cmp1.real:=Sin (2*Cmp1.real)/b;
  462.   Cmp1.imag:=Sinh(2*Cmp1.imag)/b;
  463. End;
  464.  
  465. Function  CTan  (const Cmp1: TComplex): TComplex;              //  V:=Tan(Z)
  466.   var b: Extended;
  467. Begin
  468.   b:=Cos(2*Cmp1.real)+Cosh(2*Cmp1.imag);
  469.   If b=0 Then b:=SmallTol;
  470.   Result.real:=Sin (2*Cmp1.real)/b;
  471.   Result.imag:=Sinh(2*Cmp1.imag)/b;
  472. End;
  473.  
  474.  
  475. Procedure CCotanV(var Cmp1: TComplex);                         //  Z:=Cotan(Z)
  476.   var b: Extended;
  477. Begin
  478.   b:=Cosh(2*Cmp1.imag)-Cos(2*Cmp1.real);
  479.   If b=0 Then b:=SmallTol;
  480.   Cmp1.real:= Sin( 2*Cmp1.real)/b;
  481.   Cmp1.imag:=-Sinh(2*Cmp1.imag)/b;
  482. End;
  483.  
  484. Function  CCotan (const Cmp1: TComplex): TComplex;             //  V:=Cotan(Z)
  485.   var b: Extended;
  486. Begin
  487.   b:=Cosh(2*Cmp1.imag)-Cos(2*Cmp1.real);
  488.   If b=0 Then b:=SmallTol;
  489.   Result.real:= Sin( 2*Cmp1.real)/b;
  490.   Result.imag:=-Sinh(2*Cmp1.imag)/b;
  491. End;
  492.  
  493.  
  494.  
  495. Procedure CSinhV(var Cmp1: TComplex);                          //  Z:=Sinh(Z)
  496.   var tmp1: Extended;
  497. Begin
  498.   tmp1:=Sinh(Cmp1.real)*Cos(Cmp1.imag);
  499.   Cmp1.imag:=Cosh(Cmp1.real)*Sin(Cmp1.imag);
  500.   Cmp1.real:=tmp1;
  501. End;
  502.  
  503. Function  CSinh (const Cmp1: TComplex): TComplex;              //  V:=Sinh(Z)
  504. Begin
  505.   Result.real:=Sinh(Cmp1.real)*Cos(Cmp1.imag);
  506.   Result.imag:=Cosh(Cmp1.real)*Sin(Cmp1.imag);
  507. End;
  508.  
  509.  
  510. Procedure CCoshV(var Cmp1: TComplex);                          //  Z:=Cosh(Z)
  511.   var tmp1: Extended;
  512. Begin
  513.   tmp1:=Cosh(Cmp1.real)*Cos(Cmp1.imag);
  514.   Cmp1.imag:=Sinh(Cmp1.real)*Sin(Cmp1.imag);
  515.   Cmp1.real:=tmp1;
  516. End;
  517.  
  518. Function  CCosh (const Cmp1: TComplex): TComplex;              //  V:=Cosh(Z)
  519. Begin
  520.   Result.real:=Cosh(Cmp1.real)*Cos(Cmp1.imag);
  521.   Result.imag:=Sinh(Cmp1.real)*Sin(Cmp1.imag);
  522. End;
  523.  
  524.  
  525. Procedure CTanhV (var Cmp1: TComplex);                         //  Z:=Tanh(Z)
  526.   var b: Extended;
  527. Begin
  528.   b:=Cosh(2*Cmp1.real)+Cos(2*Cmp1.imag);
  529.   If b=0 Then b:=SmallTol;
  530.   Cmp1.real:=Sinh(2*Cmp1.real)/b;
  531.   Cmp1.imag:=Sin( 2*Cmp1.imag)/b;
  532. End;
  533.  
  534. Function  CTanh  (const Cmp1: TComplex): TComplex;             //  V:=Tanh(Z)
  535.   var b: Extended;
  536. Begin
  537.   b:=Cosh(2*Cmp1.real)+Cos(2*Cmp1.imag);
  538.   If b=0 Then b:=SmallTol;
  539.   Result.real:=Sinh(2*Cmp1.real)/b;
  540.   Result.imag:=Sin( 2*Cmp1.imag)/b;
  541. End;
  542.  
  543.  
  544. Procedure CCotanhV(var Cmp1: TComplex);                        //  Z:=Cotanh(Z)
  545.   var b: Extended;
  546. Begin
  547.   b:=Cosh(2*Cmp1.real)-Cos(2*Cmp1.imag);
  548.   If b=0 Then b:=SmallTol;
  549.   Cmp1.real:= Sinh(2*Cmp1.real)/b;
  550.   Cmp1.imag:=-Sin( 2*Cmp1.imag)/b;
  551. End;
  552.  
  553. Function  CCotanh(const Cmp1: TComplex): TComplex;             //  V:=Cotanh(Z)
  554.   var b: Extended;
  555. Begin
  556.   b:=Cosh(2*Cmp1.real)-Cos(2*Cmp1.imag);
  557.   If b=0 Then b:=SmallTol;
  558.   Result.real:= Sinh(2*Cmp1.real)/b;
  559.   Result.imag:=-Sin( 2*Cmp1.imag)/b;
  560. End;
  561.  
  562. { ------------------------------------------------------------------- }
  563. Procedure CASinV (var Cmp1: TComplex);                         //  Z:=Sin(Z)
  564.   var a,b  : Extended;
  565.       xr,xi: Extended;    //  [-i * log(i*z+sqrt(1-z*z))]
  566. Begin
  567.   With Cmp1 do begin
  568.     a :=(real - imag)*(real + imag);
  569.     xi:= -2*real*imag;
  570.     xr:=1-a;
  571.  
  572.     a :=Sqrt(Sqrt(xr*xr+xi*xi));
  573.     b :=ArcTan2(xi,xr)/2;
  574.     xr:=a*cos(b)-imag;
  575.     xi:=a*sin(b)+real;
  576.  
  577.     imag:=-Ln(xr*xr + xi*xi + SmallTol)/2;
  578.     real:=ArcTan2(xi, xr);
  579.  end;
  580. End;
  581.  
  582. Function  CASin  (const Cmp1: TComplex): TComplex;             //  V:=Sin(Z)
  583.   var a,b  : Extended;
  584.       xr,xi: Extended;    //  [-i * log(i*z+sqrt(1-z*z))]
  585. Begin
  586.   With Cmp1 do begin
  587.     a :=(real - imag)*(real + imag);
  588.     xi:= -2*real*imag;
  589.     xr:=1-a;
  590.  
  591.     a :=Sqrt(Sqrt(xr*xr+xi*xi));
  592.     b :=ArcTan2(xi,xr)/2;
  593.     xr:=a*cos(b)-imag;
  594.     xi:=a*sin(b)+real;
  595.  
  596.     Result.imag:=-Ln(xr*xr + xi*xi + SmallTol)/2;
  597.     Result.real:=ArcTan2(xi, xr);
  598.  end;
  599. End;
  600.  
  601.  
  602. Procedure CACosV (var Cmp1: TComplex);                         //  Z:=Cos(Z)
  603.   var a,b  : Extended;
  604.       xr,xi: Extended;    //  [-i * log(z+sqrt(z*z-1))]
  605. Begin
  606.   With Cmp1 do begin
  607.     a :=(real - imag)*(real + imag);
  608.     xi:=2*real*imag;
  609.     xr:=a-1;
  610.  
  611.     a :=Sqrt(Sqrt(xr*xr+xi*xi));
  612.     b :=ArcTan2(xi,xr)/2;
  613.     xr:=a*cos(b)+real;
  614.     xi:=a*sin(b)+imag;
  615.  
  616.     imag:=-Ln(xr*xr + xi*xi + SmallTol)/2;
  617.     real:=ArcTan2(xi, xr);
  618.   end;
  619. End;
  620.  
  621.  
  622. Function  CACos  (const Cmp1: TComplex): TComplex;             //  V:=Cos(Z)
  623.   var a,b  : Extended;
  624.       xr,xi: Extended;    //  [-i * log(z+sqrt(z*z-1))]
  625. Begin
  626.   With Cmp1 do begin
  627.     a :=(real - imag)*(real + imag);
  628.     xi:=2*real*imag;
  629.     xr:=a-1;
  630.  
  631.     a :=Sqrt(Sqrt(xr*xr+xi*xi));
  632.     b :=ArcTan2(xi,xr)/2;
  633.     xr:=a*cos(b)+real;
  634.     xi:=a*sin(b)+imag;
  635.  
  636.     Result.imag:=-Ln(xr*xr + xi*xi + SmallTol)/2;
  637.     Result.real:=ArcTan2(xi, xr);
  638.   end;
  639. End;
  640.  
  641. Procedure CATanV (var Cmp1: TComplex);                         //  Z:=ArcTan(Z)
  642.   var a,b  : Extended;
  643.       xr,xi: Extended;
  644.       tr,ti: Extended;
  645.       f    : Extended;
  646. Begin
  647. //  i/2 * log((1-i*z)/(1+i*z))
  648.   a :=1+Cmp1.imag; b := -Cmp1.real;
  649.   xr:=1-Cmp1.imag; xi:=  Cmp1.real;
  650.  
  651.   f:= xr*xr + xi*xi + SmallTol;
  652.   tr:=(a*xr + b*xi)/f;
  653.   ti:=(b*xr - a*xi)/f;
  654.  
  655.   xr:=Ln(tr*tr + ti*ti + SmallTol)/4;
  656.   xi:=ArcTan2(ti, tr)/2;
  657.   Cmp1.real:=-xi;
  658.   Cmp1.imag:= xr;
  659. End;
  660.  
  661. Function  CATan  (const Cmp1: TComplex): TComplex;             //  V:=ArcTan(Z)
  662.   var a,b  : Extended;
  663.       xr,xi: Extended;
  664.       tr,ti: Extended;
  665.       f    : Extended;
  666. Begin
  667. //  i/2 * log((1-i*z)/(1+i*z))
  668.   a :=1+Cmp1.imag; b := -Cmp1.real;
  669.   xr:=1-Cmp1.imag; xi:=  Cmp1.real;
  670.  
  671.   f:= xr*xr + xi*xi + SmallTol;
  672.   tr:=(a*xr + b*xi)/f;
  673.   ti:=(b*xr - a*xi)/f;
  674.  
  675.   xr:=Ln(tr*tr + ti*ti + SmallTol)/4;
  676.   xi:=ArcTan2(ti, tr)/2;
  677.   Result.real:=-xi;
  678.   Result.imag:= xr;
  679. End;
  680.  
  681. { ------------------------------------------------------------------- }
  682. Procedure CASinhV(var Cmp1: TComplex);                         //  Z:=ArcSinh(Z)
  683.   var a,b  : Extended;
  684.       xr,xi: Extended;
  685. Begin
  686. //  log(z+sqrt(z*z+1))
  687.   a :=(Cmp1.real - Cmp1.imag)*(Cmp1.real + Cmp1.imag);
  688.   xi:=2*Cmp1.real*Cmp1.imag;
  689.   xr:=a+1;
  690.  
  691.   a :=Sqrt(Sqrt(xr*xr+xi*xi));
  692.   b :=ArcTan2(xi,xr)/2;
  693.   xr:=a*cos(b)+Cmp1.real;
  694.   xi:=a*sin(b)+Cmp1.imag;
  695.  
  696.   Cmp1.real:=Ln(xr*xr + xi*xi + SmallTol)/2;
  697.   Cmp1.imag:=ArcTan2(xi, xr);
  698. End;
  699.  
  700. Function  CASinh (const Cmp1: TComplex): TComplex;             //  V:=ArcSinh(Z)
  701.   var a,b  : Extended;      //  log(z+sqrt(z*z+1))
  702.       xr,xi: Extended;
  703. Begin
  704.   a :=(Cmp1.real - Cmp1.imag)*(Cmp1.real + Cmp1.imag);
  705.   xi:=2*Cmp1.real*Cmp1.imag;
  706.   xr:=a+1;
  707.  
  708.   a :=Sqrt(Sqrt(xr*xr+xi*xi));
  709.   b :=ArcTan2(xi,xr)/2;
  710.   xr:=a*cos(b)+Cmp1.real;
  711.   xi:=a*sin(b)+Cmp1.imag;
  712.  
  713.   Result.real:=Ln(xr*xr + xi*xi + SmallTol)/2;
  714.   Result.imag:=ArcTan2(xi, xr);
  715. End;
  716.  
  717.  
  718. Procedure CACoshV(var Cmp1: TComplex);                         //  Z:=ArcCosh(Z)
  719.   var a,b  : Extended;    //  log(z+sqrt(z*z-1))
  720.       xr,xi: Extended;
  721. Begin
  722.   a :=(Cmp1.real - Cmp1.imag)*(Cmp1.real + Cmp1.imag);
  723.   xi:=2*Cmp1.real*Cmp1.imag;
  724.   xr:=a-1;
  725.  
  726.   a :=Sqrt(Sqrt(xr*xr+xi*xi));
  727.   b :=ArcTan2(xi,xr)/2;
  728.   xr:=a*cos(b)+Cmp1.real;
  729.   xi:=a*sin(b)+Cmp1.imag;
  730.  
  731.   Cmp1.real:=Ln(xr*xr + xi*xi + SmallTol)/2;
  732.   Cmp1.imag:=ArcTan2(xi, xr);
  733. End;
  734.  
  735. Function  CACosh (const Cmp1: TComplex): TComplex;             //  V:=ArcCosh(Z)
  736.   var a,b  : Extended;    //  log(z+sqrt(z*z-1))
  737.       xr,xi: Extended;
  738. Begin
  739.   a :=(Cmp1.real - Cmp1.imag)*(Cmp1.real + Cmp1.imag);
  740.   xi:=2*Cmp1.real*Cmp1.imag;
  741.   xr:=a-1;
  742.  
  743.   a :=Sqrt(Sqrt(xr*xr+xi*xi));
  744.   b :=ArcTan2(xi,xr)/2;
  745.   xr:=a*cos(b)+Cmp1.real;
  746.   xi:=a*sin(b)+Cmp1.imag;
  747.  
  748.   Result.real:=Ln(xr*xr + xi*xi + SmallTol)/2;
  749.   Result.imag:=ArcTan2(xi, xr);
  750. End;
  751.  
  752.  
  753. Procedure CATanhV(var Cmp1: TComplex);                         //  Z:=ArcTanh(Z)
  754.   var a,b  : Extended;    //  log((1+z)/(1-z))/2
  755.       xr,xi: Extended;
  756.       tr,ti: Extended;
  757.       f    : Extended;
  758. Begin
  759.   a :=1+Cmp1.real; b := Cmp1.imag;
  760.   xr:=1-Cmp1.real; xi:=-Cmp1.imag;
  761.  
  762.   f:= xr*xr + xi*xi + SmallTol;
  763.   tr:=(a*xr + b*xi)/f;
  764.   ti:=(b*xr - a*xi)/f;
  765.  
  766.   Cmp1.real:=Ln(tr*tr + ti*ti + SmallTol)/4;
  767.   Cmp1.imag:=ArcTan2(ti, tr)/2;
  768. End;
  769.  
  770. Function  CATanh (const Cmp1: TComplex): TComplex;             //  V:=ArcTanh(Z)
  771.   var a,b  : Extended;    //  log((1+z)/(1-z))/2
  772.       xr,xi: Extended;
  773.       tr,ti: Extended;
  774.       f    : Extended;
  775. Begin
  776.   a :=1+Cmp1.real; b := Cmp1.imag;
  777.   xr:=1-Cmp1.real; xi:=-Cmp1.imag;
  778.  
  779.   f:= xr*xr + xi*xi + SmallTol;
  780.   tr:=(a*xr + b*xi)/f;
  781.   ti:=(b*xr - a*xi)/f;
  782.  
  783.   Result.real:=Ln(tr*tr + ti*ti + SmallTol)/4;
  784.   Result.imag:=ArcTan2(ti, tr)/2;
  785. End;
  786.  
  787.  
  788. { =================================================================== }
  789. Procedure CCabsV(var Cmp1: TComplex);
  790. Begin
  791.   With Cmp1 do begin
  792.     real:=Sqrt(real*real+imag*imag);
  793.     imag:=0;
  794.   end;
  795. End;
  796.  
  797.  
  798. Procedure CAbsV(var Cmp1: TComplex);
  799. Begin
  800.   With Cmp1 do begin
  801.     real:=Abs(real);
  802.   end;
  803. End;
  804.  
  805. Procedure CFloorV(var Cmp1: TComplex);
  806. Begin
  807.   With Cmp1 do begin
  808.     real:=Floor(real);
  809.     imag:=Floor(imag);
  810.   end;
  811. End;
  812.  
  813. Procedure CCeilV(var Cmp1: TComplex);
  814. Begin
  815.   With Cmp1 do begin
  816.     real:=Trunc(real);
  817.     imag:=Trunc(imag);
  818.   end;
  819. End;
  820.  
  821. Procedure CTruncV(var Cmp1: TComplex);
  822. Begin
  823.   With Cmp1 do begin
  824.     real:=Trunc(real);
  825.     imag:=Trunc(imag);
  826.   end;
  827. End;
  828.  
  829. Procedure CRoundV(var Cmp1: TComplex);
  830. Begin
  831.   With Cmp1 do begin
  832.     real:=Round(real);
  833.     imag:=Round(imag);
  834.   end;
  835. End;
  836.  
  837. Procedure COneV(var Cmp1: TComplex);
  838. Begin
  839.   With Cmp1 do begin
  840.     real:=1;
  841.     imag:=0;
  842.  end;
  843. End;
  844.  
  845. Procedure CCosxxV(var Cmp1: TComplex);
  846.   var a: Extended;
  847. Begin
  848. //  cos(x)cosh(y) + i sin(x)sinh(y)
  849.   With Cmp1 do begin
  850.     a   :=Cos(real)*Cosh(imag);
  851.     imag:=Sin(real)*Sinh(imag);
  852.     real:=a;
  853.   end;
  854. End;
  855.  
  856. Procedure CRecipV(var Cmp1: TComplex);
  857.   var a: Extended;
  858. Begin
  859. //  (x-iy) / (x^2+y^2)
  860.   With Cmp1 do begin
  861.     a   := real*real + imag*imag + SmallTol;
  862.     real:= real/a;
  863.     imag:=-imag/a
  864.   end;
  865. End;
  866.  
  867. Function  CConj(const Cmp1: TComplex): TComplex;
  868. Begin
  869.   Result.real:= Cmp1.real;
  870.   Result.imag:=-Cmp1.imag;
  871. End;
  872.  
  873. Procedure CConjV(var Cmp1: TComplex);
  874. Begin
  875.   Cmp1.imag:=-Cmp1.imag;
  876. End;
  877.  
  878.  
  879. Procedure CZeroV(var Cmp1: TComplex);
  880. Begin
  881.   With Cmp1 do begin
  882.     real:=0;
  883.     imag:=0;
  884.   end;
  885. End;
  886.  
  887. Procedure CLogV(var Cmp1: TComplex);
  888.   var a: Extended;
  889. Begin
  890. //  (1/2)ln(x^2 + y^2) + i*arctan2(y/x)
  891.   With Cmp1 do begin
  892.     a   :=Ln(real*real + imag*imag + SmallTol)/2;
  893.     imag:=ArcTan2(imag, real);
  894.     real:=a;
  895.   end;
  896. End;
  897.  
  898. { ------------------------------------------------------------------- }
  899. Procedure FuncDisp(const Fn: Integer; var Cmp1: TComplex);     //  dispatcher
  900. Begin
  901.   Case Fn of
  902.     0: ;                     // CIdent(Cmp1);
  903.     1: CCosV(Cmp1);
  904.     2: CTanV(Cmp1);
  905.     3: CTanhV(Cmp1);
  906.     4: CCotanV(Cmp1);
  907.     5: CCotanhV(Cmp1);
  908.     6: CFlipV(Cmp1);
  909.     7: CConjV(Cmp1);
  910.     8: CZeroV(Cmp1);
  911.     9: CASinV(Cmp1);
  912.    10: CASinhV(Cmp1);
  913.    11: CACosV(Cmp1);
  914.    12: CACoshV(Cmp1);
  915.    13: CATanV(Cmp1);
  916.    14: CATanhV(Cmp1);
  917.    15: CCabsV(Cmp1);
  918.    16: CAbsV(Cmp1);
  919.    17: CSqrtV(Cmp1);
  920.    18: CFloorV(Cmp1);
  921.    19: CCeilV(Cmp1);
  922.    20: CTruncV(Cmp1);
  923.    21: CRoundV(Cmp1);
  924.    22: COneV(Cmp1);
  925.    23: CSinV(Cmp1);
  926.    24: CCosxxV(Cmp1);
  927.    25: CSinhV(Cmp1);
  928.    26: CCoshV(Cmp1);
  929.    27: CExpV(Cmp1);
  930.    28: CLogV(Cmp1);
  931.    29: CSqrV(Cmp1);
  932.    30: CRecipV(Cmp1);
  933.   end;
  934. End;
  935.  
  936. END.
  937.