I tried to pick an exact and explicit title for this question but failed. So I guess you must read my entire question to understand the issue.
In Julia, we can define a random 2*5 Matrix using rand(2,5)
:
julia> a = rand(2,5)
2×5 Matrix{Float64}:
0.471737 0.726199 0.575673 0.760658 0.300233
0.443542 0.92782 0.501209 0.902491 0.171455
And what amazed me (Also warned me a lot) is that you can change the values of a[1, 1:4]
by assigning (=) a 2*2 Matrix:
julia> a[1 , 1:4] = [1 2;3 4]
2×2 Matrix{Int64}:
1 2
3 4
julia> a
2×5 Matrix{Float64}:
1.0 3.0 2.0 4.0 0.300233
0.443542 0.92782 0.501209 0.902491 0.171455
I mean, This is not good at all and can cause many silent problems in many algorithms! How and most importantly, why even is this possible to assign a matrix into a piece of a vector! I mean, a[1, 1:4]
is a vector:
julia> a[1, 1:4]
4-element Vector{Float64}:
1.0
3.0
2.0
4.0
Which belongs to the a
matrix, and here I changed that vector with the values of a matrix!! I expected that Julia would throw an error saying: “that dimensions don’t match, so how the hell are you expecting me (Julia) to do that?”.
But not also Julia didn’t throw anything, but it also amazed me in that way! Why is that possible?