511
511
end
512
512
513
513
@testset " Bregman Divergence" begin
514
- # Some basic tests.
514
+ # Some basic tests.
515
515
@test_throws ArgumentError bregman (x -> x, x -> 2 * x, [1 , 2 , 3 ], [1 , 2 , 3 ])
516
- # Test if Bregman() correctly implements the gkl divergence between two random vectors.
516
+ # Test if Bregman() correctly implements the gkl divergence between two random vectors.
517
517
F (p) = LinearAlgebra. dot (p, log .(p));
518
518
∇ (p) = map (x -> log (x) + 1 , p)
519
519
testDist = Bregman (F, ∇)
@@ -522,13 +522,28 @@ end
522
522
p = p/ sum (p);
523
523
q = q/ sum (q);
524
524
@test evaluate (testDist, p, q) ≈ gkl_divergence (p, q)
525
- # Test if Bregman() correctly implements the squared euclidean dist. between them.
525
+ # Test if Bregman() correctly implements the squared euclidean dist. between them.
526
526
@test bregman (x -> norm (x)^ 2 , x -> 2 * x, p, q) ≈ sqeuclidean (p, q)
527
- # Test if Bregman() correctly implements the IS distance.
527
+ # Test if Bregman() correctly implements the IS distance.
528
528
F (p) = - 1 * sum (log .(p))
529
529
∇ (p) = map (x -> - 1 * x^ (- 1 ), p)
530
530
function ISdist (p:: AbstractVector , q:: AbstractVector )
531
531
return sum ([p[i]/ q[i] - log (p[i]/ q[i]) - 1 for i in 1 : length (p)])
532
532
end
533
- @test bregman (F, ∇, p, q) ≈ ISdist (p, q)
534
- end
533
+ @test bregman (F, ∇, p, q) ≈ ISdist (p, q)
534
+ end
535
+
536
+ @testset " zero allocation colwise!" begin
537
+ d = Euclidean ()
538
+ a = rand (2 , 41 )
539
+ b = rand (2 , 41 )
540
+ z = zeros (41 )
541
+ colwise! (z, d, a, b)
542
+ # This fails when bounds checking is enforced
543
+ bounds = Base. JLOptions (). check_bounds
544
+ if bounds == 0
545
+ @test (@allocated colwise! (z, d, a, b)) == 0
546
+ else
547
+ @test_broken (@allocated colwise! (z, d, a, b)) == 0
548
+ end
549
+ end
0 commit comments