**Rational numbers**


```python
# source: https://scipy-lectures.org/packages/sympy.html

import sympy as sym
from sympy import pprint
print("SymPy defines three numerical types: Real, Rational and Integer.")
a=sym.Rational(1,2)
b=sym.Rational(1,3)
print("a:",a)
print("b:",b)
print("Rational arithmetic can be performed")
print("a+b:",a+b)
print("2a:",2*a)
```

    SymPy defines three numerical types: Real, Rational and Integer.
    a: 1/2
    b: 1/3
    Rational arithmetic can be performed
    a+b: 5/6
    2a: 1



```python
print("special constants, like e, pi, oo (Infinity), are treated as symbols and can be evaluated with arbitrary precision")
pprint(2*sym.pi)
pprint(sym.pi**2)
pprint(sym.exp(1))
print("\nEvaluating the value using evalf function")
print("2pi:",(2*sym.pi).evalf())
print("pi^2:",(sym.pi**2).evalf())
print("e^1:",(sym.exp(1)).evalf())
print("Infinity + 1:",sym.oo+1)
print("\nOne can adjust the precision required using the evalf")
print("2pi:",(2*sym.pi).evalf(3))
print("100 decimal precision of Square root of 2")
print((sym.sqrt(2)).evalf(100))
```

    special constants, like e, pi, oo (Infinity), are treated as symbols and can be evaluated with arbitrary precision
    2⋅π
     2
    π 
    ℯ
    
    Evaluating the value using evalf function
    2pi: 6.28318530717959
    pi^2: 9.86960440108936
    e^1: 2.71828182845905
    Infinity + 1: oo
    
    One can adjust the precision required using the evalf
    2pi: 6.28
    100 decimal precision of Square root of 2
    1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573


**Expand and simplify**


```python
x=sym.Symbol('x')
y=sym.Symbol('y')
print(x+y+x-y)
print((x+y)*(x-y))
print(sym.expand((x+y)**4))
print("Adding additional arguments to expand command")
pprint(sym.expand(x+y,complex=True))
print(sym.expand(sym.cos(x+y),trig=True))
pprint(sym.expand((x+y)*(x-y)))
print(sym.expand((x+y)**6))

print("Using simplify command")
print(sym.simplify((x + x * y) / x))
print(sym.simplify(sym.sin(x)/sym.cos(x),trig=True))
```

    2*x
    (x - y)*(x + y)
    x**4 + 4*x**3*y + 6*x**2*y**2 + 4*x*y**3 + y**4
    Adding additional arguments to expand command
    re(x) + re(y) + ⅈ⋅im(x) + ⅈ⋅im(y)
    -sin(x)*sin(y) + cos(x)*cos(y)
     2    2
    x  - y 
    x**6 + 6*x**5*y + 15*x**4*y**2 + 20*x**3*y**3 + 15*x**2*y**4 + 6*x*y**5 + y**6
    Using simplify command
    y + 1
    tan(x)


**Calculus and pretty printing**


```python
print("syntax for limits limit(function, variable, point)")
print(sym.limit(sym.sin(x)/x,x,0))
print(sym.limit(1/x,x,sym.oo))
print("\nsyntax for differentiation diff(function, variable, order)")
print(sym.diff(sym.log(x),x))
pprint(sym.diff(sym.tan(x),x))
print("\ncheck")
pprint(sym.limit((sym.tan(x+y)-sym.tan(x))/y,y,0))
print("\nCalculating higher derivatives using diff command")
pprint(sym.diff(x**9,x,6))
print("\ncheck")
for i in range(1,10):
    pprint(sym.diff(x**9,x,i))
```

    syntax for limits limit(function, variable, point)
    1
    0
    
    syntax for differentiation diff(function, variable, order)
    1/x
       2       
    tan (x) + 1
    
    check
       2       
    tan (x) + 1
    
    Calculating higher derivatives using diff command
           3
    60480⋅x 
    
    check
       8
    9⋅x 
        7
    72⋅x 
         6
    504⋅x 
          5
    3024⋅x 
           4
    15120⋅x 
           3
    60480⋅x 
            2
    181440⋅x 
    362880⋅x
    362880


**Taylor Series expansion**


```python
print("Syntax series(expression, variable)")
pprint(sym.series(sym.cos(x),x))
pprint(sym.series((1/sym.cos(x)),x))
```

    Syntax series(expression, variable)
         2    4        
        x    x     ⎛ 6⎞
    1 - ── + ── + O⎝x ⎠
        2    24        
         2      4        
        x    5⋅x     ⎛ 6⎞
    1 + ── + ──── + O⎝x ⎠
        2     24         


**Integration**


