<h1>Introduction (Python, MATLAB/Octave, Julia)</h1>
<p>One of things about MATLAB/Octave that I find very convenient is the simplicity when defining vectors. For example:</p>
<pre><code>v=[0 0 5];
u=[1 0 2];
dot(u,v)                % Result: 10
</code></pre>
<p>This is in contrast to Python which has a much more verbose syntax:</p>
<pre><code>import numpy as np
v = np.array([0,0,2]);
u = np.array([1,0,5]);
np.dot(u, v)            % Result: 10
</code></pre>
<p>Julia has many of the benefits of Python, while also maintaining the simple and very convenient MATLAB/Octave syntax for matrices:</p>
<pre><code>A = [1 0 2; 3 -1 2; 9  1 7]; 
B = [1 0 1; 4 -2 6; 2 -1 3];
A*B

%%% Result:
%  5  -2   7
%  3   0   3
% 27  -9  36
</code></pre>
<h1>A strange requirement in Julia</h1>
<p>However, the following does not work in Julia:</p>
<pre><code>v = [0 0 2];
u = [1 0 5];
dot(v,u)
ERROR: MethodError: `dot` has no method matching dot(::Array{Int64,2}, ::Array{Int64,2})

You might have used a 2d row vector where a 1d column vector was required.
Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].
You can convert to a column vector with the vec() function.
</code></pre>
<p>In order to do the dot product in Julia this way, I need to <em><strong>add commas</strong></em> between the elements when defining the vectors:</p>
<pre><code>v = [0, 0, 2];
u = [1, 0 ,5];
dot(v,u)              % Result: 10
</code></pre>
<h1>Why does Julia require the <em>extra</em> commas?</h1>

<syntax><language-comparison><python><matlab-octave><julia>
In Julia, what is the benefit of requiring commas between elements when defining a vector for a dot product?
CC BY-SA 4.0
2023-05-27T04:02:41.870Z
consider a language where adjacent string literals get treated as a single literal. Do you see how this could lead to user confusion?
starball
CC BY-SA 4.0
2023-05-27T04:16:50.487Z
@starball I had thought about that. Are you saying that the commas are helpful? If so, why are they not needed in Julia for the A*B matrix multiplication example that I gave? Semicolons are needed for specifying where the new rows are, but now commas are needed for the vectors that make each row.
Nike Dattani
CC BY-SA 4.0
2023-05-27T04:23:23.290Z
okay I did some light reading. isn't it just to differentiate between various forms of [concatenations](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation)?
starball
CC BY-SA 4.0
2023-05-27T04:29:58.530Z
@starball in that link that you provided, I see that it says "julia> [1 2 3] # Numbers can also be horizontally concatenated". Notice that there's no commas in the [1 2 3]? So we can define a vector without commas (which is good, because it's exactly how MATLAB/Octave have done it since the 1970s), but then the vector in Julia can't be used in a dot product unless it has commas.
Nike Dattani
CC BY-SA 4.0
2023-11-12T21:33:11.300Z
In Julia arrays the space is used to separate _columns_,  `v` and `u` in the version without commas are matrices, not vectors (albeit of a single row), so the dot product is not defined.
Antonello
<p>As hinted by the error message:
<code>Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].</code></p>
<p>The two literal syntaxes define different objects, one being a one-dimensional vector equivalent to np.array.shape = (3,), the other being a two-dimensional single-row vector equivalent to np.array.shape = (1,3). Likely, numpy does implicit conversion between shapes where Julia prefers to require the exact shape.</p>

