Julia type displaying change between versions

I don’t really follow what you are saying here, if you simply do

julia> [[2, 3], [4, 5]]
2-element Vector{Vector{Int64}}:
 [2, 3]
 [4, 5]

you get a vector of vectors if that is what you want, but if you specify that the elements should be of type Array (what you do when you say Array[...]) you get a vector of arrays. Not sure if that was the problem?

2 Likes

If this is so then it does not make sense:

**julia>** Array[[5,6],[9,78]]

2-element Vector{Array}:

It should rather print:

**julia>** Array[[5,6],[9,78]]

2×2 Matrix{Int64}:

But what if I do

julia> Array[[5,6],[7,8,9]]
2-element Vector{Array}:
 [5, 6]
 [7, 8, 9]

would you want this to be a matrix?

3 Likes

I already see it coming (like with NumPy): even using NumPy for more than 10 years I cannot use a NumPy function without looking it up. Everything is so counter intuitive.

I cannot change the Julia syntax. I will stop arguing here.

But replying to your questions: the problem stems from the fact that:

Julia> Array[[5,6],[7,8,9]]

just seems wrong and it should be:

Julia> Vector[[5,6], [7,8,9]]

Why? T[] with T being a type is syntax for creating a vector that can hold any valid T. If T is Array it will be able to hold any Array, if T is Vector it will be able to hold any Vector.

By explicitly specifying the element type you’re overriding the automatically inferred type, making it less specific.

You’ll still be able to do push!(my_vec, ["foo"]), which I don’t think you want?

They are not the same. If you want Vector[[5,6], [7,8,9]], then write that, and you will get it.

1 Like

The distinction is important, and comes up everywhere in practical coding (and definitely not mainly in linear algebra). Matlab’s failure to distinguish these two is a terrible mistake, that constantly creates problems for Matlab users.

Here is my rant about Matlab’s lack of matrix-vector distinction: Trouble Understanding Slicing - #7 by DNF

6 Likes

IDL data language also does not make a distinction between vectors and arrays as far as I know.

IDL is less used in physics and engineering but was (less so these days) a big player in astrophysics and Earth satellite image processing.

NumPy also has no vectors. And if I convert a list of numbers to a NumPy array I have no idea if NumPy thinks it has to deal with a vector from that moment onwards. It is actually pain.

Julia follows semantic versioning, which means that minor version updates are backwards compatible. Code that worked on v1.0 must work on 1.8, etc. If a new version is introduced that breaks backwards compatibility, then the major version must be updated. In other words, Julia is backwards compatible until v2.0, whenever that might be.

“Backwards compatibility” does not mean “nothing changes”. If it did, then what would be the point of new development? Just make a final version, and stop developing. If you dislike changes, pick a version you like, and never update.

Tutorials based on versions before the first stable release, v1.0, may be broken, but tutorials based on newer versions should not be. If they are, there’s a problem with the tutorial itself.

3 Likes

I presume you mean it doesn’t distinguish vector and matrix. Vectors are a type of array, as is matrix, but they are different. I don’t know IDL, but I linked to a post where I explain why this is bad.

Yes, fortunately, it does have actual vectors.

I should have said “rarely comes up in practice for me”. I don’t claim to be typical in my uses, my point was that there’s clearly a (well thought out) reason for the way things are done, and after a bit of initial confusion on my part, it rarely causes me problems. And when it does, I just put a vec() on it and it works out fine :joy:.

Never came across or heard of vectors in NumPy (I am using Python > 3.8 and a NumPy compatible version in Conda).

What would those vectors be in NumPy?

Maybe you don’t use Matlab :wink: Julia takes care of the distinction for you, so you basically rarely have to think about it. In Matlab, you must always be aware of this, and constantly check explicitly whether you are working with rows or columns.

1 Like

It’s the regular 1D arrays Array creation — NumPy v1.26 Manual

a1D = np.array([1, 2, 3, 4]) 

Perhaps you mean they are not called “vector”? But the point is that you never have to wonder whether to represent a vector as a row or a column. Just always use a flat 1d array.

1 Like

:dart:

I don’t use much of anything other than Julia these days, though I used to do more python/R. But in python I rarely had need of something more performant then a list, and in R it was basically just “read in the CSV and plot it.”

2 Likes

Both are correct syntactically, but they do different things. A Vector is a “flat” collection of items. An Array is a collection of items with “rectangular” shape (of arbitrary dimensionality). A Vector is a kind of Array; a Matrix is also a kind of Array; a 3-dimensional array is also an Array; etc. There is a type hierarchy in julia, and you can use it to confirm that, e.g., Vector is a subtype of Array (written Vector <: Array). Furthermore, a matrix is not the same as a vector-of-vectors (see my reply to an earlier comment of yours below).

T[a, b, c] creates a Vector{T} (a vector that holds things of type T) with elements a, b, and c. [a, b, c] creates a vector where T is inferred from the elements to be the most specific it can be. If a different T is provided, then the vector is initialized to hold that T instead. See:

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

julia> Number[1,2]
2-element Vector{Number}:
 1
 2

Therefore, this behavior follows naturally:

julia> [[1,2], [3,4,5]]
2-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4, 5]

julia> Array[[1,2], [3,4,5]]
2-element Vector{Array}:
 [1, 2]
 [3, 4, 5]

This is incorrect. In all languages that have a multi-dimentional array type, such an array must be “rectangular”. That is, all rows must have the same number of elements; likewise for columns. You cannot have a row of 3 followed by a row of 4. In order to store something like that, you must use an array-of-arrays, which is not the same thing. Since they are not the same thing, and they have vastly different uses, guarantees, and performance trade-offs, it make sense for a language to be able to represent both types, and many languages have done so long before julia.

13 Likes

I know and this was my point. An array consists of vectors of vectors etc.

Yes the vectors have to return the same length.

In NumPy I often have something like this (treating the list as kind of a poor mans vector):

erg=[]
for i in range(5):
   erg.append(xxxx)

np.array(erg)

where xxxx is a NumPy array of the same length and type. And erg now is a 2D array.

In Scheme there are no arrays. I used vectors of vectors to create my own array library up to a certain dimension.

No, an array does not have to be nested. It can contain any type of data, including plain numbers. So [1,2,3] is an array, too, as is a matrix, [1 2 3; 4 5 6], etc.

The point of Array is that it is very general, and be either flat or nested, be of any dimension, (0, 1, 2, 3, …, N, …), and contain any type of elements you want.

julia> [1,2,3] isa Array
true

julia> [1 2 3; 4 5 6] isa Array
true

julia> [[1,2], [3 4; 5 6], 8, "Hello"] isa Array
true
2 Likes