r/AskProgramming • u/[deleted] • Dec 30 '20
Other Why's it called a vector, anyway?
When I hear vector, I think of the geometric concept, not a dynamic size container. Where does the name come from?
This might be a stupid question, but I'm a stupid person.
26
u/video_dewd Dec 30 '20
Here's my interpretation from a data science background. As u/maestro2005 said, it's a fancy name for a 1-dimensional matrix: just a list or array of numbers. The word has a lot of uses. Since most people only ever hear about vectors in geometry, it wouldn't be uncommon for them to remember them only in that context. I would have said something similar before I took vector calculus a couple of years ago.
Vectors can represent essentially anything, not just a point in 2D or 3D space. This is especially important in my field. Vectors here can easily reach millions of elements in length just to represent one thing. For example, I recently replicated this paper for my capstone project. They represent Android apps as a list of similarities they share with other apps. So it really could be anything - an app in consideration might be represented as [123, 345, ..., 762] where 123 is the number of similarities between the app in consideration and the first app we trained on, 345 for the second app, and so on. We can then use this information to classify apps as malware or not.
I wouldn't say they are dynamic containers though. Sure, libraries give us the ability to dynamically expand them. It's often needed after all. But usually you have some idea of what size your vector will be. That way you know what operations you can do on them. This is where the main benefit of using vectors comes from: vectorized operations. These are generally faster methods of performing mathematical operations on vectors than simply iterating through them and it is why they are so common in computing.
8
u/Poddster Dec 30 '20 edited Dec 30 '20
Me too. So I once looked it up.
It comes from the idea that in [many domains of Maths](https://en.wikipedia.org/wiki/Vector_(mathematics_and_physics)), vectors are simply n-sized ordered containers of data, and one of the main uses of the term is in the subfields of matrixes. Similar to tuples.
Infact you can think of the geometric one in the same way. Each point in the N-dimension geometric space is defined by N ordered values, one value for each axis. Hence a vector.
Personally I would have just called it a "dynamic array".
(Though Array is a term that diverges from the Maths one. A Maths array is a matrix, which are usually multi-dimensional, whereas in programming arrays are usually 1d with multidimensional arrays actually being arrays of arrays!)
2
u/balefrost Jan 01 '21
Each point in the N-dimension geometric space is defined by N ordered values, one value for each axis. Hence a vector.
And then we get into the fun discussion of just how similar geometric points and geometric vectors really are. It doesn't really make sense to add two points together, but it does make sense to add two vectors together to get a new vector. It also makes sense to add a vector to a point to get a new point, and to subtract two points to get a vector. You might be able to represent both points and vectors as elements in ℝ3, but you still have to keep them separate.
We kinda sorta solve this in 3D graphics by working in homogenous coordinates. We do 3D math in a 4D space that can separately represent both vectors and points.
14
Dec 31 '20
Arguably it's not a good name for what it is. Vectors in math don't usually change the number of dimensions, while in programming, as you noticed, the number of elements changes. Also, vectors in programming aren't necessarily numeric. I prefer "List" to "Vector" in programming.
8
u/lead999x Dec 31 '20
But list could imply linked list. Dynamic array is probably the most correct term.
6
u/onebit Dec 31 '20
that's why List is a better term, because it could be backed by an array or a linked list.
3
u/munificent Dec 31 '20
because it could be backed by an array or a linked list.
You really want a name that pins down whether it is backed by an array or a linked list. Those two data structures have fundamentally difference performance characteristics so you don't want to hide that from users.
1
1
2
u/lead999x Dec 31 '20
Ah true. I forgot that a list by itself is an abstract data type that could have any internal representation.
Damn I'm job hunting so I have to find ways to keep this all straight.
4
u/WiWWWWWWWWWWWWWWWWWW Dec 31 '20
Unlike everyone else I actually think it's a very good name. In C++ the difference between vectors and lists are that vectors have constant read time for any index, whereas lists are optimized for accessing the ends. Lists are also more easily expandable whereas the idea behind a vector is that it should stay the same size. So if there HAD to be a C++ data structure that would actually represent a math vector, it's going to be a constant size, and you're going to want to be able to access every value in constant time, so the c++ vector is the better choice.
3
u/gcross Dec 31 '20
That is not an accurate description of the tradeoff between these two containers.
std::vectorlets you access elements at an arbitrary position in constant time, and also add elements to the end (and remove them from the end) in amortized constant time (*), whereasstd::listrequires linear time to access elements at an arbitrary position but you can add and remove elements at any position in the collection in constant time (though the coefficient on this time might be larger than that forstd::vectoras it needs to allocate memory more often when elements are added).Also, if what you want is a data structure that represents a math vector, then you don't want either of these containers because they both have dynamic size so it wouldn't make sense to add two
std::vectors as they might have different sizes. In this case what you really want is something more likestd::array, which has a fixed size at compile time.(*) The "amortized" part comes from the fact that occasionally
std::vectorruns out of space when elements are appended so it needs to allocate a larger chunk of memory and then move everything over so occasionally an append takes longer than constant time, so whatstd::vectordoes to keep this from being too expensive is that it grows by a constant factor each time it does this so that the total cost of appending some number of elements grows linearly with the number of elements as it is equal a geometric series.
4
2
Dec 31 '20
It is actually something impossed by C++. The guy who created the vector class in the std has said It is in fact a misleading name, and he would change it if he could.
1
u/carlinwasright Dec 31 '20
In 1979 there was a big movement where we were all going to start calling vectors “mags”, short for magnitude, one of the key elements of a vector. Then in 1980 the movie “Airplane” came out and with everyone saying “What’s your vector, Victor?” mags never stuck.
0
Dec 31 '20
That seems odd. Sure the magnitude of a vector is important, but the direction is what really separates it from a scalar...
1
1
u/amasterblaster Dec 30 '20
Yeah, if you think about it in a mathy way these are the same thing.
This might hurt your brain until you get it.
A vector is a thing that has both magnitude and direction.
4
u/lead999x Dec 31 '20 edited Dec 31 '20
In algebra whether a thing is or isn't a vector is defined by the operations that are valid for that thing. A real number, a 1 dimensional matrix, a more than 1 dimensional matrix, a function, an RGB or CMYK color are all vectors. Even fruits and vegetables could be vectors if you define all the necessary operations on them correctly. That generalizability is what makes linear algebra so useful.
1
4
Dec 31 '20
what is the "direction" in a programming vector?
0
u/amasterblaster Dec 31 '20
well, programming isn't am information discipline so there is no general definition, but in gaming cartesian components are very common. I now work in finance, so momentum metrics are common here. Depends on the data being characterized in my experience.
0
Dec 31 '20
A length n vector in programming is an object that lives in n-dimensional space, so if you are looking for a really intuitive 2D or 3D direction, there isn't one.
2
u/mennovf Dec 30 '20
They are absolutely not the same thing mathematically. You can't "add" arbitrary "std::vector<T>"s a, b;
1
u/amasterblaster Dec 30 '20
" a quantity having direction as well as magnitude, especially as determining the position of one point in space relative to another. "
I'm not sure what you are trying to say, but you may have a good point (whatever it is).
You can absolutely add vectors of the same dimension. Not sure what you are saying.
Are you attempting to say that, depending on how privatives are implemented within different languages you may not be able to mathematically cast them and sum them without adding extra code? If that is your point, then of course. :).
I'm not sure what you are saying exactly :)
3
u/xigoi Dec 31 '20
If you have this:
std::vector<std::string> a { "foo", "bar", "baz" }; std::vector<std::string> b { "quux" };you can't write
a + band expect something meaningful.1
u/amasterblaster Dec 31 '20
Right. This is very specific to one programming language, and not common use of the word vector. What language is OP talking about?
2
u/xigoi Dec 31 '20
The OP talks about using the word “vector” for an array-list, and C++ was the language that popularized this wrong usage of the term.
0
u/amasterblaster Dec 31 '20
Maybe that is how you read and understood it, which makes sense, but is certainly was not what the OP said. I can see now that is what the OP may have meant. Vectors (in the spatial sense) do have dynamic size, so his comments equally applied as a spatial vector with arbitrary dimensions, as are common in almost every programming language. You likely brought your own programming language to the question, as did I. Thus, we both answered correctly w.r.t our own languages. Makes sense. TL:DR; OP never said array list -- they said dynamically sized container, which is a way more generic description (and includes N-dimensional spatial vectors).
1
u/xigoi Dec 31 '20
C++ is not even close to my favorite programming language. In my favorite language, resizable arrays are called
seq, which I consider a quite good name.2
u/mennovf Dec 31 '20
OP is asking about the "vector" concept in programming languages, such as C++ or Haskell, where they are dynamically sized arrays. The only similarity between these programming concepts and the math concepts is that they are ordered 1D collections of the same type. Calling something a "vector", referring to the math concept, without having the capability of 'adding' or 'multiplication with a scalar' is nonsense.
It makes a bit more sense if it's derived from the Latin word itself, but it remains a misnomer imo.0
u/amasterblaster Dec 31 '20
I guess I am only familiar with vectors from C#, unity, python, matlab and C++ (game dev) where it generally denotes the magnitude and direction quantity. Perhaps it would be important for OP to specify which language they are talking about. I'll remember to specify my language in my answer next time
1
u/amasterblaster Dec 30 '20
In 1D, positive denotes the positive direction, negative can denote a negative direction. In decision theory, people denote confidence as vectors. In physical systems this can be force, or velocity. Height, however, is not a vector. (typical) Mass, for example, is a quantity and can not be negative. Hope this helps .... Basically, vectors have direction, and have more information than their quantity alone.
50
u/maestro2005 Dec 30 '20
"Vector" is an incredibly overloaded term. In this case, it's referring to a 1D matrix.