From ca80deb6b05506dff32076cede03010cdf46f700 Mon Sep 17 00:00:00 2001 From: Sebastian Stock <42280794+sostock@users.noreply.github.com> Date: Thu, 31 Oct 2024 15:44:35 +0100 Subject: [PATCH] cis/cispi without explicit uconvert (calls sincos/sincospi) --- src/Unitful.jl | 2 +- src/quantities.jl | 6 +++++- test/runtests.jl | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Unitful.jl b/src/Unitful.jl index e8919035..376755c8 100644 --- a/src/Unitful.jl +++ b/src/Unitful.jl @@ -7,7 +7,7 @@ import Base: min, max, floor, ceil, real, imag, conj import Base: complex, widen, reim # handled in complex.jl import Base: exp, exp10, exp2, expm1, log, log10, log1p, log2 import Base: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, - sinpi, cospi, sinc, cosc, cis + sinpi, cospi, sinc, cosc, cis, cispi import Base: eps, mod, rem, div, fld, cld, divrem, trunc, round, sign, signbit import Base: isless, isapprox, isinteger, isreal, isinf, isfinite, isnan import Base: copysign, flipsign diff --git a/src/quantities.jl b/src/quantities.jl index 64139c63..5bcf24ab 100644 --- a/src/quantities.jl +++ b/src/quantities.jl @@ -206,11 +206,15 @@ sqrt(x::AbstractQuantity) = Quantity(sqrt(x.val), sqrt(unit(x))) cbrt(x::AbstractQuantity) = Quantity(cbrt(x.val), cbrt(unit(x))) for _y in (:sin, :cos, :tan, :asin, :acos, :atan, :sinh, :cosh, :tanh, :asinh, :acosh, :atanh, - :sinpi, :cospi, :tanpi, :sinc, :cosc, :cis, :cispi, :sincospi) + :sinpi, :cospi, :tanpi, :sinc, :cosc, :sincospi) if isdefined(Base, _y) @eval Base.$(_y)(x::DimensionlessQuantity) = Base.$(_y)(uconvert(NoUnits, x)) end end +cis(x::DimensionlessQuantity{<:Real}) = Complex(reverse(sincos(x))...) +cis(x::DimensionlessQuantity{<:Complex}) = exp(-imag(x)) * Complex(reverse(sincos(real(x)))...) +cispi(x::DimensionlessQuantity{<:Real}) = Complex(reverse(sincospi(x))...) +cispi(x::DimensionlessQuantity{<:Complex}) = exp(-(π*imag(x))) * Complex(reverse(sincospi(real(x)))...) atan(y::AbstractQuantity{T1,D,U1}, x::AbstractQuantity{T2,D,U2}) where {T1,T2,D,U1,U2} = atan(promote(y,x)...) diff --git a/test/runtests.jl b/test/runtests.jl index 8635f9a0..0da1f85b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -741,7 +741,8 @@ Base.:(<=)(x::Issue399, y::Issue399) = x.num <= y.num @test @inferred(cbrt(m^3)) === m @test (2m)^3 === 8*m^3 @test (8m)^(1//3) === 2.0*m^(1//3) - @test @inferred(cis(90°)) ≈ im + @test @inferred(cis(90°)) == im + @test @inferred(cis((90 - rad2deg(1)*im)°)) ≈ ℯ*im # Test inferrability of literal powers _pow_m3(x) = x^-3 @@ -806,6 +807,7 @@ Base.:(<=)(x::Issue399, y::Issue399) = x.num <= y.num @test @inferred(cosc(1ft/3inch)) === 0.25 if isdefined(Base, :cispi) @test @inferred(cispi(rad/2)) === complex(0.0, 1.0) + @test @inferred(cispi(rad/2 + im*rad)) ≈ complex(0.0, exp(-π)) end if isdefined(Base, :sincospi) @test @inferred(sincospi(rad/2)) === (1.0, 0.0)