forked from JuliaDiff/TaylorSeries.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcasting.jl
161 lines (135 loc) · 4.52 KB
/
broadcasting.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# This file is part of TaylorSeries.jl, MIT licensed
#
using TaylorSeries
using Test
@testset "Broadcasting with Taylor1 expansions" begin
t = Taylor1(Int, 5)
# @test t .= t
@test t .== t
@test t .≈ t
@test t .!= (1 + t)
@test 1.0 .+ t == 1.0 + t
@test typeof(1.0 .+ t) == Taylor1{Float64}
@test 1.0 .+ [t] == [1.0 + t]
@test typeof(1.0 .+ [t]) == Vector{Taylor1{Float64}}
@test 1.0 .+ [t, 2t] == [1.0 + t, 1.0 + 2t]
@test [1.0,2.0] .+ [t 2t] == [1.0+t 1.0+2t; 2.0+t 2.0+2t]
@test [1.0] .+ t == t .+ [1.0] == [1.0 + t]
@test 1.0 .* t == t
@test typeof(1.0 .* t) == Taylor1{Float64}
st = sin(t)
@test st .== st
@test st == sin.(t)
@test st.(pi/3) == evaluate(st, pi/3)
@test st(pi/3) == evaluate.(st, pi/3)
@test st.([0.0, pi/3]) == evaluate(st, [0.0, pi/3])
@test typeof(Float32.(t)) == Taylor1{Float32}
@test (Float32.(t))[1] == Float32(1.0)
@test_throws MethodError Float32(t)
# Nested Taylor1 tests
t = Taylor1(Int, 3)
ts = zero(t)
ts .= t
@test ts == t
@. ts = 3 * t^2 - 1
@test ts == 3 * t^2 - 1
# `tt` has to be `Taylor1{Taylor1{Float64}}` (instead of `Taylor1{Taylor1{Int}}`)
# since the method a^n (n integer) is equivalent to `a^float(n).`
tt = Taylor1([zero(1.0*t), one(t)], 2)
tts = zero(tt)
@test tt .== tt
@. tts = 3 * tt^2 - 1
@test tts == 3 * tt^2 - 1
ttt = Taylor1([zero(tt), one(tt)])
ttts = zero(ttt)
@test ttt .≈ ttt
@. ttts = 3 * ttt^1 - 1
@test ttts == 3 * ttt^1 - 1
@. ttts = 3 * ttt^3 - 1
@test ttts == - 1.0
end
@testset "Broadcasting with HomogeneousPolynomial and TaylorN" begin
x, y = set_variables("x y", order=3)
xH = x[1]
yH = y[1]
@test xH .== xH
@test yH .≈ yH
@test xH .== xH
@test x[2] .== y[2]
xHs = zero(xH)
xHs .= xH
@test xHs == xH
@. xHs = 2 * xH + yH
@test xHs == 2 * xH + yH
@test 1 .* xH == xH
@test 1 .* [xH] == [xH]
@test [1] .* xH == xH .* [1] == [xH]
@test x .== x
@test y .≈ y
@test x .!= (1 + x)
@test typeof(Float32.(x)) == TaylorN{Float32}
@test (Float32.(x))[1] == HomogeneousPolynomial(Float32[1.0, 0.0])
@test_throws MethodError Float32(x)
p = zero(x)
p .= x
@test p == x
@. p = 1 + 2*x + 3x^2 - x * y
@test p == 1 + 2*x + 3*x^2 - x * y
@test 1.0 .+ x == 1.0 + x
@test y .+ x == y + x
@test typeof(big"1.0" .+ x) == TaylorN{BigFloat}
@test 1.0 .+ [x] == [1.0 + x]
@test typeof(1.0 .+ [y]) == Vector{TaylorN{Float64}}
@test 1.0 .+ [x, 2y] == [1.0 + x, 1.0 + 2y]
@test [1.0,2.0] .+ [x 2y] == [1.0+x 1.0+2y; 2.0+x 2.0+2y]
@test [1.0] .+ x == x .+ [1.0] == [1.0 + x]
@test 1.0 .* y == y
@test typeof(1.0 .* x .* y) == TaylorN{Float64}
end
@testset "Broadcasting with mixtures Taylor1{TalorN{T}}" begin
x, y = set_variables("x", numvars=2, order=6)
tN = Taylor1(TaylorN{Float64}, 3)
@test tN .== tN
@test tN .≈ tN
@test tN .!= (1 + tN)
@test 1.0 .+ tN == 1.0 + tN
@test typeof(1.0 .+ tN) == Taylor1{TaylorN{Float64}}
@test 1.0 .+ [tN] == [1.0 + tN]
@test typeof(1.0 .+ [tN]) == Vector{Taylor1{TaylorN{Float64}}}
@test 1.0 .+ [tN, 2tN] == [1.0 + tN, 1.0 + 2tN]
@test [1.0,2.0] .+ [tN 2tN] == [1.0+tN 1.0+2tN; 2.0+tN 2.0+2tN]
@test [1.0] .+ tN == tN .+ [1.0] == [1.0 + tN]
@test 1.0 .* tN == 1.0 * tN
@test typeof(1.0 .* tN) == Taylor1{TaylorN{Float64}}
tNs = zero(tN)
tNs .= tN
@test tNs == tN
@. tNs = y[1] * tN^2 - 1
@test tNs == y[1] * tN^2 - 1
@. tNs = y * tN^2 - 1
@test tNs == y * tN^2 - 1
end
@testset "Broadcasting with mixtures TaylorN{Talor1{T}}" begin
set_variables("x", numvars=2, order=6)
t = Taylor1(3)
xHt = HomogeneousPolynomial([one(t), zero(t)])
yHt = HomogeneousPolynomial([zero(t), t])
tN1 = TaylorN([HomogeneousPolynomial([t]),xHt,yHt^2])
@test tN1 .== tN1
@test tN1 .≈ tN1
@test tN1 .!= (1 + tN1)
@test 1.0 .+ tN1 == 1.0 + tN1
@test typeof(1.0 .+ tN1) == TaylorN{Taylor1{Float64}}
@test 1.0 .+ [tN1] == [1.0 + tN1]
@test typeof(1.0 .+ [tN1]) == Vector{TaylorN{Taylor1{Float64}}}
@test 1.0 .+ [tN1, 2tN1] == [1.0 + tN1, 1.0 + 2tN1]
@test [1.0, 2.0] .+ [tN1 2tN1] == [1.0+tN1 1.0+2tN1; 2.0+tN1 2.0+2tN1]
@test [1.0] .+ tN1 == tN1 .+ [1.0] == [1.0 + tN1]
@test 1.0 .* tN1 == tN1
@test typeof(1.0 .* tN1) == TaylorN{Taylor1{Float64}}
tN1s = zero(tN1)
tN1s .= tN1
@test tN1s == tN1
@. tN1s = t * tN1^2 - 1
@test tN1s == t * tN1^2 - 1
end