0% found this document useful (0 votes)
87 views24 pages

App SRM Unit 5 Notes

Symbolic programming allows programs to manipulate their own components as data. This enables programs to modify themselves and effectively learn through combining smaller logic units. Key applications include artificial intelligence, expert systems, natural language processing, and computer games. SymPy is a Python library that supports symbolic mathematics, allowing symbolic expressions to be evaluated, differentiated, integrated, expanded and factorized among other operations. Finite state automata are abstract machines that recognize patterns through a set of states and transition rules depending on input symbols. Deterministic finite automata have a single transition for each state-symbol pair, while non-deterministic automata allow multiple transitions.

Uploaded by

Harshit Batra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views24 pages

App SRM Unit 5 Notes

Symbolic programming allows programs to manipulate their own components as data. This enables programs to modify themselves and effectively learn through combining smaller logic units. Key applications include artificial intelligence, expert systems, natural language processing, and computer games. SymPy is a Python library that supports symbolic mathematics, allowing symbolic expressions to be evaluated, differentiated, integrated, expanded and factorized among other operations. Finite state automata are abstract machines that recognize patterns through a set of states and transition rules depending on input symbols. Deterministic finite automata have a single transition for each state-symbol pair, while non-deterministic automata allow multiple transitions.

Uploaded by

Harshit Batra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Symbolic programming paradigm

• In computer programming, symbolic programming is a programming


