python numpy`函数数组(lambda函数)

时间:2018-07-08 17:32:32

标签: python numpy ode

我已经定义了一个python类,以便计算差分方程系统的解。 为此,我定义了一个名为Rhs(右侧和右侧)的类,该类应表示dy / dt(i-th)的右侧和右侧,该类包含单个浮点值(初始时间,初始值,最终时间)和一个函数(函数数组)以定义此数组,我仅定义了3个lambda函数,该函数表示等式(i)并创建此函数的np.array

func1 = lambda t,u : 10   * (u[1] - u[0])
func2 = lambda t,u : 28   * u[0] - u[1] - u[0] * u[2]  
func3 = lambda t,u : -8/3 * u[2] + u[0]*u[1]

然后以这种方式传递给rhs类:

func = np.array([func1,func2,func3])
y0 = np.array([1.,0.,0.])
problem3 = rhs.Rhs(func,0.0,100.0,y0,1000) 

Rhs类是这样的:

    class Rhs:
    def __init__(self, fnum : np.ndarray , t0: np.float, tf: np.float, y0 : np.array, n: int , fanal = None ):
          self.func = fnum
          Rhs.solution  = fanal
          self.t0   = t0
          self.tf   = tf
          self.n    = n
          self.u0   = y0
   def createArray(self):
      '''   
         Create the Array time and f(time) 
         - the time array can be create now 
         - the solution array can be just initialize with the IV
      ''' 
      self.t = np.linspace(self.t0, self.tf, self.n )
      self.u = np.array([self.u0  for i in range(self.n) ])


      return self.t,self.u   

   def f(self,ti,ui):
      return self.func(ti,ui)     

   def Df(self,ti,ui):
      eps = 10e-6
      return ((self.func(ti,ui)+eps) - self.f(ti,ui))/eps

这里的问题是当euler类调用函数f

class Explicit:
     def __init__(self, dydt: rhs.Rhs, save : bool=True, _print: bool=False,  filename : str=None):
          self.dydt   = dydt
          self.dt     = (dydt.tf-dydt.t0)/dydt.n
          self._print = _print 

   def solve(self):
      self.time, self.u = self.dydt.createArray() 
      for i in range(len(self.time)-1): 
         self.u[i+1] = self.u[i] + self.dt*self.dydt.f(self.time[i],self.u[i])


      Explicit.solved = True 

      print('here')

      if self._print:
          with open(filename) as f:
              print('here')
              for i in range(len(self.u)):      
                f.write('%.4f %4f %4f %4f' %(self.time ,self.u[0,i], self.u[1], self.u[2]))


      if self.save:
         return self.time,self.u

这里的问题是:这是将shape = 1000,3的向量u传递给该函数的正确方法(以便使用在lambda函数系统中应用于3向量索引的3函数来工作。 )我不明白的是为什么在C ++中我没有这个问题 在这里看看:all the class hierarchy我不知道用什么方式计算这个东西

这是错误:

Traceback (most recent call last):
  File "drive.py", line 94, in <module>
    main()
  File "drive.py", line 63, in main
    fet,feu = fwdeuler_p1.solve()
  File "/home/marco/Programming/Python/Numeric/OdeSystem/euler.py", line 77, in solve
    self.u[i+1] = self.u[i] + self.dt*self.dydt.f(self.time[i],self.u[i])
  File "/home/marco/Programming/Python/Numeric/OdeSystem/rhs.py", line 44, in f
    return self.func(ti,ui)     
TypeError: 'numpy.ndarray' object is not callable

编辑:感谢您的回复..不幸的是,没有:( 这是错误消息:

drive.py:31: RuntimeWarning: overflow encountered in double_scalars
  func2 = lambda t,u : 28   * u[0] - u[1] - u[0] * u[2]
drive.py:32: RuntimeWarning: overflow encountered in double_scalars
  func3 = lambda t,u : -8/3 * u[2] + u[0]*u[1]
drive.py:31: RuntimeWarning: invalid value encountered in double_scalars
  func2 = lambda t,u : 28   * u[0] - u[1] - u[0] * u[2]
/home/marco/Programming/Python/Numeric/OdeSystem/euler.py:77: RuntimeWarning: invalid value encountered in add
  self.u[i+1] = self.u[i] + self.dt*self.dydt.f(self.time[i],self.u[i])

非常感谢您的帮助!您有其他解决方案吗?

@LutzL我不需要复制u0,self.u0是shape = 3,self.u应该是1000,3(1000行),这3列分别代表了u[0],u[1],u[2]方程(函数数组),顺便说一句,如果我增加步数(减少增量)

2 个答案:

答案 0 :(得分:1)

这应该有效

def f(self,ti,ui):
    return  np.array([function(ti,ui) for function in self.func])

答案 1 :(得分:0)

  self.u = np.array([self.u0  for i in range(self.n) ])

还应该给您带来问题,因为这会产生一个以u0作为行而不是预期副本的矩阵或2d数组。你可能想要

  self.u = np.array([self.u0[i]  for i in range(self.n) ])

或更简单

  self.u = self.u0.copy()