# Faulty Transpose and Adjoint Multiplication

**URL:** https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [August 20, 2018, 1:38am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742 "2018-08-20T01:38:25Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [August 20, 2018, 1:38am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/1 "2018-08-20T01:38:25Z")

</div>

```julia
a = transpose([-6.5])
b = [0, .95]'
Matrix(a) * Matrix(b) # Calling `Matrix`
a * b # Without calling `Matrix`
ERROR: DimensionMismatch("Cannot multiply two vectors")

```

```julia
size(a)
(1, 1)

size(b)
(1, 2)

```

Is this intentional or should I open an issue?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 1:46am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/2 "2018-08-20T01:46:40Z")

</div>

It works with 0.7.0.

```julia
julia> a = transpose([-6.5])
1×1 LinearAlgebra.Transpose{Float64,Array{Float64,1}}:
 -6.5

julia> b = [0, .95]'
1×2 LinearAlgebra.Adjoint{Float64,Array{Float64,1}}:
 0.0 0.95

julia> Matrix(a) * Matrix(b)
1×2 Array{Float64,2}:
 0.0 -6.175

```

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [August 20, 2018, 1:49am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/3 "2018-08-20T01:49:48Z")

</div>

You have to call `Matrix` or `reshape` on the `Transpose` or `Adjoint` for it to work.  
If you do it without `Matrix` it errors.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 1:52am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/4 "2018-08-20T01:52:17Z")

</div>

I see. Sorry, I missed the last line. This works:

```julia
reshape(a, 1, 1) * b

```

Weird!

```julia
julia> size(a)
(1, 1)

julia> size(reshape(a, 1, 1))
(1, 1)

```

---

<div class="post-metadata">

### Author: ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)
#### Post date: [August 20, 2018, 1:53am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/5 "2018-08-20T01:53:26Z")

</div>

I think this is/was intentional. Doing `a*b` in v0.6.4 gives an error that one cannot multiply two transposed vectors.

I haven’t used 0.7, but I suspect Julia sees `a` as a transposed vector, not a matrix and that calling size() on a transposed vector produces a 2-element tuple, tricking one into thinking that Julia sees `a` as a matrix.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 2:04am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/6 "2018-08-20T02:04:12Z")

</div>

And then there is this:

```julia
julia> a = [-6.5]
1-element Array{Float64,1}:
 -6.5

julia> a*b
1×2 Array{Float64,2}:
 -0.0 -6.175

julia> size(a)
(1,)

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 2:31am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/7 "2018-08-20T02:31:47Z")

</div>

Now this seems funny:

```julia
julia> a
1-element Array{Float64,1}:
 -6.5

julia> size(transpose(transpose(a)))
(1,)

julia> size(a)
(1,)

julia> size(transpose(a))
(1, 1)

```

---

<div class="post-metadata">

### Author: ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)
#### Post date: [August 20, 2018, 2:39am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/8 "2018-08-20T02:39:02Z")

</div>

```julia
julia-0.7> aa=[1]
1-element Array{Int64,1}:
 1

julia-0.7> @code_lowered transpose(transpose(aa))
CodeInfo(
105 1 ─ %1 = (Base.getproperty)(A, :parent) │
    └── return %1 │
)

```

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 3:04am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/9 "2018-08-20T03:04:33Z")

</div>

This is actually happening also with nontrivial (that is not one by one) vectors:

```julia
julia> size(transpose(a))
(1, 2)

julia> size(transpose(transpose(a)))
(2,)

julia> size(a)
(2,)

```

I guess it has to do with the fact that the actual transpose needs to somehow indicate that it is a “row”.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 3:13am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/10 "2018-08-20T03:13:43Z")

</div>

This is the “culprit” ([line 123](https://github.com/JuliaLang/julia/blob/e7d15d4a013a43442b75ba4e477382804fa4ac49/stdlib/LinearAlgebra/src/adjtrans.jl#L78-L100)):

```julia
size(v::AdjOrTransAbsVec) = (1, length(v.parent))

