Last modified: September 21, 2024

This article is written in: 🇺🇸

Vectors

A vector is a mathematical object with both magnitude and direction, essential in linear algebra and calculus. In computer science, vectors are used for various operations in data analysis, machine learning, and scientific computing. This guide explores vectors in the context of NumPy, providing definitions, operations, and practical examples.

Vector Definitions

Vector in $\mathbb{R}^n$

Row vs. Column Vectors

Row Vector:

Column Vector:

$$ \vec{v} = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix} $$

Transpose

Norm

$$ ||\vec{v}||_p = \left( \sum_i |v_i|^p \right)^{1/p} $$

Vector Operations

NumPy supports various vector operations, including addition, scalar multiplication, dot product, cross product, and more.

Vector Addition

Vectors can be added element-wise to yield a new vector.

import numpy as np

arr_1 = np.array([9, 2, 5])
arr_2 = np.array([-3, 8, 2])

# Element-wise addition
result = np.add(arr_1, arr_2)
print(result)

Expected output:

[ 6 10  7]

Explanation:

Scalar Multiplication

Each element of a vector is multiplied by a scalar value.

arr = np.array([6, 3, 4])
scalar = 2

# Scalar multiplication
result = scalar * arr
print(result)

Expected output:

[12  6  8]

Explanation:

Dot Product

The dot product of two vectors results in a scalar, calculated as the sum of the products of corresponding elements.

arr_1 = np.array([9, 2, 5])
arr_2 = np.array([-3, 8, 2])

# Dot product
result = np.dot(arr_1, arr_2)
print(result)

Expected output:

-1

Explanation:

Cross Product

The cross product is defined for 3D vectors and results in a vector perpendicular to both input vectors.

arr_1 = np.array([9, 2, 5])
arr_2 = np.array([-3, 8, 2])

# Cross product
result = np.cross(arr_1, arr_2)
print(result)

Expected output:

[-36 -33  78]

Explanation:

Angle Between Vectors

Using the dot product and norms, the cosine of the angle between two vectors can be found. This can be inverted to get the angle.

arr_1 = np.array([9, 2, 5])
arr_2 = np.array([-3, 8, 2])

# Angle between vectors
cos_angle = np.dot(arr_1, arr_2) / (np.linalg.norm(arr_1) * np.linalg.norm(arr_2))
angle_rad = np.arccos(cos_angle)
print(angle_rad)

Expected output:

1.582

Explanation:

Broadcasting

NumPy's broadcasting feature allows for arithmetic operations on arrays of different shapes, facilitating vectorized operations and eliminating the need for explicit loops.

Example of Broadcasting

arr = np.array([1, 2, 3, 4])
scalar = 2

# Broadcasting operations
print("Addition with scalar:", arr + scalar)
print("Multiplication with scalar:", arr * scalar)

Expected output:

Addition with scalar: [3 4 5 6]
Multiplication with scalar: [2 4 6 8]

Explanation:

Summary Table

Operation Description Example Code Expected Output
Vector Addition Adds two vectors element-wise. np.add(arr_1, arr_2) [ 6 10 7]
Scalar Multiplication Multiplies each element of the vector by a scalar. scalar * arr [12 6 8]
Dot Product Computes the dot product of two vectors, resulting in a scalar. np.dot(arr_1, arr_2) -1
Cross Product Computes the cross product of two 3D vectors, resulting in a perpendicular vector. np.cross(arr_1, arr_2) [-36 -33 78]
Angle Between Vectors Calculates the angle between two vectors using dot product and norms. np.arccos(np.dot(arr_1, arr_2) / (np.linalg.norm(arr_1) * np.linalg.norm(arr_2))) 1.582
Broadcasting Allows arithmetic operations on arrays of different shapes. arr + scalar, arr * scalar Addition with scalar: [3 4 5 6], Multiplication with scalar: [2 4 6 8]

Table of Contents

    Vectors
    1. Vector Definitions
      1. Vector in $\mathbb{R}^n$
      2. Row vs. Column Vectors
      3. Transpose
      4. Norm
    2. Vector Operations
      1. Vector Addition
      2. Scalar Multiplication
      3. Dot Product
      4. Cross Product
      5. Angle Between Vectors
    3. Broadcasting
      1. Example of Broadcasting
    4. Summary Table