@@ -160,35 +160,35 @@ impl<T: Clone + Signed> Complex<T> {
160
160
}
161
161
162
162
#[ cfg( any( feature = "std" , feature = "libm" ) ) ]
163
- impl < T : Clone + Float > Complex < T > {
163
+ impl < T : Float > Complex < T > {
164
164
/// Calculate |self|
165
165
#[ inline]
166
- pub fn norm ( & self ) -> T {
166
+ pub fn norm ( self ) -> T {
167
167
self . re . hypot ( self . im )
168
168
}
169
169
/// Calculate the principal Arg of self.
170
170
#[ inline]
171
- pub fn arg ( & self ) -> T {
171
+ pub fn arg ( self ) -> T {
172
172
self . im . atan2 ( self . re )
173
173
}
174
174
/// Convert to polar form (r, theta), such that
175
175
/// `self = r * exp(i * theta)`
176
176
#[ inline]
177
- pub fn to_polar ( & self ) -> ( T , T ) {
177
+ pub fn to_polar ( self ) -> ( T , T ) {
178
178
( self . norm ( ) , self . arg ( ) )
179
179
}
180
180
/// Convert a polar representation into a complex number.
181
181
#[ 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 ( ) )
184
184
}
185
185
186
186
/// Computes `e^(self)`, where `e` is the base of the natural logarithm.
187
187
#[ inline]
188
- pub fn exp ( & self ) -> Self {
188
+ pub fn exp ( self ) -> Self {
189
189
// formula: e^(a + bi) = e^a (cos(b) + i*sin(b))
190
190
// = from_polar(e^a, b)
191
- Self :: from_polar ( & self . re . exp ( ) , & self . im )
191
+ Self :: from_polar ( self . re . exp ( ) , self . im )
192
192
}
193
193
194
194
/// Computes the principal value of natural logarithm of `self`.
@@ -199,7 +199,7 @@ impl<T: Clone + Float> Complex<T> {
199
199
///
200
200
/// The branch satisfies `-π ≤ arg(ln(z)) ≤ π`.
201
201
#[ inline]
202
- pub fn ln ( & self ) -> Self {
202
+ pub fn ln ( self ) -> Self {
203
203
// formula: ln(z) = ln|z| + i*arg(z)
204
204
let ( r, theta) = self . to_polar ( ) ;
205
205
Self :: new ( r. ln ( ) , theta)
@@ -213,7 +213,7 @@ impl<T: Clone + Float> Complex<T> {
213
213
///
214
214
/// The branch satisfies `-π/2 ≤ arg(sqrt(z)) ≤ π/2`.
215
215
#[ inline]
216
- pub fn sqrt ( & self ) -> Self {
216
+ pub fn sqrt ( self ) -> Self {
217
217
if self . im . is_zero ( ) {
218
218
if self . re . is_sign_positive ( ) {
219
219
// simple positive real √r, and copy `im` for its sign
@@ -245,7 +245,7 @@ impl<T: Clone + Float> Complex<T> {
245
245
let one = T :: one ( ) ;
246
246
let two = one + one;
247
247
let ( r, theta) = self . to_polar ( ) ;
248
- Self :: from_polar ( & ( r. sqrt ( ) ) , & ( theta / two) )
248
+ Self :: from_polar ( r. sqrt ( ) , theta / two)
249
249
}
250
250
}
251
251
@@ -261,7 +261,7 @@ impl<T: Clone + Float> Complex<T> {
261
261
/// negative real numbers. For example, the real cube root of `-8` is `-2`,
262
262
/// but the principal complex cube root of `-8` is `1 + i√3`.
263
263
#[ inline]
264
- pub fn cbrt ( & self ) -> Self {
264
+ pub fn cbrt ( self ) -> Self {
265
265
if self . im . is_zero ( ) {
266
266
if self . re . is_sign_positive ( ) {
267
267
// simple positive real ∛r, and copy `im` for its sign
@@ -298,22 +298,22 @@ impl<T: Clone + Float> Complex<T> {
298
298
let one = T :: one ( ) ;
299
299
let three = one + one + one;
300
300
let ( r, theta) = self . to_polar ( ) ;
301
- Self :: from_polar ( & ( r. cbrt ( ) ) , & ( theta / three) )
301
+ Self :: from_polar ( r. cbrt ( ) , theta / three)
302
302
}
303
303
}
304
304
305
305
/// Raises `self` to a floating point power.
306
306
#[ inline]
307
- pub fn powf ( & self , exp : T ) -> Self {
307
+ pub fn powf ( self , exp : T ) -> Self {
308
308
// formula: x^y = (ρ e^(i θ))^y = ρ^y e^(i θ y)
309
309
// = from_polar(ρ^y, θ y)
310
310
let ( r, theta) = self . to_polar ( ) ;
311
- Self :: from_polar ( & r. powf ( exp) , & ( theta * exp) )
311
+ Self :: from_polar ( r. powf ( exp) , theta * exp)
312
312
}
313
313
314
314
/// Returns the logarithm of `self` with respect to an arbitrary base.
315
315
#[ inline]
316
- pub fn log ( & self , base : T ) -> Self {
316
+ pub fn log ( self , base : T ) -> Self {
317
317
// formula: log_y(x) = log_y(ρ e^(i θ))
318
318
// = log_y(ρ) + log_y(e^(i θ)) = log_y(ρ) + ln(e^(i θ)) / ln(y)
319
319
// = log_y(ρ) + i θ / ln(y)
@@ -323,7 +323,7 @@ impl<T: Clone + Float> Complex<T> {
323
323
324
324
/// Raises `self` to a complex power.
325
325
#[ inline]
326
- pub fn powc ( & self , exp : Self ) -> Self {
326
+ pub fn powc ( self , exp : Self ) -> Self {
327
327
// formula: x^y = (a + i b)^(c + i d)
328
328
// = (ρ e^(i θ))^c (ρ e^(i θ))^(i d)
329
329
// where ρ=|x| and θ=arg(x)
@@ -337,22 +337,22 @@ impl<T: Clone + Float> Complex<T> {
337
337
// = from_polar(p^c e^(−d θ), c θ + d ln(ρ))
338
338
let ( r, theta) = self . to_polar ( ) ;
339
339
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 ( ) ,
342
342
)
343
343
}
344
344
345
345
/// Raises a floating point number to the complex power `self`.
346
346
#[ inline]
347
- pub fn expf ( & self , base : T ) -> Self {
347
+ pub fn expf ( self , base : T ) -> Self {
348
348
// formula: x^(a+bi) = x^a x^bi = x^a e^(b ln(x) i)
349
349
// = 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 ( ) )
351
351
}
352
352
353
353
/// Computes the sine of `self`.
354
354
#[ inline]
355
- pub fn sin ( & self ) -> Self {
355
+ pub fn sin ( self ) -> Self {
356
356
// formula: sin(a + bi) = sin(a)cosh(b) + i*cos(a)sinh(b)
357
357
Self :: new (
358
358
self . re . sin ( ) * self . im . cosh ( ) ,
@@ -362,7 +362,7 @@ impl<T: Clone + Float> Complex<T> {
362
362
363
363
/// Computes the cosine of `self`.
364
364
#[ inline]
365
- pub fn cos ( & self ) -> Self {
365
+ pub fn cos ( self ) -> Self {
366
366
// formula: cos(a + bi) = cos(a)cosh(b) - i*sin(a)sinh(b)
367
367
Self :: new (
368
368
self . re . cos ( ) * self . im . cosh ( ) ,
@@ -372,7 +372,7 @@ impl<T: Clone + Float> Complex<T> {
372
372
373
373
/// Computes the tangent of `self`.
374
374
#[ inline]
375
- pub fn tan ( & self ) -> Self {
375
+ pub fn tan ( self ) -> Self {
376
376
// formula: tan(a + bi) = (sin(2a) + i*sinh(2b))/(cos(2a) + cosh(2b))
377
377
let ( two_re, two_im) = ( self . re + self . re , self . im + self . im ) ;
378
378
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> {
387
387
///
388
388
/// The branch satisfies `-π/2 ≤ Re(asin(z)) ≤ π/2`.
389
389
#[ inline]
390
- pub fn asin ( & self ) -> Self {
390
+ pub fn asin ( self ) -> Self {
391
391
// formula: arcsin(z) = -i ln(sqrt(1-z^2) + iz)
392
392
let i = Self :: i ( ) ;
393
393
-i * ( ( Self :: one ( ) - self * self ) . sqrt ( ) + i * self ) . ln ( )
@@ -402,7 +402,7 @@ impl<T: Clone + Float> Complex<T> {
402
402
///
403
403
/// The branch satisfies `0 ≤ Re(acos(z)) ≤ π`.
404
404
#[ inline]
405
- pub fn acos ( & self ) -> Self {
405
+ pub fn acos ( self ) -> Self {
406
406
// formula: arccos(z) = -i ln(i sqrt(1-z^2) + z)
407
407
let i = Self :: i ( ) ;
408
408
-i * ( i * ( Self :: one ( ) - self * self ) . sqrt ( ) + self ) . ln ( )
@@ -417,22 +417,22 @@ impl<T: Clone + Float> Complex<T> {
417
417
///
418
418
/// The branch satisfies `-π/2 ≤ Re(atan(z)) ≤ π/2`.
419
419
#[ inline]
420
- pub fn atan ( & self ) -> Self {
420
+ pub fn atan ( self ) -> Self {
421
421
// formula: arctan(z) = (ln(1+iz) - ln(1-iz))/(2i)
422
422
let i = Self :: i ( ) ;
423
423
let one = Self :: one ( ) ;
424
424
let two = one + one;
425
- if * self == i {
425
+ if self == i {
426
426
return Self :: new ( T :: zero ( ) , T :: infinity ( ) ) ;
427
- } else if * self == -i {
427
+ } else if self == -i {
428
428
return Self :: new ( T :: zero ( ) , -T :: infinity ( ) ) ;
429
429
}
430
430
( ( one + i * self ) . ln ( ) - ( one - i * self ) . ln ( ) ) / ( two * i)
431
431
}
432
432
433
433
/// Computes the hyperbolic sine of `self`.
434
434
#[ inline]
435
- pub fn sinh ( & self ) -> Self {
435
+ pub fn sinh ( self ) -> Self {
436
436
// formula: sinh(a + bi) = sinh(a)cos(b) + i*cosh(a)sin(b)
437
437
Self :: new (
438
438
self . re . sinh ( ) * self . im . cos ( ) ,
@@ -442,7 +442,7 @@ impl<T: Clone + Float> Complex<T> {
442
442
443
443
/// Computes the hyperbolic cosine of `self`.
444
444
#[ inline]
445
- pub fn cosh ( & self ) -> Self {
445
+ pub fn cosh ( self ) -> Self {
446
446
// formula: cosh(a + bi) = cosh(a)cos(b) + i*sinh(a)sin(b)
447
447
Self :: new (
448
448
self . re . cosh ( ) * self . im . cos ( ) ,
@@ -452,7 +452,7 @@ impl<T: Clone + Float> Complex<T> {
452
452
453
453
/// Computes the hyperbolic tangent of `self`.
454
454
#[ inline]
455
- pub fn tanh ( & self ) -> Self {
455
+ pub fn tanh ( self ) -> Self {
456
456
// formula: tanh(a + bi) = (sinh(2a) + i*sin(2b))/(cosh(2a) + cos(2b))
457
457
let ( two_re, two_im) = ( self . re + self . re , self . im + self . im ) ;
458
458
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> {
467
467
///
468
468
/// The branch satisfies `-π/2 ≤ Im(asinh(z)) ≤ π/2`.
469
469
#[ inline]
470
- pub fn asinh ( & self ) -> Self {
470
+ pub fn asinh ( self ) -> Self {
471
471
// formula: arcsinh(z) = ln(z + sqrt(1+z^2))
472
472
let one = Self :: one ( ) ;
473
473
( self + ( one + self * self ) . sqrt ( ) ) . ln ( )
@@ -481,7 +481,7 @@ impl<T: Clone + Float> Complex<T> {
481
481
///
482
482
/// The branch satisfies `-π ≤ Im(acosh(z)) ≤ π` and `0 ≤ Re(acosh(z)) < ∞`.
483
483
#[ inline]
484
- pub fn acosh ( & self ) -> Self {
484
+ pub fn acosh ( self ) -> Self {
485
485
// formula: arccosh(z) = 2 ln(sqrt((z+1)/2) + sqrt((z-1)/2))
486
486
let one = Self :: one ( ) ;
487
487
let two = one + one;
@@ -497,13 +497,13 @@ impl<T: Clone + Float> Complex<T> {
497
497
///
498
498
/// The branch satisfies `-π/2 ≤ Im(atanh(z)) ≤ π/2`.
499
499
#[ inline]
500
- pub fn atanh ( & self ) -> Self {
500
+ pub fn atanh ( self ) -> Self {
501
501
// formula: arctanh(z) = (ln(1+z) - ln(1-z))/2
502
502
let one = Self :: one ( ) ;
503
503
let two = one + one;
504
- if * self == one {
504
+ if self == one {
505
505
return Self :: new ( T :: infinity ( ) , T :: zero ( ) ) ;
506
- } else if * self == -one {
506
+ } else if self == -one {
507
507
return Self :: new ( -T :: infinity ( ) , T :: zero ( ) ) ;
508
508
}
509
509
( ( one + self ) . ln ( ) - ( one - self ) . ln ( ) ) / two
@@ -532,7 +532,7 @@ impl<T: Clone + Float> Complex<T> {
532
532
/// assert!((inv - expected).norm() < 1e-315);
533
533
/// ```
534
534
#[ inline]
535
- pub fn finv ( & self ) -> Complex < T > {
535
+ pub fn finv ( self ) -> Complex < T > {
536
536
let norm = self . norm ( ) ;
537
537
self . conj ( ) / norm / norm
538
538
}
@@ -561,12 +561,12 @@ impl<T: Clone + Float> Complex<T> {
561
561
/// assert!((quotient - expected).norm() < 1e-315);
562
562
/// ```
563
563
#[ inline]
564
- pub fn fdiv ( & self , other : Complex < T > ) -> Complex < T > {
564
+ pub fn fdiv ( self , other : Complex < T > ) -> Complex < T > {
565
565
self * other. finv ( )
566
566
}
567
567
}
568
568
569
- impl < T : Clone + FloatCore > Complex < T > {
569
+ impl < T : FloatCore > Complex < T > {
570
570
/// Checks if the given complex number is NaN
571
571
#[ inline]
572
572
pub fn is_nan ( self ) -> bool {
@@ -1632,7 +1632,7 @@ mod test {
1632
1632
fn test_polar_conv ( ) {
1633
1633
fn test ( c : Complex64 ) {
1634
1634
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 ) ;
1636
1636
}
1637
1637
for & c in all_consts. iter ( ) {
1638
1638
test ( c) ;
@@ -1775,12 +1775,12 @@ mod test {
1775
1775
let n2 = n * n;
1776
1776
assert ! ( close(
1777
1777
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 )
1779
1779
) ) ;
1780
1780
// √(0 - n²i) = n e^(-iπ/4)
1781
1781
assert ! ( close(
1782
1782
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 )
1784
1784
) ) ;
1785
1785
}
1786
1786
}
@@ -1824,12 +1824,12 @@ mod test {
1824
1824
// ∛(-n³ + 0i) = n e^(iπ/3)
1825
1825
assert ! ( close(
1826
1826
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 )
1828
1828
) ) ;
1829
1829
// ∛(-n³ - 0i) = n e^(-iπ/3)
1830
1830
assert ! ( close(
1831
1831
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 )
1833
1833
) ) ;
1834
1834
}
1835
1835
}
@@ -1841,12 +1841,12 @@ mod test {
1841
1841
let n3 = n * n * n;
1842
1842
assert ! ( close(
1843
1843
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 )
1845
1845
) ) ;
1846
1846
// ∛(0 - n³i) = n e^(-iπ/6)
1847
1847
assert ! ( close(
1848
1848
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 )
1850
1850
) ) ;
1851
1851
}
1852
1852
}
0 commit comments