Skip to main content

oxilean_std/complex/
complex_powi_group.rs

1//! # Complex - powi_group Methods
2//!
3//! This module contains method implementations for `Complex`.
4//!
5//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
6
7use super::complex_type::Complex;
8
9impl Complex {
10    /// Complex power z^n for integer n (fast exponentiation).
11    pub fn powi(self, n: i32) -> Self {
12        if n == 0 {
13            return Self::one();
14        }
15        if n < 0 {
16            let inv = self.div(Self::one()).unwrap_or(Self::zero());
17            return inv.powi(-n);
18        }
19        let mut result = Self::one();
20        let mut base = self;
21        let mut exp = n as u32;
22        while exp > 0 {
23            if exp % 2 == 1 {
24                result = result.mul(base);
25            }
26            base = base.mul(base);
27            exp /= 2;
28        }
29        result
30    }
31}