You are on page 1of 171

Scilab Textbook Companion for Switching And Finite Automata Theory by Z.

Kohavi1
Created by Kota Sumanth Kumar B.Tech (Pursuing) Electronics Engineering NIT Warangal College Teacher S. K. L. V. Sai Prakash, NIT Warangal Cross-Checked by Giridharan, IITB August 16, 2013

by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab codes written in it can be downloaded from the Textbook Companion Project section at the website http://scilab.in

1 Funded

Book Description
Title: Switching And Finite Automata Theory Author: Z. Kohavi Publisher: Tata McGraw - Hill Education Edition: 2 Year: 2008 ISBN: 0-07-099387-4

Scilab numbering policy used in this document and the relation to the above book. Exa Example (Solved example) Eqn Equation (Particular equation of the above book) AP Appendix to Example(Scilab Code that is an Appednix to a particular Example of the above book) For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means a scilab code whose theory is explained in Section 2.3 of the book.

Contents
List of Scilab Codes 1 Number System and Codes 2 Sets Relations and Lattices 3 Switching Algebra And Its Applications 4 Minimization Of Switching Functions 5 Logical Design 6 Functional Decomposition And Symmetric Functions 7 Threshold Logic 8 Reliable Design And Fault Diagnosis 5 8 21 34 44 89 95 99 105

10 Capabilities Minimization and transformation Of Sequential Machines 108 12 Structure Of Sequential Machines 13 State Identicaiton And Fault Detection Experiments 111 118

14 Memory Deniteness Information Losslessness of Finite Automata 120 15 Linear Sequential Machines 128

16 Finite State Recognizers

137

List of Scilab Codes


Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 2.1 2.2 2.4 2.5 2.6 2.7 2.11 2.12 2.15 2.16 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 converts no to base 10 . . . . . . . . . . . . converts binary number to base 10 . . . . . convert decimal number to base 8 . . . . . . Convert decimal value in base 10 to base 8 convert decimal number to base 2 . . . . . . Convert Octal number to Base 2 . . . . . . Adds two binary numbers . . . . . . . . . . Subtracts Two Binary numbers . . . . . . . Multiplies two Binary numbers . . . . . . . Division of Two Binary Numbers . . . . . . Set Of All even Numbers . . . . . . . . . . Subsets Of a Faces of a die . . . . . . . . . Relation between two sets . . . . . . . . . . Equivalence relation . . . . . . . . . . . . . Equivalence relation 2 . . . . . . . . . . . . Function Check . . . . . . . . . . . . . . . . Partial Ordering Divisibilty Relation . . . . Ordering Relation . . . . . . . . . . . . . . Lattice Of subsets . . . . . . . . . . . . . . glb and ulb . . . . . . . . . . . . . . . . . . Simplify 1 . . . . . . . . . . . . . . . . . . . Simplify 2 . . . . . . . . . . . . . . . . . . . Prove the identity . . . . . . . . . . . . . . Determine The Output of expression . . . . Simplify 3 . . . . . . . . . . . . . . . . . . . Simplify 4 . . . . . . . . . . . . . . . . . . . Expand the Expression . . . . . . . . . . . Expand the Expression 2 . . . . . . . . . . 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 9 11 12 13 14 17 18 19 19 21 22 24 25 27 28 29 30 31 32 34 34 35 36 36 37 37 38

Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa

3.9 3.10 3.11 3.12 3.13 3.14 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 5.1 5.2 5.3 5.4 5.5 6.1 6.3 6.6 6.7 7.1 7.2 7.3 7.4 7.5 8.1 8.2 8.3 8.4 10.1 10.2 12.4 12.5

POS . . . . . . . . . . . . . . . . . . Tabulate the Function of 2 variables NOR . . . . . . . . . . . . . . . . . Transmission function . . . . . . . . Air Conditioning System . . . . . . DeMorgans Law . . . . . . . . . . . Irredundant expressions . . . . . . . Irredundant expressions 2 . . . . . . Reduce Expression . . . . . . . . . . BCD to Excess 3 Convertor . . . . . 5 variable Kmap . . . . . . . . . . . Prime Implicants . . . . . . . . . . . Prime Implicants 2 . . . . . . . . . . Prime Implicants Of a Function . . . Prime Implicants Of a Function 2 . . Cyclic Prime Implicant map . . . . . Odd Parity Bit Generator . . . . . . Serial To Parallel converter . . . . . Transmission function for a network 4 Input Contact Network . . . . . . Minimal contact Network . . . . . . Function Decomposition . . . . . . . Muliplicity . . . . . . . . . . . . . . Symmetric . . . . . . . . . . . . . . Symmetric 2 . . . . . . . . . . . . . weighted Sum . . . . . . . . . . . . . Inequalities . . . . . . . . . . . . . . Unate Functions . . . . . . . . . . . three cube representation . . . . . . True Vertex . . . . . . . . . . . . . . NOR Logic Circuit . . . . . . . . . . Path Sensitizing . . . . . . . . . . . Two level OR AND Network . . . . Various errors Summarized . . . . . Reducing machine . . . . . . . . . . Compaitability Graph . . . . . . . . Closed Partitions . . . . . . . . . . . Output Consistent . . . . . . . . . . 6

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

39 39 40 41 41 42 44 52 60 68 69 86 87 87 88 88 89 90 91 92 93 95 95 97 97 99 100 102 102 103 105 106 106 107 108 109 111 112

Exa 12.7 Exa 12.8 Exa 12.9 Exa 12.10 Exa 12.11 Exa 12.12 Exa 12.14 Exa 13.1 Exa 14.2 Exa 14.3 Exa 14.4 Exa 14.6 Exa 16.7 Exa 15.3 Exa 15.4 Exa 15.5 Exa 15.6 Exa 15.10 Exa 15.11 Exa 15.12 Exa 15.15 Exa 15.16 Exa 15.17 Exa 16.2 Exa 16.3 AP 1 AP 2 AP 3 AP 4 AP 5 AP 6 AP 7 AP 8 AP 9

Dependence . . . . . . . . . . . . . . . . . . . . input Consistent Matrix . . . . . . . . . . . . . Implementation of Input Consistent matrix . . Autonous Clock . . . . . . . . . . . . . . . . . partition pair . . . . . . . . . . . . . . . . . . . partition pair 2 . . . . . . . . . . . . . . . . . . State Consistent Partitions . . . . . . . . . . . Fault DetectionExeriment . . . . . . . . . . . . Synchronizing tree . . . . . . . . . . . . . . . . Contracted State Machine . . . . . . . . . . . . Testing Table . . . . . . . . . . . . . . . . . . . Testing graph . . . . . . . . . . . . . . . . . . . Testing Table 2 . . . . . . . . . . . . . . . . . . Transfer function . . . . . . . . . . . . . . . . . Impulse response . . . . . . . . . . . . . . . . . Null Sequence . . . . . . . . . . . . . . . . . . Null Sequence 2 . . . . . . . . . . . . . . . . . Reducable . . . . . . . . . . . . . . . . . . . . . Linear Machine . . . . . . . . . . . . . . . . . . Linear Machine 2 . . . . . . . . . . . . . . . . . Commutative ring . . . . . . . . . . . . . . . . Identifying Whether the Ring is a Field Or not Finite Field . . . . . . . . . . . . . . . . . . . . Prove the identity . . . . . . . . . . . . . . . . Prove the identity 2 . . . . . . . . . . . . . . . Kmap Solver with out Dontcares . . . . . . . . No.of . . . . . . . . . . . . . . . . . . . . . . . Kmap Solver with out Dontcares . . . . . . . . No.of 3d matrix . . . . . . . . . . . . . . . . . Check . . . . . . . . . . . . . . . . . . . . . . . Decimal to Base 2 Converter . . . . . . . . . . Kmap with dont cares . . . . . . . . . . . . . . Kmap for 3 variables with out dontcares . . . . Binary to Decimal convertor . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

113 113 114 114 115 115 116 118 120 121 124 125 126 128 129 130 130 131 132 133 134 134 135 137 137 139 146 147 154 154 155 157 164 168

Chapter 1 Number System and Codes

Scilab code Exa 1.1 converts no to base 10


1 clc // c l e a r s t h e command window 2 clear // c l e a r s a l l t h e v a r i a b l e s 3 4 i =1; w =1; // f l a g s 5 6 bin =432.2 // g i v e n o c t a l number which s h o u l d be 7 8 9 10 11 12 13 14 15

e x p r e s s e d i n b a s e 10 temp1 = floor ( bin ) ; // s e p a r a t i n g i n t e g e r p a r t from t h e g i v e n number temp2 = modulo ( bin ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number temp2 = temp2 *10^3; // c o n v e r t i n g d e c i m a l v a l u e t o interger for convenience while ( temp1 >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r for convenience p ( i ) = modulo ( temp1 ,10) ; temp1 = round ( temp1 /10) ; i = i +1; end while ( temp2 >0) // s t o r i n g e a c h d e c i m a l d i g i t i n v e c t o r for convenience 8

16 q ( w ) = modulo ( temp2 ,10) ; 17 temp2 =( temp2 /10) ; 18 temp2 = round ( temp2 ) ; 19 w = w +1; 20 end 21 temp1 =0; // c l e a r i n g t e m p o r a r y v a r i a b l e temp1 22 for i =1: length ( p ) // c h e c k i n g w h e t h e r i t i s a b i n a r y 23 24 25 26 27 28 29 for i =1: length ( p ) // m u l t i p l i y i n g t h e 30 31 end 32 33 temp2 =0; // f l a g b i t 34 35 for z =1: length ( q ) 36 // m u l t i p l i y i n g t h e

number o r n o t if ( p ( i ) >8) then disp ( n o t a b i n a r y number ) ; abort ; end end bits of integer p a r t w i t h t h e i r p o s i t i o n v a l u e s and a d d i n g temp1 = temp1 +( p ( i ) *8^( i -1) ) ;

37 38 end 39 temp = temp1 + temp2 ; 40 // f i n a l l y a d d i n g b o t h t h e

b i t s o f decimal part with t h e i r p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( q ( z ) *8^( -1*(4 - z ) ) ) ;

i n t e g e r and d e c i m a l p a r t s

to get t o t a l output . 41 disp ( temp ) ; // d i s p l a y i n g t h e o u t p u t

Scilab code Exa 1.2 converts binary number to base 10


1 clc ; // c l e a r s t h e command window 2 clear ; // c l e a r s a l l t h e v a r i a b l e s

3 i =1; w =1; 4 bin =1101.01; // Given b i n a r y number which we n e e d t o 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

be c o n v e r t i n t o d e c i m a l temp1 = floor ( bin ) ; // s e p a r a t i n g i n t e g e r p a r t from t h e g i v e n number temp2 = modulo ( bin ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number temp2 = temp2 *10^3; // c o n v e r t i n g d e c i m a l v a l u e t o interger for convenience while ( temp1 >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r for convenience p ( i ) = modulo ( temp1 ,10) ; temp1 = floor ( temp1 /10) ; i = i +1; end while ( temp2 >0) // s t o r i n g e a c h d e c i m a l d i g i t i n v e c t o r for convenience q ( w ) = modulo ( temp2 ,2) ; temp2 =( temp2 /10) ; temp2 = floor ( temp2 ) ; w = w +1; end temp1 =0; // f l a g b i t for i =1: length ( p ) // c h e c k i n g w h e t h e r i t i s a b i n a r y number o r n o t if ( p ( i ) >1) then disp ( n o t a b i n a r y number ) ; abort ; end end for i =1: length ( p ) // m u l t i p l i y i n g b i t s o f i n t e g e r p a r t w i t h t h e i r p o s i t i o n v a l u e s and a d d i n g temp1 = temp1 +( p ( i ) *2^( i -1) ) ; end temp2 =0; // f l a g b i t for z =1: length ( q ) // m u l t i p l i y i n g b i t s o f d e c i m a l p a r t w i t h t h e i r 10

33 34 end 35 temp = temp1 + temp2 ; 36 // f i n a l l y a d d i n g b o t h t h e

p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( q ( z ) *2^( -1*(4 - z ) ) ) ;

i n t e g e r and d e c i m a l p a r t s

to get t o t a l output . 37 disp ( temp ) ; // d i s p l a y i n g t h e o u t p u t

Scilab code Exa 1.3 convert decimal number to base 8


1 clc ; // c l e a r s t h e command window 2 clear ; // c l e a r s a l l t h e v a r i a b l e s 3 format ( v ,8) ; // making t h e d e f a u l t 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

p r e c i s i o n to 8 significant digits i =1; dec =548; // g i v e n d e c i m a l number which s h o u l d be expressed in base 8 temp = dec ; i =1; d =8; while ( temp >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r for convenience p ( i ) =( modulo ( floor ( temp ) ,d ) ) temp = floor ( temp ) / d ; i = i +1; end temp2 =0; for j =1: length ( p ) // m u l t i p l i y i n g b i t s o f i n t e g e r p a r t w i t h t h e i r p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( p ( j ) *10^( j -1) ) ; end disp ( temp2 , O c t a l number ) ; dec =345; // g i v e n d e c i m a l number which s h o u l d be e x p r e s s e d i n 11

22 23 24 25 26 27 28 29 30 31 32 33 34 35

base 8 temp = dec ; i =1; d =6; while ( temp >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r for convenience p ( i ) =( modulo ( floor ( temp ) ,d ) ) temp = floor ( temp ) / d ; i = i +1; end temp2 =0; for j =1: length ( p ) // m u l t i p l i y i n g b i t s o f i n t e g e r p a r t w i t h t h e i r p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( p ( j ) *10^( j -1) ) ; end disp ( temp2 , Base 6 ) ;

Scilab code Exa 1.4 Convert decimal value in base 10 to base 8


1 clc ; // c l e a r s t h e command window 2 clear ; // c l e a r s a l l t h e v a r i a b l e s 3 format ( v ,8) ; // making t h e d e f a u l t

p r e c i s i o n to 8

significant

digits

4 i =1; 5 dec =0.3125; // g i v e n d e c i m a l number which s h o u l d be

expressed in base 8 6 temp = modulo (0.3125 ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number while ( temp ~=0) // s t o r i n g e a c h d e c i m a l d i g i t i n vector for convenience 9 temp = temp *8; 10 p ( i ) = floor ( temp ) ; 11 i = i +1; 12
7 8

12 temp = modulo ( temp ,1) ; 13 end 14 15 temp1 =0; // f l a g b i t 16 for j =1: length ( p ) 17 // m u l t i p l i y i n g b i t s o f d e c i m a l p a r t w i t h t h e i r

p o s i t i o n v a l u e s and a d d i n g
18 temp1 = temp1 +(10^( -1* j ) * p ( j ) ) 19 end 20 disp ( temp1 ) ; // d i s p l a y s t h e f i n a l o u t p u t

Scilab code Exa 1.5 convert decimal number to base 2


1 clc ; // c l e a r s t h e command window 2 clear ; // c l e a r s a l l t h e v a r i a b l e s 3 format ( v ,18) ; // c h a n g i n g t h e d e f a u l t

p r c i s i o n t o 20

significant

digits

4 5 i =1; x =1; // f l a g b i t s 6 7 dec =432.354; // g i v e n d e c i m a l number which s h o u l d be

expressed in base 2 8 temp2 = floor ( dec ) ; // s e p a r a t i n g i n t e g e r p a r t from t h e g i v e n number 9 temp4 = modulo ( dec ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number
10 11 12 13 14 15 16 17

while ( temp2 >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r for convenience p ( i ) =( modulo ( floor ( temp2 ) ,2) ) temp2 = floor ( temp2 ) /2; i = i +1; end temp2 =0; // c l e a r i n g t e m p o r a r y v a r i a b l e 13 temp2

18 19 for j =1: length ( p ) 20 // m u l t i p l i y i n g b i t s

21 22 end 23 24 while ( temp4 ~=0) // s t o r i n g e a c h d e c i m a l

o f i n t e g e r part with t h e i r p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( p ( j ) *10^( j -1) ) ;

d i g i t in

vector for convenience


25 temp4 = temp4 *2; 26 d ( x ) = floor ( temp4 ) ; 27 x = x +1; 28 temp4 = modulo ( temp4 ,1) ; 29 end 30 31 temp5 =0; // c l e a r i n g t e m p o r a r y v a r i a b l e

temp5
32 33 for j =1: length ( d ) 34 // m u l t i p l i y i n g b i t s

35 36 end 37 38 temp3 = temp2 + temp5 ; 39 // f i n a l l y a d d i n g b o t h t h e 40

o f decimal part with t h e i r p o s i t i o n v a l u e s and a d d i n g temp5 = temp5 +(10^( -1* j ) * d ( j ) )

i n t e g e r and d e c i m a l p a r t s

to get t o t a l output . disp ( temp3 ) ; // d i s p l a y s o u t p u t

Scilab code Exa 1.6 Convert Octal number to Base 2


1 clc ; // c l e a r s t h e command window 2 clear ; // c l e a r s a l l t h e v a r i a b l e s 3 format ( v ,8) ; // s e t t i n g t h e d e f a u l t 4

p r e c i s i o n to 8

14

5 i =1; w =1; 6 7 bin =123.4; // Given o c t a l number which we n e e d t o be

convert into binary temp1 = floor ( bin ) ; // s e p a r a t i n g i n t e g e r p a r t from t h e g i v e n number 9 temp2 = modulo ( bin ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number 10 temp2 = temp2 *10^3; // c o n v e r t i n g d e c i m a l v a l u e t o interger for convenience
8 11 12 13 14 15 16 17 18

while ( temp1 >0) // s t o r i n g e a c h i n t e g e r d i g i t i n vector for convenience p ( i ) = modulo ( temp1 ,10) ; temp1 = round ( temp1 /10) ; i = i +1; end while ( temp2 >0) // s t o r i n g e a c h d e c i m a l d i g i t i n vector for convenience q ( w ) = modulo ( temp2 ,10) ; temp2 =( temp2 /10) ; temp2 = round ( temp2 ) ; w = w +1; end temp1 i s a binary

19 20 21 22 23 24 25 temp1 =0; // c l e a r i n g t e m p o r a r y v a r i a b l e 26 27 for i =1: length ( p ) // c h e c k i n g w h e t h e r i t

number o r n o t
28 if ( p ( i ) >8) then 29 disp ( n o t a b i n a r y number ) ; 30 abort ; 31 end 32 end 33 34 for i =1: length ( p ) 35 // m u l t i p l i y i n g b i t s o f d e c i m a l p a r t w i t h t h e i r

15

36 37 end 38 39 temp2 =0; // c l e a r i n g t e m p o r a r y v a r i a b l e temp2 40 41 for z =1: length ( q ) 42 // m u l t i p l i y i n g b i t s o f d e c i m a l p a r t w i t h t h e i r

p o s i t i o n v a l u e s and a d d i n g temp1 = temp1 +( p ( i ) *8^( i -1) ) ;

p o s i t i o n v a l u e s and a d d i n g
43 temp2 = temp2 +( q ( z ) *8^( -1*(4 - z ) ) ) ; 44 end 45 46 temp = temp1 + temp2 ; 47 // a d d i n g b o t h i n t e g e r and d e c i m a l p a r t s t o g e t

total

deciaml value . 48 dec = temp ;


49 50

temp2 = floor ( dec ) ; // s e p a r a t i n g i n t e g e r p a r t from t h e g i v e n number 51 temp3 = modulo ( dec ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number 52 format ( v ,18) ; // s e t t i n g t h e d e f a u l t p r e c i s i o n t o 8
53 54 i =1; x =1; // f l a g b i t s 55 56 while ( temp2 >0) // s t o r i n g e a c h i n t e g e r

d i g i t in vector

for convenience
57 p ( i ) =( modulo ( floor ( temp2 ) ,2) ) 58 temp2 = floor ( temp2 ) /2; 59 i = i +1; 60 end 61 62 temp2 =0; // c l e a r s t e m p o r a r y v a r i a b l e temp2 63 64 for j =1: length ( p ) 65 // m u l t i p l i y i n g b i t s o f i n t e g e r p a r t w i t h t h e i r 66

p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( p ( j ) *10^( j -1) ) ; 16

67 end 68 69 temp4 = modulo ( temp3 ,1) ; 70 71 while ( temp4 ~=0) // s t o r i n g e a c h d e c i m a l

d i g i t in

72 73 74 75 76 end 77 78 temp5 =0; // c l e a r s t e m p o r a r y v a r i a b l e temp2 79 80 for j =1: length ( d ) 81 // m u l t i p l i y i n g b i t s o f d e c i m a l p a r t w i t h t h e i r

vector for convenience temp4 = temp4 *2; d ( x ) = floor ( temp4 ) ; x = x +1; temp4 = modulo ( temp4 ,1) ;

p o s i t i o n v a l u e s and a d d i n g
82 temp5 = temp5 +(10^( -1* j ) * d ( j ) ) 83 end 84 85 temp = temp2 + temp5 ; 86 // f i n a l l y a d d i n g b o t h t h e i n t e g e r and d e c i m a l p a r t s

to get t o t a l output . 87 disp ( temp ) ; // d i s p l a y i n g t h e o u t p u t check Appendix AP 9 for dependency: bin21dec.sci check Appendix AP 6 for dependency: dec21bin.sci

Scilab code Exa 1.7 Adds two binary numbers


1 clc ; 2 a =0111.10;

// f i r s t number 17

b =1111.01; // s e c o n d number A = bin21dec ( a ) ; // c o n v e r t i n g a i n t o d e c i m a l number B = bin21dec ( b ) ; // c o n v e r t i n g b i n t o d e c i m a l number S=A+B; // a d d i n g t h e two d e c i m a l numbers temp = dec21bin ( S ) ; // c o n v e r t i n g t h e d e c i m a l sum back to binary 8 format ( v ,10) ; // c h a n g i n g t h e d e f a u l t p r e c i s i o n to 8 9 disp ( temp ) ; // d i s p l a y i n g t h e f i n a l o u t p u t
3 4 5 6 7

check Appendix AP 9 for dependency: bin21dec.sci check Appendix AP 6 for dependency: dec21bin.sci

Scilab code Exa 1.8 Subtracts Two Binary numbers


1 clc ; 2 format ( v ,8) ; 3 4 5 6 7 8 9

// c h a n g i n g t h e d e f a u l t p r e c i s i o n t o

8 a =10010.11; // f i r s t number b =01100.10; // s e c o n d number A = bin21dec ( a ) ; // c o n v e r t i n g a i n t o d e c i m a l number B = bin21dec ( b ) ; // c o n v e r t i n g b i n t o d e c i m a l number S =A - B ; // s u b t r a c t i n g t h e two d e c i m a l numbers temp = dec21bin ( S ) ; // c o n v e r t i n g t h e d e c i m a l number back t o b i n a r y disp ( temp ) ; // d i s p l a y i n g t h e f i n a l o u t p u t check Appendix AP 9 for dependency: bin21dec.sci check Appendix AP 6 for dependency: 18

dec21bin.sci

Scilab code Exa 1.9 Multiplies two Binary numbers


1 clc ; 2 format ( v ,8) ; 3 4 5 6 7 8

// c h a n g i n g t h e d e f a u l t p r e c i s i o n t o

8 a =11001.1; // f i r s t number b =110.1; // s e c o n d number A = bin21dec ( a ) ; // c o n v e r t i n g a i n t o d e c i m a l number B = bin21dec ( b ) ; // c o n v e r t i n g b i n t o d e c i m a l number S=A*B; // m u l t i p l y t h e two d e c i m a l numbers temp = dec21bin ( S ) ; // c o n v e r t i n g t h e d e c i m a l p r o d u c t back t o b i n a r y 9 disp ( temp ) ; // d i s p l a y i n g t h e f i n a l o u t p u t check Appendix AP 9 for dependency: bin21dec.sci check Appendix AP 6 for dependency: dec21bin.sci

Scilab code Exa 1.10 Division of Two Binary Numbers


1 2 3 4 5 6 7 8

clc ; format ( v ,8) ; // c h a n g i n g t h e d e f a u l t p r e c i s i o n t o 8 a =1000100110; // f i r s t number b =11001; // s e c o n d number A = bin21dec ( a ) ; // c o n v e r t i n g a i n t o d e c i m a l number B = bin21dec ( b ) ; // c o n v e r t i n g b i n t o d e c i m a l number S=A/B; // m u l t i p l y t h e two d e c i m a l numbers temp = dec21bin ( S ) ; // c o n v e r t i n g t h e d e c i m a l p r o d u c t back t o b i n a r y 19

9 10

disp ( q u o t i e n t ) ; disp ( temp ) ; // d i s p l a y i n g t h e f i n a l o u t p u t

20

Chapter 2 Sets Relations and Lattices

Scilab code Exa 2.1 Set Of All even Numbers


1 clear ; 2 clc ; 3 // l o w e r=i n p u t ( i n p u t t h e l o w e r l i m i t o f t h e s e t ) ; 4 // u p p e r=i n p u t ( i n p u t t h e u p p e r l i m i t o f t h e s e t ) ; 5 lower =1; // l o w e r l i m i t o f t h e s e t 6 upper =10; // u p p e r l i m i t o f t h e s e t 7 temp = lower ; 8 h =1; i =1; 9 while ( temp <= upper ) 10 if ( modulo ( temp ,2) ==0) // c h e c k i n g w h e t h e r a 11 12 13 14 15 16 17 18 end ; 19 disp ( t h e s e t 20 disp ( e ) ;

number i s e v e n o r n o t e ( h ) = temp ; h = h +1; else o ( i ) = temp ; i = i +1; end temp = temp +1;

// odd number

o f e v e n number b e t w e e n t h e l i m i t s )

21

21 22

disp ( t h e s e t o f odd number b e t w e e n t h e l i m i t s ) disp ( o ) ;

Scilab code Exa 2.2 Subsets Of a Faces of a die


1 clear ; 2 clc ; 3 //N=i n p u t ( e n t e r t h e e l e m e n t s i n t h e s e t ) ; 4 // n =1; 5 // w h i l e ( n<=N) 6 // i ( n )=i n p u t ( e n t e r t h e e l e m e n t s o f f i r s t s e t ) ; 7 // n=n +1; 8 // end ; 9 N =6; 10 i (1) = f 1 ; i (2) = f 2 ; i (3) = f 3 ; i (4) = f 4 ; i (5) = f 5 ; i 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

(6) = f 6 ; disp ( n u l l s e t ) ; // s i n c e n u l l s e t i s s u b s e t o f any s e t . for a =1:1: N // s e t o f s i n g l e e l e m e n t s disp ( i ( a ) ) ; end c =1; for a =1:1: N -1 // s e t o f two e l e m e n t s for b = a :1: N if ( a ~= b ) m (c ,1) = i ( a ) ; m (c ,2) = i ( b ) ; c = c +1; end end ; end ; disp ( m ) ; d =1; for a =1:1: N // s e t o f t h r e e e l e m e n t s for b = a :1: N 22

29 for c = b :1: N 30 if (( a ~= b ) &( b ~= c ) &( c ~= a ) ) 31 p (d ,1) = i ( a ) ; 32 p (d ,2) = i ( b ) ; 33 p (d ,3) = i ( c ) ; 34 d = d +1; 35 end 36 end 37 end 38 end 39 disp ( p ) ; 40 e =1; 41 for a =1:1: N // s e t o f f o u r e l e m e n t s 42 for b = a :1: N 43 for c = b :1: N 44 for d = c :1: N 45 if (( a ~= b ) &( b ~= c ) &( c ~= d ) &( d ~= a ) &( b ~= d 46 47 48 49 50 51 52 end 53 end 54 end 55 end 56 disp ( q ) ; 57 f =1; 58 for a =1:1: N // s e t o f f i v e e l e m e n t s 59 for b = a :1: N 60 for c = b :1: N 61 for d = c :1: N 62 for e = d :1: N 63 if (( a ~= b ) &( b ~= c ) &( c ~= d ) &( d ~= e ) &(

) &( a ~= c ) ) q (e ,1) = i ( a ) ; q (e ,2) = i ( b ) ; q (e ,3) = i ( c ) ; q (e ,4) = i ( d ) e = e +1; end

