@@ -329,20 +329,32 @@ end
329
329
# type, and produce numerically correct results.
330
330
as_array (x:: AbstractArray ) = Array (x)
331
331
as_array (x:: UniformScaling ) = x
332
- function test_addition_and_subtraction (As, Bs, Tout:: Type )
332
+ equal_or_undef (a:: Number , b:: Number ) = (a == b) || isequal (a, b)
333
+ equal_or_undef (a, b) = all (equal_or_undef .(a, b))
334
+ function test_addition_subtraction_dot (As, Bs, Tout:: Type )
333
335
for A in As, B in Bs
334
- @testset " $(typeof (A)) ± $(typeof (B)) " begin
336
+ @testset " $(typeof (A)) and $(typeof (B)) " begin
335
337
@test A + B isa Tout{promote_type (eltype (A), eltype (B))}
336
- @test as_array (A + B) == as_array (A) + as_array (B)
338
+ @test equal_or_undef ( as_array (A + B), as_array (A) + as_array (B) )
337
339
338
340
@test A - B isa Tout{promote_type (eltype (A), eltype (B))}
339
- @test as_array (A - B) == as_array (A) - as_array (B)
341
+ @test equal_or_undef ( as_array (A - B), as_array (A) - as_array (B) )
340
342
341
343
@test B + A isa Tout{promote_type (eltype (B), eltype (A))}
342
- @test as_array (B + A) == as_array (B) + as_array (A)
344
+ @test equal_or_undef ( as_array (B + A), as_array (B) + as_array (A) )
343
345
344
346
@test B - A isa Tout{promote_type (eltype (B), eltype (A))}
345
- @test as_array (B - A) == as_array (B) - as_array (A)
347
+ @test equal_or_undef (as_array (B - A), as_array (B) - as_array (A))
348
+
349
+ # Julia 1.6 doesn't support dot(UniformScaling)
350
+ if VERSION < v " 1.6.0" || VERSION >= v " 1.8.0"
351
+ d1 = dot (A, B)
352
+ d2 = dot (as_array (A), as_array (B))
353
+ d3 = dot (B, A)
354
+ d4 = dot (as_array (B), as_array (A))
355
+ @test d1 ≈ d2 || d1 ≡ d2
356
+ @test d3 ≈ d4 || d3 ≡ d4
357
+ end
346
358
end
347
359
end
348
360
end
@@ -372,37 +384,37 @@ end
372
384
@test - A_fill === Fill (- A_fill. value, 5 )
373
385
374
386
# FillArray +/- FillArray should construct a new FillArray.
375
- test_addition_and_subtraction ((A_fill, B_fill), (A_fill, B_fill), Fill)
387
+ test_addition_subtraction_dot ((A_fill, B_fill), (A_fill, B_fill), Fill)
376
388
test_addition_and_subtraction_dim_mismatch (A_fill, Fill (randn (rng), 5 , 2 ))
377
389
378
390
# FillArray + Array (etc) should construct a new Array using `getindex`.
379
- A_dense, B_dense = randn (rng, 5 ), [5 , 4 , 3 , 2 , 1 ]
380
- test_addition_and_subtraction ((A_fill, B_fill), (A_dense, B_dense) , Array)
391
+ B_dense = ( randn (rng, 5 ), [5 , 4 , 3 , 2 , 1 ], fill ( Inf , 5 ), fill ( NaN , 5 ))
392
+ test_addition_subtraction_dot ((A_fill, B_fill), B_dense, Array)
381
393
test_addition_and_subtraction_dim_mismatch (A_fill, randn (rng, 5 , 2 ))
382
394
383
395
# FillArray + StepLenRange / UnitRange (etc) should yield an AbstractRange.
384
396
A_ur, B_ur = 1.0 : 5.0 , 6 : 10
385
- test_addition_and_subtraction ((A_fill, B_fill), (A_ur, B_ur), AbstractRange)
397
+ test_addition_subtraction_dot ((A_fill, B_fill), (A_ur, B_ur), AbstractRange)
386
398
test_addition_and_subtraction_dim_mismatch (A_fill, 1.0 : 6.0 )
387
399
test_addition_and_subtraction_dim_mismatch (A_fill, 5 : 10 )
388
400
389
401
# FillArray + UniformScaling should yield a Matrix in general
390
402
As_fill_square = (Fill (randn (rng, Float64), 3 , 3 ), Fill (5 , 4 , 4 ))
391
403
Bs_us = (UniformScaling (2.3 ), UniformScaling (3 ))
392
- test_addition_and_subtraction (As_fill_square, Bs_us, Matrix)
404
+ test_addition_subtraction_dot (As_fill_square, Bs_us, Matrix)
393
405
As_fill_nonsquare = (Fill (randn (rng, Float64), 3 , 2 ), Fill (5 , 3 , 4 ))
394
406
for A in As_fill_nonsquare, B in Bs_us
395
407
test_addition_and_subtraction_dim_mismatch (A, B)
396
408
end
397
409
398
410
# FillArray + StaticArray should not have ambiguities
399
411
A_svec, B_svec = SVector {5} (rand (5 )), SVector (1 , 2 , 3 , 4 , 5 )
400
- test_addition_and_subtraction ((A_fill, B_fill, Zeros (5 )), (A_svec, B_svec), SVector{5 })
412
+ test_addition_subtraction_dot ((A_fill, B_fill, Zeros (5 )), (A_svec, B_svec), SVector{5 })
401
413
402
414
# Issue #224
403
415
A_matmat, B_matmat = Fill (rand (3 ,3 ),5 ), [rand (3 ,3 ) for n= 1 : 5 ]
404
- test_addition_and_subtraction ((A_matmat,), (A_matmat,), Fill)
405
- test_addition_and_subtraction ((B_matmat,), (A_matmat,), Vector)
416
+ test_addition_subtraction_dot ((A_matmat,), (A_matmat,), Fill)
417
+ test_addition_subtraction_dot ((B_matmat,), (A_matmat,), Vector)
406
418
407
419
# Optimizations for Zeros and RectOrDiagonal{<:Any, <:AbstractFill}
408
420
As_special_square = (
412
424
RectDiagonal (Fill (randn (rng, Float64), 3 ), 3 , 3 ), RectDiagonal (Fill (3 , 4 ), 4 , 4 )
413
425
)
414
426
DiagonalAbstractFill{T} = Diagonal{T, <: AbstractFill{T, 1} }
415
- test_addition_and_subtraction (As_special_square, Bs_us, DiagonalAbstractFill)
427
+ test_addition_subtraction_dot (As_special_square, Bs_us, DiagonalAbstractFill)
416
428
As_special_nonsquare = (
417
429
Zeros (3 , 2 ), Zeros {Int} (3 , 4 ),
418
430
Eye (3 , 2 ), Eye {Int} (3 , 4 ),
537
549
@test [SVector (1 ,2 )' , SVector (2 ,3 )' , SVector (3 ,4 )' ]' * Zeros {Int} (3 ) === SVector (0 ,0 )
538
550
@test_throws DimensionMismatch randn (4 )' * Zeros (3 )
539
551
@test Zeros (5 )' * randn (5 ,3 ) ≡ Zeros (5 )' * Zeros (5 ,3 ) ≡ Zeros (5 )' * Ones (5 ,3 ) ≡ Zeros (3 )'
540
- @test Zeros (5 )' * randn (5 ) ≡ Zeros (5 )' * Zeros (5 ) ≡ Zeros (5 )' * Ones (5 ) ≡ 0.0
552
+ @test abs ( Zeros (5 )' * randn (5 )) ≡ abs ( Zeros (5 )' * Zeros (5 )) ≡ abs ( Zeros (5 )' * Ones (5 ) ) ≡ 0.0
541
553
@test Zeros (5 ) * Zeros (6 )' ≡ Zeros (5 ,1 ) * Zeros (6 )' ≡ Zeros (5 ,6 )
542
554
@test randn (5 ) * Zeros (6 )' ≡ randn (5 ,1 ) * Zeros (6 )' ≡ Zeros (5 ,6 )
543
555
@test Zeros (5 ) * randn (6 )' ≡ Zeros (5 ,6 )
552
564
@test transpose ([1 , 2 , 3 ]) * Zeros {Int} (3 ) === zero (Int)
553
565
@test_throws DimensionMismatch transpose (randn (4 )) * Zeros (3 )
554
566
@test transpose (Zeros (5 )) * randn (5 ,3 ) ≡ transpose (Zeros (5 ))* Zeros (5 ,3 ) ≡ transpose (Zeros (5 ))* Ones (5 ,3 ) ≡ transpose (Zeros (3 ))
555
- @test transpose (Zeros (5 )) * randn (5 ) ≡ transpose (Zeros (5 )) * Zeros (5 ) ≡ transpose (Zeros (5 )) * Ones (5 ) ≡ 0.0
567
+ @test abs ( transpose (Zeros (5 )) * randn (5 )) ≡ abs ( transpose (Zeros (5 )) * Zeros (5 )) ≡ abs ( transpose (Zeros (5 )) * Ones (5 ) ) ≡ 0.0
556
568
@test randn (5 ) * transpose (Zeros (6 )) ≡ randn (5 ,1 ) * transpose (Zeros (6 )) ≡ Zeros (5 ,6 )
557
569
@test Zeros (5 ) * transpose (randn (6 )) ≡ Zeros (5 ,6 )
558
570
@test transpose (randn (5 )) * Zeros (5 ) ≡ 0.0
@@ -571,13 +583,13 @@ end
571
583
@test + (z1) === z1
572
584
@test - (z1) === z1
573
585
574
- test_addition_and_subtraction ((z1, z2), (z1, z2), Zeros)
586
+ test_addition_subtraction_dot ((z1, z2), (z1, z2), Zeros)
575
587
test_addition_and_subtraction_dim_mismatch (z1, Zeros {Float64} (4 , 2 ))
576
588
end
577
589
578
590
# `Zeros` +/- `Fill`s should yield `Fills`.
579
591
fill1, fill2 = Fill (5.0 , 4 ), Fill (5 , 4 )
580
- test_addition_and_subtraction ((z1, z2), (fill1, fill2), Fill)
592
+ test_addition_subtraction_dot ((z1, z2), (fill1, fill2), Fill)
581
593
test_addition_and_subtraction_dim_mismatch (z1, Fill (5 , 5 ))
582
594
583
595
X = randn (3 , 5 )
@@ -1326,17 +1338,19 @@ end
1326
1338
Random. seed! (5 )
1327
1339
u = rand (n)
1328
1340
v = rand (n)
1341
+ c = rand (ComplexF16, n)
1329
1342
1330
1343
@test dot (u, D, v) == dot (u, v)
1331
1344
@test dot (u, 2 D, v) == 2 dot (u, v)
1332
1345
@test dot (u, Z, v) == 0
1333
1346
1334
- @test dot (Zeros (5 ), Zeros {ComplexF16} (5 )) ≡ zero (ComplexF64)
1335
- @test dot (Zeros (5 ), Ones {ComplexF16} (5 )) ≡ zero (ComplexF64)
1336
- @test dot (Ones {ComplexF16} (5 ), Zeros (5 )) ≡ zero (ComplexF64)
1337
- @test dot (randn ( 5 ), Zeros {ComplexF16} ( 5 )) ≡ dot (Zeros {ComplexF16} ( 5 ), randn ( 5 )) ≡ zero (ComplexF64 )
1347
+ @test @inferred ( dot (Zeros (5 ), Zeros {ComplexF16} (5 ) )) ≡ zero (ComplexF64)
1348
+ @test @inferred ( dot (Zeros (5 ), Ones {ComplexF16} (5 ) )) ≡ zero (ComplexF64)
1349
+ @test abs ( @inferred ( dot (Ones {ComplexF16} (5 ), Zeros (5 )))) ≡ abs ( @inferred ( dot ( randn ( 5 ), Zeros {ComplexF16} ( 5 )))) ≡ abs ( @inferred ( dot ( Zeros {ComplexF16} ( 5 ), randn ( 5 )))) ≡ zero (Float64) # 0.0 !≡ -0.0
1350
+ @test @inferred ( dot (c, Fill ( 1 + im, 15 ))) ≡ ( @inferred ( dot (Fill ( 1 + im, 15 ), c))) ' ≈ @inferred ( dot (c, fill ( 1 + im, 15 )) )
1338
1351
1339
- @test dot (Fill (1 ,5 ), Fill (2.0 ,5 )) ≡ 10.0
1352
+ @test @inferred (dot (Fill (1 ,5 ), Fill (2.0 ,5 ))) ≡ 10.0
1353
+ @test_skip dot (Fill (true ,5 ), Fill (Int8 (1 ),5 )) isa Int8 # not working at present
1340
1354
1341
1355
let N = 2 ^ big (1000 ) # fast dot for fast sum
1342
1356
@test dot (Fill (2 ,N),1 : N) == dot (Fill (2 ,N),1 : N) == dot (1 : N,Fill (2 ,N)) == 2 * sum (1 : N)
0 commit comments