Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.7 kB
4
Indexable
Never
import math

# Function to generate vector a with elements a_i = i
def generate_vector_a(n):
    return [i for i in range(1, n + 1)]

# Function to generate vector b with elements b_i = i^2
def generate_vector_b(n):
    return [i**2 for i in range(1, n + 1)]

# Function to calculate the inner product of two vectors
def inner_product(vector1, vector2):
    if len(vector1) != len(vector2):
        raise ValueError("Vectors must be of the same length")
    return sum(x * y for x, y in zip(vector1, vector2))

# Function to calculate the L2 norm of a vector
def l2_norm(vector):
    return math.sqrt(sum(x**2 for x in vector))

# Function to calculate the L∞ norm of a vector
def linf_norm(vector):
    return max(abs(x) for x in vector)

# Function to calculate the Euclidean distance between two points represented by vectors
def euclidean_distance(vector1, vector2):
    if len(vector1) != len(vector2):
        raise ValueError("Vectors must be of the same length")
    squared_difference = sum((x - y) ** 2 for x, y in zip(vector1, vector2))
    return math.sqrt(squared_difference)

# Given dimensions
n = 500

# Generate vectors a and b
a = generate_vector_a(n)
b = generate_vector_b(n)

# Calculate inner product
inner_prod = inner_product(a, b)
print("Inner Product:", inner_prod)

# Calculate L2 norm
l2_norm_a = l2_norm(a)
l2_norm_b = l2_norm(b)
print("L2 Norm of a:", l2_norm_a)
print("L2 Norm of b:", l2_norm_b)

# Calculate L∞ norm
linf_norm_a = linf_norm(a)
linf_norm_b = linf_norm(b)
print("L∞ Norm of a:", linf_norm_a)
print("L∞ Norm of b:", linf_norm_b)

# Calculate Euclidean distance
distance = euclidean_distance(a, b)
print("Euclidean Distance:", distance)
Leave a Comment