e ~= a ) &( a ~= c ) &( a ~= d ) &( b ~= d ) &( c ~= e ) &( b ~= e ) ) 23

64 r (f ,1) = i ( a ) ; 65 r (f ,2) = i ( b ) ; 66 r (f ,3) = i ( c ) ; 67 r (f ,4) = i ( d ) ; 68 r (f ,5) = i ( e ) ; 69 f = f +1; 70 end 71 end 72 end 73 end 74 end 75 end 76 disp ( r ) ; 77 for a =1:1: N // s e t o f s i x e l e m e n t s 78 s (1 ,1) = i (1) ; 79 s (1 ,2) = i (2) ; 80 s (1 ,3) = i (3) ; 81 s (1 ,4) = i (4) ; 82 s (1 ,5) = i (5) ; 83 s (1 ,6) = i (6) ; 84 end 85 disp ( s ) ;

Scilab code Exa 2.4 Relation between two sets


1 2 3 4 5 6 7 8 9 10

clear ; clc ; N1 =2; N2 =3; // n =1; // w h i l e ( n<=N1 ) // i ( n )=i n p u t ( e n t e r t h e e l e m e n t s o f f i r s t // n=n +1; // end ; i =[ p q ]; // s e t A 24

s e t ) ;

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// n =1; // w h i l e ( n<=N2 ) // j ( n )=i n p u t ( e n t e r t h e e l e m e n t s o f s e c o n d s e t ) ; // n=n +1; // end ; j =[ r s t ]; // s e t B c =1; d =1; for a =1:1: N1 // r e a l t i o n b e t w e e n s e t s A and B for b =1:1: N2 m (c , d ) = i ( a ) ; m (c , d +1) = j ( b ) ; c = c +1; end ; end ; disp ( m ) ;

Scilab code Exa 2.5 Equivalence relation


1 clear ; 2 clc ; 3 //N=i n p u t ( e n t e r t h e no o f e l e m e n t s i n t h e s e t : ) ; 4 // f o r i = 1 : 1 :N 5 // s ( 1 , i )=i n p u t ( e n t e r t h e e l e m e n t s i n t h e s e t 6 7 8 9 10 11 12 13

:) ; // end ; // n=i n p u t ( e n t e r t h e number o f p a i r s i n t h e r e l a t i o n :) ; // f o r j = 1 : 1 : n // for k =1:1:2 // r ( j , k )=i n p u t ( e n t e r t h e e l e m e n t s i n t h e relation :) ; // end // end N =2; 25

14 s =[ a 15 n =3; 16 r =[ a 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

b ];

// e l e m e n t s i n t h e s e t

a ; b b ; a b ]; // r e a l t i o n b e t w e e n the e l e m e n t s i n the above s e t . ref = zeros (1 , N ) ; for a =1:1: N for b =1:1: n if ( r (b ,1) == s (1 , a ) & r (b ,2) == s (1 , a ) ) ref (1 , a ) =1; end end end for i =1:1: N // c h e c k i n g w h e t h e r a b o v e r e l a t i o n i s r e f l e x i v e or not if ( ref (1 , i ) ==1) disp ( t h e a b o v e r e l a t i o n i s r e f l e x i v e w i t h e l e m e n t s ); disp ( s (1 , i ) ) ; disp ( ) ; end end sym = zeros (1 ,( N *N -1) /2) ; s (1 , N +1) = s (1 ,1) ; for a =1:1: N // c h e c k i n g w h e t h e r a b o v e r e l a t i o n i s symmetric or not for b =1:1: n if ( r (b ,1) == s (1 , a ) & r (b ,2) == s (1 , a +1) ) for d =1:1: n if ( r (d ,1) == s (1 , a +1) & r (d ,2) == s (1 , a ) ) sym (1 , a ) =1; disp ( t h e a b o v e r e l a t i o n i s symmetric f o r t h e s e p a i r s : ); disp ( ) ,s (1 , a +1) ,s (1 , a ) , ( ) ; end end end end end 26

47 for a =1:1: n 48 49 50 51 52 53 54 55

// c h e c k i n g w h e t h e r i t

is

56 57 58 59 60 61 end

t r a n s t i v e or not . u = r (a ,1) ; v = r (a ,2) ; for b = a :1: n if ( r (b ,1) == v ) w = r (b ,2) ; for c = b :1: n if ( r (c ,1) == w & r (c ,2) == u ) disp ( s a t i s f i e s t r a n s t i t v e property ) ; abort ; end end end end

Scilab code Exa 2.6 Equivalence relation 2


1 2 3 4 5 6 7 8 9 10 11 12 13 14

clear ; clc ; //N=i n p u t ( e n t e r t h e e l e m e n t s i n t h e s e t : ) ; // f o r i = 1 : 1 :N // s ( i )=i n p u t ( e n t e r t h e e l e m e n t s : ) ; // end N =2; s =[ a b ]; disp ( The e q u i v a l e n c e r e l a t i o n o f a b o v e s e t i s : ) ; h =1; for i =1:1: N // t o s a t i s f y r e f l e x i v e p r o p e r t y ref (h ,1) = s ( i ) ; ref (h ,2) = s ( i ) ; h = h +1; 27

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

end for i =1:1: N // t o s a t i s f y s y m m e t r i c p r o p e r t y for j = i :1: N if ( i ~= j ) ref (h ,1) = s ( i ) ; ref (h ,2) = s ( j ) ; h = h +1; end end end m =1; for i =1: max ( size ( ref (: ,1) ) ) // t o s a t i s f y transtive property if ( ref (i ,1) ~= ref (i ,2) ) ref1 (m ,1) = ref (i ,2) ; ref1 (m ,2) = ref (i ,1) ; m = m +1; end end disp ( ref ) ; disp ( ref1 ) ;

Scilab code Exa 2.7 Function Check


1 clc ; 2 clear ; 3 // n1=i n p u t ( e n t e r t h e no o f e l e m e n t s i n t h e 1 s t 4 5 6 7 8 9

set

) ; // f o r i = 1 : 1 : n1 // s 1 ( i )=i n p u t ( e n t e r t h e e l e m e n t s o f 1 s t s e t : ) ; // end n1 =3; s1 =[ a1 a2 a3 ]; // s e t A // n2=i n p u t ( e n t e r t h e no o f e l e m e n t s i n t h e 2 nd s e t ) ; 28

10 // f o r i = 1 : 1 : n2 11 // s 2 ( i )=i n p u t ( e n t e r t h e e l e m e n t s o f 2 n s s e t : ) ; 12 // end 13 n2 =2; 14 s1 =[ b1 b2 ]; // s e t B 15 //N=i n p u t ( e n t e r t h e p a i r s i n t h e r e l a t i o n which you 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

want t o c h e c k w h e t h e r i s a f u n c t i o n ) ; // f o r i = 1 : 1 :N // for j =1:1:2 // r ( i , j )=i n p u t ( e n t e r t h e e l e m e n t s i n t h e relation :) ; // end // end N =3; r =[ a1 b1 ; a2 b2 ; a2 b1 ]; // R e a l t i o n r for i =1:1: N // c h e c k s w h e t h e r t h e r e l a t i o n i s f u n c t i o n or not for j = i :1: N if ( r (i ,1) == r (j ,1) & i ~= j ) disp ( t h e r e l a t i o n i s n o t a f u n c t i o n ) ; abort ; end end end disp ( t h e r e a l t i o n i s a f u c n t i o n ) ;

Scilab code Exa 2.11 Partial Ordering Divisibilty Relation


1 clear ; 2 clc ; 3 //N=i n p u t ( e n t e r t h e number f o r which

d i v i s i b i l t y )

; 4 N =45; 5 h =1;

// d i v i d e n t

29

6 for i =1: N // f i n d i n g 7 if ( modulo (N , i ) ==0) 8 r(h)=i; 9 h = h +1; 10 end 11 end 12 n = max ( size ( r ) ) ; 13 disp ( H a s s e Diagram )

a l l t h e d i v i s o r s o f 45

// d i s p l a y i n g i n t h e form

o f h a s s e diagram
14 disp ( ) 15 disp ( r ( n ) ) ; 16 h =2; 17 for i =n -1: -2:3 18 disp ( ) ; 19 disp ( r ( i ) ,r (i -1) ) ; 20 h = h +1; 21 end 22 disp ( ) 23 disp ( r (1) ) ;

Scilab code Exa 2.12 Ordering Relation


1 2 3 4 5 6 7 8 9 10 11 12 13

clear clc N =4; // d e f i n i n g a l l t h e p a r t i a l o r d e r e d s e t s s (1 ,:) =[1 0]; s (2 ,:) =[0 1]; s (3 ,:) =[0 0]; s (4 ,:) =[1 1]; // F i n d i n g a =1; b =1; for i =1:1: N // s o r t i n g b a s e d on t h e l e v e l for j = i :1: N if ( i ~= j ) 30

14 u = s (i ,1) + s (i ,2) ; 15 v = s (j ,1) + s (j ,2) ; 16 if (u < v ) 17 temp (1) = s (i ,1) ; 18 temp (2) = s (i ,2) ; 19 s (i ,1) = s (j ,1) ; 20 s (i ,2) = s (j ,2) ; 21 s (j ,1) = temp (1) ; 22 s (j ,2) = temp (2) ; 23 end 24 end 25 end 26 end 27 // d i s p l a y i n g i n t h e form o f h a s s e g r a p h form 28 disp ( 1 s t s t a g e o f H a s s e d i a g r a m ) ; 29 disp ( s (1 ,:) ) ; 30 disp ( 2 nd s t a g e o f H a s s e d i a g r a m ) ; 31 disp ( s (2 ,:) ) ; 32 disp ( s (3 ,:) ) ; 33 disp ( 3 r d s t a g e o f H a s s e d i a g r a m ) ; 34 disp ( s (4 ,:) ) ;

Scilab code Exa 2.15 Lattice Of subsets


1 clear 2 clc 3 N =3; 4 s =[ a 5 6 7 8 9 10

b c ]; // s e t f o r which l a t t i c e s h o u l d be d e f i n e d for i =2:2^3 s (i ,:) = s (1 ,:) ; end // d e f i n i n g 2 nd l e v e l v e r t i c e s o f t h e l a t t i c e for i =2:4 s (i ,i -1) = 0 ; 31

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

end // d e f i n i n g 3 r d l e v e l v e r t i c e s o f t h e l a t t i c e for i =5:6 s (i ,i -4) = 0 ; s (i ,i -3) = 0 ; end s (7 ,1) = 0 ; s (7 ,3) = 0 ; // d e f i n i n g t h e f i n a l l e v e l o f v e r t i c e s o f t h e lattice s (8 ,:) =[ 0 0 0 ]; disp ( 1 s t l e v e l ) ; disp ( s (1 ,:) ) ; disp ( 2 nd l e v e l ) ; disp ( s (2 ,:) ) ; disp ( s (3 ,:) ) ; disp ( s (4 ,:) ) ; disp ( 3 r d l e v e l ) ; disp ( s (5 ,:) ) ; disp ( s (6 ,:) ) ; disp ( s (7 ,:) ) ; disp ( 4 t h l e v e l ) ; disp ( s (8 ,:) ) ;

Scilab code Exa 2.16 glb and ulb


1 clear ; 2 clc ; 3 par1 =[ ab , c d e , f h , g i ]; 4 5 6 7 8 9

// i n i t i a l

partitions

par1 par2 =[ abc , de , f g , h i ]; // p a r=p a r 1+p a r 2 ; // lub l o w e r u p p e r bound par_lub =[ a b c d e , f g h i ]; disp ( par_lub ) ; // p a r=p a r 1 . p a r 2 32

// p a r t i t i o n 2

10 11 12

// g l b g r e a t e s t l o w e r bound par_glb =[ ab , c , de , f , g , h , i ]; disp ( par_glb ) ;

33

Chapter 3 Switching Algebra And Its Applications

Scilab code Exa 3.1 Simplify 1


1 clear 2 clc ; 3 disp ( T( x , y , z )=x y z+yz+xz ) ; 4 disp ( M i n i m i s e t h e g i v e n e x p r e s s i o n ) ; 5 disp ( S i n c e z i s common i n e v e r y term t a k i n g z 6 7 8 9 10 11 12 13

common ) ; disp ( T( x , y , z )=z ( x y+y+x ) ) ; disp ( From t h e p r o p e r t y a+a b=a+b ) ; disp ( T( x , y , z )=z ( x+y+x ) ) ; disp ( S i n c e we know t h a t a+a=1 ) ; disp ( T( x , y , z )=z (1+ y ) ) ; disp ( we know t h a t 1+a=1 ) ; disp ( T( x , y , z )=z . 1 ) ; disp ( T( x , y , z )=z ) ;

Scilab code Exa 3.2 Simplify 2 34

1 clear 2 clc 3 disp ( T( x , y , z ) =(x+y ) [ x ( y+z ) ]+ x y+x z ) ; 4 disp ( From t h e p r o p e r t i e s 1 . ( ab ) =a+b 2 . ( a+b ) =a 5 6 7 8 9 10 11 12 13 14 15 16

b ) ; disp ( T( x , y , z ) =(( x+y ) ( x+yz ) )+x y+x z ) ; disp ( M u l t i p l i y i n g t h e f i r s t 2 t e r m s ) ; disp ( T( x , y , z ) =(x+xyz+xy+yz )+x y+x z ) ; disp ( T( x , y , z ) =(x (1+ y+yz )+yz )+x y+x z ) ; disp ( T( x , y , z )=x+yz+x y+x z ) ; disp ( we know a+a b=a+b ) ; disp ( T( x , y , z )=x+y+yz+x z ) ; disp ( T( x , y , z )=x+z+y+yz ) ; disp ( T( x , y , z )=x+z+y+z ) ; disp ( s i n c e z+z =1 ) ; disp ( T( x , y , z )=x+1+y ) ; disp ( T( x , y , z ) =1 )

Scilab code Exa 3.3 Prove the identity


1 clear 2 clc ; 3 disp ( L . H . S = xy+x y+yz ) ; 4 disp ( R . H . S = xy+x y+x z ) ; 5 disp ( Based on c o n s e n s u s t h e o r e m ) 6 disp ( we can w r i t e x y+yz a s x y+yz+x z b c o z t h e 7 8 9 10 11 12 13

two e x p r e s s i o n s a r e e q u a l ) ; disp ( ( x y+yz+x z ( y+y ) )=x y+yz+x yz+x y z ) ; disp ( x y+yz+x yz+x y z=x y (1+ z )+yz (1+ x ) ) ; disp ( x y+yz=x y+yz+x z ) ; disp ( s o L . H . S=xy+x y+yz=xy+x y+yz+x z ) ; disp ( I n t h e s i m i l a r way xy+yz+x z can be s i m p l i f i e d a s xy+x z ) ; disp ( s o L . H . S becomes xy+x z+x y ) ; disp ( t h u s L . H . S= R . H . S ) ; 35

14

disp ( h e n c e p r o v e d )

Scilab code Exa 3.4 Determine The Output of expression clear ; clc ; // f u n c t i o n d e f i n i t i o n x =[0;0;0;0;1;1;1;1]; y =[0;0;1;1;0;0;1;1]; z =[0;1;0;1;0;1;0;1]; f =[1;0;1;1;0;0;1;1]; g =[0;1;0;1;1;0;1;0]; // c a l c u l a t i n g t h e v a l u e s o f e x p r e s s i o n s g i v e n forg = bitor (f , g ) ; fandg = bitand (f , g ) ; fcmp = bitcmp (f ,1) ; s (: ,1) = x ; s (: ,2) = y ; s (: ,3) = z ; s (: ,4) = f ; s (: ,5) = g ; s (: ,6) = forg ; s (: ,7) = fandg ; s (: ,8) = fcmp ; p =[ x , y , z , f , g , f+g , , f ]; 22 disp ( p ) ; 23 disp ( s ) ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

fg

Scilab code Exa 3.5 Simplify 3


1 clear ; 2 clc ;

36

3 4 5 6 7 8 9 10 11

disp ( T(A, B , C , D)=AC+ABD+BCD+ABD+ABCD ) ; disp ( Assume A=x , C=y , BD=z ) ; disp ( Now from c o n s e n s u s t h e o r e m f o r t h e f i r s t t h r e e terms ); disp ( BCD i s t h e r e d u n d a n t term s o i t can be removed ) ; disp ( T(A, B , C , D)=AC+ABD+ABD+ABCD ) ; disp ( T(A, B , C , D)=AC+ABD+AD (B+BC) ) ; disp ( we know t h a t a+a b=a+b ) ; disp ( T(A, B , C , D)=AC+ABD+AD (B+C) ) ; disp ( T(A, B , C , D)=AC+A(BD+D (B+C) ) ) ;

Scilab code Exa 3.6 Simplify 4


