Skip to content

Commit

Permalink
Improve type inference in simple functions (#304)
Browse files Browse the repository at this point in the history
* add tests

* fix real, imag and conj

* fix isinf and isnan

* bump patch version
  • Loading branch information
dkarrasch authored Aug 23, 2022
1 parent c83da89 commit 28ad329
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
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.12.1"
version = "0.12.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
6 changes: 3 additions & 3 deletions src/other_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)

## real, imag, conj and ctranspose ##
for f in (:real, :imag, :conj)
@eval ($f)(a::$T) = $T(($f).(a.coeffs), a.order)
@eval ($f)(a::$T) = $T($f(a.coeffs), a.order)
end

@eval adjoint(a::$T) = conj(a)

## isinf and isnan ##
@eval isinf(a::$T) = any( isinf.(a.coeffs) )
@eval isinf(a::$T) = any(isinf, a.coeffs)

@eval isnan(a::$T) = any( isnan.(a.coeffs) )
@eval isnan(a::$T) = any(isnan, a.coeffs)
end


Expand Down
9 changes: 7 additions & 2 deletions test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ using LinearAlgebra
@test xH == convert(HomogeneousPolynomial{Float64},xH)
@test HomogeneousPolynomial(xH) == xH
@test HomogeneousPolynomial(0,0) == 0
@test (@inferred conj(xH)) == (@inferred adjoint(xH))
@test (@inferred real(xH)) == xH
xT = TaylorN(xH, 17)
yT = TaylorN(Int, 2, order=17)
@test (@inferred conj(xT)) == (@inferred adjoint(xT))
@test (@inferred real(xT)) == (xT)
zeroT = zero( TaylorN([xH],1) )
@test (@inferred imag(xT)) == (zeroT)
@test zeroT.coeffs == zeros(HomogeneousPolynomial{Int}, 1)
@test size(xH) == (2,)
@test firstindex(xH) == 1
Expand Down Expand Up @@ -376,8 +381,8 @@ using LinearAlgebra
@test (1+xT)^(3//2) == ((1+xT)^0.5)^3
@test real(xH) == xH
@test imag(xH) == zero(xH)
@test conj(im*yH) == (im*yH)'
@test conj(im*yT) == (im*yT)'
@test (@inferred conj(im*yH)) == (@inferred adjoint(im*yH))
@test (@inferred conj(im*yT)) == (@inferred adjoint(im*yT))
@test real( exp(1im * xT)) == cos(xT)
@test getcoeff(convert(TaylorN{Rational{Int}},cos(xT)),(4,0)) ==
1//factorial(4)
Expand Down
16 changes: 8 additions & 8 deletions test/mixtures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ using LinearAlgebra
end
@test string(t1N*t1N) ==
" 1.0 x₁² + 𝒪(‖x‖³) + ( 2.0 x₁ + 𝒪(‖x‖³)) t + ( 1.0 + 𝒪(‖x‖³)) t² + ( 2.0 x₂² + 𝒪(‖x‖³)) t³ + 𝒪(t⁴)"
@test !isnan(tN1)
@test !isinf(tN1)
@test !(@inferred isnan(tN1))
@test !(@inferred isinf(tN1))

@test mod(tN1+1,1.0) == 0+tN1
@test mod(tN1-1.125,2) == 0.875+tN1
Expand Down Expand Up @@ -185,10 +185,10 @@ using LinearAlgebra
@test typeof(xx) == Taylor1{TaylorN{Float64}}
@test eltype(xx) == Taylor1{TaylorN{Float64}}
@test TS.numtype(xx) == TaylorN{Float64}
@test !isnan(xx)
@test !isnan(δx)
@test !isinf(xx)
@test !isinf(δx)
@test !(@inferred isnan(xx))
@test !(@inferred isnan(δx))
@test !(@inferred isinf(xx))
@test !(@inferred isinf(δx))
@test +xx == xx
@test -xx == 0 - xx
@test xx/1.0 == 1.0*xx
Expand Down Expand Up @@ -297,8 +297,8 @@ using LinearAlgebra
@test P P
Q = deepcopy(P)
Q[2][2] = Taylor1([NaN, Inf])
@test isnan(Q)
@test isinf(Q)
@test (@inferred isnan(Q))
@test (@inferred isinf(Q))
@test !isfinite(Q)
Q[2][2] = P[2][2]+sqrt(eps())/2
@test isapprox(P, Q, rtol=1.0)
Expand Down
4 changes: 2 additions & 2 deletions test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ Base.iszero(::SymbNumber) = false
@test TaylorSeries.findlast(zt) == -1
@test iszero(zero(t))
@test !iszero(one(t))
@test isinf(Taylor1([typemax(1.0)]))
@test isnan(Taylor1([typemax(1.0), NaN]))
@test @inferred isinf(Taylor1([typemax(1.0)]))
@test @inferred isnan(Taylor1([typemax(1.0), NaN]))

@test constant_term(2.0) == 2.0
@test constant_term(t) == 0
Expand Down

2 comments on commit 28ad329

@lbenet
Copy link
Member

@lbenet lbenet commented on 28ad329 Aug 23, 2022

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/66863

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.12.2 -m "<description of version>" 28ad329f06e8a50bdc372fc3c57f5939f9d59cfc
git push origin v0.12.2

Please sign in to comment.