Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebase of #35792, add cispi function #38449

Merged
merged 5 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,30 @@ function cis(z::Complex)
Complex(v * c, v * s)
end

cispi(theta::Real) = Complex(reverse(sincospi(theta))...)

"""
cispi(z)

Compute ``\\exp(i\\pi x)`` more accurately than `cis(pi*x)`, especially for large `x`.

# Examples
```jldoctest
julia> cispi(1)
-1.0 + 0.0im

julia> cispi(0.25 + 1im)
0.030556854645952924 + 0.030556854645952924im
```

!!! compat "Julia 1.6"
This function requires Julia 1.6 or later.
"""
function cispi(z::Complex)
sipi, copi = sincospi(z)
return complex(real(copi) - imag(sipi), imag(copi) + real(sipi))
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 @@ -229,6 +229,7 @@ export
cbrt,
ceil,
cis,
cispi,
clamp,
cld,
cmp,
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 @@ -162,6 +162,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