@@ -4,6 +4,20 @@ import Base: convert, promote_rule
44
55export TaylorScalar
66
7+ """
8+ TaylorDiff.can_taylor(V::Type)
9+
10+ Determines whether the type V is allowed as the scalar type in a
11+ Dual. By default, only `<:Real` types are allowed.
12+ """
13+ can_taylorize (:: Type{<:Real} ) = true
14+ can_taylorize (:: Type ) = false
15+
16+ @noinline function throw_cannot_taylorize (V:: Type )
17+ throw (ArgumentError (" Cannot create a Taylor polynomial over scalar type $V ." *
18+ " If the type behaves as a scalar, define TaylorDiff.can_taylorize(::Type{$V }) = true." ))
19+ end
20+
721"""
822 TaylorScalar{T, N}
923
@@ -13,20 +27,23 @@ Representation of Taylor polynomials.
1327
1428- `value::NTuple{N, T}`: i-th element of this stores the (i-1)-th derivative
1529"""
16- struct TaylorScalar{T, N}
30+ struct TaylorScalar{T, N} <: Real
1731 value:: NTuple{N, T}
32+ function TaylorScalar {T, N} (value:: NTuple{N, T} ) where {T, N}
33+ can_taylorize (T) || throw_cannot_taylorize (T)
34+ new {T, N} (value)
35+ end
1836end
1937
20- TN = Union{TaylorScalar, Number}
21-
22- @inline TaylorScalar (xs:: Vararg{T, N} ) where {T, N} = TaylorScalar (xs)
38+ TaylorScalar (value:: NTuple{N, T} ) where {T, N} = TaylorScalar {T, N} (value)
39+ TaylorScalar (value:: Vararg{T, N} ) where {T, N} = TaylorScalar {T, N} (value)
2340
2441"""
2542 TaylorScalar{T, N}(x::T) where {T, N}
2643
2744Construct a Taylor polynomial with zeroth order coefficient.
2845"""
29- @generated function TaylorScalar {T, N} (x:: T ) where {T, N}
46+ @generated function TaylorScalar {T, N} (x:: S ) where {T, S <: Real , N}
3047 return quote
3148 $ (Expr (:meta , :inline ))
3249 TaylorScalar ((T (x), $ (zeros (T, N - 1 )... )))
3855
3956Construct a Taylor polynomial with zeroth and first order coefficient, acting as a seed.
4057"""
41- @generated function TaylorScalar {T, N} (x:: T , d:: T ) where {T, N}
58+ @generated function TaylorScalar {T, N} (x:: S , d:: S ) where {T, S <: Real , N}
4259 return quote
4360 $ (Expr (:meta , :inline ))
4461 TaylorScalar ((T (x), T (d), $ (zeros (T, N - 2 )... )))
6885end
6986@inline primal (t:: TaylorScalar ) = extract_derivative (t, 1 )
7087
71- @inline zero (:: Type{TaylorScalar{T, N}} ) where {T, N} = TaylorScalar {T, N} (zero (T))
72- @inline one (:: Type{TaylorScalar{T, N}} ) where {T, N} = TaylorScalar {T, N} (one (T))
73- @inline zero (:: TaylorScalar{T, N} ) where {T, N} = zero (TaylorScalar{T, N})
74- @inline one (:: TaylorScalar{T, N} ) where {T, N} = one (TaylorScalar{T, N})
75-
76- adjoint (t:: TaylorScalar ) = t
77- conj (t:: TaylorScalar ) = t
78-
7988function promote_rule (:: Type{TaylorScalar{T, N}} ,
8089 :: Type{S} ) where {T, S, N}
8190 TaylorScalar{promote_type (T, S), N}
8291end
8392
84- # Number-like convention (I patched them after removing <: Number)
85-
86- convert (:: Type{TaylorScalar{T, N}} , x:: TaylorScalar{T, N} ) where {T, N} = x
87- function convert (:: Type{TaylorScalar{T, N}} , x:: S ) where {T, S, N}
88- TaylorScalar {T, N} (convert (T, x))
89- end
90- for op in (:+ , :- , :* , :/ )
91- @eval @inline $ op (a:: TaylorScalar , b:: Number ) = $ op (promote (a, b)... )
92- @eval @inline $ op (a:: Number , b:: TaylorScalar ) = $ op (promote (a, b)... )
93- end
94- transpose (t:: TaylorScalar ) = t
95-
9693function Base. AbstractFloat (x:: TaylorScalar{T, N} ) where {T, N}
9794 TaylorScalar {Float64, N} (convert (NTuple{N, Float64}, x. value))
9895end
0 commit comments