from sympy import *
from IPython.display import *
init_printing(use_latex=True)
# source: http://mathworld.wolfram.com/PopulationGrowth.html
# problem: for a given multi-variable equation, generate all its forms
# create symbols
vl = (N,N_0,t,r) = symbols('N N_0 t r')
# base equation
b = log(N/N_0) - r*t
def wrap_tag(t,d):
return '<%s style="border-color:white">%s</%s>' % (t,d,t)
desc = (
'Population at time t','Population at time zero',
'Time','Population growth rate'
)
s = 'Variables:'
ss = ''
for n,x in enumerate(vl):
ss += wrap_tag('td',' ')
ss += wrap_tag('td','$%s$' % str(x))
ss += wrap_tag('td',': %s' % desc[n])
ss = wrap_tag('tr',ss)
s += wrap_tag('table',ss)
s += 'Equations:'
ss = ''
for v in vl:
soln = solve(b,v)[0]
ss += wrap_tag('td',' ')
ss += wrap_tag('td','$%s$' % str(v))
ss += wrap_tag('td','$\displaystyle = %s$' % latex(soln))
ss = wrap_tag('tr',ss)
s += wrap_tag('table',ss)
HTML(s)