Lac Programs
Lac Programs
Lac Programs
if len(v1) == len(v2):
for i in range(len(v1)):
v3.append(v1[i] + v2[i])
print("Resultant vector:", v3)
else:
print("Vector addition not possible")
'''
Output:
Enter first vector values: 1 2
Enter second vector values: 3 4
Vector 1: [1, 2]
Vector 2: [3, 4]
Resultant vector: [4, 6]
Output:
Enter first vector values: 2
Enter second vector values: 3
Vector 1: [2]
Vector 2: [3]
Resultant vector: [-1]
Output:
Enter row size:3
Enter Col size:3
Enter matrix values:
1
2
3
4
5
6
7
8
9
Matrix:
1 2 3
4 5 6
7 8 9
'''
r1 = int(input("Enter matrix1 row size:"))
c1 = int(input("Enter matrix1 column size:"))
m1 = []
print("Enter matrix 1 values:")
for i in range(r1):
row = []
for j in range(c1):
row.append(int(input()))
m1.append(row)
print("Matrix1:")
for i in range(r1):
for j in range(c1):
print("%2d"%(m1[i][j]),end=" ")
print()
print()
r2 = int(input("Enter matrix2 row size:"))
c2 = int(input("Enter matrix2 column size:"))
m2 = []
print("Enter matrix 2 values:")
for i in range(r2):
row = []
for j in range(c2):
row.append(int(input()))
m2.append(row)
print("Matrix2:")
for i in range(r2):
for j in range(c2):
print("%2d"%(m2[i][j]),end=" ")
print()
print()
m3=[]
if c1==r2:
print("Multiplication of 2 matrices:")
for i in range(0,r1):
row=[]
for j in range(0,c2):
x=0
for k in range(0,r2):
x = x + (m1[i][k] * m2[k][j])
row.append(x)
m3.append(row)
for i in range(r1):
for j in range(c2):
print("%2d"%(m3[i][j]),end=" ")
print()
else:
print("Multiplication of matrices not possible")
'''
Multiplication of 2 matrices:
1 2
3 8
'''
r = int(input("Enter row size:"))
c = int(input("Enter Col size:"))
m = []
print("Enter matrix values:")
for i in range(r):
row = []
for j in range(c):
row.append(int(input()))
m.append(row)
print("Matrix:")
for i in range(r):
for j in range(c):
print("%2d"%(m[i][j]),end=" ")
print()
if r==c:
trace=0
for i in range(r):
for j in range(c):
if i==j:
trace=trace+m[i][j]
print("Trace:",trace)
else:
print("Given matrix is not a square matrix")
'''
Output:
Enter row size:2
Enter Col size:2
Enter matrix values:
4
5
6
7
Matrix:
4 5
6 7
Trace: 11
EXPLANATION:
#from numpy import array: This line imports the array function from the NumPy
library.
NumPy is a popular Python library for numerical computations, and the array
function is used to create arrays
(including matrices) in NumPy.
from numpy.linalg import det: This line imports the det function from the NumPy
linear algebra module
(numpy.linalg). The det function is used to calculate the determinant of a
matrix.
Now, let's see how to define a matrix and calculate its determinant using these
imported functions:python
In this code snippet, array([[1, 2], [3, 4]]) creates a 2x2 matrix with the
following elements:
Now, let's calculate the determinant of this matrix using the det function:
Now, let's calculate the determinant of this matrix using the det function:
# Calculate the determinant of the matrix using the det function
determinant = det(matrix)
Output:
[[1 2 3]
[4 3 6]
[7 8 1]]
Calculate Determinant of A
Determinant of matrix A is B =
63.99999999999998
'''
import numpy as np
# Finding an inverse of given array
arr = np.array([[1, 2], [3, 4]])
inverse_array = np.linalg.inv(arr)
print("Inverse array is ")
print(inverse_array)
print()
'''
Output:
Inverse array is
[[-2. 1. ]
[ 1.5 -0.5]]