Skip to content

Commit

Permalink
Fix abs2 and abs function for Taylor{Complex{T}} (#292)
Browse files Browse the repository at this point in the history
* Fix abs2 and abs function for Taylor{Complex{T}}

* Use more general abs2 and fix for nested Taylor

* Update docstring of abs for complex type
  • Loading branch information
patkwee authored Dec 2, 2021
1 parent e79129f commit 7c20106
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/other_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ for T in (:Taylor1, :TaylorN)
end
end

abs2(a::$T) = a^2
abs2(a::$T) = real(a)^2 + imag(a)^2

abs(x::$T{T}) where {T<:Complex} = sqrt(abs2(x))
end
end

Expand Down Expand Up @@ -129,12 +131,19 @@ function abs(a::Taylor1{TaylorN{T}}) where {T<:Real}
end
end

abs(x::Taylor1{TaylorN{T}}) where {T<:Complex} = sqrt(abs2(x))
abs(x::TaylorN{Taylor1{T}}) where {T<:Complex} = sqrt(abs2(x))
abs(x::Taylor1{Taylor1{T}}) where {T<:Complex} = sqrt(abs2(x))

@doc doc"""
abs(a)
Returns `a` if `constant_term(a) > 0` and `-a` if `constant_term(a) < 0` for
For a `Real` type returns `a` if `constant_term(a) > 0` and `-a` if `constant_term(a) < 0` for
`a <:Union{Taylor1,TaylorN}`.
Notice that `typeof(abs(a)) <: AbstractSeries`.
For a `Complex` type, such as `Taylor1{ComplexF64}`, returns `sqrt(real(a)^2 + imag(a)^2)`.
Notice that `typeof(abs(a)) <: AbstractSeries` and
that for a `Complex` argument a `Real` type is returned (e.g. `typeof(abs(a::Taylor1{ComplexF64})) == Taylor1{Float64}`).
""" abs

Expand Down
4 changes: 4 additions & 0 deletions test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ using LinearAlgebra
@test (rem(1+xT,1.0))[0] == 0
@test abs(1-xT) == 1-xT
@test abs(-1-xT) == 1+xT
@test abs2(im*xT) == abs2(xT)
@test abs(im*(1+xT)) == abs(1+xT)
@test isapprox(abs2(exp(im*xT)), one(xT))
@test isapprox(abs(exp(im*xT)), one(xT))
@test differentiate(yH,1) == differentiate(xH, :x₂)
@test differentiate(mod2pi(2pi+yT^3),2) == derivative(yT^3, :x₂)
@test differentiate(yT^3, :x₂) == differentiate(yT^3, (0,1))
Expand Down
7 changes: 7 additions & 0 deletions test/mixtures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ using LinearAlgebra, SparseArrays
@test_throws DomainError abs(tN1)
@test_throws DomainError abs(t1N)

@test abs2(im*(tN1+1)) == (1+tN1)^2
@test abs2(im*(tN1-1)) == (1-tN1)^2
@test abs(im*(tN1+1)) == 1+tN1
@test abs(im*(tN1-1)) == 1-tN1

@test convert(Array{Taylor1{TaylorN{Float64}},1}, [tN1, tN1]) == [t1N, t1N]
@test convert(Array{Taylor1{TaylorN{Float64}},2}, [tN1 tN1]) == [t1N t1N]
@test convert(Array{TaylorN{Taylor1{Float64}},1}, [t1N, t1N]) == [tN1, tN1]
Expand Down Expand Up @@ -341,4 +346,6 @@ end
tti = (ti2to/to)/ti
@test get_order(tti) == get_order(to)-1
@test get_order(tti[0]) == get_order(ti)-1
@test isapprox(abs2(exp(im*to)), one(to))
@test isapprox(abs(exp(im*to)), one(to))
end
4 changes: 4 additions & 0 deletions test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ Base.iszero(::SymbNumber) = false
@test real(exp(tim)) == cos(t)
@test imag(exp(tim)) == sin(t)
@test exp(conj(tim)) == cos(t)-im*sin(t) == exp(tim')
@test abs2(tim) == tsquare
@test abs(tim) == t
@test isapprox(abs2(exp(tim)), ot)
@test isapprox(abs(exp(tim)), ot)
@test (exp(t))^(2im) == cos(2t)+im*sin(2t)
@test (exp(t))^Taylor1([-5.2im]) == cos(5.2t)-im*sin(5.2t)
@test getcoeff(convert(Taylor1{Rational{Int}},cos(t)),8) == 1//factorial(8)
Expand Down

0 comments on commit 7c20106

Please sign in to comment.