using SparseArrays
v = sparsevec([1, 2, 3], [1.0, 0.0, 1.0])
println("iter")
for x in v println(x); end
println("map")
map(x -> begin println(x); x; end, v)
println("end")
You mean because of the additional 0.0 in the map? I think that is because the function to be mapped is called once before the actual mapping, probably to infer the type of the output.
iteration goes over the non-trivial values you have in the sparse matrix, map will see the entire matrix, where the empty parts are just assumed zero. (by definition of sparse matrix…)
FYI, very easy to figure out with Debugger.jl: (add breakpoint at f, c to continue to breakpoint, f 2 to step up the call chain and you end up with the lines I linked above):
julia> using SparseArrays, Debugger
julia> f(x) = 0;
julia> v = sparsevec([1, 2, 5], [1.0, 2.0, 3.0]);
julia> @enter map(f, v)
In map(f, A) at /Users/fredrik/julia-master/usr/share/julia/stdlib/v1.8/SparseArrays/src/higherorderfns.jl:148
>148 map(f::Tf, A::SparseVector) where {Tf} = _noshapecheck_map(f, A)
About to run: (SparseArrays.HigherOrderFns._noshapecheck_map)(f, [1] = 1.0
[2] = 2.0
[5] = 3.0)
1|debug> bp add f
[ Info: added breakpoint for function f
1] f
1|debug> c
Hit breakpoint:
In f(x) at REPL[14]:1
>1 1 ─ return 0
About to run: return 0
1|debug> f 2
In _noshapecheck_map(f, A, Bs) at /Users/fredrik/julia-master/usr/share/julia/stdlib/v1.8/SparseArrays/src/higherorderfns.jl:164
164 function _noshapecheck_map(f::Tf, A::SparseVecOrMat, Bs::Vararg{SparseVecOrMat,N}) where {Tf,N}
>165 fofzeros = f(_zeros_eltypes(A, Bs...)...)
166 fpreszeros = _iszero(fofzeros)
167 maxnnzC = Int(fpreszeros ? min(widelength(A), _sumnnzs(A, Bs...)) : widelength(A))
168 entrytypeC = Base.Broadcast.combine_eltypes(f, (A, Bs...))
169 indextypeC = _promote_indtype(A, Bs...)
170 C = _allocres(size(A), indextypeC, entrytypeC, maxnnzC)
171 return fpreszeros ? _map_zeropres!(f, C, A, Bs...) :
172 _map_notzeropres!(f, fofzeros, C, A, Bs...)
173 end