Skip to content

Commit 4c847c5

Browse files
committed
removing extra variables from the chebyquad problem
1 parent bdfdc7f commit 4c847c5

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

src/ADNLPProblems/chebyquad.jl

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ function Cheby(xj, i)
1616
return (-1)^i * cosh(i * acosh(-xj))
1717
else
1818
# Clamp only for acos to guard against tiny floating-point/AD excursions.
19-
xj_clamped = min(max(xj, -1), 1)
20-
return cos(i * acos(xj_clamped))
19+
return cos(i * acos(min(max(xj, -1), 1)))
2120
end
2221
end
2322

@@ -31,20 +30,17 @@ function chebyquad(
3130
) where {T}
3231
m = max(m, n)
3332
function f(x; n = length(x), m = m, chebyshev = chebyshev)
34-
inv_n = one(T) / n
35-
half = one(T) / 2
36-
return half * sum(
37-
(inv_n * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1))^2 for
33+
return (one(T) / 2) * sum(
34+
((one(T) / n) * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1))^2 for
3835
i = 1:div(m, 2)
3936
) +
40-
half * sum(
41-
(inv_n * sum(chebyshev(x[j], 2i - 1) for j = 1:n))^2 for i = 1:div(m + 1, 2)
37+
(one(T) / 2) * sum(
38+
((one(T) / n) * sum(chebyshev(x[j], 2i - 1) for j = 1:n))^2 for i = 1:div(m + 1, 2)
4239
)
4340
end
44-
step = one(T) / (n + one(T))
4541
x0 = Vector{T}(undef, n)
4642
for j = 1:n
47-
x0[j] = j * step
43+
x0[j] = j * one(T) / (n + one(T))
4844
end
4945
return ADNLPModels.ADNLPModel(f, x0, name = "chebyquad"; kwargs...)
5046
end
@@ -59,20 +55,18 @@ function chebyquad(
5955
) where {T}
6056
m = max(m, n)
6157
function F!(r, x; n = length(x), m = length(r), chebyshev = chebyshev)
62-
inv_n = one(T) / n
6358
for i = 1:div(m, 2)
64-
r[2i] = inv_n * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1)
65-
r[2i - 1] = inv_n * sum(chebyshev(x[j], 2i - 1) for j = 1:n)
59+
r[2i] = (one(T) / n) * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1)
60+
r[2i - 1] = (one(T) / n) * sum(chebyshev(x[j], 2i - 1) for j = 1:n)
6661
end
6762
if mod(m, 2) == 1
68-
r[m] = inv_n * sum(chebyshev(x[j], m) for j = 1:n)
63+
r[m] = (one(T) / n) * sum(chebyshev(x[j], m) for j = 1:n)
6964
end
7065
return r
7166
end
72-
step = one(T) / (n + one(T))
7367
x0 = Vector{T}(undef, n)
7468
for j = 1:n
75-
x0[j] = j * step
69+
x0[j] = j * one(T) / (n + one(T))
7670
end
7771
return ADNLPModels.ADNLSModel!(F!, x0, m, name = "chebyquad-nls"; kwargs...)
7872
end

0 commit comments

Comments
 (0)