Skip to content

Commit 6dedcac

Browse files
committed
Go back to <: Real
1 parent 3770f8b commit 6dedcac

7 files changed

Lines changed: 74 additions & 67 deletions

File tree

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ makedocs(;
1515
assets = String[]),
1616
pages = [
1717
"Home" => "index.md",
18-
"API" => "api.md",
18+
"API" => "api.md"
1919
])
2020

2121
deploydocs(;

examples/halley.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ using TaylorDiff
44
using LinearAlgebra
55
using LinearSolve
66

7-
function newton(f, x0, p; tol=1e-10, maxiter=100)
7+
function newton(f, x0, p; tol = 1e-10, maxiter = 100)
88
x = x0
99
for i in 1:maxiter
1010
fx = f(x, p)
@@ -22,7 +22,7 @@ function newton(f, x0, p; tol=1e-10, maxiter=100)
2222
return x
2323
end
2424

25-
function halley(f, x0, p; tol=1e-10, maxiter=100)
25+
function halley(f, x0, p; tol = 1e-10, maxiter = 100)
2626
x = x0
2727
for i in 1:maxiter
2828
fx = f(x, p)

examples/integration.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ eqsdiff: RHS
1717
t: constructed by TaylorScalar{T, N}(t0, 1), which means unit perturbation
1818
x0: initial value
1919
"""
20-
function jetcoeffs_taylordiff(eqsdiff::Function, t::TaylorScalar{T, N}, x0::U, params) where
21-
{T<:Real, U<:Number, N}
20+
function jetcoeffs_taylordiff(eqsdiff::Function, t::TaylorScalar{T, N}, x0::U,
21+
params) where
22+
{T <: Real, U <: Number, N}
2223
x = TaylorScalar{U, N}(x0) # x.values[1] is defined, others are 0
23-
for index in 1:N-1 # computes x.values[index + 1]
24+
for index in 1:(N - 1) # computes x.values[index + 1]
2425
f = eqsdiff(x, params, t)
2526
df = TaylorDiff.extract_derivative(f, index)
2627
x = update_coefficient(x, index + 1, df)
@@ -35,11 +36,13 @@ eqsdiff!: RHS, in non-allocation form
3536
t: constructed by TaylorScalar{T, N}(t0, 1), which means unit perturbation
3637
x0: initial value
3738
"""
38-
function jetcoeffs_array_taylordiff(eqsdiff!::Function, t::TaylorScalar{T, N}, x0::AbstractArray{U, D}, params) where
39-
{T<:Real, U<:Number, N, D}
39+
function jetcoeffs_array_taylordiff(
40+
eqsdiff!::Function, t::TaylorScalar{T, N}, x0::AbstractArray{U, D},
41+
params) where
42+
{T <: Real, U <: Number, N, D}
4043
x = map(TaylorScalar{U, N}, x0) # x.values[1] is defined, others are 0
4144
f = similar(x)
42-
for index in 1:N-1 # computes x.values[index + 1]
45+
for index in 1:(N - 1) # computes x.values[index + 1]
4346
eqsdiff!(f, x, params, t)
4447
df = TaylorDiff.extract_derivative.(f, index)
4548
x = update_coefficient.(x, index + 1, df)

src/derivative.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ function derivatives end
4242
# Core APIs
4343

4444
# Added to help Zygote infer types
45-
@inline function make_seed(x::T, l::S, ::Val{N}) where {T <: TN, S <: TN, N}
45+
@inline function make_seed(x::T, l::S, ::Val{N}) where {T <: Real, S <: Real, N}
4646
TaylorScalar{T, N}(x, convert(T, l))
4747
end
4848

49-
@inline function make_seed(x::AbstractArray{T}, l, vN::Val{N}) where {T <: TN, N}
49+
@inline function make_seed(x::AbstractArray{T}, l, vN::Val{N}) where {T <: Real, N}
5050
broadcast(make_seed, x, l, vN)
5151
end
5252

src/primitive.jl

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ end
7474

7575
# Binary
7676

77+
const AMBIGUOUS_TYPES = (AbstractFloat, Irrational, Integer, Rational, Real, RoundingMode)
78+
7779
for op in [:>, :<, :(==), :(>=), :(<=)]
78-
@eval @inline $op(a::Number, b::TaylorScalar) = $op(a, value(b)[1])
79-
@eval @inline $op(a::TaylorScalar, b::Number) = $op(value(a)[1], b)
80+
for R in AMBIGUOUS_TYPES
81+
@eval @inline $op(a::TaylorScalar, b::$R) = $op(value(a)[1], b)
82+
@eval @inline $op(a::$R, b::TaylorScalar) = $op(a, value(b)[1])
83+
end
8084
@eval @inline $op(a::TaylorScalar, b::TaylorScalar) = $op(value(a)[1], value(b)[1])
8185
end
8286

@@ -110,41 +114,43 @@ end
110114
return :(@inbounds $ex)
111115
end
112116

113-
@generated function ^(t::TaylorScalar{T, N}, n::S) where {S <: Number, T, N}
114-
ex = quote
115-
v = value(t)
116-
w11 = 1
117-
u1 = ^(v[1], n)
118-
end
119-
for k in 1:N
117+
for R in (Integer, Real)
118+
@eval @generated function ^(t::TaylorScalar{T, N}, n::S) where {S <: $R, T, N}
120119
ex = quote
121-
$ex
122-
$(Symbol('p', k)) = ^(v[1], n - $(k - 1))
120+
v = value(t)
121+
w11 = 1
122+
u1 = ^(v[1], n)
123123
end
124-
end
125-
for i in 2:N
126-
subex = quote
127-
$(Symbol('w', i, 1)) = 0
124+
for k in 1:N
125+
ex = quote
126+
$ex
127+
$(Symbol('p', k)) = ^(v[1], n - $(k - 1))
128+
end
128129
end
129-
for k in 2:i
130+
for i in 2:N
130131
subex = quote
132+
$(Symbol('w', i, 1)) = 0
133+
end
134+
for k in 2:i
135+
subex = quote
136+
$subex
137+
$(Symbol('w', i, k)) = +($([:((n * $(binomial(i - 2, j - 1)) -
138+
$(binomial(i - 2, j - 2))) *
139+
$(Symbol('w', j, k - 1)) *
140+
v[$(i + 1 - j)])
141+
for j in (k - 1):(i - 1)]...))
142+
end
143+
end
144+
ex = quote
145+
$ex
131146
$subex
132-
$(Symbol('w', i, k)) = +($([:((n * $(binomial(i - 2, j - 1)) -
133-
$(binomial(i - 2, j - 2))) *
134-
$(Symbol('w', j, k - 1)) *
135-
v[$(i + 1 - j)])
136-
for j in (k - 1):(i - 1)]...))
147+
$(Symbol('u', i)) = +($([:($(Symbol('w', i, k)) * $(Symbol('p', k)))
148+
for k in 2:i]...))
137149
end
138150
end
139-
ex = quote
140-
$ex
141-
$subex
142-
$(Symbol('u', i)) = +($([:($(Symbol('w', i, k)) * $(Symbol('p', k)))
143-
for k in 2:i]...))
144-
end
151+
ex = :($ex; TaylorScalar($([Symbol('u', i) for i in 1:N]...)))
152+
return :(@inbounds $ex)
145153
end
146-
ex = :($ex; TaylorScalar($([Symbol('u', i) for i in 1:N]...)))
147-
return :(@inbounds $ex)
148154
end
149155

150156
@generated function raise(f::T, df::TaylorScalar{T, M},

src/scalar.jl

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import Base: convert, promote_rule
44

55
export 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
1836
end
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
2744
Construct 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)...)))
@@ -38,7 +55,7 @@ end
3855
3956
Construct 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)...)))
@@ -68,31 +85,11 @@ end
6885
end
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-
7988
function promote_rule(::Type{TaylorScalar{T, N}},
8089
::Type{S}) where {T, S, N}
8190
TaylorScalar{promote_type(T, S), N}
8291
end
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-
9693
function Base.AbstractFloat(x::TaylorScalar{T, N}) where {T, N}
9794
TaylorScalar{Float64, N}(convert(NTuple{N, Float64}, x.value))
9895
end

test/primitive.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ end
1414

1515
@testset "Unary functions" begin
1616
some_number = 3.7
17-
for f in (x -> exp(x^2), expm1, exp2, exp10, x -> sin(x^2), x -> cos(x^2), sinpi, cospi,
17+
for f in (
18+
x -> exp(x^2), expm1, exp2, exp10, x -> sin(x^2), x -> cos(x^2), sinpi, cospi,
1819
sqrt, cbrt,
1920
inv), order in (1, 4)
2021
fdm = central_fdm(12, order)

0 commit comments

Comments
 (0)