\(\newcommand{\B}[1]{ {\bf #1} }\) \(\newcommand{\R}[1]{ {\rm #1} }\)
an_ode.py¶
View page sourcean_ode: Python Source Code¶
import cmpad
import numpy
#
# an_ode_fun
class an_ode_fun :
#
# __init__
# self.x, self.n
def __init__(self, like_numpy, x) :
self.like_numpy = like_numpy
self.x = x
self.n = len(x)
#
# __call__
def __call__ (self, y) :
assert len(y) == self.n
assert len(self.x) == self.n
#
# like_numpy
like_numpy = self.like_numpy
#
# dy
dy = [ like_numpy.array( self.x[0] ).reshape(-1) ]
for i in range(1, self.n) :
dy.append( like_numpy.array( self.x[i] * y[i-1] ).reshape(-1) )
dy = like_numpy.concatenate( dy )
#
return dy
#
# BEGIN PROTOTYPE
class an_ode :
def __init__(self, like_numpy) :
self.like_numpy = like_numpy
#
def option(self) :
return self.option
#
def domain(self) :
return self.n_arg
#
def range(self) :
return self.n_arg
#
def setup(self, option) :
assert type(option) == dict
assert type( option['n_arg'] ) == int
assert type( option['n_other'] ) == int
assert option['n_arg'] > 0
assert option['n_other'] > 0
# END PROTOTYPE
#
# option
self.option = option
#
# self.n_arg
self.n_arg = option['n_arg']
#
# self.n_other
self.n_other = option['n_other']
#
def __call__(self, x) :
assert len(x) == self.n_arg
#
# like_numpy
like_numpy = self.like_numpy
#
# yi
yi = like_numpy.array( self.n_arg * [ 0.0 ] )
#
# fun
fun = an_ode_fun(like_numpy, x)
#
# tf
tf = 2.0
#
# ns
ns = self.n_other
#
# yf
yf = cmpad.runge_kutta(like_numpy, fun, yi, tf, ns)
#
assert len(yf) == self.n_arg
if like_numpy == numpy :
return yf
return yf.vec