-3
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ consider a language where adjacent string literals get treated as a single literal. Do you see how this could lead to user confusion? $\endgroup$
    – starball
    Commented May 27, 2023 at 4:02
  • $\begingroup$ @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. $\endgroup$ Commented May 27, 2023 at 4:16
  • $\begingroup$ okay I did some light reading. isn't it just to differentiate between various forms of concatenations? $\endgroup$
    – starball
    Commented May 27, 2023 at 4:23
  • $\begingroup$ @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. $\endgroup$ Commented May 27, 2023 at 4:29
  • $\begingroup$ 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. $\endgroup$
    – Antonello
    Commented Nov 12, 2023 at 21:33

1 Answer 1

6
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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) 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? $\endgroup$ Commented May 27, 2023 at 4:48
  • $\begingroup$ 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) $\endgroup$
    – kouta-kun
    Commented May 28, 2023 at 18:39
  • $\begingroup$ 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) ` $\endgroup$
    – kouta-kun
    Commented May 28, 2023 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .