Skip to content

Commit

Permalink
Adding convenient promotions
Browse files Browse the repository at this point in the history
  • Loading branch information
lrnv authored and tansongchen committed Feb 24, 2023
1 parent 54af72f commit a2e880b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/primitive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ end
ex = :($ex; TaylorScalar($([Symbol('v', i) for i in 1:N]...)))
return :(@inbounds $ex)
end
@inline *(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1,T2,N} = *(promote(a,b)...)
@inline /(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1,T2,N} = *(promote(a,b)...)

@generated function ^(t::TaylorScalar{T, N}, n::S) where {S <: Number, T, N}
ex = quote
Expand Down
6 changes: 6 additions & 0 deletions src/scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ function promote_rule(::Type{TaylorScalar{T, N}},
::Type{S}) where {T, S, N}
TaylorScalar{promote_type(T, S), N}
end
function promote_rule(::Type{TaylorScalar{T1, N}}, ::Type{TaylorScalar{T2,N}}) where {T1, T2, N}
TaylorScalar{promote_type(T1,T2), N}
end

# Number-like convention (I patched them after removing <: Number)

convert(::Type{TaylorScalar{T, N}}, x::TaylorScalar{T, N}) where {T, N} = x
function convert(::Type{TaylorScalar{T, N}}, x::S) where {T, S, N}
TaylorScalar{T, N}(convert(T, x))
end
function convert(::Type{TaylorScalar{T1,N}},x::TaylorScalar{T2,N}) where {T1,T2,N}
TaylorScalar{T1,N}(convert.(T1,value(x)))
end
@inline +(a::Number, b::TaylorScalar) = TaylorScalar((a + value(b)[1]), value(b)[2:end]...)
@inline -(a::Number, b::TaylorScalar) = TaylorScalar((a - value(b)[1]), .-value(b)[2:end]...)
@inline *(a::Number, b::TaylorScalar) = TaylorScalar((a .* value(b))...)
Expand Down

0 comments on commit a2e880b

Please sign in to comment.