Skip to content

Commit

Permalink
Clean-up arithmetic.jl (metaprogramming) and add RealOrComplex abbrev…
Browse files Browse the repository at this point in the history
…iation
  • Loading branch information
Luis Benet committed Mar 2, 2017
1 parent 613a79c commit cd9cd9f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 99 deletions.
154 changes: 55 additions & 99 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,109 +92,65 @@ one{T<:Number}(a::TaylorN{T}) = TaylorN(one(T), a.order)

## Addition and substraction ##
for f in (:+, :-)
@eval begin
function ($f)(a::Taylor1, b::Taylor1)
a, b = fixshape(a, b)
v = similar(a.coeffs)
@simd for i in eachindex(a.coeffs)
@inbounds v[i] = ($f)(a.coeffs[i], b.coeffs[i])
end
return Taylor1(v, a.order)
end
function ($f)(a::Taylor1)
v = similar(a.coeffs)
@simd for i in eachindex(v)
@inbounds v[i] = ($f)(a.coeffs[i])
end
return Taylor1(v, a.order)
end
function ($f)(a::Taylor1, b::Union{Real,Complex})
@inbounds aux = ($f)(a.coeffs[1], b)
v = Array{typeof(aux)}(length(a.coeffs))
@simd for i in eachindex(v)
@inbounds v[i] = a.coeffs[i]
end
@inbounds v[1] = aux
Taylor1(v, a.order)
end
function ($f)(a::Union{Real,Complex}, b::Taylor1)
@inbounds aux = ($f)(a, b.coeffs[1])
v = Array{typeof(aux)}(length(b.coeffs))
@simd for i in eachindex(v)
@inbounds v[i] = ($f)(b.coeffs[i])
for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
@eval begin
function $f(a::$T, b::$T)
a, b = fixshape(a, b)
v = similar(a.coeffs)
@simd for i in eachindex(v)
@inbounds v[i] = $f(a.coeffs[i], b.coeffs[i])
end
return $T(v, a.order)
end
@inbounds v[1] = aux
Taylor1(v, b.order)
end
end
end

