# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# **Rational numbers**

# +
# 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)
# -

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))

# **Expand and simplify**

# +
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))
# -

# **Calculus and pretty printing**

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))

# **Taylor Series expansion**

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

# **Integration**

# +
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)))
# -

# **Solving equations**

# +
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)))


# -

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

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())

# **Linear algebra**

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)

# **Differential equations**

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)))

StarcoderdataJupyterScriptsDedupFiltered