0% found this document useful (0 votes)
3 views

Python Matrices

The document provides Python functions for various matrix operations including transposition, addition, subtraction, multiplication, rotation, and finding determinants, eigenvalues, and eigenvectors. It also includes user input functionality for constructing matrices and performing basic operations. Example usages for each function are provided to demonstrate their application.

Uploaded by

Venkata Jwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Matrices

The document provides Python functions for various matrix operations including transposition, addition, subtraction, multiplication, rotation, and finding determinants, eigenvalues, and eigenvectors. It also includes user input functionality for constructing matrices and performing basic operations. Example usages for each function are provided to demonstrate their application.

Uploaded by

Venkata Jwala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Matrices

Transpose of a Matrix:
def transpose_matrix(matrix):
return [list(row) for row in zip(*matrix)]
# Example usage
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(transpose_matrix(matrix))

Matrix Addition:
def add_matrices(matrix1, matrix2):
result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in
range(len(matrix1))]
return result
# Example usage
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
print(add_matrices(matrix1, matrix2))

Matrix Subtraction:
def subtract_matrices(matrix1, matrix2):
result = [[matrix1[i][j] - matrix2[i][j] for j in range(len(matrix1[0]))] for i
in range(len(matrix1))]
return result
# Example usage
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
print(subtract_matrices(matrix1, matrix2))

Matrix Multiplication:
def multiply_matrices(matrix1, matrix2):
result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result
# Example usage
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
print(multiply_matrices(matrix1, matrix2))

Rotate Matrix by 90 Degrees:


def rotate_matrix(matrix):
return [list(reversed(col)) for col in zip(*matrix)]
# Example usage
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(rotate_matrix(matrix))

Spiral Order of Matrix:


def spiral_order(matrix):
result = []
while matrix:
result += matrix.pop(0)
if matrix and matrix[0]:
for row in matrix:
result.append(row.pop())
if matrix:
result += matrix.pop()[::-1]
if matrix and matrix[0]:
for row in matrix[::-1]:
result.append(row.pop(0))
return result
# Example usage
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(spiral_order(matrix)

Find the Determinant of a Matrix:


def determinant(matrix):
if len(matrix) == 1:
return matrix[0][0]
if len(matrix) == 2:
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
det = 0
for c in range(len(matrix)):
det += ((-1) ** c) * matrix[0][c] * determinant([row[:c] + row[c+1:] for
row in matrix[1:]])
return det
# Example usage
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(determinant(matrix))

Inversion of a Matrix:
import numpy as np

def invert_matrix(matrix):
return np.linalg.inv(matrix)
# Example usage
matrix = [
[1, 2],
[3, 4]
]
print(invert_matrix(matrix))

Find the Eigenvalues and Eigenvectors:


import numpy as np
def eigenvalues_eigenvectors(matrix):
values, vectors = np.linalg.eig(matrix)
return values, vectors
# Example usage
matrix = [
[1, 2],
[3, 4]
]
values, vectors = eigenvalues_eigenvectors(matrix)
print("Eigenvalues:", values)
print("Eigenvectors:", vectors)

Constructing Matrix from user input and basic operations


def input_matrix():
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
matrix = []
print("Enter the entries row-wise:")
for i in range(rows):
row = list(map(int, input().split()))
matrix.append(row)
return matrix

def print_matrix(matrix):
for row in matrix:
print(" ".join(map(str, row)))
print()

def add_matrices(matrix1, matrix2):


result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in
range(len(matrix1))]
return result

def subtract_matrices(matrix1, matrix2):


result = [[matrix1[i][j] - matrix2[i][j] for j in range(len(matrix1[0]))] for i in
range(len(matrix1))]
return result

def multiply_matrices(matrix1, matrix2):


result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result

def transpose_matrix(matrix):
return [list(row) for row in zip(*matrix)]

def rotate_matrix(matrix):
return [list(reversed(col)) for col in zip(*matrix)]

def main():
print("Input the first matrix:")
matrix1 = input_matrix()
print("Input the second matrix (for addition, subtraction, and
multiplication):")
matrix2 = input_matrix()
print("\nFirst Matrix:")
print_matrix(matrix1)
print("Second Matrix:")
print_matrix(matrix2)

print("Matrix Addition:")
print_matrix(add_matrices(matrix1, matrix2))

print("Matrix Subtraction:")
print_matrix(subtract_matrices(matrix1, matrix2))

print("Matrix Multiplication:")
print_matrix(multiply_matrices(matrix1, matrix2))

print("Transpose of the First Matrix:")


print_matrix(transpose_matrix(matrix1))

print("Rotate the First Matrix by 90 Degrees:")


print_matrix(rotate_matrix(matrix1))

if __name__ == "__main__":
main()

You might also like