1 clear 2 clc 3 disp ( T(A, B , C , D)=AB+ABD+ABCD+BC ) ; 4 disp ( T(A, B , C , D)=B(A+AD)+C(ADB+B) ) ; 5 disp ( T(A, B , C , D)=B(A+D)+C(AD+B) ) ; 6 disp ( T(A, B , C , D)=AB+BD+ACD+BC ) ; 7 disp ( T(A, B , C , D)=AB+BD+ACD+BC(A+A ) ) ; 8 disp ( T(A, B , C , D)=AB+ABC+ABC+BD+ACD ) ; 9 disp ( T(A, B , C , D)=AB(1+C)+ABC+BD+ACD ) ; 10 disp ( T(A, B , C , D)=AB+ABC+BD+ACD ) ; 11 disp ( Now a p p l y c o n s e n s u s t h e o r e m f o r 2 nd 3 r d and

4 t h t e r m s ) ; 12 disp ( l e t x=D, y=B , z=AC ) ; 13 disp ( T(A, B , C , D)=AB+BD+ACD ) ;

Scilab code Exa 3.7 Expand the Expression


1 clear ; 2 clc ;

37

disp ( T( x , y , z )=x y+z+xyz ) ; disp ( To d e t e r m i n e t h e c a n o n i c a l sum o f p r o d u c t s we have t o c h e c k f o r a p r o d u c t which i s n o t a min term and t h e n m u l t i p l y w i t h t h e m i s s i n g v a r i a b l e s u c h t h a t t h e e x p r e s s i o n v a l u e d o e s n t c h a n g e ) ; 5 disp ( T( x , y , z )=x y ( z+z ) +(x+x ) ( y+y ) z+xyz ) ; 6 disp ( T( x , y , z )=x yz+x yz+xyz+xy z+x yz+x y z+ xyz ) ; 7 disp ( T( x , y , z )=x yz+x yz+xyz+xy z+x y z+xyz ) ;
3 4

Scilab code Exa 3.8 Expand the Expression 2


1 clear ; 2 clc ; 3 disp ( T( x , y , z )=x ( y+z ) ) ; 4 disp ( To d e t e r m i n e t h e c a n o n i c a l p r o d u c t o f sums

5 6 7 8 9 10

form you n e e d t o c h e c k f o r a p r o d u c t which i s n o t a max term and t h e n add i t w i t h t h e m i s s i n g terms such that the e x p r e s s i o n value i s not a l t e r e d ) ; disp ( T( x , y , z ) =(x+yy+ z z ) ( xx+y+z ) ) ; disp ( a+b+c c can be w r i t t e n a s p r o d u c t o f 2 max t e r m s ( a+b+c ) ( a+b+c ) ) ; disp ( a+bb+ c c can be w r i t t e n a s ( a+b+c ) ( a+b+c ) ( a+ b+c ) ( a+b+c ) ) ; disp ( from t h e a b o v e two p r o p e r t i e s we can w r i t e t h e T( x , y , z ) a s ) ; disp ( T( x , y , z ) =(x+y+z ) ( x+y+z ) ( x+y+z ) ( x+y+z ) ( x+y+z ) ( x+y+z ) ) ; disp ( T( x , y , z ) =(x+y+z ) ( x+y+z ) ( x+y+z ) ( x+y+z ) ( x+y+z ) ) ;

38

Scilab code Exa 3.9 POS


1 clear ; 2 clc ; 3 disp ( T( x , y , z )=x y z+x y z+x yz+xyz+xy z+xy z ) ; 4 disp ( t h e complement T c o n s i s t s o f t h o s e m i n t e r m s

which a r e n o t c o n t a i n e d i n t h e e x p r e s s i o n f o r T ) ; 5 disp ( T=[ x yz+xyz ] ) ; 6 disp ( ( x+y+z ) ( x+y+z ) ) ;

Scilab code Exa 3.10 Tabulate the Function of 2 variables


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

clear ; clc ; a1 =0; a2 = a1 ; a3 = a1 ; // a l l c o m b i n a t i o n s o f 2 v a r i b l e i n p u t s f (: ,1) =[0;0;1;1]; f (: ,2) =[0;1;0;1]; disp ( The t u r t h t a b l e o f f f o r a l l t h e c o m b i n a t i o n s o f a0 , a1 , a2 , a3 a r e shown b e l o w ) // d e t e r m i n i n g t h e v a l u e s o f f f o r a l l c o m b i n a t i o n s o f a0 , a1 , a2 , a3 for a3 =0:1 for a2 =0:1 for a1 =0:1 for a0 =0:1 disp ([ a3 a2 a1 a0 ]) ; disp ([ a3 a2 a1 a0 ]) ; i =1; for x =0:1 for y =0:1 f0 = bitand ( a0 , bitand ( bitcmp (x ,1) , bitcmp (y ,1) ) ) ; 39

19 20 21 22 23 24 25 26 27 28 29 30 31 end 32 end 33 end 34 end

f1 = bitand ( a1 , bitand ( bitcmp (x ,1) ,y ) ) ; f2 = bitand ( a2 , bitand (x , bitcmp (y ,1) ) ) ; f3 = bitand ( a3 , bitand (x , y ) ) ; f4 = bitor ( f0 , f1 ) ; f5 = bitor ( f2 , f3 ) ; f (i ,3) = bitor ( f4 , f5 ) ; i = i +1; end end disp ([ x y f ]) ; disp ( f ) ; disp ( ) ;

Scilab code Exa 3.11 NOR


1 clear ; 2 clc ; 3 disp ( x NOR x=(x+x ) ) ; 4 disp ( NOT Gate ) ; 5 disp ( x NOR x=x x=x ) ; 6 disp ( OR Gate ) ; 7 disp ( ( x NOR y ) NOR ( x NOR y ) = ( x NOR y ) = x+y ) 8 9 10 11

; disp ( AND Gate ) ; disp ( ( x NOR x ) NOR ( y NOR y )= x NOR y = xy ) ; disp ( NAND Gate ) ; disp ( NOT ( ( x NOR x ) NOR ( y NOR y ) )= NOT ( x NOR y ) = NOT( xy ) = ( xy ) ) ; 12 disp ( XOR Gate ) ; 40

13 14 15 16

disp ( ( x NOR y ) NOR ( x NOR y ) = x y+xy ) ; disp ( XNOR Gate ) ; disp ( ( x NOR y ) NOR ( x NOR y ) = xy+x y ) ; disp ( S i n c e e v e r y o t h e r g a t e can be i m p l e m e n t e d u s i n g NOR g a t e i t i s s a i d t o be f u n c t i o n a l l y complete )

Scilab code Exa 3.12 Transmission function


1 clear ; 2 clc ; 3 disp ( T=xy +( x+y ) z ) ; 4 disp ( from t h e i d e n t i t y a+a b=a+b ) ; 5 disp ( T=xy+z ) ;

Scilab code Exa 3.13 Air Conditioning System


1 clear ; 2 clc ; 3 disp ( A i r c o n d i t i o n i n g s y s t e m o f a s t o r a g e w a r e h o u s e

4 5 6 7 8 9 10 11 12 13

w i l l be t u r n e d on i f and o n l y i f i t s a t i s f i e s t h e s e c o n d i t i o n s ); disp ( l e t W d e n o t e s w e i g h t o f 100 t o n s o r more ) ; disp ( H d e n o t e s r e l a t i v e h u m i d i t y o f a t l e a s t 60 p e r c e n t ); disp ( T d e n o t e s t e m p a r a t u r e a b o v e 60 d e g r e e s ) ; disp ( P d e n o t e s b a r o m e t r i c p r e s s u r e o f 30 o r more ) ; disp ( f i r s t c o n d i t i o n ) ; disp ( W < 100 t o n s => W ,H>=60 => H , T>60 ) ; disp ( A1= WHT ) ; disp ( s e c o n d c o n d i t i o n ) ; disp ( W > 100 t o n s => W , T>60 => T ) ; disp ( A2= WT ) ; 41

14 15 16 17

18 19 20 21 22

disp ( t h i r d c o n d i t i o n ) ; disp ( W < 100 t o n s => W ,P>30 => P ) ; disp ( A3= WP ) ; disp ( s i n c e A i r c o n d t i o n i n g s y s t e m s h o u l d be a c t i v a t e d i f any one o f t h e a b o v e i s s a t i s f i e d s o ); disp ( A=A1+A2+A3 ) ; disp ( A= WHT+ WT + WP ) ; disp ( A=T(WH+ W)+ WP ) ; disp ( A=T(W +H)+ WP ) ; disp ( Thus a c o m b i n a t i o n a l s y s t e m w i t h a b o v e e x p r e s s i o n makes t h e a i r c o n d i t i o n i n g s y s t e m on when r e q u i r e d )

Scilab code Exa 3.14 DeMorgans Law


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

clear ; clc ; i =1; // a l l c o m b i n a t i o n s o f 2 v a r i a b l e i n p u t s f (: ,1) =[0;0;1;1]; f (: ,2) =[0;1;0;1]; // v e r i f y i n g D morgan f i r s t law for a =0:1 for b =0:1 f (i ,3) = bitcmp ( bitor (a , b ) ,1) ; f (i ,4) = bitand ( bitcmp (a ,1) , bitcmp (b ,1) ) ; i = i +1; end end disp ( a b ( a+b ) ab); disp ( f ) ; disp ( T h e r e f o r e ( a+b ) =a b ) ; // v e r f y i n g D morgan 2 nd law i =1; 42

20 for a =0:1 21 for b =0:1 22 f (i ,3) = bitcmp ( bitand (a , b ) ,1) ; 23 f (i ,4) = bitor ( bitcmp (a ,1) , bitcmp (b ,1) ) ; 24 i = i +1; 25 end 26 end 27 disp ( a b ( ab ) a+b ) ; 28 disp ( f ) ; 29 // p r o v i n g D morgans l a w s t h e o r i t i c a l l y 30 disp ( ( a+b ) =a . b ) ; 31 disp ( ( a . b ) =a+b ) ; 32 disp ( we have show t h a t ( a+b ) ( a+b ) =0 and ( a+b )+a . b 33 34 35

=1 ) ; disp ( ( a+b ) a b=aa b+ba b=0+a bb=0+0=0 ) ; disp ( ( a+b )+a b=a+b+a b=a+b+a=b+a+a=b+1=1 ) ; disp ( T h i s p r o v e s t h e f i r s t Dmorgan law and i n t h e s i m i l a r way 2 nd law can a l s o be p r o v e d ) ;

43

Chapter 4 Minimization Of Switching Functions


check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.1 Irredundant expressions


1 2 3 4 5 6 7 8 9 10 11 12 13 14

clc ; n =4; // f o u r v a r i a b l e kmap k =[1 1 0 1; 0 1 1 1; 0 1 1 0; 0 0 0 0]; k (: ,: ,2) = zeros (n , n ) ; // t e m p o r a r y m a t r i x t o know whether a element i s p a i r e d or not // d e c l a r i n g n o t a t i o n s t o d i s p l a y o u t p u t var =[ y z w x ]; p1 =[ y z y z yz yz ]; p2 =[ w x ; w x ; wx ; wx ]; // minimum r e d u n d a n t e l e m e n t s a c c e p t e d w h i l e p a i r i n g cmn4 =4; cmn2 =2; 44

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

temp =1; disp ( The m i n i m a l e c p r e s s i o n o f t h e g i v e n Kmap ) ; disp ( k (: ,: ,1) ) ; disp ( i s : ) ; disp ( ) // 16 c e l l s for i =1: n for j =1: n if ( k (i , j ) ~=1) temp =0; break ; end end end printf ( f= ) ; if ( temp ==1) printf ( 1 ) ; abort ; end // 8 c e l l s z1 = ones (2 ,4) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for i =1: n if ( i ==4) t =1; else t = i +1; end z =[ k (i ,: ,1) ; k (t ,: ,1) ]; if ( z == z1 ) k (i ,: ,2) =[1 1 1 1]; k (t ,: ,2) =[1 1 1 1]; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 (t ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) 45

53 printf ( %s , var ( in ) ) ; 54 printf ( + ) ; 55 break ; 56 else 57 if ( c ( in ) ==0 & a ( in ) == 1 ) 58 printf ( var ( in ) ) ; 59 printf ( + ) ; 60 break ; 61 end 62 end 63 end 64 end 65 end 66 z2 = ones (4 ,2) ; 67 for j =1: n 68 if ( j ==4) 69 t =1; 70 else 71 t = j +1; 72 end 73 z =[ k (: ,j ,1) k (: ,t ,1) ]; 74 if ( z == z2 ) 75 k (: ,j ,2) =[1;1;1;1]; 76 k (: ,t ,2) =[1;1;1;1]; 77 a = strsplit ( temp1 (1 , j ) ) ; 78 b = strsplit ( temp1 (1 , t ) ) ; 79 c = strcmp (a , b ) ; 80 for in =1: max ( size ( c ) ) 81 if ( c ( in ) ==0 & a ( in ) == 0 ) 82 printf ( %s , var (2+ in ) ) ; 83 printf ( + ) ; 84 break ; 85 else 86 if ( c ( in ) ==0 & a ( in ) == 1 ) 87 printf ( var (2+ in ) ) ; 88 printf ( + ) ; 89 break ; 90 end

46

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

end end end end // 4 c e l l s z1 = ones (1 ,4) ; z2 = ones (4 ,1) ; z3 = ones (2 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for t =1: n z = k (t ,: ,1) ; no = number_of ( k (t ,: ,2) ,1) ; if ( z == z1 & no < cmn4 ) k (t ,: ,2) = z1 ; a = strsplit ( temp1 (1 , t ) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( a ( in ) == 1 ) printf ( var ( in ) ) ; end end printf ( + ) ; end end for t =1: n z = k (: ,t ,1) ; no = number_of ( k (: ,t ,2) ,1) ; if ( z == z2 & no < cmn4 ) k (: ,t ,2) = z2 ; a = strsplit ( temp2 (t ,1) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( a ( in ) == 1 ) 47

129 printf ( var (2+ in ) ) ; 130 end 131 end 132 printf ( + ) ; 133 end 134 end 135 for i =1: n 136 for j =1: n 137 if ( i == n ) 138 t1 =1; 139 else 140 t1 = i +1; 141 end 142 if ( j == n ) 143 t2 =1; 144 else 145 t2 = j +1; 146 end 147 z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2 ,1) 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

]; z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2 ,2) ]; no = number_of ( z5 ,1) ; if ( z4 == z3 & no < cmn4 ) k (i ,j ,2) =1; k (i , t2 ,2) =1; k ( t1 ,j ,2) =1; k ( t1 , t2 ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end 48

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

end a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end printf ( + ) ; end end end // 2 c e l l s z6 =[1 1]; z7 = z6 ; for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z8 =[ k (i ,j ,1) k (i , t2 ,1) ]; z9 =[ k (i ,j ,2) k (i , t2 ,2) ]; no1 = number_of ( z9 ,1) ; if ( z8 == z6 & no1 < cmn2 & i + j ~=2) k (i ,j ,2) =1; k (i , t2 ,2) =1; a = strsplit ( temp1 (1 , j ) ) ; 49

203 b = strsplit ( temp1 (1 , t2 ) ) ; 204 c = strcmp (a , b ) ; 205 for in =1: max ( size ( c ) ) 206 if ( c ( in ) ==0 & a ( in ) == 0 ) 207 printf ( p1 (1 , i ) ) ; 208 printf ( %s , var (2+ in ) ) ; 209 printf ( + ) ; 210 end 211 if ( c ( in ) ==0 & a ( in ) == 1 ) 212 printf ( p1 (1 , i ) ) ; 213 printf ( var (2+ in ) ) ; 214 printf ( + ) ; 215 end 216 end 217 end 218 end 219 end 220 for i =1: n 221 for j =1: n 222 if ( i == n ) 223 t1 =1; 224 else 225 t1 = i +1; 226 end 227 if ( j == n ) 228 t2 =1; 229 else 230 t2 = j +1; 231 end 232 z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ]; 233 z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ]; 234 no2 = number_of ( z11 ,1) ; 235 if ( z10 == z7 & no2 < cmn2 ) 236 k (i ,j ,2) =1; 237 k ( t1 ,j ,2) =1; 238 a = strsplit ( temp2 (i ,1) ) ; 239 b = strsplit ( temp2 ( t1 ,1) ) ; 240 c = strcmp (a , b ) ;

50

241 for in =1: max ( size ( c ) ) 242 if ( c ( in ) ==0 & a ( in ) == 0 ) 243 printf ( p2 (j ,1) ) ; 244 printf ( %s , var ( in ) ) ; 245 printf ( + ) ; 246 end 247 if ( c ( in ) ==0 & a ( in ) == 1 ) 248 printf ( p2 (j ,1) ) ; 249 printf ( var ( in ) ) ; 250 printf ( + ) ; 251 end 252 end 253 end 254 end 255 end 256 // s i n g l e c e l l 257 for i =1: n 258 for j =1: n 259 if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1) 260 a = strsplit ( temp1 (1 , j ) ) ; 261 b = strsplit ( temp2 (i ,1) ) ; 262 for in =1: max ( size ( a (: ,1) ) ) 263 if ( a ( in ,1) == 1 ) 264 printf ( var ( in +2) ) ; 265 else 266 if ( a ( in ,1) == 0 ) 267 printf ( %s , var (2+ in ) ) ; 268 end 269 end 270 end 271 for in =1: max ( size ( b (: ,1) ) ) 272 if ( b ( in ,1) == 1 ) 273 printf ( var ( in ) ) ; 274 else 275 if ( b ( in ,1) == 0 ) 276 printf ( %s , var ( in ) ) ; 277 end 278 end

51

279 end 280 if ( i ~=4 & j ~=4) 281 printf ( + ) ; 282 end 283 end 284 end 285 end 286 printf ( 0 ) ;

check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.2 Irredundant expressions 2


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

clc ; n =4; // f o u r v a r i a b l e kmap k =[1 1 0 1; 0 1 1 1; 0 1 1 0; 0 0 0 0]; k (: ,: ,2) = zeros (n , n ) ; // t e m p o r a r y m a t r i x t o know whether a element i s p a i r e d or not // d e c l a r i n g n o t a t i o n s t o d i s p l a y o u t p u t var =[ y z w x ]; p1 =[ y z y z yz yz ]; p2 =[ w x ; w x ; wx ; wx ]; // minimum r e d u n d a n t e l e m e n t s a c c e p t e d w h i l e p a i r i n g cmn4 =1; cmn2 =1; temp =1; disp ( The m i n i m a l e c p r e s s i o n o f t h e g i v e n Kmap ) ; disp ( k (: ,: ,1) ) ; disp ( i s : ) ; disp ( ) // 16 c e l l s 52

21 22 23 24 25 26 27 28 29 30 31 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

for i =1: n for j =1: n if ( k (i , j ) ~=1) temp =0; break ; end end end printf ( f= ) ; if ( temp ==1) printf ( 1 ) ; abort ; end // 8 c e l l s z1 = ones (2 ,4) ; z2 = ones (4 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for i =1: n if ( i ==4) t =1; else t = i +1; end z =[ k (i ,: ,1) ; k (t ,: ,1) ]; if ( z == z1 ) k (i ,: ,2) =[1 1 1 1]; k (t ,: ,2) =[1 1 1 1]; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 (t ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; printf ( + ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) 53

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

printf ( var ( in ) ) ; printf ( + ) ; break ; end end end end end for j =1: n if ( j ==4) t =1; else t = j +1; end z =[ k (: ,j ,1) k (: ,t ,1) ]; if ( z == z2 ) k (: ,j ,2) =[1;1;1;1]; k (: ,t ,2) =[1;1;1;1]; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; printf ( + ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; printf ( + ) ; break ; end end end end end // 4 c e l l s z1 = ones (1 ,4) ; 54

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

z2 = ones (4 ,1) ; z3 = ones (2 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for t =1: n z = k (t ,: ,1) ; no = number_of ( k (t ,: ,2) ,1) ; if ( z == z1 & no < cmn4 ) k (t ,: ,2) = z1 ; a = strsplit ( temp1 (1 , t ) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( a ( in ) == 1 ) printf ( var ( in ) ) ; end end printf ( + ) ; end end for t =1: n z = k (: ,t ,1) ; no = number_of ( k (: ,t ,2) ,1) ; if ( z == z2 & no < cmn4 ) k (: ,t ,2) = z2 ; a = strsplit ( temp2 (t ,1) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end printf ( + ) ; end end 55

135 for i =1: n 136 for j =1: n 137 if ( i == n ) 138 t1 =1; 139 else 140 t1 = i +1; 141 end 142 if ( j == n ) 143 t2 =1; 144 else 145 t2 = j +1; 146 end 147 z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2 ,1) 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

]; z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2 ,2) ]; no = number_of ( z5 ,1) ; if ( z4 == z3 & no < cmn4 ) k (i ,j ,2) =1; k (i , t2 ,2) =1; k ( t1 ,j ,2) =1; k ( t1 , t2 ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end end a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) 56

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

printf ( %s , var (2+ in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end printf ( + ) ; end end end // 2 c e l l s z6 =[1 1]; z7 = z6 ; for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z8 =[ k (i ,j ,1) k (i , t2 ,1) ]; z9 =[ k (i ,j ,2) k (i , t2 ,2) ]; no1 = number_of ( z9 ,1) ; if ( z8 == z6 & no1 < cmn2 ) k (i ,j ,2) =1; k (i , t2 ,2) =1; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p1 (1 , i ) ) ; printf ( %s , var (2+ in ) ) ; 57

209 printf ( + ) ; 210 end 211 if ( c ( in ) ==0 & a ( in ) == 1 ) 212 printf ( p1 (1 , i ) ) ; 213 printf ( var (2+ in ) ) ; 214 printf ( + ) ; 215 end 216 end 217 end 218 end 219 end 220 for i =1: n 221 for j =1: n 222 if ( i == n ) 223 t1 =1; 224 else 225 t1 = i +1; 226 end 227 if ( j == n ) 228 t2 =1; 229 else 230 t2 = j +1; 231 end 232 z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ]; 233 z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ]; 234 no2 = number_of ( z11 ,1) ; 235 if ( z10 == z7 & no2 < cmn2 ) 236 k (i ,j ,2) =1; 237 k ( t1 ,j ,2) =1; 238 a = strsplit ( temp2 (i ,1) ) ; 239 b = strsplit ( temp2 ( t1 ,1) ) ; 240 c = strcmp (a , b ) ; 241 for in =1: max ( size ( c ) ) 242 if ( c ( in ) ==0 & a ( in ) == 0 ) 243 printf ( p2 (j ,1) ) ; 244 printf ( %s , var ( in ) ) ; 245 printf ( + ) ; 246 end

58

