Why is a C++ Vector called a Vector? [closed]
Asked Answered
A

16

210

The question's pretty self-explanatory really. I know vaguely about vectors in maths, but I don't really see the link to C++ vectors.

Asia answered 24/2, 2009 at 11:30 Comment(1)
Vectors (Magnitute + direction) in physics is the first thing which hits my mind the moment I hear the word vectors.Anglosaxon
M
124

Mathematical definition of a vector is a member of the set Sn, which is an ordered sequence of values in a specific set (S). This is what a C++ vector stores.

Mahon answered 24/2, 2009 at 11:36 Comment(14)
I see. I always imagined vectors as 2- or 3-dimensional, so I didn't see the connection with multi-dimensional vectors...Asia
Vectors are only typically considered 2-3 dimensional because of their use in physics. But more generally in mathematics, they just mean a set of numbers that has an order (mathematical sets are orderless, they're like a bag filled with stuff). A vector can have any number of elements.Haymo
@Asia you're confusing vector in geometry or physics (Euclidean vector) with vector in linear algebra (coordinate vector).Harlen
So the real question is why is a physics vector (magnitude+direction) called a vector?Chopin
vartec, can't a Euclidean vector be represented as a coordinate vector and vice versa? They're just different representations of the same thing (a tuple) in Euclidian space versus the more general vector space.Tailrace
@Joseph Garvin: Vectors don't even need to have components that are numbers. For example, certain sets of functions can be used to form vector spaces where the components are functions.Stylolite
A vector is literally a "carrier". The same word is used for (e.g.) insects that transmit disease, and comes from the same Latin root as "vehicle". So it's something that takes you from one place to another. Incidentally, the word "matrix" is also from Latin, meaning "womb".Detached
Vector can have as many dimensions as you want -- two for plane, three for space. @Beckett: It doesn't have magnitude+direction, it just represents a point by it's coordinates. The direction is from (0;0;0) to this point and the magnitude is calculated from the components. Interesting to see so much confusion about something so simple. However the vector from cpp std lib have nothing to do with it.Urgent
@Machta: en.wikipedia.org/wiki/Euclidean_vector "a Euclidean vector ...is a geometric object that has a magnitude (or length) and direction..." "a vector can be represented by identifying the coordinates of its initial and terminal point." "A bound vector is [represented] by the coordinates of the terminal point, its initial point always having the coordinates of the origin O = (0,0,0)." (emphasis mine)Arhna
I don't see how this could be a mathematical definition of vector. If S can be an arbitrary sets, then how do you add vectors or multiply vectors by scalers? The set of all functions from Real Numbers to Real Numbers with point-wise multiplication and addition form a vector space. How do I represent vector from this space as a finite sequence, or even a countably infinite sequence? Where does this definition come from?Dwaynedweck
@RobertCooper The "definition" is not mathematically rigorous, but it roughly describes what people write down on paper as "vectors".Mahon
vectors are members of any set V such that there are two important operations defined on this set: + (addition of vectors) and x (multiplication of a vector by a scalar from field F) and 8 axioms are satisfied by these operationsShahjahanpur
These seems unsatisfactory because: 1. Mathematical vectors don't change in dimension, 2. Mathematical vectors have operations that std::vector does not provide, 3. The argument that std::vector is an ordered sequence of values could apply to std::list, std::deque, std::basic_string, etc.Terzetto
The mathematical definition of a vector is that it's an element of a vector set. Your definition simply isn't correct; if you want to say "this is roughly what a vector is" then change the wording, currently it's made to sound like it has a level of precision that it simply doesn't.Macbeth
C
278

It's called a vector because Alex Stepanov, the designer of the Standard Template Library, was looking for a name to distinguish it from built-in arrays. He admits now that he made a mistake, because mathematics already uses the term 'vector' for a fixed-length sequence of numbers. C++11 compounds this mistake by introducing a class 'array' that behaves similarly to a mathematical vector.

Alex's lesson: be very careful every time you name something.

Caruso answered 17/4, 2009 at 0:12 Comment(8)
But array will also don't use heap allocation which makes moving it less efficiently. We also have std::valarray, btw.Cavallaro
You can allocate it on the heap is cheap move is desired.Anytime
Nice to know that they learned that lesson even though it's too late. Although ... typedef or #define can fix it too.Brien
Alex explains this, for example, in one of the lectures at Amazon A9 here: youtube.com/watch?v=etZgaSjzqlUTamratamsky
@Drop, thank you for the link. The guy starts at 6 minute for the naming.Droll
if you are going to claim "[Alex Stepanov] admits now that he made a mistake" in your answer, would you please provide a citation?Finery
@TrevorBoydSmith The comment about it being a mistake can be found in his book "From mathematics to Generic programming"Bootie
C++ naming is horrible. They also misuse functor! Even the name is a wrong choice, since ++C is the suggested use for increment.Quintonquintuple
M
124

Mathematical definition of a vector is a member of the set Sn, which is an ordered sequence of values in a specific set (S). This is what a C++ vector stores.

Mahon answered 24/2, 2009 at 11:36 Comment(14)
I see. I always imagined vectors as 2- or 3-dimensional, so I didn't see the connection with multi-dimensional vectors...Asia
Vectors are only typically considered 2-3 dimensional because of their use in physics. But more generally in mathematics, they just mean a set of numbers that has an order (mathematical sets are orderless, they're like a bag filled with stuff). A vector can have any number of elements.Haymo
@Asia you're confusing vector in geometry or physics (Euclidean vector) with vector in linear algebra (coordinate vector).Harlen
So the real question is why is a physics vector (magnitude+direction) called a vector?Chopin
vartec, can't a Euclidean vector be represented as a coordinate vector and vice versa? They're just different representations of the same thing (a tuple) in Euclidian space versus the more general vector space.Tailrace
@Joseph Garvin: Vectors don't even need to have components that are numbers. For example, certain sets of functions can be used to form vector spaces where the components are functions.Stylolite
A vector is literally a "carrier". The same word is used for (e.g.) insects that transmit disease, and comes from the same Latin root as "vehicle". So it's something that takes you from one place to another. Incidentally, the word "matrix" is also from Latin, meaning "womb".Detached
Vector can have as many dimensions as you want -- two for plane, three for space. @Beckett: It doesn't have magnitude+direction, it just represents a point by it's coordinates. The direction is from (0;0;0) to this point and the magnitude is calculated from the components. Interesting to see so much confusion about something so simple. However the vector from cpp std lib have nothing to do with it.Urgent
@Machta: en.wikipedia.org/wiki/Euclidean_vector "a Euclidean vector ...is a geometric object that has a magnitude (or length) and direction..." "a vector can be represented by identifying the coordinates of its initial and terminal point." "A bound vector is [represented] by the coordinates of the terminal point, its initial point always having the coordinates of the origin O = (0,0,0)." (emphasis mine)Arhna
I don't see how this could be a mathematical definition of vector. If S can be an arbitrary sets, then how do you add vectors or multiply vectors by scalers? The set of all functions from Real Numbers to Real Numbers with point-wise multiplication and addition form a vector space. How do I represent vector from this space as a finite sequence, or even a countably infinite sequence? Where does this definition come from?Dwaynedweck
@RobertCooper The "definition" is not mathematically rigorous, but it roughly describes what people write down on paper as "vectors".Mahon
vectors are members of any set V such that there are two important operations defined on this set: + (addition of vectors) and x (multiplication of a vector by a scalar from field F) and 8 axioms are satisfied by these operationsShahjahanpur
These seems unsatisfactory because: 1. Mathematical vectors don't change in dimension, 2. Mathematical vectors have operations that std::vector does not provide, 3. The argument that std::vector is an ordered sequence of values could apply to std::list, std::deque, std::basic_string, etc.Terzetto
The mathematical definition of a vector is that it's an element of a vector set. Your definition simply isn't correct; if you want to say "this is roughly what a vector is" then change the wording, currently it's made to sound like it has a level of precision that it simply doesn't.Macbeth
P
75

An excerpt from The C++ Programming Language by Bjarne Stroustrup:

"One could argue that valarray should have been called vector because it is a traditional mathematical vector and that vector should have been called array. However, this is not the way the terminology evolved."

Prudential answered 24/2, 2009 at 13:41 Comment(3)
Pfftt.. what does that guy know. I've never even heard of this "Bjarne Stroustrup" person.Tailrace
I just wonder how many of the upvotes of the previous comment are actually a result of an underflow...Agler
Well, no, Bjarne. Array is very clearly a stack-allocated (unless declared static) fixed size (unless VLA) contiguous (at least in virtual memory) region of memory usually used to store elements of a single type. A better idea would be to call it da or dyarr or dyarray as it is a dynamically allocated array. Simply calling it an "array" would be no good.Nerveracking
H
17

The name comes from the linear algebra, where vector is matrix with only one column or only one row.

Harlen answered 24/2, 2009 at 11:36 Comment(0)
P
15

To complement the excellent response from @MarkRuzon:

Alex said that to give a name to what is now called std::vector he observed the name that Scheme and Common Lisp ​​had given to similar data structures.

Later he admits he was wrong because C++ vector has nothing to do with the vectors in mathematics.

He also says that he introduced an error of a community of 50 people to a community of 5 million people, so the error is likely to remain forever.

Parke answered 10/1, 2014 at 17:13 Comment(0)
S
10

Just to say why it probably isn't called array: Because std::vector has a dynamic size. An array conceptually is fixed in length. Next C++ Standard by the way has a std::array template, which is fixed in size and should be preferred over a plain array:

std::array<int, 4> f = { 1, 2, 3, 4 };
Shire answered 24/2, 2009 at 11:31 Comment(6)
Has that new C++ standard been released yet?Dotti
this isn't much of an answer, since a vector has fixed size too. vectors are pure values, they are elements in a set that together with some additional structure form an abstract vector space. the notion of "changing the size" of a vector is complete nonsense.Inconspicuous
@kai i think there's a confusion here. I have merely argued for why it is not called array. I have not tried to explained why it's specifically called vector. An array in C++ even before standardisation of the language has always had a fixed size, so naming the resizable class std::array would be no good.Shire
now you can rightly complain that this isn't much of an answer, but a valid comment. Insofar I agree with the first half-sentence (up to the comma) of your comment. However, now it's too late to move my answer into a comment on the question, because these useful comments of you and @user12 would be lost.Shire
As a compromise to solve the dilemma, I have made the answer community wiki. This removes all upvote points and downvote losses I got from my account. If anyone still wants to thanks me for my ingenious comment-in-disguise, they can upvote this representational comment.Shire
Aren't arrays a bit more "vector-ish" (in the mathematical sense), exactly because they have a fixed size?Purvey
S
9

It is just the name. C++ vector could very well (or maybe even more accurate) be called dynamic array or resizable array but this name was simply chosen. This vector is not the same as vector from methematics because in mathematics vectors are members of any set V such that there are two important operations defined on this set: + (addition of vectors) and x (multiplication of a vector by a scalar from field F) and these operations satisfy 8 axioms:


Associativity of addition

u + (v + w) = (u + v) + w

Commutativity of addition

u + v = v + u

Identity element of addition

There exists an element 0 ∈ V, called the zero vector, such that v + 0 = v for all v ∈ V.

Inverse elements of addition

For every v ∈ V, there exists an element −v ∈ V, called the additive inverse of v, such that v + (−v) = 0

Compatibility of scalar multiplication with field multiplication

a(bv) = (ab)v

Identity element of scalar multiplication

1 v = v, where 1 denotes the multiplicative identity in F.

Distributivity of scalar multiplication with respect to vector addition  

a(u + v) = au + av

Distributivity of scalar multiplication with respect to field addition

(a + b)v = av + bv


C++ std::vector supports all of them (not directly, but via C++ features), so it can somehow be called a vector, but it is just colloquialism and for example Vallaray pointed out by Bjarne Stroustrup in "C++ Programming Language" supports some of them directly.

Shahjahanpur answered 19/5, 2014 at 14:4 Comment(3)
No container in C++ have that kind of arithmetic defined so there are no vectors in C++. In particular a std::vector does not support arithmetic operations, and therefore, all of these properties are undefined for a std::vector. So a std::vector does not qualify as a vector. I would have called it dynamic_array or resizable_array which tells you what it is.Haigh
Or simply "list". But noooooo ... it had to be "vector" because that's what everyone uses, right?Brien
@LearnCocos2D, well, a list is typically understood to be a linked list. In fact, there already is a container called list in the C++ standard library - it's a doubly linked list.Tiki
G
6

Long time ago, in the B language there are vector types. Then the C language called them "arrays". Then the C with Classes and the C++ language just derived it ...

This is certainly not the whole story. As mentioned, Stepanov made the actual decision. But if "vector" was still used in C, the result maybe looks quite different.

PS. I wonder why C renames "array". What was the exact reason?

PS2. IMO for a language as C++, an array is better meaning "a type hold elements to be reasonably accessed via operator[]" (i.e. not 42[some_array_object]), e.g. an instantiation of std::map as an "associative array".

Garrett answered 19/5, 2014 at 13:38 Comment(0)
S
4

A vector is simply a sequence of values, all of the same type. This is pretty much in line with the use in mathematics. I guess the mathematical idea that vectors should support some common operations (such as adding, and scaling by a scalar) are not carried over, the important aspect is mainly the structure.

Stamm answered 24/2, 2009 at 11:33 Comment(1)
In linear algebra, where "vector" comes from in mathematics, everything is about operations, with all vectors being the same size. The linear algebra, so the operations, is what makes a bunch of numbers a vector in mathematics.Avionics
C
3

Also if you make it store integers or floating points it does make an excellent type for storing N dimensional vectors. After all all a vector is, is a list of numbers kept in a specific order.

Cheadle answered 24/2, 2009 at 11:35 Comment(4)
this is blatantly false, that is not what a vector is en.wikipedia.org/wiki/Vector_spaceInconspicuous
@kai You will find these conflicts in other areas aswell. Real numbers in some programming languages will not be able to store 0.1, wheras in mathematics, this value is a member of the set of real numbers. In the Java programming language (docs.oracle.com/javase/7/docs/api/java/util/Vector.html) a vector is "The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created."Shire
As for what a vector is, it depends on whose definition you use. As this question was asked on stackoverflow.com, rather than on math.stackexchange.com, I would argue for a less math-biased approach here.Shire
@kai I am quite familiar with the definition of vector with regards to vector spaces. I am also familiar with the computer science definitions, where a vector is merely a list of number adjacent in memory, i.e. an array. Note that in the disambiguation page for vector in wikipedia that they show one meaning to be a 1D array. This is precisely the same meaning that is used for vector processors, which have been around since at least the 70s. Therefore, while the definition I gave is not the strict mathematical definition, it is a computer science definition, albeit older.Cheadle
A
3

Think of a C++ vector as a dynamic array, which size can be altered by inserting or removing elements. They are not related to the vector's mathematical definition.

Vectors in Mathematics

Consider an nxm matrix called A, where n corresponds to the number of rows, and m corresponds to the number of columns. In a mathematical context, once you introduce a matrix like this, then later, you can't do any operations outside of A's range and you can't extend A's size either. What this means is you can't refer to an index of [n + 1] and/or [m + 1].

Now, a vector of A derives these attributes as well, while their dimensions will always be 1xm (any [i] row selected within A) or nx1 (any [j] column selected within A). A vector also cannot be specified as 2xn, because a collection of vectors cannot be interpreted as one vector, while one vector - let that be the [i] column vector of A with the dimensions of 1xm - can be interpreted as a matrix.

The important takeaway is that you cannot change the dimensions of a vector once it is introduced in terms of mathematics.

Vectors in C++

In C++, vectors are just like vectors in mathematics, but unlike in mathematics their size can be altered. Size as a term applies here because it implies the element count that one particular vector contains.

You use the term dimensions in terms of C++ vectors, when you have a vector of vectors: std::vector<std::vector<T>>> ragged_array. In this example, I called that vector "ragged", because it demonstrates how the size of each vector of that vector can be altered independently. It not only violates the rules of how the dimensions cannot be changed once a particular vector is introduced in mathematics, but it also demonstrates, how it cannot be used as a matrix.

Appalachia answered 15/3, 2018 at 11:17 Comment(0)
H
2

I'd guess it comes from the term row vector. Also, computer scientists love thinking up new names for things...

Hedge answered 24/2, 2009 at 11:35 Comment(0)
H
2

There are quite a few good negative answers, i.e. vector is not a good name. I would like to add more information why it is called so, and how the word vector is used in the computing industry.

Vector means ‘carrier’ literally. However, the main sense derives from mathematical usage, meaning:

  1. directed quantity (earlier 19th century)
  2. ordered set of numbers (later 19th century)

The second sense is extended to computing, and we have ‘a sequence of consecutive locations in memory’ (Shorter Oxford English Dictionary). It has a similar meaning to array, and we have terms like:

  • vector processor
  • vector instructions
  • vectorization
  • initialization vector
  • interrupt vector

They are established usages, so the C++ use of vector is not wrong. However, as Mr Stepanov put it (in From Mathematics to Generic Programming, when he talked about naming principles):

If there are conflicting usages, the much more established one wins.

His regret was really that this use of vector conflicted with the (more established) mathematical usage, and it could have been better to avoid it in the beginning.

Hoelscher answered 6/8, 2022 at 15:26 Comment(0)
H
1

To complement the answer of Mark Ruzon, here are the words from Alex Stepanov in his 2015 book, From Mathematics to Generic Programming (w/ Daniel Rose):

The name vector in STL was taken from the earlier programming languages Scheme and Common Lisp. Unfortunately, this was inconsistent with the much older meaning of the term in mathematics … this data structure should have been called array. Sadly, if you make a mistake … the result might stay around for a long time.

Hoelscher answered 6/8, 2022 at 4:51 Comment(0)
G
0

No idea about the real reason, but C++ calling it a vector instead of an array, reduces confusion between the C and C++ structures, although they fulfill the same roles.

Gwenn answered 24/2, 2009 at 11:38 Comment(0)
L
-1

it comes from the structure of matrix which build from vectors

Lanciform answered 6/1, 2011 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.