31 lines
367 B
Julia
31 lines
367 B
Julia
using Zygote
|
|
using LinearAlgebra: norm
|
|
|
|
g(x::Vector{<:Real}) = 2x'x
|
|
|
|
v = [1, 3, 6]
|
|
@assert g'(v) == 4*v
|
|
|
|
function f(x)
|
|
if x > 0
|
|
return 30x
|
|
else
|
|
return 50x
|
|
end
|
|
end
|
|
|
|
@assert f'(3) == 30
|
|
@assert f'(-1) == 50
|
|
|
|
function affine(A, b)
|
|
return (x) -> A*x + b
|
|
end
|
|
|
|
A = [1 2; 3 4]
|
|
b = [30, 30]
|
|
|
|
h = affine(A, b)
|
|
|
|
@assert jacobian(h, [1,2]) == (A,)
|
|
|