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

conditionally use SpecialFunctions through weak deps or Requires #186

Merged
merged 4 commits into from
Feb 7, 2023
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
12 changes: 10 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ version = "0.9.9"
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
TensorCore = "62fd8b95-f654-4bbd-a8a5-9c27f68ccd50"

[compat]
ColorTypes = "0.10, 0.11"
FixedPointNumbers = "0.8.2"
Requires = "1"
SpecialFunctions = "0.8, 0.9, 0.10, 1, 2.0"
TensorCore = "0.1"
julia = "1"

[weakdeps]
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[extensions]
SpecialFunctionsExt = "SpecialFunctions"

[extras]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Colors", "Test"]
test = ["SpecialFunctions", "Colors", "Test"]
22 changes: 22 additions & 0 deletions ext/SpecialFunctionsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module SpecialFunctionsExt
import ColorVectorSpace
using ColorTypes

isdefined(Base, :get_extension) ? (import SpecialFunctions) : (import ..SpecialFunctions)

const unaryops = (
:gamma, :logfactorial, :erf, :erfc, :erfcx, :erfi, :dawson,
:airyai, :airyaiprime, :airybi, :airybiprime,
:besselj0, :besselj1, :bessely0, :bessely1,
:eta, :zeta, :digamma
)

for op in unaryops
@eval SpecialFunctions.$op(c::AbstractGray) = Gray(SpecialFunctions.$op(gray(c)))
end

function SpecialFunctions.logabsgamma(c::AbstractGray)
lagc, s = SpecialFunctions.logabsgamma(gray(c))
return Gray(lagc), s
end
end
27 changes: 10 additions & 17 deletions src/ColorVectorSpace.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ColorVectorSpace

using ColorTypes, FixedPointNumbers, SpecialFunctions
using ColorTypes, FixedPointNumbers
using TensorCore
import TensorCore: ⊙, ⊗

Expand All @@ -11,7 +11,7 @@ import Base: abs, clamp, convert, copy, div, eps, float,
isfinite, isinf, isnan, isless, length, mapreduce, oneunit,
promote_op, promote_rule, zero, trunc, floor, round, ceil, bswap,
mod, mod1, rem, atan, hypot, max, min, real, typemin, typemax
# More unaryOps (mostly math functions)
# More unaryops (mostly math functions)
import Base: conj, sin, cos, tan, sinh, cosh, tanh,
asin, acos, atan, asinh, acosh, atanh,
sec, csc, cot, asec, acsc, acot,
Expand All @@ -23,7 +23,6 @@ import Base: conj, sin, cos, tan, sinh, cosh, tanh,
exp2, exp10, expm1, cbrt, sqrt,
significand, frexp, modf
import LinearAlgebra: norm, ⋅, dot, promote_leaf_eltypes # norm1, norm2, normInf
import SpecialFunctions: gamma, logabsgamma, lfact
using Statistics
import Statistics: middle # and `_mean_promote`

Expand Down Expand Up @@ -293,7 +292,7 @@ dotc(x::T, y::T) where {T<:AbstractRGB} = 0.200f0 * acc(red(x))*acc(red(y)) + 0.
dotc(x::AbstractRGB, y::AbstractRGB) = dotc(promote(x, y)...)

# Scalar Gray
const unaryOps = (:~, :conj, :abs,
const unaryops = (:~, :conj, :abs,
:sin, :cos, :tan, :sinh, :cosh, :tanh,
:asin, :acos, :atan, :asinh, :acosh, :atanh,
:sec, :csc, :cot, :asec, :acsc, :acot,
Expand All @@ -303,21 +302,10 @@ const unaryOps = (:~, :conj, :abs,
:asind, :atand, :rad2deg, :deg2rad,
:log, :log2, :log10, :log1p, :exponent, :exp,
:exp2, :exp10, :expm1, :cbrt, :sqrt,
:significand,
:gamma, :lfact, :frexp, :modf,
:(SpecialFunctions.erf), :(SpecialFunctions.erfc),
:(SpecialFunctions.erfcx), :(SpecialFunctions.erfi), :(SpecialFunctions.dawson),
:(SpecialFunctions.airyai),
:(SpecialFunctions.airyaiprime), :(SpecialFunctions.airybi), :(SpecialFunctions.airybiprime),
:(SpecialFunctions.besselj0), :(SpecialFunctions.besselj1), :(SpecialFunctions.bessely0), :(SpecialFunctions.bessely1),
:(SpecialFunctions.eta), :(SpecialFunctions.zeta), :(SpecialFunctions.digamma))
for op in unaryOps
:significand, :frexp, :modf)
for op in unaryops
@eval ($op)(c::AbstractGray) = Gray($op(gray(c)))
end
function logabsgamma(c::AbstractGray)
lagc, s = logabsgamma(gray(c))
return Gray(lagc), s
end

middle(c::AbstractGray) = arith_colorant_type(c)(middle(gray(c)))
middle(x::C, y::C) where {C<:AbstractGray} = arith_colorant_type(C)(middle(gray(x), gray(y)))
Expand Down Expand Up @@ -523,6 +511,8 @@ For more information about the transition, see ColorVectorSpace's README.
abs2(c::Union{Real,AbstractGray,AbstractRGB}) = c ⋅ c
end

isdefined(Base, :get_extension) || using Requires

function __init__()
if isdefined(Base, :Experimental) && isdefined(Base.Experimental, :register_error_hint)
Base.Experimental.register_error_hint(MethodError) do io, exc, argtypes, kwargs
Expand All @@ -549,6 +539,9 @@ function __init__()
end
end
end
@static if !isdefined(Base, :get_extension)
@require SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" include("../ext/SpecialFunctionsExt.jl")
end
end

## Precompilation
Expand Down
15 changes: 12 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,18 @@ ColorTypes.comp2(c::RGBA32) = alpha(c)
for g in (Gray(0.4), Gray{N0f8}(0.4))
@test @inferred(zero(g)) === typeof(g)(0)
@test @inferred(oneunit(g)) === typeof(g)(1)
for opgroup in (ColorVectorSpace.unaryOps, (:trunc, :floor, :round, :ceil, :eps, :bswap))
for op in opgroup
op ∈ (:frexp, :exponent, :modf, :lfact) && continue
SFE = if isdefined(Base, :get_extension)
Base.get_extension(ColorVectorSpace, :SpecialFunctionsExt)
else
ColorVectorSpace.SpecialFunctionsExt
end
for mod in (
ColorVectorSpace,
SFE,
(; unaryops = (:trunc, :floor, :round, :ceil, :eps, :bswap)),
)
for op in mod.unaryops
op ∈ (:frexp, :exponent, :modf, :logfactorial) && continue
op === :~ && eltype(g) === Float64 && continue
op === :significand && eltype(g) === N0f8 && continue
try
Expand Down