Files
cmdla/10-12/oneDTestFunctions.jl

65 lines
2.3 KiB
Julia
Raw Normal View History

2023-10-29 02:06:02 +01:00
using LinearAlgebra
function custom(x::Union{Nothing, Number})::Tuple{Number, Number, Number, Union{Nothing, Tuple{Number, Number}}}
# custom 10-th degree polynomial
# f = @(x) 91 * x^2 / 30 - 19 * x^3 / 6 - 54 * x^4 / 25 ...
# + 93 * x^5 / 23 - 23 * x^6 / 36 - 121 * x^7 / 93 ...
# + 72 * x^8 / 91 - 13 * x^9 / 74 + 9 * x^10 / 640
#
# f'(x)
# (9*x^9)/64 - (117*x^8)/74 + (576*x^7)/91 - (847*x^6)/93
# - (23*x^5)/6 + (465*x^4)/23 - (216*x^3)/25 - (19*x^2)/2 + (91*x)/15
#
# f''(x)
# (81*x^8)/64 - (468*x^7)/37 + (576*x^6)/13 - (1694*x^5)/31
# - (115*x^4)/6 + (1860*x^3)/23 - (648*x^2)/25 - 19*x + 91/15
if x == nothing # informative call
return (0, 0, 0, (-0.5, 3))
else
f(x) = 91 * x^2 / 30 - 19 * x^3 / 6 - 54 * x^4 / 25 +
93 * x^5 / 23 - 23 * x^6 / 36 - 121 * x^7 / 93 +
72 * x^8 / 91 - 13 * x^9 / 74 + 9 * x^10 / 640
Df(x) = 91 * x / 15 - 19 * x^2 / 2 - 216 * x^3 / 25 + 465 * x^4 / 23 -
23 * x^5 / 6 - 847 * x^6 / 93 + 576 * x^7 / 91 -
117 * x^8 / 74 + 9 * x^9 / 64
DDf(x) = 91 / 15 - 19 * x - 648 * x^2 / 25 + 1860 * x^3 / 23 -
115 * x^4 / 6 - 1694 * x^5 / 31 + 576 * x^6 / 13 -
468 * x^7 / 37 + 81 * x^8 / 64
return (f(x), Df(x), DDf(x), nothing)
end
end # custom
function genericquad(Q::Matrix, q::Vector, x::Nothing)::Tuple{Number, Nothing, Nothing, Tuple{Vector, Vector}}
# generic quadratic function f(x) = x' * Q * x / 2 + q' * x
# informative call
if minimum(eigvals(Q)) > 1e-14
xStar = Q \ -q
v = 0.5 * dot(xStar', Q, xStar) + dot(q', xStar)
else
v = -Inf
end
return (v, nothing, nothing, ([-0.5, -0.5], [3, 3]))
end # genericquad
function genericquad(Q::Matrix, q::Vector, x::Vector)::Tuple{Number, Vector, Matrix, Nothing}
# generic quadratic function f(x) = x' * Q * x / 2 + q' * x
if !(size(x) == (2,))
throw(ArgumentError("genericquad: x is of wrong size"))
end
v = 0.5 * dot(x', Q, x) + dot(q', x) # f(x)
v2 = Q * x + q # \nabla f(x)
v3 = Q # \nabla^2 f(x)
return (v, v2, v3, nothing)
end # genericquad
function oneDTestFunctions()::Array{Any}
return [custom, x -> genericquad([6 -2;-2 6], [10; 5], x)]
end