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
//! Region 2 uniform asymptotic expansion for the I function.
//!
//! Translation of Fortran ZUNI2 from TOMS 644 / SLATEC (zbsubs.f lines 7045-7312).
//! Computes I(fnu,z) in the region |arg(z)| > pi/3 using the uniform
//! asymptotic expansion for J(fnu, z*exp(m*hpi*i)).
#![allow(clippy::too_many_arguments)]
use num_complex::Complex;
use crate::airy::zairy;
use crate::algo::uchk::zuchk;
use crate::algo::unhj::zunhj;
use crate::algo::uoik::zuoik;
use crate::machine::BesselFloat;
use crate::types::{Accuracy, AiryDerivative, IkFlag, Scaling, SumOption};
use crate::utils::{mul_add, mul_i, mul_neg_i, reciprocal_z, zabs};
use crate::algo::constants::{AIC, HPI};
/// CIP rotation table (Fortran lines 7077-7078)
/// CIPR + i*CIPI: {1, i, -1, -i}
const CIPR: [f64; 4] = [1.0, 0.0, -1.0, 0.0];
const CIPI: [f64; 4] = [0.0, 1.0, 0.0, -1.0];
/// Output of ZUNI2.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Uni2Output {
/// Underflow count (number of zeroed trailing members).
/// -1 indicates overflow.
pub nz: i32,
/// If nonzero, remaining items need a different method.
pub nlast: i32,
}
/// Compute I(fnu,z) via Region 2 uniform asymptotic expansion.
///
/// Equivalent to Fortran ZUNI2 in TOMS 644 (zbsubs.f lines 7045-7312).
pub(crate) fn zuni2<T: BesselFloat>(
z: Complex<T>,
fnu: T,
kode: Scaling,
y: &mut [Complex<T>],
fnul: T,
tol: T,
elim: T,
alim: T,
) -> Uni2Output {
let zero = T::zero();
let one = T::one();
let czero = Complex::new(zero, zero);
let n = y.len();
let mut nz: i32 = 0;
let mut nd = n;
let mut nlast: i32 = 0;
y.fill(czero);
// ── 3-level scaling (Fortran lines 7090-7097) ──
let cscl = one / tol;
let crsc = tol;
let cssr = [cscl, one, crsc];
let csrr = [crsc, one, cscl];
let bry0 = T::from_f64(1.0e3) * T::MACH_TINY / tol;
// ── Rotate z to right half plane (Fortran lines 7102-7106) ──
let zn = Complex::new(z.im.abs(), -z.re);
let zb = Complex::new(z.re, z.im.abs());
let cidi = if z.im <= zero { one } else { -one };
// Safety: fnu is finite and < ~1e15 per upper-interface checks
let inu = fnu.to_i32().unwrap() as usize;
let ang = T::from_f64(HPI) * (fnu - T::from_f64(inu as f64));
let c2_base = Complex::new(ang.cos(), ang.sin());
// IN = (INU + N - 1) mod 4 (0-based)
let in_idx = (inu + n - 1) % 4;
let mut c2 = c2_base * Complex::new(T::from_f64(CIPR[in_idx]), T::from_f64(CIPI[in_idx]));
if z.im <= zero {
c2 = c2.conj();
}
// ── Check first member for overflow/underflow (Fortran lines 7127-7144) ──
let fn_val = fnu.max(one);
let result0 = zunhj(zn, fn_val, SumOption::SkipSum, tol);
let rs1 = if kode == Scaling::Exponential {
let st = zb + result0.zeta2;
let rast = fn_val / zabs(st);
-result0.zeta1.re + st.re * rast * rast
} else {
-result0.zeta1.re + result0.zeta2.re
};
if rs1.abs() > elim {
if rs1 > zero {
return Uni2Output { nz: -1, nlast: 0 };
}
// All underflow
nz = n as i32;
y.fill(czero);
return Uni2Output { nz, nlast: 0 };
}
// ── Label 40: main computation loop (Fortran lines 7145-7263) ──
'label40: loop {
let nn = nd.min(2);
let mut iflag: usize = 2;
let mut cy = [czero; 2];
let mut computed_ok = true;
#[allow(clippy::needless_range_loop)]
for i in 0..nn {
let fn_val = fnu + T::from_f64((nd - 1 - i) as f64);
let result = zunhj(zn, fn_val, SumOption::Full, tol);
let s1_exp = if kode == Scaling::Exponential {
// KODE=2 path (Fortran lines 7151-7158)
// NOTE: s1i += |zi| (absolute value!), NOT zi like ZUNI1
let st = zb + result.zeta2;
let rast = fn_val / zabs(st);
let mut s = st.conj() * (rast * rast) - result.zeta1;
s.im = s.im + z.im.abs();
s
} else {
result.zeta2 - result.zeta1
};
// ── Overflow/underflow test (Fortran lines 7167-7203) ──
let mut rs1 = s1_exp.re;
if rs1.abs() > elim {
if rs1 > zero {
return Uni2Output { nz: -1, nlast: 0 };
}
// Underflow (Fortran label 120)
y[nd - 1] = czero;
nz += 1;
nd -= 1;
if nd == 0 {
return Uni2Output { nz, nlast: 0 };
}
let nuf = zuoik(z, fnu, kode, IkFlag::I, &mut y[..nd], tol, elim, alim);
if nuf < 0 {
return Uni2Output { nz: -1, nlast: 0 };
}
nd -= nuf as usize;
nz += nuf;
if nd == 0 {
return Uni2Output { nz, nlast: 0 };
}
let fn_check = fnu + T::from_f64((nd - 1) as f64);
if fn_check >= fnul {
// Recalculate C2 (Fortran lines 7292-7296)
let in_idx2 = (inu + nd - 1) % 4;
c2 = c2_base
* Complex::new(T::from_f64(CIPR[in_idx2]), T::from_f64(CIPI[in_idx2]));
if z.im <= zero {
c2 = c2.conj();
}
computed_ok = false;
break;
}
nlast = nd as i32;
return Uni2Output { nz, nlast };
}
if i == 0 {
iflag = 2;
}
if rs1.abs() >= alim {
// Refine test (Fortran lines 7175-7181)
let aphi = zabs(result.phi);
let aarg = zabs(result.arg);
rs1 = rs1 + aphi.ln() - T::from_f64(0.25) * aarg.ln() - T::from_f64(AIC);
if rs1.abs() > elim {
if rs1 > zero {
return Uni2Output { nz: -1, nlast: 0 };
}
y[nd - 1] = czero;
nz += 1;
nd -= 1;
if nd == 0 {
return Uni2Output { nz, nlast: 0 };
}
let nuf = zuoik(z, fnu, kode, IkFlag::I, &mut y[..nd], tol, elim, alim);
if nuf < 0 {
return Uni2Output { nz: -1, nlast: 0 };
}
nd -= nuf as usize;
nz += nuf;
if nd == 0 {
return Uni2Output { nz, nlast: 0 };
}
let fn_check = fnu + T::from_f64((nd - 1) as f64);
if fn_check >= fnul {
let in_idx2 = (inu + nd - 1) % 4;
c2 = c2_base
* Complex::new(T::from_f64(CIPR[in_idx2]), T::from_f64(CIPI[in_idx2]));
if z.im <= zero {
c2 = c2.conj();
}
computed_ok = false;
break;
}
nlast = nd as i32;
return Uni2Output { nz, nlast };
}
if i == 0 {
iflag = 1;
}
if rs1 < zero {
// iflag stays
} else if i == 0 {
iflag = 3;
}
}
// ── Scale S1 and compute S2 (Fortran lines 7187-7216) ──
// S2 = PHI * (Ai*ASUM + Ai'*BSUM)
let (ai, _nai, _) = zairy(result.arg, AiryDerivative::Value, Scaling::Exponential)
.unwrap_or((czero, 0, Accuracy::Normal));
let (dai, _ndai, _) = zairy(
result.arg,
AiryDerivative::Derivative,
Scaling::Exponential,
)
.unwrap_or((czero, 0, Accuracy::Normal));
let s2_airy = result.phi * mul_add(ai, result.asum, dai * result.bsum);
let s1_scaled = s1_exp.exp() * cssr[iflag - 1];
let mut s2_val = s2_airy * s1_scaled;
if iflag == 1 && zuchk(s2_val, bry0, tol) {
// Underflow (label 120)
y[nd - 1] = czero;
nz += 1;
nd -= 1;
if nd == 0 {
return Uni2Output { nz, nlast: 0 };
}
let nuf = zuoik(z, fnu, kode, IkFlag::I, &mut y[..nd], tol, elim, alim);
if nuf < 0 {
return Uni2Output { nz: -1, nlast: 0 };
}
nd -= nuf as usize;
nz += nuf;
if nd == 0 {
return Uni2Output { nz, nlast: 0 };
}
let fn_check = fnu + T::from_f64((nd - 1) as f64);
if fn_check >= fnul {
let in_idx2 = (inu + nd - 1) % 4;
c2 = c2_base
* Complex::new(T::from_f64(CIPR[in_idx2]), T::from_f64(CIPI[in_idx2]));
if z.im <= zero {
c2 = c2.conj();
}
computed_ok = false;
break;
}
nlast = nd as i32;
return Uni2Output { nz, nlast };
}
// Conjugation for ZI <= 0 (Fortran line 7205)
if z.im <= zero {
s2_val = s2_val.conj();
}
// Phase rotation by C2 (Fortran lines 7206-7208)
s2_val = s2_val * c2;
// Store (Fortran lines 7209-7213)
cy[i] = s2_val;
let j_idx = nd - 1 - i;
y[j_idx] = s2_val * csrr[iflag - 1];
// C2 *= i*CIDI (Fortran lines 7214-7216)
c2 = if cidi > zero {
mul_i(c2)
} else {
mul_neg_i(c2)
};
}
if !computed_ok {
continue 'label40;
}
// ── Forward recurrence for remaining terms (Fortran lines 7218-7263) ──
if nd <= 2 {
return Uni2Output { nz, nlast };
}
let rz = reciprocal_z(z);
let bry1 = one / bry0;
let bry2 = T::MACH_HUGE;
let mut s1 = cy[0];
let mut s2 = cy[1];
let mut c1r = csrr[iflag - 1];
let mut ascle = if iflag == 1 {
bry0
} else if iflag == 2 {
bry1
} else {
bry2
};
let mut k = nd - 2;
let mut fn_rec = T::from_f64(k as f64);
for _i in 2..nd {
let prev = s2;
let cfn = fnu + fn_rec;
s2 = s1 + rz * prev * cfn;
s1 = prev;
let c2_scaled = s2 * c1r;
y[k] = c2_scaled;
k -= 1;
fn_rec = fn_rec - one;
if iflag >= 3 {
continue;
}
let c2m = c2_scaled.re.abs().max(c2_scaled.im.abs());
if c2m <= ascle {
continue;
}
iflag += 1;
ascle = if iflag == 2 { bry1 } else { bry2 };
s1 = s1 * c1r;
s2 = c2_scaled;
s1 = s1 * cssr[iflag - 1];
s2 = s2 * cssr[iflag - 1];
c1r = csrr[iflag - 1];
}
return Uni2Output { nz, nlast };
}
}