Faculty of Applied Science Department of Applied Mathematics 2.1.2 Requirements *Allow user to enter these information: - Encoded message matrix with size x x y.. - In this work, we used
Trang 1Faculty of Applied Science
Project Semester 212 - CC05 - Group 6
LINEAR ALGEBRA
Trang 2Faculty of Applied Science Department of Applied Mathematics
Trang 31 Introduction
Linear Algebra is one of the most important courses for students studying at Ho Chi Minh City University of Technology in particular and students studying in science, engineering and technology in general Hence, giving this subject a certain amount of time to practice is indispensable to help students have a solid basis to study well in other subjects in the training program
In this project, our team will be doing four given topics related to Linear Algebra, ranging from problems requiring writing a program in order to decode a message, or to solve the topic related to input-output matrix; to the Markov’s Mouse and the genetic problems, which are just some of the applications of Linear Algebra We hope that the project will be straightforward as well as be able to cover most if not all of the problems and our aim is to provide the best possible solution to all of the aforementioned topics
The following is the content of our project
Linear Algebra - Semester 212 - CCO5 - Group 6 Page 3
Trang 4Faculty of Applied Science Department of Applied Mathematics
2.1.2 Requirements
*Allow user to enter these information:
- Encoded message matrix with size x x y
- Key matrix which is a square matrix of order n
eCheck valid key
sCalculate and translate to meaningful message
This work base on theory of encoding and decoding message (application of matrices multiplication) described in “Elementary Linear Algebra-8th edition” by Ron Larson
Trang 5Second, we review the formula to encode message:
- The convention table:
Third, we consider how to decode, using formula: M = KE
- Inverse of key matrix:
- Multiply M =K~'E and produce the result:
1 20 3 27| 115
20] |1 11] [14] [23
Linear Algebra - Semester 212 - CCO5 - Group 6 Page 5
Trang 6Faculty of Applied Science Department of Applied Mathematics
- Development of Python: In the beginning, Python was designed to used on Unix
core After, Python was re-designed to run on MacOs, Windows, etc as well as these OS
*Numpy library for Python:
- There were many libraries supported in Python to deal with calculation, one of the most popular ones is NumPy This library give us many functions and methods to work with large, multi-dimensional arrays and matrices
- In this work, we used these functions:
* np.dot(A.B): multiply matrix A and B
* np.linalg.inv(K): find the inverse matrix of K
* np.zeros((row,col)): create the row*col zeros matrix
Note that before using NumPy MUST have this command:
import numpy as np
*Procedures:
- Receive the key matrix of order n from user input
- Find the inverse of the key matrix
- Receive the size s and the content of the encoded message from the user
Linear Algebra - Semester 212 - CCO5 - Group 6 Page 6
Trang 7- Transform the encoded content to a matrix with size x*y, in which x is equal to n,
and xx y 2s with minimum y; the missing entries of the matrix will be replaced with
Automatically generated by Colaboratory
Original file is located at
https://colab research google com/drive/1
YRQyq9OS6GcbtCKvz4FVIMJhyDAOEkWn -
##Decoding by using matrix
HHHA (tention:
This program base on encoding and decoding theory written in ”
Elementary Linear Algebra-8th edition” by Ron Larson
row = int(input(”Number of rows: ”))
col = int(input(”’Number of cols: ”))
Trang 8Faculty of Applied Science Department of Applied Mathematics
#inverse key
inverse = np linalg.inv(key)
print (inverse)
#input encoded array of number
print(”’Enter encoded matrix”)
print(”Enter size”)
eSize = int(input())
enco = []
print(”Enter encoded matrix — number only - row by row — ONLY one
value per ˆenter `”)
for i in range(eSize):
enco.append(int(input()))
#setting answer eMatrix
#calculate the size of encoded matrix
return string [i]
return “Invalid key/encoded matrix”
#translate matrix to string
Trang 9ind = answer[j ][i]
Trang 10Faculty of Applied Science Department of Applied Mathematics
- Receiving the encoded matrix:
Enter encoded matrix
Trang 11- Receiving the key matrix:
- The reserved matrix will be:
- Receiving the encoded matrix:
Enter encoded matrix
Trang 12Faculty of Applied Science Department of Applied Mathematics
*Find the matrix transition describing the path of the mouse through the house
eA vector q is called steady vector of Markov model if Pq =q where P is matrix
transition, find q
*Suppose that the mouse starts in Room 4, what is the probability that the mouse
in Room 1 after 3 steps?
*Suppose that the mouse starts in Room 5, find the number of expected steps that the mouse in Room 4 for the first time
2.2.2 Theory
- A Markov chain is a dynamical system whose state vectors at a succession of equally spaced times are probability vectors and for which the state vectors at successive times are related by an equation of the form:
Xk = Px,
in which P = [pj] is a stochastic matrix and pj is the probability that the system will
be in state i at time t=k+ 1 if itis in state j at time t= k The matrix P is called the Linear Algebra - Semester 212 - CCO5 - Group 6 Page 12
Trang 13transition matrix for the system
- The state vector for an observation of a Markov chain with k states is a column
vector x whose i-th component x; is the probability that the system is in the i-th state
at that time
- A transition matrix is regular if some integer power of it has all positive entries
223 Solution
*Finding the transition matrix:
If the mouse is in room 1, it will be trapped in room 1 with a probability of 0.1,
therefore, P;; = 0.1 On the other hand, if it is not trapped, it can go to room 2 or 3 with the same possibility of P2, = P3; = a = 0.45
If the mouse is in room 2, it can only move to room 1, it means P= 1
If the mouse is in room 3, it can move to room 1, 4, 5, 6 with the same possibility of
Pi3 = Pag = Ps3 = Pẹ = Ì 4= 0.25
If the mouse is in room 4 or 5, it can only move to room 3: P34 = P35= 1
If the mouse is in room 6, it can move to room 3 or 7, therefore, P36 =†+» 0.5
If the mouse is in room 7, it can move to room 6, 8 or 9: Pg7 = Pg7 = Paz = 1
If the mouse is in room 8 or 9, it can only move to room 7, that means P7g = Py = 1
The transition matrix will be as the following:
Trang 14Faculty of Applied Science Department of Applied Mathematics
The ij-th entry of the matrix P® gives the probability that the Markov chain, starting
in state s, will be in state s; after n steps
01135 0.5725 0.299375 0025 0025 0.0125, 0 0 [9 257625 0045 0.01125 0.1125 0.1125 005625 0 0Họ 0.538875 0.045 0.01125 0.7375 0.7375 — 7" 90 Lt 6 6 }
*Finding the number of expected steps that the mouse moves from room
5 to room 4 for the first time:
Let WG) be the number of expected steps from room number i to room number 4
Trang 15Solving the above system of equations, we get: \)(5) - 16.2222
= The number of expected steps for the mouse to move from room 5 to room 4 is 16.2222
*Construct the input-output matrix Cy
¢The matrix input-output C is called productive if (I-C)"! exists and (I-C)! = 0
Show that C, is productive
*Find the vector output-values of this system
2.3.2 Requirements
*Allow user to enter these information:
- The size of the input-output matrix
- The demand matrix D
*Construct the input-output matrix
eCheck if that matrix is productive or not
*Find the vector output-values of the system
23.3 Abstract
One of the applications of Linear Algebra is the input-output model, also known as the Leontief model, which is created by the Economics Nobel prize winner Wassilt Leon- tief It is a model for the economics of a whole country or region In the model there are
n industries producing n different products such that the input equals the output or, in other words, consumption equals production One distinguishes two models:
*Open model: some production consumed internally by industries, rest consumed by external bodies
Problem: Find production level if external demand is given
Linear Algebra - Semester 212 - CCO5 - Group 6 Page 15
Trang 16Faculty of Applied Science Department of Applied Mathematics
*Closed model: entire production consumed by industries
Problem: Find relative price of each product
- In this topic, we will cover only the open model to solve the given problem
2.3.4 Theory
*Leontief open model
The model depicts inter-industry relationships within an economy, showing how out- put from one industrial sector may become an input to another industrial sector In the inter-industry matrix, column entries typically represent inputs to an industrial sector, while row entries represent outputs from a given sector This format, therefore, shows how dependent each sector is on every other sector, both as a customer of outputs from other sectors and as a supplier of inputs Sectors may also depend internally on a portion
of their own production as delineated by the entries of the matrix diagonal Each column
of the input-output matrix shows the monetary value of inputs to each sector and each row represents the value of each sector’s outputs
Consider an economic system that has n different industries $;,S2, ., S,, each having
input needs (raw materials, utilities, etc.) and an output (finished product) In producing each unit of output, an industry may use the outputs of other industries, including itself
For example, an electric utility uses outputs from other industries, such as coal and water,
and also uses its own electricity
Let aj be the amount of output the j-th industry needs from the i-th industry to produce one unit of output per year, b; is the number of externally demanded units of
i-th industry, and x,,X2, ., X, be the total output of industry S,,S5, ., S, respectively,
then:
XỊ A11XI † A12X2a + † AInXn + bị
` 3z¡XịI † 422Xa + † 32nXa + bạ
Xn = ânIXỊ + ân2X¿ + + AnnXn + Dy
a1 a2 "°° Ain oa og 4i 322 ''' đạn X2 bạ
since aj, Xx; is the number of units produced by industry §$; and consumed by industry
Sj The total consumption equals the total production for the product of each industry
Si
XI
oO Oo oO X2
Linear Algebra - Semester 212 - CCO5 - Group 6 Page 16
Trang 17in which A is called the input-output matrix, B is the external demand vector and X is the production level vector We have:
X = AX+B
On
‘4 are given and the problem is to determine
In the open Leontief model, A & B eH
X from the matrix equation
We can transform this equation as follows:
I,X — AX=B
© (I, -A)X =B
#X = (1,-A)'B
if the inverse of the matrix (I,—A) exists, then (I,-A) ‘is called the Leontief inverse
*Find the inverse of a matrix using adjoint method
The adjoint of a matrix is one of the simpliest methods used to calculate the inverse
of a matrix It is defined as the transpose of the cofactor matrix of the original matrix For a matrix A, the adjoint is denoted as adj(A)
The minor of entry aj denoted by Mj is defined to be the determinant of the sub-
matrix of order (n-1) that remains after the i-th row and j-th column are deleted from
O&n1 Cha Tử CmO
Linear Algebra - Semester 212 - CCO5 - Group 6 Page 17
Trang 18Faculty of Applied Science Department of Applied Mathematics
adi( ) 1n Cạn TC Can
We can find the inverse of the matrix following the equation:
1 Al= — watdA) i
2.3.5 Code & Explanation
C++ introduction:
- C++ (pronounced: C plus plus) is a middle-level language rendering it the advantage
of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.) It was developed by Bjarne Stroustrup in the early 1980s based
on the C language but with added capabilities
- C++ popularity: Right now, C++ ranks as one of the most popular language in the world It is an extremely fast and efficient language with many tools and frameworks relying on the speed and efficiency of C++ It is in high demand now, and it will remain
in high demand because of its reliability, performance, and efficiency
*Procedures:
- Receive the size n of the input-output matrix from the user
- Construct the input-output matrix C, as the following:
- Receive the demand matrix D from user input
Linear Algebra - Semester 212 - CCO5 - Group 6 Page 18
Trang 19- Calculate (I-C), with I is the identity matrix
- Check if ([—C) is invertible and then fñnd (I—C) ! by using the adjoint of the matrix method
- Check if all entry of the inverse matrix is non-negative to conclude that C, is productive or not
- Find the vector output-values of the system by the following equation:
X= d-Cy'D
*Code:
The source code can be accessed here: Topic 3.cpp
eee
//Topic 3.cpp
/
//This is the program created to solve the problems related
//to the input-ouput matrix as given in Topic 3 by the members
/fof Group 6 — CCO5 -— Semester 212
/
//There are functions created by the creators of the code in
(order to find the inverse of the matrix using the adjoint of
//the matrix method Otherwise, the comments in the code will
//provide enough information about the program
/
//Any attempt to modify or copy the code is PROHIBITED The
//beginning of the program is at "int main()”