-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathchebyquad.jl
More file actions
58 lines (54 loc) · 1.64 KB
/
chebyquad.jl
File metadata and controls
58 lines (54 loc) · 1.64 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
export chebyquad
function chebyquad(; use_nls::Bool = false, kwargs...)
model = use_nls ? :nls : :nlp
return chebyquad(Val(model); kwargs...)
end
function Cheby(xj::Ti, i) where {Ti}
return (xj ≥ 1) * cosh(i * acosh(max(abs(xj), 1))) + (xj ≤ -1) * (-one(Ti))^(i) * cosh(i * acosh(max(abs(xj), 1))) + (abs(xj) ≤ 1) * cos(i * acos(min(max(xj, -1), 1)))
end
function chebyquad(
::Val{:nlp};
n::Int = default_nvar,
m::Int = n,
type::Val{T} = Val(Float64),
chebyshev = Cheby,
kwargs...,
) where {T}
m = max(m, n)
function f(x; n = length(x), m = m, chebyshev = chebyshev)
return 1 // 2 * sum((1 // n * sum( chebyshev(x[j], 2i) for j=1:n) + 1 // ((2i)^2 - 1))^2 for i = 1:Int(round(m / 2))) + 1 // 2 * sum((1 // n * sum( chebyshev(x[j], 2i - 1) for j=1:n))^2 for i = 1:(Int(round(m / 2)) + mod(n, 2)))
end
x0 = T[j // (n + 1) for j=1:n]
return ADNLPModels.ADNLPModel(f, x0, name = "chebyquad"; kwargs...)
end
function chebyquad(
::Val{:nls};
n::Int = default_nvar,
m::Int = n,
type::Val{T} = Val(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:Int(round(m / 2))
r[2i] = 1 // n * sum(
(
chebyshev(x[j], 2i)
) for j=1:n) + 1 // ((2i)^2 - 1)
r[2i - 1] = 1 // n * sum(
(
chebyshev(x[j], 2i - 1)
) for j=1:n)
end
if mod(m, 2) == 1
r[m] = 1 // n * sum(
(
chebyshev(x[j], m)
) for j=1:n)
end
return r
end
x0 = T[j // (n + 1) for j=1:n]
return ADNLPModels.ADNLSModel!(F!, x0, m, name = "chebyquad-nls"; kwargs...)
end