complex.rs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //! Complex numbers.
  11. use std::fmt;
  12. use std::ops::{Add, Div, Mul, Neg, Sub};
  13. use {Zero, One, Num, Float};
  14. // FIXME #1284: handle complex NaN & infinity etc. This
  15. // probably doesn't map to C's _Complex correctly.
  16. /// A complex number in Cartesian form.
  17. #[derive(PartialEq, Copy, Clone, Hash, Debug)]
  18. #[cfg_attr(feature = "rustc-serialize", derive(RustcEncodable, RustcDecodable))]
  19. pub struct Complex<T> {
  20. /// Real portion of the complex number
  21. pub re: T,
  22. /// Imaginary portion of the complex number
  23. pub im: T
  24. }
  25. pub type Complex32 = Complex<f32>;
  26. pub type Complex64 = Complex<f64>;
  27. impl<T: Clone + Num> Complex<T> {
  28. /// Create a new Complex
  29. #[inline]
  30. pub fn new(re: T, im: T) -> Complex<T> {
  31. Complex { re: re, im: im }
  32. }
  33. /// Returns imaginary unit
  34. #[inline]
  35. pub fn i() -> Complex<T> {
  36. Self::new(T::zero(), T::one())
  37. }
  38. /// Returns the square of the norm (since `T` doesn't necessarily
  39. /// have a sqrt function), i.e. `re^2 + im^2`.
  40. #[inline]
  41. pub fn norm_sqr(&self) -> T {
  42. self.re.clone() * self.re.clone() + self.im.clone() * self.im.clone()
  43. }
  44. /// Multiplies `self` by the scalar `t`.
  45. #[inline]
  46. pub fn scale(&self, t: T) -> Complex<T> {
  47. Complex::new(self.re.clone() * t.clone(), self.im.clone() * t)
  48. }
  49. /// Divides `self` by the scalar `t`.
  50. #[inline]
  51. pub fn unscale(&self, t: T) -> Complex<T> {
  52. Complex::new(self.re.clone() / t.clone(), self.im.clone() / t)
  53. }
  54. }
  55. impl<T: Clone + Num + Neg<Output = T>> Complex<T> {
  56. /// Returns the complex conjugate. i.e. `re - i im`
  57. #[inline]
  58. pub fn conj(&self) -> Complex<T> {
  59. Complex::new(self.re.clone(), -self.im.clone())
  60. }
  61. /// Returns `1/self`
  62. #[inline]
  63. pub fn inv(&self) -> Complex<T> {
  64. let norm_sqr = self.norm_sqr();
  65. Complex::new(self.re.clone() / norm_sqr.clone(),
  66. -self.im.clone() / norm_sqr)
  67. }
  68. }
  69. impl<T: Clone + Float> Complex<T> {
  70. /// Calculate |self|
  71. #[inline]
  72. pub fn norm(&self) -> T {
  73. self.re.hypot(self.im)
  74. }
  75. /// Calculate the principal Arg of self.
  76. #[inline]
  77. pub fn arg(&self) -> T {
  78. self.im.atan2(self.re)
  79. }
  80. /// Convert to polar form (r, theta), such that `self = r * exp(i
  81. /// * theta)`
  82. #[inline]
  83. pub fn to_polar(&self) -> (T, T) {
  84. (self.norm(), self.arg())
  85. }
  86. /// Convert a polar representation into a complex number.
  87. #[inline]
  88. pub fn from_polar(r: &T, theta: &T) -> Complex<T> {
  89. Complex::new(*r * theta.cos(), *r * theta.sin())
  90. }
  91. /// Computes `e^(self)`, where `e` is the base of the natural logarithm.
  92. #[inline]
  93. pub fn exp(&self) -> Complex<T> {
  94. // formula: e^(a + bi) = e^a (cos(b) + i*sin(b))
  95. Complex::new(self.im.cos(), self.im.sin()).scale(self.re.exp())
  96. }
  97. /// Computes the principal value of natural logarithm of `self`.
  98. ///
  99. /// This function has one branch cut:
  100. ///
  101. /// * `(-∞, 0]`, continuous from above.
  102. ///
  103. /// The branch satisfies `-π ≤ arg(ln(z)) ≤ π`.
  104. #[inline]
  105. pub fn ln(&self) -> Complex<T> {
  106. // formula: ln(z) = ln|z| + i*arg(z)
  107. Complex::new(self.norm().ln(), self.arg())
  108. }
  109. /// Computes the principal value of the square root of `self`.
  110. ///
  111. /// This function has one branch cut:
  112. ///
  113. /// * `(-∞, 0)`, continuous from above.
  114. ///
  115. /// The branch satisfies `-π/2 ≤ arg(sqrt(z)) ≤ π/2`.
  116. #[inline]
  117. pub fn sqrt(&self) -> Complex<T> {
  118. // formula: sqrt(r e^(it)) = sqrt(r) e^(it/2)
  119. let two = T::one() + T::one();
  120. let (r, theta) = self.to_polar();
  121. Complex::from_polar(&(r.sqrt()), &(theta/two))
  122. }
  123. /// Computes the sine of `self`.
  124. #[inline]
  125. pub fn sin(&self) -> Complex<T> {
  126. // formula: sin(a + bi) = sin(a)cosh(b) + i*cos(a)sinh(b)
  127. Complex::new(self.re.sin() * self.im.cosh(), self.re.cos() * self.im.sinh())
  128. }
  129. /// Computes the cosine of `self`.
  130. #[inline]
  131. pub fn cos(&self) -> Complex<T> {
  132. // formula: cos(a + bi) = cos(a)cosh(b) - i*sin(a)sinh(b)
  133. Complex::new(self.re.cos() * self.im.cosh(), -self.re.sin() * self.im.sinh())
  134. }
  135. /// Computes the tangent of `self`.
  136. #[inline]
  137. pub fn tan(&self) -> Complex<T> {
  138. // formula: tan(a + bi) = (sin(2a) + i*sinh(2b))/(cos(2a) + cosh(2b))
  139. let (two_re, two_im) = (self.re + self.re, self.im + self.im);
  140. Complex::new(two_re.sin(), two_im.sinh()).unscale(two_re.cos() + two_im.cosh())
  141. }
  142. /// Computes the principal value of the inverse sine of `self`.
  143. ///
  144. /// This function has two branch cuts:
  145. ///
  146. /// * `(-∞, -1)`, continuous from above.
  147. /// * `(1, ∞)`, continuous from below.
  148. ///
  149. /// The branch satisfies `-π/2 ≤ Re(asin(z)) ≤ π/2`.
  150. #[inline]
  151. pub fn asin(&self) -> Complex<T> {
  152. // formula: arcsin(z) = -i ln(sqrt(1-z^2) + iz)
  153. let i = Complex::i();
  154. -i*((Complex::one() - self*self).sqrt() + i*self).ln()
  155. }
  156. /// Computes the principal value of the inverse cosine of `self`.
  157. ///
  158. /// This function has two branch cuts:
  159. ///
  160. /// * `(-∞, -1)`, continuous from above.
  161. /// * `(1, ∞)`, continuous from below.
  162. ///
  163. /// The branch satisfies `0 ≤ Re(acos(z)) ≤ π`.
  164. #[inline]
  165. pub fn acos(&self) -> Complex<T> {
  166. // formula: arccos(z) = -i ln(i sqrt(1-z^2) + z)
  167. let i = Complex::i();
  168. -i*(i*(Complex::one() - self*self).sqrt() + self).ln()
  169. }
  170. /// Computes the principal value of the inverse tangent of `self`.
  171. ///
  172. /// This function has two branch cuts:
  173. ///
  174. /// * `(-∞i, -i]`, continuous from the left.
  175. /// * `[i, ∞i)`, continuous from the right.
  176. ///
  177. /// The branch satisfies `-π/2 ≤ Re(atan(z)) ≤ π/2`.
  178. #[inline]
  179. pub fn atan(&self) -> Complex<T> {
  180. // formula: arctan(z) = (ln(1+iz) - ln(1-iz))/(2i)
  181. let i = Complex::i();
  182. let one = Complex::one();
  183. let two = one + one;
  184. if *self == i {
  185. return Complex::new(T::zero(), T::infinity());
  186. }
  187. else if *self == -i {
  188. return Complex::new(T::zero(), -T::infinity());
  189. }
  190. ((one + i * self).ln() - (one - i * self).ln()) / (two * i)
  191. }
  192. /// Computes the hyperbolic sine of `self`.
  193. #[inline]
  194. pub fn sinh(&self) -> Complex<T> {
  195. // formula: sinh(a + bi) = sinh(a)cos(b) + i*cosh(a)sin(b)
  196. Complex::new(self.re.sinh() * self.im.cos(), self.re.cosh() * self.im.sin())
  197. }
  198. /// Computes the hyperbolic cosine of `self`.
  199. #[inline]
  200. pub fn cosh(&self) -> Complex<T> {
  201. // formula: cosh(a + bi) = cosh(a)cos(b) + i*sinh(a)sin(b)
  202. Complex::new(self.re.cosh() * self.im.cos(), self.re.sinh() * self.im.sin())
  203. }
  204. /// Computes the hyperbolic tangent of `self`.
  205. #[inline]
  206. pub fn tanh(&self) -> Complex<T> {
  207. // formula: tanh(a + bi) = (sinh(2a) + i*sin(2b))/(cosh(2a) + cos(2b))
  208. let (two_re, two_im) = (self.re + self.re, self.im + self.im);
  209. Complex::new(two_re.sinh(), two_im.sin()).unscale(two_re.cosh() + two_im.cos())
  210. }
  211. /// Computes the principal value of inverse hyperbolic sine of `self`.
  212. ///
  213. /// This function has two branch cuts:
  214. ///
  215. /// * `(-∞i, -i)`, continuous from the left.
  216. /// * `(i, ∞i)`, continuous from the right.
  217. ///
  218. /// The branch satisfies `-π/2 ≤ Im(asinh(z)) ≤ π/2`.
  219. #[inline]
  220. pub fn asinh(&self) -> Complex<T> {
  221. // formula: arcsinh(z) = ln(z + sqrt(1+z^2))
  222. let one = Complex::one();
  223. (self + (one + self * self).sqrt()).ln()
  224. }
  225. /// Computes the principal value of inverse hyperbolic cosine of `self`.
  226. ///
  227. /// This function has one branch cut:
  228. ///
  229. /// * `(-∞, 1)`, continuous from above.
  230. ///
  231. /// The branch satisfies `-π ≤ Im(acosh(z)) ≤ π` and `0 ≤ Re(acosh(z)) < ∞`.
  232. #[inline]
  233. pub fn acosh(&self) -> Complex<T> {
  234. // formula: arccosh(z) = 2 ln(sqrt((z+1)/2) + sqrt((z-1)/2))
  235. let one = Complex::one();
  236. let two = one + one;
  237. two * (((self + one)/two).sqrt() + ((self - one)/two).sqrt()).ln()
  238. }
  239. /// Computes the principal value of inverse hyperbolic tangent of `self`.
  240. ///
  241. /// This function has two branch cuts:
  242. ///
  243. /// * `(-∞, -1]`, continuous from above.
  244. /// * `[1, ∞)`, continuous from below.
  245. ///
  246. /// The branch satisfies `-π/2 ≤ Im(atanh(z)) ≤ π/2`.
  247. #[inline]
  248. pub fn atanh(&self) -> Complex<T> {
  249. // formula: arctanh(z) = (ln(1+z) - ln(1-z))/2
  250. let one = Complex::one();
  251. let two = one + one;
  252. if *self == one {
  253. return Complex::new(T::infinity(), T::zero());
  254. }
  255. else if *self == -one {
  256. return Complex::new(-T::infinity(), T::zero());
  257. }
  258. ((one + self).ln() - (one - self).ln()) / two
  259. }
  260. /// Checks if the given complex number is NaN
  261. #[inline]
  262. pub fn is_nan(self) -> bool {
  263. self.re.is_nan() || self.im.is_nan()
  264. }
  265. /// Checks if the given complex number is infinite
  266. #[inline]
  267. pub fn is_infinite(self) -> bool {
  268. !self.is_nan() && (self.re.is_infinite() || self.im.is_infinite())
  269. }
  270. /// Checks if the given complex number is finite
  271. #[inline]
  272. pub fn is_finite(self) -> bool {
  273. self.re.is_finite() && self.im.is_finite()
  274. }
  275. /// Checks if the given complex number is normal
  276. #[inline]
  277. pub fn is_normal(self) -> bool {
  278. self.re.is_normal() && self.im.is_normal()
  279. }
  280. }
  281. impl<T: Clone + Num> From<T> for Complex<T> {
  282. #[inline]
  283. fn from(re: T) -> Complex<T> {
  284. Complex { re: re, im: T::zero() }
  285. }
  286. }
  287. impl<'a, T: Clone + Num> From<&'a T> for Complex<T> {
  288. #[inline]
  289. fn from(re: &T) -> Complex<T> {
  290. From::from(re.clone())
  291. }
  292. }
  293. macro_rules! forward_ref_ref_binop {
  294. (impl $imp:ident, $method:ident) => {
  295. impl<'a, 'b, T: Clone + Num> $imp<&'b Complex<T>> for &'a Complex<T> {
  296. type Output = Complex<T>;
  297. #[inline]
  298. fn $method(self, other: &Complex<T>) -> Complex<T> {
  299. self.clone().$method(other.clone())
  300. }
  301. }
  302. }
  303. }
  304. macro_rules! forward_ref_val_binop {
  305. (impl $imp:ident, $method:ident) => {
  306. impl<'a, T: Clone + Num> $imp<Complex<T>> for &'a Complex<T> {
  307. type Output = Complex<T>;
  308. #[inline]
  309. fn $method(self, other: Complex<T>) -> Complex<T> {
  310. self.clone().$method(other)
  311. }
  312. }
  313. }
  314. }
  315. macro_rules! forward_val_ref_binop {
  316. (impl $imp:ident, $method:ident) => {
  317. impl<'a, T: Clone + Num> $imp<&'a Complex<T>> for Complex<T> {
  318. type Output = Complex<T>;
  319. #[inline]
  320. fn $method(self, other: &Complex<T>) -> Complex<T> {
  321. self.$method(other.clone())
  322. }
  323. }
  324. }
  325. }
  326. macro_rules! forward_all_binop {
  327. (impl $imp:ident, $method:ident) => {
  328. forward_ref_ref_binop!(impl $imp, $method);
  329. forward_ref_val_binop!(impl $imp, $method);
  330. forward_val_ref_binop!(impl $imp, $method);
  331. };
  332. }
  333. /* arithmetic */
  334. forward_all_binop!(impl Add, add);
  335. // (a + i b) + (c + i d) == (a + c) + i (b + d)
  336. impl<T: Clone + Num> Add<Complex<T>> for Complex<T> {
  337. type Output = Complex<T>;
  338. #[inline]
  339. fn add(self, other: Complex<T>) -> Complex<T> {
  340. Complex::new(self.re + other.re, self.im + other.im)
  341. }
  342. }
  343. forward_all_binop!(impl Sub, sub);
  344. // (a + i b) - (c + i d) == (a - c) + i (b - d)
  345. impl<T: Clone + Num> Sub<Complex<T>> for Complex<T> {
  346. type Output = Complex<T>;
  347. #[inline]
  348. fn sub(self, other: Complex<T>) -> Complex<T> {
  349. Complex::new(self.re - other.re, self.im - other.im)
  350. }
  351. }
  352. forward_all_binop!(impl Mul, mul);
  353. // (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c)
  354. impl<T: Clone + Num> Mul<Complex<T>> for Complex<T> {
  355. type Output = Complex<T>;
  356. #[inline]
  357. fn mul(self, other: Complex<T>) -> Complex<T> {
  358. let re = self.re.clone() * other.re.clone() - self.im.clone() * other.im.clone();
  359. let im = self.re * other.im + self.im * other.re;
  360. Complex::new(re, im)
  361. }
  362. }
  363. forward_all_binop!(impl Div, div);
  364. // (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d)
  365. // == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)]
  366. impl<T: Clone + Num> Div<Complex<T>> for Complex<T> {
  367. type Output = Complex<T>;
  368. #[inline]
  369. fn div(self, other: Complex<T>) -> Complex<T> {
  370. let norm_sqr = other.norm_sqr();
  371. let re = self.re.clone() * other.re.clone() + self.im.clone() * other.im.clone();
  372. let im = self.im * other.re - self.re * other.im;
  373. Complex::new(re / norm_sqr.clone(), im / norm_sqr)
  374. }
  375. }
  376. impl<T: Clone + Num + Neg<Output = T>> Neg for Complex<T> {
  377. type Output = Complex<T>;
  378. #[inline]
  379. fn neg(self) -> Complex<T> {
  380. Complex::new(-self.re, -self.im)
  381. }
  382. }
  383. impl<'a, T: Clone + Num + Neg<Output = T>> Neg for &'a Complex<T> {
  384. type Output = Complex<T>;
  385. #[inline]
  386. fn neg(self) -> Complex<T> {
  387. -self.clone()
  388. }
  389. }
  390. macro_rules! real_arithmetic {
  391. (@forward $imp:ident::$method:ident for $($real:ident),*) => (
  392. impl<'a, T: Clone + Num> $imp<&'a T> for Complex<T> {
  393. type Output = Complex<T>;
  394. #[inline]
  395. fn $method(self, other: &T) -> Complex<T> {
  396. self.$method(other.clone())
  397. }
  398. }
  399. impl<'a, T: Clone + Num> $imp<T> for &'a Complex<T> {
  400. type Output = Complex<T>;
  401. #[inline]
  402. fn $method(self, other: T) -> Complex<T> {
  403. self.clone().$method(other)
  404. }
  405. }
  406. impl<'a, 'b, T: Clone + Num> $imp<&'a T> for &'b Complex<T> {
  407. type Output = Complex<T>;
  408. #[inline]
  409. fn $method(self, other: &T) -> Complex<T> {
  410. self.clone().$method(other.clone())
  411. }
  412. }
  413. $(
  414. impl<'a> $imp<&'a Complex<$real>> for $real {
  415. type Output = Complex<$real>;
  416. #[inline]
  417. fn $method(self, other: &Complex<$real>) -> Complex<$real> {
  418. self.$method(other.clone())
  419. }
  420. }
  421. impl<'a> $imp<Complex<$real>> for &'a $real {
  422. type Output = Complex<$real>;
  423. #[inline]
  424. fn $method(self, other: Complex<$real>) -> Complex<$real> {
  425. self.clone().$method(other)
  426. }
  427. }
  428. impl<'a, 'b> $imp<&'a Complex<$real>> for &'b $real {
  429. type Output = Complex<$real>;
  430. #[inline]
  431. fn $method(self, other: &Complex<$real>) -> Complex<$real> {
  432. self.clone().$method(other.clone())
  433. }
  434. }
  435. )*
  436. );
  437. (@implement $imp:ident::$method:ident for $($real:ident),*) => (
  438. impl<T: Clone + Num> $imp<T> for Complex<T> {
  439. type Output = Complex<T>;
  440. #[inline]
  441. fn $method(self, other: T) -> Complex<T> {
  442. self.$method(Complex::from(other))
  443. }
  444. }
  445. $(
  446. impl $imp<Complex<$real>> for $real {
  447. type Output = Complex<$real>;
  448. #[inline]
  449. fn $method(self, other: Complex<$real>) -> Complex<$real> {
  450. Complex::from(self).$method(other)
  451. }
  452. }
  453. )*
  454. );
  455. ($($real:ident),*) => (
  456. real_arithmetic!(@forward Add::add for $($real),*);
  457. real_arithmetic!(@forward Sub::sub for $($real),*);
  458. real_arithmetic!(@forward Mul::mul for $($real),*);
  459. real_arithmetic!(@forward Div::div for $($real),*);
  460. real_arithmetic!(@implement Add::add for $($real),*);
  461. real_arithmetic!(@implement Sub::sub for $($real),*);
  462. real_arithmetic!(@implement Mul::mul for $($real),*);
  463. real_arithmetic!(@implement Div::div for $($real),*);
  464. );
  465. }
  466. real_arithmetic!(usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32, f64);
  467. /* constants */
  468. impl<T: Clone + Num> Zero for Complex<T> {
  469. #[inline]
  470. fn zero() -> Complex<T> {
  471. Complex::new(Zero::zero(), Zero::zero())
  472. }
  473. #[inline]
  474. fn is_zero(&self) -> bool {
  475. self.re.is_zero() && self.im.is_zero()
  476. }
  477. }
  478. impl<T: Clone + Num> One for Complex<T> {
  479. #[inline]
  480. fn one() -> Complex<T> {
  481. Complex::new(One::one(), Zero::zero())
  482. }
  483. }
  484. /* string conversions */
  485. impl<T> fmt::Display for Complex<T> where
  486. T: fmt::Display + Num + PartialOrd + Clone
  487. {
  488. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  489. if self.im < Zero::zero() {
  490. write!(f, "{}-{}i", self.re, T::zero() - self.im.clone())
  491. } else {
  492. write!(f, "{}+{}i", self.re, self.im)
  493. }
  494. }
  495. }
  496. #[cfg(test)]
  497. mod test {
  498. #![allow(non_upper_case_globals)]
  499. use super::{Complex64, Complex};
  500. use std::f64;
  501. use {Zero, One, Float};
  502. pub const _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 };
  503. pub const _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 };
  504. pub const _1_1i : Complex64 = Complex { re: 1.0, im: 1.0 };
  505. pub const _0_1i : Complex64 = Complex { re: 0.0, im: 1.0 };
  506. pub const _neg1_1i : Complex64 = Complex { re: -1.0, im: 1.0 };
  507. pub const _05_05i : Complex64 = Complex { re: 0.5, im: 0.5 };
  508. pub const all_consts : [Complex64; 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i];
  509. #[test]
  510. fn test_consts() {
  511. // check our constants are what Complex::new creates
  512. fn test(c : Complex64, r : f64, i: f64) {
  513. assert_eq!(c, Complex::new(r,i));
  514. }
  515. test(_0_0i, 0.0, 0.0);
  516. test(_1_0i, 1.0, 0.0);
  517. test(_1_1i, 1.0, 1.0);
  518. test(_neg1_1i, -1.0, 1.0);
  519. test(_05_05i, 0.5, 0.5);
  520. assert_eq!(_0_0i, Zero::zero());
  521. assert_eq!(_1_0i, One::one());
  522. }
  523. #[test]
  524. #[cfg_attr(target_arch = "x86", ignore)]
  525. // FIXME #7158: (maybe?) currently failing on x86.
  526. fn test_norm() {
  527. fn test(c: Complex64, ns: f64) {
  528. assert_eq!(c.norm_sqr(), ns);
  529. assert_eq!(c.norm(), ns.sqrt())
  530. }
  531. test(_0_0i, 0.0);
  532. test(_1_0i, 1.0);
  533. test(_1_1i, 2.0);
  534. test(_neg1_1i, 2.0);
  535. test(_05_05i, 0.5);
  536. }
  537. #[test]
  538. fn test_scale_unscale() {
  539. assert_eq!(_05_05i.scale(2.0), _1_1i);
  540. assert_eq!(_1_1i.unscale(2.0), _05_05i);
  541. for &c in all_consts.iter() {
  542. assert_eq!(c.scale(2.0).unscale(2.0), c);
  543. }
  544. }
  545. #[test]
  546. fn test_conj() {
  547. for &c in all_consts.iter() {
  548. assert_eq!(c.conj(), Complex::new(c.re, -c.im));
  549. assert_eq!(c.conj().conj(), c);
  550. }
  551. }
  552. #[test]
  553. fn test_inv() {
  554. assert_eq!(_1_1i.inv(), _05_05i.conj());
  555. assert_eq!(_1_0i.inv(), _1_0i.inv());
  556. }
  557. #[test]
  558. #[should_panic]
  559. fn test_divide_by_zero_natural() {
  560. let n = Complex::new(2, 3);
  561. let d = Complex::new(0, 0);
  562. let _x = n / d;
  563. }
  564. #[test]
  565. fn test_inv_zero() {
  566. // FIXME #20: should this really fail, or just NaN?
  567. assert!(_0_0i.inv().is_nan());
  568. }
  569. #[test]
  570. fn test_arg() {
  571. fn test(c: Complex64, arg: f64) {
  572. assert!((c.arg() - arg).abs() < 1.0e-6)
  573. }
  574. test(_1_0i, 0.0);
  575. test(_1_1i, 0.25 * f64::consts::PI);
  576. test(_neg1_1i, 0.75 * f64::consts::PI);
  577. test(_05_05i, 0.25 * f64::consts::PI);
  578. }
  579. #[test]
  580. fn test_polar_conv() {
  581. fn test(c: Complex64) {
  582. let (r, theta) = c.to_polar();
  583. assert!((c - Complex::from_polar(&r, &theta)).norm() < 1e-6);
  584. }
  585. for &c in all_consts.iter() { test(c); }
  586. }
  587. fn close(a: Complex64, b: Complex64) -> bool {
  588. // returns true if a and b are reasonably close
  589. (a == b) || (a-b).norm() < 1e-10
  590. }
  591. #[test]
  592. fn test_exp() {
  593. assert!(close(_1_0i.exp(), _1_0i.scale(f64::consts::E)));
  594. assert!(close(_0_0i.exp(), _1_0i));
  595. assert!(close(_0_1i.exp(), Complex::new(1.0.cos(), 1.0.sin())));
  596. assert!(close(_05_05i.exp()*_05_05i.exp(), _1_1i.exp()));
  597. assert!(close(_0_1i.scale(-f64::consts::PI).exp(), _1_0i.scale(-1.0)));
  598. for &c in all_consts.iter() {
  599. // e^conj(z) = conj(e^z)
  600. assert!(close(c.conj().exp(), c.exp().conj()));
  601. // e^(z + 2 pi i) = e^z
  602. assert!(close(c.exp(), (c + _0_1i.scale(f64::consts::PI*2.0)).exp()));
  603. }
  604. }
  605. #[test]
  606. fn test_ln() {
  607. assert!(close(_1_0i.ln(), _0_0i));
  608. assert!(close(_0_1i.ln(), _0_1i.scale(f64::consts::PI/2.0)));
  609. assert!(close(_0_0i.ln(), Complex::new(f64::neg_infinity(), 0.0)));
  610. assert!(close((_neg1_1i * _05_05i).ln(), _neg1_1i.ln() + _05_05i.ln()));
  611. for &c in all_consts.iter() {
  612. // ln(conj(z() = conj(ln(z))
  613. assert!(close(c.conj().ln(), c.ln().conj()));
  614. // for this branch, -pi <= arg(ln(z)) <= pi
  615. assert!(-f64::consts::PI <= c.ln().arg() && c.ln().arg() <= f64::consts::PI);
  616. }
  617. }
  618. #[test]
  619. fn test_sqrt() {
  620. assert!(close(_0_0i.sqrt(), _0_0i));
  621. assert!(close(_1_0i.sqrt(), _1_0i));
  622. assert!(close(Complex::new(-1.0, 0.0).sqrt(), _0_1i));
  623. assert!(close(Complex::new(-1.0, -0.0).sqrt(), _0_1i.scale(-1.0)));
  624. assert!(close(_0_1i.sqrt(), _05_05i.scale(2.0.sqrt())));
  625. for &c in all_consts.iter() {
  626. // sqrt(conj(z() = conj(sqrt(z))
  627. assert!(close(c.conj().sqrt(), c.sqrt().conj()));
  628. // for this branch, -pi/2 <= arg(sqrt(z)) <= pi/2
  629. assert!(-f64::consts::PI/2.0 <= c.sqrt().arg() && c.sqrt().arg() <= f64::consts::PI/2.0);
  630. // sqrt(z) * sqrt(z) = z
  631. assert!(close(c.sqrt()*c.sqrt(), c));
  632. }
  633. }
  634. #[test]
  635. fn test_sin() {
  636. assert!(close(_0_0i.sin(), _0_0i));
  637. assert!(close(_1_0i.scale(f64::consts::PI*2.0).sin(), _0_0i));
  638. assert!(close(_0_1i.sin(), _0_1i.scale(1.0.sinh())));
  639. for &c in all_consts.iter() {
  640. // sin(conj(z)) = conj(sin(z))
  641. assert!(close(c.conj().sin(), c.sin().conj()));
  642. // sin(-z) = -sin(z)
  643. assert!(close(c.scale(-1.0).sin(), c.sin().scale(-1.0)));
  644. }
  645. }
  646. #[test]
  647. fn test_cos() {
  648. assert!(close(_0_0i.cos(), _1_0i));
  649. assert!(close(_1_0i.scale(f64::consts::PI*2.0).cos(), _1_0i));
  650. assert!(close(_0_1i.cos(), _1_0i.scale(1.0.cosh())));
  651. for &c in all_consts.iter() {
  652. // cos(conj(z)) = conj(cos(z))
  653. assert!(close(c.conj().cos(), c.cos().conj()));
  654. // cos(-z) = cos(z)
  655. assert!(close(c.scale(-1.0).cos(), c.cos()));
  656. }
  657. }
  658. #[test]
  659. fn test_tan() {
  660. assert!(close(_0_0i.tan(), _0_0i));
  661. assert!(close(_1_0i.scale(f64::consts::PI/4.0).tan(), _1_0i));
  662. assert!(close(_1_0i.scale(f64::consts::PI).tan(), _0_0i));
  663. for &c in all_consts.iter() {
  664. // tan(conj(z)) = conj(tan(z))
  665. assert!(close(c.conj().tan(), c.tan().conj()));
  666. // tan(-z) = -tan(z)
  667. assert!(close(c.scale(-1.0).tan(), c.tan().scale(-1.0)));
  668. }
  669. }
  670. #[test]
  671. fn test_asin() {
  672. assert!(close(_0_0i.asin(), _0_0i));
  673. assert!(close(_1_0i.asin(), _1_0i.scale(f64::consts::PI/2.0)));
  674. assert!(close(_1_0i.scale(-1.0).asin(), _1_0i.scale(-f64::consts::PI/2.0)));
  675. assert!(close(_0_1i.asin(), _0_1i.scale((1.0 + 2.0.sqrt()).ln())));
  676. for &c in all_consts.iter() {
  677. // asin(conj(z)) = conj(asin(z))
  678. assert!(close(c.conj().asin(), c.asin().conj()));
  679. // asin(-z) = -asin(z)
  680. assert!(close(c.scale(-1.0).asin(), c.asin().scale(-1.0)));
  681. // for this branch, -pi/2 <= asin(z).re <= pi/2
  682. assert!(-f64::consts::PI/2.0 <= c.asin().re && c.asin().re <= f64::consts::PI/2.0);
  683. }
  684. }
  685. #[test]
  686. fn test_acos() {
  687. assert!(close(_0_0i.acos(), _1_0i.scale(f64::consts::PI/2.0)));
  688. assert!(close(_1_0i.acos(), _0_0i));
  689. assert!(close(_1_0i.scale(-1.0).acos(), _1_0i.scale(f64::consts::PI)));
  690. assert!(close(_0_1i.acos(), Complex::new(f64::consts::PI/2.0, (2.0.sqrt() - 1.0).ln())));
  691. for &c in all_consts.iter() {
  692. // acos(conj(z)) = conj(acos(z))
  693. assert!(close(c.conj().acos(), c.acos().conj()));
  694. // for this branch, 0 <= acos(z).re <= pi
  695. assert!(0.0 <= c.acos().re && c.acos().re <= f64::consts::PI);
  696. }
  697. }
  698. #[test]
  699. fn test_atan() {
  700. assert!(close(_0_0i.atan(), _0_0i));
  701. assert!(close(_1_0i.atan(), _1_0i.scale(f64::consts::PI/4.0)));
  702. assert!(close(_1_0i.scale(-1.0).atan(), _1_0i.scale(-f64::consts::PI/4.0)));
  703. assert!(close(_0_1i.atan(), Complex::new(0.0, f64::infinity())));
  704. for &c in all_consts.iter() {
  705. // atan(conj(z)) = conj(atan(z))
  706. assert!(close(c.conj().atan(), c.atan().conj()));
  707. // atan(-z) = -atan(z)
  708. assert!(close(c.scale(-1.0).atan(), c.atan().scale(-1.0)));
  709. // for this branch, -pi/2 <= atan(z).re <= pi/2
  710. assert!(-f64::consts::PI/2.0 <= c.atan().re && c.atan().re <= f64::consts::PI/2.0);
  711. }
  712. }
  713. #[test]
  714. fn test_sinh() {
  715. assert!(close(_0_0i.sinh(), _0_0i));
  716. assert!(close(_1_0i.sinh(), _1_0i.scale((f64::consts::E - 1.0/f64::consts::E)/2.0)));
  717. assert!(close(_0_1i.sinh(), _0_1i.scale(1.0.sin())));
  718. for &c in all_consts.iter() {
  719. // sinh(conj(z)) = conj(sinh(z))
  720. assert!(close(c.conj().sinh(), c.sinh().conj()));
  721. // sinh(-z) = -sinh(z)
  722. assert!(close(c.scale(-1.0).sinh(), c.sinh().scale(-1.0)));
  723. }
  724. }
  725. #[test]
  726. fn test_cosh() {
  727. assert!(close(_0_0i.cosh(), _1_0i));
  728. assert!(close(_1_0i.cosh(), _1_0i.scale((f64::consts::E + 1.0/f64::consts::E)/2.0)));
  729. assert!(close(_0_1i.cosh(), _1_0i.scale(1.0.cos())));
  730. for &c in all_consts.iter() {
  731. // cosh(conj(z)) = conj(cosh(z))
  732. assert!(close(c.conj().cosh(), c.cosh().conj()));
  733. // cosh(-z) = cosh(z)
  734. assert!(close(c.scale(-1.0).cosh(), c.cosh()));
  735. }
  736. }
  737. #[test]
  738. fn test_tanh() {
  739. assert!(close(_0_0i.tanh(), _0_0i));
  740. assert!(close(_1_0i.tanh(), _1_0i.scale((f64::consts::E.powi(2) - 1.0)/(f64::consts::E.powi(2) + 1.0))));
  741. assert!(close(_0_1i.tanh(), _0_1i.scale(1.0.tan())));
  742. for &c in all_consts.iter() {
  743. // tanh(conj(z)) = conj(tanh(z))
  744. assert!(close(c.conj().tanh(), c.conj().tanh()));
  745. // tanh(-z) = -tanh(z)
  746. assert!(close(c.scale(-1.0).tanh(), c.tanh().scale(-1.0)));
  747. }
  748. }
  749. #[test]
  750. fn test_asinh() {
  751. assert!(close(_0_0i.asinh(), _0_0i));
  752. assert!(close(_1_0i.asinh(), _1_0i.scale(1.0 + 2.0.sqrt()).ln()));
  753. assert!(close(_0_1i.asinh(), _0_1i.scale(f64::consts::PI/2.0)));
  754. assert!(close(_0_1i.asinh().scale(-1.0), _0_1i.scale(-f64::consts::PI/2.0)));
  755. for &c in all_consts.iter() {
  756. // asinh(conj(z)) = conj(asinh(z))
  757. assert!(close(c.conj().asinh(), c.conj().asinh()));
  758. // asinh(-z) = -asinh(z)
  759. assert!(close(c.scale(-1.0).asinh(), c.asinh().scale(-1.0)));
  760. // for this branch, -pi/2 <= asinh(z).im <= pi/2
  761. assert!(-f64::consts::PI/2.0 <= c.asinh().im && c.asinh().im <= f64::consts::PI/2.0);
  762. }
  763. }
  764. #[test]
  765. fn test_acosh() {
  766. assert!(close(_0_0i.acosh(), _0_1i.scale(f64::consts::PI/2.0)));
  767. assert!(close(_1_0i.acosh(), _0_0i));
  768. assert!(close(_1_0i.scale(-1.0).acosh(), _0_1i.scale(f64::consts::PI)));
  769. for &c in all_consts.iter() {
  770. // acosh(conj(z)) = conj(acosh(z))
  771. assert!(close(c.conj().acosh(), c.conj().acosh()));
  772. // for this branch, -pi <= acosh(z).im <= pi and 0 <= acosh(z).re
  773. assert!(-f64::consts::PI <= c.acosh().im && c.acosh().im <= f64::consts::PI && 0.0 <= c.cosh().re);
  774. }
  775. }
  776. #[test]
  777. fn test_atanh() {
  778. assert!(close(_0_0i.atanh(), _0_0i));
  779. assert!(close(_0_1i.atanh(), _0_1i.scale(f64::consts::PI/4.0)));
  780. assert!(close(_1_0i.atanh(), Complex::new(f64::infinity(), 0.0)));
  781. for &c in all_consts.iter() {
  782. // atanh(conj(z)) = conj(atanh(z))
  783. assert!(close(c.conj().atanh(), c.conj().atanh()));
  784. // atanh(-z) = -atanh(z)
  785. assert!(close(c.scale(-1.0).atanh(), c.atanh().scale(-1.0)));
  786. // for this branch, -pi/2 <= atanh(z).im <= pi/2
  787. assert!(-f64::consts::PI/2.0 <= c.atanh().im && c.atanh().im <= f64::consts::PI/2.0);
  788. }
  789. }
  790. #[test]
  791. fn test_exp_ln() {
  792. for &c in all_consts.iter() {
  793. // e^ln(z) = z
  794. assert!(close(c.ln().exp(), c));
  795. }
  796. }
  797. #[test]
  798. fn test_trig_to_hyperbolic() {
  799. for &c in all_consts.iter() {
  800. // sin(iz) = i sinh(z)
  801. assert!(close((_0_1i * c).sin(), _0_1i * c.sinh()));
  802. // cos(iz) = cosh(z)
  803. assert!(close((_0_1i * c).cos(), c.cosh()));
  804. // tan(iz) = i tanh(z)
  805. assert!(close((_0_1i * c).tan(), _0_1i * c.tanh()));
  806. }
  807. }
  808. #[test]
  809. fn test_trig_identities() {
  810. for &c in all_consts.iter() {
  811. // tan(z) = sin(z)/cos(z)
  812. assert!(close(c.tan(), c.sin()/c.cos()));
  813. // sin(z)^2 + cos(z)^2 = 1
  814. assert!(close(c.sin()*c.sin() + c.cos()*c.cos(), _1_0i));
  815. // sin(asin(z)) = z
  816. assert!(close(c.asin().sin(), c));
  817. // cos(acos(z)) = z
  818. assert!(close(c.acos().cos(), c));
  819. // tan(atan(z)) = z
  820. // i and -i are branch points
  821. if c != _0_1i && c != _0_1i.scale(-1.0) {
  822. assert!(close(c.atan().tan(), c));
  823. }
  824. // sin(z) = (e^(iz) - e^(-iz))/(2i)
  825. assert!(close(((_0_1i*c).exp() - (_0_1i*c).exp().inv())/_0_1i.scale(2.0), c.sin()));
  826. // cos(z) = (e^(iz) + e^(-iz))/2
  827. assert!(close(((_0_1i*c).exp() + (_0_1i*c).exp().inv()).unscale(2.0), c.cos()));
  828. // tan(z) = i (1 - e^(2iz))/(1 + e^(2iz))
  829. assert!(close(_0_1i * (_1_0i - (_0_1i*c).scale(2.0).exp())/(_1_0i + (_0_1i*c).scale(2.0).exp()), c.tan()));
  830. }
  831. }
  832. #[test]
  833. fn test_hyperbolic_identites() {
  834. for &c in all_consts.iter() {
  835. // tanh(z) = sinh(z)/cosh(z)
  836. assert!(close(c.tanh(), c.sinh()/c.cosh()));
  837. // cosh(z)^2 - sinh(z)^2 = 1
  838. assert!(close(c.cosh()*c.cosh() - c.sinh()*c.sinh(), _1_0i));
  839. // sinh(asinh(z)) = z
  840. assert!(close(c.asinh().sinh(), c));
  841. // cosh(acosh(z)) = z
  842. assert!(close(c.acosh().cosh(), c));
  843. // tanh(atanh(z)) = z
  844. // 1 and -1 are branch points
  845. if c != _1_0i && c != _1_0i.scale(-1.0) {
  846. assert!(close(c.atanh().tanh(), c));
  847. }
  848. // sinh(z) = (e^z - e^(-z))/2
  849. assert!(close((c.exp() - c.exp().inv()).unscale(2.0), c.sinh()));
  850. // cosh(z) = (e^z + e^(-z))/2
  851. assert!(close((c.exp() + c.exp().inv()).unscale(2.0), c.cosh()));
  852. // tanh(z) = ( e^(2z) - 1)/(e^(2z) + 1)
  853. assert!(close((c.scale(2.0).exp() - _1_0i)/(c.scale(2.0).exp() + _1_0i), c.tanh()));
  854. }
  855. }
  856. mod complex_arithmetic {
  857. use super::{_0_0i, _1_0i, _1_1i, _0_1i, _neg1_1i, _05_05i, all_consts};
  858. use Zero;
  859. #[test]
  860. fn test_add() {
  861. assert_eq!(_05_05i + _05_05i, _1_1i);
  862. assert_eq!(_0_1i + _1_0i, _1_1i);
  863. assert_eq!(_1_0i + _neg1_1i, _0_1i);
  864. for &c in all_consts.iter() {
  865. assert_eq!(_0_0i + c, c);
  866. assert_eq!(c + _0_0i, c);
  867. }
  868. }
  869. #[test]
  870. fn test_sub() {
  871. assert_eq!(_05_05i - _05_05i, _0_0i);
  872. assert_eq!(_0_1i - _1_0i, _neg1_1i);
  873. assert_eq!(_0_1i - _neg1_1i, _1_0i);
  874. for &c in all_consts.iter() {
  875. assert_eq!(c - _0_0i, c);
  876. assert_eq!(c - c, _0_0i);
  877. }
  878. }
  879. #[test]
  880. fn test_mul() {
  881. assert_eq!(_05_05i * _05_05i, _0_1i.unscale(2.0));
  882. assert_eq!(_1_1i * _0_1i, _neg1_1i);
  883. // i^2 & i^4
  884. assert_eq!(_0_1i * _0_1i, -_1_0i);
  885. assert_eq!(_0_1i * _0_1i * _0_1i * _0_1i, _1_0i);
  886. for &c in all_consts.iter() {
  887. assert_eq!(c * _1_0i, c);
  888. assert_eq!(_1_0i * c, c);
  889. }
  890. }
  891. #[test]
  892. fn test_div() {
  893. assert_eq!(_neg1_1i / _0_1i, _1_1i);
  894. for &c in all_consts.iter() {
  895. if c != Zero::zero() {
  896. assert_eq!(c / c, _1_0i);
  897. }
  898. }
  899. }
  900. #[test]
  901. fn test_neg() {
  902. assert_eq!(-_1_0i + _0_1i, _neg1_1i);
  903. assert_eq!((-_0_1i) * _0_1i, _1_0i);
  904. for &c in all_consts.iter() {
  905. assert_eq!(-(-c), c);
  906. }
  907. }
  908. }
  909. mod real_arithmetic {
  910. use super::super::Complex;
  911. #[test]
  912. fn test_add() {
  913. assert_eq!(Complex::new(4.0, 2.0) + 0.5, Complex::new(4.5, 2.0));
  914. assert_eq!(0.5 + Complex::new(4.0, 2.0), Complex::new(4.5, 2.0));
  915. }
  916. #[test]
  917. fn test_sub() {
  918. assert_eq!(Complex::new(4.0, 2.0) - 0.5, Complex::new(3.5, 2.0));
  919. assert_eq!(0.5 - Complex::new(4.0, 2.0), Complex::new(-3.5, -2.0));
  920. }
  921. #[test]
  922. fn test_mul() {
  923. assert_eq!(Complex::new(4.0, 2.0) * 0.5, Complex::new(2.0, 1.0));
  924. assert_eq!(0.5 * Complex::new(4.0, 2.0), Complex::new(2.0, 1.0));
  925. }
  926. #[test]
  927. fn test_div() {
  928. assert_eq!(Complex::new(4.0, 2.0) / 0.5, Complex::new(8.0, 4.0));
  929. assert_eq!(0.5 / Complex::new(4.0, 2.0), Complex::new(0.1, -0.05));
  930. }
  931. }
  932. #[test]
  933. fn test_to_string() {
  934. fn test(c : Complex64, s: String) {
  935. assert_eq!(c.to_string(), s);
  936. }
  937. test(_0_0i, "0+0i".to_string());
  938. test(_1_0i, "1+0i".to_string());
  939. test(_0_1i, "0+1i".to_string());
  940. test(_1_1i, "1+1i".to_string());
  941. test(_neg1_1i, "-1+1i".to_string());
  942. test(-_neg1_1i, "1-1i".to_string());
  943. test(_05_05i, "0.5+0.5i".to_string());
  944. }
  945. #[test]
  946. fn test_hash() {
  947. let a = Complex::new(0i32, 0i32);
  948. let b = Complex::new(1i32, 0i32);
  949. let c = Complex::new(0i32, 1i32);
  950. assert!(::hash(&a) != ::hash(&b));
  951. assert!(::hash(&b) != ::hash(&c));
  952. assert!(::hash(&c) != ::hash(&a));
  953. }
  954. #[test]
  955. fn test_is_nan() {
  956. assert!(!_1_1i.is_nan());
  957. let a = Complex::new(f64::NAN, f64::NAN);
  958. assert!(a.is_nan());
  959. }
  960. #[test]
  961. fn test_is_nan_special_cases() {
  962. let a = Complex::new(0f64, f64::NAN);
  963. let b = Complex::new(f64::NAN, 0f64);
  964. assert!(a.is_nan());
  965. assert!(b.is_nan());
  966. }
  967. #[test]
  968. fn test_is_infinite() {
  969. let a = Complex::new(2f64, f64::INFINITY);
  970. assert!(a.is_infinite());
  971. }
  972. #[test]
  973. fn test_is_finite() {
  974. assert!(_1_1i.is_finite())
  975. }
  976. #[test]
  977. fn test_is_normal() {
  978. let a = Complex::new(0f64, f64::NAN);
  979. let b = Complex::new(2f64, f64::INFINITY);
  980. assert!(!a.is_normal());
  981. assert!(!b.is_normal());
  982. assert!(_1_1i.is_normal());
  983. }
  984. }