Skip to content

Commit

Permalink
Modify scalar rules for +,-,/,*
Browse files Browse the repository at this point in the history
  • Loading branch information
lrnv authored and tansongchen committed Feb 24, 2023
1 parent b03b101 commit 54af72f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ 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
for op in (:+, :-, :*, :/)
@eval @inline $op(a::TaylorScalar, b::Number) = $op(promote(a, b)...)
@eval @inline $op(a::Number, b::TaylorScalar) = $op(promote(a, b)...)
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))...)
@inline /(a::Number, b::TaylorScalar) = /(promote(a, b)...)

@inline +(a::TaylorScalar, b::Number) = TaylorScalar((value(a)[1] + b), value(a)[2:end]...)
@inline -(a::TaylorScalar, b::Number) = TaylorScalar((value(a)[1] - b), value(a)[2:end]...)
@inline *(a::TaylorScalar, b::Number) = TaylorScalar((value(a) .* b)...)
@inline /(a::TaylorScalar, b::Number) = TaylorScalar((value(a) ./ b)...)

transpose(t::TaylorScalar) = t

0 comments on commit 54af72f

Please sign in to comment.