# Math module

## Miscellaneous functions

``````

In [ ]:

from math import isfinite, isinf, isnan

print(isfinite(4.57), isfinite(float('inf')), isfinite(float('Nan')))
print(isinf(4.57), isinf(float('inf')), isinf(float('Nan')))
print(isnan(4.57), isinf(float('inf')), isnan(float('Nan')))

``````
``````

In [ ]:

from math import ceil, floor, trunc, fabs

print(ceil(-5.0), ceil(-4.5), ceil(4.5), ceil(5))
print(floor(-5.0), floor(-4.5), floor(4.5), floor(5))
print(trunc(-4.4), trunc(-4.6), trunc(4.4), trunc(4.6))
print()

print(fabs(-3.8), fabs(-2), fabs(3.8))

``````
``````

In [ ]:

from math import modf, fmod, frexp, ldexp, copysign

print(modf(-6.4))
print(modf(6.4))
print()

print(fmod(-6.4, -2.1))
print(fmod(-6.4, 2.1))
print(fmod(6.4, -2.1))
print(fmod(6.4, 2.1))
print()

# Write nonzero x as +/-0.1b_2b_3b_4... * 2^e
# with b_2, b_3, b_3... being 0 or 1 and not cofinitely many of them being 1.
# Then returns +/-0.1b_2b_3b_4..., so a number m with 0.5 <= |m| < 1, and e.
print(frexp(-600.4))
print(frexp(-0.006004))
print(frexp(-0.5))
print(frexp(0))
print(frexp(0.5))
print(frexp(0.006004))
print(frexp(600.4))
print()

print(ldexp(4.5, -2))
print(ldexp(4.5, -1))
print(ldexp(4.5, 0))
print(ldexp(4.5, 1))
print(ldexp(4.5, 2))
print()

print(copysign(-4.5, -3.14))
print(copysign(-4.5, 0))
print(copysign(-4.5, 3.14))
print(copysign(4.5, -3.14))
print(copysign(4.5, 0))
print(copysign(4.5, 3.14))

``````
``````

In [ ]:

from math import fsum, hypot, factorial

print(fsum([-2.1, -1.4, 0, 1.4, 2.1]))
print(fsum((-2.2, -1.3, 0, 1.4, 2.5)))
print(fsum({-2.3, -1.2, 0, 1.5, 2.4}))
print(fsum({1.5: 'A', 2: 'B', 3.5: 'C'}))
print()

print(hypot(0, -4))
print(hypot(3, 4))
print()

print(factorial(0))
print(factorial(1))
print(factorial(2))
print(factorial(3))
print(factorial(4))
print(factorial(1000))

``````

## Trigonometric functions

``````

In [ ]:

from math import pi, degrees, radians

print(degrees(- 10 * pi))
print(degrees(- pi / 3))
print(degrees(pi / 3))
print(degrees(10 * pi))
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import xlim, plot

from math import pi, cos

