|
| 1 | +# helpers |
| 2 | + |
| 3 | +function exact_equal(x::SparseVector, y::SparseVector) |
| 4 | + x.n == y.n && x.nzind == y.nzind && x.nzval == y.nzval |
| 5 | +end |
| 6 | + |
| 7 | +# empty sparse vectors |
| 8 | + |
| 9 | +x0 = SparseVector(8) |
| 10 | +@test isa(x0, SparseVector{Float64,Int}) |
| 11 | +@test length(x0) == 8 |
| 12 | +@test nnz(x0) == 0 |
| 13 | + |
| 14 | +x0 = SparseVector(Float32, 8) |
| 15 | +@test isa(x0, SparseVector{Float32,Int}) |
| 16 | +@test length(x0) == 8 |
| 17 | +@test nnz(x0) == 0 |
| 18 | + |
| 19 | +x0 = SparseVector(Float32, Int32, 8) |
| 20 | +@test isa(x0, SparseVector{Float32, Int32}) |
| 21 | +@test length(x0) == 8 |
| 22 | +@test nnz(x0) == 0 |
| 23 | + |
| 24 | + |
| 25 | +# construction |
| 26 | + |
| 27 | +x = SparseVector(8, [2, 5, 6], [1.25, -0.75, 3.5]) |
| 28 | +x2 = SparseVector(8, [1, 2, 6, 7], [3.25, 4.0, -5.5, -6.0]) |
| 29 | + |
| 30 | +@test eltype(x) == Float64 |
| 31 | +@test ndims(x) == 1 |
| 32 | +@test length(x) == 8 |
| 33 | +@test size(x) == (8,) |
| 34 | +@test size(x,1) == 8 |
| 35 | +@test size(x,2) == 1 |
| 36 | +@test !isempty(x) |
| 37 | + |
| 38 | +@test countnz(x) == 3 |
| 39 | +@test nnz(x) == 3 |
| 40 | +@test nonzeros(x) == [1.25, -0.75, 3.5] |
| 41 | + |
| 42 | +dct = Dict{Int,Float64}() |
| 43 | +dct[2] = 1.25 |
| 44 | +dct[5] = -0.75 |
| 45 | +dct[6] = 3.5 |
| 46 | +xc = SparseVector(8, dct) |
| 47 | +@test isa(xc, SparseVector{Float64,Int}) |
| 48 | +@test exact_equal(x, xc) |
| 49 | + |
| 50 | +# full |
| 51 | + |
| 52 | +xf = zeros(8) |
| 53 | +xf[2] = 1.25 |
| 54 | +xf[5] = -0.75 |
| 55 | +xf[6] = 3.5 |
| 56 | +@test isa(full(x), Vector{Float64}) |
| 57 | +@test full(x) == xf |
| 58 | + |
| 59 | +xf2 = zeros(8) |
| 60 | +xf2[1] = 3.25 |
| 61 | +xf2[2] = 4.0 |
| 62 | +xf2[6] = -5.5 |
| 63 | +xf2[7] = -6.0 |
| 64 | +@test isa(full(x2), Vector{Float64}) |
| 65 | +@test full(x2) == xf2 |
| 66 | + |
| 67 | + |
| 68 | +# conversion |
| 69 | + |
| 70 | +xc = convert(SparseVector, xf) |
| 71 | +@test isa(xc, SparseVector{Float64,Int}) |
| 72 | +@test exact_equal(x, xc) |
| 73 | + |
| 74 | +xc = convert(SparseVector{Float32,Int}, x) |
| 75 | +@test isa(xc, SparseVector{Float32,Int}) |
| 76 | +@test exact_equal(x, xc) |
| 77 | + |
| 78 | +xc = convert(SparseVector{Float32}, x) |
| 79 | +@test isa(xc, SparseVector{Float32,Int}) |
| 80 | +@test exact_equal(x, xc) |
| 81 | + |
| 82 | +# copy |
| 83 | + |
| 84 | +xc = copy(x) |
| 85 | +@test isa(xc, SparseVector{Float64,Int}) |
| 86 | +@test !is(x.nzind, xc.nzval) |
| 87 | +@test !is(x.nzval, xc.nzval) |
| 88 | +@test exact_equal(x, xc) |
| 89 | + |
| 90 | +# getindex |
| 91 | + |
| 92 | +for i = 1:length(x) |
| 93 | + @test x[i] == xf[i] |
| 94 | +end |
| 95 | + |
| 96 | +# setindex |
| 97 | + |
| 98 | +xc = SparseVector(8) |
| 99 | +xc[3] = 2.0 |
| 100 | +@test exact_equal(xc, SparseVector(8, [3], [2.0])) |
| 101 | + |
| 102 | +xc = copy(x) |
| 103 | +xc[5] = 2.0 |
| 104 | +@test exact_equal(xc, SparseVector(8, [2, 5, 6], [1.25, 2.0, 3.5])) |
| 105 | + |
| 106 | +xc = copy(x) |
| 107 | +xc[3] = 4.0 |
| 108 | +@test exact_equal(xc, SparseVector(8, [2, 3, 5, 6], [1.25, 4.0, -0.75, 3.5])) |
| 109 | + |
| 110 | +xc[1] = 6.0 |
| 111 | +@test exact_equal(xc, SparseVector(8, [1, 2, 3, 5, 6], [6.0, 1.25, 4.0, -0.75, 3.5])) |
| 112 | + |
| 113 | +xc[8] = -1.5 |
| 114 | +@test exact_equal(xc, SparseVector(8, [1, 2, 3, 5, 6, 8], [6.0, 1.25, 4.0, -0.75, 3.5, -1.5])) |
| 115 | + |
| 116 | +xc = copy(x) |
| 117 | +xc[5] = 0.0 |
| 118 | +@test exact_equal(xc, SparseVector(8, [2, 6], [1.25, 3.5])) |
| 119 | + |
| 120 | +xc[6] = 0.0 |
| 121 | +@test exact_equal(xc, SparseVector(8, [2], [1.25])) |
| 122 | + |
| 123 | +xc[2] = 0.0 |
| 124 | +@test exact_equal(xc, SparseVector(8, Int[], Float64[])) |
| 125 | + |
| 126 | + |
| 127 | +# sprand |
| 128 | + |
| 129 | +xr = sprand(1000, 0.3) |
| 130 | +@test isa(xr, SparseVector{Float64,Int}) |
| 131 | +@test length(xr) == 1000 |
| 132 | +@test all(nonzeros(xr) .>= 0.0) |
| 133 | + |
| 134 | +xr = sprand(1000, 0.3, Float32) |
| 135 | +@test isa(xr, SparseVector{Float32,Int}) |
| 136 | +@test length(xr) == 1000 |
| 137 | +@test all(nonzeros(xr) .>= 0.0) |
| 138 | + |
| 139 | +xr = sprandn(1000, 0.3) |
| 140 | +@test isa(xr, SparseVector{Float64,Int}) |
| 141 | +@test length(xr) == 1000 |
| 142 | +@test any(nonzeros(xr) .> 0.0) && any(nonzeros(xr) .< 0.0) |
| 143 | + |
| 144 | +# abs and abs2 |
| 145 | + |
| 146 | +@test exact_equal(abs(x), SparseVector(8, [2, 5, 6], abs([1.25, -0.75, 3.5]))) |
| 147 | +@test exact_equal(abs2(x), SparseVector(8, [2, 5, 6], abs2([1.25, -0.75, 3.5]))) |
| 148 | + |
| 149 | +# plus and minus |
| 150 | + |
| 151 | +xa = SparseVector(8, [1,2,5,6,7], [3.25,5.25,-0.75,-2.0,-6.0]) |
| 152 | + |
| 153 | +@test exact_equal(x + x, SparseVector(8, [2,5,6], [2.5,-1.5,7.0])) |
| 154 | +@test exact_equal(x + x2, xa) |
| 155 | + |
| 156 | +xb = SparseVector(8, [1,2,5,6,7], [-3.25,-2.75,-0.75,9.0,6.0]) |
| 157 | + |
| 158 | +@test exact_equal(x - x, SparseVector(8, Int[], Float64[])) |
| 159 | +@test exact_equal(x - x2, xb) |
| 160 | + |
| 161 | +@test full(x) + x2 == full(xa) |
| 162 | +@test full(x) - x2 == full(xb) |
| 163 | +@test x + full(x2) == full(xa) |
| 164 | +@test x - full(x2) == full(xb) |
| 165 | + |
| 166 | + |
| 167 | +# reduction |
| 168 | + |
| 169 | +@test sum(x) == 4.0 |
| 170 | +@test sumabs(x) == 5.5 |
| 171 | +@test sumabs2(x) == 14.375 |
| 172 | + |
| 173 | +@test vecnorm(x) == sqrt(14.375) |
| 174 | +@test vecnorm(x, 1) == 5.5 |
| 175 | +@test vecnorm(x, 2) == sqrt(14.375) |
| 176 | +@test vecnorm(x, Inf) == 3.5 |
0 commit comments