In [1]:
import numpy as np

In [2]:
arr = np.array([[1, 2], [3, 4]])
print("shape =", arr.shape)
print("size =", arr.size)
print("data type is", arr.dtype)
print("arr[1] = ", arr[1])
print("arr[(1,1)] = ", arr[(1,1)])


shape = (2, 2)
size = 4
data type is int64
arr[1] =  [3 4]
arr[(1,1)] =  4

In [3]:
def fib(n):
    a = 1
    b = 1
    count = 0
    while count < n:
        if count % 2 == 0:
            yield a
            a = a + b
        else:
            yield b
            b = a + b
        count += 1

In [4]:
for f in fib(10):
    print(f)


1
1
2
3
5
8
13
21
34
55

In [5]:
f = np.array([f for f in fib(10)])
print(f)


[ 1  1  2  3  5  8 13 21 34 55]

In [6]:
lst = [1, 2, 3]
print(lst[1])
print(lst[-1])
print(lst[1:2:1])
print(lst[1::2])
print(lst[::-1])
print(lst[::-2])


2
3
[2]
[2]
[3, 2, 1]
[3, 1]

In [7]:
a = np.arange(1, 4, 1)
print(f'{a.cumsum()}')
print(f'{a.sum()}')


[1 3 6]
6

In [8]:
import sympy
sympy.init_printing()

In [9]:
x = sympy.Symbol('x')
x + 1


Out[9]:
$\displaystyle x + 1$

In [10]:
r1 = sympy.Rational(10, 3)
r2 = sympy.Rational(11, 24)
r1 + r2


Out[10]:
$\displaystyle \frac{91}{24}$

In [11]:
x = sympy.Symbol('x')
expr = 2 * (x ** 2) - x * (x + 1)

In [12]:
expr


Out[12]:
$\displaystyle 2 x^{2} - x \left(x + 1\right)$

In [13]:
sympy.simplify(expr)


Out[13]:
$\displaystyle x \left(x - 1\right)$

In [14]:
a, b = sympy.symbols('a, b')
expr = sympy.sin(a + b)
expr.expand(trig=True)


Out[14]:
$\displaystyle \sin{\left(a \right)} \cos{\left(b \right)} + \sin{\left(b \right)} \cos{\left(a \right)}$

In [15]:
x, y, z = sympy.symbols('x, y, z')
sympy.sin(x * z).subs({z: sympy.exp(y), x: y, sympy.sin: sympy.cos})


Out[15]:
$\displaystyle \cos{\left(y e^{y} \right)}$

In [16]:
x, y, z = sympy.symbols('x, y, z')
expr = x * y + z ** 2 * x
values = {x: 1.25, y: 0.4, z: 3.2}
expr.subs(values)


Out[16]:
$\displaystyle 13.3$

In [17]:
x = sympy.Symbol('x')
sympy.sin(x).series(n=4)


Out[17]:
$\displaystyle x - \frac{x^{3}}{6} + O\left(x^{4}\right)$

In [18]:
x = sympy.Symbol('x')
sympy.sin(x).diff(x)


Out[18]:
$\displaystyle \cos{\left(x \right)}$

In [19]:
import matplotlib.pyplot as plt

In [20]:
x = np.linspace(-5, 2, 100)

y1 = x ** 3 + 5 * x ** 2 + 10
y2 = 3 * x ** 2 + 10 * x
y3 = 6 * x + 10

fig, ax = plt.subplots()
ax.plot(x, y1, color='blue', label='y(x)')
ax.plot(x, y2, color='red', label="y'(x)")
ax.plot(x, y3, color='green', label="y''(x)")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.legend()


Out[20]:
<matplotlib.legend.Legend at 0x7f6119458d90>

In [21]:
fig = plt.figure(figsize=(8, 2.5), facecolor='#f1f1f1')

# axes coordinates as fractions of the canvas width and height
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax = fig.add_axes((left, bottom, width, height), facecolor='#e1e1e1')

x = np.linspace(-2, 2, 1000)
y1 = np.cos(40 * x)
y2 = np.exp(-x ** 2)

ax.plot(x, y1 * y2)
ax.plot(x, y2, color='green')
ax.plot(x, -y2, color='green')

ax.set_xlabel('x')
ax.set_ylabel('y')


Out[21]:
Text(0, 0.5, 'y')

In [22]:
fig, ax1 = plt.subplots(figsize=(8, 4))

r = np.linspace(0, 5, 100)
a = 4 * np.pi * r ** 2
v = (4 * np.pi / 3) * r ** 3

ax1.set_title("surface area and volume of a sphere", fontsize=16)
ax1.set_xlabel('radius [m]', fontsize=16)

ax1.plot(r, a, lw=2, color='blue')
ax1.set_ylabel(r'surface area [$m^2$]', fontsize=16, color='blue')

ax2 = ax1.twinx()

ax2.plot(r, v, lw=2, color='red')
ax2.set_ylabel(r'volume [$m^3$]', fontsize=16, color='red')


Out[22]:
Text(0, 0.5, 'volume [$m^3$]')

In [24]:
fig = plt.figure(figsize=(8,4))

def f(x):
    return 1/(1 + x**2) + 0.1/(1 + ((3 - x) / 0.1)**2)


def plot_and_format_axes(ax, x, f, fontsize):
    ax.plot(x, f(x), lw=2)
    ax.set_xlabel(r'$x$', fontsize=fontsize)
    ax.set_ylabel(f'$f(x)$', fontsize=fontsize)

# main graph
ax = fig.add_axes((0.1, 0.15, 0.8, 0.8), facecolor='#f5f5f5')
x = np.linspace(-4, 14, 1000)
plot_and_format_axes(ax, x, f, 18)

x0, x1 = 2.5, 3.5
ax.axvline(x0, ymax=0.3, color='gray', ls=':')
ax.axvline(x1, ymax=0.3, color='gray', ls=':')

ax_insert = fig.add_axes((0.5, 0.5, 0.38, 0.42), facecolor='none')
x = np.linspace(x0, x1, 1000)
plot_and_format_axes(ax_insert, x, f, 14)



In [ ]: