<reponame>washimimizuku/data-science-tutorials<gh_stars>0
from typing import List
import math

Vector = List[float]

height_weight_age = [
    70, # inches
    170, # pounds
    40, # years
]

grades = [
    95, # exam1
    80, # exam2
    75, # exam3
    62, # exam4
]

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]

def subtract(v: Vector, w: Vector) -> Vector:
    """Substracts 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]

def vector_sum(vectors: List[Vector]) -> Vector:
    """Sums all corresponding elements"""
    # Check that vectors is not empty
    assert vectors, "no vectors provided!"

    # Check the vector are all the same size
    num_elements = len(vectors[0])
    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]

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, [1, 2, 3]) == [2, 4, 6]

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]

def dot(v: Vector, w: Vector) -> float:
    """Computes v_1 * w_1 + ... + v_n * w_n"""
    assert len(v) == len(w), "vectors must be 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 # 1 * 4 + 2 * 5 + 3 * 6

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 # 1 * 1 + 2 * 2 + 3 * 3

def magnitude(v: Vector) -> float:
    """Returns the magnitude (or length) of v"""
    return math.sqrt(sum_of_squares(v))

assert magnitude([3, 4]) == 5

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))

assert squared_distance([1, 2, 3], [4, 5, 6]) == 27

def distance(v: Vector, w: Vector) -> float:
    """Computes distance between v and w"""
    return math.sqrt(squared_distance(v, w))

assert distance([1, 2], [2, 2]) == 1.0

def distance_alternative(v: Vector, w: Vector) -> float:
    """Computes distance between v and w"""
    return magnitude(subtract(v, w))

assert distance_alternative([1, 2], [2, 2]) == 1.0

from typing import List
import math

Vector = List[float]

height_weight_age = [
    70, # inches
    170, # pounds
    40, # years
]

grades = [
    95, # exam1
    80, # exam2
    75, # exam3
    62, # exam4
]

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]

def subtract(v: Vector, w: Vector) -> Vector:
    """Substracts 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]

def vector_sum(vectors: List[Vector]) -> Vector:
    """Sums all corresponding elements"""
    # Check that vectors is not empty
    assert vectors, "no vectors provided!"

    # Check the vector are all the same size
    num_elements = len(vectors[0])
    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]

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, [1, 2, 3]) == [2, 4, 6]

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]

def dot(v: Vector, w: Vector) -> float:
    """Computes v_1 * w_1 + ... + v_n * w_n"""
    assert len(v) == len(w), "vectors must be 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 # 1 * 4 + 2 * 5 + 3 * 6

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 # 1 * 1 + 2 * 2 + 3 * 3

def magnitude(v: Vector) -> float:
    """Returns the magnitude (or length) of v"""
    return math.sqrt(sum_of_squares(v))

assert magnitude([3, 4]) == 5

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))

assert squared_distance([1, 2, 3], [4, 5, 6]) == 27

def distance(v: Vector, w: Vector) -> float:
    """Computes distance between v and w"""
    return math.sqrt(squared_distance(v, w))

assert distance([1, 2], [2, 2]) == 1.0

def distance_alternative(v: Vector, w: Vector) -> float:
    """Computes distance between v and w"""
    return magnitude(subtract(v, w))

assert distance_alternative([1, 2], [2, 2]) == 1.0
# inches # pounds # years # exam1 # exam2 # exam3 # exam4 Adds corresponding elements Substracts corresponding elements Sums all corresponding elements # Check that vectors is not empty # Check the vector are all the same size # The i-th element of the result is the sum of every vector[i] Multiplies every element by c Computes v_1 * w_1 + ... + v_n * w_n # 1 * 4 + 2 * 5 + 3 * 6 Returns v_1 * v_1 + ... + v_n * v_n # 1 * 1 + 2 * 2 + 3 * 3 Returns the magnitude (or length) of v Computes (v_1 - w_1) ** 2 + ... + (v_n - w_n) ** 2 Computes distance between v and w Computes distance between v and w