Skip to content

Instantly share code, notes, and snippets.

@Konard
Created April 3, 2024 21:56
Show Gist options
  • Save Konard/c4f1a4bf2440124ab2b677839accd0f6 to your computer and use it in GitHub Desktop.
Save Konard/c4f1a4bf2440124ab2b677839accd0f6 to your computer and use it in GitHub Desktop.
Comparision of tensor product and cartesian product
import itertools
def cartesian_product_vectors(vector1, vector2):
product = list(itertools.product(vector1, vector2))
return product
def cartesian_product_scalar(scalar1, scalar2):
return cartesian_product_vectors([scalar1], [scalar2])
scalar_a = 2
scalar_b = 3
result = cartesian_product_scalar(scalar_a, scalar_b)
print(f"{scalar_a} × {scalar_b} = {result}")
vector_a = [1, 2]
vector_b = [1, 2]
result = cartesian_product_vectors(vector_a, vector_b)
print(f"{vector_a} × {vector_b} = {result}")
konard@konard-MS-7982:~/Desktop/products$ python3 tensor.py
2 ⊗ 3 = 6
[1 2] ⊗ [1 2] = [[1 2] [2 4]]
konard@konard-MS-7982:~/Desktop/products$ python3 cartesian.py
2 × 3 = [(2, 3)]
[1, 2] × [1, 2] = [(1, 1), (1, 2), (2, 1), (2, 2)]
import numpy as np
# Scalar tensor product (simply multiplication)
scalar1 = 2
scalar2 = 3
tensor_product_scalars = scalar1 * scalar2
print(f"{scalar1} ⊗ {scalar2} = {tensor_product_scalars}")
# Vector tensor product (outer product)
vector1 = np.array([1, 2])
vector2 = np.array([1, 2])
tensor_product_vectors = np.outer(vector1, vector2)
print(f"{vector1} ⊗ {vector2} = {tensor_product_vectors}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment