Skip to content

Commit

Permalink
Implement 2-arg atan (#91)
Browse files Browse the repository at this point in the history
* Implement atan

* Test atan

* Fix implementation

* Test mixed real cases

* Increment version number

* Increment version number
  • Loading branch information
sethaxen authored Mar 23, 2022
1 parent 1036d45 commit b54f132
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DualNumbers"
uuid = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74"
version = "0.6.7"
version = "0.6.8"

[deps]
Calculus = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
Expand Down
13 changes: 13 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,17 @@ Base.exp10(x::Dual) = (y = exp10(value(x)); Dual(y, y * log(10) * epsilon(x)))
Base.sinpi(z::Dual) = Dual(sinpi(value(z)),epsilon(z)*cospi(value(z))*π)
Base.cospi(z::Dual) = Dual(cospi(value(z)),-epsilon(z)*sinpi(value(z))*π)

function Base.atan(y::Dual, x::Dual)
u = value(x)^2 + value(y)^2
return dual(atan(value(y), value(x)), (value(x)/u) * epsilon(y) - (value(y)/u) * epsilon(x))
end
function Base.atan(y::Dual, x::Real)
u = x^2 + value(y)^2
return Dual(atan(value(y), x), (x/u) * epsilon(y))
end
function Base.atan(y::Real, x::Dual)
u = value(x)^2 + y^2
return Dual(atan(y, value(x)), (y/u) * -epsilon(x))
end

Base.checkindex(::Type{Bool}, inds::AbstractUnitRange, i::Dual) = checkindex(Bool, inds, value(i))
13 changes: 13 additions & 0 deletions test/automatic_differentiation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ function squareroot(x)
return it
end

@testset "atan consistency" begin
x = dual(randn(2)...)
y = dual(randn(2)...)
@test value(atan(y, x)) atan(value(y), value(x))
@test value(atan(y / x)) atan(value(y) / value(x))
@test epsilon(atan(y, x)) epsilon(atan(y / x))

@test value(atan(y, value(x))) atan(value(y), value(x))
@test epsilon(atan(y, value(x))) epsilon(atan(y, dual(value(x))))
@test value(atan(value(y), x)) atan(value(y), value(x))
@test epsilon(atan(value(y), x)) epsilon(atan(dual(value(y)), x))
end

@test epsilon(squareroot(Dual(10000.0,1.0))) 0.005

@test epsilon(exp(1)^Dual(1.0,1.0)) exp(1)
Expand Down

2 comments on commit b54f132

@sethaxen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/57180

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.8 -m "<description of version>" b54f132ea3274754f51b8a5f170349eb83a070ac
git push origin v0.6.8

Please sign in to comment.