lib.rs 37 KB

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