# How to make \`\[0\] == 0\`?

**URL:** <https://discourse.julialang.org/t/how-to-make-0-0/112630>\
**Category:** General Usage\
**Tags:** question, syntax\
**Created:** [April 7, 2024, 6:37am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630 "2024-04-07T06:37:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 7, 2024, 6:37am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/1 "2024-04-07T06:37:33Z")

</div>

```julia
julia> 2 .- [1 1] * [1; 1] == 0
false

```

This means in Julia `[0] != 0`. Although I can understand this rule, I feel it inconvenient to use. Do I have to build awareness of this kind and to code carefully when using Julia, or is there any workaround to make it more convenient?

Thanks

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [April 7, 2024, 6:44am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/2 "2024-04-07T06:44:06Z")

</div>

It really should have been an error to compare two different types like that, but it’s too late now. Anyway yeah you just have to be careful.

---

<div class="post-metadata">

**Author:** ![Wen-Wei\_Tseng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wen-wei_tseng/32/28839_2.png) [@Wen-Wei\_Tseng](https://discourse.julialang.org/u/Wen-Wei_Tseng)\
**Post date:** [April 7, 2024, 6:48am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/3 "2024-04-07T06:48:27Z")

</div>

You could use `iszero(arr)`. [link](https://docs.julialang.org/en/v1/base/numbers/#Base.iszero)

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [April 7, 2024, 8:33am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/4 "2024-04-07T08:33:52Z")

</div>

Unlike, R or Matlab, Julia does not identify vectors and scalars as they are fundamentally different data structures, e.g., vectors are mutable, but scalars not. Thus, you need to take a bit more care about that.

- If you happen to know that your vector/matrix is actually a singleton, you can use `only` to extract the value:

```julia-repl
julia> only(2 .- [1 1] * [1; 1]) == 0
true

```

- The behaviour of R can be replicated via broadcasting:

```julia-repl
julia> 2 .- [1 1] * [1; 1] .== 0
1-element BitVector:
   1

 julia> all(2 .- [1 1] * [1; 1] .== 0)
 true

```

The `all(...)` phrase does basically the same as `iszero` when called on a vector.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 7, 2024, 2:34pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/5 "2024-04-07T14:34:01Z")

</div>

> [@WuSiren](#):
>
> or is there any workaround to make it more convenient?

you could try this form `[itr...]'`, as was pointed out to me in a [recent post](https://discourse.julialang.org/t/is-there-a-built-in-function-for-the-natural-length-of-a-segment-represented-by-an-iterator/112414/8) where I had hastily confused the two expressions

```julia
2 .- [1; 1]' * [1; 1] == 0

```

in the place of

```julia

2 .- [1 1] * [1; 1] == 0

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [April 7, 2024, 2:35pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/6 "2024-04-07T14:35:37Z")

</div>

One other option is to write your code in a way that you’re guaranteed to get a scalar back:

```julia
julia> using LinearAlgebra: dot

julia> 2 - dot([1 1], [1; 1]) == 0
true

```

or even just use vector multiplication:

```julia
julia> 2 - [1, 1]' * [1, 1] == 0
true

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 7, 2024, 4:10pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/7 "2024-04-07T16:10:22Z")

</div>

> [@Alexander\_Knudson](#):
>
> ```julia
> julia> using LinearAlgebra: dot
> 
> julia> 2 - dot([1 1], [1; 1]) == 0
> true
> 
> ```

Good! I think this may be what I most want. Thanks a lot!

---

<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:** [April 7, 2024, 7:36pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/8 "2024-04-07T19:36:30Z")

</div>

Not that `dot([1, 2], [3, 4])` and `[1, 2]' * [3, 4]` are essentially equivalent in Julia, because `[1, 2]'` acts like a covector, as discussed in [Transposes vs 1-row matrices](https://discourse.julialang.org/t/transposes-vs-1-row-matrices/112652)

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 8, 2024, 4:12am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/9 "2024-04-08T04:12:41Z")

</div>

Thanks! I see.

```julia
using LinearAlgebra: dot

a_vec = [1, 2] # 2-element Vector{Int64}
a_col_mat = [1; 2] # 2-element Vector{Int64}
a_row_mat = [1 2] # 1×2 Matrix{Int64}

a_vec == a_col_mat # true
a_vec != a_row_mat' # true
a_vec' * a_vec == a_col_mat' * a_vec # true
a_vec' * a_vec != a_row_mat * a_vec # true

a_vec' * a_vec == dot(a_vec, a_vec) # true
a_vec' * a_vec == dot(a_col_mat, a_vec) # true
a_vec' * a_vec == dot(a_row_mat, a_vec) # true

```

---

<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:** [April 8, 2024, 12:31pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/10 "2024-04-08T12:31:34Z")

</div>

> [@WuSiren](#):
>
> `a_col_mat = [1; 2] # 2-element Vector{Int64}`

To make a 1-column matrix (2d array), you can use `[1; 2;;]`

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 8, 2024, 1:24pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/11 "2024-04-08T13:24:28Z")

</div>

Thank you for making it more complete and accurate, although this kind of syntax makes me feel somewhat confused that I couldn’t find a consistent way to understand Julia’s syntax rules regarding vectors and matrices. 🤔

---

<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:** [April 8, 2024, 1:27pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/12 "2024-04-08T13:27:48Z")

</div>

> [@WuSiren](#):
>
> I couldn’t find a consistent way to understand Julia’s syntax rules regarding vectors and matrices.

It’s explained [here in the manual](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation):

> Using double semicolons for the horizontal concatenation, on the other hand, performs any vertical concatenations before horizontally concatenating the result. […] Just as `;` and `;;` concatenate in the first and second dimension, using more semicolons extends this same general scheme. The number of semicolons in the separator specifies the particular dimension, so `;;;` concatenates in the third dimension, `;;;;` in the 4th, and so on. Fewer semicolons take precedence, so the lower dimensions are generally concatenated first.

(It’s not very common to need to define a 1-column matrix literal expression like `[1; 2;;]`, and the syntax for this was only added to Julia fairly recently. I don’t think I’ve ever used it in practice myself. You can safely ignore the `;;…` corner of the language if you find it confusing.)

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 8, 2024, 1:28pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/13 "2024-04-08T13:28:46Z")

</div>

Oh! Many many thanks! 🤝 🤝

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 8, 2024, 1:37pm UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/14 "2024-04-08T13:37:59Z")

</div>

> [@stevengj](#):
>
> Using double semicolons for the horizontal concatenation

Thank you so much, now I know that I can also define a matrix by columns in this way!

```julia
julia> [1;2;;3;4]
2×2 Matrix{Int64}:
 1 3
 2 4

```

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 29, 2024, 7:41am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/15 "2024-04-29T07:41:18Z")

</div>

More strange things confused me: **A column vector isn’t equal to a single-column matrix but a row vector is equal to a single-row matrix.**

Why?

```julia
julia> col_vec = [1, 2]
2-element Vector{Int64}:
 1
 2

julia> row_vec = col_vec'
1×2 adjoint(::Vector{Int64}) with eltype Int64:
 1 2

julia> one_col_mat = [1; 2;;]
2×1 Matrix{Int64}:
 1
 2

julia> one_row_mat = [1 2]
1×2 Matrix{Int64}:
 1 2

julia> one_col_mat'
1×2 adjoint(::Matrix{Int64}) with eltype Int64:
 1 2

julia> one_row_mat'
2×1 adjoint(::Matrix{Int64}) with eltype Int64:
 1
 2

julia> col_vec == one_col_mat
false

julia> col_vec == one_row_mat'
false

julia> row_vec == one_col_mat'
true

julia> row_vec == one_row_mat
true

julia> one_col_mat == one_row_mat'
true

julia> one_row_mat == one_col_mat'
true

```

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [April 29, 2024, 8:09am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/16 "2024-04-29T08:09:14Z")

</div>

> [@WuSiren](#):
>
> More strange things confused me: **A column vector isn’t equal to a single-column matrix but a row vector is equal to a single-row matrix.**
> 
> Why?

There is no such thing as a row-vector in julia. When you transpose or form the adjoint of a `Vector` you get a 2-dimensional structure, essentially a one-row matrix.

```julia
julia> a = rand(4)
4-element Vector{Float64}:
 0.2034574270239713
 0.7019936882200452
 0.9387129254650988
 0.5892713898810321

julia> b = a'
1×4 adjoint(::Vector{Float64}) with eltype Float64:
 0.203457 0.701994 0.938713 0.589271

julia> size(a)
(4,)

julia> size(b)
(1, 4)

```

But, I agree that it’s a bit confusing, because the adjoint acts as a co-vector, not as a 1x4 matrix when multiplying with a vector.

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 29, 2024, 8:19am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/17 "2024-04-29T08:19:12Z")

</div>

But they have different types:

```julia
julia> typeof(row_vec)
LinearAlgebra.Adjoint{Int64, Vector{Int64}}

julia> typeof(one_row_mat)
Matrix{Int64} (alias for Array{Int64, 2})

julia> typeof(row_vec) == typeof(one_row_mat)
false

```

How can two instances from different types equal to each other? Given

> [@jar1](#):
>
> It really should have been an error to compare two different types like that

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [April 29, 2024, 8:57am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/18 "2024-04-29T08:57:41Z")

</div>

> [@WuSiren](#):
>
> But they have different types:
> 
> ```julia
> julia> typeof(row_vec)
> LinearAlgebra.Adjoint{Int64, Vector{Int64}}
> 
> julia> typeof(one_row_mat)
> Matrix{Int64} (alias for Array{Int64, 2})
> 
> julia> typeof(row_vec) == typeof(one_row_mat)
> false
> 
> ```
> 
> How can two instances from different types equal to each other? Given

The type system is more powerful than that. In general, comparing types with `==` is usually a bad idea. Use `isa` or `<:` instead.

In particular, relevant for this case:

```julia-repl
julia> LinearAlgebra.Adjoint <: AbstractMatrix
true

julia> Matrix <: AbstractMatrix
true

```

BTW, asking questions here is completely fine, but TBH I think using the Manual more would be an efficiency win for you, given that I think all your questions are answered in the Manual.

BTW, in case you’re interested, the docs for `<:` are better in the in-development version of Julia than for the released version, here’s the link: [`<:`](https://docs.julialang.org/en/v1.12-dev/base/base/#Core.:%3C:)

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [April 29, 2024, 11:21am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/19 "2024-04-29T11:21:10Z")

</div>

Thanks!

> [@nsajko](#):
>
> BTW, asking questions here is completely fine, but TBH I think using the Manual more would be an efficiency win for you

Thank you for your advice! I completely agree and acknowledge that I need to improve my efficiency in accurately locating my issues in the documentation. It is undeniable that asking questions here has provided me with a lot of friendly help (including yours), which has increased my familiarity with the documentation and helped me learn things beyond the documentation. For example, I learned from you that

> [@nsajko](#):
>
> the docs for `<:` are better in the in-development version of Julia than for the released version

This information has been very helpful.

> [@nsajko](#):
>
> I think all your questions are answered in the Manual.

I apologize for the presumption, but I cannot agree with your viewpoint that seems too absolute. As a novice in the Julia language, although some of my questions may appear elementary to you, the fact is that through my interactions and discussions with the community here, as well as my own exploration, I have discovered several (potential) bugs in Julia itself or in Julia packages (perhaps more than five recently?) and raised some issues on GitHub. Don’t you think this is beneficial for Julia, its developers, and its users? If your answer is negative, I will try to minimize my exploration and inquiries regarding such issues.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 29, 2024, 11:32am UTC](https://discourse.julialang.org/t/how-to-make-0-0/112630/20 "2024-04-29T11:32:44Z")

</div>

Please continue asking questions, they are absolutely welcome. Each answer here (even if only pointing to the proper manual page) helps other users to find answers to similar questions.

[Next page](https://discourse.julialang.org/t/how-to-make-0-0/112630.md?page=2)
