Skip to content

Commit

Permalink
fixed bug in DerivativeGraph constructor. If function had no variable…
Browse files Browse the repository at this point in the history
…s it would throw an exception.
  • Loading branch information
brianguenter committed Nov 22, 2023
1 parent d2187ee commit 78d075e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/DerivativeGraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ struct DerivativeGraph{T<:Integer}

edges = partial_edges(roots, postorder_number, expression_cache, length(var_array), length(roots))

@assert length(var_array) != 0 "No variables in your function. Your function must have at least one variable."

sort!(var_array, by=x -> postorder_number[x]) #sort by postorder number from lowest to highest

root_index_to_postorder_number = Vector{index_type}(undef, length(roots)) #roots are handled differently than variables because variable node can only occur once in list of variables but root node can occur multiple times in list of roots
Expand All @@ -188,7 +186,12 @@ struct DerivativeGraph{T<:Integer}
root_postorder_to_index[postorder_number] = i
end

variable_index_to_postorder_number = [postorder_number[x] for x in var_array]
if length(var_array) == 0
variable_index_to_postorder_number = index_type[] #special case when have no variables
else
variable_index_to_postorder_number = [postorder_number[x] for x in var_array]
end

variable_postorder_to_index = IdDict{index_type,index_type}()
for (i, postorder_number) in pairs(variable_index_to_postorder_number)
variable_postorder_to_index[postorder_number] = i
Expand Down
11 changes: 11 additions & 0 deletions test/FDTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1789,3 +1789,14 @@ end
end
end

@testitem "no variables in function" begin
@variables x y

f = x + y
h = hessian(f, [x, y])
hexe = make_function(h, [x, y])

@test isapprox(hexe([1, 1]), [0 0; 0 0])
end


0 comments on commit 78d075e

Please sign in to comment.