# Speed up matrix multiplication with permuted vector

**URL:** <https://discourse.julialang.org/t/speed-up-matrix-multiplication-with-permuted-vector/67491>\
**Category:** Performance\
**Tags:** linearalgebra, sparse\
**Created:** [September 1, 2021, 10:55am UTC](https://discourse.julialang.org/t/speed-up-matrix-multiplication-with-permuted-vector/67491 "2021-09-01T10:55:22Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 1, 2021, 11:16am UTC](https://discourse.julialang.org/t/speed-up-matrix-multiplication-with-permuted-vector/67491/5 "2021-09-01T11:16:46Z")

</div>

> [@Thomas](#):
>
> `x[QR.prow]`

Sorry, I still have trouble constructing this `x` 😅

Does this look correct for a MWE ([Please provide a copy-pastable example next time 🙂](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757))?

```julia
julia> A = sprand(10,20,0.5)                                  
10×20 SparseMatrixCSC{Float64, Int64} with 93 stored entries: 
⡙⠂⢍⣘⢪⣎⠴⢁⣹⡷                                                    
⠟⣅⢆⢀⣗⣠⡡⠨⡈⢬                                                    
⠈⠚⠘⠛⠂⠉⠉⠈⠊⠚                                                    
                                                              
julia> x = sprand(10, 0.5)                                    
10-element SparseVector{Float64, Int64} with 5 stored entries:
  [1] = 0.766693                                           
  [2] = 0.66149                                            
  [4] = 0.385874                                           
  [8] = 0.90622                                            
  [10] = 0.931918                                           

```

The `x[QR.prow]` allocates a bunch of new memory, which may be what slows you down here as I believe `x` is somewhat large. Sadly, using a `view` here doesn’t seem to help too much:

```julia
julia> @time y = QR.Q' * x[QR.prow];                
  0.000117 seconds (2.18 k allocations: 126.141 KiB)
                                                    
julia> @time y = QR.Q' * @view x[QR.prow];          
  0.000104 seconds (2.17 k allocations: 125.047 KiB)

```

I think at this point there’s some expert in terms of sparse matrices etc. needed 😅

Nevertheless, doesn’t the suggestion at the end of the post you linked do what you want? This one:

> [@Computing sparse orthogonal projections](https://discourse.julialang.org/t/computing-sparse-orthogonal-projections/62652/2):
>
> **Alternatively** , realize that the intermediate step (A^_A)^{-1} A^_ x (A∗A)−1A∗x(A^_A)^{-1} A^_ x is simply the **least-squares solution**. So, your projection can also be computed by:
> 
> ```julia
> p = A * (A \ x)
> 
> ```
> 
> since `A \ x` does least-squares (via QR) for a matrix with more rows than columns. Or, using a precomputed QR factorization:
> 
> ```julia
> QR = qr(A)
> p = A * (QR \ x)
> 
> ```

---

_[View the full topic](https://discourse.julialang.org/t/speed-up-matrix-multiplication-with-permuted-vector/67491)._
