Prima&esercitazione&
Prima&esercitazione&
Riccardo(Ca*aneo(
Dipar/mento(di(Ele*ronica,(Informazione(e(Biomedica(( Politecnico(di(Milano&
Prima&esercitazione&
Prima&esercitazione&
Riccardo(Ca*aneo(
(
Dipar/mento(di(Ele*ronica,(Informazione(e(Biomedica(
Politecnico(di(Milano&
1
Algoritmi Iterativi con Cicli, Logica Booleana e Mappe di Karnaugh
Alessandro A. Nacci
alessandro.nacci@polimi.it
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
2
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Trovare il maggiore tra N numeri positivi inseriti da tastiera
Pseudocodice:
• Richiedere quanti numeri si vogliono inserire
• Acquisire il numero di numeri da inserire (= contatore)
• Finché ho ancora numeri da inserire
• Richiedo un numero
• Acquisisco il numero
• se è il più grande che ho visto fino ad ora
• è il mio nuovo massimo
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
3
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
4
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
5
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
6
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
7
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
8
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
9
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
Si può fare in un altro modo? 10
(Scorsa esercitazione) Esercizio 5: trova il maggiore
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
11
Il ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
12
Il ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
13
Il ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
14
Esercizio 1: trova il maggiore con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
Esercizio 1: trova il maggiore con ciclo for
15
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
16
Esercizio 1: trova il maggiore con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
17
Esercizio 1: trova il maggiore con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
18
Esercizio 1: trova il maggiore con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
19
Esercizio 1: trova il maggiore con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
20
Cicli a confronto
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
21
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
22
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
23
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
24
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
25
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
26
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
27
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
28
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
29
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
30
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
31
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
32
Esercizio 2: fattoriale con ciclo for
Prima&esercitazione&
2
Riccardo Cattaneo
- Studente PhD al NECSTLab
- Autonomic (Operating) Systems (CHANGE project)
- Reconfigurable architectures for HPC systems (FASTER project)
- exaFPGA (accelerazione hardware)
Ciao!
33
Esercizio 2: fattoriale con ciclo while?
34
Logica Booleana e
Mappe di Karnaugh
George Boole
George Boole (Lincoln, 2
novembre 1815[1] – Ballintemple, 8 dicembre 1864[1]) è stato un
matematico e logico britannico, ed è considerato il fondatore della
logica matematica[1]. La sua opera influenzò anche settori della
filosofia e diede vita alla scuola degli algebristi della logica.
35
Le operazioni logiche (booleane)
36
A B A and B
F F F
F V F
V F F
V V V
A B A or B
F F F
F V V
V F V
V V V
A not A
F V
V F
Operazione “and” Operazione “or”
Operazione “not”
Ci sono altri operatori?
Le operazioni logiche (booleane) - 2
37
Un riassunto non esaustivo su…
https://it.wikipedia.org/wiki/Algebra_di_Boole#NOT
Proprietà logiche
38
Dimostrazione della proprietà di
39
Dimostrazione
Dimostrazione
Leggi di De Morgan
40
Esercizi sulla Algebra di Boole
ATTENZIONE!
Questi li facciamo alla lavagna ;) Trovate delle scansioni sul mio
sito internet alessandronacci.it
41
Esercizio sulla Algebra di Boole
42
AXO – esame di giovedì 4 marzo 2010 - CON SOLUZIONI pagina 13 di 22
seconda parte – algebra booleana
Si consideri la funzione booleana di 3 variabili G(a ,b, c) espressa dall’equazione seguente:
G(a, b, c) = a b c + !a !b c + !a b c + a b !c
Si trasformi - tramite le proprietà dell’algebra di commutazione - l’equazione di G in modo da ridurre il co- sto della sua realizzazione, indicando le singole operazioni svolte e il nome oppure la forma della proprietà utilizzata.
Soluzione via breve
a b c + !a !b c + !a b c + a b !c distributiva
!a c (b + !b) + a b (c + !c) elemento neutro
!a c 1 + a b 1 elemento neutro
!a c + a b forma minima
oppure (ma è più lunga)
a b c + !a !b c + !a b c + a b !c
a b c + !a !b c + !a b c + a b !c + a b c + !a b c idempotenza a b c + !a b c + !a b c + !a !b c + a b c + a b !c commutativa (a + !a) b c + !a (b + !b) c + a b (c + !c) distributiva
1 b c + !a 1 c + a b 1 elemento neutro
b c + !a c + a b elemento neutro
1 b c + !a c + a b elemento inverso
(a + !a) b c + !a c + a b distributiva
a b c + !a b c + !a c + a b commutativa
a b c + a b + !a b c + !a c assorbimento
a b + !a c forma minima
Esercizio sulla Algebra di Boole (soluzione)
43
AXO – esame di giovedì 4 marzo 2010 - CON SOLUZIONI pagina 13 di 22
seconda parte – algebra booleana
Si consideri la funzione booleana di 3 variabili G(a ,b, c) espressa dall’equazione seguente:
G(a, b, c) = a b c + !a !b c + !a b c + a b !c
Si trasformi - tramite le proprietà dell’algebra di commutazione - l’equazione di G in modo da ridurre il co- sto della sua realizzazione, indicando le singole operazioni svolte e il nome oppure la forma della proprietà utilizzata.
Soluzione via breve
a b c + !a !b c + !a b c + a b !c distributiva
!a c (b + !b) + a b (c + !c) elemento neutro
!a c 1 + a b 1 elemento neutro
!a c + a b forma minima
oppure (ma è più lunga)
a b c + !a !b c + !a b c + a b !c
a b c + !a !b c + !a b c + a b !c + a b c + !a b c idempotenza a b c + !a b c + !a b c + !a !b c + a b c + a b !c commutativa (a + !a) b c + !a (b + !b) c + a b (c + !c) distributiva
1 b c + !a 1 c + a b 1 elemento neutro
b c + !a c + a b elemento neutro
1 b c + !a c + a b elemento inverso
(a + !a) b c + !a c + a b distributiva
a b c + !a b c + !a c + a b commutativa
a b c + a b + !a b c + !a c assorbimento
a b + !a c forma minima
Esercizio sulla Algebra di Boole (soluzione)
44
AXO – esame di giovedì 4 marzo 2010 - CON SOLUZIONI pagina 13 di 22
seconda parte – algebra booleana
Si consideri la funzione booleana di 3 variabili G(a ,b, c) espressa dall’equazione seguente:
G(a, b, c) = a b c + !a !b c + !a b c + a b !c
Si trasformi - tramite le proprietà dell’algebra di commutazione - l’equazione di G in modo da ridurre il co- sto della sua realizzazione, indicando le singole operazioni svolte e il nome oppure la forma della proprietà utilizzata.
Soluzione via breve
a b c + !a !b c + !a b c + a b !c distributiva
!a c (b + !b) + a b (c + !c) elemento neutro
!a c 1 + a b 1 elemento neutro
!a c + a b forma minima
oppure (ma è più lunga)
a b c + !a !b c + !a b c + a b !c
a b c + !a !b c + !a b c + a b !c + a b c + !a b c idempotenza a b c + !a b c + !a b c + !a !b c + a b c + a b !c commutativa (a + !a) b c + !a (b + !b) c + a b (c + !c) distributiva
1 b c + !a 1 c + a b 1 elemento neutro
b c + !a c + a b elemento neutro
1 b c + !a c + a b elemento inverso
(a + !a) b c + !a c + a b distributiva
a b c + !a b c + !a c + a b commutativa
a b c + a b + !a b c + !a c assorbimento
a b + !a c forma minima
MOLTO CARIN O MA…
Troppo intuiti vo! Ergo, poco automatizzati le…
Esiste quindi un metodo pi ù meccanico?
Mappe di Karnaugh (1)
45
f (a, b, c) =
∑
(001, 011,101,110,111) 0 0 0 0 10 0 0 1 1 0 0 1 0 1 0 0 1 1 0 0 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1
a b c d f
00 01 11 10 00 1 1 0 0
1 1 1 1 0 0 1 1 1 0 0 1 c,da,b
Mappe di Karnaugh (2)
46
f (a, b, c) =
∑
(001, 011,101,110,111) 0 0 0 0 10 0 0 1 1 0 0 1 0 1 0 0 1 1 0 0 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1
a b c d f
a'c'; ad
a'b'd'; b'cd'; ab'c; c'd
00 01 11 10 00
01 11 10
1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 1 c,da,b
Implicanti primi essenziali
Implicanti primi
Completamente ridondante
Un implicante si dice implicante primo essenziale se esiste almeno un mintermine che non è coperto da nessun altro implicante primo.
Mappe di Karnaugh (3)
47
a'c'; ad
a'b'd'; b'cd'; ab'c; c'd
00 01 11 10 00
01 11 10
0 0 0 0 1 0 0 1 c,d a,b
Implicanti primi essenziali
Implicanti primi
f(a,b,c,d)= a'c'+ad+b'cd‘
Forma minima (unica)
Parzialmente ridondanti
Tabella ottenuta dopo la selezione degli implicanti primi essenziali
1 da coprire
Un implicante si dice implicante primo essenziale se esiste almeno un mintermine che non è coperto da nessun altro implicante primo.
Mappe di Karnaugh (4)
48
( )
∑
= 0,2,4,5,10,11,13,15 )
, , ,
(a b c d f
Nessuno
a'c'd'; bc’d; acd; b’cd’;
a’b’d’; a’bc’; abd; ab’c
00 01 11 10 00
01 11 10
1 1 0 0 0 1 1 0 0 0 1 1 1 0 0 1 c,da,b
Implicanti primi essenziali Implicanti primi
f(a,b,c,d)= a'c'd'+ bc’d+ acd+ b’cd’
f(a,b,c,d)= a’b’d’+ a’bc’+ abd+ ab’c Due forme minime
a b c d f 0 0 0 0 1 0 0 0 1 0 0 0 1 0 1 0 0 1 1 0 0 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 1 1 0 1 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1
Un implicante si dice implicante primo essenziale se esiste almeno un mintermine che non è coperto da nessun altro implicante
primo.
Mappe di Karnaugh (5)
• Online trovate una dispensa che le spiega con un altro esempio! :)
49
50