# Question regarding sparse vector

**URL:** <https://discourse.julialang.org/t/question-regarding-sparse-vector/72724>\
**Category:** New to Julia\
**Created:** [December 7, 2021, 5:51pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724 "2021-12-07T17:51:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [December 7, 2021, 5:51pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/1 "2021-12-07T17:51:55Z")

</div>

What am I doing wrong here:

```julia
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")

```

yielding

```julia
iter
1.0
0.0
1.0
map
0.0
1.0
0.0
1.0
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 7, 2021, 5:57pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/2 "2021-12-07T17:57:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 7, 2021, 5:58pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/3 "2021-12-07T17:58:36Z")

</div>

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…)

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 7, 2021, 5:59pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/4 "2021-12-07T17:59:05Z")

</div>

Not the type, but to check whether `f(0) = 0`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 7, 2021, 6:00pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/5 "2021-12-07T18:00:32Z")

</div>

But that specifically (at least) for sparse arrays. I was deleting the post because that does not happen with standard vectors.

```julia
julia> v = [1.0, 2.0, 3.0]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> map(x -> println(x), v)
1.0
2.0
3.0
3-element Vector{Nothing}:
 nothing
 nothing
 nothing

julia> v = sparsevec([1, 2, 3], [1.0, 2.0, 3.0])
3-element SparseVector{Float64, Int64} with 3 stored entries:
  [1] = 1.0
  [2] = 2.0
  [3] = 3.0

julia> map(x -> println(x), v)
0.0
1.0
2.0
3.0
3-element SparseVector{Nothing, Int64} with 3 stored entries:
  [1] = nothing
  [2] = nothing
  [3] = nothing

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [December 7, 2021, 6:01pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/6 "2021-12-07T18:01:45Z")

</div>

Sorry for the confusion, so the function given to `map` definitely has to be pure. Any reference where to find `map` for sparse vectors in the repository (I did have a quick look into [https://github.com/JuliaLang/julia/blob/c646b5d4893e45a413705d3fea0a7227c4fe7aae/stdlib/SparseArrays/src/sparsevector.jl](https://github.com/JuliaLang/julia/blob/c646b5d4893e45a413705d3fea0a7227c4fe7aae/stdlib/SparseArrays/src/sparsevector.jl))?

BTW is there any reason, why the iteration doesn’t return pairs of (index, value) for non-zero values?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 7, 2021, 6:25pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/7 "2021-12-07T18:25:55Z")

</div>

Is something like this what you want?

```julia
julia> v = sparsevec([1, 2, 5], [1.0, 2.0, 3.0]);

julia> map(i -> (i,sin(v[i])), v.nzind)
3-element Vector{Tuple{Int64, Float64}}:
 (1, 0.8414709848078965)
 (2, 0.9092974268256817)
 (5, 0.1411200080598672)

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [December 7, 2021, 6:32pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/8 "2021-12-07T18:32:14Z")

</div>

> [@lmiq](#):
>
> `map(i -> (i,sin(v[i])), v.nzind)`

Nice! I missed that the original slack discussion is continuing [here](https://github.com/JuliaLang/SparseArrays.jl/issues/4#issuecomment-987843838).

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [December 7, 2021, 7:04pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/9 "2021-12-07T19:04:51Z")

</div>

> [@fredrikekre](#):
>
> Not the type, but to check whether `f(0) = 0`

I suspect you are right. Any hint how to verify? Thanks!

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 7, 2021, 7:44pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/10 "2021-12-07T19:44:37Z")

</div>

[SparseArrays/src/higherorderfns.jl#L165-L166](https://github.com/JuliaLang/julia/blob/2e9bbda5bd5bd6c04d019aa565ea60efab7c10d4/stdlib/SparseArrays/src/higherorderfns.jl#L165-L166) for example.

* * *

FYI, very easy to figure out with [Debugger.jl](https://github.com/JuliaDebug/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
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

```

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [December 7, 2021, 11:18pm UTC](https://discourse.julialang.org/t/question-regarding-sparse-vector/72724/11 "2021-12-07T23:18:33Z")

</div>

Thanks a lot!
