0% found this document useful (0 votes)
6 views2 pages

Practical no 2

Uploaded by

Arpita Siddhanti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Practical no 2

Uploaded by

Arpita Siddhanti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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)

You might also like