```python
print("syntax: integrate(function,(variable,lowlimit,highlimit))")
pprint(sym.integrate(6*x**5+y,x))
pprint(sym.integrate(sym.log(x),x))

print("\nDefinite integrals")
print(sym.integrate(x**3,(x,-1,1)))
print(sym.integrate(sym.sin(x),(x,0,sym.pi/2)))
print(sym.integrate((sym.cos(x)),(x,-sym.pi/2,sym.pi/2)))
print(sym.integrate(sym.exp(-x),(x,0,sym.oo)))
print("integrating the following function from from -infinity to infinity")
pprint(sym.exp(-x ** 2))
print("gives")
pprint(sym.integrate(sym.exp(-x**2),(x,-sym.oo,sym.oo)))
```

    syntax: integrate(function,(variable,lowlimit,highlimit))
     6      
    x  + x⋅y
    x⋅log(x) - x
    
    Definite integrals
    0
    1
    2
    1
    integrating the following function from from -infinity to infinity
       2
     -x 
    ℯ   
    gives
    √π


**Solving equations**


```python
print("for solving algebraic equations use solveset(function,variable), for solving SLE use solve((eq1,eq2),(var1,var2))")
pprint(sym.solveset(x**4-1,x))
pprint(sym.solveset(sym.exp(x)+1,x))

sol=sym.solve((x + 5 * y - 2, -3 * x + 6 * y - 15),(x,y))
print(sol)
print(sol[x])
print(sym.solve((x+y-2,2*x+y),(x,y)))

print("\nfactorization of a polynomial")
f=x**4-3*x**2+1
pprint(f)
pprint(sym.factor(f))

print("\nSolving boolean expressions")
pprint(sym.satisfiable((~x|y)&(~y|x)))
```

    for solving algebraic equations use solveset(function,variable), for solving SLE use solve((eq1,eq2),(var1,var2))
    {-1, 1, -ⅈ, ⅈ}
    {ⅈ⋅(2⋅n⋅π + π) | n ∊ ℤ}
    {x: -3, y: 1}
    -3
    {x: -2, y: 4}
    
    factorization of a polynomial
     4      2    
    x  - 3⋅x  + 1
    ⎛ 2        ⎞ ⎛ 2        ⎞
    ⎝x  - x - 1⎠⋅⎝x  + x - 1⎠
    
    Solving boolean expressions
    {x: False, y: False}


**Finding the value of a function at various points**


```python
def f(x):
    return sym.exp(x)**4-3*x**2+1
for i in range(1,4):
    pprint(f(i))
    print(f(i).evalf())
```

          4
    -2 + ℯ 
    52.5981500331442
           8
    -11 + ℯ 
    2969.95798704173
           12
    -26 + ℯ  
    162728.791419004


**Linear algebra**


```python
A=sym.Matrix([[1,2,3],[3,4,5]])
pprint(A)
print("unlike a NumPy array, you can also put Symbols in it")
x,y=sym.symbols('x,y')
A=sym.Matrix([[1,x],[y,1]])
pprint(A)
```

    ⎡1  2  3⎤
    ⎢       ⎥
    ⎣3  4  5⎦
    unlike a NumPy array, you can also put Symbols in it
    ⎡1  x⎤
    ⎢    ⎥
    ⎣y  1⎦


**Differential equations**


```python
f, g = sym.symbols('f g', cls=sym.Function) #create an undefined function by passing cls=Function 
#  f(x), will represent an unknown function
pprint(f(x).diff(x)+f(x))
pprint(f(x).diff(x,x)+f(x))
print("solving differential equations")
pprint(sym.dsolve(f(x).diff(x,x)+f(x),f(x)))
# pprint(sym.dsolve(sym.sin(x) * sym.cos(f(x)) + sym.cos(x) * sym.sin(f(x)) * f(x).diff(x), f(x), hint='separable')) #solve as seperable equation
pprint((f(x).diff(x)*x+f(x)-f(x)**2))
pprint(sym.dsolve(f(x).diff(x)*x+f(x)-f(x)**2,f(x)))
```

           d       
    f(x) + ──(f(x))
           dx      
             2      
            d       
    f(x) + ───(f(x))
             2      
           dx       
    solving differential equations
    f(x) = C₁⋅sin(x) + C₂⋅cos(x)
      d           2          
    x⋅──(f(x)) - f (x) + f(x)
      dx                     
             -C₁  
    f(x) = ───────
           -C₁ + x


9d28c43aa4e28dac690e03039ce754332f22fe4c
ipynb
Jupyter Notebook
basics_of_sympy.ipynb
rsatwik/Python_Practice
3ab027be3cabac96a41e8e616296fb2497916f7b
MIT
basics_of_sympy.ipynb
rsatwik/Python_Practice
3ab027be3cabac96a41e8e616296fb2497916f7b
MIT
basics_of_sympy.ipynb
rsatwik/Python_Practice
3ab027be3cabac96a41e8e616296fb2497916f7b
MIT
2021-01-09T17:00:13.000Z
2021-01-09T17:00:13.000Z
Qwen/Qwen-72B
1. YES
2. YES