for T in (:HomogeneousPolynomial, :TaylorN), f in (:+, :-)
@eval begin
function ($f)(a::($T), b::($T))
a, b = fixshape(a, b)
v = Array{eltype(a.coeffs)}(length(a.coeffs))
for i in eachindex(v)
@inbounds v[i] = ($f)(a.coeffs[i], b.coeffs[i])
function $f(a::$T)
v = similar(a.coeffs)
@simd for i in eachindex(v)
@inbounds v[i] = $f(a.coeffs[i])
end
return $T(v, a.order)
end
return ($T)(v, a.order)
end
function ($f)(a::($T))
v = Array{eltype(a.coeffs)}(length(a.coeffs))
for i in eachindex(v)
@inbounds v[i] = ($f)(a.coeffs[i])
if $T != HomogeneousPolynomial
function $f(a::$T, b::RealOrComplex)
R = promote_type(eltype(a.coeffs), eltype(b))
coeffs = Array{R}(a.order+1)
@simd for i in eachindex(coeffs)
@inbounds coeffs[i] = a.coeffs[i]
end
@inbounds coeffs[1] = $f(a.coeffs[1], b)
return $T(coeffs, a.order)
end
function $f(b::RealOrComplex, a::$T)
R = promote_type(eltype(a.coeffs), eltype(b))
coeffs = Array{R}(a.order+1)
@simd for i in eachindex(coeffs)
@inbounds coeffs[i] = $f(a.coeffs[i])
end
@inbounds coeffs[1] = $f(b, a.coeffs[1])
return $T(coeffs, a.order)
end
end
return ($T)(v, a.order)
end
end
end
for f in (:+, :-)
@eval begin
function ($f)(a::TaylorN, b::Union{Real,Complex})
@inbounds aux = ($f)(a.coeffs[1], b)
S = eltype(aux)
coeffs = Array{HomogeneousPolynomial{S}}(length(a.coeffs))
@simd for i in eachindex(coeffs)
@inbounds coeffs[i] = a.coeffs[i]
end
@inbounds coeffs[1] = aux
return TaylorN{S}(coeffs, a.order)
end
function ($f)(b::Union{Real,Complex}, a::TaylorN)
@inbounds aux = ($f)(b, a.coeffs[1])
S = eltype(aux)
coeffs = Array{HomogeneousPolynomial{S}}(length(a.coeffs))
@simd for i in eachindex(coeffs)
@inbounds coeffs[i] = ($f)(a.coeffs[i])
end
@inbounds coeffs[1] = aux
return TaylorN{S}(coeffs, a.order)
end
function ($f){T<:Union{Real,Complex}}(a::TaylorN{Taylor1{T}},
b::Taylor1{T})
@inbounds aux = ($f)(a.coeffs[1], b)
S = typeof(aux)
# @inbounds aux = ($f)(a.coeffs[1].coeffs[1], b)
# S = Taylor1{T}
coeffs = Array{HomogeneousPolynomial{S}}(length(a.coeffs))
function ($f){T<:RealOrComplex}(a::TaylorN{Taylor1{T}}, b::Taylor1{T})
@inbounds aux = $f(a.coeffs[1].coeffs[1], b)
# S = typeof(aux)
coeffs = Array{HomogeneousPolynomial{Taylor1{T}}}(length(a.coeffs))
@simd for i in eachindex(coeffs)
@inbounds coeffs[i] = a.coeffs[i]
end
@inbounds coeffs[1] = aux
return TaylorN{S}(coeffs, a.order)
return TaylorN(coeffs, a.order)
end
function ($f){T<:Union{Real,Complex}}(b::Taylor1{T},
a::TaylorN{Taylor1{T}})
@inbounds aux = ($f)(b, a.coeffs[1])
S = typeof(aux)
# @inbounds aux = ($f)(b, a.coeffs[1].coeffs[1])
# S = Taylor1{T}
coeffs = Array{HomogeneousPolynomial{S}}(length(a.coeffs))
function ($f){T<:RealOrComplex}(b::Taylor1{T}, a::TaylorN{Taylor1{T}})
@inbounds aux = $f(b, a.coeffs[1].coeffs[1])
# S = typeof(aux)
coeffs = Array{HomogeneousPolynomial{Taylor1{T}}}(length(a.coeffs))
@simd for i in eachindex(coeffs)
@inbounds coeffs[i] = ($f)(a.coeffs[i])
@inbounds coeffs[i] = $f(a.coeffs[i])
end
@inbounds coeffs[1] = aux
return TaylorN{S}(coeffs, a.order)
return TaylorN(coeffs, a.order)
end
end
end
Expand All @@ -204,15 +160,15 @@ end
## Multiplication ##
*(a::Bool, b::Taylor1) = *(promote(a,b)...)
*(a::Taylor1, b::Bool) = b*a
function *(a::Union{Real,Complex}, b::Taylor1)
function *(a::RealOrComplex, b::Taylor1)
@inbounds aux = a * b.coeffs[1]
v = Array{typeof(aux)}(length(b.coeffs))
@simd for i in eachindex(v)
@inbounds v[i] = a * b.coeffs[i]
end
Taylor1(v, b.order)
end
*(a::Taylor1, b::Union{Real,Complex}) = b * a
*(a::Taylor1, b::RealOrComplex) = b * a
doc"""
```
*(a, b)
Expand Down Expand Up @@ -291,7 +247,7 @@ end

*(a::Bool, b::HomogeneousPolynomial) = *(promote(a,b)...)
*(a::HomogeneousPolynomial, b::Bool) = b * a
function *{T<:Union{Real,Complex}}(a::HomogeneousPolynomial, b::T)
function *{T<:RealOrComplex}(a::HomogeneousPolynomial, b::T)
@inbounds aux = a.coeffs[1] * b
S = typeof(aux)
coeffs = Array{S}(length(a.coeffs))
Expand All @@ -300,8 +256,8 @@ function *{T<:Union{Real,Complex}}(a::HomogeneousPolynomial, b::T)
end
return HomogeneousPolynomial{S}(coeffs, a.order)
end
*{T<:Union{Real,Complex}}(b::T, a::HomogeneousPolynomial) = a * b
function *{T<:Union{Real,Complex}}(a::HomogeneousPolynomial{Taylor1{T}},
*{T<:RealOrComplex}(b::T, a::HomogeneousPolynomial) = a * b
function *{T<:RealOrComplex}(a::HomogeneousPolynomial{Taylor1{T}},
b::Taylor1{T})
@inbounds aux = a.coeffs[1] * b
S = typeof(aux)
Expand All @@ -311,13 +267,13 @@ function *{T<:Union{Real,Complex}}(a::HomogeneousPolynomial{Taylor1{T}},
end
return HomogeneousPolynomial{S}(coeffs, a.order)
end
*{T<:Union{Real,Complex}}(b::Taylor1{T},
*{T<:RealOrComplex}(b::Taylor1{T},
a::HomogeneousPolynomial{Taylor1{T}}) = a * b


*(a::Bool, b::TaylorN) = *(promote(a,b)...)
*(a::TaylorN, b::Bool) = b * a
function *{T<:Union{Real,Complex}}(a::TaylorN, b::T)
function *{T<:RealOrComplex}(a::TaylorN, b::T)
@inbounds aux = a.coeffs[1] * b
S = eltype(aux)
coeffs = Array{HomogeneousPolynomial{S}}(length(a.coeffs))
Expand All @@ -326,8 +282,8 @@ function *{T<:Union{Real,Complex}}(a::TaylorN, b::T)
end
return TaylorN{S}(coeffs, a.order)
end
*{T<:Union{Real,Complex}}(b::T, a::TaylorN) = a * b
function *{T<:Union{Real,Complex}}(a::TaylorN{Taylor1{T}}, b::Taylor1{T})
*{T<:RealOrComplex}(b::T, a::TaylorN) = a * b
function *{T<:RealOrComplex}(a::TaylorN{Taylor1{T}}, b::Taylor1{T})
@inbounds aux = a.coeffs[1] * b
S = eltype(aux)
coeffs = Array{HomogeneousPolynomial{S}}(length(a.coeffs))
Expand All @@ -336,7 +292,7 @@ function *{T<:Union{Real,Complex}}(a::TaylorN{Taylor1{T}}, b::Taylor1{T})
end
return TaylorN{S}(coeffs, a.order)
end
*{T<:Union{Real,Complex}}(b::Taylor1{T}, a::TaylorN{Taylor1{T}}) = a * b
*{T<:RealOrComplex}(b::Taylor1{T}, a::TaylorN{Taylor1{T}}) = a * b


"""
Expand Down
3 changes: 3 additions & 0 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#
# Parameters for HomogeneousPolynomial and TaylorN

"Abbreviation for the union of Real and Complex"
const RealOrComplex = Union{Real, Complex}

@doc """
ParamsTaylorN
Expand Down

4 comments on commit cd9cd9f

@dpsanders
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't the union of Real and Complex just Number?

@dpsanders
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, not exactly, eg quaternions?

@dpsanders
Copy link
Contributor

Choose a reason for hiding this comment

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

In any case just putting Number would seem to be reasonable

@dpsanders
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it even need to be restricted at all?

Please sign in to comment.