complex.rs 36 KB

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