Skip to content

Commit 2c3a49e

Browse files
committed
adding changes to chebyquad.jl
1 parent 4c847c5 commit 2c3a49e

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

src/ADNLPProblems/chebyquad.jl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ function chebyquad(; use_nls::Bool = false, kwargs...)
55
return chebyquad(Val(model); kwargs...)
66
end
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
2124
end
2225

2326
function chebyquad(

0 commit comments

Comments
 (0)