2. Introduction
Symbolic computation deals with the computation of mathematical objects symbolically. This means that the mathematical objects
are represented exactly, not approximately, and mathematical expressions with unevaluated variables are left in symbolic form.
It Covers the following:
• As A calculator and symbols
• Algebraic Manipulations - Expand and Simplify
• Calculus – Limits, Differentiation, Series , Integration
• Equation Solving – Matrices
3. Calculator and Symbols
Rational - ½, or 5/2
>>import sympy as sym
>>a = sym.Rational(1, 2)
>>a Answer will be 1/2
Constants like pi,e
>>sym.pi**2 Answer is pi**2
>>sym.pi.evalf() Answer is 3.14159265358979
>> (sym.pi + sym.exp(1)).evalf() Answer is 5.85987448204884
X AND Y
>> x = sym.Symbol('x')
>>y = sym.Symbol('y')
>>x + y + x – y Answer is 2*x
4. Algebraic Manipulations
EXPAND ( X+Y)**3 = X+3X^2Y+3XY^2+Y
>> sym.expand((x + y) ** 3) Answer is x**3 + 3*x**2*y + 3*x*y**2 + y**3
>> 3 * x * y ** 2 + 3 * y * x ** 2 + x ** 3 + y ** 3 Answer is x**3 + 3*x**2*y + 3*x*y**2 + y**3
WITH TRIGNOMETRY LIKE SIN,COSINE
eg. COS(X+Y)= - SIN(X)*SIN(Y)+COS(X)*COS(Y)
>> sym.expand(sym.cos(x + y), trig=True) Answer is -sin(x)*sin(y) + cos(x)*cos(y)
SIMPLIFY
(X+X*Y/X)=Y+1
>>sym.simplify((x + x * y) / x) Answer is: y+1
12. Introduction
Automata-based programming is a programming paradigm in which the program or its part is thought of as a model of a finite state
machine or any other formal automation.
What is Automata Theory?
• Automata theory is the study of abstract computational devices
• Abstract devices are (simplified) models of real computations
• Computations happen everywhere: On your laptop, on your cell phone, in nature, …
Example:
input: switch
output: light bulb
actions: flip switch
states: on, off
BATTER
Y
14. Another “computer”
Example:
inputs: switches 1 and 2
actions: 1 for “flip switch 1”
actions: 2 for “flip switch 2”
states: on, off
bulb is on if and only if both switches were flipped an odd
number of times
BATTERY
1
2
of
f
of
f
star
t 1
of
f
o
n
1
1
2 2 2 2
15. Types of Automata
finite automata Devices with a finite amount of memory.
Used to model “small” computers.
push-down
automata
Devices with infinite memory that can be
accessed in a restricted way.
Used to model parsers, etc.
Turing
Machines
Devices with infinite memory.
Used to model any computer.
16. Alphabets and strings
A common way to talk about words, number, pairs of words, etc. is by representing them as strings
To define strings, we start with an alphabet
Examples:
An alphabet is a finite set of
symbols.
Σ1 = {a, b, c, d, …, z}: the set of letters in English
Σ2 = {0, 1, …, 9}: the set of (base 10) digits
Σ3 = {a, b, …, z, #}: the set of letters plus the special symbol #
Σ4 = {(, )}: the set of open and closed brackets
17. Strings
The empty string will be denoted by e
Examples:
A string over alphabet Σ is a finite sequence of symbols
in Σ.
abfbz is a string over Σ1 = {a, b, c, d, …,
z}
9021 is a string over Σ2 = {0, 1, …, 9}
ab#bc is a string over Σ3 = {a, b, …, z,
#}
))()(() is a string over Σ4 = {(, )}
18. Languages
Languages can be used to describe problems with “yes/no” answers, for example:
A language is a set of strings over an alphabet.
L1 = The set of all strings over Σ1 that contain the substring “SRM”
L2 = The set of all strings over Σ2 that are divisible by 7 = {7, 14, 21, …}
L3 = The set of all strings of the form s#s where s is any string over {a, b, …,
z}
L4 = The set of all strings over Σ4 where every ( can be matched with a
subsequent )
19. Finite Automata
There are states off and on, the automaton starts in off and tries to reach the “good state” on
What sequences of fs lead to the good state?
Answer: {f, fff, fffff, …} = {f n: n is odd}
This is an example of a deterministic finite automaton over alphabet {f}
off on
f
f
20. Deterministic finite automata
• DFA refers to deterministic finite automata. The finite automata are called
deterministic finite automata if the machine is read an input string one symbol at a
time.
• In DFA, there is only one path for specific input from the current state to the next
state.
• DFA does not accept the null move, i.e., the DFA cannot change state without any
input character.
• DFA can contain multiple final states.
21. Deterministic finite automata
• A deterministic finite automaton (DFA) is a 5-tuple (Q, Σ, δ, q0, F) where
• Q is a finite set of states
• Σ is an alphabet
• δ: Q × Σ → Q is a transition function
• q0 ∈ Q is the initial state
• F ⊆ Q is a set of accepting states (or final states).
• In diagrams, the accepting states will be denoted by double loops
22. Example
DFA with ∑ = {0, 1} accepts all starting with 0.
Solution:
DFA with ∑ = {0, 1} accepts all ending with 0.
Solution:
23. Example
q0 q1 q2
1 0
0 0,1
1
alphabet Σ = {0, 1}
start state Q = {q0, q1, q2}
initial state q0
accepting states F = {q0, q1}
transition function
δ:
state
s
0 1
q0
q1
q2
q0 q1
q2
q2
q2
q1
input
s
24. Language of a DFA
The language of a DFA (Q, Σ, δ, q0, F) is the set of all strings over Σ that,
starting from q0 and following the transitions as the string is read left to
right, will reach some accepting state.
•Language of M is {f, fff, fffff, …} = {f n
: n is odd}
off on
f
f
M:
25. Example of DFA
1. Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ*
, and Σ+
.
For {}: For {ε}:
For Σ*
: For Σ+
:
q0
0/
1
0/
1
q
0
q1
q
0
0/
1
0/
1
0/1
q0
q
1
0/
1
27. Example of DFA
Build an automaton that accepts all and only those strings that contain 001
q q00
1 0
1
q0 q001
0 0 1
0,1
28. Example of DFA using Python
from automata.fa.dfa import DFA
# DFA which matches all binary strings ending in an odd number of '1's
dfa = DFA(
states={'q0', 'q1', 'q2'},
input_symbols={'0', '1'},
transitions={
'q0': {'0': 'q0', '1': 'q1'},
'q1': {'0': 'q0', '1': 'q2'},
'q2': {'0': 'q2', '1': 'q1'}
},
initial_state='q0',
final_states={'q1'}
)
dfa.read_input('01') # answer is 'q1‘
dfa.read_input('011') # answer is error
print(dfa.read_input_stepwise('011'))
Answer # yields:
# 'q0‘ # 'q0‘ # 'q1'
# 'q2‘ # 'q1'
if dfa.accepts_input('011'):
print('accepted')
else:
print('rejected')
29. Questions for DFA
b) Find a DFA for the language of a + aa*b.
Find an DFA for each of the following languages over the alphabet {a, b}.
(a) {(ab)n
| n ∈ N}, which has regular expression (ab)*.
Start 0
1
4
a,
b
a
a
a,
b
b
a
b b
2
3
Solution
:
(a): Start
b a, b
a
a
b
Solution
:
30. Questions for DFA
c) A DFA that accepts all strings that contain 010 or do not contain 0.
1
0
0
0
1
1 0, 1
0
1
31. Table Representation of a DFA
A DFA over A can be represented by a transition function T : States X A -> States, where T(i, a) is the state
reached from state i along the edge labelled a, and we mark the start and final states. For example, the
following figures show a DFA and its transition table.
Start 0
1
2
a,
b a
b
a,
b
32. Sample Exercises - DFA
1. Write a automata code for the Language that accepts all and only those strings that
contain 001
2. Write a automata code for L(M) ={ w | w has an even number of 1s}
3. Write a automata code for L(M) ={0,1}*
4. Write a automata code for L(M)=a + aa*b.
5. Write a automata code for L(M)={(ab)n
| n N}
∈
6. Write a automata code for Let Σ = {0, 1}.
Given DFAs for {}, {ε}, Σ*
, and Σ+
.
33. NDFA
• In any DFA, the next state the machine goes to on any given symbol is uniquely determined. This is why these machines are
deterministic.
• Nondeterministic finite automata (NFAs) allow for several or no choices to exist for the next state on a given symbol
• Remember that the transition function in a DFA is defined as
• δ : Q × Σ → Q.
• Because range of δ is Q, fcn δ always returns a single state.
• DFA has exactly one transition leaving each state for each symbol.
• δ(q, λ) tells what state the edge out of q labeled with λ leads to.
34. NDFA
• For a state q and symbol λ Σ, NFA can have
∈
• multiple edges leaving q labelled with the same symbol λ
• no edge leaving q labelled with symbol λ
• edges leaving q labelled with ε
• can take ε-edge without reading any symbol from input string.
• Suppose NFA is in a state with multiple ways to proceed, e.g., in state q1 and the next symbol in input string is 1. NFA may
be in a set of states, instead of a single state.
35. NDFA
• A nondeterministic finite automaton M is a five-tuple M = (Q, Σ, δ, q0, F), where:
• Q is a finite set of states of M
• Σ is the finite input alphabet of M
• δ: Q × Σ → power set of Q, is the state transition function mapping a state-symbol pair to
a subset of Q
• q0 is the start state of M
• F Q is the set of accepting states or final states of M
⊆
36. NFA Differs over DFA
1. Transition of (null) or lambda is allowed in NFA
∈
2. NFA on reading one input it can transition to two different states
3. Dead configuration is possible in NFA (Its not necessary to consider all input for transition)
37. Example NDFA
• NFA that recognizes the language of strings that end in 01
Exercise: Draw the complete
transition table for this NFA
q0
q2
0,1
0 1
q1
note: δ(q0,0) = {q0,q1}
δ(q1,0) = {}
38. NDFA
A nondeterministic finite automaton (NFA) over an alphabet A is similar to a DFA except that epislon-edges are
allowed, there is no requirement to emit edges from a state, and multiple edges with the same letter can be
emitted from a state.
Example. The following NFA recognizes the language of a + aa*b + a*b.
Table representation of NFA
An NFA over A can be represented by a function T : States × A {L} → power(States), where T(i, a) is the set of
∪
states reached from state i along the edge labeled a, and we mark the start and final states. The following figure
shows the table for the preceding NFA.
Start 0 2
1
a
a
ɛ
a
b
39. Examples
Start
b
a
(c):
Find an NFA to recognize the language (a + ba)*bb(a + ab)*.
A solution:
Start
a
b
a
b
b
a
a
b
Start
Solutions: (a):
Start
(b)
:
40. Examples
40
Algorithm: Transform a Regular Expression into a Finite Automaton
Start by placing the regular expression on the edge between a start and final state:
Regular
expression
Start
Apply the following rules to obtain a finite automaton after erasing any -edges.
∅
R + S
i j
R
i j
S
transforms
to
RS
i j i j
R S
transforms
to
R*
i j i j
ɛ
R
transforms
to
Quiz. Use the algorithm to construct a finite automaton for (ab)* + ba.
Start
a
b
b a
Answer:
ɛ
ɛ
ɛ
41. Example of NFA using Python
from automata.fa.nfa import NFA
# NFA which matches strings beginning with 'a', ending with 'a', and
containing
# no consecutive 'b's
nfa = NFA(
states={'q0', 'q1', 'q2'},
input_symbols={'a', 'b'},
transitions={
'q0': {'a': {'q1'}},
# Use '' as the key name for empty string (lambda/epsilon)
transitions
'q1': {'a': {'q1'}, '': {'q2'}},
'q2': {'b': {'q0'}}
},
initial_state='q0',
final_states={'q1'}
)
nfa.read_input('aba')
ANSWER :{'q1', 'q2'}
nfa.read_input('abba')
ANSWER: ERROR
nfa.read_input_stepwise('aba')
if nfa.accepts_input('aba'):
print('accepted')
else:
print('rejected')
ANSWER: ACCEPTED
nfa.validate()
ANSWR: TRUE
42. 42
Algorithm: Transform a Regular Expression into a Finite Automaton
Start by placing the regular expression on the edge between a start and final state:
Regular
expression
Start
Apply the following rules to obtain a finite automaton after erasing any -edges.
∅
R + S
i j
R
i j
S
transforms
to
RS
i j i j
R S
transforms
to
R*
i j i j
ɛ
R
transforms
to
Quiz. Use the algorithm to construct a finite automaton for (ab)* + ba.
Star
t
a
b
b a
Answe
r:
ɛ
ɛ
ɛ
43. Example of NFA using Python
from automata.fa.nfa import NFA
# NFA which matches strings beginning with 'a', ending with 'a', and
containing no consecutive 'b's
nfa = NFA(
states={'q0', 'q1', 'q2'},
input_symbols={'a', 'b'},
transitions={
'q0': {'a': {'q1'}},
# Use '' as the key name for empty string (lambda/epsilon)
transitions
'q1': {'a': {'q1'}, '': {'q2'}},
'q2': {'b': {'q0'}}
},
initial_state='q0',
final_states={'q1'}
)
nfa.read_input('aba')
ANSWER :{'q1', 'q2'}
nfa.read_input('abba')
ANSWER: ERROR
nfa.read_input_stepwise('aba')
if nfa.accepts_input('aba'):
print('accepted')
else:
print('rejected')
ANSWER: ACCEPTED
nfa.validate()
ANSWR: TRUE
44. Sample Exercises - NFA
1. Write a automata code for the Language that accepts all end with 01
2. Write a automata code for L(M)= a + aa*b + a*b.
3. Write a automata code for Let Σ = {0, 1}.
Given NFAs for {}, {ε}, {(ab)n
| n N}, which has regular expression (
∈ ab)*.