Skip to content

Commit 96d59f9

Browse files
jmerteschnettsimeonschaub
authored
Rebase of #35792, add cispi function (#38449)
* Implement cispi * Review comments in #35792 * Remove docs relating cispi and signbit * Review comment, more expressive function doc. Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com> * Switch examples to show complex outputs Co-authored-by: Erik Schnetter <schnetter@gmail.com> Co-authored-by: Simeon Schaub <simeondavidschaub99@gmail.com>
1 parent 65517f2 commit 96d59f9

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

base/complex.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,30 @@ function cis(z::Complex)
541541
Complex(v * c, v * s)
542542
end
543543

544+
cispi(theta::Real) = Complex(reverse(sincospi(theta))...)
545+
546+
"""
547+
cispi(z)
548+
549+
Compute ``\\exp(i\\pi x)`` more accurately than `cis(pi*x)`, especially for large `x`.
550+
551+
# Examples
552+
```jldoctest
553+
julia> cispi(1)
554+
-1.0 + 0.0im
555+
556+
julia> cispi(0.25 + 1im)
557+
0.030556854645952924 + 0.030556854645952924im
558+
```
559+
560+
!!! compat "Julia 1.6"
561+
This function requires Julia 1.6 or later.
562+
"""
563+
function cispi(z::Complex)
564+
sipi, copi = sincospi(z)
565+
return complex(real(copi) - imag(sipi), imag(copi) + real(sipi))
566+
end
567+
544568
"""
545569
angle(z)
546570

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export
229229
cbrt,
230230
ceil,
231231
cis,
232+
cispi,
232233
clamp,
233234
cld,
234235
cmp,

doc/src/base/math.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Base.reim
162162
Base.conj
163163
Base.angle
164164
Base.cis
165+
Base.cispi
165166
Base.binomial
166167
Base.factorial
167168
Base.gcd

test/complex.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ end
123123
@test atanh(x) atanh(big(x))
124124
@test cis(real(x)) cis(real(big(x)))
125125
@test cis(x) cis(big(x))
126+
@test cispi(real(x)) cispi(real(big(x)))
127+
@test cispi(x) cispi(big(x))
126128
@test cos(x) cos(big(x))
127129
@test cosh(x) cosh(big(x))
128130
@test exp(x) exp(big(x))
@@ -918,6 +920,21 @@ end
918920
@test cis(1.0+0.0im) 0.54030230586813971740093660744297660373231042061+0.84147098480789650665250232163029899962256306079im
919921
@test cis(pi) -1.0+0.0im
920922
@test cis(pi/2) 0.0+1.0im
923+
@test cispi(false) == 1
924+
@test cispi(true) == -1
925+
@test cispi(-1) == -1
926+
@test cispi(0) == 1
927+
@test cispi(1) == -1
928+
@test cispi(2) == 1
929+
@test cispi(0.0) == cispi(0)
930+
@test cispi(1.0) == cispi(1)
931+
@test cispi(2.0) == cispi(2)
932+
@test cispi(0.5) == im
933+
@test cispi(1.5) == -im
934+
@test cispi(0.25) cis/4)
935+
@test cispi(0.0+0.0im) == cispi(0)
936+
@test cispi(1.0+0.0im) == cispi(1)
937+
@test cispi(2.0+0.0im) == cispi(2)
921938
end
922939

923940
@testset "exp2" begin

0 commit comments

Comments
 (0)