Skip to content

New Julia function cispi #35792

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

Closed
wants to merge 1 commit into from
Closed
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
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))
Comment on lines +544 to +546
Copy link
Member

@simeonschaub simeonschaub Jun 23, 2020

Choose a reason for hiding this comment

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

Suggested change
cispi(sign::Bool) = sign ? -1 : 1
cispi(sign::Integer) = oftype(sign, cispi(isodd(sign)))
cispi(theta::Real) = Complex(cospi(theta), sinpi(theta))
cispi(theta::Real) = complex(reverse(sincospi(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
Comment on lines +574 to +576
Copy link
Member

@simeonschaub simeonschaub Jun 23, 2020

Choose a reason for hiding this comment

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

Suggested change
function cispi(z::Complex)
cospi(z) + im*sinpi(z)
end
function cispi(z::Complex)
sipi, copi = sincospi(z)
return complex(real(copi) - imag(sipi), imag(copi) + real(sipi))
end

The second line might be a bit of a micro-optimization, but certainly won't hurt.


"""
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