247 if ( c ( in ) ==0 & a ( in ) == 1 ) 248 printf ( p2 (j ,1) ) ; 249 printf ( var ( in ) ) ; 250 printf ( + ) ; 251 end 252 end 253 end 254 end 255 end 256 // s i n g l e c e l l 257 for i =1: n 258 for j =1: n 259 if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1) 260 a = strsplit ( temp1 (1 , j ) ) ; 261 b = strsplit ( temp2 (i ,1) ) ; 262 for in =1: max ( size ( a (: ,1) ) ) 263 if ( a ( in ,1) == 1 ) 264 printf ( var ( in +2) ) ; 265 else 266 if ( a ( in ,1) == 0 ) 267 printf ( %s , var (2+ in ) ) ; 268 end 269 end 270 end 271 for in =1: max ( size ( b (: ,1) ) ) 272 if ( b ( in ,1) == 1 ) 273 printf ( var ( in ) ) ; 274 else 275 if ( b ( in ,1) == 0 ) 276 printf ( %s , var ( in ) ) ; 277 end 278 end 279 end 280 printf ( + ) ; 281 end 282 end 283 end 284 printf ( 0 ) ;

59

check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.3 Reduce Expression


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

clc ; n =4; // f o u r v a r i a b l e kmap k =[0 0 1 0; 1 1 1 0; 0 1 1 1; 0 1 0 0]; k (: ,: ,2) = zeros (n , n ) ; // t e m p o r a r y m a t r i x t o know whether a element i s p a i r e d or not // d e c l a r i n g n o t a t i o n s t o d i s p l a y o u t p u t var =[ y z w x ]; p1 =[ y z y z yz yz ]; p2 =[ w x ; w x ; wx ; wx ]; // minimum r e d u n d a n t e l e m e n t s a c c e p t e d w h i l e p a i r i n g cmn4 =1; cmn2 =1; temp =1; disp ( The m i n i m a l e c p r e s s i o n o f t h e g i v e n Kmap ) ; disp ( k (: ,: ,1) ) ; disp ( i s : ) ; disp ( ) // 16 c e l l s for i =1: n for j =1: n if ( k (i , j ) ~=1) temp =0; break ; end end end 60

29 30 31 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

printf ( f= ) ; if ( temp ==1) printf ( 1 ) ; abort ; end // 8 c e l l s z1 = ones (2 ,4) ; z2 = ones (4 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for i =1: n if ( i ==4) t =1; else t = i +1; end z =[ k (i ,: ,1) ; k (t ,: ,1) ]; if ( z == z1 ) k (i ,: ,2) =[1 1 1 1]; k (t ,: ,2) =[1 1 1 1]; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 (t ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; printf ( + ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; printf ( + ) ; break ; end end end end end 61

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

for j =1: n if ( j ==4) t =1; else t = j +1; end z =[ k (: ,j ,1) k (: ,t ,1) ]; if ( z == z2 ) k (: ,j ,2) =[1;1;1;1]; k (: ,t ,2) =[1;1;1;1]; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; printf ( + ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; printf ( + ) ; break ; end end end end end // 2 c e l l s z6 =[1 1]; z7 = z6 ; for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end 62

105 if ( j == n ) 106 t2 =1; 107 else 108 t2 = j +1; 109 end 110 z8 =[ k (i ,j ,1) k (i , t2 ,1) ]; 111 z9 =[ k (i ,j ,2) k (i , t2 ,2) ]; 112 no1 = number_of ( z9 ,1) ; 113 if ( z8 == z6 & no1 < cmn2 ) 114 if ( i ==3 & i + j ==5) 115 ; 116 else 117 k (i ,j ,2) =1; 118 k (i , t2 ,2) =1; 119 a = strsplit ( temp1 (1 , j ) ) ; 120 b = strsplit ( temp1 (1 , t2 ) ) ; 121 c = strcmp (a , b ) ; 122 for in =1: max ( size ( c ) ) 123 if ( c ( in ) ==0 & a ( in ) == 0 ) 124 printf ( p1 (1 , i ) ) ; 125 printf ( %s , var (2+ in ) ) ; 126 printf ( + ) ; 127 end 128 if ( c ( in ) ==0 & a ( in ) == 1 ) 129 printf ( p1 (1 , i ) ) ; 130 printf ( var (2+ in ) ) ; 131 printf ( + ) ; 132 end 133 end 134 end 135 end 136 end 137 end 138 for i =1: n 139 for j =1: n 140 if ( i == n ) 141 t1 =1; 142 else

63

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ]; z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ]; no2 = number_of ( z11 ,1) ; if ( z10 == z7 & no2 < cmn2 ) k (i ,j ,2) =1; k ( t1 ,j ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p2 (j ,1) ) ; printf ( %s , var ( in ) ) ; printf ( + ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( p2 (j ,1) ) ; printf ( var ( in ) ) ; printf ( + ) ; end end end end end // 4 c e l l s z1 = ones (1 ,4) ; z2 = ones (4 ,1) ; z3 = ones (2 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for t =1: n 64

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

z = k (t ,: ,1) ; no = number_of ( k (t ,: ,2) ,1) ; if ( z == z1 & no < cmn4 ) k (t ,: ,2) = z1 ; a = strsplit ( temp1 (1 , t ) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( a ( in ) == 1 ) printf ( var ( in ) ) ; end end printf ( + ) ; end end for t =1: n z = k (: ,t ,1) ; no = number_of ( k (: ,t ,2) ,1) ; if ( z == z2 & no < cmn4 ) k (: ,t ,2) = z2 ; a = strsplit ( temp2 (t ,1) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end printf ( + ) ; end end for i =1: n for j =1: n if ( i == n ) t1 =1; else 65

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2 ,1) ]; z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2 ,2) ]; no = number_of ( z5 ,1) ; if ( z4 == z3 & no < cmn4 ) k (i ,j ,2) =1; k (i , t2 ,2) =1; k ( t1 ,j ,2) =1; k ( t1 , t2 ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end end a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end 66

255 end 256 printf ( + ) ; 257 end 258 end 259 end 260 // s i n g l e c e l l 261 for i =1: n 262 for j =1: n 263 if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1) 264 a = strsplit ( temp1 (1 , j ) ) ; 265 b = strsplit ( temp2 (i ,1) ) ; 266 for in =1: max ( size ( a (: ,1) ) ) 267 if ( a ( in ,1) == 1 ) 268 printf ( var ( in +2) ) ; 269 else 270 if ( a ( in ,1) == 0 ) 271 printf ( %s , var (2+ in ) ) ; 272 end 273 end 274 end 275 for in =1: max ( size ( b (: ,1) ) ) 276 if ( b ( in ,1) == 1 ) 277 printf ( var ( in ) ) ; 278 else 279 if ( b ( in ,1) == 0 ) 280 printf ( %s , var ( in ) ) ; 281 end 282 end 283 end 284 printf ( + ) ; 285 end 286 end 287 end 288 printf ( 0 ) ;

check Appendix AP 6 for dependency: dec21bin.sci 67

check Appendix AP 7 for dependency: donkmap.sci check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.4 BCD to Excess 3 Convertor


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

19 20 21 22 23

clc ; n =10; // c o v e r t i n g d e c i m a l numbers i n t o e x c e s s 3 v a l u e s for i =0: n -1 c ( i +1 ,1) = dec21bin ( i +3) ; end a=c; b = zeros (10 ,4) ; // p l a c i n g e x c e s s 3 o u t p u t s i n m a t r i x f o r c o n v e n i e n c e for i =1: n j =4; while ( a (i ,1) >=1) b (i , j ) = round ( modulo ( a (i ,1) ,10) ) ; a (i ,1) = a (i ,1) /10; j =j -1; end end // d o n t c a r e i s r e p r e s e n t e d by a 2 s i n c e s c i l a b d o e s n t a l l o w a m a t r i x t o c o n t a i n s t r i n g and a number . for i = n +1:16 b (i ,:) =[2 2 2 2]; end //map o f e a c h o u t p u t v a r i a b l e z =[ b (1 ,1) b (5 ,1) b (13 ,1) b (9 ,1) ; b (2 ,1) b (6 ,1) b (14 ,1) b (10 ,1) ; 68

24 25 26 27 28 29 30 31 32 33 34

b (3 ,1) b (7 ,1) b (15 ,1) (16 ,1) b (12 ,1) ]; y =[ b (1 ,2) b (5 ,2) b (13 ,2) (14 ,2) b (10 ,2) ; b (3 ,2) b (7 ,2) b (15 ,2) (16 ,2) b (12 ,2) ]; w =[ b (1 ,3) b (5 ,3) b (13 ,3) (14 ,3) b (10 ,3) ; b (3 ,3) b (7 ,3) b (15 ,3) (16 ,3) b (12 ,3) ]; x =[ b (1 ,4) b (5 ,4) b (13 ,4) (14 ,4) b (10 ,4) ; b (3 ,4) b (7 ,4) b (15 ,4) (16 ,4) b (12 ,4) ]; donkmap (w ,1) ; donkmap (x ,2) ; donkmap (y ,3) ; donkmap (z ,4) ;

b (11 ,1) ; b (4 ,1) b (8 ,1) b b (9 ,2) ; b (2 ,2) b (6 ,2) b b (11 ,2) ; b (4 ,2) b (8 ,2) b b (9 ,3) ; b (2 ,3) b (6 ,3) b b (11 ,3) ; b (4 ,3) b (8 ,3) b b (9 ,4) ; b (2 ,4) b (6 ,4) b b (11 ,4) ; b (4 ,4) b (8 ,4) b

check Appendix AP 5 for dependency: check.sci check Appendix AP 4 for dependency: noof3.sci check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.5 5 variable Kmap


1 // f=x y z+wxz+xy+v w yz 2 clc ; 3 n =4; 4 k =[0 0 0 0;

69

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

1 0 1 1; 0 1 1 0; 1 1 1 0]; k (: ,: ,2) =[0 0 0 0; 1 0 1 1; 0 1 1 0; 0 1 1 0]; // k =[1 0 0 0 ; // 0 0 0 0; // 0 0 0 0; // 0 0 1 0]; // k ( : , : , 2 ) =[1 0 0 0 ; // 0 0 0 0; // 0 0 0 0; // 1 0 0 0]; k (: ,: ,3) = zeros (n , n ) ; k (: ,: ,4) = zeros (n , n ) ; var =[ y z v w x ]; p1 =[ y z y z yz yz ]; p2 =[ v w x ; v w x ; v wx ; v wx ; vw x ; vw x ; vwx ; vwx ]; cmn16 =9; cmn8 =5; cmn4 =3; cmn2 =2; temp =1; printf ( The m i n i m a l e c p r e s s i o n o f t h e g i v e n Kmap ); disp ( k (: ,: ,1) ) ; disp ( k (: ,: ,2) ) ; disp ( i s : ) ; printf ( f ) ; printf ( = ) ; // 32 c e l l s for i =1: n for j =1: n for l =1:2 if ( k (i ,j , l ) ~=1 & k (i ,j , l ) ~=2) 70

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 73 74 75 76 77 78

temp =0; break ; end end end end if ( temp ==1) printf ( 1 ) ; abort ; end // 16 c e l l s // 8+8 row c e l l s z1 = ones (2 ,4 ,2) ; z2 = ones (4 ,2 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 =[ 000 001 011 010 100 101 111 110 ]; for i =1: n if ( i ==4) t =1; else t = i +1; end z =[ k (i ,: ,1:2) ; k (t ,: ,1:2) ]; z1 =[ k (i ,: ,3:4) ; k (t ,: ,3:4) ]; if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) < cmn16 ) k (i ,: ,3:4) = ones (4 ,2) ; k (t ,: ,3:4) = ones (4 ,2) ; a = strsplit ( temp1 (1 , i ) ) ; b = strsplit ( temp1 (1 , t ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end 71

79 end 80 end 81 k (i ,: ,3:4) = ones (1 ,4 ,2) ; 82 k (t ,: ,3:4) = ones (1 ,4 ,2) ; 83 end 84 end 85 // 8+8 column c e l l s 86 for j =1: n 87 if ( j ==4) 88 t =1; 89 else 90 t = j +1; 91 end 92 z =[ k (: ,j ,1:2) k (: ,t ,1:2) ]; 93 z1 =[ k (: ,j ,3:4) k (: ,t ,3:4) ]; 94 if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) < cmn16 ) 95 k (: ,j ,3:4) = ones (4 ,2) ; 96 k (: ,t ,3:4) = ones (4 ,2) ; 97 a = strsplit ( temp2 (1 , j ) ) ; 98 b = strsplit ( temp2 (1 , t ) ) ; 99 c = strsplit ( temp2 (1 , j +4) ) ; 100 d = strsplit ( temp2 (1 , t +4) ) ; 101 c1 = check (a ,b ,c , d ) ; 102 for in =1: max ( size ( c1 ) ) 103 if ( c1 ( in ) ==0 & a ( in ) == 0 ) 104 printf ( %s , var (2+ in ) ) ; 105 else 106 if ( c1 ( in ) ==0 & a ( in ) == 1 ) 107 printf ( var (2+ in ) ) ; 108 end 109 end 110 end 111 printf ( + ) ; 112 k (: ,j ,3:4) = ones (1 ,4 ,2) ; 113 k (: ,t ,3:4) = ones (1 ,4 ,2) ; 114 end 115 end 116 // 4 x4 f r o n t m a t r i x

72

117 if ( number_of ( k (: ,: ,1) ,0) ==0 & number_of ( k (: ,: ,3) ,1) <

cmn16 )
118 printf ( var (3) ) ; 119 printf ( ) ; 120 k (: ,: ,3) = ones (4 ,4) ; 121 end 122 // 4 x4 r e a r m a t r i x 123 if ( number_of ( k (: ,: ,2) ,0) ==0 & number_of ( k (: ,: ,4) ,1) <

cmn16 )
124 printf ( var (3) ) ; 125 k (: ,: ,4) = ones (4 ,4) ; 126 end 127 // 8 c e l l s 128 // 2 x2 f r o n t and r e a r c e l l s 129 for i =1: n 130 for j =1: n 131 if ( i ==4) 132 t =1; 133 else 134 t = i +1; 135 end 136 if ( j ==4) 137 u =1; 138 else 139 u = j +1; 140 end 141 z = k (i ,j ,1:2) ; 142 z (1 ,2 ,:) = k (i ,u ,1:2) ; 143 z (2 ,1 ,:) = k (t ,j ,1:2) ; 144 z (2 ,2 ,:) = k (t ,u ,1:2) ; 145 z1 = k (i ,j ,3:4) ; 146 z1 (1 ,2 ,:) = k (i ,u ,3:4) ; 147 z1 (2 ,1 ,:) = k (t ,j ,3:4) ; 148 z1 (2 ,2 ,:) = k (t ,u ,3:4) ; 149 if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) < cmn8 ) 150 a = strsplit ( temp1 (1 , i ) ) ; 151 b = strsplit ( temp1 (1 , t ) ) ; 152 c = strcmp (a , b ) ;

73

153 for in =1: max ( size ( c ) ) 154 if ( c ( in ) ==0 & a ( in ) == 0 ) 155 printf ( %s , var ( in ) ) ; 156 else 157 if ( c ( in ) ==0 & a ( in ) == 1 ) 158 printf ( var ( in ) ) ; 159 end 160 end 161 end 162 a = strsplit ( temp2 (1 , j ) ) ; 163 b = strsplit ( temp2 (1 , u ) ) ; 164 c = strsplit ( temp2 (1 ,4+ j ) ) ; 165 d = strsplit ( temp2 (1 ,4+ u ) ) ; 166 c1 = check (a ,b ,c , d ) ; 167 for in =1: max ( size ( c1 ) ) 168 if ( c1 ( in ) ==0 & a ( in ) == 0 ) 169 printf ( %s , var (2+ in ) ) ; 170 else 171 if ( c1 ( in ) ==0 & a ( in ) == 1 ) 172 printf ( var (2+ in ) ) ; 173 end 174 end 175 end 176 k (i ,j ,3:4) = ones (1 ,1 ,2) ; 177 k (i ,u ,3:4) = ones (1 ,1 ,2) ; 178 k (t ,j ,3:4) = ones (1 ,1 ,2) ; 179 k (t ,u ,3:4) = ones (1 ,1 ,2) ; 180 printf ( + ) ; 181 end 182 end 183 end 184 // 1 x4 f r o n t and r e a r c e l l s 185 for i =1: n 186 z = k (i ,: ,1:2) ; 187 z1 = k (i ,: ,3:4) ; 188 if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) < cmn8 ) 189 printf ( p1 ( i ) ) ; 190 printf ( + ) ;

74

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

k (i ,: ,3:4) = ones (1 ,4 ,2) ; end end // 4 x1 f r o n t and r e a r c e l l s for j =1: n z = k (: ,j ,1:2) ; z1 = k (: ,j ,3:4) ; if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) < cmn8 ) a = strsplit ( temp2 (1 , j ) ) ; b = strsplit ( temp2 (1 , u ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end end printf ( + ) ; k (: ,j ,3:4) = ones (1 ,2 ,4) ; end end // 2 x4 f r o n t c e l l s for i =1: n if ( i ==4) t =1; else t = i +1; end z = k (i ,: ,1) ; z (2 ,: ,1) = k (t ,: ,1) ; z1 = k (i ,: ,3) ; z1 (2 ,: ,1) = k (t ,: ,3) ; if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn8 ) a = strsplit ( temp1 (1 , i ) ) ; b = strsplit ( temp1 (1 , t ) ) ; 75

229 c = strcmp (a , b ) ; 230 for in =1: max ( size ( c ) ) 231 if ( c ( in ) ==0 & a ( in ) == 0 ) 232 printf ( %s , var ( in ) ) ; 233 else 234 if ( c ( in ) ==0 & a ( in ) == 1 ) 235 printf ( var ( in ) ) ; 236 end 237 end 238 end 239 printf ( %s , var (3) ) ; 240 printf ( + ) ; 241 k (i ,: ,3) = ones (1 ,4) ; 242 k (t ,: ,3) = ones (1 ,4) ; 243 end 244 end 245 // 2 x4 r e a r c e l l s 246 for i =1: n 247 if ( i ==4) 248 t =1; 249 else 250 t = i +1; 251 end 252 z = k (i ,: ,2) ; 253 z (2 ,: ,1) = k (t ,: ,2) ; 254 z1 = k (i ,: ,4) ; 255 z1 (2 ,: ,1) = k (t ,: ,4) ; 256 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn8 ) 257 a = strsplit ( temp1 (1 , i ) ) ; 258 b = strsplit ( temp1 (1 , t ) ) ; 259 c = strcmp (a , b ) ; 260 for in =1: max ( size ( c ) ) 261 if ( c ( in ) ==0 & a ( in ) == 0 ) 262 printf ( %s , var ( in ) ) ; 263 else 264 if ( c ( in ) ==0 & a ( in ) == 1 ) 265 printf ( var ( in ) ) ; 266 end

76

267 end 268 end 269 printf ( var (3) ) ; 270 printf ( + ) ; 271 k (i ,: ,4) = ones (1 ,4) ; 272 k (t ,: ,4) = ones (1 ,4) ; 273 end 274 end 275 // 4 x2 f r o n t c e l l s 276 for j =1: n 277 if ( j ==4) 278 u =1; 279 else 280 u = j +1; 281 end 282 z = k (: ,j ,1) ; 283 z (: ,2 ,1) = k (: ,u ,1) ; 284 z1 = k (: ,j ,3) ; 285 z1 (: ,2 ,1) = k (: ,u ,3) ; 286 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn8 ) 287 a = strsplit ( temp2 (1 , i ) ) ; 288 b = strsplit ( temp2 (1 , t ) ) ; 289 c = strcmp (a , b ) ; 290 for in =1: max ( size ( c ) ) 291 if ( c ( in ) ==0 & a ( in ) == 0 ) 292 printf ( %s , var ( in ) ) ; 293 else 294 if ( c ( in ) ==0 & a ( in ) == 1 ) 295 printf ( var ( in ) ) ; 296 end 297 end 298 end 299 printf ( %s , var (3) ) ; 300 printf ( + ) ; 301 k (: ,j ,3) = ones (4 ,1) ; 302 k (: ,u ,3) = ones (4 ,1) ; 303 end 304 end

77

305 // 4 x2 r e a r c e l l s 306 for j =1: n 307 if ( j ==4) 308 u =1; 309 else 310 u = j +1; 311 end 312 z = k (: ,j ,2) ; 313 z (: ,2 ,1) = k (: ,u ,2) ; 314 z1 = k (: ,j ,4) ; 315 z1 (: ,2 ,1) = k (: ,u ,4) ; 316 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn8 ) 317 a = strsplit ( temp2 (1 , i ) ) ; 318 b = strsplit ( temp2 (1 , t ) ) ; 319 c = strcmp (a , b ) ; 320 for in =1: max ( size ( c ) ) 321 if ( c ( in ) ==0 & a ( in ) == 0 ) 322 printf ( %s , var (4+ in ) ) ; 323 else 324 if ( c ( in ) ==0 & a ( in ) == 1 ) 325 printf ( var (4+ in ) ) ; 326 end 327 end 328 end 329 printf ( var (3) ) ; 330 printf ( + ) ; 331 k (: ,j ,4) = ones (4 ,1) ; 332 k (: ,u ,4) = ones (4 ,1) ; 333 end 334 end 335 // 4 c e l l s 336 // 1 x4 f r o n t c e l l s 337 for i =1: n 338 z = k (i ,: ,1) ; 339 z1 = k (i ,: ,3) ; 340 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn4 ) 341 printf ( p1 (1 , i ) ) ; 342 printf ( %s , var (3) ) ;

78

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380

printf ( + ) ; k (i ,: ,3) = ones (1 ,4) ; end end // 1 x4 r e a r c e l l s for i =1: n z = k (i ,: ,2) ; z1 = k (i ,: ,4) ; if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn4 ) printf ( p1 (1 , i ) ) ; printf ( var (3) ) ; printf ( + ) ; k (i ,: ,4) = ones (1 ,4) ; end end // 4 x1 f r o n t c e l l s for j =1: n z = k (: ,j ,1) ; z1 = k (: ,j ,3) ; if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn4 ) printf ( p2 (j ,1) ) ; printf ( + ) ; k (: ,j ,3) = ones (4 ,1) ; end end // 4 x1 r e a r c e l l s for j =1: n z = k (: ,j ,2) ; z1 = k (: ,j ,4) ; if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn4 ) printf ( p2 (4+ j ,1) ) ; printf ( + ) ; k (: ,j ,4) = ones (4 ,1) ; end end // 2 x1 f r o n t and r e a r m a t r i x for i =1: n for j =1: n 79

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