min_x = -2 * pi
max_x = 2 * pi
xlim(min_x, max_x)
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [cos(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import acos

min_x = -1
max_x = 1
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [acos(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import xlim, plot

from math import pi, sin

min_x = -2 * pi
max_x = 2 * pi
xlim(min_x, max_x)
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [sin(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import asin

min_x = -1
max_x = 1
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [asin(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import xlim, plot

from math import pi, tan

min_x = - pi / 2 + 0.01
max_x = pi / 2 - 0.01
xlim(min_x, max_x)
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [tan(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import atan

min_x = - 20
max_x = 20
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [atan(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib notebook
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.pyplot import figure

from math import atan2

min_x = -5
max_x = 5
nb_of_points_x = 70
min_y = -5
max_y = 5
nb_of_points_y = 70
xs = [min_x + (max_x - min_x) / nb_of_points_x * i for i in range(nb_of_points_x + 1)]
xs.remove(0)
xs = [x for x in xs for i in range(nb_of_points_y)]
ys = [min_x + (max_y - min_y) / nb_of_points_y * i for i in range(nb_of_points_y + 1)]
ys.remove(0)
ys = ys * nb_of_points_x
zs = [atan2(y, x) for (x, y) in zip(xs, ys)]
# s is for the size of the points, whose default is 20
figure().add_subplot(111, projection='3d').scatter(xs, ys, zs, s = [1] *len(xs))
print()

``````

## Hyperbolic functions

``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import cosh

min_x = -6
max_x = 6
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [cosh(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import acosh

min_x = 1
max_x = 200
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [acosh(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import sinh

min_x = -6
max_x = 6
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [sinh(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import asinh

min_x = 0
max_x = 200
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [asinh(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import tanh

min_x = -3
max_x = 3
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [tanh(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot

from math import atanh

min_x = -0.99
max_x = 0.99
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [atanh(x) for x in xs]
plot(xs, ys)
print()

``````

## Exponential, logarithmic and power functions

``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import xlim, plot

from math import exp, expm1

min_x = -3
max_x = 4
xlim(min_x, max_x)
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [exp(x) for x in xs]
plot(xs, ys)
ys = [expm1(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot, subplot, text, xticks, yticks

from math import e, log, log1p, log10, log2

min_x = 0
max_x = 12
nb_of_points = 200
xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]
xs.remove(0)
subplot(411)
xticks(())
yticks((-3, -1.5, 0, 1.5, 3))
ys = [log(x, 0.5) for x in xs]
plot(xs, ys)
text(10, -1, 'Base \$\\frac{1}{2}\$')
subplot(412)
xticks(())
yticks((-3.5, -2, -0.5, 1, 2.5))
ys = [log2(x) for x in xs]
plot(xs, ys)
text(10, -1, 'Base 2')
subplot(413)
xticks(())
yticks((range(-3, 5)))
ys = [log(x) for x in xs]
plot(xs, ys)
ys = [log1p(x) for x in xs]
plot(xs, ys)
text(10, -1, 'Base \$e\$')
subplot(414)
yticks((-1, -0.5, 0, 0.5, 1))
ys = [log10(x) for x in xs]
plot(xs, ys)
text(10, 0, 'Base 10')
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import xlim, plot

from math import sqrt

min_x = 0
max_x = 10
nb_of_points = 100
xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]
ys = [sqrt(x) for x in xs]
plot(xs, ys)
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot, subplot, text, xticks, yticks, subplots_adjust

from math import e, pow

min_x = 0
max_x = 3
nb_of_points = 200
xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]
xs.remove(0)
subplot(141)
xticks(range(0, 4))
yticks((0.2, 0.5, 0.8, 1.1, 1.4, 1.7))
ys = [pow(x, 0.5) for x in xs]
plot(xs, ys)
text(0.6, 1.55, '\$x^{0.5}\$')
subplot(142)
xticks(range(0, 4))
yticks(range(0, 10, 2))
ys = [pow(x, 2) for x in xs]
plot(xs, ys)
text(0.6, 8, '\$x^2\$')
subplot(143)
xticks(range(0, 4))
yticks(range(0, 20, 4))
ys = [pow(x, e) for x in xs]
plot(xs, ys)
text(0.6, 17.7, '\$x^e\$')
subplot(144)
xticks(range(0, 4))
yticks(range(0, 30, 5))
ys = [pow(x, 3) for x in xs]
plot(xs, ys)
text(0.6, 24.2, '\$x^3\$')
print()

``````

## Probability functions

``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot, text

from math import erf, erfc

min_x = -3
max_x = 3
nb_of_intervals = 100
xs = [min_x + (max_x - min_x) / nb_of_intervals * i for i in range(nb_of_intervals + 1)]
ys = [erf(x) for x in xs]
plot(xs, ys)
ys = [erfc(x) for x in xs]
plot(xs, ys)
text(-1.2, 1.5, 'erfc', color = 'g')
text(-1.2, -0.5, 'erf', color = 'b')
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot, axvline, axhline

from math import gamma

min_x = 0
max_x = 4.9
nb_of_points = 100
xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]
xs.remove(0)
ys = [gamma(x) for x in xs]
plot(xs, ys, color = 'blue')

# For negative values of x,
# Have to awfully hack point coordinates
# to get decents results
xs = [i / 100 for i in range(10, 91)] + [0.95]
x_1s = [-0.95] + [-1 + x for x in xs]
y_1s = [gamma(x) for x in x_1s]
plot(x_1s, y_1s, color = 'b')
x_2s = [-1.974] + [-2 + x for x in xs]
y_2s = [gamma(x) for x in x_2s]
plot(x_2s, y_2s, color = 'b')
x_3s = [-2.992] + [-3 + x for x in xs] + [-2.025]
y_3s = [gamma(x) for x in x_3s]
plot(x_3s, y_3s, color = 'b')
axvline(-2, linestyle = '--', color = 'y')
axvline(-1, linestyle = '--', color = 'y')
axvline(0, color = 'black')
axhline(0, color = 'black')
print()

``````
``````

In [ ]:

%matplotlib inline
from matplotlib.pyplot import plot, axvline, axhline

from math import lgamma

min_x = 0
max_x = 4.9
nb_of_points = 100
xs = [min_x + (max_x - min_x) / nb_of_points * i for i in range(nb_of_points + 1)]
xs.remove(0)
ys = [lgamma(x) for x in xs]
plot(xs, ys, color = 'blue')

# For negative values of x,
# Have to awfully hack point coordinates
# to get decents results
xs = [i / 100 for i in range(10, 91)] + [0.95]
x_1s = [-0.95] + [-1 + x for x in xs]
y_1s = [lgamma(x) for x in x_1s]
plot(x_1s, y_1s, color = 'b')
x_2s = [-1.974] + [-2 + x for x in xs]
y_2s = [lgamma(x) for x in x_2s]
plot(x_2s, y_2s, color = 'b')
x_3s = [-2.992] + [-3 + x for x in xs] + [-2.025]
y_3s = [lgamma(x) for x in x_3s]
plot(x_3s, y_3s, color = 'b')
axvline(-2, linestyle = '--', color = 'y')
axvline(-1, linestyle = '--', color = 'y')
axvline(0, color = 'black')
axhline(0, color = 'black')
print()

``````