CC BY-SA 4.0
2023-05-16T18:05:21.033Z
2023-05-16T18:05:21.033Z
kouta-kun
CC BY-SA 4.0
2023-05-27T04:48:20.657Z
Thanks, but this leaves me with a few questions. First of all, is `np.array.shape` even correct syntax? I tried it and got an error, and I can't seem to find that syntax online either. This is the second time recently (the first was [here](https://mattermodeling.stackexchange.com/a/10258/5)) in which someone's included Python syntax in their answer that didn't work for me, so I wonder if it's me. Next, why would it allow us to do the dot product for arrays of the form (3,) but not for arrays of the form (1,3) or *vice versa*? Returning to the title of my question: why would Julia care?
Nike Dattani
CC BY-SA 4.0
2023-05-28T18:39:26.203Z
It's not correct syntax in that I seem to have misnamed the classname: it's actually `np.ndarray.shape`, for example: `a = np.asarray([[1,2,3]]); print(a.shape)`
kouta-kun
CC BY-SA 4.0
2023-05-28T18:47:20.050Z
As for why Julia might care about that, only the language designers may know but fundamentally a row and a matrix with a single row are not the same thing. Dot products for example are defined only for vectors and not for matrixes. Numpy chooses to implement matrix dot product as matrix multiplication, leading to the following confusing case:
`>>> a = np.asarray([[1,2,3]])`
`>>> np.dot(a[0],a[0])`
`14`
`>>> np.dot(a,a)`
`ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)`
`
kouta-kun
# In Julia, what is the benefit of requiring commas between elements when defining a vector for a dot product?
Posted by: username_0 on 2023-05-16
Introduction (Python, MATLAB/Octave, Julia)
One of things about MATLAB/Octave that I find very convenient is the simplicity when defining vectors. For example:
v=[0 0 5];
u=[1 0 2];
dot(u,v)                % Result: 10

This is in contrast to Python which has a much more verbose syntax:
import numpy as np
v = np.array([0,0,2]);
u = np.array([1,0,5]);
np.dot(u, v)            % Result: 10

Julia has many of the benefits of Python, while also maintaining the simple and very convenient MATLAB/Octave syntax for matrices:
A = [1 0 2; 3 -1 2; 9  1 7]; 
B = [1 0 1; 4 -2 6; 2 -1 3];
A*B

%%% Result:
%  5  -2   7
%  3   0   3
% 27  -9  36

A strange requirement in Julia
However, the following does not work in Julia:
v = [0 0 2];
u = [1 0 5];
dot(v,u)
ERROR: MethodError: `dot` has no method matching dot(::Array{Int64,2}, ::Array{Int64,2})

You might have used a 2d row vector where a 1d column vector was required.
Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].
You can convert to a column vector with the vec() function.

In order to do the dot product in Julia this way, I need to add commas between the elements when defining the vectors:
v = [0, 0, 2];
u = [1, 0 ,5];
dot(v,u)              % Result: 10

Why does Julia require the extra commas?
**Tags:** syntax language-comparison python matlab-octave julia
> **Comments:**
> **username_1**: consider a language where adjacent string literals get treated as a single literal. Do you see how this could lead to user confusion?  
> **username_0**: @starball I had thought about that. Are you saying that the commas are helpful? If so, why are they not needed in Julia for the A*B matrix multiplication example that I gave? Semicolons are needed for specifying where the new rows are, but now commas are needed for the vectors that make each row.  
> **username_1**: okay I did some light reading. isn't it just to differentiate between various forms of [concatenations](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation)?  
> **username_0**: @starball in that link that you provided, I see that it says "julia> [1 2 3] # Numbers can also be horizontally concatenated". Notice that there's no commas in the [1 2 3]? So we can define a vector without commas (which is good, because it's exactly how MATLAB/Octave have done it since the 1970s), but then the vector in Julia can't be used in a dot product unless it has commas.  
> **username_2**: In Julia arrays the space is used to separate _columns_,  `v` and `u` in the version without commas are matrices, not vectors (albeit of a single row), so the dot product is not defined.  
---
## 1 answers
### Answer by username_3 on 2023-05-16. Score: 6
As hinted by the error message:
`Note the difference between 1d column vector [1,2,3] and 2d row vector [1 2 3].`

The two literal syntaxes define different objects, one being a one-dimensional vector equivalent to np.array.shape = (3,), the other being a two-dimensional single-row vector equivalent to np.array.shape = (1,3). Likely, numpy does implicit conversion between shapes where Julia prefers to require the exact shape.
> **Comments:**
> **username_0** (2023-05-27): Thanks, but this leaves me with a few questions. First of all, is `np.array.shape` even correct syntax? I tried it and got an error, and I can't seem to find that syntax online either. This is the second time recently (the first was [here](https://mattermodeling.stackexchange.com/a/10258/5)) in which someone's included Python syntax in their answer that didn't work for me, so I wonder if it's me. Next, why would it allow us to do the dot product for arrays of the form (3,) but not for arrays of the form (1,3) or *vice versa*? Returning to the title of my question: why would Julia care?  
> **username_3** (2023-05-28): It's not correct syntax in that I seem to have misnamed the classname: it's actually `np.ndarray.shape`, for example: `a = np.asarray([[1,2,3]]); print(a.shape)`  
> **username_3** (2023-05-28): As for why Julia might care about that, only the language designers may know but fundamentally a row and a matrix with a single row are not the same thing. Dot products for example are defined only for vectors and not for matrixes. Numpy chooses to implement matrix dot product as matrix multiplication, leading to the following confusing case:
`>>> a = np.asarray([[1,2,3]])`
`>>> np.dot(a[0],a[0])`
`14`
`>>> np.dot(a,a)`
`ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)`
`  
---
