# Vectors

## Example Vectors

`height_weight_age = [70,170,40]grades = [95,80,75,62]`

`from typing import ListVector = List[float]def add(v: Vector, w: Vector) -> Vector:    """Adds corresponding elements"""    assert len(v) == len(w), "vectors must be the same length"    return [v_i + w_i for v_i, w_i in zip(v,w)]assert add([1,2,3], [4,5,6]) == [5,7,9]`

# Subtract

`def subtract(v: Vector, w: Vector) -> Vector:    """Subtracts corresponding elements"""    assert len(v) == len(w), "vectors must be the same length"    return [v_i - w_i for v_i, w_i in zip(v,w)]assert subtract([5,7,9], [4,5,6]) == [1,2,3]`

# Componentwise Sum

`def vector_sum(vectors: List[Vector]) -> Vector:    """Sum all corresponding elements (componentwise sum)"""    # Check that vectors is not empty    assert vectors, "no vectors provided!"    # Check the vectorss are all the same size    num_elements = len(vectors)    assert all(len(v) == num_elements for v in vectors), "different sizes!"    # the i-th element of the result is the sum of every vector[i]    return [sum(vector[i] for vector in vectors)            for i in range(num_elements)]assert vector_sum([[1,2], [3,4], [5,6], [7,8]]) == [16,20]`

# Multiply Vector with a Number

`def scalar_multiply(c: float, v: Vector) -> Vector:    """Multiplies every element by c"""    return [c * v_i for v_i in v]assert scalar_multiply(2, [2,4,6]) == [4,8,12]`

# Componentwise Mean

`def vector_mean(vectors: List[Vector]) -> Vector:     """Computes the element-wise average"""    n = len(vectors)    return scalar_multiply(1/n, vector_sum(vectors))assert vector_mean([ [1,2], [3,4], [5,6] ]) == [3,4]`

# Dot Product

`def dot(v: Vector, w: Vector) -> float:    """Computes v_1 * w_1 + ... + v_n * w_n"""    assert len(v) == len(w), "vectors must be the same length"    return sum(v_i * w_i for v_i, w_i in zip(v,w))assert dot([1,2,3], [4,5,6]) == 32`

# Sum of Squares

`def sum_of_squares(v: Vector) -> float:    """Returns v_1 * v_1 + ... + v_n * v_n"""    return dot(v,v)assert sum_of_squares([1,2,3]) == 14`

# Magnitude

`def magnitude(v: Vector) -> float:    """Returns  the magnitude (or length) of v"""    return math.sqrt(sum_of_squares(v)) # math.sqrt is the square root functionassert magnitude([3,4]) == 5`

# Squared Distance

`def squared_distance(v: Vector, w: Vector) -> float:    """Computes (v_1 - w_1) ** 2 + ... + (v_n - w_n) ** 2"""    return sum_of_squares(subtract(v,w))`

# (Euclidean) Distance

`import mathdef distance(v: Vector, w: Vector) -> float:    """Also computes the distance between v and w"""    return math.sqrt(squared_distance(v,w))`

Written by