# Vector operations: any preference for using AbstractVector or n-by-1 array

**URL:** <https://discourse.julialang.org/t/vector-operations-any-preference-for-using-abstractvector-or-n-by-1-array/23483>\
**Category:** New to Julia\
**Created:** [April 24, 2019, 7:59pm UTC](https://discourse.julialang.org/t/vector-operations-any-preference-for-using-abstractvector-or-n-by-1-array/23483 "2019-04-24T19:59:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [April 24, 2019, 7:59pm UTC](https://discourse.julialang.org/t/vector-operations-any-preference-for-using-abstractvector-or-n-by-1-array/23483/1 "2019-04-24T19:59:38Z")

</div>

Consider the multiplication of a n-by-n matrix `M` and a n-dimensional vector `v`, M\*v. One may write v either

- as an n-element Array (`typeof(v) <: AbstractVector` is `true`),
- or write it as an `n×1` array (`typeof(v) <: AbstractVector` is `false`).

Glancing through the source code matmul.jl, it looks like one uses ` gemv!` and the other uses `gemm_wrapper!`; as for timing, there seems to be very small differences

```julia
m=rand(10000,10000);
v=rand(10000);
v2=rand(10000,1);

@btime $m*$v;
  34.902 ms (2 allocations: 78.20 KiB)

@btime $m*$v2;
  36.459 ms (2 allocations: 78.20 KiB)

```

Also, to make an n-by-n matrix from v\*v', `@which` shows `Vector` uses `adjtrans.jl` while n-by-1 array uses `matmul.jl`. Looks like `AbstractVector` can be slightly faster for high dimension cases, and for things like v'\*m\*v, `typeof(v) <: AbstractVector` gives a number whereas n-by-1 matrix v gives a 1-by-1 matrix.  
Generally, what’s the pros/cons of using one as opposed to the other, and is there any preferences for using either one?

Thanks!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 25, 2019, 12:36am UTC](https://discourse.julialang.org/t/vector-operations-any-preference-for-using-abstractvector-or-n-by-1-array/23483/2 "2019-04-25T00:36:11Z")

</div>

In my opinion, you are taking too fine-grained a perspective. Keep it simple.  
`typeof(zeros(Int, 3)) == Array{Int64,1}`, so do what Julia does: use an n-element Array. That way, whatever you do likely works with other sorts of AbstractVector – which is very helpful.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 25, 2019, 2:32am UTC](https://discourse.julialang.org/t/vector-operations-any-preference-for-using-abstractvector-or-n-by-1-array/23483/3 "2019-04-25T02:32:45Z")

</div>

This is very related to a very (in)famous Julia issue:  
[https://github.com/JuliaLang/julia/issues/4774](https://github.com/JuliaLang/julia/issues/4774)  
(Warning: it’s also one of the _longest_ Julia issues…)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 25, 2019, 2:33am UTC](https://discourse.julialang.org/t/vector-operations-any-preference-for-using-abstractvector-or-n-by-1-array/23483/4 "2019-04-25T02:33:16Z")

</div>

To summarize: Use a 1-dimensional `Vector` if you mean a vector.

---

<div class="post-metadata">

**Author:** ![Egwene\_al\_Vere](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@Egwene\_al\_Vere](https://discourse.julialang.org/u/Egwene_al_Vere)\
**Post date:** [April 25, 2019, 2:37pm UTC](https://discourse.julialang.org/t/vector-operations-any-preference-for-using-abstractvector-or-n-by-1-array/23483/5 "2019-04-25T14:37:15Z")

</div>

Thanks!
