ucomplex.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Pierre Muller,
  4. member of the Free Pascal development team.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. Unit UComplex;
  12. {$ifndef VER2_0}
  13. {$INLINE ON}
  14. {$define TEST_INLINE}
  15. {$endif VER2_0}
  16. { created for FPC by Pierre Muller }
  17. { inpired from the complex unit from JD GAYRARD mai 95 }
  18. { FPC supports operator overloading }
  19. interface
  20. uses math;
  21. type complex = record
  22. re : real;
  23. im : real;
  24. end;
  25. pcomplex = ^complex;
  26. const i : complex = (re : 0.0; im : 1.0);
  27. _0 : complex = (re : 0.0; im : 0.0);
  28. { assignment overloading is also used in type conversions
  29. (beware also in implicit type conversions)
  30. after this operator any real can be passed to a function
  31. as a complex arg !! }
  32. operator := (r : real) z : complex;
  33. {$ifdef TEST_INLINE}
  34. inline;
  35. {$endif TEST_INLINE}
  36. { operator := (i : longint) z : complex;
  37. not needed because longint can be converted to real }
  38. { four operator : +, -, * , / and comparison = }
  39. operator + (z1, z2 : complex) z : complex;
  40. {$ifdef TEST_INLINE}
  41. inline;
  42. {$endif TEST_INLINE}
  43. { these ones are created because the code
  44. is simpler and thus faster }
  45. operator + (z1 : complex; r : real) z : complex;
  46. {$ifdef TEST_INLINE}
  47. inline;
  48. {$endif TEST_INLINE}
  49. operator + (r : real; z1 : complex) z : complex;
  50. {$ifdef TEST_INLINE}
  51. inline;
  52. {$endif TEST_INLINE}
  53. operator - (z1, z2 : complex) z : complex;
  54. {$ifdef TEST_INLINE}
  55. inline;
  56. {$endif TEST_INLINE}
  57. operator - (z1 : complex;r : real) z : complex;
  58. {$ifdef TEST_INLINE}
  59. inline;
  60. {$endif TEST_INLINE}
  61. operator - (r : real; z1 : complex) z : complex;
  62. {$ifdef TEST_INLINE}
  63. inline;
  64. {$endif TEST_INLINE}
  65. operator * (z1, z2 : complex) z : complex;
  66. {$ifdef TEST_INLINE}
  67. inline;
  68. {$endif TEST_INLINE}
  69. operator * (z1 : complex; r : real) z : complex;
  70. {$ifdef TEST_INLINE}
  71. inline;
  72. {$endif TEST_INLINE}
  73. operator * (r : real; z1 : complex) z : complex;
  74. {$ifdef TEST_INLINE}
  75. inline;
  76. {$endif TEST_INLINE}
  77. operator / (znum, zden : complex) z : complex;
  78. {$ifdef TEST_INLINE}
  79. inline;
  80. {$endif TEST_INLINE}
  81. operator / (znum : complex; r : real) z : complex;
  82. {$ifdef TEST_INLINE}
  83. inline;
  84. {$endif TEST_INLINE}
  85. operator / (r : real; zden : complex) z : complex;
  86. {$ifdef TEST_INLINE}
  87. inline;
  88. {$endif TEST_INLINE}
  89. { ** is the exponentiation operator }
  90. operator ** (z1, z2 : complex) z : complex;
  91. {$ifdef TEST_INLINE}
  92. inline;
  93. {$endif TEST_INLINE}
  94. operator ** (z1 : complex; r : real) z : complex;
  95. {$ifdef TEST_INLINE}
  96. inline;
  97. {$endif TEST_INLINE}
  98. operator ** (r : real; z1 : complex) z : complex;
  99. {$ifdef TEST_INLINE}
  100. inline;
  101. {$endif TEST_INLINE}
  102. operator = (z1, z2 : complex) b : boolean;
  103. {$ifdef TEST_INLINE}
  104. inline;
  105. {$endif TEST_INLINE}
  106. operator = (z1 : complex;r : real) b : boolean;
  107. {$ifdef TEST_INLINE}
  108. inline;
  109. {$endif TEST_INLINE}
  110. operator = (r : real; z1 : complex) b : boolean;
  111. {$ifdef TEST_INLINE}
  112. inline;
  113. {$endif TEST_INLINE}
  114. operator - (z1 : complex) z : complex;
  115. {$ifdef TEST_INLINE}
  116. inline;
  117. {$endif TEST_INLINE}
  118. { complex functions }
  119. function cong (z : complex) : complex; { conjuge }
  120. { inverse function 1/z }
  121. function cinv (z : complex) : complex;
  122. { complex functions with real return values }
  123. function cmod (z : complex) : real; { module }
  124. function carg (z : complex) : real; { argument : a / z = p.e^ia }
  125. { fonctions elementaires }
  126. function cexp (z : complex) : complex; { exponential }
  127. function cln (z : complex) : complex; { natural logarithm }
  128. function csqrt (z : complex) : complex; { square root }
  129. { complex trigonometric functions }
  130. function ccos (z : complex) : complex; { cosinus }
  131. function csin (z : complex) : complex; { sinus }
  132. function ctg (z : complex) : complex; { tangent }
  133. { inverse complex trigonometric functions }
  134. function carc_cos (z : complex) : complex; { arc cosinus }
  135. function carc_sin (z : complex) : complex; { arc sinus }
  136. function carc_tg (z : complex) : complex; { arc tangent }
  137. { hyperbolic complex functions }
  138. function cch (z : complex) : complex; { hyperbolic cosinus }
  139. function csh (z : complex) : complex; { hyperbolic sinus }
  140. function cth (z : complex) : complex; { hyperbolic tangent }
  141. { inverse hyperbolic complex functions }
  142. function carg_ch (z : complex) : complex; { hyperbolic arc cosinus }
  143. function carg_sh (z : complex) : complex; { hyperbolic arc sinus }
  144. function carg_th (z : complex) : complex; { hyperbolic arc tangente }
  145. { functions to write out a complex value }
  146. function cstr(z : complex) : string;
  147. function cstr(z:complex;len : integer) : string;
  148. function cstr(z:complex;len,dec : integer) : string;
  149. implementation
  150. operator := (r : real) z : complex;
  151. {$ifdef TEST_INLINE}
  152. inline;
  153. {$endif TEST_INLINE}
  154. begin
  155. z.re:=r;
  156. z.im:=0.0;
  157. end;
  158. { four base operations +, -, * , / }
  159. operator + (z1, z2 : complex) z : complex;
  160. {$ifdef TEST_INLINE}
  161. inline;
  162. {$endif TEST_INLINE}
  163. { addition : z := z1 + z2 }
  164. begin
  165. z.re := z1.re + z2.re;
  166. z.im := z1.im + z2.im;
  167. end;
  168. operator + (z1 : complex; r : real) z : complex;
  169. { addition : z := z1 + r }
  170. {$ifdef TEST_INLINE}
  171. inline;
  172. {$endif TEST_INLINE}
  173. begin
  174. z.re := z1.re + r;
  175. z.im := z1.im;
  176. end;
  177. operator + (r : real; z1 : complex) z : complex;
  178. { addition : z := r + z1 }
  179. {$ifdef TEST_INLINE}
  180. inline;
  181. {$endif TEST_INLINE}
  182. begin
  183. z.re := z1.re + r;
  184. z.im := z1.im;
  185. end;
  186. operator - (z1, z2 : complex) z : complex;
  187. {$ifdef TEST_INLINE}
  188. inline;
  189. {$endif TEST_INLINE}
  190. { substraction : z := z1 - z2 }
  191. begin
  192. z.re := z1.re - z2.re;
  193. z.im := z1.im - z2.im;
  194. end;
  195. operator - (z1 : complex; r : real) z : complex;
  196. {$ifdef TEST_INLINE}
  197. inline;
  198. {$endif TEST_INLINE}
  199. { substraction : z := z1 - r }
  200. begin
  201. z.re := z1.re - r;
  202. z.im := z1.im;
  203. end;
  204. operator - (z1 : complex) z : complex;
  205. {$ifdef TEST_INLINE}
  206. inline;
  207. {$endif TEST_INLINE}
  208. { substraction : z := - z1 }
  209. begin
  210. z.re := -z1.re;
  211. z.im := -z1.im;
  212. end;
  213. operator - (r : real; z1 : complex) z : complex;
  214. {$ifdef TEST_INLINE}
  215. inline;
  216. {$endif TEST_INLINE}
  217. { substraction : z := r - z1 }
  218. begin
  219. z.re := r - z1.re;
  220. z.im := - z1.im;
  221. end;
  222. operator * (z1, z2 : complex) z : complex;
  223. { multiplication : z := z1 * z2 }
  224. {$ifdef TEST_INLINE}
  225. inline;
  226. {$endif TEST_INLINE}
  227. begin
  228. z.re := (z1.re * z2.re) - (z1.im * z2.im);
  229. z.im := (z1.re * z2.im) + (z1.im * z2.re);
  230. end;
  231. operator * (z1 : complex; r : real) z : complex;
  232. {$ifdef TEST_INLINE}
  233. inline;
  234. {$endif TEST_INLINE}
  235. { multiplication : z := z1 * r }
  236. begin
  237. z.re := z1.re * r;
  238. z.im := z1.im * r;
  239. end;
  240. operator * (r : real; z1 : complex) z : complex;
  241. {$ifdef TEST_INLINE}
  242. inline;
  243. {$endif TEST_INLINE}
  244. { multiplication : z := r * z1 }
  245. begin
  246. z.re := z1.re * r;
  247. z.im := z1.im * r;
  248. end;
  249. operator / (znum, zden : complex) z : complex;
  250. {$ifdef TEST_INLINE}
  251. inline;
  252. {$endif TEST_INLINE}
  253. { division : z := znum / zden }
  254. var
  255. denom : real;
  256. begin
  257. with zden do denom := (re * re) + (im * im);
  258. { generates a fpu exception if denom=0 as for reals }
  259. z.re := ((znum.re * zden.re) + (znum.im * zden.im)) / denom;
  260. z.im := ((znum.im * zden.re) - (znum.re * zden.im)) / denom;
  261. end;
  262. operator / (znum : complex; r : real) z : complex;
  263. { division : z := znum / r }
  264. begin
  265. z.re := znum.re / r;
  266. z.im := znum.im / r;
  267. end;
  268. operator / (r : real; zden : complex) z : complex;
  269. { division : z := r / zden }
  270. var denom : real;
  271. begin
  272. with zden do denom := (re * re) + (im * im);
  273. { generates a fpu exception if denom=0 as for reals }
  274. z.re := (r * zden.re) / denom;
  275. z.im := - (r * zden.im) / denom;
  276. end;
  277. function cmod (z : complex): real;
  278. { module : r = |z| }
  279. begin
  280. with z do
  281. cmod := sqrt((re * re) + (im * im));
  282. end;
  283. function carg (z : complex): real;
  284. { argument : 0 / z = p ei0 }
  285. begin
  286. carg := arctan2(z.im, z.re);
  287. end;
  288. function cong (z : complex) : complex;
  289. { complex conjugee :
  290. if z := x + i.y
  291. then cong is x - i.y }
  292. begin
  293. cong.re := z.re;
  294. cong.im := - z.im;
  295. end;
  296. function cinv (z : complex) : complex;
  297. { inverse : r := 1 / z }
  298. var
  299. denom : real;
  300. begin
  301. with z do denom := (re * re) + (im * im);
  302. { generates a fpu exception if denom=0 as for reals }
  303. cinv.re:=z.re/denom;
  304. cinv.im:=-z.im/denom;
  305. end;
  306. operator = (z1, z2 : complex) b : boolean;
  307. { returns TRUE if z1 = z2 }
  308. begin
  309. b := (z1.re = z2.re) and (z1.im = z2.im);
  310. end;
  311. operator = (z1 : complex; r :real) b : boolean;
  312. { returns TRUE if z1 = r }
  313. begin
  314. b := (z1.re = r) and (z1.im = 0.0)
  315. end;
  316. operator = (r : real; z1 : complex) b : boolean;
  317. { returns TRUE if z1 = r }
  318. begin
  319. b := (z1.re = r) and (z1.im = 0.0)
  320. end;
  321. { fonctions elementaires }
  322. function cexp (z : complex) : complex;
  323. { exponantial : r := exp(z) }
  324. { exp(x + iy) = exp(x).exp(iy) = exp(x).[cos(y) + i sin(y)] }
  325. var expz : real;
  326. begin
  327. expz := exp(z.re);
  328. cexp.re := expz * cos(z.im);
  329. cexp.im := expz * sin(z.im);
  330. end;
  331. function cln (z : complex) : complex;
  332. { natural logarithm : r := ln(z) }
  333. { ln( p exp(i0)) = ln(p) + i0 + 2kpi }
  334. var modz : real;
  335. begin
  336. with z do
  337. modz := (re * re) + (im * im);
  338. cln.re := ln(modz);
  339. cln.im := arctan2(z.re, z.im);
  340. end;
  341. function csqrt (z : complex) : complex;
  342. { square root : r := sqrt(z) }
  343. var
  344. root, q : real;
  345. begin
  346. if (z.re<>0.0) or (z.im<>0.0) then
  347. begin
  348. root := sqrt(0.5 * (abs(z.re) + cmod(z)));
  349. q := z.im / (2.0 * root);
  350. if z.re >= 0.0 then
  351. begin
  352. csqrt.re := root;
  353. csqrt.im := q;
  354. end
  355. else if z.im < 0.0 then
  356. begin
  357. csqrt.re := - q;
  358. csqrt.im := - root
  359. end
  360. else
  361. begin
  362. csqrt.re := q;
  363. csqrt.im := root
  364. end
  365. end
  366. else csqrt := z;
  367. end;
  368. operator ** (z1, z2 : complex) z : complex;
  369. { exp : z := z1 ** z2 }
  370. begin
  371. z := cexp(z2*cln(z1));
  372. end;
  373. operator ** (z1 : complex; r : real) z : complex;
  374. { multiplication : z := z1 * r }
  375. begin
  376. z := cexp( r *cln(z1));
  377. end;
  378. operator ** (r : real; z1 : complex) z : complex;
  379. { multiplication : z := r + z1 }
  380. begin
  381. z := cexp(z1*ln(r));
  382. end;
  383. { direct trigonometric functions }
  384. function ccos (z : complex) : complex;
  385. { complex cosinus }
  386. { cos(x+iy) = cos(x).cos(iy) - sin(x).sin(iy) }
  387. { cos(ix) = cosh(x) et sin(ix) = i.sinh(x) }
  388. begin
  389. ccos.re := cos(z.re) * cosh(z.im);
  390. ccos.im := - sin(z.re) * sinh(z.im);
  391. end;
  392. function csin (z : complex) : complex;
  393. { sinus complex }
  394. { sin(x+iy) = sin(x).cos(iy) + cos(x).sin(iy) }
  395. { cos(ix) = cosh(x) et sin(ix) = i.sinh(x) }
  396. begin
  397. csin.re := sin(z.re) * cosh(z.im);
  398. csin.im := cos(z.re) * sinh(z.im);
  399. end;
  400. function ctg (z : complex) : complex;
  401. { tangente }
  402. var ccosz, temp : complex;
  403. begin
  404. ccosz := ccos(z);
  405. temp := csin(z);
  406. ctg := temp / ccosz;
  407. end;
  408. { fonctions trigonometriques inverses }
  409. function carc_cos (z : complex) : complex;
  410. { arc cosinus complex }
  411. { arccos(z) = -i.argch(z) }
  412. begin
  413. carc_cos := -i*carg_ch(z);
  414. end;
  415. function carc_sin (z : complex) : complex;
  416. { arc sinus complex }
  417. { arcsin(z) = -i.argsh(i.z) }
  418. begin
  419. carc_sin := -i*carg_sh(i*z);
  420. end;
  421. function carc_tg (z : complex) : complex;
  422. { arc tangente complex }
  423. { arctg(z) = -i.argth(i.z) }
  424. begin
  425. carc_tg := -i*carg_th(i*z);
  426. end;
  427. { hyberbolic complex functions }
  428. function cch (z : complex) : complex;
  429. { hyberbolic cosinus }
  430. { cosh(x+iy) = cosh(x).cosh(iy) + sinh(x).sinh(iy) }
  431. { cosh(iy) = cos(y) et sinh(iy) = i.sin(y) }
  432. begin
  433. cch.re := cosh(z.re) * cos(z.im);
  434. cch.im := sinh(z.re) * sin(z.im);
  435. end;
  436. function csh (z : complex) : complex;
  437. { hyberbolic sinus }
  438. { sinh(x+iy) = sinh(x).cosh(iy) + cosh(x).sinh(iy) }
  439. { cosh(iy) = cos(y) et sinh(iy) = i.sin(y) }
  440. begin
  441. csh.re := sinh(z.re) * cos(z.im);
  442. csh.im := cosh(z.re) * sin(z.im);
  443. end;
  444. function cth (z : complex) : complex;
  445. { hyberbolic complex tangent }
  446. { th(x) = sinh(x) / cosh(x) }
  447. { cosh(x) > 1 qq x }
  448. var temp : complex;
  449. begin
  450. temp := cch(z);
  451. z := csh(z);
  452. cth := z / temp;
  453. end;
  454. { inverse complex hyperbolic functions }
  455. function carg_ch (z : complex) : complex;
  456. { hyberbolic arg cosinus }
  457. { _________ }
  458. { argch(z) = -/+ ln(z + i.V 1 - z.z) }
  459. begin
  460. carg_ch:=-cln(z+i*csqrt(z*z-1.0));
  461. end;
  462. function carg_sh (z : complex) : complex;
  463. { hyperbolic arc sinus }
  464. { ________ }
  465. { argsh(z) = ln(z + V 1 + z.z) }
  466. begin
  467. carg_sh:=cln(z+csqrt(z*z+1.0));
  468. end;
  469. function carg_th (z : complex) : complex;
  470. { hyperbolic arc tangent }
  471. { argth(z) = 1/2 ln((z + 1) / (1 - z)) }
  472. begin
  473. carg_th:=cln((z+1.0)/(z-1.0))/2.0;
  474. end;
  475. { functions to write out a complex value }
  476. function cstr(z : complex) : string;
  477. var
  478. istr : string;
  479. begin
  480. str(z.im,istr);
  481. str(z.re,cstr);
  482. while istr[1]=' ' do
  483. delete(istr,1,1);
  484. if z.im<0 then
  485. cstr:=cstr+istr+'i'
  486. else if z.im>0 then
  487. cstr:=cstr+'+'+istr+'i';
  488. end;
  489. function cstr(z:complex;len : integer) : string;
  490. var
  491. istr : string;
  492. begin
  493. str(z.im:len,istr);
  494. while istr[1]=' ' do
  495. delete(istr,1,1);
  496. str(z.re:len,cstr);
  497. if z.im<0 then
  498. cstr:=cstr+istr+'i'
  499. else if z.im>0 then
  500. cstr:=cstr+'+'+istr+'i';
  501. end;
  502. function cstr(z:complex;len,dec : integer) : string;
  503. var
  504. istr : string;
  505. begin
  506. str(z.im:len:dec,istr);
  507. while istr[1]=' ' do
  508. delete(istr,1,1);
  509. str(z.re:len:dec,cstr);
  510. if z.im<0 then
  511. cstr:=cstr+istr+'i'
  512. else if z.im>0 then
  513. cstr:=cstr+'+'+istr+'i';
  514. end;
  515. end.