From 28f6a3f1c0ad3cc10e49f0eee2264c8afccde1d8 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Wed, 21 Sep 2016 17:37:16 -0400 Subject: [PATCH] workarounds for broadcast not preserving sparsity in general --- test/sparsedir/sparsevector.jl | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/test/sparsedir/sparsevector.jl b/test/sparsedir/sparsevector.jl index 932f50fd9d0ad0..cebe94c9c1051e 100644 --- a/test/sparsedir/sparsevector.jl +++ b/test/sparsedir/sparsevector.jl @@ -581,7 +581,9 @@ let x = spv_x1, x2 = x2 = spv_x2 # multiplies xm = SparseVector(8, [2, 6], [5.0, -19.25]) - @test exact_equal(x .* x, abs2(x)) + let y=x # workaround for broadcast not preserving sparsity in general + @test exact_equal(x .* y, abs2(x)) + end @test exact_equal(x .* x2, xm) @test exact_equal(x2 .* x, xm) @@ -724,19 +726,19 @@ let x = sprand(16, 0.5), x2 = sprand(16, 0.4) end # scale - let sx = SparseVector(x.n, x.nzind, x.nzval * 2.5) - @test exact_equal(x * 2.5, sx) - @test exact_equal(x * (2.5 + 0.0*im), complex(sx)) - @test exact_equal(2.5 * x, sx) - @test exact_equal((2.5 + 0.0*im) * x, complex(sx)) - @test exact_equal(x * 2.5, sx) - @test exact_equal(2.5 * x, sx) - @test exact_equal(x .* 2.5, sx) - @test exact_equal(2.5 .* x, sx) - @test exact_equal(x / 2.5, SparseVector(x.n, x.nzind, x.nzval / 2.5)) + let α = 2.5, sx = SparseVector(x.n, x.nzind, x.nzval * α) + @test exact_equal(x * α, sx) + @test exact_equal(x * (α + 0.0*im), complex(sx)) + @test exact_equal(α * x, sx) + @test exact_equal((α + 0.0*im) * x, complex(sx)) + @test exact_equal(x * α, sx) + @test exact_equal(α * x, sx) + @test exact_equal(x .* α, sx) + @test exact_equal(α .* x, sx) + @test exact_equal(x / α, SparseVector(x.n, x.nzind, x.nzval / α)) xc = copy(x) - @test is(scale!(xc, 2.5), xc) + @test is(scale!(xc, α), xc) @test exact_equal(xc, sx) end