4. Functions and Classes: Exercise#
Name:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
1. Functions#
Define the following functions and show some sample outputs.
Factorial of n: \(1 \times 2 \times \cdots \times n\).
def factorial(n):
"""factorial of n"""
for n in range(1, 10):
print(factorial(n))
None
None
None
None
None
None
None
None
None
For a circle of radius r (default r=1), given x coordinate, return possible y coordinates (two, one, or None).
def circley(x, r=1):
Cell In[4], line 2
^
SyntaxError: incomplete input
Draw a star-like shape with n vertices, every m-th vertices connected, with default of n=5 and m=2.
def star(n=5, m=2):
Any function(s) of your interest
2. Classes#
Extend the
Cell
,gCell
orCulture
class with new methods, such as:
cells with other shapes, e.g. ellipse, polygon,…
move to avoid overlap with another cell
connect with another cell like a neuron
growth based on the environment, e.g., number of neighbors
…
# example of drawing an ellipse
import matplotlib.patches as pat
p = pat.Ellipse(xy=(1,2), width=2, height=1, angle=30)
plt.gca().add_patch(p)
plt.axis('equal');
# example of drawing a polygon
xy = np.array([[np.sin(th),np.cos(th)] for th in np.arange(0, 4*np.pi, np.pi*4/5)])
p = pat.Polygon(xy)
plt.gca().add_patch(p)
plt.axis('equal');
Save the defined class(es) as a module
cell.py
.
Import the module and test how it works.
import cell
import importlib
importlib.reload(cell) # This is needed after updating a module