Skip to content

Commit 85cda89

Browse files
committed
More robust derivative/gradient/jacobian
1 parent f033b0c commit 85cda89

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/direct.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A helper function for computing the derivative of an expression with respect to
88
"""
99
function derivative(O, v; simplify = true)
1010
if O isa AbstractArray
11-
Num[expand_derivatives(Differential(v)(value(o)), simplify) for o in O]
11+
Num[Num(expand_derivatives(Differential(v)(value(o)), simplify)) for o in O]
1212
else
1313
Num(expand_derivatives(Differential(v)(value(O)), simplify))
1414
end
@@ -23,7 +23,7 @@ A helper function for computing the gradient of an expression with respect to
2323
an array of variable expressions.
2424
"""
2525
function gradient(O, vars::AbstractVector; simplify = true)
26-
Num[expand_derivatives(Differential(v)(value(O)),simplify) for v in vars]
26+
Num[Num(expand_derivatives(Differential(v)(value(O)),simplify)) for v in vars]
2727
end
2828

2929
"""
@@ -35,7 +35,7 @@ A helper function for computing the Jacobian of an array of expressions with res
3535
an array of variable expressions.
3636
"""
3737
function jacobian(ops::AbstractVector, vars::AbstractVector; simplify = true)
38-
Num[expand_derivatives(Differential(value(v))(value(O)),simplify) for O in ops, v in vars]
38+
Num[Num(expand_derivatives(Differential(value(v))(value(O)),simplify)) for O in ops, v in vars]
3939
end
4040

4141
"""
@@ -57,7 +57,7 @@ function sparsejacobian(ops::AbstractVector, vars::AbstractVector; simplify = tr
5757
exprs = Num[]
5858

5959
for (i,j) in zip(I, J)
60-
push!(exprs, expand_derivatives(Differential(vars[j])(ops[i]), simplify))
60+
push!(exprs, Num(expand_derivatives(Differential(vars[j])(ops[i]), simplify)))
6161
end
6262
sparse(I,J, exprs, length(ops), length(vars))
6363
end

0 commit comments

Comments
 (0)