ucomplex.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. begin
  335. cln.re := ln(cmod(z));
  336. cln.im := arctan2(z.im, z.re);
  337. end;
  338. function csqrt (z : complex) : complex;
  339. { square root : r := sqrt(z) }
  340. var
  341. root, q : real;
  342. begin
  343. if (z.re<>0.0) or (z.im<>0.0) then
  344. begin
  345. root := sqrt(0.5 * (abs(z.re) + cmod(z)));
  346. q := z.im / (2.0 * root);
  347. if z.re >= 0.0 then
  348. begin
  349. csqrt.re := root;
  350. csqrt.im := q;
  351. end
  352. else if z.im < 0.0 then
  353. begin
  354. csqrt.re := - q;
  355. csqrt.im := - root
  356. end
  357. else
  358. begin
  359. csqrt.re := q;
  360. csqrt.im := root
  361. end
  362. end
  363. else csqrt := z;
  364. end;
  365. operator ** (z1, z2 : complex) z : complex;
  366. { exp : z := z1 ** z2 }
  367. begin
  368. z := cexp(z2*cln(z1));
  369. end;
  370. operator ** (z1 : complex; r : real) z : complex;
  371. { multiplication : z := z1 * r }
  372. begin
  373. z := cexp( r *cln(z1));
  374. end;
  375. operator ** (r : real; z1 : complex) z : complex;
  376. { multiplication : z := r + z1 }
  377. begin
  378. z := cexp(z1*ln(r));
  379. end;
  380. { direct trigonometric functions }
  381. function ccos (z : complex) : complex;
  382. { complex cosinus }
  383. { cos(x+iy) = cos(x).cos(iy) - sin(x).sin(iy) }
  384. { cos(ix) = cosh(x) et sin(ix) = i.sinh(x) }
  385. begin
  386. ccos.re := cos(z.re) * cosh(z.im);
  387. ccos.im := - sin(z.re) * sinh(z.im);
  388. end;
  389. function csin (z : complex) : complex;
  390. { sinus complex }
  391. { sin(x+iy) = sin(x).cos(iy) + cos(x).sin(iy) }
  392. { cos(ix) = cosh(x) et sin(ix) = i.sinh(x) }
  393. begin
  394. csin.re := sin(z.re) * cosh(z.im);
  395. csin.im := cos(z.re) * sinh(z.im);
  396. end;
  397. function ctg (z : complex) : complex;
  398. { tangente }
  399. var ccosz, temp : complex;
  400. begin
  401. ccosz := ccos(z);
  402. temp := csin(z);
  403. ctg := temp / ccosz;
  404. end;
  405. { fonctions trigonometriques inverses }
  406. function carc_cos (z : complex) : complex;
  407. { arc cosinus complex }
  408. { arccos(z) = -i.argch(z) }
  409. begin
  410. carc_cos := -i*carg_ch(z);
  411. end;
  412. function carc_sin (z : complex) : complex;
  413. { arc sinus complex }
  414. { arcsin(z) = -i.argsh(i.z) }
  415. begin
  416. carc_sin := -i*carg_sh(i*z);
  417. end;
  418. function carc_tg (z : complex) : complex;
  419. { arc tangente complex }
  420. { arctg(z) = -i.argth(i.z) }
  421. begin
  422. carc_tg := -i*carg_th(i*z);
  423. end;
  424. { hyberbolic complex functions }
  425. function cch (z : complex) : complex;
  426. { hyberbolic cosinus }
  427. { cosh(x+iy) = cosh(x).cosh(iy) + sinh(x).sinh(iy) }
  428. { cosh(iy) = cos(y) et sinh(iy) = i.sin(y) }
  429. begin
  430. cch.re := cosh(z.re) * cos(z.im);
  431. cch.im := sinh(z.re) * sin(z.im);
  432. end;
  433. function csh (z : complex) : complex;
  434. { hyberbolic sinus }
  435. { sinh(x+iy) = sinh(x).cosh(iy) + cosh(x).sinh(iy) }
  436. { cosh(iy) = cos(y) et sinh(iy) = i.sin(y) }
  437. begin
  438. csh.re := sinh(z.re) * cos(z.im);
  439. csh.im := cosh(z.re) * sin(z.im);
  440. end;
  441. function cth (z : complex) : complex;
  442. { hyberbolic complex tangent }
  443. { th(x) = sinh(x) / cosh(x) }
  444. { cosh(x) > 1 qq x }
  445. var temp : complex;
  446. begin
  447. temp := cch(z);
  448. z := csh(z);
  449. cth := z / temp;
  450. end;
  451. { inverse complex hyperbolic functions }
  452. function carg_ch (z : complex) : complex;
  453. { hyberbolic arg cosinus }
  454. { _________ }
  455. { argch(z) = -/+ ln(z + i.V 1 - z.z) }
  456. begin
  457. carg_ch:=-cln(z+i*csqrt(z*z-1.0));
  458. end;
  459. function carg_sh (z : complex) : complex;
  460. { hyperbolic arc sinus }
  461. { ________ }
  462. { argsh(z) = ln(z + V 1 + z.z) }
  463. begin
  464. carg_sh:=cln(z+csqrt(z*z+1.0));
  465. end;
  466. function carg_th (z : complex) : complex;
  467. { hyperbolic arc tangent }
  468. { argth(z) = 1/2 ln((z + 1) / (1 - z)) }
  469. begin
  470. carg_th:=cln((z+1.0)/(z-1.0))/2.0;
  471. end;
  472. { functions to write out a complex value }
  473. function cstr(z : complex) : string;
  474. var
  475. istr : string;
  476. begin
  477. str(z.im,istr);
  478. str(z.re,cstr);
  479. while istr[1]=' ' do
  480. delete(istr,1,1);
  481. if z.im<0 then
  482. cstr:=cstr+istr+'i'
  483. else if z.im>0 then
  484. cstr:=cstr+'+'+istr+'i';
  485. end;
  486. function cstr(z:complex;len : integer) : string;
  487. var
  488. istr : string;
  489. begin
  490. str(z.im:len,istr);
  491. while istr[1]=' ' do
  492. delete(istr,1,1);
  493. str(z.re:len,cstr);
  494. if z.im<0 then
  495. cstr:=cstr+istr+'i'
  496. else if z.im>0 then
  497. cstr:=cstr+'+'+istr+'i';
  498. end;
  499. function cstr(z:complex;len,dec : integer) : string;
  500. var
  501. istr : string;
  502. begin
  503. str(z.im:len:dec,istr);
  504. while istr[1]=' ' do
  505. delete(istr,1,1);
  506. str(z.re:len:dec,cstr);
  507. if z.im<0 then
  508. cstr:=cstr+istr+'i'
  509. else if z.im>0 then
  510. cstr:=cstr+'+'+istr+'i';
  511. end;
  512. end.