|
1 | 1 | # Compute the Jacobian matrix of a real-valued callable f.
|
2 | 2 | function finite_difference_jacobian(f, x::AbstractArray{<:Number},
|
3 |
| - fdtype::DataType=Val{:central}, funtype::DataType=Val{:Real}, wrappertype::DataType=Val{:Default}, |
4 |
| - fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x), |
5 |
| - df::Union{Void,AbstractArray{<:Number}}=nothing) |
| 3 | + fdtype::DataType=Val{:central}, funtype::DataType=Val{:Real}, |
| 4 | + wrappertype::DataType=Val{:Default}, |
| 5 | + fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x)) |
6 | 6 |
|
7 | 7 | J = zeros(returntype, length(x), length(x))
|
8 |
| - finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx, epsilon, returntype, df) |
| 8 | + finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx, |
| 9 | + epsilon, returntype) |
9 | 10 | end
|
10 | 11 |
|
11 |
| -function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, df::AbstractVector, f, x::AbstractArray{<:Number}, |
12 |
| - fdtype::DataType=Val{:central}, funtype::DataType=Val{:Real}, wrappertype::DataType=Val{:Default}, |
13 |
| - fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Number}}=nothing, returntype=eltype(x)) |
| 12 | +function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, |
| 13 | + x::AbstractArray{<:Number}, |
| 14 | + fdtype::DataType=Val{:central}, funtype::DataType=Val{:Real}, |
| 15 | + wrappertype::DataType=Val{:Default}, |
| 16 | + fx::Union{Void,AbstractArray{<:Number}}=nothing, |
| 17 | + epsilon::Union{Void,AbstractArray{<:Number}}=nothing, returntype=eltype(x)) |
14 | 18 |
|
15 |
| - finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx, epsilon, returntype, df) |
| 19 | + finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx, epsilon, returntype) |
16 | 20 | end
|
17 | 21 |
|
18 |
| -function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, x::AbstractArray{<:Number}, |
19 |
| - fdtype::DataType=Val{:central}, funtype::DataType=Val{:Real}, wrappertype::DataType=Val{:Default}, |
20 |
| - fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Number}}=nothing, returntype=eltype(x), |
21 |
| - df::Union{Void,AbstractArray{<:Number}}=nothing) |
22 |
| - |
23 |
| - finite_difference_jacobian!(J, f, x, fdtype, funtype, wrappertype, fx, epsilon, returntype, df) |
24 |
| -end |
25 |
| - |
26 |
| -function finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f, x::AbstractArray{<:Real}, |
| 22 | +function finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f, |
| 23 | + x::AbstractArray{<:Real}, |
27 | 24 | fdtype::DataType, ::Type{Val{:Real}}, ::Type{Val{:Default}},
|
28 |
| - fx::Union{Void,AbstractArray{<:Real}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x), |
29 |
| - df::Union{Void,AbstractArray{<:Real}}=nothing) |
| 25 | + fx::Union{Void,AbstractArray{<:Real}}=nothing, |
| 26 | + epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x)) |
30 | 27 |
|
31 | 28 | # TODO: test and rework this to support GPUArrays and non-indexable types, if possible
|
32 | 29 | m, n = size(J)
|
@@ -66,16 +63,13 @@ function finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f, x::AbstractAr
|
66 | 63 | else
|
67 | 64 | fdtype_error(Val{:Real})
|
68 | 65 | end
|
69 |
| - if typeof(df) != Void |
70 |
| - df .= diag(J) |
71 |
| - end |
72 | 66 | J
|
73 | 67 | end
|
74 | 68 |
|
75 |
| -function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, x::AbstractArray{<:Number}, |
| 69 | +function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, |
| 70 | + x::AbstractArray{<:Number}, |
76 | 71 | fdtype::DataType, ::Type{Val{:Complex}}, ::Type{Val{:Default}},
|
77 |
| - fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x), |
78 |
| - df::Union{Void,AbstractArray{<:Number}}=nothing) |
| 72 | + fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, returntype=eltype(x)) |
79 | 73 |
|
80 | 74 | # TODO: test and rework this to support GPUArrays and non-indexable types, if possible
|
81 | 75 | m, n = size(J)
|
@@ -107,9 +101,6 @@ function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, x::Abstract
|
107 | 101 | else
|
108 | 102 | fdtype_error(Val{:Complex})
|
109 | 103 | end
|
110 |
| - if typeof(df) != Void |
111 |
| - df .= diag(J) |
112 |
| - end |
113 | 104 | J
|
114 | 105 | end
|
115 | 106 |
|
|
0 commit comments