lib.rs 36 KB

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