4. Functions and Classes: Exercise

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.

  1. 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
  1. 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
  1. 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):
    
  1. Any function(s) of your interest

2. Classes#

  1. Extend the Cell, gCell or Culture 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');
  1. Save the defined class(es) as a module cell.py.

  1. Import the module and test how it works.

import cell
import importlib
importlib.reload(cell) # This is needed after updating a module