lec_4_number_systems_arrays_15

advertisement
Number Systems
Binary System The binary system is the positional numeration system with base or radix
2. This is extremely important for computers since digital computers represent all units of
information using the binary system since the binary system only needs two symbols 0
and 1 and the digital computer has only two states(transistor on (1) or off(0)).
Any binary number is a sequence of 1's and 0 's (called bits=binary digits )possibly with an
embedded binary point. The place value in the binary system are powers of the base or
radix 2 just as the place value in the decimal system are powers of ten. The place values
in the integral part of the binary number are the nonnegative powers of two namely
2 0 ,21 ,2 2 ,2 3 ,2 4 
and the place values of the fractional part of a binary number are the negative powers of
two, namely 2 1 ,2 2 ,2 3 ,2 4 , .
Thus 1101.012  1 2 3  1 2 2  0  21  1 2 0  0  2 1  1 2 2 .
|
embedded binary point
Note: The reason that binary numbers arise so often in computing can be summarized as
follows. In the digital systems in common use today the devices that store data or
information of any kind (whether magnetically on a disc or tape, electronically in the
random access memory (RAM) in a microchip or in some other way) consist of a large
number of memory elements, each of which can be in one of two states (such as
magnetised or unmagnetised, on or off (1 or 0). Similarly when data is transmitted inside
a computer or through a network, it is usually coded as a stream of signal elements that
take one of two forms, such as the presence or absence of an electric current, or two
alternating currents with different frequencies (The exception is the modem used for data
communication, which work with more than two states). The manipulation of data to
perform arithmetic and other computations takes place in the digital circuitry etched on a
microchip which also operates using currents of just two kinds.
In short the data handled by a digital computer is stored, transmitted and manipulated as a
stream of information “elements” of two types, which can be denoted by the symbols 0
and 1. Thus the binary number system is the most natural way of representing numbers in
a digital computer.
1
Table below shows integers from 0 to 25 in their decimal and binary representations.
Binary
Binary
Decimal
16 8 4 2 1
Decimal
16 8 4 2 1
0
0
17
10001
1
1
18
10010
2
01
19
10011
3
11
20
10100
4
100
21
10101
5
101
22
10110
6
110
23
10111
7
111
24
11000
8
1000
25
11001
9
1001
26
11010
10
1010
27
11011
11
1011
28
11100
12
1100
29
11101
13
1101
30
11110
14
1110
31
11111
15
1111
32
100000
16
10000
Binary to Decimal Conversion To convert a binary number to its decimal representation
we just write the appropriate place value over each bit and then add the powers of two
that are weighted by 1.
2 5 2 4 2 3 2 2 21 2 0
Example 1 1101012  1 1 0 1 0 1
 1  2 5  1  2 4  0  2 3  1  2 2  0  21  1  2 0
 32  16  0  4  0  1=53.
2 2 21 2 0 . 2 1 2 2 2 3
Example 2 101.11012  1 0 1 . 1 1
0
2 4
1
 1  2 2  0  21  1  2 0  1  2 1  1  2 2  0  2 3  1  2 4
1 1
1
 4  0 1   0 
2 4
16
 5  0.5  0.25  0.0625
 5.8125 .
2
Example Write in expanded notation and give the decimal representation of the
following: (a) 11011 0 2 and (b) 11.0110 12 .
Solution
25
(a) 11011 0 2  1
24 23 22
1 0 1
21
1
20
0
 1  2 5  1  2 4  0  2 3  1  2 2  1  21  0  2 0
 32  16  0  4  2  0
 54 .
21
(b) 11.011012  1
2 0 2 1
1 . 0
2 2
1
2 3
1
2 4
0
2 5
1
 1  21  1  2 0  0  2 1  1  2 2  1  2 3  0  2 4  1  2 5
 2 1 0 
1 1
1
 0
4 8
32
 3  0.25  0.125  0.03125
 3.40625
3
Decimal to Binary Conversion We find the binary representation of a decimal number
by converting its integral part N I and its fractional part N f separately.
Example 1 Find the binary representation of N  109.78125
First we find the binary representation of 109. To do this we divide N I  109 and each
successive quotient by 2 noting the remainders as follows
Divisions
109  2
54  2
27  2
13  2
62
32
1 2
Quotients
54
27
13
6
3
1
0
Remainders
1
0
1
1
0
1
1
Note The zero quotient indicates the end of the calculation. Observe the remainders can
only be 0 or 1 since divisions are by 2. The sequence of remainders from the bottom to
the top yields the requires binary equivalent.
Thus N I  109  11011012
In practice this can be condensed as follows
2|109
2 54
Remainders
1
0
2 27
1
2 13
1
2 6
0
2 3
1
21
1
0
Thus N I  109  11011012 .
Note The reason this works is that the constant division by 2 with remainder of 1's or 0 's
give the numbers of powers of 2(nonzero number with remainder 1) in the decimal
number.
4
To convert the fractional part N f  0.78125 to its binary equivalent multiply N f and its
successive fractional part by 2 noting the integral part of the product as follows:
Multiplication
0.78125  2  1.56250
0.56250  2  1.1250
0.12500  2  0.25000
0.25000  2  0.50000
0.50000  2  1.00000
Integral part
1
1
0
0
1
2 1
2 2
2 3
2 4
2 5
Note The zero fractional part indicates the end of the calculation.
The reason this works is that the integral part of any product can only be 0 or 1 since we
are doubling a number less than 1 always. The first doubling gives you the power of 2 1 .
If the integral part is 1 then we have the power of 2 1 in the binary representation, while
if the integral part is 0 then we do not have the power of 2 1 in the binary representation.
With each successive multiplication of the fractional part by 2 we are getting the
successive negative power of 2(if the integral part of the product is 1).
Hence 0.7812510  0.110012 and 109.7812510  1101101.110012
.
Example Convert 25 to binary form.
2|25
2 12
Remainder
1
Powers of 2
20
2 6
0
21
2 3
0
22
21
1
23
___
0
1
24
Hence 2510  110012
Check 110012  1  2 4  1  2 3  0  2 2  0  21  1  2 0
 16  8  0  0  1  25 .
5
The binary representation of a terminating decimal fraction does not always terminate as
shown in the next example.
Example Find the binary representation of N  0.610 .
0.6
Solution
2
_____
1.2
2
____
0.4
2
____
0.8
2
____
1.6
2
____
1.2
2
____
0.4
2
_____
0.8
2
____
1.6
Note 1: The first four steps repeat and hence we obtain the above four digits again and
again.
Thus N  0.610  0.1001100110012
Note 2: The number of bits which repeat is not always four nor does the repeating block
necessarily begin at the binary point as next example shows. It all depends on N .
Note 3 However the decimal equivalent of a terminating binary also terminates.
Example 0.1101112  1 2 1  1 2 2  1 2 4  1 2 5  1 2 6
=0.5+0.25+0.0625+0.03125+0.015625=0.859375.—terminates.
Note 4: Binary representation must be terminating since negative powers of 2 are
terminating in their decimal representation.
The main drawback of using the binary system is that for large integers in the decimal
system will have many digits(  3 times) in their binary representation(example
9910  11000112 ). The advantage of larger bases or radix is that we can write numbers
using fewer digits(example 985.781210  3D9.C816 ).
6
The Octal and Hexadeciml systems Now the binary system is the number system that is
used internally in the computer, because of the nature of the digital computer(i.e. on an
off states and the Boolean logic circuits). However strings of 1's and 0 's are difficult for
people to interpret and translate into the much more human friendly decimal system. Also
the decimal system is inconvenient if we have to convert between it and the binary
system, because as we have seen the conversion can involve a substantial amount of
calculation. To bridge the gap between the binary system and the decimal system other
number systems are used in computing, principally the octal(base 8) and the
hexadecimal(base 16) systems. On the other hand because 8 and 16 are powers of 2 there
is an almost instant interconnection between the octal and binary systems and the
hexadecimal and binary systems. On the other hand the octal and the hexadecimal
systems are comparable in compactness with the decimal system and there are simple
algorithms for converting between binary and octal and between binary and hexadecimal.
The Octal System In the octal system numbers are written using a base 8 and the octal
digits are 0,1,2,3,4,5,6,7. The place value of each digit is a power of 8.
Example 374.2 8  3  8 2  7  81  4  8 0  2  8 1
1
 3  64  7  8  4  1  2 
8
 192  54  4  0.25
 252.2510
Converting a decimal number to its octal representation is the same as converting from a
decimal to binary except that you divide the integer part by 8 (instead of 2 for a binary)
and multiply the fractional part by 8(instead of 2 for binary)
Example Convert 275.437 510 to its octal representation.
8|275
8 34
Remainder
3
Powers of 8
80
8 4
2
81
___
0
4
82
Observe the remainders can only be 0 , 1 ,2,3,4,5,6,7 since divisions are by 8. The
sequence of remainders from the bottom to the top yields the requires octal equivalent.
Hence 27510  4238 .
Check 4238  4  8 2  2  81  3  8 0
 4  64  16  3
 27510
7
To convert the fractional part we multiply it by 8 and take the integer part as follows.
We do this successively and the resulting integer parts form the octal representation of
the factional part of the original decimal number.
Fractional part
0.4375  8  3.5000
.5000  8  4.0000
Integer part
3
4
Powers of 8
8 1
8 2
Note: Process ends when fractional part becomes zero.
Thus 0.437510  0.34 8 .
3 4
Check 0.348  3  8 1  4  8 2  
 0.37510  0.062510  0.437510
8 64
Thus 275.437 510  423.348 .
Example Convert 981.72510 to its octal representation
Solution First convert the integer part 98110
8|981
Remainder
5
8 122
Powers of 8
80
8 15
2
81
81
7
82
____
83
0
1
Hence 98110  17258 .Check 17258  1  8 3  7  8 2  2  81  5  8 0
 512  448  16  5  98110
To convert the fractional part.
Fractional part
Integer part
Powers of 8
8 1
5
0.725  8  5.800
8 2
6
0.800  8  6.400
8 3
3
0.400  8  3.200
8 4
1
0.200  8  1.600
8 5
4
0.600  8  4.800
8 6
6
0.800  8  6.400
it is now recurring so we stop here
Thus 0.72510  0.5631468  repeating
Check 0.563146 8   5  8 1  6  8 2  3  8 3  1  8 4  4  8 5  
5 6
3
1
4
 



 
8 64 512 4096 32768
 0.625  0.09375  0.005859375  0.00024 4141
 0.00012207  
 0.72497558610 
8
Conversion of binary to octal
To convert binary to octal is particularly easy. We will illustrate by a few examples.
Example 1 Convert (a) 10001101112 and (b) 1011.010112 to octal.
Solution Break string of 1's and 0 's into groups of 3 from the binary point going from
right to left(for integer part) and from the binary point from left to right(for the
fractional part).
Remember octal digits have a unique 3 bit representation
Octal
0
1
2
3
4
5
6
7
Binary 3 bit representation
000
001
010
011
100
101
110
111
1 000 110 1112
1 0
6
7
Thus 10001101112  1067 8 .
00 1 011 . 010 11 0 2
1
3 . 2
6
Hence 1011.010112  13.268 .
Conversion of octal to binary
To convert from octal to binary is equally easy as we just reverse the process.
Example 2 Convert (a) 61702 58 and (b) 43.0276 8 to binary form.
Solution (a)
6
110
1
001
7
111
0
2
000 010
58
101
Hence 61702 58  1100011110000101012
(b)
4
100
3
011
. 0
. 000
2
010
7
111
68
110
Hence 43.0276 8  100011.000010111110 2
9
Hexadecimal system In the hexadecimal system the base is 16 and the hexadecimal
digits are
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A B C D E F
except that the new symbols are needed (to get rid of the decimal system) and used for 10
11 12 13 14 15 namely, A B C D E F respectively. The place value of each
digit is the power of 16.
Example Convert E 9C.816 to decimal.
E 9C.816  E  16 2  9  161  C  16 0  8  16 1
8
 14  16 2  9  161  12  16 0 
16
 3584  144  12  0.5
 3740.510
Example Convert 4 A5C16 to decimal form.
4 A5C16  4  16 3  A  16 2  5  161  C  16 0
 4  16 3  10  16 2  5  161  12  16 0
=16384+2560+80+12
 1903610
Converting a decimal form to a hexadecimal form we do similar to binary or octal except
we divide by 16.
Example Concert 985.7812 510 to hexadecimal.
Convert the integer part first.
16 985
Remainder
Powers of 18
16 61
9
16 0
16 3
13
161
____
0
3
16 2
Hence 98510  3D916
To convert the fractional part we multiply it by 16 thus
0 .78125
16
12 . 500
16
8 .0000
Hence 0.7812510  0.C816 . Hence 985.7812510  3D9.C816
Check 3D9.C816  3  16 2  13  16  9  16 0  12  16 1  8  16 2
12
8
 3  256  208  9  
=985+0.75+0.03125
16 256
= 985.7812510
10
Example Convert each decimal to its hexadecimal form
(a) 96710 (b) 289310
Solution To convert each decimal to its hexadecimal form we just divide by 16.
(a)
16|967
Remainder Powers of16
16 0
7
16 60
16 3
12
161
___
0
3
16 2
Hence 96710  3C 716
Check 3C 716  3  16 2  12  161  7  16 0
 768  192  7  96710 .
(b)
16|2893
Remainder Powers of16
16 0
13
16 180
16 11
4
161
___
0
11
16 2
Hence 289310  B4D16
Check B4D16 = 11  16 2  4  161  13  16 0  2816  64  13  289310 .
11
Conversion of hexadecimal to binary
To convert from hexadecimal to binary and from binary to hexadecimal we just make use
of the unique 4-bit representation of the hexadecimal digits given below.
Hexadecimal digits
0
1
2
3
4
5
6
7
8
9
10=A
11=B
12=C
13=D
14=E
15=F
4-bit binary representation
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Example Convert to binary form (a) 3 D5916 and 27. A3C16
Solution
(a)
3
D
5
916
0011
1101
0101
100 12
Hence 3D5916  00111101010110012
Check 00111101010110012  213  1  212  1  211  1  210  1  2 9  0  28  1
 2 7  0  2 6  1  2 5  0  2 4  1  2 3  1  2 2  0  21  0  2 0  12
=8192+4096+2048+1024+256+64+16+8+ 12
=1570 510
Also 3 D5916  3  16 3  13  16 2  5  161  9  16 0
 12288  3328  80  9  1570510
(b)
2
0010
7
.
A
3
C16
0111
.
1010
0011
110 0 2
Hence 27. A3C16  100111.10100011112
12
Conversion from binary to its hexadecimal form
To convert binary to its hexadecimal form, we just reverse the process, that is we group
the 0 s and 1s into groups of 4 starting from the binary point . on each side and give the
unique hexadecimal representation of these groups of 0 s and 1s according to the table
above.
Example Convert (a)1011010010111 0 2 and (b) 11100 .10110110112
to hexadecimal form.
Solution
(a)
10
1101 0010 111 0 2
2
D
2
E
Thus 1011010010111 0 2  2 D 2 E16
(b)
0001
1
1100 .
C
1011 0110
.
B
Check 1  161  12  16 0 .
.
28
6
11 00 2
C
11  16 1  6  16 2  12  16 3
11
6
12


16
256
4096
.6875+.0234375+.002929688
28.71386718 810 .
11100 .10110110112  1 2 4  1 23
 1  2 2  0  21  0  2 0  1  2 1  0  2 2  1  2 3
 1  2 4  0  2 5  1  2 6  1  2 7  0  2 8  1  2 9  1  2 10
1 1 1
1
1
1
1
 




2 8 16 64 128 512 1024
1
1
1


 28 . 0.5+0.125+0.0625+0.015625 
128 512 1024
 16  8  4 
=28.7138671 910
13
Lectures on Matrices or Arrays
Introduction Matrices are important in computer science for following reasons
(i)Matrices are used in computer languages such as JavaScript where they are
called arrays.
(ii)Matrices or Arrays are used for storing and manipulating large amounts of data.
(iii)Matrices or Arrays are essential for solving systems of equations particularly on
the computer.
(v)Matrices or Arrays are fundamental tool used in Computer Graphics.
(vi)Matrices or Arrays are used to represent and manipulate Relations which are at the
heart of the theory of databases.
(vii) Matrices or Arrays are used in neural networks in building robots in artificial
intelligence.
.
Definition A matrix is a rectangular arrangement of values i.e.
 1 4


1 0 4
 2 5
 or B  
A  
3 6
3  6 8


7 8


and the dimension or order of the matrix is the number of rows and columns of the
matrix i.e. A above is a 2 3 matrix(Note always give the number of rows first) while B
is a 4  2 matrix.
Note 1: If you multiply number of rows by the number of columns=the number of
elements in the matrix.
Note 2: If a matrix has only one row it is called a row vector

i.e. C  1 2 3 4 5 is a 1 5 matrix=row matrix
0
 
   1
and D    is a 4  1 matrix= column vector.
6
 
5
 
The above matrices have arrows to indicate they are vectors.
 1

E    and F  0 3 are columns and row vectors can be represented
1
graphically.
14
 a1,1 a1, 2 a1,3 a1, 4 


In general the matrix A   a 2,1 a 2, 2 a 2,3 a 2, 4  is a 3 4 matrix
a

 3,1 a3, 2 a3,3 a3, 4 
and a2,3  element in the 2 th row and 3th column and in general
ai , j  element in the i th row and jth column .
e.g
1 2 3 4 


A   5 6 7 8  a1, 4  4 and a3, 4  12 .
 9 10 11 12 


Note 3:Matrices are usually denoted by capital letters i.e. A and their elements by the
corresponding small letters i.e. a i , j .
Square matrices are matrices where the number of rows=the number of columns
and are particularly important as they arise frequently.
 1 1

e.g A  
 24 14 
The main diagonal has elements 1 and 14.
Algebra of Matrices
Two matrices are equal if and only if(iff)
(i)They have the same number of rows and columns i.e. their order or dimension are
the same.
(ii) The corresponding elements are equal.
3 4
 x 4




Example Let X   1 y  and Y   1 6 
 2 w
 z 0




then X  Y if
(i)
the order is the same which it is namely 3 2
(ii)
corresponding elements are the same
i.e if x  3
y6
z2
w  0.
15
Scalar Multiplication is multiplying a matrix by a scalar ( pure number) k --multiply
each element of the matrix by k .
 1 1
 10 10 
 then 10A  
 where the new matrix 10 A is obtained
Example If A  
 24 14 
 240 140 
by multiplying every of A by 10.
kb1, 2 
 kb
 b1,1 b1, 2 
.
 then kB   1,1
If B  


 kb2,1 kb2, 2 
 b2,1 b2, 2 
Addition(Subtraction) of Matrices The sum(difference) of two matrices A and
B written A  B( A  B) is only defined if A and B have the same dimension or order and
then in this case we just add( subtract) the corresponding elements.
Example
 1 3 6
 0  2 8




If A   2 0 4  and B   1 5 2  then since their dimensions are the same
  4 5 1
 2 3 3




Matrix addition (subtraction) is defined and
 1  0 3  (2) 6  8   1

 
C  A  B   2 1
05
4  2   3
 4  2
53
1  3    2

while
 1  0 3  (2) 6  8   1

 
D  A  B   2 1
05
4  2   1
 4  2
53
1  3    6

16
1 14 

5 6
8 4 
 2

5 2 .
2  2 
5
Example
1 2
 5 6
 1 2 3
 , B  
 and D  

If A  
 3 4
 7 8
 4 5 6
Find (i) A  B , (ii) B  D , (iii) B  A and (iv) D  A
Solution
1 2  5 6  1  5 2  6  6 8 
  
  
  

(i) A  B  
 3 4   7 8   3  7 4  8  10 12 
 5 6  1 2 3
  
 .This is not possible since B is a 2  2 matrix
(ii) B  D  
 7 8  4 5 6
while D is a 2 3 matrix and hence matrix addition is not even defined.
 5 6 1 2  5  1 6  2  4 4
  
  
  

(iii) B  A  
7
8
3
4
7

3
8

4
4
4

 
 
 

 1 2 3  1 2
  
 . This is not possible since D is a 2 3 matrix
(iv) D  A  
 4 5 6  3 4
while A is a 2  2 matrix and hence matrix addition is not even defined.
17
Matrix Multiplication is more complicated than matrix addition or subtraction.
For matrix multiplication to be defined the number of columns of the first matrix must
be equal to the number of rows of the second matrix and it goes as follows.
 5 3


 2 4 3
 and B   2 2  then
Let A  
 4  1 2
 6 5


 5 3

 2 4 3 
 2 2 
A  B  A.B  AB  
 4  1 2  6 5 


2 3 = 3 2
2(3)  4(2)  3(5) 
 2(5)  4(2)  3(6)

 
 4(5)  (1)( 2)  2(6) 4(3)  (1)( 2)  2(5) 
 10  8  18 6  8  15   36 29 
  
 .
 
 20  2  12 12  2  10   30 20 
 5 3

 2 4 3 

B  A  B. A  BA   2 2 
 6 5  4  1 2 


3 2 = 2 3
 5(2)  3(4) 5(4)  3(1) 5(3)  3(2) 


  2(2)  2(4) 2(4)  2(1) 2(3)  2(2) 
 6(2)  5(4) 6(4)  5(1) 6(3)  5(2) 


 10  12 20  3 15  6   22 17 21


 
  48
82
6  4    12 6 10   AB .
12  20 24  5 18  10   32 19 28 


 
Matrix multiplication is non-commutative that is AB  BA .
18
 1 6  4 0 

 ,(ii)
Example Compute (i) 
  3 5  2  1
 1 6  2 

  , (iii)
  3 5   7 
 1  1 6 
 
 ,
  6   3 5 
 1
 1
(iv)  3  2  and (v) 2  1  .
 6
 6
Solution
1(0)  6(1)   16  6 
 1 6  4 0   1(4)  6(2)

  
  
 .
(i) 
  3 5  2  1  (3)4  5(2) (3)(0)  5(1)    2  5 
2 2
2 2
 1 6  2   1(2)  6(7)    40 
   
  
 .
(ii) 
  3 5   7   (3)2  5(7)    41
 1  1 6 
 is not possible since we have a 2  1 matrix
(iii)  
  6   3 5 
2  1 2  2 multiply by a 2  2 matrix and hence matrix
multiplication is not even defined.
 1
 1(3) 1(2)   3  2 
  

(iv)  3  2   
 6
 6(3) 6(2)  18  12 
2  1 = 1 2 .
 1
(v) 2  1   2(1)  (1)6   4
 6
1 2 = 2  1
19
Definition The transpose of a matrix A , denoted by AT is the matrix obtained from A
by turning rows into columns
 1 4


 1 2 3
T
 then the transpose of A is A   2 5  .
If A  
 4 5 6
 3 6


 1 4

 1 2 3 T 
T
T T
 , A   2 5  , AT  A .
Note A
 A , thus if A  
 4 5 6
 3 6


 
 
Definition Symmetric matrix Any n  n square matrix A is symmetric if AT  A or
if the matrix is symmetric by inspection about the main diagonal.
 1 2 3
 1 2 3




T
Example A   2 4 5  is symmetric since A   2 4 5   A
 3 5 6
 3 5 6




.
2 w u


Example Let A   7 0 v  find u, v and w so that A is symmetric.
 1  3 4


2 7 1 


Solution A   w 0  3  then A  AT if w  7, u  1 and v  3 .
u v 4 


T
Definition The identity matrix denoted by I is a matrix which has 1S on the main
diagonal and zeros elsewhere, such as
 1 0 0 0


 1 0 0


 1 0
 0 1 0 0
, I   0 1 0  and I  
I  
.
0 0 1 0
 0 1
 0 0 1




 0 0 0 1


The identity matrix I is very important because it plays the same role in matrix algebra
as 1 does in arithmetic i.e. IA  A1  A while 1 5  5 1  5 .
 1 0  a1,1 a1, 2  1(a1,1 )  0(a1, 2 ) 1(a1, 2 )  0(a2, 2 ) 
 


IA  
 

 0 1  a 2,1 a 2, 2   0(a1,1 )  1(a2,1 ) 0(a1, 2 )  1(a2, 2 ) 
 a1,1
 a 2,1
 
Similarly A.I  A .
a1, 2 
  A.
a 2, 2 
20
Example
 1 2 1
 3  2  1




(a)If
A   2 3 1
B   4 3
1
  2 0 1
 6  4  1




compute (i) A  B , (ii) A  5B and (iii) AB .
 1 2 1


Solution (a) A   2 3 1
  2 0 1


 3  2  1


B   4 3
1
 6  4  1


2  (2) 1  (1)    2 4 2 
 1 3


 
A  B   2  (4)
33
11    6 0 0
  2  6 0  (4) 1  (1)    8 4 2 


 
 8  4
 1  15 2  10 1  5   16

 

A  5B   2  20 3  15 1  5     18 18
6 
  2  30 0  20 1  5   28  20  4 

 

 1 2 1 3  2  1



AB   2 3 1  4 3
1
  2 0 1 6  4  1



=
1 0

 0 1
0 0

1(2)  2(3)  1(4)
1(1)  2(1)  1(1) 
 1(3)  2(4)  1(6)


2(2)  3(3)  1(4)
2(1)  3(1)  1(1) 
 2(3)  3(4)  1(6)
  2(3)  (0)( 4)  1(6)  2(2)  0(3)  1(4)  2(1)  0(1)  1(1) 


0

0 .
1 
21
Example
(a)Given the matrices
1  4
 4 3
 5  1  1

 , B  
 , C  
A  
2 
12 0
0  9
  7 0
calculate ,if possible
(i) A  B , (ii) A  C , (iii) BC , (iv) CB and (v) C T A .
Solution
(a)
 1  4  4 3  5  1
  
  

(i) A  B  
 0  9   7 0   7  9
(ii) A  C is not possible since the order of matrices are different.
 4 3  5  1  1


(iii) BC  
2 
  7 0 12 0
2  (2  2)  3 matrix multiplication possible.
4(1)  3(0)
4(1)  3(2) 
 4(5)  3(12)

 

7
(
5
)

0
(
12
)

7
(

1
)

0
(
0
)

7
(

1
)

0
(
2
)


20

36

4

4

6
56

4
2

 

  
 .
= 

35
7
7

35
7
7

 

 5  1  1 4 3 


(iv) CB  
12 0 2   7 0 
2  (3  2)  2 =matrix multiplication is not possible
=
since no. of columns of C  no. of rows of B .
22
 5 12 


(v) C    1 0  . C T A  3  (2  2)  2 =Matrix multiplication possible.
1 2 


 5 12 
 5(1)  12(0) 5(4)  12(9) 

 1  4  

T
 =   1(1)  0(0)  1(4)  0(9) 
C A    1 0 
  1 2  0  9    1(1)  2(0)  1(4)  2(9) 




 5  128 


T
4 .
Hence C A    1
  1  14 


T
23
Use of Matrices for Storage and Manipulation of Data
Matrices and matrix algebra can be used to store and manipulate large sets of data.
We will illustrate this with a number of examples. In computer languages, such as
JaveScript, arrays and array operations are the computer equivalents of matrices and
matrix operations of mathematics.
Example 1
Jim a sales manager, has been requested to provide details of the value of the sales of
bolts in the three regions of the country for the month of June. Jim has data on the
numbers of bolts sold in June which he has recorded in a table as shown below.
Sales in June
North
West
South
Small
100
200
70
Medium
60
60
90
Large
120
90
110
Jim also has the unit costs of the three sizes of bolt, recorded in table below.
Small
Medium
Large
Unit cost of
50
60
70
bolts(cent)
Using matrix algebra calculate the value of the sales in the different regions.
100 200 70 


Let A   60 60 90  be the matrix representing the quantities of the different sizes
120 90 110 


of bolts per region.
Let B  50 60 70 be the matrix representing the unit costs of the different size bolts.
Calculate BA as follows
100 200 70 


BA  50 60 70 60 60 90  .
120 90 110 


BA 
50 100  60  60  70 120
50  200  60  60  70  90 50  70  60  90  70 110
BA  17000 19900 16600
Thus total sales of bolts per region in the month of June
North
€170.00
West
€199.00
South
€166.00
24
Example 2
An engineering company pays its sales representatives a monthly commission based
upon the value of sales they achieve. The rate at which the commission is paid varies
from region to region. The commission on sales in the North is 2%, in the West it is 3%
while in the south it is 1%. If the following table holds the values of sales, expressed in
euros achieved by the company’s representatives for the month of May
Anne
Brian
Carol
North
200
160
120
West
220
140
100
South
260
240
180
how can we calculate the commission owing to the representatives? What we need to do
for a particular representative is to multiply the value of sales achieved in each region
by the appropriate commission rate and then add these values together. This can be done
by matrix algebra. The commission rates are recorded in table below.
North
West
South
2%=0.02
3%=0.03
1%=0.01
Solution
The calculation can be done as follows
 200 220 260  0.02   200  0.02  220  0.03  260  0.01  4.0  6.6  2.6 


 
 

 160 140 240  0.03  =  160  0.02  140  0.03  240  0.01  =  3.2  4.2  2.4 
 120 100 180  0.01  120  0.02  100  0.03  180  0.01   2.4  3.0  1.8 


 
 

13.2 


=  9 .8 
 7 .2 


Thus the commission received by representatives in May was Ann €13.2, Brian €9.2 and
Carol €7.2.
25
Example 3
Consider a small engineering company and consider the problem of calculating the costs
of overheads incurred in selling bolts in the three regions of the country. For this example
we will regard the costs of deliveries and warehousing are the two main overheads. The
sales manager records these costs which vary from region to region in following table.
North
West
South
Costs per unit in cents
Delivery
0.3
0.2
0.2
Warehousing
0.01
0.02
0.04
The breakdown of sales totals for the months of June is given in Table below.
Sales in June
North
West
South
Small
100
200
70
Medium
60
60
90
Large
120
90
110
To calculate the overhead costs incurred in June, the costs per unit must be multiplied by
number of units sold.
Solution
Let matrix A and matrix B represent costs per unit and sales in June respectively.
 0.3 0.01 
100 200 70 




A   0.2 0.02  and B   60 60 90  .
 0.2 0.04 
120 90 110 




100 200 70  0.3


C  BA   60 60 90  0.2
120 90 110  0.2


100  0.3  200  0.2  70  0.2

  60  0.3  60  0.2  90  0.2
 120  0.3  90  0.2  110  0.2

0.01

0.02 
0.04 
100  0.01  200  0.02  70  0.04 

60  0.01  60  0.02  90  0.04 
120  0.01  90  0.02  110  0.04 
 84 7.8 


  48 5.4 
 76 7.4 


26
Tutorial on Use of Matrices for storage and manipulation of Data
Example 4 A distributor records the weekly sales of personal computers(PC’s) in three
retail outlets in different parts of the country and given in Table below.
Shop A
Shop B
Shop C
Number of computers sold in each shop
Pentium(basic)
Pentium(special)
150
320
170
420
201
63
Pentium(latest)
180
190
58
The cost price of each model is Pentium(basic) €480, Pentium(special) €600 and
Pentium(latest) €1020.
The retail price of each model in each of the three shops is given in Table below.
Shop A
Shop B
Shop C
Selling Price of computers in each shop
Pentium(basic)
Pentium(special)
560
750
520
690
590
720
Use matrix multiplication to calculate
(a) Total weekly cost of computers to each shop.
(b) Total weekly revenue of each model for each shop
(c) Total weekly profit for each shop.
Which shop makes the greatest overall profit?
27
Pentium(latest)
1580
1390
1780
Arrays or Matrices in computer programming
In computer programming array or matrix addition and multiplication can easily be done
in a few lines of code using the for do loops.
Example Write a few lines of Usercode to do array or matrix addition.
Comment This will add two matrices A and B of order m n and store the result in
matrix C of order m n .
for i=1 to m do
for j=1 to n do
C(i,j)=A(i,j)+B(i,j)
end do
end do
Example Write a few lines of Usercode to do array or matrix multiplication.
.
Comment This will multiply two matrices A and B of order m  p and p  n and
store the result in matrix C of order m n .
for i=1 to m do
for j=1 to n do
sum=0
for k=1 to p do
C(i,j)=sum+A(i,k)  B(k,j)
end do
end do
end do
28
Functions
Introduction The idea of one thing depending on another leads to the concept of
function. Functions are very important in mathematics and in computing.
Definition Let X and Y be sets. A function f from X to Y is a rule that assigns to each
element of X exactly one element of Y . The set X is called the domain and the set Y
the codomain .
If f is a function from X to Y we indicate this by writing f : X  Y , Rule
You can think of a function as a black box. There is an input x from the domain and the
function f (x) or black box carries out some process on x ,say squaring y  f ( x)  x 2 and
gives an output y  x 2 to the codomain.
This function can be written f : R  R, f ( x)  x 2 .
Here the domain is R(the real number system) or the x-axis and the codomain is R(the
real number system) or the y-axis.The Range is the set of all possible outputs which in
this case is the non-negative y-axis.
We can also draw its graph.
Graph of x^2
y-axis
10
5
2
x
0
4
2
0
2
4
x
x-axis
29
We have already met the concept of a function in coordinate geometry namely, the linear
function y  f ( x)  ax  b , where a and b are constants.
Example 1 Draw the graphs of the linear functions
2x  5 y  7
9x  7 y  2 .
Rewrite these linear functions in their slope form, namely y  f (x) and y  g (x) and
determine their domain X and codomain Y .
Solution
2
7
Rewriting first equation we get 2 x  5 y  7  5 y  2 x  7  y   x  .
5
5
2
7
This is a function since it is a rule  y   x  that maps every point in its domain
5
5
(, ) [ that is every point on the x  axis ] to a point on the codomain (, ) [ that is
to a point on the y  axis] .
Rewriting the second equation we get 9 x  7 y  2  7 y  9 x  2  y 
9
2
x
7
7
9
2
x  that maps every point in its domain
7
7
(, ) [ that is every point on the x  axis ] to a point on the codomain (, ) [ that is
to a point on the y  axis] .
This is a function since it is a rule  y 
Their graphs are shown below.
30
Graph of Linear Functions
4
2
  2  x  7 
   
 5   5
 9  x 2 
   
 7  7
0
2
4
4
2
0
2
x
31
4
Example 2 Draw the graphs of the quadratic functions (i) y  x 2  x  6 ,
(ii) y  x 2  2 x  1 and (iii) y  x 2  2 x  2 .
Solution
(i) This is a function since it is a rule y  x 2  x  6 that maps every point in its domain
(, ) [ that is every point on the x  axis ] to a points on the codomain (, ) [ that is
to a points on the y  axis] .
y  x 2  x  6  y  ( x  3)( x  2)  x  3 or x  2
Alternatively x 
1  1  4(1)( 6) 1  25 1  5


 3 or  2
2
2
2(1)
.
Thus it cuts the x-axis at x=3 and x=-2. Also it cuts the y-axis at y=-6
since y  0  0  6  6 .Its graph is shown in red below.
(ii) This is a function since it is a rule y  x 2  2 x  1 that maps every point in its domain
(, ) [ that is every point on the x  axis ] to a point on the codomain (, ) [ that is
to a points on the y  axis] .
y  x 2  2 x  1  ( x  1) 2  x  1.
2  4  4(1)(1) 2  0

 1.
2
2(1)
Double root at x  1 so graph touches x-axis at x=1. Also it cuts the y-axis at y=1
Alternatively x 
since y  0  0  1  1 .Its graph is shown in small dotted blue below.
(iii) This is a function since it is a rule y  x 2  2 x  2 that maps every point in its
domain (, ) [ that is every point on the x  axis ] to a point on the codomain (, ) [
that is to a points on the y  axis] .
y  x 2  2x  2 .
 2  4  4(1)( 2)  2   4  2  2  1


 1   1
2
2
2(1)
These are imaginary roots and so graph does not cross x-axis.
Also it cuts the y-axis at y=2 since y  0  0  1  1 .Its graph is shown in large dotted
green below.
x
32
Graphs of Quadratic Functions
10
5
2
x  x 6
2
x  2x 1
 x2 2x  2
0
5
 10
 10
5
0
5
x
33
10
1
Let f : [0, )  [0, ) where f ( x)  x  x 2 . Draw its graph and state its domain, codomain and range. Is this a function?
Solution Yes it is a function as every real number in its domain [o, ) is mapped to an
element of its co-domain [o, ) e.g.  x  0  [0, ) is mapped to
Its domain is [o, ) ,co-domain [o, ) and range [o, ) .
Its graph is given below.
100
75
x
50
25
0
0
25
50
75
100
x
The graph of the square root function
34
x  0  [0, ) .
In computing, many functions are not functions whose graphs are continuous curves
like above but discrete functions ( usually given by arrow diagrams) as shown below.
Example Consider the set A  1,2,3,namely the domain and the set B  1,2,3,4the
Codomain and the discrete function f from A  B defined as follows
f : A  B, by f (1)  3 , f (2)  2 and f (3)  2 shown in the arrow diagram
below.
f (1)  3
f ( 2)  2
f (3)  2
Note: 3 is called the image of 1 under f
Note the domain is A  1,2,3,the codomain B  1,2,3,4 and the Range  2,3.
This is a function since it is given by a rule and every element in the domain is mapped to
an element in the codomain This is typical of the discrete functions in computing.
Definition A function is onto if its range is equal to its codomain.
The above function is not onto since its range  2,3  codomain 1,2,3,4.
Definition A function is one-to-one if no two distinct elements of the domain have the
same image.
Above function is not one-to-one since the two elements of the domain 2 and
3 have the same image namely 2 in the codomain.
Example Consider the set A  a, b, c,namely the domain and the set B  1,2,3the
Codomain and the discrete function f : A  B defined by f (a)  2 ,
f (b)  1 and f (c )  3 shown in the arrow diagram below.
f (a)  2
f (b)  1
f (c )  3
This is a function since it is given by a rule and every element in the domain is mapped to
an element in the codomain This discrete function is (i) onto since its range
 1,2,3  its codomain 1,2,3.
It is also (ii) one-to-one as no two distinct elements of the domain have the same image.
35
The importance of the properties of (i) onto and (ii) one-to-one for functions is that
these two properties ensure that the function have an inverse according to the following
theorem
Theorem If a function f : X  Y is (i) onto and (ii) one-to-one then f (x) has an
inverse
f 1 : Y  X .
Now the above function f has an inverse f 1 : B  1,2,3  A  a, b, c given by
f 1 (2)  a
f 1 (1)  b
f 1 (3)  c .
You just reverse the process.
The big deal is that if a function f is onto and one-to-one then it has an inverse f 1
then you can go from f to f 1 and form f 1 to f as next computer example shows.
36
A Computer example of a function and its inverse function
An important computer example of the use of the concepts of a function and its
inverse function is the ASCII code( Acronym for American Standard Code for
Information Interchange )used in most computers to represent text which makes it
possible to transfer data from one computer to another computer.
Computers were designed first to manipulate binary numbers and that is all they can still
do. The cleverness of modern software may make it appear that they do much more but
this is an illusion. The interior world of the computer is binary numbers only.
Characters are fundamental form of data. Computers store characters as integers and
system hardware and software translate them into integer codes so that monitors and
printers can display them.
Definition A character is a symbol that is defined by the ASCII Table below and these
include those that are typed from a key board(possibly with the Shift key pressed) and is
written in single inverted commas such as ‘C’ .
Aside: As well as the familiar characters on a keyboard, the current international
standard(UNICODE) includes codes for characters from a variety of languages(English,
Spanish, Chinese etc) and alphabets. For simplicity we only use part of the code shown
below. This is confined to printable symbols that appear on a standard computer
keyboard for an English-speaking user. The original ASCII set(developed by ANSI, the
American National Standards Institute) was finalized in 1968 and provides a basic(but by
no means complete) character set for English. With the development of the Internet and a
more global economy, efforts are being made to create a standard character set, catering
more completely for many languages and bringing together hundreds of incompatible
standards from different countries. UNICODE is the result of this development, but it
still only represents some of the written languages, currently standing at about 94000
symbols. No existing character set caters for all languages.
We reduce written text to binary numbers by using a code that sets up a unique
correspondence between all characters(letters, decimal characters and special symbols)
and binary numbers. The standard code that does this is UNICODE a development of
earlier code called ASCII (American Standard Code for Information Interchange).
Unicode supports exactly 65536 numbers stored from 0—65535 in 16
bits= 216  1  65536 .Now each ASCII character has a unique character code in the range
of integers from 0 to 126 and conversely each integer in this range is the ASCII code of
exactly one character as defined in partial ASCII code table below.
37
ASCII Table
ASCII ( Acronym for American Standard Code for Information Interchange) is a code
for representing English characters as numbers with each letter assigned a unique
number(integer) from 0-127(This is extended to more characters and integers n the
extended ASCII code).
e.g. uppercase M is represented in ASCII code by 77
lowercase m is represented in ASCII code by 109.
Most computers use the ASCII codes to represent text which makes it possible to transfer
data from one computer to another.
38
Character
(space)
!
"
#
$
&
%
'
(
)
*
+
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
Integer
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Partial ASCII Code Table
Character Integer
Character Integer
I
73
q
114
J
74
r
115
K
75
s
116
L
76
t
117
M
77
u
118
N
78
v
119
O
79
w
120
P
80
x
121
Q
81
y
122
R
82
z
123
S
83
{
124
T
84
|
125
U
85
}
126
V
86
~
127
W
87
X
88
Y
89
Z
90
[
91
\
92
]
93
^
94
_
95
'
96
a
97
b
98
c
99
d
100
e
101
f
102
g
103
Null
0
h
104
start of heading 1
i
105
start of text
2
j
106
end of text
3
k
107
l
108
m
109
n
110
o
111
112
p
113
39
In mathematical terms this table can be represented by the function ord
ord : null , start of headng,}, ~  0,1126,127 ,
where the domain S is the set of 128 characters null , start of heading, ,}, ~ and
the the codomain T is a set of 128 integers 0,1,126,127
ord is a function since the table gives the mapping S  T namely
the subset of ordered pars null ,0, start of headng,1 },126, ~,127.
The function ord is onto since its range R  codoman T .
The function ord is one-to-one since chr1  chr2  S  ord (chr1 )  ord (chr2 )  T .
Thus the function ord is (i)onto and (ii) one-to-one and hence has an
inverse which we call chr  ord 1 .
This inverse function chr is given (by reversing the ASCII table and
interchanging the domain and codomain) by
chr : 0,1,2,3,125,126,127  null , start of headng, ,}, ~
where the domain is a set of 128 integers 0,1,126,127 and the
codomain is the set of 128 characters null , start of heading, ,}, ~.
Thus ord ( start of heading )  1 and ord (})  126 .
Also from the table we see ord (@)  64 , ord (C )  67 and
ord (c)  99 .
In addition chr (1)  ord 1 (1)  start of heading , chr (126)  ord 1 (126) }
chr (64)  ord 1 (64)  @ , chr (67)  ord 1 (67)  C and chr (99)  ord 1 (99)  c .
Example From the table above what is chr (94) ? ord (' B' ) ? ord ('b' ) ? and ord ('7' ) ?
Solution chr (94)  ^ , ord (' B' )  66 , ord (' b' )  98 and ord ('7' )  55
40
Octal
0
1
2
3
4
5
6
7
Binary 3 bit representation
000
001
010
011
100
101
110
111
Hexadecimal digits
0
1
2
3
4
5
6
7
8
9
10=A
11=B
12=C
13=D
14=E
15=F
4-bit binary representation
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
41
Download