import numpy as np
def add_matrices(A, B):
"""Adds two matrices."""
if A.shape != B.shape:
raise ValueError("Matrices must have the same dimensions.")
return A + B
def subtract_matrices(A, B):
"""Subtracts two matrices."""
if A.shape != B.shape:
raise ValueError("Matrices must have the same dimensions.")
return A - B
def multiply_matrices(A, B):
"""Multiplies two matrices."""
if A.shape[1] != B.shape[0]:
raise ValueError("Number of columns in A must equal number of rows in B.")
return np.dot(A, B)
def transpose_matrix(A):
"""Transposes a matrix."""
return A.T
if __name__ == "__main__":
# Example matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# Addition
C = add_matrices(A, B)
print("Addition:")
print(C)
# Subtraction
D = subtract_matrices(A, B)
print("\nSubtraction:")
print(D)
# Multiplication
E = multiply_matrices(A, B)
print("\nMultiplication:")
print(E)
# Transpose
F = transpose_matrix(A)
print("\nTranspose:")
print(F)