Array type modification

A final example. I only define vectors! This is natural for a one dimensional problem, right? I did a slice operation, and it is not working.

using LinearAlgebra
a=zeros(10)
b2=-ones(3)
b1=a[1:3,:].+5
c1=b1'*b2
det(c1)

Vectors and matrices are completely different things. A matrix is a linear map that acts on a vector. The determinant of a vector doesn’t exist. Etc. If you want a matrix, define a matrix. The dimension of the problem is irrelevant.

If you are worried that a and b2 are inputs given by other people, then you can define a wrapper function that takes in vectors and calls the matrix function with a slice. If only you will use the function, then you can just pass a matrix, and only accept matrix arguments. The following is not recommended but here is a workaround for this specific case:

@views det(c1[:,:])

@views is to avoid extra allocations.

Elaborating on what I mean:

using LinearAlgebra

mydet(a, b) = @views mydet(a[:,:], b[:,:])
function mydet(a::AbstractMatrix, b::AbstractMatrix)
	return det(a'*b)
end

a=zeros(10)
b2=-ones(3)
b1=a[1:3,:].+5
mydet(b1, b2)

I know the determinant of a vector doesn’t exist, however, the determinant of a number is ok. If I multiply a row with a vector, I get a number, and the determinant of a number is ok.

Now Julia return a vector, rather than a number, which is not intended. What I need is a number for a one dimensional problem, which was stated in my original post.

Anyway, I should never slice an array out from a vector without an error message, and this is what I get from Julia!

Why are you slicing a vector with a[1:3,;] instead of a[1:3] ?
When you do that you are explicitly asking for an Array{T,2}.

Note that:

julia> a = zeros(5)
5-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> a[1:2]
2-element Array{Float64,1}:
 0.0
 0.0

julia> a[1:2,:]
2×1 Array{Float64,2}:
 0.0
 0.0

julia> a[1:2,:,:]
2×1×1 Array{Float64,3}:
[:, :, 1] =
 0.0
 0.0

julia> a[1:2,:,:,:]
2×1×1×1 Array{Float64,4}:
[:, :, 1, 1] =
 0.0
 0.0

julia> a[1:2,:,:,:,:]
2×1×1×1×1 Array{Float64,5}:
[:, :, 1, 1, 1] =
 0.0
 0.0

This is your central mistake, I think. You are working with matrices, and then, when you have an Nx1 matrix, you think “Oh, that’s the same as a vector!”, and you switch to vectors. This is the behaviour of Matlab (but not Julia or Numpy!) Sometimes it’s convenient, but more often it is a serious annoyance! (speaking as a long-time Matlab user, here.)

When you have a d-dimensional problem, you use an Nxd dimensional matrix. Do this consistently, even when d = 1.

I’m glad Julia distinguishes between vectors and Nx1 matrices, it’s actually one of my favourite ‘little things’ in Julia, and a huge relief coming from Matlab.

If someone uses your code, and inputs a vector where there should be an Nx1 matrix, they are making a mistake (albeit an understandable one). And if you make a mistake, the best thing that can happen is that you get an error.

Many thanks. I think there are already many solutions to the questions that I posted originally.
The example just shows that the square bracket operator in Julia lacks rigorous definition and documentation.

I would prefer some solid justifications for your statements here. In Linear Algebra, a vector is a Nx1 matrix, and it is what has been defined in Matlab. You can see this definition here: Row and column vectors - Wikipedia
and some other books if you like.

I don’t know what is your definition of a vector, and what is the point of separating a vector from a Nx1 matrix?

I’m pretty sure this is wrong, but it is common to conflate them in linear algebra courses because it is convenient to treat them the same in many cases.

But I was not talking about theoretical linear algebra, I was talking about types in programming languages, and here, an Nx1 dimensional matrix is definitely not the same as a length N vector. Suddenly switching types because d happens to equal to 1, is inconsistent.

And I was also talking about programming convenience. The conflation of vectors and matrices in Matlab has caused me endless problems (particularly because Matlab can never decide whether to treat Nx1 or 1xN as the default type of vector).

I like Jiahao Chen’s talk about this: https://www.youtube.com/watch?v=C2RO34b_oPM

In particular that there are two different definitions of vectors.

Definition 1: A vector is a column of a matrix
Definition 2: A vector is that which has the algebra of a vector space

Definition 1 is your definition (an Nx1 matrix) and it’s what Matlab uses. But Definition 2 is a more traditional definition of what a vector is and, if I’ve understood correctly, it’s what Julia uses.

In my opinion, a scientific programming language has to be consistent with a basic mathematical definition.

In terms of Matlab, when you define a vector, you have to specify whether it is a Nx1 or 1xN vector from the very beginning, and I never experienced any problem with it.

Thanks for the link. I will have a look. I think the two definitions shall be consistent with each other, at least In Linear Algebra.

They are. A vector and a matrix are different things according to mathematical definitions. It is only in basic linear algebra that they don’t bother to make the distinction.

Matlab is column-major, and most array functions work along the first non-singleton dimension. But most constructive functions (such as linspace, 1:n, [1,2,3], {a,b,c}, 'abc', etc, etc, etc.) return row-vectors! Figure that!! All over the place I have to put in a(:) or a(:)' if I need a column or a row, and am unsure what I will be receiving. And if it turns out that it’s actually an NxM matrix, I ruin the matrix.

Gigantic pain in the neck! I have cursed that conflation countless times, and I am super happy to be rid of it.

2 Likes

[quote=“DNF, post:31, topic:13496, full:true”]

Please make sure you present evidence or justification for your statements when you say 'this is wrong or this is common sense etc. ’ If you think a mathematical definition is wrong, then give your evidence, rather than just a statement.

I don’t see what is the problem here. Just like Linear Algebra, the vector addition, multiplication etc obey the same rules as matrix addition, multiplication etc. If we understand these Linear Algebra rules, I don’t understand what is the problem of transposing a vector.

On the other hand, what we are discussing is much more fundamental than this. It is the definition of a Linear Algebra term, rather than the creation of a row vector or a column vector in some applications.

This is going to be hopeless, because the mathematical literature is wildly inconsistent, that is, it talks about vectors in different ways. Sometimes, they just say ‘Oh, a vector is a kind of matrix’. Other books forcefully assert that they are different. A matrix is a linear operator, and a vector is the sort of object it operates on.

My tensor calculus book states that a vector is ‘a directed segment’, and is completely separate from its representation as a list of numbers. That is, [1,2,5]is not even a vector! It is just a representation of an underlying vector in a specific basis.

Look at your computer screen, a vector points from the tip of your nose to the center of the screen. What numbers represent it? That depends what coordinate system you choose. You can express it according to the orientations of the walls in the room with the origin at the door, or you could put the origin at your nose, or you can express it in terms of the cardinal directions of the earth. But it’s all the same vector.

So here’s the problem: there are many working definitions of a vector. The definition you are used is not the most useful one, and Julia has chosen a different one.

1 Like

Please look the first line of my code: using Linear Algebra, and all the code after.
Did I use geometry or quantum mechanics where you are not even you?

Vectors and matrices are not defined in the LinearAlgebra library. They are defined in Base.

Also, I did not say a vector is a matrix in linear algebra. I said that in parts of linear algebra they are conflated, because the differences are not important.

Welcome to Julia,

Please consider what has been written already in this thread, before continuing what appears to be an argument going nowhere.

In Julia, we have two abstract types: vectors (AbstractVector) and matrices (AbstractMatrix). They are not the same, at all. They are distinct abstractions. This fits well with many mathematical texts. It doesn’t fit well with many others. Since mathematics as a whole does not agree how to define a vector, we cannot be in compliance with all of mathematics. This has been discussed a lot in the Julia community.

Please watch JuliaCon 2017 | Taking Vector Transposes Seriously | Jiahao Chen - YouTube

If you’re not convinced, read

Please read https://github.com/JuliaLang/julia/issues/4774

The problem is not that we don’t know what we’re doing. The problem is not lack of documentation. The problem is not lack of mathematical rigour. The only problem is that you’re new to Julia, and you just need to get used to it. This is addressed in the video - please watch it.

Luckily, lots of people are willing to help!

4 Likes