paradigm in which the program can manipulate its own formulas and
program components as if they were plain data.
• Through symbolic programming, complex processes can be developed that
build other more intricate processes by combining smaller units of logic or
functionality.
• Thus, such programs can effectively modify themselves and appear to
"learn", which makes them better suited for applications such as artificial
intelligence, expert systems, natural language processing, and computer
games.
Symbolic language (programming)
• In computer science, a symbolic language is a language that uses
characters or symbols to represent concepts, such as mathematical
operations and the entities (or operands) on which these operations are
performed.
• Modern programming languages use symbols to represent concepts
and/or data and are therefore, examples of symbolic languages.
• Some programming languages (such as Lisp and Mathematica) make
it easy to represent higher-level abstractions as expressions in the
language, enabling symbolic programming
Sympy : Symbolic Mathematics in Python
• SymPy defines three numerical types: Real, Rational and Integer.
import sympy as sym
sym.pi**2
----pi**2
sym.pi.evalf()
-----3.14159265358979
(sym.pi + sym.exp(1)).evalf()
---------5.8598744820488
x = sym.Symbol('x')
y = sym.Symbol('y’)
x+y+x-y
2*x
(x + y) ** 2
(x + y)**2
Algebraic manipulations
sym.expand(x + y, complex=True)
re(x) + re(y) + I*im(x) + I*im(y)
>>> sym.I * sym.im(x) + sym.I * sym.im(y) + sym.re(x) + sym.re(y)
re(x) + re(y) + I*im(x) + I*im(y)

>>> sym.expand(sym.cos(x + y), trig=True)


-sin(x)*sin(y) + cos(x)*cos(y)
>>> sym.cos(x) * sym.cos(y) - sym.sin(x) * sym.sin(y)
-sin(x)*sin(y) + cos(x)*cos(y)
Limits

sym.limit(sym.sin(x) / x, x, 0)
-----1
sym.limit(x, x, sym.oo)
------oo
sym.limit(1 / x, x, sym.oo)
------0
sym.limit(x ** x, x, 0)
---------1
Differentiation
sym.diff(sym.sin(x), x)
-----cos(x)
sym.diff(sym.sin(2 * x), x)
-----2*cos(2*x)
sym.diff(sym.sin(2 * x), x, 1)
2*cos(2*x)
sym.diff(sym.sin(2 * x), x, 2)
-4*sin(2*x)
sym.diff(sym.sin(2 * x), x, 3)
-8*cos(2*x)
Series
sym.series(sym.cos(x), x)
2 4
x x / 6\
1 - -- + -- + O\x /
2 24
sym.series(1/sym.cos(x), x)
2 4
x 5*x / 6\
1 + -- + ---- + O\x /
2 24
Integration
>>>sym.integrate(6 * x ** 5, x)
6
x
>>> sym.integrate(sym.sin(x), x)
-cos(x)
>>> sym.integrate(sym.log(x), x)
x*log(x) - x
>>> sym.integrate(2 * x + sym.sinh(x), x)
2
x + cosh(x)
sym.integrate(x**3, (x, -1, 1))
0
>>> sym.integrate(sym.sin(x), (x, 0, sym.pi / 2))
1
>>> sym.integrate(sym.cos(x), (x, -sym.pi / 2, sym.pi / 2))
2
Equation solving
sym.solveset(x ** 4 - 1, x)
{-1, 1, -I, I}
sym.solveset(sym.exp(x) + 1, x)
{I*(2*n*pi + pi) | n in Integers}
solution = sym.solve((x + 5 * y - 2, -3 * x + 6 * y - 15), (x, y))
>>> solution[x], solution[y]
(-3,1)
sym.satisfiable(x & y)
{x: True, y: True}
sym.satisfiable(x & ~x)
False
Matrices
sym.Matrix([[1, 0], [0, 1]])
[1 0]
[0 1]
>>> x, y = sym.symbols('x, y')
>>> A = sym.Matrix([[1, x], [y, 1]])
>>> A
[1 x]
[ ]
[y 1]
>>> A**2
[x*y + 1 2*x ]
[ ]
[ 2*y x*y + 1]
Symbolic Maths in Python
from sympy import Symbol, symbols
X = Symbol('X')
expression = X + X + 1
print(expression)
a, b, c = symbols('a, b, c')
expression = a*b + b*a + a*c + c*a
print(expression)
o/p:2*X + 1
2*a*b + 2*a*c
Factorization And Expansion

### factorization and expansion


import sympy as sp
x, y = sp.symbols('x, y')
expr = sp.factor(x**2 - y**2)
print(expr)
expr = sp.expand(expr)
print(expr)
Finite state automata
• Finite Automata(FA) is the simplest machine to recognize patterns.The
finite automata or finite state machine is an abstract machine which
have five elements or tuple.
• It has a set of states and rules for moving from one state to another but
it depends upon the applied input symbol.
• Basically it is an abstract model of digital computer.
Finite Automata consists of the following :

• Q : Finite set of states.


• Σ : set of Input Symbols.
• q : Initial state.
• F : set of Final States.
• δ : Transition Function.
def FA(s):
size=0
#scan complete string and make sure that it contains only 'a' & ‘b’ # RE: (a+b)*bba
for i in s:
if i=='a' or i=='b’:
size+=1
else:
return "Rejected"
#After checking that it contains only 'a' & 'b’
#check it's length it should be 3 atleast
if size>=3:
#check the last 3 elements
if s[size-3]=='b’:
if s[size-2]=='b’:
if s[size-1]=='a’:
return "Accepted"
return "Rejected"
return "Rejected"
return "Rejected"
return "Rejected"
inputs=['bba', 'ababbba', 'abba','abb', 'baba','bbb','']
for i in inputs:
print(FA(i))
Types of finite state automata
• Deterministic finite state automata
• Non Deterministic finite state automata
Deterministic finite state automata
• DFA consists of 5 tuples {Q, Σ, q, F, δ}. Q : set of all states. Σ : set of
input symbols. ( Symbols which machine takes as input ) q : Initial
state. ( Starting state of a machine ) F : set of final state. δ : Transition
Function, defined as δ : Q X Σ --> Q.
• In a DFA, for a particular input character, the machine goes to one
state only. A transition function is defined on every state for every
input symbol. Also in DFA null (or ε) move is not allowed, i.e., DFA
cannot change state without any input character.
For example, below DFA with Σ = {0, 1} accepts all strings ending with
0.

• Figure: DFA with Σ = {0, 1}


• One important thing to note is, there can be many possible DFAs for a
pattern. A DFA with minimum number of states is generally preferred
#import the requirements from the library automata
from automata.fa.dfa import DFA
#the transition function
dfa=DFA(
states= {'q0', 'q1', 'q2', 'q3'},
input_symbols={'0','1'},
transitions={
'q0':{'0':'q3','1':'q1'},
'q1':{'0':'q2','1':'q1'},
'q2':{'0':'q2','1':'q1'},
'q3':{'0':'q3','1':'q3'}
},
initial_state='q0',
final_states={'q2'}
)
if(dfa.accepts_input('10000')):
print("Accepted")
else:
print("Rejected")
Non deterministic finite state automata
NFA is similar to DFA except following additional features:
1. Null (or ε) move is allowed i.e., it can move forward without reading
2. Ability to transmit to any number of states for a particular input.
However, these above features don’t add any power to NFA. If we
compare both in terms of power, both are equivalent.
• Due to above additional features, NFA has a different transition
function, rest is same as DFA.
• δ: Transition Function δ: Q X (Σ U ε ) --> 2 ^ Q. As you can see in
transition function is for any input including null (or ε), NFA can go to
any state number of states.
For example, below is a NFA for above problem

• One important thing to note is, in NFA, if any path for an input string
leads to a final state, then the input string accepted. For example, in
above NFA, there are multiple paths for input string “00”. Since, one
of the paths leads to a final state, “00” is accepted by above NFA.

Problem-2: Construction of a minimal NFA accepting a set of strings
over {a, b} in which each string of the language is not containing ‘a’ as
the substring.
Explanation: The desired language will be like:
L1 = {b, bb, bbbb, ...........}Here as we can see that each string of the
above language is not containing ‘a’ as the substring But the below
language is not accepted by this NFA because some of the string of
below language is containing ‘a’ as the substring.
L2 = {ab, aba, ababaab..............}The state transition diagram of the
desired language will be like below:
def stateY(n):
#if length of n become 0
#then print accepted
if(len(n)==0):
print("string accepted")

else:
#if at zero index
#'a' found then
#print not accepted
if (n[0]=='a'):
print("String not accepted")

#if at zero index


#'b' found call
#stateY function
elif (n[0]=='b'):
stateY(n[1:])

#take input
n=input()

#call stateY function


#to check the input
stateY(n)
Automata based programming
• Automata-based programming is a computer programming paradigm
that treats sections of the program as finite automata. Each automaton
can take one "step" at a time, and the execution of the program is
broken down into individual steps. The steps communicate with each
other by changing the value of a variable, representing the "state."
The control flow of the program is determined by the variable value.
• A common technique is to create a state transition table, a two-
dimensional array comprising rows representing every possible state,
and columns representing input parameter. The table value where the
row and column meet is the next state the machine should transition to
if both conditions are met

You might also like