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

Vertopal.com Em Assignment

Uploaded by

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

Vertopal.com Em Assignment

Uploaded by

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

# task 1: matrix determinant calculation

def determinant(matrix):
n = len(matrix)
if n == 1:
return matrix[0][0]
if n == 2:
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1]
[0]
det = 0
for i in range(n):
submatrix = [row[:i] + row[i+1:] for row in matrix[1:]]
sign = (-1) ** i
det += sign * matrix[0][i] * determinant(submatrix)
return det

matrix = [[1,2,3],
[4,5,6],
[7,8,9]]

result = determinant(matrix)
print("determinant of the matrix:", result)

determinant of the matrix: 0

#task 2: matrix rank calculation

def rank(matrix):
rows, cols = len(matrix), len(matrix[0])
for i in range(rows):
row = i
while row < rows and matrix[row][i] == 0:
row += 1
if row == rows:
continue
if row != i:
matrix[i], matrix[row] = matrix[row], matrix[i]
for j in range(i + 1, rows):
factor = matrix[j][i] / matrix[i][i]
for k in range(i, cols):
matrix[j][k] -= factor * matrix[i][k]
rank = 0
for row in matrix:
if any(row):
rank += 1
return rank

matrix = [[1, 2, 3],


[4, 5, 6],
[7, 8, 9]]
r = rank(matrix)
print("Rank of the matrix:", r)

Rank of the matrix: 2

#task 3: eigenvalue calculation

import sympy as sp
def eigenvalues(matrix):
A = sp.Matrix(matrix)
lambda_ = sp.symbols('lambda')
I = sp.eye(A.shape[0])
characteristic_polynomial = sp.det(A - lambda_ * I)
eigenvals = sp.solve(characteristic_polynomial, lambda_)
return eigenvals

matrix = [
[4, -2],
[1, 1]]
eigenvals = eigenvalues(matrix)
print(f"eigenvalues: {eigenvals}")

eigenvalues: [2, 3]

#task 4 : solving a linear system

import numpy as np
def forward_substitution(L, b):
n = len(b)
y = np.zeros_like(b, dtype=float)
for i in range(n):
y[i] = (b[i] - np.dot(L[i, :i], y[:i])) / L[i, i]
return y

def back_substitution(U, y):


n = len(y)
x = np.zeros_like(y, dtype=float)
for i in range(n-1, -1, -1):
x[i] = (y[i] - np.dot(U[i, i+1:], x[i+1:])) / U[i, i]
return x

def lu_decomposition(A):
n = len(A)
L = np.zeros((n, n), dtype=float)
U = np.zeros((n, n), dtype=float)

for i in range(n):
for j in range(i, n):
U[i, j] = A[i, j] - np.dot(L[i, :i], U[:i, j])
for j in range(i+1, n):
L[j, i] = (A[j, i] - np.dot(L[j, :i], U[:i, i])) / U[i, i]
L[i, i] = 1
return L, U

def solve_linear_system(A, b):


A = np.array(A, dtype=float)
b = np.array(b, dtype=float)
L, U = lu_decomposition(A)
y = forward_substitution(L, b)
x = back_substitution(U, y)

return x
A = [
[2, -1, 1],
[3, 3, 9],
[3, 3, 5]
]

b = [1, 0, 4]

x = solve_linear_system(A, b)
print("solution:", x)

solution: [ 1.66666667 1.33333333 -1. ]

#task 5: matrix multiplication

def matrix_multiply(A, B):


rows_A, cols_A = len(A), len(A[0]) if A else 0
rows_B, cols_B = len(B), len(B[0]) if B else 0

if cols_A != rows_B:
raise ValueError("Incompatible matrix dimensions for
multiplication.")

C = [[0 for _ in range(cols_B)] for _ in range(rows_A)]


for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
C[i][j] += A[i][k] * B[k][j]
return C

A = [[1, 2, 3], [4, 5, 6]]


B = [[1, 4], [2, 5], [3, 6]]

try:
C = matrix_multiply(A, B)
print("multiplication of A and B matrices is:", C)
except ValueError as e:
print(e)

multiplication of A and B matrices is: [[14, 32], [32, 77]]

You might also like