From c05e53d801de54e37f415474e4f75d33778d1522 Mon Sep 17 00:00:00 2001 From: chachaleo Date: Tue, 12 Dec 2023 16:45:34 +0700 Subject: [PATCH] Complex64Num --- src/numbers.cairo | 235 +++++++ src/numbers/complex_number/complex64.cairo | 167 ++++- .../complex_number/complex_trait.cairo | 593 ++++++++++++++++-- tests/numbers/complex_number_test.cairo | 265 ++++++-- 4 files changed, 1154 insertions(+), 106 deletions(-) diff --git a/src/numbers.cairo b/src/numbers.cairo index 9b5d3ef8d..99d33c29a 100644 --- a/src/numbers.cairo +++ b/src/numbers.cairo @@ -2879,3 +2879,238 @@ impl u32Number of NumberTrait { lhs - rhs } } + + +use orion::numbers::complex_number::complex_trait::ComplexTrait; +use orion::numbers::complex_number::complex64::{ + Complex64Impl, complex64, Complex64Add, Complex64Sub +}; + + +impl Complex64Number of NumberTrait { + fn new(mag: FP64x64, sign: bool) -> complex64 { + panic(array!['not supported!']) + } + + fn new_unscaled(mag: FP64x64, sign: bool) -> complex64 { + panic(array!['not supported!']) + } + + fn from_felt(val: felt252) -> complex64 { + panic(array!['not supported!']) + } + + fn ceil(self: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn exp(self: complex64) -> complex64 { + Complex64Impl::exp(self) + } + + fn exp2(self: complex64) -> complex64 { + Complex64Impl::exp2(self) + } + + fn floor(self: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn ln(self: complex64) -> complex64 { + Complex64Impl::ln(self) + } + + fn log2(self: complex64) -> complex64 { + Complex64Impl::log2(self) + } + + fn log10(self: complex64) -> complex64 { + Complex64Impl::log10(self) + } + + fn pow(self: complex64, b: complex64) -> complex64 { + Complex64Impl::pow(self, b) + } + + fn round(self: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn sqrt(self: complex64) -> complex64 { + Complex64Impl::sqrt(self) + } + + fn acos(self: complex64) -> complex64 { + Complex64Impl::acos(self) + } + + fn asin(self: complex64) -> complex64 { + Complex64Impl::asin(self) + } + + fn atan(self: complex64) -> complex64 { + Complex64Impl::atan(self) + } + + fn cos(self: complex64) -> complex64 { + Complex64Impl::cos(self) + } + + fn sin(self: complex64) -> complex64 { + Complex64Impl::sin(self) + } + + fn tan(self: complex64) -> complex64 { + Complex64Impl::tan(self) + } + + fn acosh(self: complex64) -> complex64 { + Complex64Impl::acosh(self) + } + + fn asinh(self: complex64) -> complex64 { + Complex64Impl::asinh(self) + } + + fn atanh(self: complex64) -> complex64 { + Complex64Impl::atanh(self) + } + + fn cosh(self: complex64) -> complex64 { + Complex64Impl::cosh(self) + } + + fn sinh(self: complex64) -> complex64 { + Complex64Impl::sinh(self) + } + + fn tanh(self: complex64) -> complex64 { + Complex64Impl::tanh(self) + } + + fn zero() -> complex64 { + Complex64Impl::zero() + } + + fn is_zero(self: complex64) -> bool { + if self == Complex64Impl::zero() { + return true; + } + false + } + + fn half() -> complex64 { + panic(array!['not supported!']) + } + + fn one() -> complex64 { + Complex64Impl::one() + } + + fn neg_one() -> complex64 { + Complex64Impl::new(FP64x64 { mag: core_fp64x64::ONE, sign: true }, FP64x64Impl::ZERO()) + } + + fn is_one(self: complex64) -> bool { + if self == Complex64Impl::one() { + return true; + } + false + } + + fn abs(self: complex64) -> complex64 { + Complex64Impl::new(Complex64Impl::mag(self), FP64x64Impl::ZERO()) + } + + fn neg(self: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn min_value() -> complex64 { + panic(array!['not supported!']) + } + + fn max_value() -> complex64 { + panic(array!['not supported!']) + } + + fn min(self: complex64, other: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn max(self: complex64, other: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn mag(self: complex64) -> FP64x64 { + Complex64Impl::mag(self) + } + + fn is_neg(self: complex64) -> bool { + panic(array!['not supported!']) + } + + fn xor(lhs: complex64, rhs: complex64) -> bool { + panic(array!['not supported!']) + } + + fn or(lhs: complex64, rhs: complex64) -> bool { + panic(array!['not supported!']) + } + + fn sign(self: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn and(lhs: complex64, rhs: complex64) -> bool { + panic(array!['not supported!']) + } + + fn where(self: complex64, x: complex64, y: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn NaN() -> complex64 { + panic(array!['not supported!']) + } + + fn is_nan(self: complex64) -> bool { + panic(array!['not supported!']) + } + + fn INF() -> complex64 { + panic(array!['not supported!']) + } + + fn is_inf(self: complex64) -> bool { + panic(array!['not supported!']) + } + + fn is_pos_inf(self: complex64) -> bool { + panic(array!['not supported!']) + } + + fn is_neg_inf(self: complex64) -> bool { + panic(array!['not supported!']) + } + + fn bitwise_and(lhs: complex64, rhs: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn bitwise_xor(lhs: complex64, rhs: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn bitwise_or(lhs: complex64, rhs: complex64) -> complex64 { + panic(array!['not supported!']) + } + + fn add(lhs: complex64, rhs: complex64) -> complex64 { + Complex64Add::add(lhs, rhs) + } + + fn sub(lhs: complex64, rhs: complex64) -> complex64 { + Complex64Sub::sub(lhs, rhs) + } +} diff --git a/src/numbers/complex_number/complex64.cairo b/src/numbers/complex_number/complex64.cairo index 5b588fb9d..fe7b70581 100644 --- a/src/numbers/complex_number/complex64.cairo +++ b/src/numbers/complex_number/complex64.cairo @@ -20,6 +20,7 @@ const PI: u128 = 57952155664616982739; const HALF_PI: u128 = 28976077832308491370; const TWO: u128 = 36893488147419103232; const E: u128 = 50143449208471493718; +const HALF: u128 = 9223372036854775808; impl Complex64Impl of ComplexTrait { fn new(real: FP64x64, img: FP64x64) -> complex64 { @@ -61,6 +62,12 @@ impl Complex64Impl of ComplexTrait { complex64 { real, img } } + fn exp2(self: complex64) -> complex64 { + let two = complex64 { real: FP64x64Impl::new(TWO, false), img: FP64x64Impl::ZERO() }; + two.pow(self) + } + + fn sqrt(self: complex64) -> complex64 { let x = self.real; let y = self.img; @@ -77,6 +84,18 @@ impl Complex64Impl of ComplexTrait { complex64 { real, img } } + fn log2(self: complex64) -> complex64 { + let ln_2 = FP64x64Impl::new(12786309186476892720, false); + let ln = self.ln(); + complex64 { real: (ln.real / ln_2), img: (ln.img / ln_2) } + } + + fn log10(self: complex64) -> complex64 { + let ln_10 = FP64x64Impl::new(42475197399893398429, false); + let ln = self.ln(); + complex64 { real: (ln.real / ln_10), img: (ln.img / ln_10) } + } + fn pow(self: complex64, b: complex64) -> complex64 { let two = FP64x64Impl::new(TWO, false); let x = self.real; @@ -108,6 +127,132 @@ impl Complex64Impl of ComplexTrait { complex64 { real, img } } + //cos(z) = cos(a+bi) = cos(a)cosh(b)-isin(a)sinh(b) + fn cos(self: complex64) -> complex64 { + let a = self.real; + let b = self.img; + complex64 { + real: FP64x64Impl::cos(a) * FP64x64Impl::cosh(b), + img: -FP64x64Impl::sin(a) * FP64x64Impl::sinh(b) + } + } + + + //sin(z) = sin(a+bi) = sin(a)cosh(b)+icos(a)sinh(b) + fn sin(self: complex64) -> complex64 { + let a = self.real; + let b = self.img; + complex64 { + real: FP64x64Impl::sin(a) * FP64x64Impl::cosh(b), + img: FP64x64Impl::cos(a) * FP64x64Impl::sinh(b) + } + } + + //tan(z) = tan(a+bi) = sin(2a) / (cosh(2b) + cos(2a)) + i sinh(2b) / (cosh(2b) + cos(2a)) + fn tan(self: complex64) -> complex64 { + let two = FP64x64Impl::new(TWO, false); + let a = self.real; + let b = self.img; + let den = FP64x64Impl::cosh(two * b) + FP64x64Impl::cos(two * a); + complex64 { real: FP64x64Impl::sin(two * a) / den, img: FP64x64Impl::sinh(two * b) / den } + } + + //acos(z) = pi/2 + i ln (iz sqrt(1 - z**2)) + fn acos(self: complex64) -> complex64 { + let pi = Complex64Impl::new(FP64x64Impl::new(PI, false), FP64x64Impl::ZERO()); + let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); + let i = Complex64Impl::new(FP64x64Impl::ZERO(), FP64x64Impl::ONE()); + let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); + let acos = pi / two + + i * Complex64Impl::ln(i * self + Complex64Impl::sqrt(one - (self.pow(two)))); + + acos + } + + //asin(z) = - i ln (iz sqrt(1 - z**2)) + fn asin(self: complex64) -> complex64 { + let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); + let i = Complex64Impl::new(FP64x64Impl::ZERO(), FP64x64Impl::ONE()); + let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); + let asin = -i * Complex64Impl::ln(i * self + Complex64Impl::sqrt(one - (self.pow(two)))); + + asin + } + + + //atan(z) = 1/2 * i[ln (1 - iz) - ln(1 + iz)] + fn atan(self: complex64) -> complex64 { + let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); + let i = Complex64Impl::new(FP64x64Impl::ZERO(), FP64x64Impl::ONE()); + let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); + let atan = one + / two + * i + * (Complex64Impl::ln(one - i * self) - Complex64Impl::ln(one + i * self)); + + atan + } + + + //acosh(z) = ln (z + sqrt(z + 1) * sqrt(z - 1)) + fn acosh(self: complex64) -> complex64 { + let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); + let acosh = Complex64Impl::ln( + self + Complex64Impl::sqrt(self + one) * Complex64Impl::sqrt(self - one) + ); + + acosh + } + + //asinh(z) = ln (z + sqrt(z**2 + 1)) + fn asinh(self: complex64) -> complex64 { + let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); + let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); + let asinh = Complex64Impl::ln(self + Complex64Impl::sqrt(one + (self.pow(two)))); + + asinh + } + + + //atanh(z) = 1/2 * [ln (1 + z) - ln(1 - z)] + fn atanh(self: complex64) -> complex64 { + let two = Complex64Impl::new(FP64x64Impl::new(TWO, false), FP64x64Impl::ZERO()); + let i = Complex64Impl::new(FP64x64Impl::ZERO(), FP64x64Impl::ONE()); + let one = Complex64Impl::new(FP64x64Impl::ONE(), FP64x64Impl::ZERO()); + let atanh = (Complex64Impl::ln(one + self) - Complex64Impl::ln(one - self)) / two; + + atanh + } + + //acos(z) = acos(a+bi) = cosh a * cos b + i sinh a * sin b + fn cosh(self: complex64) -> complex64 { + let a = self.real; + let b = self.img; + complex64 { + real: FP64x64Impl::cosh(a) * FP64x64Impl::cos(b), + img: FP64x64Impl::sinh(a) * FP64x64Impl::sin(b) + } + } + + //sinh(z) = sin(a+bi) = sinh(a)cos(b)+icosh(a)sin(b) + fn sinh(self: complex64) -> complex64 { + let a = self.real; + let b = self.img; + complex64 { + real: FP64x64Impl::sinh(a) * FP64x64Impl::cos(b), + img: FP64x64Impl::cosh(a) * FP64x64Impl::sin(b) + } + } + + //tanh(z) = tan(a+bi) = sin(2a) / (cosh(2a) + cos(2b)) + i sinh(2b) / (cosh(2a) + cos(2b)) + fn tanh(self: complex64) -> complex64 { + let two = FP64x64Impl::new(TWO, false); + let a = self.real; + let b = self.img; + let den = FP64x64Impl::cosh(two * a) + FP64x64Impl::cos(two * b); + complex64 { real: FP64x64Impl::sinh(two * a) / den, img: FP64x64Impl::sin(two * b) / den } + } + fn to_polar(self: complex64) -> (FP64x64, FP64x64) { let mag = self.mag(); @@ -143,7 +288,7 @@ fn atan2(x: FP64x64, y: FP64x64) -> FP64x64 { } } -impl complex64Print of PrintTrait { +impl Complex64Print of PrintTrait { fn print(self: complex64) { self.real.print(); '+'.print(); @@ -153,14 +298,14 @@ impl complex64Print of PrintTrait { } // Implements the Add trait for complex64. -impl complex64Add of Add { +impl Complex64Add of Add { fn add(lhs: complex64, rhs: complex64) -> complex64 { complex64_add(lhs, rhs) } } // Implements the AddEq trait for complex64. -impl complex64AddEq of AddEq { +impl Complex64AddEq of AddEq { #[inline(always)] fn add_eq(ref self: complex64, other: complex64) { self = Add::add(self, other); @@ -168,14 +313,14 @@ impl complex64AddEq of AddEq { } // Implements the Sub trait for complex64. -impl complex64Sub of Sub { +impl Complex64Sub of Sub { fn sub(lhs: complex64, rhs: complex64) -> complex64 { complex64_sub(lhs, rhs) } } // Implements the SubEq trait for complex64. -impl complex64SubEq of SubEq { +impl Complex64SubEq of SubEq { #[inline(always)] fn sub_eq(ref self: complex64, other: complex64) { self = Sub::sub(self, other); @@ -183,14 +328,14 @@ impl complex64SubEq of SubEq { } // Implements the Mul trait for complex64. -impl complex64Mul of Mul { +impl Complex64Mul of Mul { fn mul(lhs: complex64, rhs: complex64) -> complex64 { complex64_mul(lhs, rhs) } } // Implements the MulEq trait for complex64. -impl complex64MulEq of MulEq { +impl Complex64MulEq of MulEq { #[inline(always)] fn mul_eq(ref self: complex64, other: complex64) { self = Mul::mul(self, other); @@ -198,14 +343,14 @@ impl complex64MulEq of MulEq { } // Implements the Div trait for complex64. -impl complex64Div of Div { +impl Complex64Div of Div { fn div(lhs: complex64, rhs: complex64) -> complex64 { complex64_div(lhs, rhs) } } // Implements the DivEq trait for complex64. -impl complex64DivEq of DivEq { +impl Complex64DivEq of DivEq { #[inline(always)] fn div_eq(ref self: complex64, other: complex64) { self = Div::div(self, other); @@ -214,7 +359,7 @@ impl complex64DivEq of DivEq { // Implements the PartialEq trait for complex64. -impl complex64PartialEq of PartialEq { +impl Complex64PartialEq of PartialEq { fn eq(lhs: @complex64, rhs: @complex64) -> bool { complex64_eq(*lhs, *rhs) } @@ -225,7 +370,7 @@ impl complex64PartialEq of PartialEq { } // Implements the Neg trait for complex64. -impl i8Neg of Neg { +impl Complex64Neg of Neg { fn neg(a: complex64) -> complex64 { complex64_neg(a) } diff --git a/src/numbers/complex_number/complex_trait.cairo b/src/numbers/complex_number/complex_trait.cairo index 37cf8396b..170d0f27a 100644 --- a/src/numbers/complex_number/complex_trait.cairo +++ b/src/numbers/complex_number/complex_trait.cairo @@ -1,20 +1,36 @@ /// Trait /// -/// new - Constructs a new `complex_number` -/// real - Returns the real part of the `complex_number` -/// img - Returns the imaginary part of the `complex_number` -/// conjugate - Returns the conjugate of the `complex_number` -/// zero - Returns the additive identity element zero -/// one - Returns the multiplicative identity element one -/// mag - Returns the magnitude of the `complex_number` -/// arg - Returns the argument of the `complex_number` -/// exp - Returns the value of e raised to the power of the `complex_number` -/// sqrt - Returns the value of the squre root of the `complex_number` -/// pow - Returns the result of raising the `complex_number` to the power of another `complex_number` -/// ln - Returns the natural logarithm of the `complex_number` -/// to_polar - Returns the polar coordinates of the `complex_number` -/// from_polar - Returns a `complex_number` from the polar coordinates of the `complex_number` -/// reciprocal - Returns a the reciprocal of the `complex_number` +/// new - Constructs a new `complex_number`. +/// from_felt - Creates a new `complex_number` instance from two felt252 values. +/// real - Returns the real part of the `complex_number`. +/// img - Returns the imaginary part of the `complex_number`. +/// conjugate - Returns the conjugate of the `complex_number`. +/// zero - Returns the additive identity element zero. +/// one - Returns the multiplicative identity element one. +/// mag - Returns the magnitude of the `complex_number`. +/// arg - Returns the argument of the `complex_number`. +/// exp - Returns the value of e raised to the power of the `complex_number`. +/// exp2 - Returns the value of 2 raised to the power of the `complex_number`. +/// ln - Returns the natural logarithm of the `complex_number`. +/// log2 - Returns the base-2 logarithm of the `complex_number`. +/// log10 - Returns the base-10 logarithm of the `complex_number`. +/// pow - Returns the result of raising the `complex_number` to the power of another `complex_number`. +/// sqrt - Returns the value of the squre root of the `complex_number`. +/// acos - Returns the arccosine (inverse of cosine) of the `complex_number`. +/// asin - Returns the arcsine (inverse of sine) of the `complex_number`. +/// atan - Returns the arctangent (inverse of tangent) of the input `complex_number`. +/// cos - Returns the cosine of the `complex_number`. +/// sin - Returns the sine of the `complex_number`. +/// tan - Returns the tangent of the `complex_number`. +/// acosh - Returns the value of the inverse hyperbolic cosine of the `complex_number`. +/// asinh - Returns the value of the inverse hyperbolic sine of the `complex_number`. +/// atanh - Returns the value of the inverse hyperbolic tangent of the `complex_number`. +/// cosh - Returns the value of the hyperbolic cosine of the `complex_number`. +/// sinh - Returns the value of the hyperbolic sine of the `complex_number`. +/// tanh - Returns the value of the hyperbolic tangent of the `complex_number`. +/// to_polar - Returns the polar coordinates of the `complex_number`. +/// from_polar - Returns a `complex_number` from the polar coordinates of the `complex_number`. +/// reciprocal - Returns a the reciprocal of the `complex_number`. /// trait ComplexTrait { /// # ComplexTrait::new @@ -288,40 +304,142 @@ trait ComplexTrait { /// ``` /// fn exp(self: T) -> T; - /// # ComplexTrait::sqrt + /// # ComplexTrait::exp2 /// /// ```rust - /// fn arg(self: T) -> F; + /// fn exp2(self: T) -> T; /// ``` /// - /// Returns the value of the squre root of the complex number. + /// Returns the value of 2 raised to the power of the complex number. /// /// ## Args - /// + /// /// * `self`(`T`) - The input complex number /// /// ## Returns + /// + /// The binary exponent of the input complex number. + /// + /// ## Examples /// - /// A complex number '', representing the square root of the complex number. - /// 'arg(z) = atan2(b, a)'. + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; /// - /// ## Examples + /// fn exp2_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(73786976294838206464, false), + /// FixedTrait::new(774763251095801167872, false) + /// ); // 4 + 42i + /// ComplexTrait::exp2(z) + /// } + /// >>> {real: {mag: 197471674372309809080, sign: true}, im: {mag: 219354605088992285353, sign: true}} // -10.70502356986 -11.89127707 i + /// ``` /// - /// ```rust + fn exp2(self: T) -> T; + /// # ComplexTrait::ln + /// + /// ```rust + /// fn ln(self: T) -> T; + /// ``` + /// + /// Returns the natural logarithm of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// A complex number representing the natural logarithm of the input number. + /// + /// ## Examples + /// + /// ```rust /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; /// - /// fn sqrt_complex64_example() -> complex64 { + /// fn ln_complex64_example() -> complex64 { /// let z: complex64 = ComplexTrait::new( /// FixedTrait::new(73786976294838206464, false), /// FixedTrait::new(774763251095801167872, false) /// ); // 4 + 42i - /// z.sqrt() + /// z.ln() /// } - /// >>> {real: {mag: 88650037379463118848, sign: false}, im: {mag: 80608310115317055488, sign: false}} // 4.80572815603723 + 4.369785247552674 i + /// >>> {real: {mag: 69031116512113681970, sign: false}, im: {mag: 27224496882576083824, sign: false}} // 3.7421843216430655 + 1.4758446204521403 i + /// ``` + /// + fn ln(self: T) -> T; + /// # ComplexTrait::log2 + /// + /// ```rust + /// fn log2(self: T) -> T; /// ``` /// - fn sqrt(self: T) -> T; + /// Returns the base-2 logarithm of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Panics + /// + /// * Panics if the input is negative. + /// + /// ## Returns + /// + /// A complex number representing the binary logarithm of the input number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn log2_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.log2() + /// } + /// >>> {real: {mag: 34130530934667840346, sign: false}, im: {mag: 26154904847122126193, sign: false}} // 1.85021986 + 1.41787163 i + /// ``` + /// + fn log2(self: T) -> T; + /// # ComplexTrait::log10 + /// + /// ```rust + /// fn log10(self: T) -> T; + /// ``` + /// + /// Returns the base-10 logarithm of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// A complex number representing the base 10 logarithm of the input number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn log10_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.log10() + /// } + /// >>> {real: {mag: 10274314139629458970, sign: false}, im: {mag: 7873411322133748801, sign: false}} // 0.5569716761 + 0.4268218908 i + /// ``` + /// + fn log10(self: T) -> T; /// # ComplexTrait::pow /// /// ```rust @@ -372,21 +490,88 @@ trait ComplexTrait { /// ``` /// fn pow(self: T, b: T) -> T; - /// # ComplexTrait::ln + /// # ComplexTrait::sqrt + /// + /// ```rust + /// fn arg(self: T) -> F; + /// ``` + /// + /// Returns the value of the squre root of the complex number. /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number + /// + /// ## Returns + /// + /// A complex number '', representing the square root of the complex number. + /// 'arg(z) = atan2(b, a)'. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn sqrt_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(73786976294838206464, false), + /// FixedTrait::new(774763251095801167872, false) + /// ); // 4 + 42i + /// z.sqrt() + /// } + /// >>> {real: {mag: 88650037379463118848, sign: false}, im: {mag: 80608310115317055488, sign: false}} // 4.80572815603723 + 4.369785247552674 i + /// ``` + /// + fn sqrt(self: T) -> T; + /// # ComplexTrait::acos + /// /// ```rust - /// fn ln(self: T) -> T; + /// fn acos(self: T) -> T; /// ``` /// - /// Returns the natural logarithm of the complex number. + /// Returns the arccosine (inverse of cosine) of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// A complex number representing the acos of the input value. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn acos_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.acos() + /// } + /// >>> {real: {mag: 18449430688981877061, sign: false}, im: {mag: 36587032881711954470, sign: true}} // 1.000143542473797 - 1.98338702991653i + /// ``` + /// + fn acos(self: T) -> T; + /// # ComplexTrait::asin /// + /// ```rust + /// fn asin(self: T) -> T; + /// ``` + /// + /// Returns the arcsine (inverse of sine) of the complex number. + /// /// ## Args /// /// * `self`(`T`) - The input complex number. /// - /// ## Returns + /// ## Returns /// - /// A complex number representing the natural logarithm of the input number. + /// A complex number representing the asin of the input value. /// /// ## Examples /// @@ -394,22 +579,348 @@ trait ComplexTrait { /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; /// - /// fn ln_complex64_example() -> complex64 { + /// fn asin_complex64_example() -> complex64 { /// let z: complex64 = ComplexTrait::new( - /// FixedTrait::new(73786976294838206464, false), - /// FixedTrait::new(774763251095801167872, false) - /// ); // 4 + 42i - /// z.ln() + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.asin() /// } - /// >>> {real: {mag: 69031116512113681970, sign: false}, im: {mag: 27224496882576083824, sign: false}} // 3.7421843216430655 + 1.4758446204521403 i + /// >>> {real: {mag: 10526647143326614308, sign: false}, im: {mag: 36587032881711954470, sign: false}} // 0.57065278432 + 1.9833870299i /// ``` /// - fn ln(self: T) -> T; + fn asin(self: T) -> T; + /// # ComplexTrait::atan + /// + /// ```rust + /// fn atan(self: T) -> T; + /// ``` + /// + /// Returns the arctangent (inverse of tangent) of the input complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// A complex number representing the arctangent (inverse of tangent) of the input value. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn atan_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.atan() + /// } + /// >>> {real: {mag: 26008453796191787243, sign: false}, im: {mag: 4225645162986888119, sign: false}} // 1.40992104959 + 0.2290726829i + /// ``` + /// + fn atan(self: T) -> T; + /// # ComplexTrait::cos + /// + /// ```rust + /// fn cos(self: T) -> T; + /// ``` + /// + /// Returns the cosine of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// A complex number representing the cosine of the input value. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn cos_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.cos() + /// } + /// >>> {real: {mag: 77284883172661882094, sign: true}, im: {mag: 168035443352962049425, sign: true}} // -4.18962569 + -9.10922789375i + /// ``` + + /// + fn cos(self: T) -> T; + /// # ComplexTrait::sin + /// + /// ```rust + /// fn sin(self: T) -> T; + /// ``` + /// + /// Returns the sine of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// A complex number representing the sin of the input value. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn sin_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.sin() + /// } + /// >>> {real: {mag: 168870549816927860082, sign: false}, im: {mag: 76902690389051588309, sign: true}} // 9.15449914 - 4.168906959 i + /// ``` + /// + fn sin(self: T) -> T; + /// # ComplexTrait::tan + /// + /// ```rust + /// fn tan(self: T) -> T; + /// ``` + /// + /// Returns the tangent of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// A complex number representing the tan of the input value. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn tan_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.tan() + /// } + /// >>> {real: {mag: 69433898428143694, sign: true}, im: {mag: 18506486100303669886, sign: false}} // -0.00376402 + 1.00323862i + /// ``` + /// + fn tan(self: T) -> T; + /// # ComplexTrait::acosh + /// + /// ```rust + /// fn acosh(self: T) -> T; + /// ``` + /// + /// Returns the value of the inverse hyperbolic cosine of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// The inverse hyperbolic cosine of the input complex number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn acosh_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.acosh() + /// } + /// >>> {real: {mag: 36587032878947915965, sign: false}, im: {mag: 18449360714192945790, sign: false}} // 1.9833870 + 1.0001435424i + /// ``` + /// + fn acosh(self: T) -> T; + /// # ComplexTrait::asinh + /// + /// ```rust + /// fn asinh(self: T) -> T; + /// ``` + /// + /// Returns the value of the inverse hyperbolic sine of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// The inverse hyperbolic sine of the input complex number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn asinh_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.asinh() + /// } + /// >>> {real: {mag: 36314960239770126586, sign: false}, im: {mag: 17794714057579789616, sign: false}} //1.9686379 + 0.964658504i + /// ``` + /// + fn asinh(self: T) -> T; + /// # ComplexTrait::atanh + /// + /// ```rust + /// fn atanh(self: T) -> T; + /// ``` + /// + /// Returns the value of the inverse hyperbolic tangent of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// The inverse hyperbolic tangent of the input complex number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn atanh_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.atanh() + /// } + /// >>> {real: {mag: 2710687792925618924, sign: false}, im: {mag: 24699666646262346226, sign: false}} // 0.146946666 + 1.33897252i + /// ``` + + /// + fn atanh(self: T) -> T; + /// # ComplexTrait::cosh + /// + /// ```rust + /// fn cosh(self: T) -> T; + /// ``` + /// + /// Returns the value of the hyperbolic cosine of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// The hyperbolic cosine of the input complex number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn cosh_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.cosh() + /// } + /// >>> {real: {mag: 68705646899632870392, sign: true}, im: {mag: 9441447324287988702, sign: false}} // -3.72454550491 + 0.511822569987i + /// ``` /// - //fn log2(self: T) -> T; + fn cosh(self: T) -> T; + /// # ComplexTrait::sinh + /// + /// ```rust + /// fn sinh(self: T) -> T; + /// ``` + /// + /// Returns the value of the hyperbolic sine of the complex number. + /// ## Args + /// + /// * `self`(`T`) - The input complex number. /// - //fn log10(self: T) -> T; + /// ## Returns + /// + /// The hyperbolic sine of the input complex number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn sinh_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.sinh() + /// } + /// >>> {real: {mag: 66234138518106676624, sign: true}, im: {mag: 9793752294470951790, sign: false}} // -3.59056458998 + 0.530921086i + /// ``` + /// + fn sinh(self: T) -> T; + /// # ComplexTrait::tanh + /// + /// ```rust + /// fn tanh(self: T) -> T; + /// ``` + /// + /// Returns the value of the hyperbolic tangent of the complex number. + /// + /// ## Args + /// + /// * `self`(`T`) - The input complex number. + /// + /// ## Returns + /// + /// The hyperbolic tangent of the input complex number. + /// + /// ## Examples + /// + /// ```rust + /// use orion::numbers::complex_number::{complex_trait::ComplexTrait, complex64::complex64}; + /// use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; + /// + /// fn tanh_complex64_example() -> complex64 { + /// let z: complex64 = ComplexTrait::new( + /// FixedTrait::new(36893488147419103232, false), + /// FixedTrait::new(55340232221128654848, false) + /// ); // 2 + 3i + /// z.tanh() + /// } + /// >>> {real: {mag: 17808227710002974080, sign: false}, im: {mag: 182334107030204896, sign: true}} // 0.96538587902 + 0.009884375i + /// ``` /// + fn tanh(self: T) -> T; /// # ComplexTrait::to_polar /// /// ```rust diff --git a/tests/numbers/complex_number_test.cairo b/tests/numbers/complex_number_test.cairo index 9b91c0f5a..053bb83ad 100644 --- a/tests/numbers/complex_number_test.cairo +++ b/tests/numbers/complex_number_test.cairo @@ -1,3 +1,4 @@ +//use cubit::f128::types::fixed::FixedTrait; use orion::numbers::complex_number::complex_trait::ComplexTrait; use orion::numbers::complex_number::complex64::{TWO, complex64}; use orion::numbers::{FP64x64, FP64x64Impl, FixedTrait}; @@ -209,9 +210,6 @@ fn test_mag() { ); let mag = a.mag(); assert(mag == FixedTrait::new(0x2a30a6de7900000000, false), 'mag = 42.190046219457976'); -// should be 778268985068318500000 -// is : 778268985067028086784 - } #[test] @@ -228,8 +226,6 @@ fn test_arg() { assert( arg == FixedTrait::::new(27224496882576083824, false), 'arg = 1.4758446204521403' ); -// should be 27224528006041640000 -// is : 27224496882576083824 } #[test] @@ -245,15 +241,22 @@ fn test_exp() { let z_expected: complex64 = ComplexTrait::new( FixedTrait::new(402848450095324460000, true), FixedTrait::new(923082101320478400000, true) ); -// real part : -// should be 402848450095324460000 -// is : 402847992570293444378 +} -// img part : -// should be 923082101320478400000 -// is : 923081058030224714169 -//assert(z == z_expected, '-21.838458238788455-50.04038098170736j'); +#[test] +#[available_gas(2000000000)] +fn test_exp2() { + // Test exp2 of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(73786976294838206464, false), FixedTrait::new(774763251095801167872, false) + ); // 4 + 42i + + let exp2 = a.exp2(); + assert( + exp2.real == FixedTrait::new(197471674372309809080, true), 'exp2.real = -10.70502356986' + ); + assert(exp2.img == FixedTrait::new(219354605088992285353, true), 'exp2.img = -11.89127707'); } @@ -267,15 +270,9 @@ fn test_sqrt() { let sqrt = a.sqrt(); assert(sqrt.real == FixedTrait::new(88650037379463118848, false), 'real = 4.80572815603723'); assert(sqrt.img == FixedTrait::new(80608310115317055488, false), 'img = 4.369785247552674'); -// real part : -// should be 88650037382238900000 -// is : 88650037379463118848 - -// img part : -// should be 80608310118675710000 -// is : 80608310115317055488 } + #[test] #[available_gas(2000000000)] fn test_ln() { @@ -286,15 +283,204 @@ fn test_ln() { let ln = a.ln(); assert(ln.real == FixedTrait::new(69031116512113681970, false), 'ln.real = 3.7421843216430655'); assert(ln.img == FixedTrait::new(27224496882576083824, false), 'ln.img = 1.4758446204521403'); -// real part : -// should be 69031116457998020000 -// is : 69031116512113681970 +} + + +#[test] +#[available_gas(2000000000)] +fn test_log2() { + // Test log2 of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let log2 = a.log2(); + assert(log2.real == FixedTrait::new(34130530934667840346, false), 'log2.real = 1.85021986'); + assert(log2.img == FixedTrait::new(26154904847122126193, false), 'log2.img = 1.41787163'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_log10() { + // Test log10 of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let log10 = a.log10(); + assert( + log10.real == FixedTrait::new(10274314139629458970, false), + 'log10.real = 0.5569716761534184' + ); + assert( + log10.img == FixedTrait::new(7873411322133748801, false), 'log10.img = 0.42682189085546657' + ); +} + +#[test] +#[available_gas(2000000000)] +fn test_acos() { + // Test acos of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let acos = a.acos(); + assert( + acos.real == FixedTrait::new(18449430688981877061, false), 'acos.real = 1.000143542473797' + ); + assert(acos.img == FixedTrait::new(36587032881711954470, true), 'acos.img = -1.98338702991653'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_asin() { + // Test asin of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let asin = a.asin(); + assert(asin.real == FixedTrait::new(10526647143326614308, false), 'asin.real = 0.57065278432'); + assert(asin.img == FixedTrait::new(36587032881711954470, false), 'asin.img = 1.9833870299'); +} + +#[test] +#[available_gas(2000000000)] +fn test_atan() { + // Test atan of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let atan = a.atan(); + assert(atan.real == FixedTrait::new(26008453796191787243, false), 'atan.real = 1.40992104959'); + assert(atan.img == FixedTrait::new(4225645162986888119, false), 'atan.img = 0.229072682968538'); +} -// img part : -// should be 27224528006041640000 -// is : 27224496882576083824 +#[test] +#[available_gas(2000000000)] +fn test_cos() { + // Test cos of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let cos = a.cos(); + assert( + cos.real == FixedTrait::new(77284883172661882094, true), 'cos.real = -4.189625690968807' + ); + assert(cos.img == FixedTrait::new(168035443352962049425, true), 'cos.img = -9.109227893755337'); } + +#[test] +#[available_gas(2000000000)] +fn test_sin() { + // Test sin of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let sin = a.sin(); + assert( + sin.real == FixedTrait::new(168870549816927860082, false), 'sin.real = 9.15449914691143' + ); + assert(sin.img == FixedTrait::new(76902690389051588309, true), 'sin.img = -4.168906959966565'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_tan() { + // Test tan of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + + let tan = a.tan(); + assert(tan.real == FixedTrait::new(69433898428143694, true), 'tan.real = -0.003764025641'); + assert(tan.img == FixedTrait::new(18506486100303669886, false), 'tan.img = 1.00323862735361'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_acosh() { + // Test acosh of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let acosh = a.acosh(); + assert(acosh.real == FixedTrait::new(36587032878947915965, false), 'acosh.real = 1.9833870'); + assert(acosh.img == FixedTrait::new(18449360714192945790, false), 'acosh.img = 1.0001435424'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_asinh() { + // Test asinh of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let asinh = a.asinh(); + assert( + asinh.real == FixedTrait::new(36314960239770126586, false), 'asinh.real = 1.96863792579' + ); + assert(asinh.img == FixedTrait::new(17794714057579789616, false), 'asinh.img = 0.96465850440'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_atanh() { + // Test atanh of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let atanh = a.atanh(); + assert( + atanh.real == FixedTrait::new(2710687792925618924, false), 'atanh.real = 0.146946666225' + ); + assert(atanh.img == FixedTrait::new(24699666646262346226, false), 'atanh.img = 1.3389725222'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_cosh() { + // Test cosh of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let cosh = a.cosh(); + assert(cosh.real == FixedTrait::new(68705646899632870392, true), 'cosh.real = -3.72454550491'); + assert(cosh.img == FixedTrait::new(9441447324287988702, false), 'cosh.img = 0.511822569987'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_sinh() { + // Test sinh of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let sinh = a.sinh(); + assert(sinh.real == FixedTrait::new(66234138518106676624, true), 'sinh.real = -3.59056458998'); + assert(sinh.img == FixedTrait::new(9793752294470951790, false), 'sinh.img = 0.530921086'); +} + + +#[test] +#[available_gas(2000000000)] +fn test_tanh() { + // Test tanh of a complex number + let a: complex64 = ComplexTrait::new( + FixedTrait::new(36893488147419103232, false), FixedTrait::new(55340232221128654848, false) + ); + let tanh = a.tanh(); + assert(tanh.real == FixedTrait::new(17808227710002974080, false), 'tanh.real = 0.96538587902'); + assert(tanh.img == FixedTrait::new(182334107030204896, true), 'tanh.img = 0.009884375'); +} + + #[test] #[available_gas(2000000000)] fn test_pow() { @@ -320,14 +506,6 @@ fn test_pow() { assert(pow.real == FixedTrait::new(389305023520047451076807, true), 'pow.real = -21104'); assert(pow.img == FixedTrait::new(1329485652886846033475029, true), 'pow.img = 72072'); - // real part : - // should be 389300086931566377304064 - // is : 389305023520047451076807 - - // img part : - // should be 1329493738880394804068352 - // is : 1329485652886846033475029 - // Test pow with exp = w, complex let w: complex64 = ComplexTrait::new( FixedTrait::new(36893488147419103232, false), FixedTrait::new(18446744073709551616, false) @@ -340,13 +518,6 @@ fn test_pow() { assert( pow.img == FixedTrait::new(2996539405459717736042, false), 'pow.img = 162.4438823807959' ); -// real part : -// should be 6881530958869354000000 -// is : 6881545343236111419203 - -// img part : -// should be 2996560724618318400000 -// is : 2996539405459717736042 } #[test] @@ -360,13 +531,6 @@ fn test_to_polar() { assert(mag == FixedTrait::new(778268985067028086784, false), 'mag = 42.190046219457976'); assert(arg == FixedTrait::new(27224496882576083824, false), 'arg = 1.4758446204521403'); -// mag : -// should be 778268985067028086784 -// is : 778268985068318500000 - -// arg : -// should be 27224496882576083824 -// is : 27224528006041640000 } #[test] @@ -380,13 +544,6 @@ fn test_from_polar() { let z_expected: complex64 = ComplexTrait::new( FixedTrait::new(73787936714814843012, false), FixedTrait::new(774759489569697723777, false) ); - // mag : - // should be 73786976294838206464 - // is : 73787936714814843012 - - // img : - // should be 774763251095801167872 - // is : 774759489569697723777 assert(z_actual == z_expected, 'wrong number'); }