Cartesian Product (or Descartes Product)
공집합(empty set, null set)이 아닌 여러 sets를 이용하여 새로운 set을 만드는 연산.
Cartesian product는
- operand인 여러 집합들의
- 각 elements를 원소(component, element)로 하는 tuple을 element(원소)로 하는 set을 반환함.
2개의 집합 A, B의 Cartesian product A×B는 다음과 같음.
A×B={(a,b)|a∈A,b∈B}
n 개의 집합 A1,A2,…,An의 Cartesian Product는 다음과 같이 정의됨.
n∏i=1Ai=A1×A2×⋯×An={(x1,x2,…,xn)|∀i:xi∈Ai}
참고로, 두 집합 A, B의 Cartesian Product A×B의 subset R은 A에서 B로의 relation (관계) 가 됨.
Relation에 대한 자세한 내용은 다음을 참고
2024.02.25 - [.../Math] - [Math] Relation
[Math] Relation
다음은 Relation에 대한 간단한 정의임. A relation (or, more precisely, a binary relation) on a set A is a collection of ordered pairs of elements from A. 이를 Cartesian Product를 통해 설명하면, A로부터 B의 (binary) relation
dsaint31.tistory.com
용어 Cartesian 의 유래
“René Descartes”라는 이름을 라틴어로 쓰면, “Renatus”는 그의 성인 “René”에 해당하고, “Cartesius”는 그의 성인 “Descartes”에 해당함. 라틴어 이름은 주로 academic field에서 사용되는 특성을 보임.
Example
X가 x-축 상에서 같은 간격으로 떨어진 M개의 값들로 구성된 set이고, Y가 y-축 상에서 같은 간격으로 떨어진 N개의 값들로 구성된 set인 경우, 이 두 set의 Cartesian product는 M×N rectangular array의 좌표(coordinate)를 정의함.
itertools.product(*iterable, repeat=1)
Python의 itertools
모듈의 product
함수가 이 Catersian Product를 구현하고 있음.
- 단,
set
을 argument로 사용하지 않고 sequence에 해당하는iterable
객체들이 argument로 할당됨. - "여러 argument들의 item들로 이루어진 그룹들"을 item으로 가지는
iterator
를 반환함.
다음 예제를 확인할것.
# -------------------------- # 일반적인 cartesian product 수행. >>> A = [0,1,2] >>> B = ['a','b'] >>> for i in itertools.product(A,B,repeat=1): ... print(i) ... (0, 'a') (0, 'b') (1, 'a') (1, 'b') (2, 'a') (2, 'b') # -------------------------- # repeat 인자의 사용법을 보여줌. >>> for i in itertools.product(A,B,repeat=2): ... print(i) ... (0, 'a', 0, 'a') (0, 'a', 0, 'b') (0, 'a', 1, 'a') (0, 'a', 1, 'b') (0, 'a', 2, 'a') (0, 'a', 2, 'b') (0, 'b', 0, 'a') (0, 'b', 0, 'b') (0, 'b', 1, 'a') (0, 'b', 1, 'b') (0, 'b', 2, 'a') (0, 'b', 2, 'b') (1, 'a', 0, 'a') (1, 'a', 0, 'b') (1, 'a', 1, 'a') (1, 'a', 1, 'b') (1, 'a', 2, 'a') (1, 'a', 2, 'b') (1, 'b', 0, 'a') (1, 'b', 0, 'b') (1, 'b', 1, 'a') (1, 'b', 1, 'b') (1, 'b', 2, 'a') (1, 'b', 2, 'b') (2, 'a', 0, 'a') (2, 'a', 0, 'b') (2, 'a', 1, 'a') (2, 'a', 1, 'b') (2, 'a', 2, 'a') (2, 'a', 2, 'b') (2, 'b', 0, 'a') (2, 'b', 0, 'b') (2, 'b', 1, 'a') (2, 'b', 1, 'b') (2, 'b', 2, 'a') (2, 'b', 2, 'b') >>>
Example0
NumPy 를 이용한 구현: meshgrid 와 flatten을 활용한 예제임.
import numpy as np # 두 배열 정의 a = np.array([1, 2, 3]) b = np.array([4, 5]) # numpy.meshgrid를 사용하여 Cartesian product 구하기 A, B = np.meshgrid(a, b) # 결과를 2차원 배열로 변환 cartesian_product = np.array([A.flatten(), B.flatten()]).T print(cartesian_product)
다음 gist 는 tensorflow와 pytorch에서의 구현하고 있음.
https://gist.github.com/dsaint31x/c9f1f533440a84db5c2033314defee21
dl_cartesian_product.ipynb
dl_cartesian_product.ipynb. GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
https://dsaint31.github.io/math/math-week01/#class-set-and-relation
[Math] Week 01
Basic
dsaint31.github.io
'... > Math' 카테고리의 다른 글
[LA] Singular Value Decomposition (특이값분해, SVD) (1) | 2024.02.15 |
---|---|
[math] Factorial(계승), Permutation (순열) & Combination (조합) (1) | 2024.02.04 |
[Math] Normal Distribution (정규분포) (2) | 2023.10.25 |
[Math] Euler’s Constant (자연상수, 오일러 상수) (0) | 2023.10.25 |
[Math] Poisson Distribution (포아송분포) (1) | 2023.10.25 |