1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::f64::EPSILON;
use std::f64::consts::PI;

use crate::pid::{EuclideanRing, PrincipalIdealRing};
use crate::field::Field;
use crate::integer::{IntegerRingStore, IntegerRing};
use crate::impl_eq_based_self_iso;
use crate::ring::*;
use crate::divisibility::{DivisibilityRing, Domain};

use super::float_real::Real64;

///
/// An approximate implementation of the complex numbers `C`, using 64 bit floating
/// point numbers.
/// 
/// # Warning
/// 
/// Since floating point numbers do not exactly represent the complex numbers, and this crate follows
/// a mathematically precise approach, we cannot provide any function related to equality.
/// In particular, `Complex64Base.eq_el(a, b)` is not supported, and will panic. 
/// Hence, this ring has only limited use within this crate, and is currently only used for
/// floating-point FFTs. 
/// 
#[derive(Clone, Copy, PartialEq)]
pub struct Complex64Base;

#[derive(Clone, Copy)]
pub struct Complex64El(f64, f64);

///
/// [`RingStore`] corresponding to [`Complex64Base`]
/// 
pub type Complex64 = RingValue<Complex64Base>;

impl Complex64 {

    pub const RING: Self = RingValue::from(Complex64Base);
    pub const I: Complex64El = Complex64El(0., 1.);
}

impl Complex64Base {

    pub fn abs(&self, Complex64El(re, im): Complex64El) -> f64 {
        (re * re + im * im).sqrt()
    }

    pub fn conjugate(&self, Complex64El(re, im): Complex64El) -> Complex64El {
        Complex64El(re, -im)
    }

    pub fn exp(&self, Complex64El(exp_re, exp_im): Complex64El) -> Complex64El {
        let angle = exp_im;
        let abs = exp_re.exp();
        Complex64El(abs * angle.cos(), abs * angle.sin())
    }

    pub fn closest_gaussian_int(&self, Complex64El(re, im): Complex64El) -> (i64, i64) {
        (re.round() as i64, im.round() as i64)
    }

    pub fn ln_main_branch(&self, Complex64El(re, im): Complex64El) -> Complex64El {
        Complex64El(self.abs(Complex64El(re, im)).ln(), im.atan2(re))
    }

    pub fn is_absolute_approx_eq(&self, lhs: Complex64El, rhs: Complex64El, absolute_threshold: f64) -> bool {
        self.abs(self.sub(lhs, rhs)) < absolute_threshold
    }

    pub fn is_relative_approx_eq(&self, lhs: Complex64El, rhs: Complex64El, relative_limit: f64) -> bool {
        self.is_absolute_approx_eq(lhs, rhs, self.abs(lhs) * relative_limit)
    }

    pub fn is_approx_eq(&self, lhs: Complex64El, rhs: Complex64El, precision: u64) -> bool {
        let scaled_precision = precision as f64 * EPSILON;
        if self.is_absolute_approx_eq(lhs, self.zero(), scaled_precision) {
            self.is_absolute_approx_eq(rhs, self.zero(), scaled_precision)
        } else {
            self.is_relative_approx_eq(lhs, rhs, scaled_precision)
        }
    }

    pub fn from_f64(&self, x: f64) -> Complex64El {
        Complex64El(x, 0.)
    }

    pub fn root_of_unity(&self, i: i64, n: i64) -> Complex64El {
        self.exp(self.mul(self.from_f64((i as f64 / n as f64) * (2. * PI)), Complex64::I))
    }

    pub fn re(&self, Complex64El(re, _im): Complex64El) -> f64 {
        re
    }

    pub fn im(&self, Complex64El(_re, im): Complex64El) -> f64 {
        im
    }
}

impl Complex64 {
    
    pub fn abs(&self, val: Complex64El) -> f64 { self.get_ring().abs(val) }

    pub fn conjugate(&self, val: Complex64El) -> Complex64El { self.get_ring().conjugate(val) }

    pub fn exp(&self, exp: Complex64El) -> Complex64El { self.get_ring().exp(exp) }

    pub fn closest_gaussian_int(&self, val: Complex64El) -> (i64, i64) { self.get_ring().closest_gaussian_int(val) }

    pub fn ln_main_branch(&self, val: Complex64El) -> Complex64El { self.get_ring().ln_main_branch(val) }

    pub fn is_absolute_approx_eq(&self, lhs: Complex64El, rhs: Complex64El, absolute_threshold: f64) -> bool { self.get_ring().is_absolute_approx_eq(lhs, rhs, absolute_threshold) }

    pub fn is_relative_approx_eq(&self, lhs: Complex64El, rhs: Complex64El, relative_limit: f64) -> bool { self.get_ring().is_relative_approx_eq(lhs, rhs, relative_limit) }

    pub fn is_approx_eq(&self, lhs: Complex64El, rhs: Complex64El, precision: u64) -> bool { self.get_ring().is_approx_eq(lhs, rhs, precision) }

    pub fn from_f64(&self, x: f64) -> Complex64El { self.get_ring().from_f64(x) }

    pub fn root_of_unity(&self, i: i64, n: i64) -> Complex64El { self.get_ring().root_of_unity(i, n) }

    pub fn re(&self, x: Complex64El) -> f64 { self.get_ring().re(x) }

    pub fn im(&self, x: Complex64El) -> f64 { self.get_ring().im(x) }
}

impl RingBase for Complex64Base {
 
    type Element = Complex64El;
    
    fn clone_el(&self, val: &Self::Element) -> Self::Element {
        *val
    }

    fn add_assign(&self, Complex64El(lhs_re, lhs_im): &mut Self::Element, Complex64El(rhs_re, rhs_im): Self::Element) {
        *lhs_re += rhs_re;
        *lhs_im += rhs_im;
    }

    fn negate_inplace(&self, Complex64El(re, im): &mut Self::Element) {
        *re = -*re;
        *im = -*im;
    }

    fn mul_assign(&self, Complex64El(lhs_re, lhs_im): &mut Self::Element, Complex64El(rhs_re, rhs_im): Self::Element) {
        let new_im = *lhs_re * rhs_im + *lhs_im * rhs_re;
        *lhs_re = *lhs_re * rhs_re - *lhs_im * rhs_im;
        *lhs_im = new_im;
    }

    fn from_int(&self, value: i32) -> Self::Element {
        Complex64El(value as f64, 0.)
    }
    
    fn eq_el(&self, _: &Self::Element, _: &Self::Element) -> bool {
        panic!("Cannot provide equality on approximate rings")
    }

    fn pow_gen<R: IntegerRingStore>(&self, x: Self::Element, power: &El<R>, integers: R) -> Self::Element 
        where R::Type: IntegerRing
    {
        self.exp(self.mul(self.ln_main_branch(x), Complex64El(integers.to_float_approx(power), 0.)))
    }

    fn is_commutative(&self) -> bool { true }

    fn is_noetherian(&self) -> bool { true }

    fn is_approximate(&self) -> bool { true }

    fn dbg<'a>(&self, Complex64El(re, im): &Self::Element, out: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
        write!(out, "{} + {}i", re, im)
    }
    
    fn characteristic<I: IntegerRingStore>(&self, ZZ: &I) -> Option<El<I>>
        where I::Type: IntegerRing
    {
        Some(ZZ.zero())
    }
}

impl_eq_based_self_iso!{ Complex64Base }

impl Domain for Complex64Base {}

impl DivisibilityRing for Complex64Base {

    fn checked_left_div(&self, lhs: &Self::Element, rhs: &Self::Element) -> Option<Self::Element> {
        let abs_sqr = self.abs(*rhs) * self.abs(*rhs);
        let Complex64El(res_re, res_im) =  self.mul(*lhs, self.conjugate(*rhs));
        return Some(Complex64El(res_re / abs_sqr, res_im / abs_sqr));
    }
}

impl PrincipalIdealRing for Complex64Base {

    fn extended_ideal_gen(&self, _lhs: &Self::Element, _rhs: &Self::Element) -> (Self::Element, Self::Element, Self::Element) {
        panic!("Since Complex64 is only approximate, this cannot be implemented properly")
    }
}

impl EuclideanRing for Complex64Base {

    fn euclidean_div_rem(&self, _lhs: Self::Element, _rhs: &Self::Element) -> (Self::Element, Self::Element) {
        panic!("Since Complex64 is only approximate, this cannot be implemented properly")
    }

    fn euclidean_deg(&self, _: &Self::Element) -> Option<usize> {
        panic!("Since Complex64 is only approximate, this cannot be implemented properly")
    }
}

impl Field for Complex64Base {
    
    fn div(&self, lhs: &Self::Element, rhs: &Self::Element) -> Self::Element {
        self.checked_left_div(lhs, rhs).unwrap()
    }
}

impl RingExtension for Complex64Base {

    type BaseRing = Real64;

    fn base_ring<'a>(&'a self) -> &'a Self::BaseRing {
        &Real64::RING
    }

    fn from(&self, x: El<Self::BaseRing>) -> Self::Element {
        self.from_f64(x)
    }

    fn mul_assign_base(&self, lhs: &mut Self::Element, rhs: &El<Self::BaseRing>) {
        lhs.0 *= *rhs;
        lhs.1 *= *rhs;
    }
}

#[test]
fn test_pow() {
    let CC = Complex64::RING;
    let i = Complex64::I;
    assert!(CC.is_approx_eq(CC.negate(i), CC.pow(i, 3), 1));
    assert!(!CC.is_approx_eq(CC.negate(i), CC.pow(i, 1024 + 3), 1));
    assert!(CC.is_approx_eq(CC.negate(i), CC.pow(i, 1024 + 3), 100));
    assert!(CC.is_approx_eq(CC.exp(CC.mul(CC.from_f64(std::f64::consts::PI / 4.), i)), CC.mul(CC.add(CC.one(), i), CC.from_f64(2f64.powf(-0.5))), 1));

    let seventh_root_of_unity = CC.exp(CC.mul(i, CC.from_f64(2. * std::f64::consts::PI / 7.)));
    assert!(CC.is_approx_eq(CC.pow(seventh_root_of_unity, 7 * 100 + 1), seventh_root_of_unity, 1000));
}

#[test]
fn test_mul() {
    let CC = Complex64::RING;
    let i = Complex64::I;
    assert!(CC.is_approx_eq(CC.mul(i, i), CC.from_f64(-1.), 1));
    assert!(CC.is_approx_eq(CC.mul(i, CC.negate(i)), CC.from_f64(1.), 1));
    assert!(CC.is_approx_eq(CC.mul(CC.add(i, CC.one()), i), CC.sub(i, CC.one()), 1));
}