Skip to content

v0.4.1

Compare
Choose a tag to compare
@github-actions github-actions released this 19 Sep 18:24
· 21 commits to main since this release
bebc4dc

FastDifferentiation v0.4.1

Diff since v0.4.0

This is a patch to fix two bugs, one introduced in 0.3.17 and one introduced in 0.4.0 when conditionals were added as a feature. These bugs caused derivative(x^y) to fail. Other less commonly used functions were also affected. Neither 0.3.17 nor 0.4.0 should be used.

This release makes it possible to do two things:

Create an FD function that contains conditionals: f = if_else(x<y,x,y) where x,y are both FD variables.

Take derivatives of functions whose derivative definitions use conditionals. For example, the derivative definition for mod2pi is now

DiffRules.@define_diffrule Base.mod2pi(x) = :(if_else(isinteger($x / $DiffRules.twoπ), oftype(float($x), NaN), one(float($x))))
Example: derivative of mod2pi
julia> @variables x 
x

julia> f = mod2pi(x)
mod2pi(x)

julia> derivative([f],x)
1-element Vector{FastDifferentiation.Node}:
 (if_else  isinteger((x / twoπ)) NaN 1)

julia> g = derivative([f],x)
1-element Vector{FastDifferentiation.Node}:
 (if_else  isinteger((x / twoπ)) NaN 1)

julia> h = make_function(g,[x])
RuntimeGeneratedFunction(#=in FastDifferentiation=#, #=using FastDifferentiation=#, :((input_variables,)->begin
          #= c:\Users\seatt\source\FastDifferentiation.jl\src\CodeGeneration.jl:229 =#
          #= c:\Users\seatt\source\FastDifferentiation.jl\src\CodeGeneration.jl:229 =# @inbounds begin
                  #= c:\Users\seatt\source\FastDifferentiation.jl\src\CodeGeneration.jl:230 =#
                  begin
                      result_element_type = promote_type(Float64, eltype(input_variables))
                      result = Array{result_element_type}(undef, (1,))
                      var"##227" = input_variables[1] / twoπ
                      var"##226" = isinteger(var"##227")
                      var"##225" = if var"##226"
                              #= c:\Users\seatt\source\FastDifferentiation.jl\src\CodeGeneration.jl:58 =#
                              begin
                                  var"##s#228" = NaN
                              end
                          else
                              #= c:\Users\seatt\source\FastDifferentiation.jl\src\CodeGeneration.jl:60 =#
                              begin
                                  var"##s#229" = 1
                              end
                          end
                      result[1] = var"##225"
                      return result
                  end
              end
      end))

julia> h(2*pi)
1-element Vector{Float64}:
 NaN

You cannot (yet) take derivatives of a function which contains conditionals. This will come in a future PR.

julia> f = if_else(x<y,x,y)
(if_else  (x < y) x y)

julia> derivative([f],x)
ERROR: Your expression contained a if_else expression. FastDifferentiation does not yet support differentiation through this function)

Merged pull requests:

Closed issues:

  • Yank version 0.4.0? (#92)