Skip to content

Commit 160227d

Browse files
bors[bot]cuviper
andauthored
Merge #82
82: Use values for all Float methods r=cuviper a=cuviper Since `T: Float` also implies `T: Copy`, we don't need the indirection of a reference for `self` or other parameters. Most of these are marked `#[inline]` anyway, so in the end the change won't make much difference to optimization, but it's semantically cleaner. Users probably won't notice the change in method calls from `&self` to `self`, but construction `from_polar` will need adjustment. Fixes #80. Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents 15763f2 + 59624e8 commit 160227d

File tree

2 files changed

+53
-49
lines changed

2 files changed

+53
-49
lines changed

src/cast.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ macro_rules! impl_to_primitive {
55
($ty:ty, $to:ident) => {
66
#[inline]
77
fn $to(&self) -> Option<$ty> {
8-
if self.im.is_zero() { self.re.$to() } else { None }
8+
if self.im.is_zero() {
9+
self.re.$to()
10+
} else {
11+
None
12+
}
913
}
10-
}
14+
};
1115
} // impl_to_primitive
1216

1317
// Returns None if Complex part is non-zero

src/lib.rs

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -160,35 +160,35 @@ impl<T: Clone + Signed> Complex<T> {
160160
}
161161

162162
#[cfg(any(feature = "std", feature = "libm"))]
163-
impl<T: Clone + Float> Complex<T> {
163+
impl<T: Float> Complex<T> {
164164
/// Calculate |self|
165165
#[inline]
166-
pub fn norm(&self) -> T {
166+
pub fn norm(self) -> T {
167167
self.re.hypot(self.im)
168168
}
169169
/// Calculate the principal Arg of self.
170170
#[inline]
171-
pub fn arg(&self) -> T {
171+
pub fn arg(self) -> T {
172172
self.im.atan2(self.re)
173173
}
174174
/// Convert to polar form (r, theta), such that
175175
/// `self = r * exp(i * theta)`
176176
#[inline]
177-
pub fn to_polar(&self) -> (T, T) {
177+
pub fn to_polar(self) -> (T, T) {
178178
(self.norm(), self.arg())
179179
}
180180
/// Convert a polar representation into a complex number.
181181
#[inline]
182-
pub fn from_polar(r: &T, theta: &T) -> Self {
183-
Self::new(*r * theta.cos(), *r * theta.sin())
182+
pub fn from_polar(r: T, theta: T) -> Self {
183+
Self::new(r * theta.cos(), r * theta.sin())
184184
}
185185

186186
/// Computes `e^(self)`, where `e` is the base of the natural logarithm.
187187
#[inline]
188-
pub fn exp(&self) -> Self {
188+
pub fn exp(self) -> Self {
189189
// formula: e^(a + bi) = e^a (cos(b) + i*sin(b))
190190
// = from_polar(e^a, b)
191-
Self::from_polar(&self.re.exp(), &self.im)
191+
Self::from_polar(self.re.exp(), self.im)
192192
}
193193

194194
/// Computes the principal value of natural logarithm of `self`.
@@ -199,7 +199,7 @@ impl<T: Clone + Float> Complex<T> {
199199
///
200200
/// The branch satisfies `-π ≤ arg(ln(z)) ≤ π`.
201201
#[inline]
202-
pub fn ln(&self) -> Self {
202+
pub fn ln(self) -> Self {
203203
// formula: ln(z) = ln|z| + i*arg(z)
204204
let (r, theta) = self.to_polar();
205205
Self::new(r.ln(), theta)
@@ -213,7 +213,7 @@ impl<T: Clone + Float> Complex<T> {
213213
///
214214
/// The branch satisfies `-π/2 ≤ arg(sqrt(z)) ≤ π/2`.
215215
#[inline]
216-
pub fn sqrt(&self) -> Self {
216+
pub fn sqrt(self) -> Self {
217217
if self.im.is_zero() {
218218
if self.re.is_sign_positive() {
219219
// simple positive real √r, and copy `im` for its sign
@@ -245,7 +245,7 @@ impl<T: Clone + Float> Complex<T> {
245245
let one = T::one();
246246
let two = one + one;
247247
let (r, theta) = self.to_polar();
248-
Self::from_polar(&(r.sqrt()), &(theta / two))
248+
Self::from_polar(r.sqrt(), theta / two)
249249
}
250250
}
251251

@@ -261,7 +261,7 @@ impl<T: Clone + Float> Complex<T> {
261261
/// negative real numbers. For example, the real cube root of `-8` is `-2`,
262262
/// but the principal complex cube root of `-8` is `1 + i√3`.
263263
#[inline]
264-
pub fn cbrt(&self) -> Self {
264+
pub fn cbrt(self) -> Self {
265265
if self.im.is_zero() {
266266
if self.re.is_sign_positive() {
267267
// simple positive real ∛r, and copy `im` for its sign
@@ -298,22 +298,22 @@ impl<T: Clone + Float> Complex<T> {
298298
let one = T::one();
299299
let three = one + one + one;
300300
let (r, theta) = self.to_polar();
301-
Self::from_polar(&(r.cbrt()), &(theta / three))
301+
Self::from_polar(r.cbrt(), theta / three)
302302
}
303303
}
304304

305305
/// Raises `self` to a floating point power.
306306
#[inline]
307-
pub fn powf(&self, exp: T) -> Self {
307+
pub fn powf(self, exp: T) -> Self {
308308
// formula: x^y = (ρ e^(i θ))^y = ρ^y e^(i θ y)
309309
// = from_polar(ρ^y, θ y)
310310
let (r, theta) = self.to_polar();
311-
Self::from_polar(&r.powf(exp), &(theta * exp))
311+
Self::from_polar(r.powf(exp), theta * exp)
312312
}
313313

314314
/// Returns the logarithm of `self` with respect to an arbitrary base.
315315
#[inline]
316-
pub fn log(&self, base: T) -> Self {
316+
pub fn log(self, base: T) -> Self {
317317
// formula: log_y(x) = log_y(ρ e^(i θ))
318318
// = log_y(ρ) + log_y(e^(i θ)) = log_y(ρ) + ln(e^(i θ)) / ln(y)
319319
// = log_y(ρ) + i θ / ln(y)
@@ -323,7 +323,7 @@ impl<T: Clone + Float> Complex<T> {
323323

324324
/// Raises `self` to a complex power.
325325
#[inline]
326-
pub fn powc(&self, exp: Self) -> Self {
326+
pub fn powc(self, exp: Self) -> Self {
327327
// formula: x^y = (a + i b)^(c + i d)
328328
// = (ρ e^(i θ))^c (ρ e^(i θ))^(i d)
329329
// where ρ=|x| and θ=arg(x)
@@ -337,22 +337,22 @@ impl<T: Clone + Float> Complex<T> {
337337
// = from_polar(p^c e^(−d θ), c θ + d ln(ρ))
338338
let (r, theta) = self.to_polar();
339339
Self::from_polar(
340-
&(r.powf(exp.re) * (-exp.im * theta).exp()),
341-
&(exp.re * theta + exp.im * r.ln()),
340+
r.powf(exp.re) * (-exp.im * theta).exp(),
341+
exp.re * theta + exp.im * r.ln(),
342342
)
343343
}
344344

345345
/// Raises a floating point number to the complex power `self`.
346346
#[inline]
347-
pub fn expf(&self, base: T) -> Self {
347+
pub fn expf(self, base: T) -> Self {
348348
// formula: x^(a+bi) = x^a x^bi = x^a e^(b ln(x) i)
349349
// = from_polar(x^a, b ln(x))
350-
Self::from_polar(&base.powf(self.re), &(self.im * base.ln()))
350+
Self::from_polar(base.powf(self.re), self.im * base.ln())
351351
}
352352

353353
/// Computes the sine of `self`.
354354
#[inline]
355-
pub fn sin(&self) -> Self {
355+
pub fn sin(self) -> Self {
356356
// formula: sin(a + bi) = sin(a)cosh(b) + i*cos(a)sinh(b)
357357
Self::new(
358358
self.re.sin() * self.im.cosh(),
@@ -362,7 +362,7 @@ impl<T: Clone + Float> Complex<T> {
362362

363363
/// Computes the cosine of `self`.
364364
#[inline]
365-
pub fn cos(&self) -> Self {
365+
pub fn cos(self) -> Self {
366366
// formula: cos(a + bi) = cos(a)cosh(b) - i*sin(a)sinh(b)
367367
Self::new(
368368
self.re.cos() * self.im.cosh(),
@@ -372,7 +372,7 @@ impl<T: Clone + Float> Complex<T> {
372372

373373
/// Computes the tangent of `self`.
374374
#[inline]
375-
pub fn tan(&self) -> Self {
375+
pub fn tan(self) -> Self {
376376
// formula: tan(a + bi) = (sin(2a) + i*sinh(2b))/(cos(2a) + cosh(2b))
377377
let (two_re, two_im) = (self.re + self.re, self.im + self.im);
378378
Self::new(two_re.sin(), two_im.sinh()).unscale(two_re.cos() + two_im.cosh())
@@ -387,7 +387,7 @@ impl<T: Clone + Float> Complex<T> {
387387
///
388388
/// The branch satisfies `-π/2 ≤ Re(asin(z)) ≤ π/2`.
389389
#[inline]
390-
pub fn asin(&self) -> Self {
390+
pub fn asin(self) -> Self {
391391
// formula: arcsin(z) = -i ln(sqrt(1-z^2) + iz)
392392
let i = Self::i();
393393
-i * ((Self::one() - self * self).sqrt() + i * self).ln()
@@ -402,7 +402,7 @@ impl<T: Clone + Float> Complex<T> {
402402
///
403403
/// The branch satisfies `0 ≤ Re(acos(z)) ≤ π`.
404404
#[inline]
405-
pub fn acos(&self) -> Self {
405+
pub fn acos(self) -> Self {
406406
// formula: arccos(z) = -i ln(i sqrt(1-z^2) + z)
407407
let i = Self::i();
408408
-i * (i * (Self::one() - self * self).sqrt() + self).ln()
@@ -417,22 +417,22 @@ impl<T: Clone + Float> Complex<T> {
417417
///
418418
/// The branch satisfies `-π/2 ≤ Re(atan(z)) ≤ π/2`.
419419
#[inline]
420-
pub fn atan(&self) -> Self {
420+
pub fn atan(self) -> Self {
421421
// formula: arctan(z) = (ln(1+iz) - ln(1-iz))/(2i)
422422
let i = Self::i();
423423
let one = Self::one();
424424
let two = one + one;
425-
if *self == i {
425+
if self == i {
426426
return Self::new(T::zero(), T::infinity());
427-
} else if *self == -i {
427+
} else if self == -i {
428428
return Self::new(T::zero(), -T::infinity());
429429
}
430430
((one + i * self).ln() - (one - i * self).ln()) / (two * i)
431431
}
432432

433433
/// Computes the hyperbolic sine of `self`.
434434
#[inline]
435-
pub fn sinh(&self) -> Self {
435+
pub fn sinh(self) -> Self {
436436
// formula: sinh(a + bi) = sinh(a)cos(b) + i*cosh(a)sin(b)
437437
Self::new(
438438
self.re.sinh() * self.im.cos(),
@@ -442,7 +442,7 @@ impl<T: Clone + Float> Complex<T> {
442442

443443
/// Computes the hyperbolic cosine of `self`.
444444
#[inline]
445-
pub fn cosh(&self) -> Self {
445+
pub fn cosh(self) -> Self {
446446
// formula: cosh(a + bi) = cosh(a)cos(b) + i*sinh(a)sin(b)
447447
Self::new(
448448
self.re.cosh() * self.im.cos(),
@@ -452,7 +452,7 @@ impl<T: Clone + Float> Complex<T> {
452452

453453
/// Computes the hyperbolic tangent of `self`.
454454
#[inline]
455-
pub fn tanh(&self) -> Self {
455+
pub fn tanh(self) -> Self {
456456
// formula: tanh(a + bi) = (sinh(2a) + i*sin(2b))/(cosh(2a) + cos(2b))
457457
let (two_re, two_im) = (self.re + self.re, self.im + self.im);
458458
Self::new(two_re.sinh(), two_im.sin()).unscale(two_re.cosh() + two_im.cos())
@@ -467,7 +467,7 @@ impl<T: Clone + Float> Complex<T> {
467467
///
468468
/// The branch satisfies `-π/2 ≤ Im(asinh(z)) ≤ π/2`.
469469
#[inline]
470-
pub fn asinh(&self) -> Self {
470+
pub fn asinh(self) -> Self {
471471
// formula: arcsinh(z) = ln(z + sqrt(1+z^2))
472472
let one = Self::one();
473473
(self + (one + self * self).sqrt()).ln()
@@ -481,7 +481,7 @@ impl<T: Clone + Float> Complex<T> {
481481
///
482482
/// The branch satisfies `-π ≤ Im(acosh(z)) ≤ π` and `0 ≤ Re(acosh(z)) < ∞`.
483483
#[inline]
484-
pub fn acosh(&self) -> Self {
484+
pub fn acosh(self) -> Self {
485485
// formula: arccosh(z) = 2 ln(sqrt((z+1)/2) + sqrt((z-1)/2))
486486
let one = Self::one();
487487
let two = one + one;
@@ -497,13 +497,13 @@ impl<T: Clone + Float> Complex<T> {
497497
///
498498
/// The branch satisfies `-π/2 ≤ Im(atanh(z)) ≤ π/2`.
499499
#[inline]
500-
pub fn atanh(&self) -> Self {
500+
pub fn atanh(self) -> Self {
501501
// formula: arctanh(z) = (ln(1+z) - ln(1-z))/2
502502
let one = Self::one();
503503
let two = one + one;
504-
if *self == one {
504+
if self == one {
505505
return Self::new(T::infinity(), T::zero());
506-
} else if *self == -one {
506+
} else if self == -one {
507507
return Self::new(-T::infinity(), T::zero());
508508
}
509509
((one + self).ln() - (one - self).ln()) / two
@@ -532,7 +532,7 @@ impl<T: Clone + Float> Complex<T> {
532532
/// assert!((inv - expected).norm() < 1e-315);
533533
/// ```
534534
#[inline]
535-
pub fn finv(&self) -> Complex<T> {
535+
pub fn finv(self) -> Complex<T> {
536536
let norm = self.norm();
537537
self.conj() / norm / norm
538538
}
@@ -561,12 +561,12 @@ impl<T: Clone + Float> Complex<T> {
561561
/// assert!((quotient - expected).norm() < 1e-315);
562562
/// ```
563563
#[inline]
564-
pub fn fdiv(&self, other: Complex<T>) -> Complex<T> {
564+
pub fn fdiv(self, other: Complex<T>) -> Complex<T> {
565565
self * other.finv()
566566
}
567567
}
568568

569-
impl<T: Clone + FloatCore> Complex<T> {
569+
impl<T: FloatCore> Complex<T> {
570570
/// Checks if the given complex number is NaN
571571
#[inline]
572572
pub fn is_nan(self) -> bool {
@@ -1632,7 +1632,7 @@ mod test {
16321632
fn test_polar_conv() {
16331633
fn test(c: Complex64) {
16341634
let (r, theta) = c.to_polar();
1635-
assert!((c - Complex::from_polar(&r, &theta)).norm() < 1e-6);
1635+
assert!((c - Complex::from_polar(r, theta)).norm() < 1e-6);
16361636
}
16371637
for &c in all_consts.iter() {
16381638
test(c);
@@ -1775,12 +1775,12 @@ mod test {
17751775
let n2 = n * n;
17761776
assert!(close(
17771777
Complex64::new(0.0, n2).sqrt(),
1778-
Complex64::from_polar(&n, &(f64::consts::FRAC_PI_4))
1778+
Complex64::from_polar(n, f64::consts::FRAC_PI_4)
17791779
));
17801780
// √(0 - n²i) = n e^(-iπ/4)
17811781
assert!(close(
17821782
Complex64::new(0.0, -n2).sqrt(),
1783-
Complex64::from_polar(&n, &(-f64::consts::FRAC_PI_4))
1783+
Complex64::from_polar(n, -f64::consts::FRAC_PI_4)
17841784
));
17851785
}
17861786
}
@@ -1824,12 +1824,12 @@ mod test {
18241824
// ∛(-n³ + 0i) = n e^(iπ/3)
18251825
assert!(close(
18261826
Complex64::new(-n3, 0.0).cbrt(),
1827-
Complex64::from_polar(&n, &(f64::consts::FRAC_PI_3))
1827+
Complex64::from_polar(n, f64::consts::FRAC_PI_3)
18281828
));
18291829
// ∛(-n³ - 0i) = n e^(-iπ/3)
18301830
assert!(close(
18311831
Complex64::new(-n3, -0.0).cbrt(),
1832-
Complex64::from_polar(&n, &(-f64::consts::FRAC_PI_3))
1832+
Complex64::from_polar(n, -f64::consts::FRAC_PI_3)
18331833
));
18341834
}
18351835
}
@@ -1841,12 +1841,12 @@ mod test {
18411841
let n3 = n * n * n;
18421842
assert!(close(
18431843
Complex64::new(0.0, n3).cbrt(),
1844-
Complex64::from_polar(&n, &(f64::consts::FRAC_PI_6))
1844+
Complex64::from_polar(n, f64::consts::FRAC_PI_6)
18451845
));
18461846
// ∛(0 - n³i) = n e^(-iπ/6)
18471847
assert!(close(
18481848
Complex64::new(0.0, -n3).cbrt(),
1849-
Complex64::from_polar(&n, &(-f64::consts::FRAC_PI_6))
1849+
Complex64::from_polar(n, -f64::consts::FRAC_PI_6)
18501850
));
18511851
}
18521852
}

0 commit comments

Comments
 (0)