-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathchebyquad.jl
More file actions
76 lines (70 loc) · 1.82 KB
/
chebyquad.jl
File metadata and controls
76 lines (70 loc) · 1.82 KB
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
export chebyquad
function chebyquad(; use_nls::Bool = false, kwargs...)
model = use_nls ? :nls : :nlp
return chebyquad(Val(model); kwargs...)
end
function Cheby(xj, i::Integer)
# Evaluate T_i(x) via recurrence to avoid domain-branching and AD/tracer issues.
if i == 0
return one(xj)
elseif i == 1
return xj
end
tk_minus_1 = one(xj)
tk = xj
for _ = 2:i
tk_plus_1 = 2 * xj * tk - tk_minus_1
tk_minus_1 = tk
tk = tk_plus_1
end
return tk
end
function chebyquad(
::Val{:nlp};
n::Int = default_nvar,
m::Int = n,
type::Type{T} = Float64,
chebyshev = Cheby,
kwargs...,
) where {T}
m = max(m, n)
function f(x; n = length(x), m = m, chebyshev = chebyshev)
return (one(T) / 2) * sum(
((one(T) / n) * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1))^2 for
i = 1:div(m, 2)
) +
(one(T) / 2) * sum(
((one(T) / n) * sum(chebyshev(x[j], 2i - 1) for j = 1:n))^2 for i = 1:div(m + 1, 2)
)
end
x0 = Vector{T}(undef, n)
for j = 1:n
x0[j] = j * one(T) / (n + one(T))
end
return ADNLPModels.ADNLPModel(f, x0, name = "chebyquad"; kwargs...)
end
function chebyquad(
::Val{:nls};
n::Int = default_nvar,
m::Int = n,
type::Type{T} = Float64,
chebyshev = Cheby,
kwargs...,
) where {T}
m = max(m, n)
function F!(r, x; n = length(x), m = length(r), chebyshev = chebyshev)
for i = 1:div(m, 2)
r[2i] = (one(T) / n) * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1)
r[2i - 1] = (one(T) / n) * sum(chebyshev(x[j], 2i - 1) for j = 1:n)
end
if mod(m, 2) == 1
r[m] = (one(T) / n) * sum(chebyshev(x[j], m) for j = 1:n)
end
return r
end
x0 = Vector{T}(undef, n)
for j = 1:n
x0[j] = j * one(T) / (n + one(T))
end
return ADNLPModels.ADNLSModel!(F!, x0, m, name = "chebyquad-nls"; kwargs...)
end