-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from dingraha/cs_safe
Add complex-step safe versions of a few functions
- Loading branch information
Showing
8 changed files
with
223 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,105 @@ | ||
function abs_cs_safe(x::T) where {T<:Complex} | ||
using LinearAlgebra: norm, dot | ||
|
||
|
||
""" | ||
abs_cs_safe(x) | ||
Calculate the absolute value of `x` in a manner compatible with the complex-step derivative approximation. | ||
See also: [`abs`](@ref). | ||
""" | ||
abs_cs_safe | ||
|
||
@inline function abs_cs_safe(x::T) where {T<:Complex} | ||
return x*sign(real(x)) | ||
end | ||
|
||
function abs_cs_safe(x) | ||
@inline function abs_cs_safe(x) | ||
return abs(x) | ||
end | ||
|
||
""" | ||
abs2_cs_safe(x) | ||
Calculate the squared absolute value of `x` in a manner compatible with the complex-step derivative approximation. | ||
See also: [`abs2`](@ref). | ||
""" | ||
abs2_cs_safe | ||
|
||
@inline function abs2_cs_safe(x::T) where {T<:Complex} | ||
return abs_cs_safe(x)^2 | ||
end | ||
|
||
@inline function abs2_cs_safe(x) | ||
return abs2(x) | ||
end | ||
|
||
""" | ||
norm_cs_safe(x, p) | ||
Calculate the `p`-norm value of iterable `x` in a manner compatible with the complex-step derivative approximation. | ||
See also: [`norm`](@ref). | ||
""" | ||
norm_cs_safe | ||
|
||
@inline function norm_cs_safe(x::AbstractArray{T}, p::Real=2) where {T<:Complex} | ||
return sum(x.^p)^(1/p) | ||
end | ||
|
||
@inline function norm_cs_safe(x, p::Real=2) | ||
return norm(x, p) | ||
end | ||
|
||
""" | ||
dot_cs_safe(a, b) | ||
Calculate the dot product of vectors `a` and `b` in a manner compatible with the complex-step derivative approximation. | ||
See also: [`norm`](@ref). | ||
""" | ||
dot_cs_safe | ||
|
||
@inline function dot_cs_safe(a::AbstractVector{T}, b) where {T<:Complex} | ||
# `dot` conjugates its first argument, so we only need to worry about the case where the first argument is complex. | ||
# return sum(a.*b) | ||
return dot(conj.(a), b) | ||
end | ||
|
||
@inline function dot_cs_safe(a, b) | ||
return dot(a, b) | ||
end | ||
|
||
|
||
""" | ||
atan_cs_safe(y, x) | ||
Calculate the two-argument arctangent function in a manner compatible with the complex-step derivative approximation. | ||
See also: [`atan`](@ref). | ||
""" | ||
atan_cs_safe | ||
|
||
@inline function atan_cs_safe(y, x) | ||
return atan_cs_safe(promote(y, x)...) | ||
end | ||
|
||
@inline function atan_cs_safe(y::T, x::T) where {T<:Complex} | ||
# Stolen from openmdao/utils/cs_safe.py | ||
# a = np.real(y) | ||
# b = np.imag(y) | ||
# c = np.real(x) | ||
# d = np.imag(x) | ||
# return np.arctan2(a, c) + 1j * (c * b - a * d) / (a**2 + c**2) | ||
a = real(y) | ||
b = imag(y) | ||
c = real(x) | ||
d = imag(x) | ||
return complex(atan(a, c), (c * b - a * d) / (a^2 + c^2)) | ||
end | ||
|
||
@inline function atan_cs_safe(y::T, x::T) where {T} | ||
return atan(y, x) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
319b287
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
319b287
There was a problem hiding this comment.
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/62899
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: