# Vector\*matrix in 0.6

**URL:** <https://discourse.julialang.org/t/vector-matrix-in-0-6/1838>\
**Category:** General Usage\
**Created:** [February 2, 2017, 9:48am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838 "2017-02-02T09:48:40Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [February 2, 2017, 9:48am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/1 "2017-02-02T09:48:40Z")

</div>

This caught me by surprise:

```julia
julia> ones(10)*ones(1,10)
ERROR: DimensionMismatch("Cannot left-multiply a matrix by a vector")
Stacktrace:
 [1] *(::Array{Float64,1}, ::Array{Float64,2}) at ./linalg/rowvector.jl:157

```

Is this a bug, or intentional? (I don’t see why this wouldn’t make sense…)

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [February 2, 2017, 10:04am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/2 "2017-02-02T10:04:37Z")

</div>

Maybe because of  
[http://docs.julialang.org/en/latest/stdlib/linalg.html#Base.LinAlg.RowVector](http://docs.julialang.org/en/latest/stdlib/linalg.html#Base.LinAlg.RowVector)  
and related to [https://github.com/JuliaLang/julia/pull/19670](https://github.com/JuliaLang/julia/pull/19670)?

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [February 2, 2017, 10:07am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/3 "2017-02-02T10:07:06Z")

</div>

Yes I believe that is the cause, I’m more wondering about the why? It seems to me that the multiplication above should still work as the dimensions are consistent, unless `Vector` no longer means column vector (in which case, I would expect a type `ColumnVector`)

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [February 2, 2017, 10:20am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/4 "2017-02-02T10:20:36Z")

</div>

I think it was discussed in one of the comments, for instance [here](https://github.com/JuliaLang/julia/pull/19670#issuecomment-268881100).

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [February 2, 2017, 10:28am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/5 "2017-02-02T10:28:45Z")

</div>

That’s a very long discussion, I didn’t see this issue addressed .

Note that this issue is completely independent of whether a `RowVector` exists. It’s just a matter of a column vector times a 1 x matrix is mathematically well-defined.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [February 2, 2017, 10:34am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/6 "2017-02-02T10:34:53Z")

</div>

Agreed. It seems that this is such a fundamental operation that it should be in the test suite.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [February 2, 2017, 11:15am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/7 "2017-02-02T11:15:21Z")

</div>

OK I’ve created an issue: [https://github.com/JuliaLang/julia/issues/20389](https://github.com/JuliaLang/julia/issues/20389)

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [February 2, 2017, 11:49am UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/8 "2017-02-02T11:49:33Z")

</div>

Maybe this is just a broadcast operation?

```julia
julia> ones(3).*ones(1,3)
3×3 Array{Float64,2}:
 1.0 1.0 1.0
 1.0 1.0 1.0
 1.0 1.0 1.0

```

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 2, 2017, 12:35pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/9 "2017-02-02T12:35:00Z")

</div>

To me, the question is in reverse - why did this method exist in v0.5?

The (only) reason is it was a way of obtaining the outer product of two vectors. You can now do `vector * rowvector` for this. In fact, this is still a (specialized) method of `*(::AbstractVector, ::AbstractMatrix)`.

Otherwise `vector * matrix` doesn’t make too much sense (outside of MATLAB/Householder notation). You wouldn’t impress a math lecturer with writing _B_ = **v**._A_ in an assignment/exam, and it would be a misuse of Dirac notation to write `|psi> H` and expect an operator back. The correct things are _B_ = **v**. **w** ᵀ and `O = |psi><phi|`.

Believe me, I pained over this one, but really its not difficult to write `ones(10) * ones(10)'` for the outer product (in fact, it’s one less character 🙂 ). Since the `*(A::AbstractVector, B::AbstractMatrix)` method was an error if `size(B, 1)` was not `1`, it is implied that the programmer knows for sure `B` is a 1xN shaped Array, which is guaranteed by `B::RowVector`.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 2, 2017, 12:36pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/10 "2017-02-02T12:36:16Z")

</div>

> Maybe this is just a broadcast operation?

The implementation is precisely this.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 2, 2017, 12:42pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/11 "2017-02-02T12:42:20Z")

</div>

> [@andyferris](#):
>
> Otherwise vector \* matrix doesn’t make too much sense

If we view a vector as a 1-column matrix, then vector \* (1-row matrix) makes perfect sense in standard linear algebra…

(I agree that there is some tension between this viewpoint and viewing vectors as living in some kind of abstract finite-dimensional Hilbert space with matrices as linear operators on them. But this seems like a case where we might as well continue to support both viewpoints, since most users will expect it.)

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [February 2, 2017, 12:42pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/12 "2017-02-02T12:42:53Z")

</div>

> [@andyferris](#):
>
> You wouldn’t impress a math lecturer

Ehm, @dlfivefifty is a math professor…

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 2, 2017, 12:49pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/13 "2017-02-02T12:49:47Z")

</div>

> Ehm, @dlfivefifty is a math professor…

Thanks for the heads up… 🙂

EDIT: to clarify, you wouldn’t have impressed _me_ as a physics lecturer or tutor, but that doesn’t mean there’s not a better way.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 2, 2017, 12:57pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/14 "2017-02-02T12:57:11Z")

</div>

> If we view a vector as a 1-column matrix, then vector \* (1-row matrix) makes perfect sense in standard linear algebra…

Right. The point is that, in Julia, I really, really, don’t view a vector as a 1-column matrix. They are unlikely to dispatch similarly in many, many contexts.

This is precisely the Householder notation, which is entirely self-consistent and embraced by MATLAB. On the other hand, Julian’s have held it as a point of honor that there is a very real distinction between `Vector` and `Matrix`, unlike in MATLAB.

Without `RowVector`, the linear algebra bits had been living in some kind of half-way land between Householder notation and making this distinguishment. It’s fair to say that now I’ve tried to implement semantics consistent with Dirac notation. We allow

- rowvec \* vec (inner product)
- vec \* rowvec (outer product)
- matrix \* vec (-\> vec)
- rowvec \* matrix (-\> rowvec)
- matrix \* matrix (-\> matrix)
- also of course scalar-array multiplication

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 2, 2017, 1:05pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/15 "2017-02-02T13:05:37Z")

</div>

> [@andyferris](#):
>
> This is precisely the Householder notation, which is entirely self-consistent and embraced by MATLAB.

(Not entirely, because of the case of scalars vs 1x1 matrices.)

I think it is more useful to maximize utility than “points of honor” here. If you have an operation that is not defined in one viewpoint and is defined in the other (rather than cases where the definitions conflict), it makes more sense to me to support it.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [February 2, 2017, 1:06pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/16 "2017-02-02T13:06:02Z")

</div>

Mathematicians generally do not use Dirac notation…if we’ve held out 78 years, you’re unlikely to convince us now. I say Julia should cater to both audiences, and not have unnecessary errors to force a specific viewpoint.

---

<div class="post-metadata">

**Author:** ![andyferris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andyferris/32/235_2.png) [@andyferris](https://discourse.julialang.org/u/andyferris)\
**Post date:** [February 2, 2017, 1:20pm UTC](https://discourse.julialang.org/t/vector-matrix-in-0-6/1838/17 "2017-02-02T13:20:58Z")

</div>

> [@dlfivefifty](#):
>
> if we’ve held out 78 years, you’re unlikely to convince us now

lol. Fair point.

I _still_ think on pen-and-paper (in say a first-year maths class) you’d squirm at _B_ = **v**._A_ but not _B_ = **v**. **w** ᵀ, but I don’t care so much on this particular point to hold out.
