u = [2,2]
v = [2,3]
z = [3,5]
print("vector의 덧셈")
result = [sum(t) for t in zip(u,v,z)]
print(result)
print("vector의 뺄셈")
result = [x-y-z for x,y,z in zip(u,v,z)]
print(result)
vector의 덧셈
[7, 10]
vector의 뺄셈
[-3, -6]
2. Scalar-Vector product
상수(Scalar)와 리스트(Vector)를 한번에 곱하는 방법
print("scalar-vector product")
u = [1,2,3]
v = [4,4,4]
alpha = 2
result = [alpha * sum(z) for z in zip(u, v)]
print(result)
scalar-vector product
[10, 12, 14]
3. Matrix addition
** zip으로 matrix를 풀면 ([3,6]) 이런 식으로 튜플로 묶이기 때문에, asterisk(*)로 풀어줘야 한다.
matrix_a = [[3, 6], [4, 5]]
matrix_a = [[5, 8], [3, 7]]
result = [[sum(row) for row in zip(*t)] for t in zip(matrix_a, matrix_b)]
print(result)
Matrix addition
[[8, 14], [7, 12]]
4. Scalar-Matrix Product
matrix_a = [[3, 6], [4, 5]]
alpha = 4
result = [[alpha * element for element in t] for t in matrix_a]
print(result)
Scalar-Matrix Product
[[12, 24], [16, 20]]
5. Matrix Transpose
print("Matrix Transpose")
matrix_a = [[1, 2, 3], [4, 5, 6]]
result = [[element for element in t] for t in zip(*matrix_a)]
print(result)
Matrix Transpose
[[1, 4], [2, 5], [3, 6]]
6. Matrix Product
print("Matrix Product")
matrix_a = [[1, 1, 2], [2, 1, 1]]
matrix_b = [[1, 1], [2, 1], [1, 3]]
result = [[sum(a * b for a, b in zip(row_a, column_b))
for column_b in zip(*matrix_b)] for row_a in matrix_a]
print(result)