0% found this document useful (0 votes)
21 views6 pages

Lac Programs

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 6

1.

Programs on algebraic operations on 2D arrays/ matrices


#display elements of vector
'''
n = int(input("Enter size of vector: "))
v = []
print("Enter elements of vector:")
for i in range(n):
v.append(int(input()))
print("Vector:", v)
'''
Output:
Enter size of vector: 2
Enter elements of vector:
1
1
Vector: [1, 1]

#To Find sum of Vectors


'''
v1 = list(map(int, input("Enter first vector values: ").split()))
v2 = list(map(int, input("Enter second vector values: ").split()))
v3 = []
print("Vector 1:", v1)
print("Vector 2:", v2)

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]

#To Find Differences of Vectors


'''
v1 = list(map(int,input("Enter first vector values: ").split()))
v2 = list(map(int,input("Enter second vector values: ").split()))
v3=[]
print("Vector 1:",v1)
print("Vector 2:",v2)
if len(v1)==len(v2):
for i in range(len(v1)):
v3.append(v1[i]-v2[i])
print("Resultant vector:",v3)
else:
print("Vector subtraction not possible")
'''

Output:
Enter first vector values: 2
Enter second vector values: 3
Vector 1: [2]
Vector 2: [3]
Resultant vector: [-1]

#To Display Higher Order Matrix Solution:


'''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()
'''

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

#To Find Matrix Multiplication Solution:


#To perform multiplication of two matrices, we should make sure that the number of
columns in the 1st matrix is equal to the rows in the 2nd matrix. Therefore, the
resulting matrix product will have a number of rows of the 1st matrix and a number
of columns of the 2nd matrix.2x2,3x3.

'''
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")
'''

Output:Enter matrix1 row size:2


Enter matrix1 column size:2
Enter matrix 1 values:
1
2
3
8
Matrix1:
1 2
3 8

Enter matrix2 row size:2


Enter matrix2 column size:2
Enter matrix 2 values:
1
0
0
1
Matrix2:
1 0
0 1

Multiplication of 2 matrices:
1 2
3 8

2.Programs on finding trace, determinant and inverse of a matrix


#To Display Trace of A Matrix.
#The trace of a square matrix is the sum of its diagonal entries.
The trace of a matrix is defined as the sum of the principal diagonal elements of a
square matrix.In linear algebra, the trace of a square matrix A, denoted tr(A), is
defined to be the sum of elements on the main diagonal of A.

'''
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)

# Print the determinant


print("Determinant:", determinant)#

#To Find Determinant Of A Matrix


The determinant of a matrix is a single numerical value which is used when
calculating the inverse or when solving systems of linear equations. The
determinant of a matrix A is denoted |A| , or sometimes det(A) .
It is simply obtained by cross multiplying the elements starting from top left and
then subtracting the products.
The determinant of a 2x2 matrix A =
a b
c d⎦is |A| = ad - bc
'''
from numpy import array
from numpy.linalg import det #define a matrix
A = array([[1, 2, 3],[4, 3, 6],[7, 8, 1]])
print(A)
print("Calculate Determinant of A")
B = det(A)
print("Determinant of matrix A is B = ")
print(B)

Output:
[[1 2 3]
[4 3 6]
[7 8 1]]
Calculate Determinant of A
Determinant of matrix A is B =
63.99999999999998

#To Find Inverse of A Matrix.


#To find the inverse of the array, swap the index and the value of the array. So,
the inverse array is equal to the given array

'''
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]]

You might also like