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
use crate::core::integer::{FullInt, IntConvert};
use crate::core::undefined::*;
use crate::{
Circle, CircleConstants, ExponentConstants, FractionConstants, Integer, Scalar, ScalarConstants,
};
use i256::I256;
use num_traits::{AsPrimitive, WrappingAdd, WrappingMul, WrappingNeg, WrappingSub};
use core::ops::*;
#[allow(private_bounds)]
impl<
F: Integer
+ FractionConstants
+ FullInt
+ Shl<isize, Output = F>
+ Shr<isize, Output = F>
+ Shl<F, Output = F>
+ Shr<F, Output = F>
+ Shl<E, Output = F>
+ Shr<E, Output = F>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
E: Integer
+ ExponentConstants
+ FullInt
+ Shl<isize, Output = E>
+ Shr<isize, Output = E>
+ Shl<E, Output = E>
+ Shr<E, Output = E>
+ Shl<F, Output = E>
+ Shr<F, Output = E>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
> Circle<F, E>
where
Circle<F, E>: CircleConstants,
Scalar<F, E>: ScalarConstants,
u8: AsPrimitive<F>,
u16: AsPrimitive<F>,
u32: AsPrimitive<F>,
u64: AsPrimitive<F>,
u128: AsPrimitive<F>,
usize: AsPrimitive<F>,
i8: AsPrimitive<F>,
i16: AsPrimitive<F>,
i32: AsPrimitive<F>,
i64: AsPrimitive<F>,
i128: AsPrimitive<F>,
isize: AsPrimitive<F>,
I256: From<F>,
u8: AsPrimitive<E>,
u16: AsPrimitive<E>,
u32: AsPrimitive<E>,
u64: AsPrimitive<E>,
u128: AsPrimitive<E>,
usize: AsPrimitive<E>,
i8: AsPrimitive<E>,
i16: AsPrimitive<E>,
i32: AsPrimitive<E>,
i64: AsPrimitive<E>,
i128: AsPrimitive<E>,
isize: AsPrimitive<E>,
I256: From<E>,
{
/// Raises a complex number to a scalar power.
///
/// Implements z^s (complex raised to scalar power) using:
/// - Specialized algorithm for integer exponents
/// - General formula z^s = exp(s * ln(z)) for non-integer exponents
///
/// # Special Cases:
/// - Undefined or escaped values return appropriate undefined states
/// - 0^0 returns the ZERO_POWER_ZERO undefined state
/// - 0^s returns 0 if s is positive
/// - 0^s returns ZERO_NEGATIVE_POWER undefined state if s is negative
///
/// # Parameters:
/// - `self`: The complex base
/// - `exp`: The scalar exponent
///
/// # Returns:
/// - A complex number representing z^s
pub(crate) fn circle_power_scalar(&self, exp: &Scalar<F, E>) -> Self {
if !self.is_normal() || !exp.is_normal() {
if self.is_undefined() {
return *self;
}
if exp.is_undefined() {
return Self {
real: exp.fraction,
imaginary: exp.fraction,
exponent: exp.exponent,
};
}
if self.is_zero() {
// Special case: 0^0 = 1 by convention
if exp.is_zero() {
return Self::ONE;
}
if exp.fraction.is_positive() {
return Self::ZERO;
}
let prefix: F = NEGLIGIBLE_POWER.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if self.exploded() {
let prefix: F = TRANSFINITE_POWER.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if self.vanished() {
let prefix: F = NEGLIGIBLE_POWER.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if exp.exploded() {
let prefix: F = POWER_TRANSFINITE.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
let prefix: F = POWER_NEGLIGIBLE.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
// Check if exponent is integer for exact computation
if exp.is_integer() {
return self.integer_power(exp);
}
let ln_z = self.ln();
let s_ln_z = ln_z * exp;
s_ln_z.exp()
}
pub(crate) fn integer_power(&self, n: &Scalar<F, E>) -> Self {
if n.is_zero() {
return Self::ONE;
}
let mut result = Self::ONE;
let mut base = if n.is_negative() {
Self::ONE / *self
} else {
*self
};
let mut exp = n.magnitude();
while !exp.is_zero() {
if (exp & Scalar::<F, E>::ONE) == 1 {
result *= base;
}
base = base.square();
exp = (exp >> 1u8).floor();
}
result
}
/// Computes the logarithm of a complex number with a scalar base.
///
/// Implements c log s (logarithm of complex c with scalar base s) using:
/// c log s = ln(c) / ln(s)
///
/// # Special Cases:
/// - Undefined or escaped values return appropriate undefined states
/// - Exploded or vanished values return appropriate undefined states
/// - 0 log s returns ZERO_LOG undefined state
/// - c log 0 returns LOG_ZERO undefined state
/// - c log -# returns LOG_NEGATIVE undefined state
///
/// # Parameters:
/// - `self`: The complex number to take the logarithm of
/// - `base`: The scalar base of the logarithm
///
/// # Returns:
/// - A complex number representing c log s
pub(crate) fn circle_logarithm_scalar(&self, base: &Scalar<F, E>) -> Self {
if !self.is_normal() || !base.is_normal() {
if self.is_undefined() {
return *self;
}
if base.is_undefined() {
return Self {
real: base.fraction,
imaginary: 0.as_(),
exponent: base.exponent,
};
}
if self.is_zero() {
let prefix: F = NEGLIGIBLE_LOG.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if base.is_zero() {
let prefix: F = LOG_NEGLIGIBLE.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if self.exploded() {
let prefix: F = TRANSFINITE_LOG.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if self.vanished() {
let prefix: F = NEGLIGIBLE_LOG.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if base.exploded() {
let prefix: F = LOG_TRANSFINITE.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
if base.vanished() {
let prefix: F = LOG_NEGLIGIBLE.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
}
if base.fraction.is_negative() {
let prefix: F = LOG_NEGATIVE.prefix.sa();
return Self {
real: prefix,
imaginary: prefix,
exponent: E::AMBIGUOUS_EXPONENT,
};
}
let ln_z = self.ln();
let ln_base = base.ln();
ln_z / ln_base
}
}