if ( i ==4) t =1; else t = i +1; end z =[ k (i ,j ,1) ; k (t ,j ,1) ]; z (: ,: ,2) =[ k (i ,j ,2) k (t ,j ,2) ]; z1 =[ k (i ,j ,3) ; k (t ,j ,3) ]; z1 (: ,: ,2) =[ k (i ,j ,4) k (t ,j ,4) ]; if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) < cmn4 ) a = strsplit ( temp1 (1 , i ) ) ; b = strsplit ( temp1 (1 , t ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end end end a = strsplit ( temp2 (1 , j ) ) ; b = strsplit ( temp2 (1 ,4+ j ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end end printf ( + ) ; k (i ,j ,3) =1; k (t ,j ,3) =1; k (i ,j ,4) =1; k (t ,j ,4) =1; end 80

419 end 420 end 421 // 1 x2 f r o n t and r e a r m a t r i x 422 for i =1: n 423 for j =1: n 424 if ( j ==4) 425 u =1; 426 else 427 u = j +1; 428 end 429 z =[ k (i ,j ,1) k (i ,u ,1) ]; 430 z (: ,: ,2) =[ k (i ,j ,2) k (i ,u ,2) ]; 431 z1 =[ k (i ,j ,3) k (i ,u ,3) ]; 432 z1 (: ,: ,2) =[ k (i ,j ,4) k (i ,u ,4) ]; 433 if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) <1) 434 printf ( p1 ( i ) ) ; 435 a = strsplit ( temp2 (1 , j ) ) ; 436 b = strsplit ( temp2 (1 , u ) ) ; 437 c = strsplit ( temp2 (1 ,4+ j ) ) ; 438 d = strsplit ( temp2 (1 ,4+ j ) ) ; 439 c1 = check (a ,b ,c , d ) ; 440 for in =1: max ( size ( c1 ) ) 441 if ( c1 ( in ) ==0 & a ( in ) == 0 ) 442 printf ( %s , var (2+ in ) ) ; 443 else 444 if ( c1 ( in ) ==0 & a ( in ) == 1 ) 445 printf ( var (2+ in ) ) ; 446 end 447 end 448 end 449 printf ( + ) ; 450 k (i ,j ,3) =1; k (i ,u ,3) =1; 451 k (i ,j ,4) =1; k (i ,u ,4) =1; 452 end 453 end 454 end 455 // 2 c e l l s 456 // 1 x2 f r o n t c e l l s

81

457 for i =1: n 458 for j =1: n 459 if ( j ==4) 460 u =1; 461 else 462 u = j +1; 463 end 464 z =[ k (i ,j ,1) k (i ,u ,1) ]; 465 z1 =[ k (i ,j ,3) k (i ,u ,3) ]; 466 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn2 ) 467 printf ( p1 (1 , i ) ) ; 468 a = strsplit ( temp2 (1 , j ) ) ; 469 b = strsplit ( temp2 (1 , u ) ) ; 470 c = strcmp (a , b ) ; 471 for in =1: max ( size ( c ) ) 472 if ( c ( in ) ==0 & a ( in ) == 0 ) 473 printf ( %s , var (2+ in ) ) ; 474 else 475 if ( c ( in ) ==0 & a ( in ) == 1 ) 476 printf ( var (2+ in ) ) ; 477 end 478 end 479 end 480 printf ( + ) ; 481 k (i ,j ,3) =1; k (i ,u ,3) =1; 482 end 483 end 484 end 485 // 1 x2 r e a r c e l l s 486 for i =1: n 487 for j =1: n 488 if ( j ==4) 489 u =1; 490 else 491 u = j +1; 492 end 493 z =[ k (i ,j ,2) k (i ,u ,2) ]; 494 z1 =[ k (i ,j ,4) k (i ,u ,4) ];

82

495 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn2 ) 496 printf ( p1 (1 , i ) ) ; 497 a = strsplit ( temp2 (1 ,4+ j ) ) ; 498 b = strsplit ( temp2 (1 ,4+ u ) ) ; 499 c = strcmp (a , b ) ; 500 for in =1: max ( size ( c ) ) 501 if ( c ( in ) ==0 & a ( in ) == 0 ) 502 printf ( %s , var (2+ in ) ) ; 503 else 504 if ( c ( in ) ==0 & a ( in ) == 1 ) 505 printf ( var (2+ in ) ) ; 506 end 507 end 508 end 509 printf ( + ) ; 510 k (i ,j ,4) =1; k (i ,u ,4) =1; 511 end 512 end 513 end 514 // 2 x1 f r o n t c e l l s 515 for i =1: n 516 for j =1: n 517 if ( i ==4) 518 t =1; 519 else 520 t = i +1; 521 end 522 z =[ k (i ,j ,1) ; k (t ,j ,1) ]; 523 z1 =[ k (i ,j ,3) k (t ,j ,3) ]; 524 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn2 ) 525 a = strsplit ( temp1 (1 , i ) ) ; 526 b = strsplit ( temp1 (1 , t ) ) ; 527 c = strcmp (a , b ) ; 528 for in =1: max ( size ( c ) ) 529 if ( c ( in ) ==0 & a ( in ) == 0 ) 530 printf ( %s , var ( in ) ) ; 531 else 532 if ( c ( in ) ==0 & a ( in ) == 1 )

83

533 printf ( var ( in ) ) ; 534 end 535 end 536 end 537 printf ( p2 (j ,1) ) 538 printf ( + ) ; 539 k (i ,j ,3) =1; k (i ,u ,3) =1; 540 end 541 end 542 end 543 // 2 x1 r e a r c e l l s 544 for i =1: n 545 for j =1: n 546 if ( i ==4) 547 t =1; 548 else 549 t = i +1; 550 end 551 z =[ k (i ,j ,2) ; k (t ,j ,2) ]; 552 z1 =[ k (i ,j ,4) k (t ,j ,4) ]; 553 if ( number_of (z ,0) ==0 & number_of ( z1 ,1) < cmn2 ) 554 a = strsplit ( temp1 (1 , i ) ) ; 555 b = strsplit ( temp1 (1 , t ) ) ; 556 c = strcmp (a , b ) ; 557 for in =1: max ( size ( c ) ) 558 if ( c ( in ) ==0 & a ( in ) == 0 ) 559 printf ( %s , var ( in ) ) ; 560 else 561 if ( c ( in ) ==0 & a ( in ) == 1 ) 562 printf ( var ( in ) ) ; 563 end 564 end 565 end 566 printf ( p2 (4+ j ,1) ) 567 printf ( + ) ; 568 k (i ,j ,4) =1; k (i ,u ,4) =1; 569 end 570 end

84

571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

end // 1 c e l l f r o n t and r e a r m a t r i x for i =1: n for j =1: n z = k (i ,j ,1:2) ; z1 = k (i ,j ,3:4) ; if ( noof3 (z ,0) ==0 & noof3 ( z1 ,1) < cmn2 ) printf ( p1 (1 , i ) ) ; a = strsplit ( temp2 (1 , j ) ) ; b = strsplit ( temp2 (1 ,4+ j ) ) ; c = strcmp (a , b ) ; for in =2: max ( size ( c ) ) if ( a ( in ) == 0 & c ( in ) ==0) printf ( %s , var (2+ in ) ) ; else if ( a ( in ) == 1 & c ( in ) ==0) printf ( var (2+ in ) ) ; end end end printf ( + ) ; k (i ,j ,3:4) = ones (1 ,1 ,2) ; end end end // s i n g l e c e l l for i =1: n for j =1: n for z =1:2 if ( k (i ,j , z ) ==1 & k (i ,j , z +2) ==0) printf ( p2 (j ,1) ) ; printf ( p1 (1 , i ) ) ; printf ( + ) ; end end end end printf ( 0 ) ; 85

Scilab code Exa 4.6 Prime Implicants


1 clc ; 2 clear ; 3 z =1; 4 // e v a l u a t i n g f i r s t e x p r e s s i o n 5 for i =0:1 6 for j =0:1 7 for k =0:1 8 for l =0:1 9 f1 (z ,1) = bitor ( bitand (i , j ) , bitand (k , l

));
10 z = z +1; 11 end 12 end 13 end 14 end 15 z =1; 16 // e v a l u a t i n g 2 nd e x p r e s s i o n 17 for i =0:1 18 for j =0:1 19 for k =0:1 20 for l =0:1 21 f2 (z ,1) = bitand ( bitand (i , j ) , bitcmp (k

,1) ) ;
22 z = z +1; 23 end 24 end 25 end 26 end 27 // d e t e r m i n i n g w h e t h e r f c o v e r s h o r n o t . 28 for i =1:16 29 if ( f2 (i ,1) ==1) 30 if ( f2 (i ,1) == f1 (i ,1) )

86

31 ; 32 else 33 disp ( f d o e s n t c o v e r h ) ; 34 abort ; 35 end 36 end 37 end 38 disp ( f c o v e r s h and h i m p l i e s f ) ;

Scilab code Exa 4.7 Prime Implicants 2


1 clc ; 2 clear ; 3 disp ( f=x y+xz+y z ) ; 4 disp ( s i n c e x y i s a p r i m e i m p l i c a n t

n e i t h e r x nor

y a l o n e i m p l i e s f ); 5 disp ( o n l y x y a r e c o v e r e d by f ) ; check Appendix AP 1 for dependency: karmap.sci check Appendix AP 3 for dependency: karmap1.sci check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.8 Prime Implicants Of a Function


1 clc ; 2 k =[1 1 0 1; 3 0 1 1 1;

87

4 5 6 7 8

0 1 1 0; 0 0 0 0]; disp ( The p r i m e i m p l i c a n t s o f f u n c t i o n f ) ; karmap ( k ) ; karmap1 ( k ) ; check Appendix AP 1 for dependency: karmap.sci check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.9 Prime Implicants Of a Function 2


1 clc ; 2 k =[0 1 1 1; 3 0 1 1 0; 4 0 0 1 0; 5 0 0 1 0]; 6 disp ( The p r i m e i m p l i c a n t s 7 karmap ( k ) ;

o f the f u n c t i o n f );

check Appendix AP 8 for dependency: karmap3.sci check Appendix AP 2 for dependency: number_of.sci

Scilab code Exa 4.10 Cyclic Prime Implicant map


1 clc ; 2 k =[1 1 0 1; 3 0 1 1 1]; 4 karmap3 ( k ) ;

88

Chapter 5 Logical Design

Scilab code Exa 5.1 Odd Parity Bit Generator


1 clc ; 2 clear ; 3 // Takes x i n p u t and c h e c k w h e t h e r 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

it

is valid logic

l e v e l or not . x = input ( x = ) ; while ( x ~=0 & x ~=1) disp ( e n t e r a v a l i d l o g i c a l l e v e l ) ; x = input ( x = ) ; end // Takes y i n p u t and c h e c k w h e t h e r i t i s v a l i d l o g i c l e v e l or not . y = input ( y = ) ; while ( y ~=0 & y ~=1) disp ( e n t e r a v a l i d l o g i c a l l e v e l ) ; y = input ( y = ) ; end // Takes z i n p u t and c h e c k w h e t h e r i t i s v a l i d l o g i c l e v e l or not . z = input ( z = ) ; while ( z ~=0 & z ~=1) disp ( e n t e r a v a l i d l o g i c a l l e v e l ) ; 89

19 20 21 22 23 24 25 26

z = input ( z = ) ;

end p1 = bitand ( bitand ( bitcmp (x ,1) , bitcmp (y ,1) ) ,z ) ; p2 = bitand ( bitand ( bitcmp (x ,1) ,y ) , bitcmp (z ,1) ) ; p3 = bitand ( bitand ( bitcmp (y ,1) ,x ) , bitcmp (z ,1) ) ; p4 = bitand ( bitand (x , y ) ,z ) ; p = bitor ( bitor ( p1 , p2 ) , bitor ( p3 , p4 ) ) ; disp (p , The o u t p u t o f t h e odd p a r i t y g e n e r a t o r c i r c u i t i s ); 27 disp ( p= x y z+x yz+xy z+xyz ) ;

Scilab code Exa 5.2 Serial To Parallel converter


1 clc ; 2 clear ; 3 // Takes x i n p u t and c h e c k w h e t h e r 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

it is valid logic l e v e l or not . disp ( e n t e r t h e vaLue o f i n p u t L i n e & c o n t r o L s i g n a L s C1 and C2 ) ; x = input ( x = ) ; while ( x ~=0 & x ~=1) disp ( e n t e r a v a L i d L o g i c a L LeveL ) ; x = input ( x = ) ;1 end // Takes C1 i n p u t and c h e c k w h e t h e r i t i s v a l i d l o g i c l e v e l or not . C1 = input ( C1 = ) ; while ( C1 ~=0 & C1 ~=1) disp ( e n t e r a v a L i d L o g i c a L LeveL ) ; C1 = input ( C1 = ) ; end // Takes C2 i n p u t and c h e c k w h e t h e r i t i s v a l i d l o g i c l e v e l or not . C2 = input ( C2 = ) ; while ( C2 ~=0 & C2 ~=1) 90

19 disp ( e n t e r a v a L i d L o g i c a L LeveL ) ; 20 C2 = input ( C2 = ) ; 21 end 22 disp ( OUTPUT ) ; 23 disp ( L1 = bitand ( bitand ( bitcmp ( C1 ,1) , bitcmp ( C2 ,1) ) ,x ) , 24 25 26 27 28 29 30

L1 = ) ; disp ( L2 = bitand ( bitand ( bitcmp ( C1 ,1) , C2 ) ,x ) , L2 = ) ; disp ( L3 = bitand ( bitand ( C1 , bitcmp ( C2 ,1) ) ,x ) , L3 = ) ; disp ( L4 = bitand ( bitand ( C1 , C2 ) ,x ) , L4 = ) ; disp ( L1=xC1C2 ) ; disp ( L2=xC1C2 ) ; disp ( L3=xC1C2 ) ; disp ( L4=xC1C2 ) ;

Scilab code Exa 5.3 Transmission function for a network


1 clc ; 2 clear ; 3 // Takes i n p u t and c h e c k w h e t h e r 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

it

is valid logic

l e v e l or not . w = input ( w = ) ; while ( w ~=0 & w ~=1) disp ( e n t e r a v a l i d l o g i c a l w = input ( w = ) ; end x = input ( x = ) ; while ( x ~=0 & x ~=1) disp ( e n t e r a v a l i d l o g i c a l x = input ( x = ) ; end y = input ( y = ) ; while ( y ~=0 & y ~=1) disp ( e n t e r a v a l i d l o g i c a l y = input ( y = ) ; end 91

l e v e l );

l e v e l );

l e v e l );

19 z = input ( z = ) ; 20 while ( z ~=0 & z ~=1) 21 disp ( e n t e r a v a l i d l o g i c a l l e v e l ) ; 22 z = input ( z = ) ; 23 end 24 disp ( o r i g i n a l n e t w o r k x [ ( ( y z+z y )w )+w+y+x z ] ) 25 26 27 28 29 30 31 32

; disp ( x [ w y z+w yz+w+y+x z ] ) ; disp ( x [ y (w z +1)+w yz+w+x z ] ) ; disp ( x [ y+w+yz+x z ] ) ; disp ( x [ y+yz+w+x z ] ) ; disp ( x [ y+z+x z+w ] ) ; disp ( x [ y+z+w ] ) ; // o u t p u t o f t h e r e l a y n e t w o r k f o r t h e i n p u t s g i v e n disp ( p = bitand ( bitcmp (x ,1) , bitor ( bitor ( bitcmp (y ,1) , bitcmp (z ,1) ) ,w ) ) , o u t p u t = ) ;

Scilab code Exa 5.4 4 Input Contact Network


1 clc ; 2 clear ; 3 // Takes i n p u t and c h e c k w h e t h e r 4 5 6 7 8 9 10 11 12 13 14 15

it

is valid logic

l e v e l or not . w = input ( w = ) ; while ( w ~=0 & w ~=1) disp ( e n t e r a v a l i d l o g i c a l w = input ( w = ) ; end x = input ( x = ) ; while ( x ~=0 & x ~=1) disp ( e n t e r a v a l i d l o g i c a l x = input ( x = ) ; end y = input ( y = ) ; while ( y ~=0 & y ~=1) 92

l e v e l );

l e v e l );

16 disp ( e n t e r a v a l i d l o g i c a l l e v e l ) ; 17 y = input ( y = ) ; 18 end 19 z = input ( z = ) ; 20 while ( z ~=0 & z ~=1) 21 disp ( e n t e r a v a l i d l o g i c a l l e v e l ) ; 22 z = input ( z = ) ; 23 end 24 disp ( We have 4 r e l a y s W, X, Y, Z which t a k e s BCD 25 26

27 28 29 30 31 32 33

number a s i n p u t ) ; disp ( s o t h e max v a l u e v a l u e we can g e t i s 1 0 0 1 ) ; disp ( By d r a w i n g t h e Karnaugh map ( c o n s i d e r a l l t e r m s above 1001 as dont c a r e s ) the minimised expression i s ); disp ( T(w , x , y , z )=wz+xyz+x yz ) ; disp ( T(w , x , y , z )=xyz+x yz ) ; disp ( Draw t h e s e r i e s p a r a l l e l r e a l i z a t i o n o f T ) ; disp ( We can i d e n t i f y t h e r e d u n d a n c y o f y c o n t a c t a t t h e r i g h t end ) ; disp ( which can be removed ) ; disp ( The c u t s e t r e a l i s a t i o n o f t h e a b o v e m i n i m i z e d s e r i e s p a r a l l e l network i s ); disp ( T(w , x , y , z ) =(w+y ) ( x+z ) ( x+z ) ) ;

Scilab code Exa 5.5 Minimal contact Network


1 clc ; 2 clear ; 3 disp ( Given m i n i m a l c o n t a c t n e t w o r k

i s T(w , x , y , z )= wxy+wxz+w x y z+w x yz ) ; 4 disp ( T(w , x , y , z )=wx ( y+z )+w x ( y z+yz ) ) ; 5 disp ( Draw t h e e q u i v a l e n t s e r i e s p a r a l l e l c i r c u i t o f T ) ; 6 disp ( I n t e r c h a n g e t h e l o c a t i o n s o f c o n t a c t s o f w and x and c o n n e c t t h e n o d e s ) ; 93

disp ( By c o n n e c t i n g i n t h e a b o v e manner t h e r e i s no l o g i c a l e f f e c t s i n c e the c o n n e c t i o n path i s not at a l l used ); 8 disp ( now t h e l o w e r b r a n c h o f yz+y z can be w r i t t e n a s ( y+z ) ( y+z ) s o t r a n s f e r c o n t a c t s can be u s e d ); 9 disp ( T h i s p a r a l l e l c o n n e c t i o n e n a b l e s u s t o combine two p a r a l l e l z c o n t a c t s and t h u s t h e minimum s p r i n g c o n n e c t i o n i s o b t a i n e d );
7

94

Chapter 6 Functional Decomposition And Symmetric Functions

Scilab code Exa 6.1 Function Decomposition


1 clc ; 2 clear ; 3 disp ( Given f u n c t i o n 4 5 6 7 8

f (w , x , y , z ) can be w r i t t e n a s

f o l l o w s ); disp ( f (w , x , y , z )=w x z+wx z+w yz+wyz ) ; disp ( f (w , x , y , z ) =((w z+wz ) x +(w z+wz ) y ) ) ; disp ( l e t Q=w z+wz ) ; disp ( t h e n we can r e w r i t e f a s f (w , x , y , z )=Qx+Q y ) ; disp ( f (w , x , y , z )=Qx+Q y=F [ Q(w , z ) , x , y ] ) ;

Scilab code Exa 6.3 Muliplicity


1 clc ; 2 clear ; 3 // d e t e r m i n e s t h e row and column m u l t i p l i c i t y

of a

mtraix 95

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

//m=i n p u t ( e n t e r t h e number o f v a r i a b l e s i n t h e f u n c t i o n =) ; m =4; for i =1: m for j =1: m x (i , j ) =0; end end // k =y ; // d i s p ( E n t e r t h e c e l l s i n d e x whose v a l u e s a r e 1 i n the matrix r e p r e s e n t i n g your f u n c t i o n ) ; // w h i l e ( k = n ) // i =i n p u t ( x i n d e x =) ; // j=i n p u t ( y i n d e x =) ; // x ( i , j ) =1; // k=i n p u t ( I f you want t o e n t e r more e n t e r y ( i n quotes ) e l s e n( in quotes ) : ) ; // end x =[1 0 1 1; 0 0 1 0; 0 1 1 1; 1 1 0 1]; l =0; // c h e c k s e v e r y row and f i n d t h e max no o f o n e s i n a row . for i =1: m k =0; for j =1: m if ( x (i , j ) ==1) k = k +1; end end if (k > l ) l=k; end end disp (l , Row m u l t i p l i c i t y = ) ; l =0; 96

38 39 40 41 42 43 44 45 46 47 48 49 50

// c h e c k s e v e r y column and f i n d t h e max no o f o n e s i n a column . for j =1: m k =0; for i =1: m if ( x (i , j ) ==1) k = k +1; end end if (k > l ) l=k; end end disp (l , Column m u l t i p l i c i t y = ) ;

Scilab code Exa 6.6 Symmetric


1 clc ; 2 clear ; 3 disp ( f ( x , y , z )=x y z+xy z+x yz ) ; 4 disp ( s i n c e i n t e r c h a n g i n g any two v a r i a b l e s 5 6 7 8

g i v e s us t h e same e q u a t i o n ) ; disp ( f o r e x a m p l e i n t e r c h a n g e x and y ) ; disp ( f ( y , x , z )=y x z+yx z+y xz ) ; disp ( f ( x , y , z )=f ( y , x , z ) ) ; disp ( So t h e f u n c t i o n f ( x , y , z ) i s s y m m e t r i c ) ;

Scilab code Exa 6.7 Symmetric 2


1 clc ; 2 clear ; 3 disp ( f ( x1 , x2 , x3 )=x1 x2 x3+x1x2 x3+x1 x2x3 ) ;

97

4 5 6 7 8

disp ( you can c h e c k t h a t a b o v e e q u a t i o i n i s n o t s y m m e t r i c i n x1 , x2 , x3 ) ; disp ( But i t i s s y m m e t r i c i n x1 , x2 , x3 ) ; disp ( l e t u s v e r i f y i t w i t h i n t e r c h a n g i n g x1 and x3 ); disp ( f ( x3 , x2 , x1 )=x3x2 x1+x3 x2 x1+x3x2x1 ) ; disp ( you can i d e n t i f y t h a t f ( x1 , x2 , x3 )=f ( x3 , x2 , x1 ) );

