Skip to content

Commit

Permalink
Implement cispi
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed May 11, 2020
1 parent 4562ece commit 27d53d8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,40 @@ function cis(z::Complex)
Complex(v * c, v * s)
end

cispi(sign::Bool) = sign ? -1 : 1
cispi(sign::Integer) = oftype(sign, cispi(isodd(sign)))
cispi(theta::Real) = Complex(cospi(theta), sinpi(theta))

"""
cispi(z)
Return ``\\exp(iπz)``.
This function also accepts integer or boolean arguments, and then
returns integer results. This allows calculating ``(-1)^s``
conveniently and efficiently.
`cispi` is related to [`signbit`](@ref), and allows decomposing
integers into sign bit and absolute value: ``cispi(signbit(n)) *
abs(n) == n``. For boolean values, it is also, ``signbit(cispi(b)) ==
b``.
# Examples
```jldoctest
julia> cispi(1) == -1
true
julia> cispi(0) == 1
true
```
!!! compat "Julia 1.6"
This function requires Julia 1.6 or later.
"""
function cispi(z::Complex)
cospi(z) + im*sinpi(z)
end

"""
angle(z)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export
cbrt,
ceil,
cis,
cispi,
clamp,
cld,
cmp,
Expand Down
5 changes: 5 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ copy(x::Number) = x # some code treats numbers as collection-like
Returns `true` if the value of the sign of `x` is negative, otherwise `false`.
Related to `signbit` is [`cispi`](@ref) (which calculates `(-1)^n`).
This allows decomposing integers into sign bit and absolute value:
``cispi(signbit(n)) * abs(n) == n``. For boolean values, it is also,
``signbit(cispi(b)) == b``.
# Examples
```jldoctest
julia> signbit(-4)
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Base.reim
Base.conj
Base.angle
Base.cis
Base.cispi
Base.binomial
Base.factorial
Base.gcd
Expand Down
17 changes: 17 additions & 0 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ end
@test atanh(x) atanh(big(x))
@test cis(real(x)) cis(real(big(x)))
@test cis(x) cis(big(x))
@test cispi(real(x)) cispi(real(big(x)))
@test cispi(x) cispi(big(x))
@test cos(x) cos(big(x))
@test cosh(x) cosh(big(x))
@test exp(x) exp(big(x))
Expand Down Expand Up @@ -918,6 +920,21 @@ end
@test cis(1.0+0.0im) 0.54030230586813971740093660744297660373231042061+0.84147098480789650665250232163029899962256306079im
@test cis(pi) -1.0+0.0im
@test cis(pi/2) 0.0+1.0im
@test cispi(false) == 1
@test cispi(true) == -1
@test cispi(-1) == -1
@test cispi(0) == 1
@test cispi(1) == -1
@test cispi(2) == 1
@test cispi(0.0) == cispi(0)
@test cispi(1.0) == cispi(1)
@test cispi(2.0) == cispi(2)
@test cispi(0.5) == im
@test cispi(1.5) == -im
@test cispi(0.25) cis/4)
@test cispi(0.0+0.0im) == cispi(0)
@test cispi(1.0+0.0im) == cispi(1)
@test cispi(2.0+0.0im) == cispi(2)
end

@testset "exp2" begin
Expand Down

0 comments on commit 27d53d8

Please sign in to comment.