# In-place matrix multiplication compatible with 0.5 and 0.6

**URL:** https://discourse.julialang.org/t/in-place-matrix-multiplication-compatible-with-0-5-and-0-6/1403
**Category:** General Usage
**Created:** [January 10, 2017, 7:37pm UTC](https://discourse.julialang.org/t/in-place-matrix-multiplication-compatible-with-0-5-and-0-6/1403 "2017-01-10T19:37:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [January 10, 2017, 7:37pm UTC](https://discourse.julialang.org/t/in-place-matrix-multiplication-compatible-with-0-5-and-0-6/1403/1 "2017-01-10T19:37:07Z")

</div>

I want to perform in-place matrix multiplications where the matrices can be sparse or dense. As a simple case, consider evaluating residuals from a regression model

```julia
resid .= y - X*β

```

where `resid` is a pre-allocated dense vector, `y` and `β` are both dense vectors, and `X` is a `StridedArray` or a `SparseMatrixCSC`.

For the sparse `X` I can call

```julia
A_mul_B!(-1, X, β, 1, copy!(resid, y))

```

but that is not defined for dense `X`. I can drop down to `BLAS.gemv!` or `BLAS.gemm!` for the dense case but I would prefer a method call that applies to both sparse and dense and doesn’t cause allocation or introduce inefficiencies. I would also prefer syntax that applies to both v0.5 and v0.6

---

<div class="post-metadata">

### Author: ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)
#### Post date: [January 10, 2017, 7:49pm UTC](https://discourse.julialang.org/t/in-place-matrix-multiplication-compatible-with-0-5-and-0-6/1403/2 "2017-01-10T19:49:28Z")

</div>

Are you OK with adding a dependency ? Then I believe [https://github.com/andreasnoack/LinearAlgebra.jl](https://github.com/andreasnoack/LinearAlgebra.jl) has the A\_mul\_B! definitions you’re llooking for.

---

<div class="post-metadata">

### Author: ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)
#### Post date: [January 10, 2017, 7:52pm UTC](https://discourse.julialang.org/t/in-place-matrix-multiplication-compatible-with-0-5-and-0-6/1403/3 "2017-01-10T19:52:05Z")

</div>

Similar methods should be defined in the dense case. I have most of the definitions in [https://github.com/andreasnoack/LinearAlgebra.jl/blob/master/src/juliaBLAS.jl](https://github.com/andreasnoack/LinearAlgebra.jl/blob/master/src/juliaBLAS.jl) but it requires a bit of work to migrate them to Base and I haven’t had time for that yet. A solution could be to register `LinearAlgebra.jl` and depend on it but the package doesn’t have documentation yet. For now, the easiest solution might be to copy the methods to your package.

---

<div class="post-metadata">

### Author: ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)
#### Post date: [January 10, 2017, 7:53pm UTC](https://discourse.julialang.org/t/in-place-matrix-multiplication-compatible-with-0-5-and-0-6/1403/4 "2017-01-10T19:53:25Z")

</div>

Thanks @andreasnoack, will do.
