Skip to content

Commit a2e880b

Browse files
lrnvtansongchen
authored andcommitted
Adding convenient promotions
1 parent 54af72f commit a2e880b

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/primitive.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ end
106106
ex = :($ex; TaylorScalar($([Symbol('v', i) for i in 1:N]...)))
107107
return :(@inbounds $ex)
108108
end
109+
@inline *(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1,T2,N} = *(promote(a,b)...)
110+
@inline /(a::TaylorScalar{T1, N}, b::TaylorScalar{T2, N}) where {T1,T2,N} = *(promote(a,b)...)
109111

110112
@generated function ^(t::TaylorScalar{T, N}, n::S) where {S <: Number, T, N}
111113
ex = quote

src/scalar.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,19 @@ function promote_rule(::Type{TaylorScalar{T, N}},
7272
::Type{S}) where {T, S, N}
7373
TaylorScalar{promote_type(T, S), N}
7474
end
75+
function promote_rule(::Type{TaylorScalar{T1, N}}, ::Type{TaylorScalar{T2,N}}) where {T1, T2, N}
76+
TaylorScalar{promote_type(T1,T2), N}
77+
end
7578

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

7881
convert(::Type{TaylorScalar{T, N}}, x::TaylorScalar{T, N}) where {T, N} = x
7982
function convert(::Type{TaylorScalar{T, N}}, x::S) where {T, S, N}
8083
TaylorScalar{T, N}(convert(T, x))
8184
end
85+
function convert(::Type{TaylorScalar{T1,N}},x::TaylorScalar{T2,N}) where {T1,T2,N}
86+
TaylorScalar{T1,N}(convert.(T1,value(x)))
87+
end
8288
@inline +(a::Number, b::TaylorScalar) = TaylorScalar((a + value(b)[1]), value(b)[2:end]...)
8389
@inline -(a::Number, b::TaylorScalar) = TaylorScalar((a - value(b)[1]), .-value(b)[2:end]...)
8490
@inline *(a::Number, b::TaylorScalar) = TaylorScalar((a .* value(b))...)

0 commit comments

Comments
 (0)