@@ -30,7 +30,7 @@ ForwardDiff.:≺(::Int, ::Type{TestTag()}) = false
30
30
ForwardDiff.:≺ (:: Type{TestTag} , :: Type{OuterTestTag} ) = true
31
31
ForwardDiff.:≺ (:: Type{OuterTestTag} , :: Type{TestTag} ) = false
32
32
33
- for N in (0 ,3 ), M in (0 ,4 ), V in (Int, Float32)
33
+ @testset " Dual{Z, $V , $N } and Dual{Z,Dual{Z, $V , $M }, $N } " for N in (0 ,3 ), M in (0 ,4 ), V in (Int, Float32)
34
34
println (" ...testing Dual{TestTag(),$V ,$N } and Dual{TestTag(),Dual{TestTag(),$V ,$M },$N }" )
35
35
36
36
PARTIALS = Partials {N,V} (ntuple (n -> intrand (V), N))
@@ -44,6 +44,13 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
44
44
PARTIALS3 = Partials {N,V} (ntuple (n -> intrand (V), N))
45
45
PRIMAL3 = intrand (V)
46
46
FDNUM3 = Dual {TestTag()} (PRIMAL3, PARTIALS3)
47
+
48
+ if ! allunique ([PRIMAL, PRIMAL2, PRIMAL3])
49
+ @info " testing with non-unique primals" PRIMAL PRIMAL2 PRIMAL3
50
+ end
51
+ if N > 0 && ! allunique ([PARTIALS, PARTIALS2, PARTIALS3])
52
+ @info " testing with non-unique partials" PARTIALS PARTIALS2 PARTIALS3
53
+ end
47
54
48
55
M_PARTIALS = Partials {M,V} (ntuple (m -> intrand (V), M))
49
56
NESTED_PARTIALS = convert (Partials{N,Dual{TestTag (),V,M}}, PARTIALS)
@@ -231,15 +238,27 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
231
238
@test ForwardDiff. isconstant (one (NESTED_FDNUM))
232
239
@test ForwardDiff. isconstant (NESTED_FDNUM) == (N == 0 )
233
240
234
- @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2))
235
- @test isequal (PRIMAL, PRIMAL2) == isequal (FDNUM, FDNUM2)
236
-
237
- @test isequal (NESTED_FDNUM, Dual {TestTag()} (Dual {TestTag()} (PRIMAL, M_PARTIALS2), NESTED_PARTIALS2))
238
- @test isequal (PRIMAL, PRIMAL2) == isequal (NESTED_FDNUM, NESTED_FDNUM2)
239
-
240
- @test FDNUM == Dual {TestTag()} (PRIMAL, PARTIALS2)
241
- @test (PRIMAL == PRIMAL2) == (FDNUM == FDNUM2)
242
- @test (PRIMAL == PRIMAL2) == (NESTED_FDNUM == NESTED_FDNUM2)
241
+ # Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
242
+ # and FDNUM2 has everything with a 2, and all random numbers nonzero.
243
+ # M is the length of M_PARTIALS, which affects:
244
+ # NESTED_FDNUM = Dual{TestTag()}(Dual{TestTag()}(PRIMAL, M_PARTIALS), NESTED_PARTIALS)
245
+
246
+ @test (FDNUM == Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
247
+ @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
248
+ @test isequal (NESTED_FDNUM, Dual {TestTag()} (Dual {TestTag()} (PRIMAL, M_PARTIALS2), NESTED_PARTIALS2)) == ((M_PARTIALS == M_PARTIALS2) && (NESTED_PARTIALS == NESTED_PARTIALS2))
249
+
250
+ if PRIMAL == PRIMAL2
251
+ @test isequal (FDNUM, Dual {TestTag()} (PRIMAL, PARTIALS2)) == (PARTIALS == PARTIALS2)
252
+ @test isequal (FDNUM, FDNUM2) == (PARTIALS == PARTIALS2)
253
+
254
+ @test (FDNUM == FDNUM2) == (PARTIALS == PARTIALS2)
255
+ @test (NESTED_FDNUM == NESTED_FDNUM2) == ((M_PARTIALS == M_PARTIALS2) && (NESTED_PARTIALS == NESTED_PARTIALS2))
256
+ else
257
+ @test ! isequal (FDNUM, FDNUM2)
258
+
259
+ @test FDNUM != FDNUM2
260
+ @test NESTED_FDNUM != NESTED_FDNUM2
261
+ end
243
262
244
263
@test isless (Dual {TestTag()} (1 , PARTIALS), Dual {TestTag()} (2 , PARTIALS2))
245
264
@test ! (isless (Dual {TestTag()} (1 , PARTIALS), Dual {TestTag()} (1 , PARTIALS2)))
@@ -344,7 +363,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
344
363
@test typeof (WIDE_NESTED_FDNUM) === Dual{TestTag (),Dual{TestTag (),WIDE_T,M},N}
345
364
346
365
@test value (WIDE_FDNUM) == PRIMAL
347
- @test value (WIDE_NESTED_FDNUM) == PRIMAL
366
+ @test ( value (WIDE_NESTED_FDNUM) == PRIMAL) == (M == 0 )
348
367
349
368
@test convert (Dual, FDNUM) === FDNUM
350
369
@test convert (Dual, NESTED_FDNUM) === NESTED_FDNUM
@@ -395,6 +414,8 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
395
414
# ----------#
396
415
397
416
if M > 0 && N > 0
417
+ # Recall that FDNUM = Dual{TestTag()}(PRIMAL, PARTIALS) has N partials,
418
+ # all random numbers nonzero, and FDNUM2 another draw. M only affects NESTED_FDNUM.
398
419
@test Dual {1} (FDNUM) / Dual {1} (PRIMAL) === Dual {1} (FDNUM / PRIMAL)
399
420
@test Dual {1} (PRIMAL) / Dual {1} (FDNUM) === Dual {1} (PRIMAL / FDNUM)
400
421
@test_broken Dual {1} (FDNUM) / FDNUM2 === Dual {1} (FDNUM / FDNUM2)
@@ -413,6 +434,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
413
434
414
435
# Exponentiation #
415
436
# ----------------#
437
+
416
438
# If V == Int, the LHS terms are Int's. Large inputs cause integer overflow
417
439
# within the generic fallback of `isapprox`, resulting in a DomainError.
418
440
# Promote to Float64 to avoid issues.
@@ -442,7 +464,7 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
442
464
@test abs (NESTED_FDNUM) === NESTED_FDNUM
443
465
444
466
if V != Int
445
- for (M, f, arity) in DiffRules. diffrules (filter_modules = nothing )
467
+ @testset " $f " for (M, f, arity) in DiffRules. diffrules (filter_modules = nothing )
446
468
if f in (:/ , :rem2pi )
447
469
continue # Skip these rules
448
470
elseif ! (isdefined (@__MODULE__ , M) && isdefined (getfield (@__MODULE__ , M), f))
@@ -502,10 +524,14 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
502
524
else
503
525
@test dx isa Complex{<: Dual{TestTag()} }
504
526
@test dy isa Complex{<: Dual{TestTag()} }
505
- @test real (value (dx)) == real (actualval)
506
- @test real (value (dy)) == real (actualval)
507
- @test imag (value (dx)) == imag (actualval)
508
- @test imag (value (dy)) == imag (actualval)
527
+ # @test real(value(dx)) == real(actualval)
528
+ # @test real(value(dy)) == real(actualval)
529
+ # @test imag(value(dx)) == imag(actualval)
530
+ # @test imag(value(dy)) == imag(actualval)
531
+ @test value (real (dx)) == real (actualval)
532
+ @test value (real (dy)) == real (actualval)
533
+ @test value (imag (dx)) == imag (actualval)
534
+ @test value (imag (dy)) == imag (actualval)
509
535
@test partials (real (dx), 1 ) ≈ real (actualdx) nans= true
510
536
@test partials (real (dy), 1 ) ≈ real (actualdy) nans= true
511
537
@test partials (imag (dx), 1 ) ≈ imag (actualdx) nans= true
@@ -568,6 +594,10 @@ for N in (0,3), M in (0,4), V in (Int, Float32)
568
594
end
569
595
end
570
596
597
+ # ############
598
+ # bug fixes #
599
+ # ############
600
+
571
601
@testset " Exponentiation of zero" begin
572
602
x0 = 0.0
573
603
x1 = Dual {:t1} (x0, 1.0 )
0 commit comments