# Can't mul! to a slice

**URL:** <https://discourse.julialang.org/t/cant-mul-to-a-slice/78178>\
**Category:** New to Julia\
**Tags:** linearalgebra\
**Created:** [March 20, 2022, 8:40pm UTC](https://discourse.julialang.org/t/cant-mul-to-a-slice/78178 "2022-03-20T20:40:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fredrikpaues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikpaues/32/34080_2.png) [@fredrikpaues](https://discourse.julialang.org/u/fredrikpaues)\
**Post date:** [March 20, 2022, 8:40pm UTC](https://discourse.julialang.org/t/cant-mul-to-a-slice/78178/1 "2022-03-20T20:40:15Z")

</div>

After watching Brian Jackson’s talk on JuliaCon 2020 [Adventures in Avoiding Allocations](https://www.youtube.com/watch?v=o8qTJGcPWkE), I wanted to try `LinearAlgebra.mul!` out. But I’m guessing that I don’t get it as `test1` and `test2` don’t yield the same result. The problem seem to arise when I want to make the “out” variable of `mul!` be a slice. Can I use `mul!` to write to an array slice?

```julia-repl
julia> import LinearAlgebra

julia> function test1(A, B, n)
           Y = Matrix{Float64}(undef, 2, n)
           LinearAlgebra.mul!(Y[1, :], A, B)
           return Y
       end
test1 (generic function with 1 method)

julia> function test2(A, B, n)
           Y = Matrix{Float64}(undef, 2, n)
           Y[1, :] .= A * B
           return Y
       end
test2 (generic function with 1 method)

julia> n1 = 2
2

julia> n2 = 3
3

julia> A = rand(n2, n1)
3×2 Matrix{Float64}:
 0.425788 0.748112
 0.817301 0.299914
 0.0576634 0.713061

julia> B = ones(n1)
2-element Vector{Float64}:
 1.0
 1.0

julia> Y1 = test1(A, B, n2)
2×3 Matrix{Float64}:
 8.93663e-316 8.93663e-316 8.93663e-316
 8.93663e-316 8.93663e-316 8.93663e-316

julia> Y2 = test2(A, B, n2)
2×3 Matrix{Float64}:
 1.1739 1.11721 0.770724
 0.0 0.0 0.0

julia> [Y1[1, :] Y2[1, :]]
3×2 Matrix{Float64}:
 8.93663e-316 1.1739
 8.93663e-316 1.11721
 8.93663e-316 0.770724

```

---

<div class="post-metadata">

**Author:** ![fredrikpaues](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikpaues/32/34080_2.png) [@fredrikpaues](https://discourse.julialang.org/u/fredrikpaues)\
**Post date:** [March 20, 2022, 8:58pm UTC](https://discourse.julialang.org/t/cant-mul-to-a-slice/78178/2 "2022-03-20T20:58:45Z")

</div>

While I don’t understand **why** , the problem seems to go away if I use `view` to rewrite `test1` as

```julia
function test1(A, B, n)
    Y = Matrix{Float64}(undef, 2, n)
    LinearAlgebra.mul!(view(Y, 1, :), A, B)
    return Y
end

```

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [March 20, 2022, 9:10pm UTC](https://discourse.julialang.org/t/cant-mul-to-a-slice/78178/3 "2022-03-20T21:10:39Z")

</div>

Without the view you allocate a new array that is a copy of the slice of Y, and send this to mul to store the result in. This means Y is never changed in that case. When creating the view you actually use the memory of Y corresponding to that slice to send to mul to be written to.
