File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,19 +5,22 @@ function chebyquad(; use_nls::Bool = false, kwargs...)
55 return chebyquad (Val (model); kwargs... )
66end
77
8- function Cheby (xj, i)
9- # Use standard Chebyshev definition with robust handling of tiny excursions:
10- # T_i(x) = cos(i * acos(x)) for |x| ≤ 1
11- # T_i(x) = cosh(i * acosh(x)) for x > 1
12- # T_i(x) = (-1)^i * cosh(i * acosh(-x)) for x < -1
13- if xj > 1
14- return cosh (i * acosh (xj))
15- elseif xj < - 1
16- return (- 1 )^ i * cosh (i * acosh (- xj))
17- else
18- # Clamp only for acos to guard against tiny floating-point/AD excursions.
19- return cos (i * acos (min (max (xj, - 1 ), 1 )))
20- end
8+ function Cheby (xj, i:: Integer )
9+ # Evaluate T_i(x) via recurrence to avoid domain-branching and AD/tracer issues.
10+ if i == 0
11+ return one (xj)
12+ elseif i == 1
13+ return xj
14+ end
15+
16+ tk_minus_1 = one (xj)
17+ tk = xj
18+ for _ = 2 : i
19+ tk_plus_1 = 2 * xj * tk - tk_minus_1
20+ tk_minus_1 = tk
21+ tk = tk_plus_1
22+ end
23+ return tk
2124end
2225
2326function chebyquad (
You can’t perform that action at this time.
0 commit comments