sympy

sympy library can be used to handle mathematical formula. mathemathica and matlab are the famous language in this genre, but they are not open source and expensive software.

The advantage of sympy is that

  • calculate algebra
  • copy the formula in latex format, which can be paste in thesis or technical paper
  • (and many more)

fraction (Rational)

sympy supports fraction(Function name is Rational) such as 1/3.


In [13]:
import sympy as smp

x = smp.Rational(1,3)
y = smp.Rational(1,2)

print(x, y, x+y)
print(x*3, (x+y)*6)


1/3 1/2 5/6
1 5

Irrational Number

sympy support irrational number such as $\sqrt{2}, \pi, e$


In [14]:
root2 = smp.sqrt(2)
print('Root 2:', root2.evalf(100))
print('Pi:    ', smp.pi.evalf(100))
print('e:     ', smp.E.evalf(100))


Root 2: 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573
Pi:     3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
e:      2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427

algebra

sympy is powerful when solving algebra.


In [15]:
x = smp.Symbol('x')  # Define 1 symbol
y, z = smp.symbols('y z')  # Define 2 or more symbols

f = x**2 - 2*x + 1
print('Factor', smp.factor(f))
print('Solve', smp.solve(f, x))


Factor (x - 1)**2
Solve [1]

In [16]:
f = x + y -7     # x+y = 7
g = x * y -10    # x*y = 10
smp.solve([f,g])


Out[16]:
[{x: 2, y: 5}, {x: 5, y: 2}]

In [17]:
from sympy import symbols, solve, factor

f = x**7 - 1
print(factor(f))
print(solve(f))


(x - 1)*(x**6 + x**5 + x**4 + x**3 + x**2 + x + 1)
[1, -cos(pi/7) - I*sin(pi/7), -cos(pi/7) + I*sin(pi/7), cos(2*pi/7) - I*sin(2*pi/7), cos(2*pi/7) + I*sin(2*pi/7), -cos(3*pi/7) - I*sin(3*pi/7), -cos(3*pi/7) + I*sin(3*pi/7)]

Prime factorization

factorint() function of sympy performs prime factorization(decomposition).


In [18]:
from sympy import factorint

years = [2017,2018,2019]

for y in years:
    print(factorint(y))


{2017: 1}
{2: 1, 1009: 1}
{3: 1, 673: 1}

In [19]:
# print years that are product of 3 different prims

for year in range(1900, 2019):
    primes = factorint(year)
    if sum(primes.values()) == 3 and len(primes) == 3:
        print('Year:', year, 'Prime', primes)


Year: 1902 Prime {2: 1, 3: 1, 317: 1}
Year: 1905 Prime {3: 1, 5: 1, 127: 1}
Year: 1910 Prime {2: 1, 5: 1, 191: 1}
Year: 1918 Prime {2: 1, 7: 1, 137: 1}
Year: 1930 Prime {2: 1, 5: 1, 193: 1}
Year: 1946 Prime {2: 1, 7: 1, 139: 1}
Year: 1947 Prime {3: 1, 11: 1, 59: 1}
Year: 1955 Prime {5: 1, 17: 1, 23: 1}
Year: 1958 Prime {2: 1, 11: 1, 89: 1}
Year: 1965 Prime {3: 1, 5: 1, 131: 1}
Year: 1970 Prime {2: 1, 5: 1, 197: 1}
Year: 1978 Prime {2: 1, 23: 1, 43: 1}
Year: 1986 Prime {2: 1, 3: 1, 331: 1}
Year: 1990 Prime {2: 1, 5: 1, 199: 1}
Year: 2001 Prime {3: 1, 23: 1, 29: 1}
Year: 2006 Prime {2: 1, 17: 1, 59: 1}
Year: 2013 Prime {3: 1, 11: 1, 61: 1}
Year: 2014 Prime {2: 1, 19: 1, 53: 1}
Year: 2015 Prime {5: 1, 13: 1, 31: 1}

In [20]:
factorint(1234567890123456789012345678913)


Out[20]:
{13: 1, 199: 1, 263: 1, 467: 1, 184567: 1, 67947437: 1, 309826661: 1}

In [ ]: