talrost 0.1.2

A mathematics library for embedded scientific computation.
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
use crate::algebra::*;
use crate::element::Element;
use crate::float::Float;
use crate::integer::Integer;
use crate::natural::Natural;
use crate::{impl_field, impl_group, impl_ring, impl_semiring};
use core::fmt::Debug;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Complex<F>
where
    F: Float,
{
    pub re: F,
    pub im: F,
}

impl<F> Complex<F>
where
    F: Float,
{
    #[allow(dead_code)]
    const NAN: Self = Self {
        re: F::NAN,
        im: F::NAN,
    };

    #[allow(dead_code)]
    const INFINITY: Self = Self {
        re: F::INFINITY,
        im: F::INFINITY,
    };

    #[allow(dead_code)]
    const EPSILON: Self = Self {
        re: F::EPSILON,
        im: F::ZERO,
    };

    const ZERO: Self = Self {
        re: F::ZERO,
        im: F::ZERO,
    };

    const ONE: Self = Self {
        re: F::ONE,
        im: F::ZERO,
    };

    const MIN: Self = Self {
        re: F::MIN,
        im: F::MIN,
    };

    const MAX: Self = Self {
        re: F::MAX,
        im: F::MAX,
    };

    #[allow(dead_code)]
    #[allow(non_upper_case_globals)]
    const i: Self = Self {
        re: F::ZERO,
        im: F::ONE,
    };

    #[allow(dead_code)]
    const J: Self = Self::i;

    pub fn new(re: F, im: F) -> Self {
        Self { re, im }
    }

    pub fn magnitude(&self) -> F {
        (self.re.powi(2) + self.im.powi(2)).powi(2)
    }

    pub fn sqrt(self) -> Self {
        if self.re == F::ZERO && self.im == F::ZERO {
            Self::ZERO
        } else {
            let mdl = (self.re * self.re + self.im * self.im).sqrt();
            let arg = self.im.atan2(self.re);
            let sq_mdl = mdl.sqrt();
            let harg = arg / (F::ONE + F::ONE);
            let re = sq_mdl * harg.cos();
            let im = sq_mdl * harg.sin();
            Self { re, im }
        }
    }

    pub fn normalize(&self) -> Self {
        let mag = self.magnitude();
        Self {
            re: self.re / mag,
            im: self.im / mag,
        }
    }

    pub fn powi(&self, power: i32) -> Self {
        let mut result = Self::ONE;
        for _ in 0..power {
            result *= *self;
        }
        result
    }
}

macro_rules! impl_natural_for_complex {
    ($($base_type: ty),+) => {
        $(
            impl Natural for $base_type {
                const MIN: Self = Self::MIN;
                const MAX: Self = Self::MAX;
                const BITS: Self = Self::ZERO; // Totally wrong but leave it for now

                fn powi(&self, power: i32) -> Self {
                    Self::powi(self, power)
                }
            }
        )+
    };
}

macro_rules! stack_complex{
    ($(($type: ty, $basis: ty)),+) => {
        $(
            impl Element for $type {}
            impl_group!(($type, <$type>::ZERO));
            impl_semiring!(($type, <$type>::ONE));
            impl_ring!($type);
            impl_field!($type);

            impl_natural_for_complex!($type);
            impl Integer for $type {}

            impl Float for $type {
                const DIGITS: u32 = 0;
                const MANTISSA_DIGITS: u32 = 0;
                const RADIX: u32 = 0;
                const MIN_EXP: i32 = 0;
                const MAX_EXP: i32 = 0;
                const INFINITY: Self = Self::INFINITY;
                const NEG_INFINITY: Self = Self::INFINITY;
                const NAN: Self = Self::NAN;
                const EPSILON: Self = Self::EPSILON;

                fn abs(&self) -> Self {
                    todo!()
                }

                fn floor(&self) -> Self {
                    todo!()
                }
                fn ceil(&self) -> Self {
                    todo!()
                }


                fn sin(&self) -> Self {
                    todo!()
                }
                #[allow(unconditional_recursion)]
                fn cos(&self) -> Self {
                    todo!()
                }
                #[allow(unconditional_recursion)]
                fn tan(&self) -> Self {
                    todo!()
                }
                #[allow(unconditional_recursion)]
                fn atan2(&self, _other: Self) -> Self {
                   todo!()
                }
                fn sin_cos(&self) -> (Self, Self) {
                    todo!()
                }
                fn sqrt(&self) -> Self {
                    <$type>::sqrt(*self)
                }
                fn cbrt(&self) -> Self {
                    todo!()
                }
                fn mul_add(self, _a: Self, _b: Self) -> Self {
                    todo!()
                }


                fn copysign(self, _sign: Self) -> Self {
                    todo!()
                }
                fn is_nan(self) -> bool {
                    self.re.is_nan() || self.im.is_nan()
                }
                fn is_finite(self) -> bool {
                    self.re.is_finite() && self.im.is_finite()
                }
            }
        )+
    };
}
stack_complex!((c32, f32), (c64, f64));

// Implement core::fmt::Display for Complex<F>
impl<F> core::fmt::Display for Complex<F>
where
    F: Float,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.im < F::ZERO {
            write!(f, "{} - {}i", self.re, -self.im)
        } else {
            write!(f, "{} + {}i", self.re, self.im)
        }
    }
}

// Implement std::iter::Sum for Complex<F>
impl<F> std::iter::Sum for Complex<F>
where
    F: Float,
{
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::ZERO, |a, b| a + b)
    }
}

// Implement From for &str to Complex<F>
impl<F> From<&str> for Complex<F>
where
    F: Float + std::str::FromStr,
{
    fn from(s: &str) -> Self {
        let mut re = String::new();
        let mut im = String::new();
        let mut is_re = true;
        for c in s.chars() {
            match c {
                ' ' => continue,
                '+' => {
                    is_re = false;
                    continue;
                }
                'i' => break,
                _ => {
                    if is_re {
                        re.push(c);
                    } else {
                        im.push(c);
                    }
                }
            }
        }
        Self::new(
            re.parse::<F>().unwrap_or(F::ZERO),
            im.parse::<F>().unwrap_or(F::ZERO),
        )
    }
}

// Implement From for (F, F) to Complex<F>
impl<F> From<(F, F)> for Complex<F>
where
    F: Float,
{
    fn from(value: (F, F)) -> Self {
        Self::new(value.0, value.1)
    }
}

// Implement From for [F, F] to Complex<F>
impl<F> From<[F; 2]> for Complex<F>
where
    F: Float,
{
    fn from(value: [F; 2]) -> Self {
        Self::new(value[0], value[1])
    }
}

// // Implement Into for f32 to Complex<f32>
// impl Into<Complex<f32>> for f32 {
//     fn into(self) -> Complex<f32> {
//         Complex::new((self, 0.0))
//     }
// }

// // Implement Into for f64 to Complex<f64>
// impl Into<Complex<f64>> for f64 {
//     fn into(self) -> Complex<f64> {
//         Complex::new((self, 0.0))
//     }
// }

// Implement core::ops::Add for Complex<F>
impl<F> Add for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re + rhs.re,
            im: self.im + rhs.im,
        }
    }
}

// Implement core::ops::Add for Complex<F> where RHS is F
impl<F> Add<F> for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn add(self, rhs: F) -> Self::Output {
        Self {
            re: self.re + rhs,
            im: self.im,
        }
    }
}

// Implement core::ops::Sub for Complex<F>
impl<F> Sub for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re - rhs.re,
            im: self.im - rhs.im,
        }
    }
}

// Implement core::ops::Sub for Complex<F> where RHS is F
impl<F> Sub<F> for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn sub(self, rhs: F) -> Self::Output {
        Self {
            re: self.re - rhs,
            im: self.im,
        }
    }
}

// Implement core::ops::Mul for Complex<F>
impl<F> Mul for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re * rhs.re - self.im * rhs.im,
            im: self.re * rhs.im + self.im * rhs.re,
        }
    }
}

// Implement core::ops::Mul for Complex<F> where RHS is F
impl<F> Mul<F> for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn mul(self, rhs: F) -> Self::Output {
        Self {
            re: self.re * rhs,
            im: self.im * rhs,
        }
    }
}

// Implement core::ops::Div for Complex<F>
impl<F> Div for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        let denom = rhs.re.powi(2) + rhs.im.powi(2);
        Self {
            re: (self.re * rhs.re + self.im * rhs.im) / denom,
            im: (self.im * rhs.re - self.re * rhs.im) / denom,
        }
    }
}

// Implement core::ops::Div for Complex<F> where RHS is F
impl<F> Div<F> for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn div(self, rhs: F) -> Self::Output {
        Self {
            re: self.re / rhs,
            im: self.im / rhs,
        }
    }
}

// Implement core::ops::AddAssign for Complex<F>
impl<F> AddAssign for Complex<F>
where
    F: Float,
{
    fn add_assign(&mut self, rhs: Self) {
        self.re += rhs.re;
        self.im += rhs.im;
    }
}

// Implement core::ops::AddAssign for Complex<F> where RHS is F
impl<F> AddAssign<F> for Complex<F>
where
    F: Float,
{
    fn add_assign(&mut self, rhs: F) {
        self.re += rhs;
    }
}

// Implement core::ops::SubAssign for Complex<F>
impl<F> SubAssign for Complex<F>
where
    F: Float,
{
    fn sub_assign(&mut self, rhs: Self) {
        self.re -= rhs.re;
        self.im -= rhs.im;
    }
}

// Implement core::ops::SubAssign for Complex<F> where RHS is F
impl<F> SubAssign<F> for Complex<F>
where
    F: Float,
{
    fn sub_assign(&mut self, rhs: F) {
        self.re -= rhs;
    }
}

// Implement core::ops::MulAssign for Complex<F>
impl<F> MulAssign for Complex<F>
where
    F: Float,
{
    fn mul_assign(&mut self, rhs: Self) {
        let re = self.re * rhs.re - self.im * rhs.im;
        let im = self.re * rhs.im + self.im * rhs.re;
        self.re = re;
        self.im = im;
    }
}

// Implement core::ops::MulAssign for Complex<F> where RHS is F
impl<F> MulAssign<F> for Complex<F>
where
    F: Float,
{
    fn mul_assign(&mut self, rhs: F) {
        self.re *= rhs;
        self.im *= rhs;
    }
}

// Implement core::ops::DivAssign for Complex<F>
impl<F> DivAssign for Complex<F>
where
    F: Float,
{
    fn div_assign(&mut self, rhs: Self) {
        let denom = rhs.re * rhs.re + rhs.im * rhs.im;
        let re = (self.re * rhs.re + self.im * rhs.im) / denom;
        let im = (self.im * rhs.re - self.re * rhs.im) / denom;
        self.re = re;
        self.im = im;
    }
}

// Implement core::ops::DivAssign for Complex<F> where RHS is F
impl<F> DivAssign<F> for Complex<F>
where
    F: Float,
{
    fn div_assign(&mut self, rhs: F) {
        self.re /= rhs;
        self.im /= rhs;
    }
}

// Implement core::ops::Neg for Complex<F>
impl<F> Neg for Complex<F>
where
    F: Float,
{
    type Output = Self;
    fn neg(self) -> Self::Output {
        Self {
            re: -self.re,
            im: -self.im,
        }
    }
}

impl<F> From<F> for Complex<F>
where
    F: Float,
{
    fn from(value: F) -> Self {
        Self {
            re: value,
            im: F::ZERO,
        }
    }
}

#[allow(non_camel_case_types)]
pub type c64 = Complex<f64>;
#[allow(non_camel_case_types)]
pub type c32 = Complex<f32>;

// Implement core::ops::Add for Complex<f64> where Self is f64
impl Add<Complex<f64>> for f64 {
    type Output = Complex<f64>;
    fn add(self, rhs: Complex<f64>) -> Self::Output {
        Self::Output {
            re: self + rhs.re,
            im: rhs.im,
        }
    }
}

// Implement core::ops::Sub for Complex<f64> where Self is f64
impl Sub<Complex<f64>> for f64 {
    type Output = Complex<f64>;
    fn sub(self, rhs: Complex<f64>) -> Self::Output {
        Self::Output {
            re: self - rhs.re,
            im: -rhs.im,
        }
    }
}

// Implement core::ops::Mul for Complex<f64> where Self is f64
impl Mul<Complex<f64>> for f64 {
    type Output = Complex<f64>;
    fn mul(self, rhs: Complex<f64>) -> Self::Output {
        Self::Output {
            re: self * rhs.re,
            im: self * rhs.im,
        }
    }
}

// Implement core::ops::Div for Complex<f64> where Self is f64
impl Div<Complex<f64>> for f64 {
    type Output = Complex<f64>;
    fn div(self, rhs: Complex<f64>) -> Self::Output {
        let denom = rhs.re.powi(2) + rhs.im.powi(2);
        Self::Output {
            re: (self * rhs.re) / denom,
            im: -(self * rhs.im) / denom,
        }
    }
}

// Implement core::ops::Add for Complex<f32> where Self is f32
impl Add<Complex<f32>> for f32 {
    type Output = Complex<f32>;
    fn add(self, rhs: Complex<f32>) -> Self::Output {
        Self::Output {
            re: self + rhs.re,
            im: rhs.im,
        }
    }
}

// Implement core::ops::Sub for Complex<f32> where Self is f32
impl Sub<Complex<f32>> for f32 {
    type Output = Complex<f32>;
    fn sub(self, rhs: Complex<f32>) -> Self::Output {
        Self::Output {
            re: self - rhs.re,
            im: -rhs.im,
        }
    }
}

// Implement core::ops::Mul for Complex<f32> where Self is f32
impl Mul<Complex<f32>> for f32 {
    type Output = Complex<f32>;
    fn mul(self, rhs: Complex<f32>) -> Self::Output {
        Self::Output {
            re: self * rhs.re,
            im: self * rhs.im,
        }
    }
}

// Implement core::ops::Div for Complex<f32> where Self is f32
impl Div<Complex<f32>> for f32 {
    type Output = Complex<f32>;
    fn div(self, rhs: Complex<f32>) -> Self::Output {
        let denom = rhs.re.powi(2) + rhs.im.powi(2);
        Self::Output {
            re: (self * rhs.re) / denom,
            im: -(self * rhs.im) / denom,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_constants() {
        // non-finite numbers are a bit of an issue since they are Self (which we don't want to change without having to rework the Float trait (might be okay with GATs?)).  Should probably impl and use is_nan() etc.
        // assert_eq!(c64::NAN, c64::new(f64::NAN, f64::NAN));
        // assert_eq!(c64::INFINITY, c64::new(f64::INFINITY, f64::INFINITY));
        // assert_eq!(
        //     c64::NEG_INFINITY,
        //     c64::new(f64::NEG_INFINITY, f64::NEG_INFINITY)
        // );
        assert_eq!(c64::ZERO, c64::new(0.0, 0.0));
        assert_eq!(c64::ONE, c64::new(1.0, 0.0));
        assert_eq!(c64::i, c64::new(0.0, 1.0));
        assert_eq!(c64::J, c64::new(0.0, 1.0));
    }

    #[test]
    fn test_c32_addition() {
        let mut a: f32 = 0.25;
        let mut b: c32 = [0.75, 0.66].into();

        assert_eq!(a + a, 0.5);
        assert_eq!(a + b, [1.0, 0.66].into());
        assert_eq!(b + a, [1.0, 0.66].into());
        assert_eq!(b + b, [1.5, 1.32].into());

        a += a;
        assert_eq!(a, 0.5);

        b += a;
        assert_eq!(b, [1.25, 0.66].into());

        b += b;
        assert_eq!(b, [2.5, 1.32].into());
    }

    #[test]
    fn test_c64_addition() {
        let mut a: f64 = 0.25;
        let mut b: c64 = [0.75, 0.66].into();

        assert_eq!(a + a, 0.5);
        assert_eq!(a + b, [1.0, 0.66].into());
        assert_eq!(b + a, [1.0, 0.66].into());
        assert_eq!(b + b, [1.5, 1.32].into());

        a += a;
        assert_eq!(a, 0.5);

        b += a;
        assert_eq!(b, [1.25, 0.66].into());

        b += b;
        assert_eq!(b, [2.5, 1.32].into());
    }

    #[test]
    fn test_c32_subtraction() {
        let mut a: f32 = 0.25;
        let mut b: c32 = [0.75, 0.66].into();

        assert_eq!(a - a, 0.0);
        assert_eq!(a - b, [-0.5, -0.66].into());
        assert_eq!(b - a, [0.5, 0.66].into());
        assert_eq!(b - b, [0.0, 0.0].into());

        a -= 0.5 * a;
        assert_eq!(a, 0.125);

        b -= a;
        assert_eq!(b, [0.625, 0.66].into());

        b -= b;
        assert_eq!(b, [0.0, 0.0].into());
    }

    #[test]
    fn test_c64_subtraction() {
        let mut a: f64 = 0.25;
        let mut b: c64 = [0.75, 0.66].into();

        assert_eq!(a - a, 0.0);
        assert_eq!(a - b, [-0.5, -0.66].into());
        assert_eq!(b - a, [0.5, 0.66].into());
        assert_eq!(b - b, [0.0, 0.0].into());

        a -= 0.5 * a;
        assert_eq!(a, 0.125);

        b -= a;
        assert_eq!(b, [0.625, 0.66].into());

        b -= b;
        assert_eq!(b, [0.0, 0.0].into());
    }

    #[test]
    fn test_c32_multiplication() {
        let mut a: f32 = 3.0;
        let mut b: c32 = [7.0, 13.0].into();

        assert_eq!(a * a, 9.0);
        assert_eq!(a * b, [21.0, 39.0].into());
        assert_eq!(b * a, [21.0, 39.0].into());
        assert_eq!(b * b, [-120.0, 182.0].into());

        a *= a;
        assert_eq!(a, 9.0);

        b *= a;
        assert_eq!(b, [63.0, 117.0].into());

        b *= b;
        assert_eq!(b, [-9720.0, 14742.0].into());
    }

    #[test]
    fn test_c64_multiplication() {
        let mut a: f64 = 3.0;
        let mut b: c64 = [7.0, 13.0].into();

        assert_eq!(a * a, 9.0);
        assert_eq!(a * b, [21.0, 39.0].into());
        assert_eq!(b * a, [21.0, 39.0].into());
        assert_eq!(b * b, [-120.0, 182.0].into());

        a *= a;
        assert_eq!(a, 9.0);

        b *= a;
        assert_eq!(b, [63.0, 117.0].into());

        b *= b;
        assert_eq!(b, [-9720.0, 14742.0].into());
    }

    #[test]
    fn test_c32_division() {
        let mut a: f32 = 24.0;
        let mut b: c32 = [12.0, 240.0].into();

        assert_eq!(a / a, 1.0);
        assert_eq!(a / b, [2.0 / 401.0, -40.0 / 401.0].into());
        assert_eq!(b / a, [0.5, 10.0].into());
        assert_eq!(b / b, [1.0, 0.0].into());

        a /= 0.5 * a;
        assert_eq!(a, 2.0);

        b /= a;
        assert_eq!(b, [6.0, 120.0].into());

        b /= b;
        assert_eq!(b, [1.0, 0.0].into());
    }

    #[test]
    fn test_c32_negation() {
        let a: c32 = [3.0, 7.0].into();
        let b: c32 = [-3.0, -7.0].into();

        assert_eq!(-a, [-3.0, -7.0].into());
        assert_eq!(-b, [3.0, 7.0].into());
    }

    #[test]
    fn test_c32_powi() {
        let a: c32 = [3.0, 7.0].into();

        assert_eq!(a.powi(0), [1.0, 0.0].into());
        assert_eq!(a.powi(1), [3.0, 7.0].into());
        assert_eq!(a.powi(2), [-40.0, 42.0].into());
        assert_eq!(a.powi(3), [-414.0, -154.0].into());
        assert_eq!(a.powi(4), [-164.0, -3360.0].into());

        let b: c32 = [0.0, 0.0].into();
        assert_eq!(b.powi(0), [1.0, 0.0].into());
        assert_eq!(b.powi(1), [0.0, 0.0].into());
        assert_eq!(b.powi(2), [0.0, 0.0].into());
        assert_eq!(b.powi(3), [0.0, 0.0].into());

        let c: c32 = [0.0, 1.0].into();
        assert_eq!(c.powi(0), [1.0, 0.0].into());
        assert_eq!(c.powi(1), [0.0, 1.0].into());
        assert_eq!(c.powi(2), [-1.0, 0.0].into());
        assert_eq!(c.powi(3), [0.0, -1.0].into());
        assert_eq!(c.powi(4), [1.0, 0.0].into());

        let d: c32 = [-11.0, -47.0].into();
        assert_eq!(d.powi(0), [1.0, 0.0].into());
        assert_eq!(d.powi(1), [-11.0, -47.0].into());
        assert_eq!(d.powi(2), [-2088.0, 1034.0].into());
        assert_eq!(d.powi(3), [71566.0, 86762.0].into());
        assert_eq!(d.powi(4), [3290588.0, -4317984.0].into());
    }

    #[test]
    fn test_f32_sqrt() {
        let a: f32 = 2.0;
        assert_eq!(a.sqrt(), 2_f32.sqrt());

        let b: f32 = 0.0;
        assert_eq!(b.sqrt(), 0_f32.sqrt());

        let c: f32 = -2.0;
        assert_eq!(c.sqrt().is_nan(), true);

        let d: f32 = 1.0;
        assert_eq!(d.sqrt(), 1_f32.sqrt());
    }

    #[test]
    fn test_c32_sqrt() {
        let a: c32 = [0.0, 0.0].into();
        assert_eq!(a.sqrt(), [0.0, 0.0].into());

        let b: c32 = [1.0, 0.0].into();
        assert_eq!(b.sqrt(), [1.0, 0.0].into());

        let c: c32 = [0.0, 1.0].into();
        assert_eq!(c.sqrt(), [0.7071067811865476, 0.7071067811865476].into());

        let d: c32 = [1.0, 1.0].into();
        assert_eq!(d.sqrt(), [1.09868411346781, 0.45508986056222733].into());

        let e: c32 = [1.0, -1.0].into();
        assert_eq!(e.sqrt(), [1.09868411346781, -0.45508986056222733].into());
    }
}