CHAPTER 1 - Methodology of Solving TasksThe funtion to check if a number is prime that takes an integer n as input and returns True if n is prime and False otherwise.The function first c
Trang 1TỔNG LIÊN ĐOÀN LAO ĐỘNG VIỆT NAM
TRƯỜNG ĐẠI HỌC TÔN ĐỨC THẮNG
KHOA CÔNG NGHỆ THÔNG TIN
MIDTERM ESSAY: Applied Linear Algebra for IT
Methodology of Solving Tasks
Người hướng dẫn: TS Trịnh Hùng Cường Người thực hiện: NGUYỄN NHẬT KHOA
NGUYỄN BẢO THẮNG Lớp : 22K50301 Khoá : 26
THÀNH PHỐ HỒ CHÍ MINH, NĂM 2023
TỔNG LIÊN ĐOÀN LAO ĐỘNG VIỆT NAM
Trang 2TRƯỜNG ĐẠI HỌC TÔN ĐỨC THẮNG
KHOA CÔNG NGHỆ THÔNG TIN
MIDTERM ESSAY: Applied Linear Algebra for IT
Source codes and outputs
Người hướng dẫn: TS Trịnh Hùng Cường Người thực hiện: NGUYỄN NHẬT KHOA
NGUYỄN BẢO THẮNG Lớp : 22K50301 Khoá : 26
THÀNH PHỐ HỒ CHÍ MINH, NĂM 2023
Trang 3I would like to express my heartfelt gratitude to you for your support and guidance during the course of my research and the writing of this report Thanks to your invaluable knowledge and expertise, I have gained the necessary skills to complete this report to the best of my ability Your advice and encouragement have helped
me overcome the challenges I encountered in the research process and produce a high-quality report I truly appreciate and respect the hard work and dedication you have shown You have been a great mentor to me, and I hope to continue learning from you in the future.
Trang 4COMPLETED THESIS
AT TON DUC THANG UNIVERSITY
I hereby declare that this thesis is our original work, which was conducted under the guidance of TS Trinh Hung Cuong The research content and results presented
in this thesis are honest and have not been previously published in any form The data presented in the tables, which serve for analysis, comments, and evaluation, were collected by the author from various sources, which are clearly stated in the reference section Additionally, some comments, evaluations, and data from other authors or organizations were used in this thesis, and their sources were cited and acknowledged If any fraudulence is detected, I will take full responsibility for the content of my thesis Ton Duc Thang University is not liable for any copyright violations or infringements that I may have caused during the research process.
Tp Hồ Chí Minh, ngày tháng năm Tác giả
Nguyễn Nhật Khoa
Nguyễn Bảo Thắng
Trang 5
The confirmation and evaluation section of the supervising teacher. The confirmation section of the supervising teacher.
_ _ _ _ _ _ _
Tp Hồ Chí Minh, ngày tháng năm
The assessment section of the grading teacher.
Tp Hồ Chí Minh, ngày tháng năm
Trang 6TABLE OF CONTENTS
CHAPTER 1 - Methodology of Solving Tasks 7
Task 1.d 7
Task 1.e 7
Task 1.f 7
Task 1.g 8
Task 1.h 8
CHAPTER 2-Source codes and outputs 9
Task 1.a 9
Task 1.b 10
Task 1.c 11
Task 1.d 12
Task 1.e 13
Task 1.f 14
Task 1.g 15
Task 1.h 16
Trang 7CHAPTER 1 - Methodology of Solving Tasks
The funtion to check if a number is prime that takes an integer n as input and returns True if n is prime and False otherwise.The function first checks if the number is less than 2, which is not a prime number If
it is less than 2, the function returns False.If the number is greater than or equal to 2, the function checks
if it is divisible by any number from 2 up to the square root of n (inclusive) If it is divisible by any of those numbers, then the function returns False, indicating that the number is not prime If none of those numbers divides n, then the function returns True, indicating that the number is prime Overall, this function implements a basic primality test based on the fact that a number is prime if and only if it is not divisible by any prime number less than or equal to its square root
Task 1.d
The code above is written in Python and performs the following tasks:
It creates a new vector called "odd_integers"
It selects only the odd integers from the matrix A by using the modulo operator (%), which returns the remainder of a division operation When A % 2 is not equal to 0 (i.e., it has a non-zero remainder), that element is an odd integer
It saves these odd integers into the "odd_integers" vector
It prints out the resulting "odd_integers" vector
The code essentially filters out all even integers from the original matrix A and creates a new vector containing only odd integers The resulting "odd_integers" vector is printed out for verification purposes
Task 1.e
The code above is written in Python and performs the following tasks:
It creates a new vector called "prime_numbers"
It uses the "np.vectorize()" function to apply the "is_prime()" function to every element in matrix A
The "is_prime()" function is a user-defined function that takes an input integer and returns True if
it is a prime number, and False otherwise
The result of the "np.vectorize()" function is a Boolean vector indicating whether each element in
A is prime or not
The Boolean vector is used to index matrix A, returning only the prime numbers from A and saving them into the "prime_numbers" vector
The resulting "prime_numbers" vector is printed out for verification purposes
The code essentially filters out all non-prime numbers from the original matrix A and creates a new vector containing only prime numbers The resulting "prime_numbers" vector is printed out for verification purposes
Task 1.f
The code above is written in Python and performs the following tasks:
It creates a new matrix called "odd_rows" containing only the odd-numbered rows of matrix D
It uses the "np.flip()" function to reverse the order of elements in each row of "odd_rows"
Trang 8 The "axis=1" parameter passed to the "np.flip()" function specifies that the reversal should be done along the horizontal axis (i.e., within each row)
The result of the "np.flip()" function is a new matrix called "odd_rows_reversed" containing the odd-numbered rows of D with their elements reversed
The resulting "odd_rows_reversed" matrix is printed out for verification purposes
The code essentially reverses the order of elements in each row of the odd-numbered rows of the original matrix D and creates a new matrix containing these modified rows The resulting "odd_rows_reversed" matrix is printed out for verification purposes
Task 1.g
This code performs the following operations:
It first counts the number of prime numbers in each row of the matrix A using the np.vectorize function with the is_prime function as an argument The resulting array is stored in the variable prime_counts
It then finds the maximum count of prime numbers across all rows of the matrix using the np.max function
It finds the indices of the rows that have the maximum count of prime numbers using the np.where function and stores them in the max_prime_rows variable
Finally, it prints out the elements in each row of the matrix A that has the maximum count of prime numbers
Task 1.h
This code defines a function find_longest_contiguous_odd_sequence that takes a matrix as input and returns a list of rows that have the longest contiguous sequence of odd numbers
The function starts by initializing the max_seq_length and rows_with_max_seq variables to 0 and an empty list, respectively It then iterates over each row in the matrix and for each row, it iterates over each number in the row If the number is odd, it increases the seq_length variable
by 1 and updates the max_seq_in_row variable if the current sequence length is greater than the previous max sequence in the row If the number is even, it resets the seq_length variable to 0
If the max_seq_in_row variable is greater than the current max_seq_length, it updates the max_seq_length variable and sets the rows_with_max_seq list to the current row If the max_seq_in_row variable is equal to the max_seq_length variable, it appends the current row to the rows_with_max_seq list
Finally, the function returns the rows_with_max_seq list containing the rows with the longest contiguous sequence of odd numbers The code then calls this function with matrix A and prints the resulting rows with the longest contiguous sequence of odd numbers
Trang 9CHAPTER 2-Source codes and outputs Task 1.a
Source codes:
Output:
Trang 10Task 1.b
Source code:
Output:
Trang 11Task 1.c
Source code:
Output:
Trang 12Task 1.d
Source code:
Output:
Task 1.e
Source code:
Trang 13Output:
Task 1.f
Source code:
Trang 14Output:
Trang 15Task 1.g
Source code:
Output:
Trang 16Task 1.h
Source code:
Output: