"how numbers are stored and used in computers"
A vector is a mathematical object that has both magnitude and direction. Here is an example of a mathematical object that only has magnitude:
Here is an example of a mathematical object that only has direction:
By combining these two, we can intuitively understand a vector as a "movement in space".
In linear algebra, vectors are typically represented as ordered lists of numbers, called components. These components describe the vector's position in a coordinate system.
In an n-dimensional space, a vector
Addition: Adding vectors component-wise
Scalar Multiplication: Multiplying a vector by a scalar
Dot Product: The sum of component-wise products
Vectors are fundamental in many areas:
A vector space is a collection of vectors that satisfy certain axioms under addition and scalar multiplication. The most common vector space is
The most common way to think about vectors in software development is as an array of numbers, such as
Vectors serve as the building blocks for more complex structures in linear algebra, like matrices and tensors. A vector is an element of a vector space, which is a collection of objects that can be added together and multiplied by scalars. In a geometric sense, vectors can be visualized as arrows in space, where the length of the arrow represents the vector's magnitude, and the direction of the arrow indicates the vector's direction.
The magnitude (or length) of a vector
Here is a simple implementation of magnitude
in JavaScript.
code.js1const v = [3, 4]; 2const mag = magnitude(v); 3console.log(mag); 4 5// Euclidean distance 6function magnitude(v) { 7 return Math.sqrt(v.reduce((sum, value) => sum + value ** 2, 0)); 8}
Vectors are a cornerstone of linear algebra, providing a way to represent quantities that have both magnitude and direction. In mathematical terms, a vector is an element of a vector space, which is a set equipped with two operations: vector addition and scalar multiplication. These operations must satisfy certain axioms, such as associativity, commutativity, and distributivity.
Consider a vector in a two-dimensional space, represented as
This formula generalizes to an
In JavaScript, vectors can be represented using arrays. Here is an example of how to define a vector and compute its magnitude:
Vector addition is a fundamental operation in linear algebra, where two vectors of the same dimension are combined to produce a new vector. Mathematically, if we have two vectors
In JavaScript, vector addition can be implemented using arrays. Here is a simple function to add two vectors:
code.js1function addVectors(u, v) { 2 return u.map((value, index) => value + v[index]); 3}
This function takes two vectors (arrays) and returns a new vector (array) that is the sum of the two input vectors. For example:
code.js1const u = [1, 2, 3]; 2const v = [4, 5, 6]; 3 4const w = addVectors(u, v); 5console.log(w); // Output: [5, 7, 9]
Scalar multiplication is another fundamental operation in linear algebra, where a vector is multiplied by a scalar (a real number). If
In JavaScript, scalar multiplication can be implemented using arrays. Here is a simple function to multiply a vector by a scalar:
code.js1function scalarMultiply(c, v) { 2 return v.map(value => c * value); 3}
This function takes a scalar (a number) and a vector (an array) and returns a new vector (array) that is the result of multiplying each component of the input vector by the scalar. For example:
code.js1const v = [1, 2, 3]; 2const c = 2; 3 4const w = scalarMultiply(c, v); 5console.log(w); // Output: [2, 4, 6]
Vector subtraction is the inverse operation of vector addition. It involves subtracting corresponding components of two vectors to produce a new vector. For example, given two vectors
In JavaScript, vector subtraction can be implemented using arrays. Here is a simple function to subtract one vector from another:
code.js1function subtractVectors(u, v) { 2 return u.map((value, index) => value - v[index]); 3}
This function takes two vectors (arrays) and returns a new vector (array) that is the difference between the two input vectors. For example:
code.js1const u = [1, 2, 3]; 2const v = [4, 5, 6]; 3 4const w = subtractVectors(u, v); 5console.log(w); // Output: [-3, -3, -3]
The magnitude of a vector is a measure of its length or size. In linear algebra, the magnitude of a vector
In JavaScript, the magnitude of a vector can be computed using the magnitude
function:
code.js1function magnitude(v) { 2 return Math.sqrt(v.reduce((sum, value) => sum + value * value, 0)); 3}
This function takes a vector (an array) and returns its magnitude as a number. For example:
code.js1const v = [3, 4]; 2const mag = magnitude(v); 3console.log(mag); // Output: 5
The cross product of two vectors in three-dimensional space is a vector that is perpendicular to both input vectors. If
In JavaScript, the cross product can be computed using the crossProduct
function:
code.js1function crossProduct(u, v) { 2 return [ 3 u[1] * v[2] - u[2] * v[1], 4 u[2] * v[0] - u[0] * v[2], 5 u[0] * v[1] - u[1] * v[0] 6 ]; 7}
This function takes two vectors (arrays) and returns their cross product as a new vector (array). For example:
code.js1const u = [1, 2, 3]; 2const v = [4, 5, 6]; 3 4const crossProd = crossProduct(u, v); 5console.log(crossProd); // Output: [-3, 6, -3]
The projection of a vector
In JavaScript, the projection can be computed using the projection
function:
code.js1function projection(u, v) { 2 const dotProd = dotProduct(u, v); 3 const magV = magnitude(v); 4 const magV2 = magV * magV; 5 return scalarMultiply(dotProd / magV2, v); 6}
This function takes two vectors (arrays) and returns their projection as a new vector (array). For example:
code.js1const u = [1, 2, 3]; 2const v = [4, 5, 6]; 3 4const proj = projection(u, v); 5console.log(proj); // Output: [0.96, 1.2, 1.44]
Normalizing a vector involves scaling it to have a magnitude of 1 while preserving its direction. If
In JavaScript, the normalization can be computed using the normalize
function:
code.js1function normalize(v) { 2 const mag = magnitude(v); 3 return scalarMultiply(1 / mag, v); 4}
This function takes a vector (an array) and returns its normalized form as a new vector (array). For example:
code.js1const v = [3, 4]; 2const norm = normalize(v); 3console.log(norm); // Output: [0.6, 0.8]
The angle between two vectors can be computed using the dot product. If
In JavaScript, we can implement a simple angle
function to compute the angle between two vectors.
code.js1function angle(u, v) { 2 const dotProd = dotProduct(u, v); 3 const magU = magnitude(u); 4 const magV = magnitude(v); 5 if (magU === 0 || magV === 0) { 6 throw new Error("Cannot compute angle with zero magnitude vector."); 7 } 8 9 const cosTheta = dotProd / (magU * magV); 10 11 // Ensure the value is within the valid range for acos 12 if (cosTheta < -1 || cosTheta > 1) { 13 throw new Error("Numerical error: cosine value out of range."); 14 } 15 16 return Math.acos(cosTheta); 17} 18 19function magnitude(v) { 20 return Math.sqrt(v.reduce((sum, e) => sum + e ** 2, 0)); 21} 22 23function dotProduct(u, v) { 24 return u.reduce((sum, value, index) => sum + value * v[index], 0); 25} 26 27const u = [1, 2, 3]; 28const v = [4, 5, 6]; 29 30const theta = angle(u, v); 31console.log(theta);