# How to build this sparsematrix in Julia

**URL:** <https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608>\
**Category:** General Usage\
**Tags:** question, linearalgebra, sparse\
**Created:** [July 21, 2022, 9:20pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608 "2022-07-21T21:20:02Z")\
**Posts on this page:** 4\
**Page:** 3

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 26, 2022, 7:11pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/45 "2022-07-26T19:11:10Z")

</div>

@acxz

> [@acxz](#):
>
> If you want to use the .nzval field (or the nonzeros function) directly you need to make sure that Ik is in the order of how it is represented internally, which is in the CSC ordering, instead of the input CSR ordering you used to create the sparse matrix. (I think this is true, you can make sure by checking the source code: [julia/sparsematrix.jl at 742b9abb4dd4621b667ec5bb3434b8b3602f96fd · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/742b9abb4dd4621b667ec5bb3434b8b3602f96fd/stdlib/SparseArrays/src/sparsematrix.jl) or just try assigning the nzval with CSC ordering and if it works it work!)

I got lost here 🙂

> [@acxz](#):
>
> Can you be more quantitative? What are the time differences? I would be surprised if the performance is drastically hurt bottled.

`For t in 0:25e-6:2`

> [@acxz](#):
>
> Can you also comment on the density of the matrices that you are working with, specifically the F and O matrices? This information along with the dimensions will help us understand which is the appropriate data structure to use

In may real code, `R` is 33x33 and `F` is 102x33. `O` is 102x33.

```julia
F_density = 0.2935234699940582
R_density = 0.02938475665748393
O_density = 0.030303030303030304

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 26, 2022, 9:08pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/46 "2022-07-26T21:08:15Z")

</div>

> [@Amro](#):
>
> > [@acxz](#):
> >
> > Can you be more quantitative? What are the time differences? I would be surprised if the performance is drastically hurt bottled.
> 
> `For t in 0:25e-6:2`

The time difference I was referring to here was the time it takes for your program to run with the for loop using nzrange. Can you give us the timings without using nzrange and after using nzrange? I won’t believe

> using this iteration in my time-domain for-loop is expensive

until I see what the times were.

> [@Amro](#):
>
> In may real code, `R` is 33x33 and `F` is 102x33. `O` is 102x33.
> 
> ```julia
> F_density = 0.2935234699940582
> R_density = 0.02938475665748393
> O_density = 0.030303030303030304
> 
> ```

Thank you for these values! Although no need to put the density of R since we know it is a structured matrix. These are really helpful in terms of creating a “minimal working example” (MWE) can you update your example with this information similar to how I used these pieces of information in an above post: [How to build this sparsematrix in Julia - #37 by acxz](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/37)

Just tested these dimensions and densities and using sparse matrices wile result in faster code, but slightly larger memory usage. Generally speed matters more for us tho, as I’m is the case for your problem as well.

> I got lost here 🙂

Let’s have you implement your code with nzrange and see if the time difference is worth it for us to go deeper. baby steps right?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 27, 2022, 3:28pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/47 "2022-07-27T15:28:09Z")

</div>

> [@acxz](#):
>
> Let’s have you implement your code with nzrange

Thanks!  
Actually, I tried to implement `nzrange` in the MWE (before going to my real code), but looks like I made mistake somewhere. Could you please help me to correct it?

```julia

function update!(SparseMat, val)
       rows = rowvals(SparseMat)
       vals = nonzeros(SparseMat)
       n = size(SparseMat,2)
       iter = 0;
       for j = 1:n
              for i in nzrange(SparseMat, j)
                 #row = rows[i]
                 #val = vals[i]
                 iter += 1;
                 O[rows[i],j] = Ik[iter];
                 s=0;
              end
       end
end

R = sparse([zeros(1,8); Matrix(1.0I, 8-1, 8-1) zeros(8-1,1)]);
Nh = [6, 6, 6, 8, 7, 7]; # fixed
Ik = [7, 1, -4, 8, 4, 1];
F = sparse([3 3 3 4 4 4 0.0 0.0;
            1 2 1 7 9 3 0.0 0.0; 
            7 8 1 4 8 8 0.0 0.0;
            1 1 2 8 5 1 0.0 0.0;
            5 6 1 4 2 5 4.6 0.0;
            1 6 8 1 1 4 6.8 0.0]);

F1 = copy(F);
F2 = copy(F);
K = F * R;
O = sparse(1:6, Nh, Ik)
Ik = [10, 20, 30, 40, 50, 60];
update!(O, Ik) # This works as as O.nzval .= Ik;
S = sparse(1:6, Nh, Ik);
julia> O == S
false

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 30, 2022, 3:27pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/48 "2022-07-30T15:27:24Z")

</div>

I think using nzrange as you have defined your problem in the MWE will not work (or at least you’ll have to do more manipulation)

With how you have currently defined your problem, this is probably the best you can do:

```julia
col_vec = [6, 6, 6, 8, 7, 7];
row_vec = [1, 2, 3, 4, 5, 6];
val_vec = [7, 1, -4, 8, 4, 1];

O = sparse(row_vec, col_vec, val_vec)

new_val_vec = 10:10:60;

for idx in 1:length(val_vec)
    O[row_vec[idx], col_vec[idx]] = new_val_vec[idx]
end

S = sparse(row_vec, col_vec, new_val_vec)

O == S

```

> [@acxz](#):
>
> I got lost here 🙂

Print out `O` right after you create it. `println(O)` Do you notice how the row\_vec and the col\_vec are stored in the sparse matrix type? That is the ordering you need your new values in to use `.nzval`. In CSC, the sort is by columns first and then the rows are sorted for each column. The values of the matrix need to be in that order.

[Previous page](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608.md?page=2)