```

Does a (N, ) vector really need to become a (1, N) size array when it is transposed? Is this an “issue-worthy” question?

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [August 20, 2018, 3:31am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/11 "2018-08-20T03:31:36Z")

</div>

I opened [28772](https://github.com/JuliaLang/julia/issues/28772). It really doesn’t seem to be intentional or by design. I would consider it an issue to patch on the next release.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 3:48am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/12 "2018-08-20T03:48:46Z")

</div>

I have to admit that I would rather see the multiplication of two vectors to be undefined. However, I would take issue with the size of the transpose of a vector, which indicates that the result really is a matrix:

```julia
julia> size(a)
(2,)

julia> size(transpose(a))
(1, 2)

```

That doesn’t seem right to me.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [August 20, 2018, 4:16am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/13 "2018-08-20T04:16:09Z")

</div>

What do you recommend as an alternative though? I think `length(size(a))` should be be equal to the dimension of `a`. So if `a = aa'` where `aa` is a column “vector”, then `a` would be a row “vector” which is 1D. But using the same size `(2,)` for example for both `a` and `aa` hides shape information. But then the shape is encoded in the type of `a` or `aa` so maybe that’s ok. Maybe `size` can also return an `ArraySize` instance that has both the `size` and `shape` (or array type) of a vector, matrix, adjoint, transpose, etc. I am not sure if these are good proposals but the current state is confusing.

---

<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: [August 20, 2018, 11:40am UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/14 "2018-08-20T11:40:28Z")

</div>

This has been discussed extensively. Please take a look at [https://github.com/JuliaLang/julia/issues/4774](https://github.com/JuliaLang/julia/issues/4774) and the issues linked in that issue before continuing here.

---

<div class="post-metadata">

### Author: ![fhenneke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fhenneke/32/2211_2.png) [@fhenneke](https://discourse.julialang.org/u/fhenneke)
#### Post date: [August 20, 2018, 1:19pm UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/15 "2018-08-20T13:19:28Z")

</div>

I think there was a lengthy discussion about this, see e.g. the comment [Taking vector transposes seriously · Issue #4774 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/4774#issuecomment-269142361) referring to backwards compatibility.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 2:40pm UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/16 "2018-08-20T14:40:04Z")

</div>

The design of linear algebra operations on vectors and matrices in Julia relies on four types (refer to this nice [presentation](https://www.youtube.com/watch?v=C2RO34b_oPM)): scalar, vector, row vector, matrix. The dispatch then takes care of the correctness of the operations between these objects. There is no need for checking the size of the row vector starting with a singleton 1, for instance. Therefore I would argue that the correct implementation of the function `size` for the row vector should be

```julia
size(v::AdjOrTransAbsVec) = size(v.parent)

```

This would correspond with the notion of the row vector being a one-dimensional object of the same length as its transpose (the “column” vector).

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 20, 2018, 2:59pm UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/17 "2018-08-20T14:59:30Z")

</div>

That’s not how we decided to go. A row vector—i.e. the adjoint of a vector—is embedded into normal arrays by behavioral identification with row matrices. That is, a transposed vector mostly behaves like a flat `1 x n`matrix. It has been the way since 0.6 and it has worked quite well. The only thing that changed in 1.0 is that the specific RowVector type has been generalized to the Adjoint of a vector.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 20, 2018, 3:02pm UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/18 "2018-08-20T15:02:45Z")

</div>

What would bother me about that is the inconsistency of the sizes. So if the row vector is behaviorally identified with a row matrix, why is the vector not identified with a column matrix? Why is the size of a row vector (1, N), but the size of a vector is (N,). Isn’t there a lack of consistency here?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [August 20, 2018, 3:06pm UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/19 "2018-08-20T15:06:11Z")

</div>

I don’t really feel like rehashing the debate, especially since we are definitely not changing it now, so the debate would be fully moot. There’s several thousand comments about this in that issue and Andy Ferris’ PR which implemented the behavior we went with in the end. You can also watch Jiahao’s talk about #4774 last year.

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [August 20, 2018, 3:09pm UTC](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742/20 "2018-08-20T15:09:58Z")

</div>

Could it be at least documented if it isn’t? Something like, one should call `Matrix` on `Transponse` and `Adjoint` to ensure they are treated as the `Matrix` these represent mathematically for matrix multiplication?

[Next page](https://discourse.julialang.org/t/faulty-transpose-and-adjoint-multiplication/13742.md?page=2)
