# Difference between \[1, 2, 3, 4\] / \[1, 2, 3, 4\] vs \[1, 2, 3, 4\] ./ \[1, 2, 3, 4\]

**URL:** <https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999>\
**Category:** New to Julia\
**Tags:** question, vector\
**Created:** [March 1, 2024, 8:12am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999 "2024-03-01T08:12:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [March 1, 2024, 8:12am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/1 "2024-03-01T08:12:23Z")

</div>

Hello, Does someone know what exactly happens in background when we do `[1, 2, 3, 4] / [1, 2, 3, 4]`. For `[1, 2, 3, 4] ./ [1, 2, 3, 4]` is clear that it’s doing element-wise division as shown below.

![image](https://global.discourse-cdn.com/julialang/original/3X/b/f/bf6a0ce2630b3d5fbdbcdbefec10161a201cc7f8.png)

But what about `[1, 2, 3, 4] / [1, 2, 3, 4]` ?

![image](https://global.discourse-cdn.com/julialang/original/3X/c/3/c3071b4b6bace50cb50910b1f3f16112cb1c753e.png)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 1, 2024, 9:01am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/2 "2024-03-01T09:01:04Z")

</div>

> [@Sahil\_Khan](#):
>
> `[1, 2, 3, 4] / [1, 2, 3, 4]` ?

It’s doing a matrix division:

```julia
help?> /

[...]

  ──────────────────────────────────────────────────────

  A / B

  Matrix right-division: A / B is equivalent to (B' \ A')' where \ is the
  left-division operator. For square matrices, the result X is such
  that A == X*B.

```

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [March 1, 2024, 9:54am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/3 "2024-03-01T09:54:09Z")

</div>

What I know about matrix division is that its basically multiplication with inverse of other Matrix so like X = A \* inv(B) is same as X = A/B and I guess it is specific for square matrices.

Now I played around with \ (left division operator) and it seems its works in same way just changes dividend and divisor. So, A/B == B\A.

If thats the case, I will expect

```julia
julia> [1 2 3 4]\ [1 2 3 4]
4×4 Matrix{Float64}:
 0.0333333 0.0666667 0.1 0.133333
 0.0666667 0.133333 0.2 0.266667
 0.1 0.2 0.3 0.4
 0.133333 0.266667 0.4 0.533333

```

and

```julia
julia> [1 2 3 4]/ [1 2 3 4]
1×1 Matrix{Float64}:
 0.9999999999999999

```

to be same which is true in case of sqaure matrix. So, what I’m wondering is how exactly that division happens when I do ` [1 2 3 4] / [1 2 3 4]` ?

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 1, 2024, 9:58am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/4 "2024-03-01T09:58:19Z")

</div>

Essentially if A and B are vectors of the same length there are two ways of multiplying them, either as

- (1 by n matrix) \* (n by 1 matrix) = (1 x 1 matrix), or
- (n by 1 matrix) \* (1 by n matrix) = (n by n matrix).

The two possibilities are what you’re seeing here.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [March 1, 2024, 10:15am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/5 "2024-03-01T10:15:47Z")

</div>

In my case `[1 2 3 4] \ [1 2 3 4]` both are `1*4` Matrix. And In case of vector I think it always taken as n\*1 matrix if we try to multiply it to a matrix example below.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/4/a475e523da9103a0deefe6c6518907ef17c450ea.png)

I understood what you were trying to say about matrix multiplication but I still dont see it answering my question about how that division take place. 😅.

---

<div class="post-metadata">

**Author:** ![mstewart](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mstewart](https://discourse.julialang.org/u/mstewart)\
**Post date:** [March 1, 2024, 10:16am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/6 "2024-03-01T10:16:46Z")

</div>

For rectangular matrices this division corresponds to multiplication by the [Moore-Penrose pseudoinverse](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse). None of these results are going to make much sense unless you know that. You can see the pseudoinverse of a matrix (or vector) with the function `LinearAlgebra.pinv`.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 1, 2024, 10:21am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/7 "2024-03-01T10:21:57Z")

</div>

> [@Sahil\_Khan](#):
>
> I understood what you were trying to say about matrix multiplication but I still dont see it answering my question about how that division take place. 😅.

The exact way can be found with `@edit [1, 2, 3, 4] / [1, 2, 3, 4]`, which shows me this on my machine:

```julia
function (/)(A::AbstractVecOrMat, B::AbstractVecOrMat)
    size(A,2) != size(B,2) && throw(DimensionMismatch("Both inputs should have the same number of columns"))
    return copy(adjoint(adjoint(B) \ adjoint(A)))
end

```

As the docstring says, it takes the adjoint and then does left division, taking the adjoint of the result again.

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 1, 2024, 10:39am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/8 "2024-03-01T10:39:54Z")

</div>

Just to flesh this out a touch: the pseudoinverse of a vector is the transpose divided by it’s squared magnitude (see [here](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse#Vectors)). ie if `v` is a column vector then `pinv(v)` is a row vector, and vice-versa.

The left (`\`) and right (`/`) division is just whether you invert the first or second argument, so you get the two situations I first described: multiplication of (1 x n) with (n x 1) or the other way around.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [March 1, 2024, 10:46am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/9 "2024-03-01T10:46:57Z")

</div>

Thanks I reproduced result with concept of [pseudo-inverse](https://nhigham.com/2023/07/25/what-is-the-pseudoinverse-of-a-matrix/).

I will put results here. So, it can be helpful for others 🙂

Most important think is this :

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/0/d00fbad4b7dcc5db28959daf46855fed255526fb.png)

where x\* is conjugate transpose.

```julia
julia> [1, 2, 3, 4] / [1, 2, 3, 4]
4×4 Matrix{Float64}:
 0.0333333 0.0666667 0.1 0.133333
 0.0666667 0.133333 0.2 0.266667
 0.1 0.2 0.3 0.4
 0.133333 0.266667 0.4 0.533333

```

Now issue was how that division occurs. In normal scenarios we find A/B by A \* B-1 .

So, similarly we can do here by finding pseudo-inverse for [1, 2, 3, 4]

```julia
julia> INV = [1 2 3 4] ./ ([1 2 3 4] * [1 ,2 ,3, 4])
1×4 Matrix{Float64}:
 0.0333333 0.0666667 0.1 0.133333

julia> [1, 2, 3, 4] * INV
4×4 Matrix{Float64}:
 0.0333333 0.0666667 0.1 0.133333
 0.0666667 0.133333 0.2 0.266667
 0.1 0.2 0.3 0.4
 0.133333 0.266667 0.4 0.533333

```

Let me know if you guys don’t see any issue.

Thanks everyone for your inputs 😀

---

<div class="post-metadata">

**Author:** ![mstewart](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mstewart](https://discourse.julialang.org/u/mstewart)\
**Post date:** [March 1, 2024, 10:58am UTC](https://discourse.julialang.org/t/difference-between-1-2-3-4-1-2-3-4-vs-1-2-3-4-1-2-3-4/110999/10 "2024-03-01T10:58:50Z")

</div>

Yes. You picked that up quickly. More generally for a wide matrix A with full rank the pseudoinverse is A^\dagger = A^T (AA^T)^{-1} and A^\dagger b gives the unique solution of minimum norm to an underdetermined system Ax=b. For a tall matrix A with full rank it is A^\dagger = (A^T A)^{-1} A^T and A^\dagger b gives a unique least squares solution to the overdetermined system Ax=b. Even more generally, for any size matrix that is not necessarily of full rank, multiplying by the pseudoinverse gives the least squares solution of minimum norm, which is unique.
