@@ -11,147 +11,6 @@ use num::Float;
1111//#[cfg(feature = "decimal")]
1212//use decimal::d128;
1313
14- macro_rules! complex_trait_methods (
15- ( $RealField: ident $( , $prefix: ident) * ) => {
16- paste:: item! {
17- /// Builds a pure-real complex number from the given value.
18- fn [ <from_ $( $prefix) * real>] ( re: Self :: $RealField) -> Self ;
19-
20- /// The real part of this complex number.
21- fn [ <$( $prefix) * real>] ( self ) -> Self :: $RealField;
22-
23- /// The imaginary part of this complex number.
24- fn [ <$( $prefix) * imaginary>] ( self ) -> Self :: $RealField;
25-
26- /// The modulus of this complex number.
27- fn [ <$( $prefix) * modulus>] ( self ) -> Self :: $RealField;
28-
29- /// The squared modulus of this complex number.
30- fn [ <$( $prefix) * modulus_squared>] ( self ) -> Self :: $RealField;
31-
32- /// The argument of this complex number.
33- fn [ <$( $prefix) * argument>] ( self ) -> Self :: $RealField;
34-
35- /// The sum of the absolute value of this complex number's real and imaginary part.
36- fn [ <$( $prefix) * norm1>] ( self ) -> Self :: $RealField;
37-
38- /// Multiplies this complex number by `factor`.
39- fn [ <$( $prefix) * scale>] ( self , factor: Self :: $RealField) -> Self ;
40-
41- /// Divides this complex number by `factor`.
42- fn [ <$( $prefix) * unscale>] ( self , factor: Self :: $RealField) -> Self ;
43-
44- /// The polar form of this complex number: (modulus, arg)
45- fn [ <$( $prefix) * to_polar>] ( self ) -> ( Self :: $RealField, Self :: $RealField) {
46- ( self . clone( ) . [ <$( $prefix) * modulus>] ( ) , self . [ <$( $prefix) * argument>] ( ) )
47- }
48-
49- /// The exponential form of this complex number: (modulus, e^{i arg})
50- fn [ <$( $prefix) * to_exp>] ( self ) -> ( Self :: $RealField, Self ) {
51- let m = self . clone( ) . [ <$( $prefix) * modulus>] ( ) ;
52-
53- if !m. is_zero( ) {
54- ( m. clone( ) , self . [ <$( $prefix) * unscale>] ( m) )
55- } else {
56- ( Self :: $RealField:: zero( ) , Self :: one( ) )
57- }
58- }
59-
60- /// The exponential part of this complex number: `self / self.modulus()`
61- fn [ <$( $prefix) * signum>] ( self ) -> Self {
62- self . [ <$( $prefix) * to_exp>] ( ) . 1
63- }
64-
65- fn [ <$( $prefix) * floor>] ( self ) -> Self ;
66- fn [ <$( $prefix) * ceil>] ( self ) -> Self ;
67- fn [ <$( $prefix) * round>] ( self ) -> Self ;
68- fn [ <$( $prefix) * trunc>] ( self ) -> Self ;
69- fn [ <$( $prefix) * fract>] ( self ) -> Self ;
70- fn [ <$( $prefix) * mul_add>] ( self , a: Self , b: Self ) -> Self ;
71-
72- /// The absolute value of this complex number: `self / self.signum()`.
73- ///
74- /// This is equivalent to `self.modulus()`.
75- fn [ <$( $prefix) * abs>] ( self ) -> Self :: $RealField;
76-
77- /// Computes (self.conjugate() * self + other.conjugate() * other).sqrt()
78- fn [ <$( $prefix) * hypot>] ( self , other: Self ) -> Self :: $RealField;
79-
80- fn [ <$( $prefix) * recip>] ( self ) -> Self ;
81- fn [ <$( $prefix) * conjugate>] ( self ) -> Self ;
82- fn [ <$( $prefix) * sin>] ( self ) -> Self ;
83- fn [ <$( $prefix) * cos>] ( self ) -> Self ;
84- fn [ <$( $prefix) * sin_cos>] ( self ) -> ( Self , Self ) ;
85- #[ inline]
86- fn [ <$( $prefix) * sinh_cosh>] ( self ) -> ( Self , Self ) {
87- ( self . clone( ) . [ <$( $prefix) * sinh>] ( ) , self . [ <$( $prefix) * cosh>] ( ) )
88- }
89- fn [ <$( $prefix) * tan>] ( self ) -> Self ;
90- fn [ <$( $prefix) * asin>] ( self ) -> Self ;
91- fn [ <$( $prefix) * acos>] ( self ) -> Self ;
92- fn [ <$( $prefix) * atan>] ( self ) -> Self ;
93- fn [ <$( $prefix) * sinh>] ( self ) -> Self ;
94- fn [ <$( $prefix) * cosh>] ( self ) -> Self ;
95- fn [ <$( $prefix) * tanh>] ( self ) -> Self ;
96- fn [ <$( $prefix) * asinh>] ( self ) -> Self ;
97- fn [ <$( $prefix) * acosh>] ( self ) -> Self ;
98- fn [ <$( $prefix) * atanh>] ( self ) -> Self ;
99-
100- /// Cardinal sine
101- #[ inline]
102- fn [ <$( $prefix) * sinc>] ( self ) -> Self {
103- if self . is_zero( ) {
104- Self :: one( )
105- } else {
106- self . clone( ) . [ <$( $prefix) * sin>] ( ) / self
107- }
108- }
109-
110- #[ inline]
111- fn [ <$( $prefix) * sinhc>] ( self ) -> Self {
112- if self . is_zero( ) {
113- Self :: one( )
114- } else {
115- self . clone( ) . [ <$( $prefix) * sinh>] ( ) / self
116- }
117- }
118-
119- /// Cardinal cos
120- #[ inline]
121- fn [ <$( $prefix) * cosc>] ( self ) -> Self {
122- if self . is_zero( ) {
123- Self :: one( )
124- } else {
125- self . clone( ) . [ <$( $prefix) * cos>] ( ) / self
126- }
127- }
128-
129- #[ inline]
130- fn [ <$( $prefix) * coshc>] ( self ) -> Self {
131- if self . is_zero( ) {
132- Self :: one( )
133- } else {
134- self . clone( ) . [ <$( $prefix) * cosh>] ( ) / self
135- }
136- }
137-
138- fn [ <$( $prefix) * log>] ( self , base: Self :: $RealField) -> Self ;
139- fn [ <$( $prefix) * log2>] ( self ) -> Self ;
140- fn [ <$( $prefix) * log10>] ( self ) -> Self ;
141- fn [ <$( $prefix) * ln>] ( self ) -> Self ;
142- fn [ <$( $prefix) * ln_1p>] ( self ) -> Self ;
143- fn [ <$( $prefix) * sqrt>] ( self ) -> Self ;
144- fn [ <$( $prefix) * exp>] ( self ) -> Self ;
145- fn [ <$( $prefix) * exp2>] ( self ) -> Self ;
146- fn [ <$( $prefix) * exp_m1>] ( self ) -> Self ;
147- fn [ <$( $prefix) * powi>] ( self , n: i32 ) -> Self ;
148- fn [ <$( $prefix) * powf>] ( self , n: Self :: $RealField) -> Self ;
149- fn [ <$( $prefix) * powc>] ( self , n: Self ) -> Self ;
150- fn [ <$( $prefix) * cbrt>] ( self ) -> Self ;
151- }
152- }
153- ) ;
154-
15514/// Trait shared by all complex fields and its subfields (like real numbers).
15615///
15716/// Complex numbers are equipped with functions that are commonly used on complex numbers and reals.
@@ -176,9 +35,140 @@ SubsetOf<Self>
17635+ Debug
17736+ Display
17837{
179- type RealField : RealField ;
180- complex_trait_methods ! ( RealField ) ;
38+ type RealField : RealField ; /// Builds a pure-real complex number from the given value.
39+ fn from_real ( re : Self :: RealField ) -> Self ;
40+
41+ /// The real part of this complex number.
42+ fn real ( self ) -> Self :: RealField ;
43+
44+ /// The imaginary part of this complex number.
45+ fn imaginary ( self ) -> Self :: RealField ;
46+
47+ /// The modulus of this complex number.
48+ fn modulus ( self ) -> Self :: RealField ;
49+
50+ /// The squared modulus of this complex number.
51+ fn modulus_squared ( self ) -> Self :: RealField ;
52+
53+ /// The argument of this complex number.
54+ fn argument ( self ) -> Self :: RealField ;
55+
56+ /// The sum of the absolute value of this complex number's real and imaginary part.
57+ fn norm1 ( self ) -> Self :: RealField ;
58+
59+ /// Multiplies this complex number by `factor`.
60+ fn scale ( self , factor : Self :: RealField ) -> Self ;
61+
62+ /// Divides this complex number by `factor`.
63+ fn unscale ( self , factor : Self :: RealField ) -> Self ;
64+
65+ /// The polar form of this complex number: (modulus, arg)
66+ fn to_polar ( self ) -> ( Self :: RealField , Self :: RealField ) {
67+ ( self . clone ( ) . modulus ( ) , self . argument ( ) )
68+ }
69+
70+ /// The exponential form of this complex number: (modulus, e^{i arg})
71+ fn to_exp ( self ) -> ( Self :: RealField , Self ) {
72+ let m = self . clone ( ) . modulus ( ) ;
73+
74+ if !m. is_zero ( ) {
75+ ( m. clone ( ) , self . unscale ( m) )
76+ } else {
77+ ( Self :: RealField :: zero ( ) , Self :: one ( ) )
78+ }
79+ }
80+
81+ /// The exponential part of this complex number: `self / self.modulus()`
82+ fn signum ( self ) -> Self {
83+ self . to_exp ( ) . 1
84+ }
85+
86+ fn floor ( self ) -> Self ;
87+ fn ceil ( self ) -> Self ;
88+ fn round ( self ) -> Self ;
89+ fn trunc ( self ) -> Self ;
90+ fn fract ( self ) -> Self ;
91+ fn mul_add ( self , a : Self , b : Self ) -> Self ;
92+
93+ /// The absolute value of this complex number: `self / self.signum()`.
94+ ///
95+ /// This is equivalent to `self.modulus()`.
96+ fn abs ( self ) -> Self :: RealField ;
97+
98+ /// Computes (self.conjugate() * self + other.conjugate() * other).sqrt()
99+ fn hypot ( self , other : Self ) -> Self :: RealField ;
100+
101+ fn recip ( self ) -> Self ;
102+ fn conjugate ( self ) -> Self ;
103+ fn sin ( self ) -> Self ;
104+ fn cos ( self ) -> Self ;
105+ fn sin_cos ( self ) -> ( Self , Self ) ;
106+ #[ inline]
107+ fn sinh_cosh ( self ) -> ( Self , Self ) {
108+ ( self . clone ( ) . sinh ( ) , self . cosh ( ) )
109+ }
110+ fn tan ( self ) -> Self ;
111+ fn asin ( self ) -> Self ;
112+ fn acos ( self ) -> Self ;
113+ fn atan ( self ) -> Self ;
114+ fn sinh ( self ) -> Self ;
115+ fn cosh ( self ) -> Self ;
116+ fn tanh ( self ) -> Self ;
117+ fn asinh ( self ) -> Self ;
118+ fn acosh ( self ) -> Self ;
119+ fn atanh ( self ) -> Self ;
120+
121+ /// Cardinal sine
122+ #[ inline]
123+ fn sinc ( self ) -> Self {
124+ if self . is_zero ( ) {
125+ Self :: one ( )
126+ } else {
127+ self . clone ( ) . sin ( ) / self
128+ }
129+ }
130+
131+ #[ inline]
132+ fn sinhc ( self ) -> Self {
133+ if self . is_zero ( ) {
134+ Self :: one ( )
135+ } else {
136+ self . clone ( ) . sinh ( ) / self
137+ }
138+ }
139+
140+ /// Cardinal cos
141+ #[ inline]
142+ fn cosc ( self ) -> Self {
143+ if self . is_zero ( ) {
144+ Self :: one ( )
145+ } else {
146+ self . clone ( ) . cos ( ) / self
147+ }
148+ }
149+
150+ #[ inline]
151+ fn coshc ( self ) -> Self {
152+ if self . is_zero ( ) {
153+ Self :: one ( )
154+ } else {
155+ self . clone ( ) . cosh ( ) / self
156+ }
157+ }
181158
159+ fn log ( self , base : Self :: RealField ) -> Self ;
160+ fn log2 ( self ) -> Self ;
161+ fn log10 ( self ) -> Self ;
162+ fn ln ( self ) -> Self ;
163+ fn ln_1p ( self ) -> Self ;
164+ fn sqrt ( self ) -> Self ;
165+ fn exp ( self ) -> Self ;
166+ fn exp2 ( self ) -> Self ;
167+ fn exp_m1 ( self ) -> Self ;
168+ fn powi ( self , n : i32 ) -> Self ;
169+ fn powf ( self , n : Self :: RealField ) -> Self ;
170+ fn powc ( self , n : Self ) -> Self ;
171+ fn cbrt ( self ) -> Self ;
182172 fn is_finite ( & self ) -> bool ;
183173 fn try_sqrt ( self ) -> Option < Self > ;
184174}
0 commit comments