98

Chapter 7 Threshold Logic

Scilab code Exa 7.1 weighted Sum


1 clc ; 2 clear ; 3 // t a k e s t h e i n p u t and c h e c k w h e t h e r 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

it

i s v a l i d or

not x1 = input ( x1 = ) ; while ( x1 ~=0 & x1 ~=1) disp ( e n t e r a v a l i d l o g i c a l x1 = input ( x1 = ) ; end x2 = input ( x2 = ) ; while ( x2 ~=0 & x2 ~=1) disp ( e n t e r a v a l i d l o g i c a l x2 = input ( x = ) ; end x3 = input ( x3 = ) ; while ( x3 ~=0 & x3 ~=1) disp ( e n t e r a v a l i d l o g i c a l x3 = input ( x3 = ) ; end f = - x1 +(2* x2 ) + x3 ; if (f >0.5) then 99

l e v e l );

l e v e l );

l e v e l );

21 f =1; 22 else 23 f =0; 24 end 25 disp (f , o u t p u t y i s ) ; 26 m =1; 27 // d i s p l a y s t h e o u t p u t o f t h e a b o v e e x p r e s s i o n 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

for

a l l the combinations of inputs . for x =0:1 for y =0:1 for z =0:1 f1 (m ,1) = x ; f1 (m ,2) = y ; f2 (m ,3) = z ; f1 (m ,4) = - x +(2* y ) + z ; if ( f1 (m ,4) >0.5) then f1 (m ,5) =1; else f1 (m ,5) =0; end m = m +1; end end end disp ( x1 x2 x3 sum y); disp ( f1 )

Scilab code Exa 7.2 Inequalities


1 clc ; 2 clear ; 3 y= y ; 4 i =1; 5 // Takes t h e e q u i v a l e n t d e c i m a l v a l u e o f t h e min

t e r m s f o r e g : x yz =011=3 100

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

while ( y == y ) disp ( e n t e r t h e minterm o f a 3 v a r i a b l e f u n c t i o n ); x ( i ) = input ( : ) ; while ( x ( i ) >7) disp ( e n t e r a v a l i d minterm ) ; end disp ( p r e s s y i f you want t o e n t e r more min terms e l s e n : ); y = input ( ) ; i = i +1; end a =1; // G e n e r a t i n g t r u t h t a b l e f o r d e t e r m i n i n g t h e inequalities for i =0:1 for j =0:1 for k =0:1 for z =1: length ( x ) if ( x ( z ) == a -1) ; f (a ,4) =1; end end f (a ,1) = i ; f (a ,2) = j ; f (a ,3) = k ; a = a +1; end end end // d i s p l a y i n g t h e t r u t h t a b l e disp ( x1 x2 x3 f ); disp ( f ) ; disp ( ) ; a =1; // g e n e r a t i n g i n e q u a l i t i e s for i =0:1 for j =0:1 101

41 42 43 44 45 46

47 48 49 50 51 end 52 end 53 disp ( By s o l v i n g t h e a b o v e

for k =0:1 if ( f (a ,4) ==1) printf ( %3d w1 + %3d w2 + %3d w3>=T ,f (a ,1) ,f (a ,2) ,f (a ,3) ) disp ( ) else printf ( %3d w1 + %3d w2 + %3d w3<T ,f (a ,1) ,f (a ,2) ,f (a ,3) ) disp ( ) end a = a +1; end

i n e q u a l i t i e s we can g e t t h e v a l u e s o f w e i g h t s and T ) ;

Scilab code Exa 7.3 Unate Functions


1 clc ; 2 clear ; 3 disp ( Given f u n c t i o n i s f=x1x2+x2x3 ) ; 4 disp ( S i n c e x1 h a s no complemented form i n t h e a b o v e

f u n c t i o n f , f i s p o s i t i v e i n x1 ) ; 5 disp ( x2 h a s b o t h complemented and uncomplemented f o r m s i n f s o f i s n o t u n a t e i n x2 ) ; 6 disp ( x3 i s o n l y i n complemented form s o f i s n e g a t i v e i n x3 ) ;

Scilab code Exa 7.4 three cube representation


1 clc ; 2 clear ;

102

disp ( g i v e n f u n c t i o n i s f=x1 x2+x2x3 ) ; disp ( S i n c e t h e v a r i b l e s x1 and x3 a r e o n l y i n t h e i r complemented form f i s n e g a t i v e and u n a t e i n x1 and x3 ) ; 5 disp ( e v e n x2 i s o n l y i n i t s uncomplemented form s o f i s p o s i t i v e i n x2 ) ;
3 4

Scilab code Exa 7.5 True Vertex


1 clc ; 2 clear ; 3 n = input ( E n t e r t h e no o f i n p u t v a r i a b l e s : ) ; 4 // I n p u t t h e t r u e m i n i m a l v e r t i c e s 5 v = input ( E n t e r t h e no o f m i n i m a l t r u e v e r t i c e s : ) ; 6 disp ( v e r t e x w i l l be i n t h e form o f 101 i f i t i s 3

v a r i a b l e );
7 for i =1: v 8 printf ( V e r t e x %3d : ,i ) 9 s ( i ) = input ( ) ; 10 end 11 tv = input ( e n t e r a v e r t e x which you want f i n d w h e t h e r 12 13 14 15 16 17 18 19

20 21

t r u e v e r t e x or not ); // d e t e r m i n e s w h e t h e r t h e v e r t e x i s a t r u e o r n o t by comparing i t with the t r u e minimal v e r t i c e s for i =1: v if ( tv > s ( i ) ) disp ( I t i s a t r u e v e r t e x ) ; break ; else if ( i == v ) disp ( I t i s n o t a t r u e v e r t e x s i n c e i t i s n o t > t h a n any o f t h e min v e r t i c e s ); end end 103

22 end

104

Chapter 8 Reliable Design And Fault Diagnosis

Scilab code Exa 8.1 NOR Logic Circuit


1 clc ; 2 clear ; 3 disp ( To f i n d w h e t h e r h i s s a 0 o r n o t ) ; 4 disp ( F i r s t we have t o e x p r e s s f a s f u n c t i o n 5 6 7 8 9 10 11

12 13

of h and x1 , x2 , x3 , x4 ) ; disp ( S i m p l i f y i n g t h e g i v e n l o g i c a l c i r c u i t we o b t a i n the e x p r e s s i o n o f output as ); disp ( f (X, h ) = x 1 x 2 x 3 x 4 + h ( x2x3 + x1 x2 x3 x4 ) ) ) ; disp ( Here h = x2 x3 ) ; disp ( l e t G = x 1 x 2 x 3 x 4 & H = h ( x2x3 + x1 x2 x3 x4 ) ); disp ( Taking d e r i v a t i v e on b o t h s i d e s ) ; disp ( d f / dh=GdH/ dh ) ; disp ( A p p l l y i n g t h e f o r m u l a d [ f (X)+g (X) ] / dx=f (X) dg ( X) / dx e x o r g (X) d f (X) / dx e x o r d f (X) / dx . dg (X) / dx ) ; disp ( d f / dh=GdH/ dh=( x1+x2+x3+x4 ) ( x2x3+x1 x2 x3 x4 ) ) ; disp ( d f / dh=x1 x2x3+x2x3x4+x1 x2 x3 x4 ) ; 105

disp ( To f i n d t e s t s f o r h s a 0 we compute t h e v a l u e s f o r which h ( d f / dh ) =1 ) ; 15 disp ( h d f / dh=x2 x3 ( x1 x2x3+x2x3x4+x1 x2 x3 x4 ) ) ; 16 disp ( h d f / dh=x1 x2 x3 x4 ) ; 17 disp ( Thus t h e t e s t s e t f o r h s a 0 i s X( 0 , 0 , 0 , 0 ) ) ;
14

Scilab code Exa 8.2 Path Sensitizing


1 clc ; 2 clear ; 3 disp ( To p r o v e w h e t h e r t h e r e

4 5 6 7 8 9

10

11

p o s s i b i l i y of i d e n t i f i n g t h e f a u l t by s e n s i t i s i n g j u s t a s i n g l e path ); disp ( L e t u s s e n s i t i z e t h e p a t h G3 G6 G9 ) ; disp ( T h i s r e q u i r e s G6=1 , G10=0 , G11=0 , G8=0 ) ; disp ( Which i n t u r n r e q u i r e s x2=0 and x3=0 ( s i n c e G6 =1) ) ; disp ( G10=0 i m p i l e s t h a t x4=1 r e g a r d l e s s o f w h e t h e r t h e r e i s a f a u l t or not ); disp ( G11=0 i m p l i e s G7=1( s i n c e x3 =0) which i n t u r n i m p l i e s x4=0 ) ; disp ( E v i d e n t l y t o s a t i s f y b o t h G10=0 and G11=0 we must s e t c o n f l i c t i n g r e q u i r e m e n t s on x4 and t h u s have a c o n t r a d i c t i o n ) ; disp ( By symmetry o f t h e c i r c u i t i t i s o b v i o u s t h a t an a t t e m p t t o s e n s i t i z e t h e p a t h t h r o u g h G10 w i l l a l s o f a i l ); disp ( Hence t h e method o f one d i m e n s i o n a l p a t h s e n s i t i z i n g f a i l s to generate the t e s t inputs to d e t e c t the f a u l t );

Scilab code Exa 8.3 Two level OR AND Network 106

1 clc ; 2 clear ; 3 disp ( BY f o l l o w i n g 4 5 6

the procedure d i s c u s s e d in

S e c t i o n 8 . 6 ); disp ( we f i n d t h a t { a } = { 0 o r 2 o r 3 , 9 , 1 5 } ) ; disp ( and { b } = { 7 , 8 . 1 1 . 1 3 . 1 4 } ) ; disp ( Thus t h e m i n i m a l s e t s o f t e s t s f o r t h e a b o v e network a r e ); 7 disp ( {T} = { 0 o r 2 o r 3 , 7 , 8 , 9 , 1 1 , 1 3 , 1 4 , 1 5 } ) ; 8 disp ( I n t h i s c a s e t h e s t e s t s and b t e s t s c o r r e s p o n d t o s a 0 and s a 1 r e s p e c t i v e l y ) ;

Scilab code Exa 8.4 Various errors Summarized


1 clc ; 2 clear ; 3 disp ( V a r i o u s 4 5 6 7 8 9

e r r o r s a s s o c i a t e d w i t h some commonly u s e d g a t e s a r e su m ma ri se d b e l o w ) ; disp ( A 0 to 1 e r r o r i n an AND g a t e w i t h R i d e n t i c a l i n p u t s i s s u b c r i t i c a l ); disp ( s i n c e t h e o u t p u t d e p e n d s on t h e o t h e r i n p u t s o i t i s s u b c r i t i c a l ); disp ( I f t h e same 0 to 1 e r r o r i s i n a OR g a t e w i t h R i d e n t i c a l i n p u t s then i t i s c r i t i c a l e r r o r ); disp ( s i n c e an e r r o r i n p u t s t r u c k a t 1 w i l l make t h e o u t p u t o f OR g a t e t o 1 a l l t h e t i m e ) ; disp ( S i m i l a r l y t h e c r i t i c a l e r r o r o f AND g a t e i s s u b c r i t i c a l e r r o r o f OR g a t e ) ; disp ( S i m i l a r l y t h e e r r o r c l a s s i c f i c a t i o n i n v a r i o u s g a t e s i s m e n t i o n e d i n T a b l e 8 2 )

107

Chapter 10 Capabilities Minimization and transformation Of Sequential Machines

Scilab code Exa 10.1 Reducing machine


1 clc ; 2 clear ; 3 disp ( ... 4 disp ( PS 5 disp ( A 6 disp ( B 7 disp ( C 8 disp ( D 9 disp ( E 10 disp ( F 11 disp ( G 12 disp ( I n i t i a l

); x=0 x=1 ) ; E,0 C, 0 ) ; C, 0 A, 0 ) ; B, 0 G, 0 ) ; G, 0 A, 0 ) ; F,1 B, 0 ); E,0 D, 0 ) ; D, 0 G, 0 ) ; p a r t i t i o n c o n s i s t s of a l l the s t a t e s )

NS

; 13 disp ( P0=(ABCDEFG) ) ; 14 disp ( S i n c e E o n l y h a s 1 e q u i v a l e n t d i f f e r e n t from o t h e r s , i t can be p a r t i t i o n e d from o t h e r s ) ; 15 disp ( P1=(ABCDFG)E ) ; 108

16

17 18 19

20 21 22 23 24 25 26 27 28 29 30 31 32

disp ( now c h e c k f o r 2 e q u i v a l e n t i . e . 1 e q u i v a l e n t and t h e i r I i s u c c e s o r s f o r a l l p o s s i b l e I i a r e a l s o 1 e q u i v a l e n t ); disp ( A, F d o n t s a t i s f y t h e 2 e q u i v a l e n t s o t h e y can be p a r t i t i o n e d from o t h e r s ) ; disp ( P2=(AF) (BCDG)E ) ; disp ( I n t h e s i m i l a r manner P3 can be o b t a i n e d by s p l i t t i n g BD and CG s i n c e t h e y d o n t have t h e same 3 e q u i v a l e n t ); disp ( P3=(AF) (BD) (CG)E ) ; disp ( I n t h e same way P4 can be o b t a i n e d by s p l i t t i n g A and F ) ; disp ( P4=(A) ( F ) (BD) (CG)E ) ; disp ( P4 c a n t be s p l i t t e d more s o P5 w i l l be t h e same and t h e p a r t i t i o n s t o p s ) ; disp ( P5=(A) ( F ) (BD) (CG) (E) ) ; disp ( s o t h e r e d u c e d machine i s ) ; disp ( ... NS ); disp ( . . PS x=0 x=1 ) ; disp ( Aa e ,0 c , 0 ); disp ( F f e ,0 b , 0 ); disp ( . ( BD)b c ,0 a , 0 ); disp ( (CG)c b ,0 c , 0 ); disp ( Ee f ,1 b , 0 );

Scilab code Exa 10.2 Compaitability Graph


1 clc ; 2 clear ; 3 disp ( C o n s i d e r i n g t h e m e r g e r g r a p h o f machine M6 i n 4

PG 339 ) ; disp ( A s e t o f c o m p a i t a b i l i t i e s i s s a i d t o be c l o s e d i f and o n l y i f f o r e v e r y c o m p a i t a b l e c o n t a i n e d i n t h e s e t and a l l i t s i m p l i e d c o m p a i t a b i l i t i e s a r e a l s o i n t h e same s e t ) ; 109

disp ( A c l o s e d s e t o f c o m p a i t a b l e s which c o n t a i n s a l l the s t a t e s of M i s c a l l e d a c l o s e d c o v e r i n g ) ; 6 disp ( By o b s e r v i n g t h e m e r g e r g r a p h we can f i n d t h a t { (AD) (BE) (CD) } i s a c l o s e d s e t ) ; 7 disp ( And t h e s e t { (AB) (CD) (EF) } a p p e a r s t o be a c l o s e d c o v e r i n g );
5

110

Chapter 12 Structure Of Sequential Machines

Scilab code Exa 12.4 Closed Partitions


1 clc ; 2 clear ; 3 disp ( we know t h a t a p a r t i t i o n

4 5 6 7 8 9 10 11 12 13

p i on t h e s e t o f s t a t e s o f a S e q u e n t i a l machine M i s s a i d t o be c l o s e d i f , f o r e v e r y two s t a t e s S i and S j which a r e i n t h e same b l o c k o f p i and any I t h i n p u t s u c c e s s o r o f S i and S j a r e a l s o i n t h e same g r o u p ); disp ( Based on t h e a b o v e d e f i n i t i o n we can make seven c l o s e d p a r t i t i o n s as below ); disp ( P a r i t i o n 1 ) ; disp ( {A, B , C , D, E , F , G, H} ) disp ( P a r i t i o n 1 ) ; disp ( { (ABCD) (EFGH) } ) ; disp ( P a r i t i o n 2 ) ; disp ( { (ADEH) (BCFG) } ) disp ( P a r i t i o n 3 ) ; disp ( { (AD) (BCFG) (EH) } ) ; disp ( P a r i t i o n 4 ) ; 111

14 15 16 17 18 19

20 21 22 23

disp ( { (ADEH) (BC) (FG) } ) disp ( P a r i t i o n 5 ) ; disp ( { (AD) (BC) (EH) (FG) } ) disp ( P a r i t i o n 6 ) ; disp ( { (ABCCDEFGH) } ) ; disp ( By a s s i g n i n g v a l u e s from 000 t o 111 t o a l l t h e s t a t e s from A t o H and o b t a i n i n g t h e f u n c t i o n s f o r Y1 , Y2 , Y3 and z w i l l r e s u l t i n t h i s e q u a t i o n s ); disp ( Y1=x y1 ) ; disp ( Y2=x y2+xy2 ) ; disp ( Y3=xy2+x y1 y2y3+y3 y2 y3+y1y2y3+x y1y2 y3 ) ; disp ( z=y1 y2 y3 ) ;

Scilab code Exa 12.5 Output Consistent


1 clc ; 2 clear ; 3 disp ( For t h e 4 s t a t e machine M4 i n PG 397 ) ; 4 disp ( IF t h e f o u r s t a t e s A, B , C , D a r e a s s i g n e d a s

6 7 8 9 10 11 12 13

0 0 , 0 1 , 1 0 , 1 1 ( t a = { (AC) (BD) } ) and 0 0 , 0 1 , 1 1 , 1 0 ( t b = { ( AD) (BC) } ) ) ; disp ( And i f t h e o u t p u t , n e x t s t a t e f u n c t i o n s o f b o t h t h e a s s i g n m e n t s a r e c a l c u l a t e d we can f i n d t h a t ); disp ( For f i r s t a s s i g n m e n t ) ; disp ( Y1=x y1+xy1 ) ; disp ( Y2=x y2+y1 y2+xy1y2 ) ; disp ( z=x y1 y2+x y1y2+xy1 y2+xy1y2 ) ; disp ( S e c o n d A s s i g n m e n t ) ; disp ( Y1=x y1+x y1 ) ; disp ( Y2=x y2+xy1 y2+y1y2 ) ; disp ( z=x y2+xy2 ) ;

112

14

disp ( S i n c e t h e s e c o n d a s s i g n m e n t r e s u l t s i n r e d u c e d output e x p r e s s i o n t h i s p a r t i t i o n i s c a l l e d as output c o n s i s t e n t p a r t i t i o n )

Scilab code Exa 12.7 Dependence


1 clc ; 2 clear ; 3 disp ( A p a r t i t i o n

4 5 6

p i 0 on t h e s t a t e s o f a machine M i s s a i d t o be o u t p u t c o n s i s t e n t i f f o r e v e r y b l o c k p i 0 and e v e r y i n p u t , a l l t h e s t a t e s c o n t a i n e d i n t h e b l o c k have t h e same o u t p u t s ) ; disp ( C o n s i d e r i n g t h e a b o v e d e f i n i t i o n I t can be understood t h a t ); disp ( p i 0 = { (AD) , (BC) } i s an o u t p u t c o n s i s t e n t p a r t i t i o n o f machine M4 ) ; disp ( s i n c e t h e o u t p u t s o f A, D s t a t e s f o r any i n p u t s a r e same and s i m i l a r l y t h e o u t p u t s o f B , C a r e same . ) ; disp ( So t h e p a r t i t i o n { (AD) , (BC) } a r e s a i d t o be output c o n s i s t e n t );

Scilab code Exa 12.8 input Consistent Matrix


1 clc ; 2 clear ; 3 disp ( For t h e Machine M5 i n PG 400 ) ; 4 disp ( S t a t e A i m p l i e s t h e i d e n t i f i c a t i o n

of states C and D ) ; 5 disp ( s i m i l a r l y C i m p l i e s t h e i d e n t i f i c a t i o n o f E and F s t a t e s ) ; 6 disp ( I n t h e same way C i m p l i e s t h e i d e n t i f i c a t i o n o f A and B ) ; 113

disp ( Thus t h e s m a l l e s t i n p u t c o n s i s t e n t p a r t i t i o n f o r M5 i s ) ; 8 disp ( p i = { (AB) , (CD) , ( EF) } ) ; 9 disp ( Any o t h e r p a r t i t i o n t h a t c o n t a i n s p i i s a l s o input c o n s i s t e n t )


7

Scilab code Exa 12.9 Implementation of Input Consistent matrix


1 clc ; 2 clear ; 3 disp ( I n p r e v i o s u l y p r o b l e m s we have d e t e r m i n e d t h e

4 5 6 7 8 9 10 11

i n p u t and o u t p u t c o n s i s t e n t p a r t i t i o n s f o r t h e Machine M5 ) ; disp ( I n p u t c o n s i s t e n t p a r t i t i o n { (AB) , (CD) , ( EF) } ) ; disp ( Output c o n s i s t e n t p a r t i t i o n { (ACE) , (BDF) } ) ; disp ( By a s s i g n i n g 000 t o 101 t o a l l t h e s t a t e s from A t o F ) ; disp ( we can f i n d t h e e x p r e s s i o n s f o r t h e n e x t s t a t e and t h e o u t p u t ) ; disp ( Y1=y2 ) ; disp ( Y2=y1 y2 ) ; disp ( Y3=xy3+xy2+x y2 y3+y2y3 ) ; disp ( z=xy3 ) ;

Scilab code Exa 12.10 Autonous Clock


