Created
April 3, 2024 21:56
-
-
Save Konard/c4f1a4bf2440124ab2b677839accd0f6 to your computer and use it in GitHub Desktop.
Comparision of tensor product and cartesian product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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