|
2 | 2 | function finite_difference_jacobian(f, x::AbstractArray{<:Number},
|
3 | 3 | fdtype::DataType=Val{:central}, funtype::DataType=Val{:Real},
|
4 | 4 | wrappertype::DataType=Val{:Default},
|
5 |
| - fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x)) |
6 |
| - |
| 5 | + fx::Union{Void,AbstractArray{<:Number}}=nothing, |
| 6 | + epsilon::Union{Void,AbstractArray{<:Number}}=nothing, returntype=eltype(x), |
| 7 | + inplace::DataType=Val{true}) |
7 | 8 | J = zeros(returntype, length(x), length(x))
|
8 | 9 | finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx,
|
9 |
| - epsilon, returntype) |
| 10 | + epsilon, returntype, inplace) |
10 | 11 | end
|
11 | 12 |
|
12 | 13 | function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f,
|
13 | 14 | x::AbstractArray{<:Number},
|
14 | 15 | fdtype::DataType=Val{:central}, funtype::DataType=Val{:Real},
|
15 | 16 | wrappertype::DataType=Val{:Default},
|
16 | 17 | fx::Union{Void,AbstractArray{<:Number}}=nothing,
|
17 |
| - epsilon::Union{Void,AbstractArray{<:Number}}=nothing, returntype=eltype(x)) |
| 18 | + epsilon::Union{Void,AbstractArray{<:Number}}=nothing, returntype=eltype(x), |
| 19 | + inplace::DataType=Val{true}) |
18 | 20 |
|
19 |
| - finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx, epsilon, returntype) |
| 21 | + _finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx, |
| 22 | + epsilon, returntype, inplace) |
20 | 23 | end
|
21 | 24 |
|
22 |
| -function finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f, |
| 25 | +function _finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f, |
23 | 26 | x::AbstractArray{<:Real},
|
24 | 27 | fdtype::DataType, ::Type{Val{:Real}}, ::Type{Val{:Default}},
|
25 |
| - fx::Union{Void,AbstractArray{<:Real}}=nothing, |
26 |
| - epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x)) |
| 28 | + fx, epsilon, returntype, inplace::Type{Val{true}}) |
| 29 | + |
| 30 | + # TODO: test and rework this to support GPUArrays and non-indexable types, if possible |
| 31 | + m, n = size(J) |
| 32 | + epsilon_elemtype = compute_epsilon_elemtype(epsilon, x) |
| 33 | + if fdtype == Val{:forward} |
| 34 | + if typeof(fx) == Void |
| 35 | + fx = similar(x,returntype) |
| 36 | + end |
| 37 | + # TODO: Remove these allocations |
| 38 | + fx2 = similar(x,returntype) |
| 39 | + shifted_x = copy(x) |
| 40 | + epsilon_factor = compute_epsilon_factor(Val{:forward}, epsilon_elemtype) |
| 41 | + f(fx,x) |
| 42 | + @inbounds for i in 1:n |
| 43 | + epsilon = compute_epsilon(Val{:forward}, x[i], epsilon_factor) |
| 44 | + shifted_x[i] += epsilon |
| 45 | + f(fx2,shifted_x) |
| 46 | + J[:, i] .= (fx2 - fx) / epsilon |
| 47 | + shifted_x[i] = x[i] |
| 48 | + end |
| 49 | + elseif fdtype == Val{:central} |
| 50 | + epsilon_factor = compute_epsilon_factor(Val{:central}, epsilon_elemtype) |
| 51 | + if typeof(fx) == Void |
| 52 | + fx1 = similar(x,returntype) |
| 53 | + else |
| 54 | + fx1 = fx |
| 55 | + end |
| 56 | + # TODO: Remove these allocations |
| 57 | + fx2 = similar(x,returntype) |
| 58 | + shifted_x_plus = copy(x) |
| 59 | + shifted_x_minus = copy(x) |
| 60 | + @inbounds for i in 1:n |
| 61 | + epsilon = compute_epsilon(Val{:central}, x[i], epsilon_factor) |
| 62 | + shifted_x_plus[i] += epsilon |
| 63 | + shifted_x_minus[i] -= epsilon |
| 64 | + f(fx1,shifted_x_plus) |
| 65 | + f(fx2,shifted_x_minus) |
| 66 | + J[:, i] .= (fx1 - fx2) / (epsilon + epsilon) |
| 67 | + shifted_x_plus[i] = x[i] |
| 68 | + shifted_x_minus[i] = x[i] |
| 69 | + end |
| 70 | + elseif fdtype == Val{:complex} |
| 71 | + x0 = Vector{Complex{eltype(x)}}(x) |
| 72 | + epsilon = eps(eltype(x)) |
| 73 | + fx1 = similar(x,Complex{eltype(x)}) |
| 74 | + @inbounds for i in 1:n |
| 75 | + x0[i] += im * epsilon |
| 76 | + f(fx1,x0) |
| 77 | + J[:,i] .= imag.(fx1) / epsilon |
| 78 | + x0[i] -= im * epsilon |
| 79 | + end |
| 80 | + else |
| 81 | + fdtype_error(Val{:Real}) |
| 82 | + end |
| 83 | + J |
| 84 | +end |
| 85 | + |
| 86 | +function _finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, |
| 87 | + x::AbstractArray{<:Number}, |
| 88 | + fdtype::DataType, ::Type{Val{:Complex}}, ::Type{Val{:Default}}, |
| 89 | + fx, epsilon, returntype, inplace::Type{Val{true}}) |
27 | 90 |
|
28 | 91 | # TODO: test and rework this to support GPUArrays and non-indexable types, if possible
|
29 | 92 | m, n = size(J)
|
30 | 93 | epsilon_elemtype = compute_epsilon_elemtype(epsilon, x)
|
31 | 94 | if fdtype == Val{:forward}
|
| 95 | + |
32 | 96 | if typeof(fx) == Void
|
33 |
| - fx = f(x) |
| 97 | + fx = similar(x,returntype) |
| 98 | + end |
| 99 | + # TODO: Remove these allocations |
| 100 | + fx2 = similar(x,returntype) |
| 101 | + shifted_x = copy(x) |
| 102 | + |
| 103 | + epsilon_factor = compute_epsilon_factor(Val{:forward}, epsilon_elemtype) |
| 104 | + f(fx,x) |
| 105 | + @inbounds for i in 1:n |
| 106 | + epsilon = compute_epsilon(Val{:forward}, real(x[i]), epsilon_factor) |
| 107 | + shifted_x[i] += epsilon |
| 108 | + f(fx2,shifted_x) |
| 109 | + @. J[:, i] = ( real(fx2 - fx ) + im*imag( fx2 - fx ) ) / epsilon |
| 110 | + shifted_x[i] = x[i] |
| 111 | + end |
| 112 | + elseif fdtype == Val{:central} |
| 113 | + epsilon_factor = compute_epsilon_factor(Val{:central}, epsilon_elemtype) |
| 114 | + |
| 115 | + if typeof(fx) == Void |
| 116 | + fx1 = similar(x,returntype) |
| 117 | + else |
| 118 | + fx1 = fx |
| 119 | + end |
| 120 | + # TODO: Remove these allocations |
| 121 | + fx2 = similar(x,returntype) |
| 122 | + shifted_x_plus = copy(x) |
| 123 | + shifted_x_minus = copy(x) |
| 124 | + |
| 125 | + @inbounds for i in 1:n |
| 126 | + epsilon = compute_epsilon(Val{:central}, real(x[i]), epsilon_factor) |
| 127 | + shifted_x_plus[i] += epsilon |
| 128 | + shifted_x_minus[i] -= epsilon |
| 129 | + f(fx1,shifted_x_plus) |
| 130 | + f(fx2,shifted_x_minus) |
| 131 | + @. J[:, i] = ( real(fx1 - fx2) + im*imag(fx1 - fx2) ) / (2 * epsilon) |
| 132 | + shifted_x_plus[i] = x[i] |
| 133 | + shifted_x_minus[i] = x[i] |
34 | 134 | end
|
| 135 | + else |
| 136 | + fdtype_error(Val{:Complex}) |
| 137 | + end |
| 138 | + J |
| 139 | +end |
| 140 | + |
| 141 | +function _finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f, |
| 142 | + x::AbstractArray{<:Real}, |
| 143 | + fdtype::DataType, ::Type{Val{:Real}}, ::Type{Val{:Default}}, |
| 144 | + fx, epsilon, returntype, inplace::Type{Val{false}}) |
| 145 | + |
| 146 | + # TODO: test and rework this to support GPUArrays and non-indexable types, if possible |
| 147 | + m, n = size(J) |
| 148 | + epsilon_elemtype = compute_epsilon_elemtype(epsilon, x) |
| 149 | + if fdtype == Val{:forward} |
| 150 | + fx = f(x) |
35 | 151 | epsilon_factor = compute_epsilon_factor(Val{:forward}, epsilon_elemtype)
|
36 | 152 | shifted_x = copy(x)
|
37 | 153 | @inbounds for i in 1:n
|
@@ -66,18 +182,16 @@ function finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f,
|
66 | 182 | J
|
67 | 183 | end
|
68 | 184 |
|
69 |
| -function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, |
| 185 | +function _finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, |
70 | 186 | x::AbstractArray{<:Number},
|
71 | 187 | fdtype::DataType, ::Type{Val{:Complex}}, ::Type{Val{:Default}},
|
72 |
| - fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x)) |
| 188 | + fx, epsilon, returntype, inplace::Type{Val{false}}) |
73 | 189 |
|
74 | 190 | # TODO: test and rework this to support GPUArrays and non-indexable types, if possible
|
75 | 191 | m, n = size(J)
|
76 | 192 | epsilon_elemtype = compute_epsilon_elemtype(epsilon, x)
|
77 | 193 | if fdtype == Val{:forward}
|
78 |
| - if typeof(fx) == Void |
79 |
| - fx = f(x) |
80 |
| - end |
| 194 | + fx = f(x) |
81 | 195 | epsilon_factor = compute_epsilon_factor(Val{:forward}, epsilon_elemtype)
|
82 | 196 | shifted_x = copy(x)
|
83 | 197 | @inbounds for i in 1:n
|
|
0 commit comments