Methodology of Solving Tasks1.1 Overview of problems: Firstly, we need to create random matrix A10x10 with random integers in range of 1 to 100, matrix B2x10 with random integers fluctua
Trang 1VIETNAM GENERAL CONFEDERATION OF LABOR
TON DUC THANG UNIVERSITY
FACULTY OF INFORMATION TECHNOLOGY
MID-TERM
LINEAR ALGEBRA FOR INFORMATION TECHNOLOGY
Advised by
Dr
HO CHI MINH CITY, 2024
Trang 2TABLE OF CONTENT
LIST OF FIGURES iii
CHAPTER 1 Methodology of Solving Tasks v
1.1 Overview of problems: v
1.2 Methodology for solving task a: vii
1.3 Methodology for solving task b: vii
1.4 Methodology for solving task c: ix
1.5 Methodology for solving task d: ix
1.6 Methodology for solving task e: x
1.7 Methodology for solving task f: xi
1.8 Methodology for solving task g: xii
1.9 Methodology for solving task h: xiii
CHAPTER 2 Source codes and outputs xiii
2.1 Source codes: xiv
2.2 Output xvi
Trang 3LIST OF FIGURES
Figure 1: createMatrix fuction v
Figure 2: Creating matrix from creatMatrix function v
Figure 3: Matrices A, B and C vi
Figure 4: Tasks vi
Figure 5: Essential libraries vii
Figure 6: Task_a vii
Figure 7: Output Task_a vii
Figure 8: Task_b viii
Figure 9: Output Task_b viii
Figure 10: Task_c ix
Figure 11: Output Task_c ix
Figure 12: Task_d x
Figure 13: Output Task_d x
Figure 14: isPrime x
Figure 15: Task_e xi
Figure 16: Output Task_e xi
Figure 17: Addition subcription xi
Figure 18: Task_f xi
Figure 19: Output Task_f xii
Figure 20: Task_g xii
Figure 21: Output Task_g xiii
Figure 22: Task_h xiii
Trang 4Figure 23: Output Task_h xiii
Trang 5CHAPTER 1 Methodology of Solving Tasks
1.1 Overview of problems:
Firstly, we need to create random matrix A10x10 with random integers in range
of 1 to 100, matrix B2x10 with random integers fluctuating from 1 to 20, and matrix C10x2 with elements as integers matrix B
An automated function generates a random matrix with the given parameters including row, column, start value (left) and end value (right)
Figure 1: createMatrix fuction
Figure 2: Creating matrix from creatMatrix function
Matrices A, B and C:
Trang 6Figure 3: Matrices A, B and C.
These tasks are divided into small parts and are called respectively In Task f for convenience, matrix D is initialized by matrix multiplication between matrices C and B, and passed through the function Task_f as a parameter
Figure 4: Tasks For dealing with all the problems, we just need to import essential libraries below:
Trang 7Figure 5: Essential libraries
1.2 Methodology for solving task a:
For calculating expression: A+ A T B
+CB+ T
C T Use np.transpose() for finding transpose matrix as A T
, B T
∨C T and np.matmul() for finding multiplication between
2 matrices C and B
Figure 6: Task_a Result output:
Figure 7: Output Task_a
1.3 Methodology for solving task b:
In order to solve equation: A+(A
)2 +(A
)3
+…+(A
)8 +(A
)9 +(A
19)10
.
Trang 8 Initialize result’s variable = A
10 , exp = 2 represents for exponentiation of fraction A For instance (A
10)2
= A2
.(10)1 2
res=res0+ Aexp.(1
i)exp
∧(res0=10)A
Conduct a loop in range of 11 to 20 based on denominator of fraction A, then update values of res and exp
Figure 8: Task_b Result output:
Trang 9Figure 9: Output Task_b
1.4 Methodology for solving task c:
In order to find odd rows of the matrix A into a new matrix
Initialize a resultant_matrix variable to store a new matrix
Using list comprehension methods to extract all entries in odd row of matrix A into resultant_matrix respectively, then cast it into array In that:
A.shape[0] is size row of A
List comprehension methods: value_return for oddNumber in range (start, stop, end)
Figure 10: Task_c Result output:
Figure 11: Output Task_c
1.5 Methodology for solving task d:
In order to find odd integers numbers in the matrix A into a new vector
Initialize an empty resultant_vector
Considering each element (using two loops for accessing row and column) in matrix A to determine whether it is an odd integer numbers, then append it into resultant_vector
In that: A.shape[0] is size of A row, and A.shape[1] is size of A column
Trang 10Figure 12: Task_d Result output:
Figure 13: Output Task_d
1.6 Methodology for solving task e:
Similar to task d, but with a different condition for check elements to append Therefore, create an isPrime() fuction to check whether a number is prime or not Definition of prime number: a number that cannot divided by any number, except itself and 1
Figure 14: isPrime After that:
Initialize an empty resultant_vector
Considering each element (using two loops for accessing row and column) in matrix A to determine whether it is a prime numbers, then append it into resultant_vector
In that: A.shape[0] is size of A row, and A.shape[1] is size of A column
Trang 11Figure 15: Task_e Result output:
Figure 16: Output Task_e
1.7 Methodology for solving task f:
As mention, when calling Task_f, a parameter CBis passed through
Figure 17: Addition subcription
Initialize an empty resultant_matrix
Determine if it is odd row of A, then append reversed elements (by using list slicing) to resultant_matrix, if not append even row of A to resultant_matrix
In that:
D.shape[0] is size of D row
List slicing method: D[current_row][start:stop:step], with D[i][::-1] will start from D[i][last_index : 0 : -1]
Figure 18: Task_f Result output:
Trang 12Figure 19: Output Task_f
1.8 Methodology for solving task g:
In order to find the rows which have maximun count of prime number
Initialize a countPrimeInRowMatrix as a vector of zeros with the size of rows in matrix A to store the count of prime number in each row
Using two loops to consider each element in matrix A whether prime number or not, then update countPrimeInRowMatrix[index] for the current row
Assign maximumCountPrime variable to the maximum value in countPrimeInRowMatrix
Use a loop to check if any row of A has the same value as
maximumCountPrime, then print it to the screen
Figure 20: Task_g Result output:
Trang 13Figure 21: Output Task_g
1.9 Methodology for solving task h:
In order to find the rows which have the longest contiguous odd number sequences
Initialize a countOddNumberSequence as a vector of zeros with the size of rows in matrix A to store the contiguous odd number sequences in each row
Using two loops to count contiguous odd number in each row of matrix A,
then update countOddNumberSequence[current_row].
Assign longestContiguousOddNumber variable to the maximum value in
countOddNumberSequence.
Use a loop to check if any row of A has the same value as
longetsContiguousOddNumber, then print it to the screen.
Figure 22: Task_h Result output:
Figure 23: Output Task_h
CHAPTER 2 Source codes and outputs
Trang 142.1 Source codes:
import numpy as np
import random as rd
import math as m
def createMatrix (row, column, left, right) :
a = np zeros ((row, column))
for i in range (row) :
for j in range (column) :
a i [ ][ ] j = rd randint (left, right)
return a
def isPrime (n) :
for i in range 2 ( , int ( m sqrt (n)) + 1 ) :
if n % i == 0 : return False
return n > 1
def Task_a (A, , B C)
print ( "Task a:" )
#Calculate A + A^T + CB + B^T.C^T
result = A + np transpose (A) + np matmul (C, B) +
np matmul ( np transpose ( )B, np transpose ( ))C
print ( "A + A^T + CB + B^T.C^T: " , result , sep = \n ' )
def Task_b (A) :
print ( "Task b:" )
#Calculate A/10 + (A/11)^2 + (A/12)^3 + + (A/18)^9 + (A/19)^10
result = A / 10
index = 2
for i in range 11 ( , 20 )
result += np.linalg matrix_power (A, index ) * 1 / m pow ( ,
index )
index += 1
print ( "Result: " , result , sep = \n ' )
def Task_c ( )A:
print ( "Task c:" )
#Saving odd rows of the matrix A into a new matrix
resultant_matrix = np array ( [ [Ai ] for i in range 1 ( , A.shape [ ] 0 ,
2 ) ) ]
print ( "Result:" , resultant_matrix )
def Task_d (A) :
print ( "Task d:" )
#Saving odd integer numbers in the matrix A into a new vector resultant_vector = []
for i in range (A.shape [ ] 0 ) :
for j in range (A.shape [ ] 1 ) :
if A[ , j ] % 2 != 0 : resultant_vector append ( int (A[ i ,
j ))
print ( "Result:" , resultant_vector )
def Task_e (A) :
print ( "Task e:" )
Trang 15resultant_vector = []
for i in range (A.shape [ ] 0 )
for j in range (A.shape [ ] 1 ) :
if isPrime (A[ i j , ] ) : resultant_vector append ( int (A[ i ,
j ))
print ( "Result:" , resultant_vector )
def Task_f (D) :
print ( "Task f:" )
#Given D=CB, reverse elements in the odd rows of the matrix D #matrix D = C.B
# print(D)
resultant_matrix = []
for i in range (D.shape [ ] 0 )
if i % 2 != 0 : resultant_matrix append (D[ ][ i ::- 1 ] ) else : resultant_matrix append (D[ ] i )
print ( "result:" , np array ( [ resultant_matrix ] ))
def Task_g (A) :
print ( "Task g:" )
#Regarding the matrix A, find the rows which have maximum count
of prime numbers
countPrimeInRowMatrix = np zeros (A.shape [ ] 0 )
for i in range (A.shape [ ] 0 )
countPrime = 0
for j in range (A.shape [ ] 1 )
if isPrime (A[ i j , ] ) : countPrime += 1
countPrimeInRowMatrix i [ ] = countPrime
# print(countPrimeInRowMatrix)
maximumCountPrime = max ( countPrimeInRowMatrix )
print ( "Result:" )
for i in range (A.shape [ ] 0 )
if countPrimeInRowMatrix i [ ] == maximumCountPrime :
print (A[ ] i )
def Task_h (A) :
print ( "Task h:" )
#Regarding the matrix A, find the rows which have the longest contiguous odd numbers sequences
countOddNumberSequence = np zeros (A.shape [ ] 0 )
for i in range (A.shape [ ] 0 )
countContiguosOddNumber = 1
for j in range (A.shape [ ] 1 - 1 :
if(A[ ][ i j ] % 2 != 0 and A[ ][ i j 1 ] % 2 != 0 ) :
countContiguosOddNumber += 1
countOddNumberSequence i [ ] = countContiguosOddNumber longestContiguousOddNumber = max ( countOddNumberSequence ) # print("Count Odd Number Sequence", countOddNumberSequence) print ( "Result:" )
for i in range (A.shape [ ] 0 )
if countOddNumberSequence i [ ] == longestContiguousOddNumber : print (A[ ] i )
if name == ' main ' :
A = createMatrix ( 10 , 10 , , 1 100 )
Trang 16C = createMatrix ( 10 , , , 2 1 20 )
print ( "Matrix A:" , A, "Matrix B:" , B, "Matrix C:" , C, sep = \n " ) Task_a ( A, B, C )
Task_b ( ) A
Task_c ( ) A
Task_d ( ) A
Task_e ( ) A
#Given D = CB
Task_f ( np matmul ( C, B ))
Task_g ( ) A
Task_h ( ) A
2.2 Output
Task a :
A + A T + CB + B T.C T ^ ^ :
[[ 880 947 966 1013 853 777 898 627 955 479 ] [ 947 810 861 754 702 774 735 708 782 631 ] [ 966 861 968 957 814 800 929 671 894 478 ] [ 1013 754 957 812 695 792 771 633 964 634 ] [ 853 702 814 695 702 653 723 511 788 490 ] [ 777 774 800 792 653 798 744 529 784 483 ] [ 898 735 929 771 723 744 800 588 884 569 ] [ 627 708 671 633 511 529 588 456 623 276 ] [ 955 782 894 964 788 784 884 623 776 460 ] [ 479 631 478 634 490 483 569 276 460 186 ]]
Task b :
Result :
[[ 1.01102255e+13 1.83508347e+13 1.94803842e+13 1.31069348e+13 1.47872520e+13 1.64639830e+13 1.20151687e+13 1.34792230e+13 1.07450869e+13 1.25545463e+13 ]
[ 8.56595079e+12 1.55478597e+13 1.65048770e+13 1.11049326e+13 1.25285912e+13 1.39492138e+13 1.01799258e+13 1.14203546e+13 9.10384101e+12 1.06369177e+13 ]
[ 9.20583028e+12 1.67092888e+13 1.77377942e+13 1.19344732e+13 1.34644804e+13 1.49912223e+13 1.09403704e+13 1.22734594e+13 9.78390067e+12 1.14314987e+13 ]
[ 1.01369668e+13 1.83993713e+13 1.95319132e+13 1.31416040e+13 1.48263656e+13 1.65075297e+13 1.20469475e+13 1.35148763e+13 1.07735085e+13 1.25877511e+13 ]
[ 1.06625633e+13 1.93533716e+13 2.05446342e+13 1.38229905e+13 1.55951051e+13 1.73634400e+13 1.26715753e+13 1.42156143e+13 1.13321092e+13 1.32404212e+13 ]
[ 6.46700627e+12 1.17381128e+13 1.24606317e+13 8.38385289e+12 9.45866752e+12 1.05311889e+13 7.68550264e+12 8.62198579e+12 6.87309598e+12 8.03051599e+12 ]
[ 1.02001077e+13 1.85139819e+13 1.96535767e+13 1.32234628e+13 1.49187165e+13 1.66103587e+13 1.21219848e+13 1.35990551e+13 1.08406141e+13 1.26661607e+13 ]
[ 9.04123434e+12 1.64105368e+13 1.74206504e+13 1.17210905e+13 1.32237411e+13 1.47231891e+13 1.07447619e+13 1.20540140e+13 9.60896853e+12 1.12271110e+13 ]
[ 8.12120836e+12 1.47406167e+13 1.56479460e+13 1.05283659e+13 1.18781091e+13 1.32249713e+13 9.65138648e+12 1.08274130e+13
Trang 17[ 7.86632693e+12 1.42779893e+13 1.51568480e+13 1.01979400e+13 1.15053199e+13 1.28099150e+13 9.34847818e+12 1.04875959e+13 8.36028631e+12 9.76814728e+12 ]]
Task c :
Result : [[ 14 85 83 13 48 86 1 53 20 85.] [ 71 46 98 68 58 50 31 11 83 23.]
[ 12 53 95 52 2 81 29 9 3 27 ]
[ 21 95 34 27 10 54 92 100 5 55 ]
[ 27 94 10 77 38 91 44 4 30 18 ]]
Task d :
Result : [1, 99, 79, 31, 93, 37, 85, 83, 13, 1, 53, 85, 29, 29, 87,
87, 43, 37, 71, 31, 11, 83, 23, 73, 47, 61, 81, 37, 53, 95, 81, 29,
9, 3, 27, 71, 97, 17, 51, 57, 21, 95, 27, 5, 55, 43, 53, 89, 75,
21, 27, 77, 91]
Task e :
Result : [79, 31, 37, 83, 13, 53, 29, 29, 43, 37, 71, 31, 11, 83,
23, 73, 47, 61, 37, 53, 2, 29, 3, 71, 97, 17, 5, 43, 53, 89]
Task f :
Result : [[[439 474 417 596 444 358 439 177 386 93.] [ 72 272 184 360 304 384 448 296 320 360.]
[470 450 408 604 492 392 470 222 376 96.]
[ 54 208 131 267 224 282 338 226 247 267 ]
[ 247 197 188 294 270 212 247 139 172 48 ]
[ 75 282 194 376 318 402 466 307 331 376 ]
[ 348 348 312 456 360 288 348 156 288 72 ]
[ 72 304 128 336 272 336 464 328 376 336 ]
[ 489 454 415 620 516 410 489 239 382 99 ]
[ 75 310 145 355 290 360 480 335 380 355 ]]]
Task g :
Result :
[ 71 46 98 68 58 50 31 11 83 23 ]
Task h :
Result :