1 clc ; 2 clear ; 3 disp ( P a r t i t i o n 4

p i o f t h e machine M5 i s { (AB) , (CD) , (

EF) } ) ; disp ( I f M5 p o s s e s a c l o s e d p a r t i t i o n p i s u c h t h a t p i > i . i f t h e autonomous c l o c k h a s #p i s t a t e s 114

t h e p e r i o d p w i l l be l e s s t h a n o r e q u a l t o #p i ) ; 5 disp ( s i n c e i n t h e a b o v e c l o s e d p a r t i t i o n o f M5 we have 3 s t a t e s i . e . (AB) a s one s t a t e and (CD) and (EF) a s o t h e r two ) ; 6 disp ( So t h e p e r i o d o f t h e autonomous c l o c k i s p=3 )

Scilab code Exa 12.11 partition pair


1 clc ; 2 clear 3 disp ( From t h e

4 5 6 7

d e f i n i t i o n o f p a r t i t i o n p a i r (T , T ) which i s an o r d e r e d p a i r o f p a r t i t i o n s s u c h t h a t i f S i and S j a r e i n t h e same b l o c k o f t , t h e n f o r e v e r y i n p u t I k i n I , n e x t s t a t e s a r e i n t h e same b l o c k o f T ) ; disp ( By a p p l y i n g t h e a b o v e d e f i n i t i o n we can o b t a i n the f o l l o w i n g p a r t i t i o n p a i r s ); disp ( ( p i 1 , p i 1 ) =( { (ABC) , (DEF) } , { (ABC) , (DEF) } ) ) ; disp ( ( t1 , t 1 ) =( { (ABCD) , ( EF) } , { (AE) , (BCDF) } ) ) ; disp ( ( t2 , t 2 ) =( { (AE) , (BCDF) } , { (ACDE) , ( BF) } ) ) ;

Scilab code Exa 12.12 partition pair 2


1 clc ; 2 clear ; 3 // 1 i s e q u i v a l e n t t o A ad 2 t o B and s o on . 4 p1 =[ A D ]; // p a i r s i n p a r t i t i o n t ( 1 and 4

r e p r e s e n t s t h a t 1 s t and 4 t h s t a t e a r e i n t h e same block ) ; 5 p2 =[ C E ];


6 7 q1 =[ A

E ];

// p a i r s i n p a r t i t i o n t 115

8 q2 =[ B D ]; 9 q3 =[ C F ]; 10 11 disp ( t h e f o l l o w i n g 12 13 14 15 16 17 18 19 20 21

are the p a r t i t i o n s of the machine M8 ) ; disp ( T ) ; disp ( F , p2 (2) , p2 (1) , B , p1 ) ; disp ( T ) ; disp ( q3 , q2 , q1 ) ; disp ( ) ; disp ( T ) ; disp ( F ,p2 , B , p1 ) ; disp ( T ) ; disp ( q3 ,[ q1 q2 ]) ; disp ( ) ;

Scilab code Exa 12.14 State Consistent Partitions


1 clc ; 2 clear ; 3 // assume t h e 4 5 6 7 8 9 10 11 12 13 14 15 16

f i r s t cloumn v a l u e s a r e o f machine M1 and 2 nd column a r e o f M2 p =[1 ,1;1 3;2 2;2 4;3 3;3 1;4 4;4 2]; z =1; for i =1: length ( p (: ,1) ) for j = i : length ( p (: ,1) ) if ( p (i ,1) == p (j ,1) & i ~= j ) q (z ,:) =[ p (i ,:) p (j ,:) ]; z = z +1; end end end disp ( p i (R) ) ; disp ( q ) ; z =1; 116

17 for i =1: length ( p (: ,1) ) 18 for j = i : length ( p (: ,1) ) 19 if ( p (i ,2) == p (j ,2) & i ~= j ) 20 q (z ,:) =[ p (i ,:) p (j ,:) ]; 21 z = z +1; 22 end 23 end 24 end 25 disp ( p i ( S ) ) ; 26 disp ( q ) ;

117

Chapter 13 State Identicaiton And Fault Detection Experiments

Scilab code Exa 13.1 Fault DetectionExeriment


1 clc ; 2 clear ; 3 disp ( By f o l l o w i n g 4

5 6 7 8 9 10 11 12 13 14 15 16

the ge ne ra l procedure f o r f a u l t d e t e c t i o n i n t h e t e x t book Page n . o 478 ) ; disp ( A c c o r d i n g t o 3 & 4 s t e p s o f g e n e r a l p r o c e d u r e a l l t h e i n p u t s a r e t a k e n a s 0 and t h e o u t p u t i s checked ] ); disp ( X : 0 0 0); disp ( A B A B ) ; disp ( Z : 1 0 1); disp ( By f o l l o w i n g t h e s t e p 5 ) ; disp ( X : 1 0); disp ( B B A ) ; disp ( Z : 0 0); disp ( A c c o r i n g t o s t e p 6 ) ; disp ( X : 1 0 0 1 0 0); disp ( A D D D C D D ) ; disp ( Z : 0 3 3 1 2 3); disp ( S t e p 7 & 8 ) 118

17 18 19 20 21

disp ( X : 1 1 0); disp ( D C A B ) ; disp ( Z : 1 1 1); disp ( Whole c h e c k i n g e x p e r i m e n t ) disp ( X : 0 0 0 1 0 1 0 0 1 0 0 1 1 0 ); 22 disp ( A B A B B A D D D C D D C A B ) ; 23 disp ( Z : 1 0 1 0 0 0 3 3 1 2 3 1 1 1);

119

Chapter 14 Memory Deniteness Information Losslessness of Finite Automata

Scilab code Exa 14.2 Synchronizing tree


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

clc ; clear ; N =4; // s t a t e t a b l e o f t h e machine s =[ PS x=0 x=1 ; A A B ; B C B ; C A D ; D C B ]; k =1; l =2; m =1; n =1; f (k ,4) = ABCD ; k = k +1; for i =2: max ( size ( s (: ,1) ) ) for j = i : max ( size ( s (: ,1) ) ) if ( s (i ,2) == s (j ,2) & i ~= j ) f (k , l ) = strcat ([ s (i ,1) s (j ,1) ]) ; 120

18 l = l +4; 19 if ( s (i ,2) == s (j ,2) ) 20 g (n , m ) = s (i ,2) ; 21 g (n , m +1) = ; 22 m = m +2; 23 else 24 g (n , m ) = s (i ,2) ; 25 g (n , m +1) = s (j ,2) ; 26 g (n , m +2) = ; 27 m = m +3; 28 end 29 if ( s (i ,3) == s (j ,3) ) 30 g (n , m ) = s (i ,3) ; 31 g (n , m +1) = ; 32 m = m +2; 33 else 34 g (n , m ) = strcat ([ s (i ,3) s (j ,3) ]) ; 35 g (n , m +1) = ; 36 m = m +2; 37 for z =2: max ( size ( s (: ,1) ) ) 38 if ( s (z ,3) == s (z ,1) ) 39 h (1 ,3) = s (z ,2) ; 40 h (1 ,5) = s (z ,3) ; 41 end 42 end 43 end 44 end 45 end 46 end 47 disp ( S y n c h r o n i z i n g t r e e f o r Machine M3 i s ) 48 disp ( f ) ; 49 disp ( g ) ; 50 disp ( h ) ;

Scilab code Exa 14.3 Contracted State Machine 121

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

clc ; clear ; s =[ PS x=0 x=1 ; A A B ; B E B ; C E F ; D E F ; E A D ; F E B ]; z =0; disp ( O r i g i n a l Machine M4 ) ; disp ( s ) ; p=s; n = max ( size ( s (: ,1) ) ) ; for i =2: n for j = i : n if ( s (i ,2) == s (j ,2) & s (i ,3) == s (j ,3) & i ~= j ) z = z +1; for k = j :n -1 p (k ,:) = p ( k +1 ,:) ; end for k =2: n -1 if ( p (k ,2) == s (j ,1) ) p (k ,2) = s (i ,1) ; end if ( p (k ,3) == s (j ,1) ) p (k ,3) = s (i ,1) ; end end end end end s = p (1: n -z ,:) ; disp ( The c o n t r a c t e d T a b l e M4 ) ; disp ( s ) ; p=s; n =n - z ; z =0; 122

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 73 74 75 76

for i =2: n for j = i : n if ( s (i ,2) == s (j ,2) & s (i ,3) == s (j ,3) & i ~= j ) z = z +1; for k = j :n -1 p (k ,:) = p ( k +1 ,:) ; end for k =2: n -1 if ( p (k ,2) == s (j ,1) ) p (k ,2) = s (i ,1) ; end if ( p (k ,3) == s (j ,1) ) p (k ,3) = s (i ,1) ; end end end end end s = p (1: n -z ,:) ; disp ( R e p e a t e d C o n t r a c t i o n s o f M4 ) ; disp ( s ) ; p=s; n =n - z ; z =0; for i =2: n for j = i : n if ( s (i ,2) == s (j ,2) & s (i ,3) == s (j ,3) & i ~= j ) z = z +1; for k = j :n -1 p (k ,:) = p ( k +1 ,:) ; end for k =2: n -1 if ( p (k ,2) == s (j ,1) ) p (k ,2) = s (i ,1) ; end if ( p (k ,3) == s (j ,1) ) p (k ,3) = s (i ,1) ; end 123

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

end end end end s = p (1: n -z ,:) ; disp ( s ) ; p=s; n =n - z ; z =0; for i =2: n for j = i : n if ( s (i ,2) == s (j ,2) & s (i ,3) == s (j ,3) & i ~= j ) z = z +1; for k = j :n -1 p (k ,:) = p ( k +1 ,:) ; end for k =2: n -1 if ( p (k ,2) == s (j ,1) ) p (k ,2) = s (i ,1) ; end if ( p (k ,3) == s (j ,1) ) p (k ,3) = s (i ,1) ; end end end end end s = p (1: n -z ,:) ; disp ( s ) ;

Scilab code Exa 14.4 Testing Table


1 clc ; 2 clear ; 3 N =4;

124

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

s =[ PS x=0 x=1 ; A A B ; B C B ; C A D ; D C B ]; n = max ( size ( s (: ,1) ) ) ; s ( n +1 ,:) =[ ]; l =2; for i =2: n for j = i : n if ( i ~= j ) s ( n +l ,1) = strcat ([ s (i ,1) s (j ,1) ]) ; s ( n +l ,2) = strcat ([ s (i ,2) s (j ,2) ]) ; s ( n +l ,3) = strcat ([ s (i ,3) s (j ,3) ]) ; l = l +1; end end end disp ( s ) ;

Scilab code Exa 14.6 Testing graph


1 2 3 4 5 6 7 8 9 10 11 12 13

clc ; clear ; s =[ PS x=0 x=1 ; A A B ; B C D ; C D C ; D B A ]; q =[0 0;0 0;1 1;1 1]; n = max ( size ( s (: ,1) ) ) ; l =1; for i =2: n z =0; x =0; for j = i : n 125

14 if ( q (i -1 ,1) ==0 & q (i -1 ,2) ==0 & i ~= j & z ==0) 15 p (l ,1) = s (i ,1) ; 16 p (l ,2) = strcat ([ s (i ,2) s (i ,3) ]) ; 17 p (l ,3) = ; 18 l = l +1; z = z +1; 19 else 20 if ( q (i -1 ,1) ==1 & q (i -1 ,2) ==1 & x ==0) 21 p (l ,1) = s (i ,1) ; 22 p (l ,2) = ; 23 p (l ,3) = strcat ([ s (i ,2) s (i ,3) ]) ; 24 l = l +1; x = x +1; 25 end 26 end 27 end 28 end 29 disp ([ PS z=0 z=1 ]) ; 30 disp ( p ) ; 31 disp ([ AB (AC) (AD) (BC) (BD) ; 32 CD (AC) (AD) (BC) (BD) ]) ;

Scilab code Exa 16.7 Testing Table 2


1 2 3 4 5 6 7 8 9 10 11 12 13

clc ; clear ; s =[ S C1 SB1 ; B1 S ; C1 C2 ; C2 S ]; n = max ( size ( s (: ,1) ) ) ; l =1; for i =1: n for j = i : n if ( i ~= j ) p (l ,1) = strcat ([ s (i ,1) s (j ,1) ]) ; if ( s (i ,2) == | s (j ,2) == ) 126

14 15 16 17 18 19 20 21 22 23 24 end 25 end 26 end

p (l ,2) = ; else p (l ,2) = strcat ([ s (i ,2) s (j ,2) ]) ; end if ( s (i ,3) == | s (j ,3) == ) p (l ,3) = ; else p (l ,3) = strcat ([ s (i ,3) s (j ,3) ]) ; end l = l +1;

127

Chapter 15 Linear Sequential Machines

Scilab code Exa 15.3 Transfer function


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

clc ; clear ; T1 =[1 2 1]; T2 =[0 1 1]; n = max ( size ( T1 ) ) ; a =n -1; b=a; T3 = zeros (1 , a + b +1) ; for i =1: n b =n -1; for j =1: n T3 ( a + b +1) = T3 ( a + b +1) +( T1 ( i ) * T2 ( j ) ) ; b =b -1; end a =a -1; end m = max ( size ( T3 ) ) ; i =1; j=m; while (i <= floor ( m /2) & j >=( ceil ( m /2) +1) ) t = T3 ( i ) ; 128

22 T3 ( i ) = modulo ( T3 ( j ) ,3) ; 23 T3 ( j ) = modulo (t ,3) ; 24 i = i +1; 25 j =j -1; 26 end 27 T3 ( ceil ( m /2) ) = modulo ( T3 ( ceil ( m /2) ) ,3) ; 28 disp ( Top row o f t h e b e l o w m a t r i x i s p o w e r s o f t h e

t r a n s f e r f u n c t i o n and bottom row i s o f c o e f f i c i e n t s o f r e s p e c t i v e powers ); 29 disp ([4 3 2 1 0]) ; 30 disp ( T3 ) ;

Scilab code Exa 15.4 Impulse response


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

clc ; clear ; T1 =[1 0 1 1]; h =[1 1 0 1 0 0 0 0 0]; in =[1 0 1 1]; l =1; for i =2: max ( size ( in (1 ,:) ) ) h1 = h ; if ( in ( i ) ==1) for k =1: i -1 t = h1 ( max ( size ( h1 (1 ,:) ) ) ) ; for j = max ( size ( h1 (1 ,:) ) ) -1: -1:1 h1 ( j +1) = h1 ( j ) ; end h1 (1) = t ; end D (l ,:) = h1 (1 ,:) ; l = l +1; end end output = h + D (1 ,:) + D (2 ,:) ; 129

22 23

output = modulo ( output ,2) ; disp ( output ) ;

Scilab code Exa 15.5 Null Sequence


1 clc ; 2 clear ; 3 //T=1+D+D3 o v e r GF( 2 ) 4 // l e t X0 i s t h e n u l l s e q u e n c e 5 //0=X0+DX0+D3X0 s i n c e o u t p u t o f t h e n u l l 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

sequence

i s zero // Adding X0 on b o t h s i d e s we g e t X0=DX0+D3X0 nul =[0 0 1]; T =[0 1 0 1]; j =1; for i =1: max ( size ( T ) ) ; if ( T ( i ) ==1) s (1 , j ) =i -1; j = j +1; end end j =4; for i =1:7 nul (1 , j ) = nul (1 ,j - s (1) ) + nul (1 ,j - s (2) ) ; nul (1 , j ) = modulo ( nul (1 , j ) ,2) ; j = j +1; end disp ( N u l l S e q u e n c e f o r i n p u t 001 i s ) ; disp ( nul ) ;

Scilab code Exa 15.6 Null Sequence 2


1 clc ;

130

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

clear ; //T=1+2D2+D3 o v e r GF( 3 ) // l e t X0 i s t h e n u l l s e q u e n c e //0=X0+2D2 X0+D3 X0 s i n c e o u t p u t o f t h e n u l l sequence i s zero // Adding X0 on b o t h s i d e s we g e t 2X0=2D2 X0+D3 X0 //X0=D2 X0+2D3 X0 nul =[1 1 1]; T =[0 0 1 2]; j =1; for i =1: max ( size ( T ) ) ; if ( T ( i ) >=1) s (1 , j ) =i -1; j = j +1; end end j =4; for i =1:15 nul (1 , j ) = nul (1 ,j - s (1) ) +2* nul (1 ,j - s (2) ) ; nul (1 , j ) = modulo ( nul (1 , j ) ,3) ; j = j +1; end disp ( N u l l S e q u e n c e f o r i n p u t 001 i s ) ; disp ( nul ) ;

Scilab code Exa 15.10 Reducable


1 2 3 4 5 6 7 8

clc ; clear ; A =[0 1 1; 1 0 0; 1 0 0]; B =[1;1;0]; C =[1 1 0; 1 1 1]; 131

9 D =[0;1]; 10 K =[ C ; C * A ; C *( A * A ) ]; 11 K = modulo (K ,2) ; 12 disp ( K m a t r i x ) 13 disp ( K ) ; 14 disp ( rank ( K ) , s i n c e t h e r a n k o f K i s 15 disp ( K i s n o t f u r t h e r r e d u c i b l e ) ; 16 // z=K y ;

);

Scilab code Exa 15.11 Linear Machine


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

clc ; clear ; A =[0 1 0; 1 0 0; 0 1 1]; B =[1;1;1]; C =[1 0 0]; D =[1]; K =[ C ; C * A ; C *( A * A ) ]; disp ( K ) ; disp ( rank ( K ) , r a n k o f K m a t r i x i s ) ; disp ( S i n c e t h e r a n k i s 2 K m a t r i x can be r e d u c e d ) ; disp ( As o n l y t h e f i r s t 2 r o w s a r e l i n e a r l y independent t h e r e f o r e ); T =[1 0 0;0 1 0]; disp (T , T = ) ; disp ( I n t h i s c a s e Q i s ) ; Q =[1 0;0 1]; disp ( Q ) ; Q_inv = inv ( Q ) ; R =T ; // y r e d=T y ; A_red = T * A * R ; B_red = T * B ; 132

24 25

C_red = C * R ; D_red = D ;

Scilab code Exa 15.12 Linear Machine 2


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

clc ; clear ; A =[1 0 0 0; 0 0 1 1; 1 1 0 0; 1 0 1 0]; B =[1 0; 0 0; 1 1; 1 1]; C =[0 1 0 1; 1 1 1 0]; D =[1 0; 0 1]; K =[ C ; C * A ; C *( A * A ) ]; K = modulo (K ,2) ; T = K (1:3 ,:) ; Q = K (1:3 ,1:3) ; Q_inv = inv ( Q ) ; Q_inv = abs ( modulo ( Q_inv ,2) ) ; R =[ Q_inv ;[0 0 0]]; disp ( Reduced m a t r i x A i s ) ; disp ( A_red = T * A * R ) ; disp ( Reduced m a t r i x B i s ) ; disp ( B_red = T * B ) ; disp ( Reduced m a t r i x C i s ) ; disp ( C_red = C * R ) ; disp ( Reduced m a t r i x D i s ) ; disp ( D_red = D ) ;

133

Scilab code Exa 15.15 Commutative ring


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

clc ; clear ; n =4; for i =1:4 for j =1:4 p (i , j ) = modulo ( i +j -2 ,4) ; end end disp ( modulo 4 A d d i t i o n ) ; disp ( p ) ; for i =1:4 for j =1:4 p (i , j ) = modulo (( i -1) *( j -1) ,4) ; end end disp ( modulo 4 M u l t i p l i c a t i o n ) ; disp ( p ) ;

Scilab code Exa 15.16 Identifying Whether the Ring is a Field Or not
1 2 3 4 5 6 7 8 9 10

clc ; clear ; n =4; a = zeros (1 , n ) ; for i =1: n for j =1: n pad (i , j ) = modulo ( i +j -2 ,4) ; end end for i =1: n 134

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

for j =1: n pmu (i , j ) = modulo (( i -1) *( j -1) ,4) ; end end t =0; for i =1: n for j =1: n if ( pad (i , j ) ==1) t =1; break ; end end end if ( t ==0) disp ( Modulo 4 r i n g i s n o t a f i e l d ) ; end for i =0:3 for j =0:3 if ( modulo ( i *j ,4) ==1) a ( i ) =1; end end end res =0; for i =1: max ( size ( a ) ) if ( a ( i ) ==1) res = res +1; end end if ( res ==4) disp ( Modulo 4 r i n g i s a f i e l d ) ; else disp ( Modulo 4 r i n g i s n o t a f i e l d ) ; end

135

Scilab code Exa 15.17 Finite Field


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

clc ; clear ; n =3; for i =1: n for j =1: n p (i , j ) = modulo ( i +j -2 ,3) ; end end disp ( modulo 3 A d d i t i o n ) ; disp ( p ) ; for i =1: n for j =1: n p (i , j ) = modulo (( i -1) *( j -1) ,3) ; end end disp ( modulo 3 M u l t i p l i c a t i o n ) ; disp ( p ) ;

136

Chapter 16 Finite State Recognizers

Scilab code Exa 16.2 Prove the identity


1 clc ; 2 clear ; 3 disp ( R1= + 1 ( 0 1 1 ) ( 1 ( 0 1 1 ) ) ) ; 4 // from t h e i d e n t i t y +RR=R where R= 1 ( 0 1 1 ) 5 disp ( R2=(1+011) ) ; 6 // from t h e i d e n t i t y (P+Q) =(P Q ) 7 disp ( R1= + 1 ( 0 1 1 ) ( 1 ( 0 1 1 ) ) ) ; 8 disp ( ( 1 ( 0 1 1 ) ) ) ; 9 disp ( ( 1 + 0 1 1 ) =R2 ) ;

Scilab code Exa 16.3 Prove the identity 2


1 clc ; 2 clear ; 3 disp ( ( 1 + 0 0 1 ) +(1+00 1) ( 0 + 1 0 1 ) ( 0 + 1 0 1 ) =0 1(0+10 1)

); 4 disp ( ( 1 + 0 0 1 ) +(1+00 1) ( 0 + 1 0 1 ) ( 0 + 1 0 1 ) ) ; 5 // by t a k i n g ( 1 + 0 0 1 ) a s common 137

6 7 8 9

disp ( =(1+00 1) [ +(0+10 1) ( 0 + 1 0 1 ) ] ) ; disp ( [ ( +00 ) 1 ] [ +(0+10 1) ( 0 + 1 0 1 ) ] ) ; // from t h e i d e n t i t y +RR=R disp ( [ 0 1 ( 0 + 1 0 1 ) ] ) ;

138

Appendix
Scilab code AP 1 Kmap Solver with out Dontcares
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

function []= karmap ( k ) n =4; k (: ,: ,2) = zeros (n , n ) ; var =[ y z w x ]; // v a r = [ w x y z ] ; p1 =[ y z y z yz yz ]; p2 =[ w x ; w x ; wx ; wx ]; cmn4 =4; cmn2 =2; temp =1; // 16 c e l l s for i =1: n for j =1: n if ( k (i , j ) ~=1) temp =0; break ; end end end if ( temp ==1) printf ( 1 ) ; abort ; end // 8 c e l l s z1 = ones (2 ,4) ; z2 = ones (4 ,2) ; 139

27 28 29 30 31 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

temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for i =1: n if ( i ==4) t =1; else t = i +1; end z =[ k (i ,: ,1) ; k (t ,: ,1) ]; if ( z == z1 ) k (i ,: ,2) =[1 1 1 1]; k (t ,: ,2) =[1 1 1 1]; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 (t ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; disp ( ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; disp ( ) ; break ; end end end end end for j =1: n if ( j ==4) t =1; else t = j +1; end z =[ k (: ,j ,1) k (: ,t ,1) ]; if ( z == z2 ) 140

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

k (: ,j ,2) =[1;1;1;1]; k (: ,t ,2) =[1;1;1;1]; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; disp ( ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; disp ( ) ; break ; end end end end end // 4 c e l l s z1 = ones (1 ,4) ; z2 = ones (4 ,1) ; z3 = ones (2 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for t =1: n z = k (t ,: ,1) ; no = number_of ( k (t ,: ,2) ,1) ; if ( z == z1 & no < cmn4 ) k (t ,: ,2) = z1 ; a = strsplit ( temp1 (1 , t ) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( a ( in ) == 1 ) printf ( var ( in ) ) ; 141

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

end end disp ( ) ; end end for t =1: n z = k (: ,t ,1) ; no = number_of ( k (: ,t ,2) ,1) ; if ( z == z2 & no < cmn4 ) k (: ,t ,2) = z2 ; a = strsplit ( temp2 (t ,1) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end disp ( ) ; end end for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2 ,1) ]; z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2 ,2) ]; 142

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

no = number_of ( z5 ,1) ; if ( z4 == z3 & no < cmn4 ) k (i ,j ,2) =1; k (i , t2 ,2) =1; k ( t1 ,j ,2) =1; k ( t1 , t2 ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end end a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end disp ( ) ; end end end // 2 c e l l s z6 =[1 1]; z7 = z6 ; for i =1: n for j =1: n if ( i == n ) 143

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z8 =[ k (i ,j ,1) k (i , t2 ,1) ]; z9 =[ k (i ,j ,2) k (i , t2 ,2) ]; no1 = number_of ( z9 ,1) ; if ( z8 == z6 & no1 < cmn2 & i + j ~=2) k (i ,j ,2) =1; k (i , t2 ,2) =1; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p1 (1 , i ) ) ; printf ( %s , var (2+ in ) ) ; disp ( ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( p1 (1 , i ) ) ; printf ( var (2+ in ) ) ; disp ( ) ; end end end end end for i =1: n for j =1: n if ( i == n ) t1 =1; else 144

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ]; z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ]; no2 = number_of ( z11 ,1) ; if ( z10 == z7 & no2 < cmn2 ) k (i ,j ,2) =1; k ( t1 ,j ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p2 (j ,1) ) ; printf ( %s , var ( in ) ) ; disp ( ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( p2 (j ,1) ) ; printf ( var ( in ) ) ; disp ( ) ; end end end end end // s i n g l e c e l l for i =1: n for j =1: n if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1) a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp2 (i ,1) ) ; for in =1: max ( size ( a (: ,1) ) ) 145

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

if ( a ( in ,1) == 1 ) printf ( var ( in +2) ) ; else if ( a ( in ,1) == 0 ) printf ( %s , var (2+ in ) ) ; end end end for in =1: max ( size ( b (: ,1) ) ) if ( b ( in ,1) == 1 ) printf ( var ( in ) ) ; else if ( b ( in ,1) == 0 ) printf ( %s , var ( in ) ) ; end end end if ( i ~=4 & j ~=4) disp ( ) ; end end end end endfunction Scilab code AP 2 No.of

1 2 3 4 5 6 7 8 9 10

// f i n d s t h e number o f z s i n t h e m a t r i x A function res = number_of (a , z ) res =0; for i =1: max ( size ( a (: ,1) ) ) for j =1: max ( size ( a (1 ,:) ) ) if ( a (i , j ) == z ) res = res +1; end end end 146

11

endfunction Scilab code AP 3 Kmap Solver with out Dontcares

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

function []= karmap1 ( k ) n =4; k (: ,: ,2) = zeros (n , n ) ; var =[ y z w x ]; p1 =[ y z y z yz yz ]; p2 =[ w x ; w x ; wx ; wx ]; cmn4 =1; cmn2 =1; temp =1; // 16 c e l l s for i =1: n for j =1: n if ( k (i , j ) ~=1) temp =0; break ; end end end if ( temp ==1) printf ( 1 ) ; abort ; end // 8 c e l l s z1 = ones (2 ,4) ; z2 = ones (4 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for i =1: n if ( i ==4) t =1; else t = i +1; end z =[ k (i ,: ,1) ; k (t ,: ,1) ]; 147

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

if ( z == z1 ) k (i ,: ,2) =[1 1 1 1]; k (t ,: ,2) =[1 1 1 1]; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 (t ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; disp ( ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; disp ( ) ; break ; end end end end end for j =1: n if ( j ==4) t =1; else t = j +1; end z =[ k (: ,j ,1) k (: ,t ,1) ]; if ( z == z2 ) k (: ,j ,2) =[1;1;1;1]; k (: ,t ,2) =[1;1;1;1]; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; disp ( ) ; 148

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; disp ( ) ; break ; end end end end end // 4 c e l l s z1 = ones (1 ,4) ; z2 = ones (4 ,1) ; z3 = ones (2 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for t =1: n z = k (t ,: ,1) ; no = number_of ( k (t ,: ,2) ,1) ; if ( z == z1 & no < cmn4 ) k (t ,: ,2) = z1 ; a = strsplit ( temp1 (1 , t ) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( a ( in ) == 1 ) printf ( var ( in ) ) ; end end disp ( ) ; end end for t =1: n z = k (: ,t ,1) ; no = number_of ( k (: ,t ,2) ,1) ; if ( z == z2 & no < cmn4 ) 149

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

k (: ,t ,2) = z2 ; a = strsplit ( temp2 (t ,1) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end disp ( ) ; end end for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2 ,1) ]; z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2 ,2) ]; no = number_of ( z5 ,1) ; if ( z4 == z3 & no < cmn4 ) k (i ,j ,2) =1; k (i , t2 ,2) =1; k ( t1 ,j ,2) =1; k ( t1 , t2 ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; 150

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end end a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end disp ( ) ; end end end // 2 c e l l s z6 =[1 1]; z7 = z6 ; for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end 151

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

z8 =[ k (i ,j ,1) k (i , t2 ,1) ]; z9 =[ k (i ,j ,2) k (i , t2 ,2) ]; no1 = number_of ( z9 ,1) ; if ( z8 == z6 & no1 < cmn2 ) k (i ,j ,2) =1; k (i , t2 ,2) =1; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p1 (1 , i ) ) ; printf ( %s , var (2+ in ) ) ; disp ( ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( p1 (1 , i ) ) ; printf ( var (2+ in ) ) ; disp ( ) ; end end end end end for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ]; z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ]; 152

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

no2 = number_of ( z11 ) ; if ( z10 == z7 & no2 < cmn2 ) k (i ,j ,2) =1; k ( t1 ,j ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p2 (j ,1) ) ; printf ( %s , var ( in ) ) ; disp ( ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( p2 (j ,1) ) ; printf ( var ( in ) ) ; disp ( ) ; end end end end end // s i n g l e c e l l for i =1: n for j =1: n if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1) a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp2 (i ,1) ) ; for in =1: max ( size ( a (: ,1) ) ) if ( a ( in ,1) == 1 ) printf ( var ( in +2) ) ; else if ( a ( in ,1) == 0 ) printf ( %s , var (2+ in ) ) ; end end end 153

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

for in =1: max ( size ( b (: ,1) ) ) if ( b ( in ,1) == 1 ) printf ( var ( in ) ) ; else if ( b ( in ,1) == 0 ) printf ( %s , var ( in ) ) ; end end end if ( i ~=4 & j ~=4) disp ( ) ; end end end end endfunction Scilab code AP 4 No.of 3d matrix

1 2 3 4 5 6 7 8 9 10 11 12 13

// f i n d s t h e number o f z s i n t h e 3 d i m e n s i o n a l matrix A function res = noof3 (a , z ) res =0; for i =1: max ( size ( a (: ,1 ,1) ) ) for j =1: max ( size ( a (1 ,: ,1) ) ) for l =1:2 if ( a (i ,j , l ) == z ) res = res +1; end end end end endfunction Scilab code AP 5 Check

1 2

function out = check (a ,b ,c , d ) for i =1: max ( size ( a ) ) ; 154

3 4 5 6 7 8 9

if ( a (i ,1) == b (i ,1) & b (i ,1) == c (i ,1) & c (i ,1) == d (i ,1) ) out ( i ) =0; else out ( i ) =1; end end endfunction Scilab code AP 6 Decimal to Base 2 Converter

// d e c 2 1 b i n i s a f u n c t i o n whcih c o n v e r t s any d e c i m a l number g i v e n t o i t w i l l o u t p u t i t s e q u i v a l e n t b i n a r y number 2 // p a s s t h e d e c i m a l number a s an argument t o t h e function 3 // For e g : d e c 2 1 b i n ( 1 0 ) 4 // W i l l g i v e an o u t p u t o f 1 0 1 0


1 5 6 7

function [ temp ]= dec21bin ( dec ) temp2 = floor ( dec ) ; // s e p a r a t i n g i n t e g e r p a r t from t h e g i v e n number temp4 = modulo ( dec ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number format ( v ,18) ; // c h a n g i n g t h e d e f a u l t p r e c i s i o n t o 18

9 10

11 12

i =1; p =0; x =1; // f l a g bits

13 14

while ( temp2 >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r f o r convenience 155

15 16 17 18 19 20 21 22

p ( i ) =( modulo ( floor ( temp2 ) ,2) ) temp2 = floor ( temp2 ) /2; i = i +1; end temp2 =0; // c l e a r i n g t h e t e m p o r a r y v a r i a b l e

temp2

23 24 25 26

for j =1: length ( p ) // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p a r t w i t h t h e i r p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( p ( j ) *10^( j -1) ) ; end while ( temp4 ~=0) // s t o r i n g each i n t e g e r d i g i t in v e c t o r f o r convenience temp4 = temp4 *2; d ( x ) = floor ( temp4 ) ; x = x +1; temp4 = modulo ( temp4 ,1) ; end temp5 =0; // c l e a r i n g t h e t e m p o r a r y v a r i a b l e

27 28 29 30 31 32 33 34 35

temp2

36 37 38 39

for j =1: x -1 // m u l t i p l i y i n g the b i t s o f decimal part with t h e i r p o s i t i o n v a l u e s and a d d i n g temp5 = temp5 +(10^( -1* j ) * d ( j ) ) end temp = temp2 + temp5 ;

// f i n a l l y a d d i n g b o t h t h e i n t e g e r and d e c i m a l parts to get t o t a l output . 40 endfunction

156

Scilab code AP 7 Kmap with dont cares


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

function []= donkmap (k , l ) n =4; // f o u r v a r i a b l e kmap k (: ,: ,2) = zeros (n , n ) ; // t e m p o r a r y m a t r i x t o know w h e t h e r a e l e m e n t i s p a i r e d o r n o t // d e c l a r i n g n o t a t i o n s t o d i s p l a y o u t p u t var =[ y z w x ]; p1 =[ y z y z yz yz ]; p2 =[ w x ; w x ; wx ; wx ]; // minimum r e d u n d a n t e l e m e n t s a c c e p t e d w h i l e pairing cmn4 =4; cmn2 =2; temp =1; printf ( f ) ; printf ( %1d ,l ) ; printf ( = ) ; // 16 c e l l s for i =1: n for j =1: n if ( k (i , j ) ~=1 | k (i , j ) ~=2) temp =0; break ; end end end if ( temp ==1) printf ( 1 ) ; abort ; end // 8 c e l l s z1 = ones (2 ,4) ; z2 = ones (4 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for i =1: n if ( i ==4) 157

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

t =1; else t = i +1; end z =[ k (i ,: ,1) ; k (t ,: ,1) ]; if ( number_of (z ,0) ==0 & number_of (z ,1) >1) k (i ,: ,2) =[1 1 1 1]; k (t ,: ,2) =[1 1 1 1]; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 (t ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; printf ( + ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; printf ( + ) ; break ; end end end end end for j =1: n if ( j ==4) t =1; else t = j +1; end z =[ k (: ,j ,1) k (: ,t ,1) ]; if ( number_of (z ,0) ==0 & number_of (z ,1) >0) k (: ,j ,2) =[1;1;1;1]; k (: ,t ,2) =[1;1;1;1]; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t ) ) ; 158

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; printf ( + ) ; break ; else if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; printf ( + ) ; break ; end end end end end // 4 c e l l s z1 = ones (1 ,4) ; z2 = ones (4 ,1) ; z3 = ones (2 ,2) ; temp1 =[ 00 01 11 10 ]; temp2 = temp1 ; for t =1: n z = k (t ,: ,1) ; no = number_of ( k (t ,: ,2) ,1) ; if ( number_of (z ,0) ==0 & no < cmn4 & number_of (z ,1) >0) k (t ,: ,2) = z1 ; a = strsplit ( temp1 (1 , t ) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( a ( in ) == 1 ) printf ( var ( in ) ) ; end end printf ( + ) ; 159

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

end end for t =1: n z = k (: ,t ,1) ; no = number_of ( k (: ,t ,2) ,1) ; if ( number_of (z ,0) ==0 & no < cmn4 & number_of (z ,1) >0) k (: ,t ,2) = z2 ; a = strsplit ( temp2 (t ,1) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end printf ( + ) ; end end for i =1: n for j =1: n if ( i == n ) t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2 ,1) ]; z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2 ,2) ]; no = number_of ( z5 ,1) ;

160

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

if ( number_of ( z4 ,0) ==0 & no < cmn4 & number_of ( z4 ,1) >0) k (i ,j ,2) =1; k (i , t2 ,2) =1; k ( t1 ,j ,2) =1; k ( t1 , t2 ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var ( in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var ( in ) ) ; end end a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (2+ in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (2+ in ) ) ; end end printf ( + ) ; end end end // 2 c e l l s z6 =[1 1]; z7 = z6 ; for i =1: n for j =1: n if ( i == n ) 161

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

t1 =1; else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z8 =[ k (i ,j ,1) k (i , t2 ,1) ]; z9 =[ k (i ,j ,2) k (i , t2 ,2) ]; no1 = number_of ( z9 ,1) ; if ( number_of ( z8 ,0) ==0 & no1 < cmn2 & number_of ( z8 ,1) >0) k (i ,j ,2) =1; k (i , t2 ,2) =1; a = strsplit ( temp1 (1 , j ) ) ; b = strsplit ( temp1 (1 , t2 ) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p1 (1 , i ) ) ; printf ( %s , var (2+ in ) ) ; printf ( + ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( p1 (1 , i ) ) ; printf ( var (2+ in ) ) ; printf ( + ) ; end end end end end for i =1: n for j =1: n if ( i == n ) t1 =1; 162

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

else t1 = i +1; end if ( j == n ) t2 =1; else t2 = j +1; end z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ]; z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ]; no2 = number_of ( z11 ,1) ; if ( number_of ( z10 ,0) ==0 & no2 < cmn2 & number_of ( z10 ,1) >0) k (i ,j ,2) =1; k ( t1 ,j ,2) =1; a = strsplit ( temp2 (i ,1) ) ; b = strsplit ( temp2 ( t1 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( p2 (j ,1) ) ; printf ( %s , var ( in ) ) ; printf ( + ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( p2 (j ,1) ) ; printf ( var ( in ) ) ; printf ( + ) ; end end end end end // s i n g l e c e l l for i =1: n for j =1: n if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1) a = strsplit ( temp1 (1 , j ) ) ; 163

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

b = strsplit ( temp2 (i ,1) ) ; for in =1: max ( size ( a (: ,1) ) ) if ( a ( in ,1) == 1 ) printf ( var ( in +2) ) ; else if ( a ( in ,1) == 0 ) printf ( %s , var (2+ in ) ) ; end end end for in =1: max ( size ( b (: ,1) ) ) if ( b ( in ,1) == 1 ) printf ( var ( in ) ) ; else if ( b ( in ,1) == 0 ) printf ( %s , var ( in ) ) ; end end end if ( i ~=4 & j ~=4) printf ( + ) ; end end end end printf ( 0 ) ; disp ( ) endfunction Scilab code AP 8 Kmap for 3 variables with out dontcares

1 2 3 4 5 6

function []= karmap3 ( k ) n =4; // t h r e e v a r i a b l e kmap m =2 k (: ,: ,2) = zeros (m , n ) ; var =[ z x y ]; p1 =[ z z ]; 164

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

p2 =[ x y ; x y ; xy ; xy ]; cmn4 =4; cmn2 =3; temp =1; printf ( The m i n i m a l e c p r e s s i o n o f t h e g i v e n Kmap ); disp ( k (: ,: ,1) ) ; disp ( i s : ) ; printf ( f ) ; printf ( = ) ; // 8 c e l l s for i =1: m for j =1: n if ( k (i , j ) ~=1 & k (i , j ) ~=2) temp =0; break ; end end end if ( temp ==1) printf ( 1 ) ; abort ; end // 4 c e l l s z1 = ones (1 ,4) ; z2 = ones (4 ,1) ; z3 = ones (2 ,2) ; temp1 =[ 0 1 ]; temp2 =[ 00 ; 01 ; 11 ; 10 ]; for t =1: m z = k (t ,: ,1) ; no = number_of ( k (t ,: ,2) ,1) ; if ( number_of (z ,0) ==0 & no < cmn4 & number_of (z ,1) >0) k (t ,: ,2) = z1 ; a = strsplit ( temp1 (1 , t ) ) ; for in =1: max ( size ( a ) ) if ( a ( in ) == 0 ) 165

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 73 74 75 76 77

printf ( %s , var ( in ) ) ; end if ( a ( in ) == 1 ) printf ( var ( in ) ) ; end end printf ( + ) ; end end for i =1: m -1 for j =1: n t1 = i +1; if ( j == n ) t2 =1; else t2 = j +1; end z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2 ,1) ]; z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2 ,2) ]; no = number_of ( z5 ) ; if ( number_of ( z4 ,0) ==0 & no < cmn4 & number_of ( z4 ,1) >0) k (i ,j ,2) =1; k (i , t2 ,2) =1; k ( t1 ,j ,2) =1; k ( t1 , t2 ,2) =1; a = strsplit ( temp2 (j ,1) ) ; b = strsplit ( temp2 ( t2 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (1+ in ) ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (1+ in ) ) ; end 166

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

end printf ( + ) ; end end end // 2 c e l l s z6 =[1 1]; z7 = z6 ; for i =1: m for j =1: n t1 = i +1; if ( j == n ) t2 =1; else t2 = j +1; end z8 =[ k (i ,j ,1) k (i , t2 ,1) ]; z9 =[ k (i ,j ,2) k (i , t2 ,2) ]; no1 = number_of ( z9 ,1) ; if ( number_of ( z8 ,0) ==0 & no1 < cmn2 & number_of ( z8 ,1) >0) k (i ,j ,2) =1; k (i , t2 ,2) =1; printf ( p1 (1 , i ) ) ; a = strsplit ( temp2 (j ,1) ) ; b = strsplit ( temp2 ( t2 ,1) ) ; c = strcmp (a , b ) ; for in =1: max ( size ( c ) ) if ( c ( in ) ==0 & a ( in ) == 0 ) printf ( %s , var (1+ in ) ) ; printf ( + ) ; end if ( c ( in ) ==0 & a ( in ) == 1 ) printf ( var (1+ in ) ) ; printf ( + ) ; end end end 167

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

end end for i =1: m -1 for j =1: n t1 = i +1; if ( j == n ) t2 =1; else t2 = j +1; end z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ]; z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ]; no2 = number_of ( z11 ,1) ; if ( number_of ( z10 ,0) ==0 & no2 < cmn2 & number_of ( z10 ,1) >0) k (i ,j ,2) =1; k ( t1 ,j ,2) =1; printf ( p2 (j ,1) ) ; printf ( + ) ; end end end // s i n g l e c e l l for i =1: m for j =1: n if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1) printf ( p1 (1 , i ) ) ; printf ( p2 (j ,1) ) ; printf ( + ) ; end end end printf ( 0 ) ; disp ( ) endfunction Scilab code AP 9 Binary to Decimal convertor

168

// b i n 2 1 d e c i s a f u n c t i o n whcih c o n v e r t s any b i n a r y number g i v e n t o i t w i l l o u t p u t i t s e q u i v a l e n t d e c i m a l number 2 // p a s s t h e b i n a r y number a s an argument t o t h e function 3 // For e g : b i n 2 1 d e c i m a l ( 1 0 1 0 ) 4 // W i l l g i v e an o u t p u t o f 10


1 5 6 7 8 9

function [ temp ]= bin21dec ( bin ) i =1; w =1; temp1 = floor ( bin ) ; // s e p a r a t i n g i n t e g e r p a r t from t h e g i v e n number temp2 = modulo ( bin ,1) ; // s e p a r a t i n g d e c i m a l p a r t from t h e g i v e n number temp2 = temp2 *10^3; // c o n v e r t i n g decimal value to i n t e r g e r f o r convenience while ( temp1 >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r f o r convenience p ( i ) = modulo ( temp1 ,10) ; temp1 = floor ( temp1 /10) ; i = i +1; end while ( temp2 >0) // s t o r i n g e a c h i n t e g e r d i g i t i n v e c t o r f o r convenience q ( w ) = modulo ( temp2 ,2) ; temp2 =( temp2 /10) ; temp2 = floor ( temp2 ) ; w = w +1; end

10

11

12 13

14 15 16 17 18 19

20 21 22 23 24 25

169

26 27 28 29 30 31 32 33 34 35

temp1 =0; // c l e a r i n g t h e t e m p o r a r y v a r i a b l e

temp2

for i =1: length ( p ) // c h e c k i n g w h e t h e r i t i s b i n a r y o r n o t . if ( p ( i ) >1) then disp ( n o t a b i n a r y number ) ; abort ; end end for i =1: length ( p ) // m u l t i p l i y i n g t h e b i t s o f i n t e g e r p a r t w i t h t h e i r p o s i t i o n v a l u e s and a d d i n g temp1 = temp1 +( p ( i ) *2^( i -1) ) ; end temp2 =0; // c l e a r i n g t h e t e m p o r a r y v a r i a b l e

36 37 38 39 40 41

temp2

temp = temp1 + temp2 ; // f i n a l l y a d d i n g b o t h t h e i n t e g e r and d e c i m a l parts to get t o t a l output . 46 endfunction

42 43 44 45

for z =1: w -1 // m u l t i p l i y i n g t h e b i t s o f d e c i m a l p a r t w i t h t h e i r p o s i t i o n v a l u e s and a d d i n g temp2 = temp2 +( q ( z ) *2^( -1*(4 - z ) ) ) ; end

170

You might also like