Skip to content

Commit

Permalink
Solve invalidations (#344)
Browse files Browse the repository at this point in the history
* Comment code leading to an ambiguity

* Solve some invalidations

* Solve problem with docstrings of _populate_dicts!

* Solve ambiguities when IA is loaded, with tests

* Bump patch version

* Bring back a conditional test
  • Loading branch information
lbenet authored Dec 5, 2023
1 parent 9a16131 commit 9437c3f
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:

jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.julia-threads }} thread(s) - ${{ github.event_name }}
name: Julia ${{ matrix.julia-version }} - ${{ matrix.os }} - ${{ matrix.julia-threads }} thread(s) - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
if: "!contains(github.event.head_commit.message, 'skip ci')"
env:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TaylorSeries"
uuid = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
repo = "https://github.com/JuliaDiff/TaylorSeries.jl.git"
version = "0.15.3"
version = "0.15.4"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ _dict_unary_ops
_dict_binary_calls
_dict_unary_calls
_dict_binary_ops
_populate_dicts!
@isonethread
```

Expand Down
15 changes: 9 additions & 6 deletions ext/TaylorSeriesIAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ isdefined(Base, :get_extension) ? (using IntervalArithmetic) : (using ..Interval

# Method used for Taylor1{Interval{T}}^n
for T in (:Taylor1, :TaylorN)
@eval function ^(a::$T{Interval{S}}, n::Integer) where {S<:Real}
n == 0 && return one(a)
n == 1 && return copy(a)
n == 2 && return square(a)
n < 0 && return a^float(n)
return power_by_squaring(a, n)
@eval begin
function ^(a::$T{Interval{S}}, n::Integer) where {S<:Real}
n == 0 && return one(a)
n == 1 && return copy(a)
n == 2 && return square(a)
n < 0 && return a^float(n)
return power_by_squaring(a, n)
end
^(a::$T{Interval{S}}, r::Rational) where {S<:Real} = a^float(r)
end
end

Expand Down
51 changes: 27 additions & 24 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,26 @@ function convert(::Type{Taylor1{TaylorN{T}}}, s::TaylorN{Taylor1{T}}) where {T<:
return Taylor1(vT)
end

function convert(::Type{Array{TaylorN{Taylor1{T}},N}},
s::Array{Taylor1{TaylorN{T}},N}) where {T<:NumberNotSeries,N}

v = Array{TaylorN{Taylor1{T}}}(undef, size(s))
@simd for ind in eachindex(s)
@inbounds v[ind] = convert(TaylorN{Taylor1{T}}, s[ind])
end
return v
end

function convert(::Type{Array{Taylor1{TaylorN{T}},N}},
s::Array{TaylorN{Taylor1{T}},N}) where {T<:NumberNotSeries,N}

v = Array{Taylor1{TaylorN{T}}}(undef, size(s))
@simd for ind in eachindex(s)
@inbounds v[ind] = convert(Taylor1{TaylorN{T}}, s[ind])
end
return v
end
# iss 339: this triggers some invalidations
# function convert(::Type{Array{TaylorN{Taylor1{T}},N}},
# s::Array{Taylor1{TaylorN{T}},N}) where {T<:NumberNotSeries,N}
#
# v = Array{TaylorN{Taylor1{T}}}(undef, size(s))
# @simd for ind in eachindex(s)
# @inbounds v[ind] = convert(TaylorN{Taylor1{T}}, s[ind])
# end
# return v
# end
#
# function convert(::Type{Array{Taylor1{TaylorN{T}}}},
# s::Array{TaylorN{Taylor1{T}}}) where {T<:NumberNotSeries}
#
# v = Array{Taylor1{TaylorN{T}}}(undef, size(s))
# @simd for ind in eachindex(s)
# @inbounds v[ind] = convert(Taylor1{TaylorN{T}}, s[ind])
# end
# return v
# end



Expand Down Expand Up @@ -197,11 +198,13 @@ promote_rule(::Type{TaylorN{T}}, ::Type{S}) where {T<:Number,S<:Number} =
TaylorN{promote_type(T,S)}


# Order may matter
promote_rule(::Type{S}, ::Type{T}) where {S<:NumberNotSeries,T<:AbstractSeries} =
promote_rule(T,S)
# disambiguation with Base
promote_rule(::Type{Bool}, ::Type{T}) where {T<:AbstractSeries} = promote_rule(T, Bool)
# iss 339: this triggers some invalidations
# # Order may matter
# promote_rule(::Type{S}, ::Type{T}) where {S<:NumberNotSeries, T<:AbstractSeries} =
# promote_rule(T,S)
#
# # disambiguation with Base
# promote_rule(::Type{Bool}, ::Type{T}) where {T<:AbstractSeries} = promote_rule(T, Bool)

promote_rule(::Type{S}, ::Type{T}) where
{S<:AbstractIrrational,T<:AbstractSeries} = promote_rule(T,S)
Expand Down
148 changes: 84 additions & 64 deletions src/dictmutfunct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct _InternalMutFuncs
end

# Constructor
function _InternalMutFuncs( namef::Vector )
function _InternalMutFuncs( namef::Tuple )
if length(namef) == 3
return _InternalMutFuncs( namef[1], namef[2], namef[3], Expr(:nothing) )
else
Expand All @@ -56,11 +56,11 @@ of `:_res`.
"""
const _dict_binary_ops = Dict(
:+ => [:add!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 + _arg2)],
:- => [:subst!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 - _arg2)],
:* => [:mul!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 * _arg2)],
:/ => [:div!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 / _arg2)],
:^ => [:pow!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 ^ float(_arg2))],
:+ => (:add!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 + _arg2)),
:- => (:subst!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 - _arg2)),
:* => (:mul!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 * _arg2)),
:/ => (:div!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 / _arg2)),
:^ => (:pow!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 ^ float(_arg2))),
);

"""
Expand All @@ -83,50 +83,50 @@ added as the last entry of the vector.
"""
const _dict_unary_ops = Dict(
:+ => [:add!, (:_res, :_arg1, :_k), :(_res = + _arg1)],
:- => [:subst!, (:_res, :_arg1, :_k), :(_res = - _arg1)],
:sqr => [:sqr!, (:_res, :_arg1, :_k), :(_res = sqr(_arg1))],
:sqrt => [:sqrt!, (:_res, :_arg1, :_k), :(_res = sqrt(_arg1))],
:exp => [:exp!, (:_res, :_arg1, :_k), :(_res = exp(_arg1))],
:expm1 => [:expm1!, (:_res, :_arg1, :_k), :(_res = expm1(_arg1))],
:log => [:log!, (:_res, :_arg1, :_k), :(_res = log(_arg1))],
:log1p => [:log1p!, (:_res, :_arg1, :_k), :(_res = log1p(_arg1))],
:identity => [:identity!, (:_res, :_arg1, :_k), :(_res = identity(_arg1))],
:zero => [:zero!, (:_res, :_arg1, :_k), :(_res = zero(_arg1))],
:one => [:one!, (:_res, :_arg1, :_k), :(_res = one(_arg1))],
:abs => [:abs!, (:_res, :_arg1, :_k), :(_res = abs(_arg1))],
:abs2 => [:abs2!, (:_res, :_arg1, :_k), :(_res = abs2(_arg1))],
:deg2rad => [:deg2rad!, (:_res, :_arg1, :_k), :(_res = deg2rad(_arg1))],
:rad2deg => [:rad2deg!, (:_res, :_arg1, :_k), :(_res = rad2deg(_arg1))],
:+ => (:add!, (:_res, :_arg1, :_k), :(_res = + _arg1)),
:- => (:subst!, (:_res, :_arg1, :_k), :(_res = - _arg1)),
:sqr => (:sqr!, (:_res, :_arg1, :_k), :(_res = sqr(_arg1))),
:sqrt => (:sqrt!, (:_res, :_arg1, :_k), :(_res = sqrt(_arg1))),
:exp => (:exp!, (:_res, :_arg1, :_k), :(_res = exp(_arg1))),
:expm1 => (:expm1!, (:_res, :_arg1, :_k), :(_res = expm1(_arg1))),
:log => (:log!, (:_res, :_arg1, :_k), :(_res = log(_arg1))),
:log1p => (:log1p!, (:_res, :_arg1, :_k), :(_res = log1p(_arg1))),
:identity => (:identity!, (:_res, :_arg1, :_k), :(_res = identity(_arg1))),
:zero => (:zero!, (:_res, :_arg1, :_k), :(_res = zero(_arg1))),
:one => (:one!, (:_res, :_arg1, :_k), :(_res = one(_arg1))),
:abs => (:abs!, (:_res, :_arg1, :_k), :(_res = abs(_arg1))),
:abs2 => (:abs2!, (:_res, :_arg1, :_k), :(_res = abs2(_arg1))),
:deg2rad => (:deg2rad!, (:_res, :_arg1, :_k), :(_res = deg2rad(_arg1))),
:rad2deg => (:rad2deg!, (:_res, :_arg1, :_k), :(_res = rad2deg(_arg1))),
#
:sin => [:sincos!, (:_res, :_aux, :_arg1, :_k), :(_res = sin(_arg1)),
:(_aux = cos(_arg1))],
:cos => [:sincos!, (:_aux, :_res, :_arg1, :_k), :(_res = cos(_arg1)),
:(_aux = sin(_arg1))],
:sinpi => [:sincospi!, (:_res, :_aux, :_arg1, :_k), :(_res = sinpi(_arg1)),
:(_aux = cospi(_arg1))],
:cospi => [:sincospi!, (:_aux, :_res, :_arg1, :_k), :(_res = cospi(_arg1)),
:(_aux = sinpi(_arg1))],
:tan => [:tan!, (:_res, :_arg1, :_aux, :_k), :(_res = tan(_arg1)),
:(_aux = tan(_arg1)^2)],
:asin => [:asin!, (:_res, :_arg1, :_aux, :_k), :(_res = asin(_arg1)),
:(_aux = sqrt(1 - _arg1^2))],
:acos => [:acos!, (:_res, :_arg1, :_aux, :_k), :(_res = acos(_arg1)),
:(_aux = sqrt(1 - _arg1^2))],
:atan => [:atan!, (:_res, :_arg1, :_aux, :_k), :(_res = atan(_arg1)),
:(_aux = 1 + _arg1^2)],
:sinh => [:sinhcosh!, (:_res, :_aux, :_arg1, :_k), :(_res = sinh(_arg1)),
:(_aux = cosh(_arg1))],
:cosh => [:sinhcosh!, (:_aux, :_res, :_arg1, :_k), :(_res = cosh(_arg1)),
:(_aux = sinh(_arg1))],
:tanh => [:tanh!, (:_res, :_arg1, :_aux, :_k), :(_res = tanh(_arg1)),
:(_aux = tanh(_arg1)^2)],
:asinh => [:asinh!, (:_res, :_arg1, :_aux, :_k), :(_res = asinh(_arg1)),
:(_aux = sqrt(_arg1^2 + 1))],
:acosh => [:acosh!, (:_res, :_arg1, :_aux, :_k), :(_res = acosh(_arg1)),
:(_aux = sqrt(_arg1^2 - 1))],
:atanh => [:atanh!, (:_res, :_arg1, :_aux, :_k), :(_res = atanh(_arg1)),
:(_aux = 1 - _arg1^2)],
:sin => (:sincos!, (:_res, :_aux, :_arg1, :_k), :(_res = sin(_arg1)),
:(_aux = cos(_arg1))),
:cos => (:sincos!, (:_aux, :_res, :_arg1, :_k), :(_res = cos(_arg1)),
:(_aux = sin(_arg1))),
:sinpi => (:sincospi!, (:_res, :_aux, :_arg1, :_k), :(_res = sinpi(_arg1)),
:(_aux = cospi(_arg1))),
:cospi => (:sincospi!, (:_aux, :_res, :_arg1, :_k), :(_res = cospi(_arg1)),
:(_aux = sinpi(_arg1))),
:tan => (:tan!, (:_res, :_arg1, :_aux, :_k), :(_res = tan(_arg1)),
:(_aux = tan(_arg1)^2)),
:asin => (:asin!, (:_res, :_arg1, :_aux, :_k), :(_res = asin(_arg1)),
:(_aux = sqrt(1 - _arg1^2))),
:acos => (:acos!, (:_res, :_arg1, :_aux, :_k), :(_res = acos(_arg1)),
:(_aux = sqrt(1 - _arg1^2))),
:atan => (:atan!, (:_res, :_arg1, :_aux, :_k), :(_res = atan(_arg1)),
:(_aux = 1 + _arg1^2)),
:sinh => (:sinhcosh!, (:_res, :_aux, :_arg1, :_k), :(_res = sinh(_arg1)),
:(_aux = cosh(_arg1))),
:cosh => (:sinhcosh!, (:_aux, :_res, :_arg1, :_k), :(_res = cosh(_arg1)),
:(_aux = sinh(_arg1))),
:tanh => (:tanh!, (:_res, :_arg1, :_aux, :_k), :(_res = tanh(_arg1)),
:(_aux = tanh(_arg1)^2)),
:asinh => (:asinh!, (:_res, :_arg1, :_aux, :_k), :(_res = asinh(_arg1)),
:(_aux = sqrt(_arg1^2 + 1))),
:acosh => (:acosh!, (:_res, :_arg1, :_aux, :_k), :(_res = acosh(_arg1)),
:(_aux = sqrt(_arg1^2 - 1))),
:atanh => (:atanh!, (:_res, :_arg1, :_aux, :_k), :(_res = atanh(_arg1)),
:(_aux = 1 - _arg1^2)),
);


Expand All @@ -146,8 +146,32 @@ _internalmutfunc_call( fn :: _InternalMutFuncs ) = (
Expr( :call, Meta.parse("TaylorSeries.$(fn.namef)"), fn.argsf... ), fn.defexpr, fn.auxexpr )


"""
`_populate_dicts!()`
Function that populates the internal dictionaries [`_dict_unary_calls`](@ref) and
[`_dict_binary_calls`](@ref)
"""
function _populate_dicts!()
#Populates the constant vector `_dict_unary_calls`.
_dict_unary_calls = Dict{Symbol, NTuple{3,Expr}}()
for kk in keys(_dict_unary_ops)
res = _internalmutfunc_call( _InternalMutFuncs(_dict_unary_ops[kk]) )
push!(_dict_unary_calls, kk => res )
end

#Populates the constant vector `_dict_binary_calls`.
_dict_binary_calls = Dict{Symbol, NTuple{3,Expr}}()
for kk in keys(_dict_binary_ops)
res = _internalmutfunc_call( _InternalMutFuncs(_dict_binary_ops[kk]) )
push!(_dict_binary_calls, kk => res )
end
return _dict_unary_calls, _dict_binary_calls
end

const _dict_unary_calls, _dict_binary_calls = _populate_dicts!()

@doc """
`_dict_unary_calls::Dict{Symbol, NTuple{2,Expr}}`
Dictionary with the expressions that define the
Expand All @@ -158,16 +182,16 @@ internal mutating functions.
Evaluating the entries generates expressions that represent
the actual calls to the internal mutating functions.
"""
const _dict_unary_calls = Dict{Symbol, NTuple{3,Expr}}()
""" _dict_unary_calls
# const _dict_unary_calls = Dict{Symbol, NTuple{3,Expr}}()

#Populates the constant vector `_dict_unary_calls`.
for kk in keys(_dict_unary_ops)
res = _internalmutfunc_call( _InternalMutFuncs(_dict_unary_ops[kk]) )
push!(_dict_unary_calls, kk => res )
end
# #Populates the constant vector `_dict_unary_calls`.
# for kk in keys(_dict_unary_ops)
# res = _internalmutfunc_call( _InternalMutFuncs(_dict_unary_ops[kk]) )
# push!(_dict_unary_calls, kk => res )
# end

"""
@doc """
`_dict_binary_calls::Dict{Symbol, NTuple{2,Expr}}`
Dictionary with the expressions that define the
Expand All @@ -178,10 +202,6 @@ internal mutating functions.
Evaluating the entries generates symbols that represent
the actual calls to the internal mutating functions.
"""
const _dict_binary_calls = Dict{Symbol, NTuple{3,Expr}}()
#Populates the constant vector `_dict_binary_calls`.
for kk in keys(_dict_binary_ops)
res = _internalmutfunc_call( _InternalMutFuncs(_dict_binary_ops[kk]) )
push!(_dict_binary_calls, kk => res )
end
""" _dict_binary_calls
# const _dict_binary_calls = Dict{Symbol, NTuple{3,Expr}}()

2 changes: 1 addition & 1 deletion src/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ for T in (:Taylor1, :TaylorN)
return power_by_squaring(a, n)
end

@eval ^(a::$T, x::Rational) = a^(x.num/x.den)
@eval ^(a::$T, x::S) where {S<:Rational} = a^(x.num/x.den)

@eval ^(a::$T, b::$T) = exp( b*log(a) )

Expand Down
4 changes: 4 additions & 0 deletions test/aqua.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ using Aqua
pkg_match(pkgname, pkdir::Nothing) = false
pkg_match(pkgname, pkdir::AbstractString) = occursin(pkgname, pkdir)
filter!(x -> pkg_match("TaylorSeries", pkgdir(last(x).module)), ambs)
for method_ambiguity in ambs
@show method_ambiguity
end
if VERSION < v"1.10.0-DEV"
@test length(ambs) == 0
end
Expand All @@ -27,6 +30,7 @@ end
Aqua.test_piracies(TaylorSeries)
Aqua.test_unbound_args(TaylorSeries)
Aqua.test_project_extras(TaylorSeries)
Aqua.test_persistent_tasks(TaylorSeries)
end

nothing
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ for file in testfiles
VERSION < v"1.1" && file == "intervals.jl" && continue
include(file)
end
# After `using intervalArithmetic` new ambiguities may arise
include("aqua.jl")

2 comments on commit 9437c3f

@lbenet
Copy link
Member Author

@lbenet lbenet commented on 9437c3f Dec 5, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/96487

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.15.4 -m "<description of version>" 9437c3f2409558554ee137f544bd309800315d76
git push origin v0.15.4

Please sign in to comment.