Symbolic programming: 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.
Languages that support symbolic programming include languages such
as Wolfram Language, LISP and Prolog.
Symbolic programming paradigm is a symbolic computer algebra system. They
use symbolic in the mathematical sense. Some authors consider it as a part of
functional programming but it is very much different from that.
For example, if you do 1.0/3*3 in, say, C, it divides 1.0 by 3 and then multiplies
the result by 3 = (1.0/3)*3. You get 0.9999999999998.
Mathematica (and other symbolic mathematics software, for example
Macsyma/Maxima) treats numbers as symbols... Like, 1/3*3 is not a lot
different from a/b*b until you need the actual result in numeric form. Since
a/b*b is a, 1/3*3 is 1. No computation with actual numeric values needed.
Getting started with SymPy module: Sympy is a Python library for symbolic
mathematics. It aims to become a full-featured computer algebra system (CAS)
while keeping the code as simple as possible in order to be comprehensible and
easily extensible. SymPy is written entirely in Python.
SymPy only depends on mpmath, a pure Python library for arbitrary floating
point arithmetic, making it easy to use.
SymPy as a calculator:SymPy defines following numerical types: Rational and
Integer. The Rational class represents a rational number as a pair of two
Integers, numerator and denominator, so Rational(1, 2) represents 1/2,
Rational(5, 2) 5/2 and so on. The Integer class represents Integer number.
# import everything from sympy module
from sympy import *
a = Rational(5, 8)
print("value of a is :" + str(a))
b = Integer(3.579)
print("value of b is :" + str(b))
SymPy uses mpmath in the background, which makes it possible to perform
computations using arbitrary-precision arithmetic. That way, some special
,constants, like exp, pi, oo (Infinity), are treated as symbols and can be evaluated
with arbitrary precision.
# import everything from sympy module
from sympy import *
# you can't get any numerical value
p = pi**3
print("value of p is :" + str(p))
# evalf method evaluates the expression to a floating-point number
q = pi.evalf()
print("value of q is :" + str(q))
# equivalent to e ^ 1 or e ** 1
r = exp(1).evalf()
print("value of r is :" + str(r))
s = (pi + exp(1)).evalf()
print("value of s is :" + str(s))
rslt = oo + 10000
print("value of rslt is :" + str(rslt))
if oo > 9999999 :
print("True")
else:
print("False")
output:value of p is :pi^3
value of q is :3.14159265358979
value of r is :2.71828182845905
value of s is :5.85987448204884
value of rslt is :oo
True
The real power of a symbolic computation system such as SymPy is the ability
to do all sorts of computations symbolically. SymPy can simplify expressions,
compute derivatives, integrals, and limits, solve equations, work with matrices,
and much, much more, and do it all symbolically. Here is a small sampling of
the sort of symbolic power SymPy is capable of, to whet your appetite.
Example : Find derivative, integration, limits, quadratic equation.
# import everything from sympy module
from sympy import *
# make a symbol
x = Symbol('x')
# ake the derivative of sin(x)*e ^ x
ans1 = diff(sin(x)*exp(x), x)
print("derivative of sin(x)*e ^ x : ", ans1)
# Compute (e ^ x * sin(x)+ e ^ x * cos(x))dx
ans2 = integrate(exp(x)*sin(x) + exp(x)*cos(x), x)
print("indefinite integration is : ", ans2)
,# Compute definite integral of sin(x ^ 2)dx
# in b / w interval of ? and ?? .
ans3 = integrate(sin(x**2), (x, -oo, oo))
print("definite integration is : ", ans3)
# Find the limit of sin(x) / x given x tends to 0
ans4 = limit(sin(x)/x, x, 0)
print("limit is : ", ans4)
# Solve quadratic equation like, example : x ^ 2?2 = 0
ans5 = solve(x**2 - 2, x)
print("roots are : ", ans5)
output: derivative of sin(x)*e^x : exp(x)*sin(x) + exp(x)*cos(x)
indefinite integration is : exp(x)*sin(x)
definite integration is : sqrt(2)*sqrt(pi)/2
limit is : 1
roots are : [-sqrt(2), sqrt(2)]
sympy.Derivative() method: With the help of sympy.Derivative() method, we
can create an unevaluated derivative of a SymPy expression. It has the same
syntax as diff() method. To evaluate an unevaluated derivative, use the doit()
method.
Syntax: Derivative(expression, reference variable)
Parameters:
expression – A SymPy expression whose unevaluated derivative is found.
reference variable – Variable with respect to which derivative is found.
# import sympy
from sympy import *
x, y = symbols('x y')
expr = x**2 + 2 * y + y**3
print("Expression : {} ".format(expr))
# Use sympy.Derivative() method
expr_diff = Derivative(expr, x)
print("Derivative of expression with respect to x : {}".format(expr_diff))
print("Value of the derivative : {} ".format(expr_diff.doit()))
output: Expression : x**2 + y**3 + 2*y
Derivative of expression with respect to x : Derivative(x**2 + y**3 + 2*y, x)
Value of the derivative : 2*x
Example2:
# import sympy
from sympy import *
x, y = symbols('x y')
expr = y**2 * x**2 + 2 * y*x + x**3 * y**3
print("Expression : {} ".format(expr))
, # Use sympy.Derivative() method
expr_diff = Derivative(expr, x, y)
print("Derivative of expression with respect to x : {}".format(expr_diff))
print("Value of the derivative : {} ".format(expr_diff.doit()))
output: Expression : x**3*y**3 + x**2*y**2 + 2*x*y
Derivative of expression with respect to x : Derivative(x**3*y**3 + x**2*y**2
+ 2*x*y, x, y)
Value of the derivative : 9*x**2*y**2 + 4*x*y + 2
sympy.diff() method: With the help of sympy.diff() method, we can find the
differentiation of mathematical expressions in the form of variables by using
sympy.diff() method.
Syntax : sympy.diff(expression, reference variable)
Return : Return differentiation of mathematical expression.
Example:In this example we can see that by using sympy.diff() method, we can
find the differentiation of mathematical expression with variables. Here we use
symbols() method also to declare a variable as symbol.
# import sympy
from sympy import * x, y = symbols('x y')
gfg_exp = x + y
exp = sympy.expand(gfg_exp**2)
print("Before Differentiation : {}".format(exp))
# Use sympy.diff() method
dif = diff(exp, x)
print("After Differentiation : {}".format(dif))
Output :
Before Differentiation : x**2 + 2*x*y + y**2
After Differentiation : 2*x + 2*y
sympy.factor() method: With the help of sympy.factor() method, we can find
the factors of mathematical expressions in the form of variables by using
sympy.factor() method.
Syntax : sympy.factor(expression)
Return : Return factor of mathematical expression.
Example #1 :
In this example we can see that by using sympy.factor() method, we can find the
factors of mathematical expression with variables. Here we use symbols()
method also to declare a variable as symbol.
# import sympy
from sympy import expand, symbols, factor
x, y = symbols('x y')
gfg_exp = x + y
exp = sympy.expand(gfg_exp**2)