【问题标题】:Build a coupled map lattice using 2D array使用二维数组构建耦合地图格
【发布时间】:2020-05-07 22:53:28
【问题描述】:

所以我试图在我的计算机上构建一个耦合地图格。

耦合映射点阵 (CML) 由以下 eq'n 给出:

其中,函数 f(Xn) 是一个逻辑映射:

x 值从 0 到 1,对于此 CML,r=4。

注意:“n”可以被认为是时间,“i”可以被认为是空间

我花了很多时间来理解迭代,我想出了如下代码,但是我不确定这是否是迭代这个方程的正确代码。

注意:我使用了 2d numpy 数组,其中行是“n”,列是“i”,从代码中可以明显看出。

所以基本上,我想开发一个代码来模拟这个方程,这是我的看法

不要直接跳到代码,如果不先看方程式,你就不会明白发生了什么。

import numpy as np
import matplotlib.pyplot as plt

'''The 4 definitions created below are actually similar and only vary in their indexings. These 4 
   have been created only because of the if conditions I have put in the for loop '''
def logInit(r,x):
    y[n,0]=r*x[n,0]*(1-x[n,0])
    return y[n,0]

def logPresent(r,x):
    y[n,i]=r*x[n,i]*(1-x[n,i])
    return y[n,i] 

def logLast(r,x):
    y[n,L-1]=r*x[n,L-1]*(1-x[n,L-1])
    return y[n,L-1] 

def logNext(r,x):
    y[n,i+1]=r*x[n,i+1]*(1-x[n,i+1])
    return y[n,i+1]

def logPrev(r,x):
    y[n,i-1]=r*x[n,i-1]*(1-x[n,i-1])
    return y[n,i-1]

# 2d array with 4 row, 3 col. I created this because I want to store the evaluated values of log 
  function into this y[n,i] array

y=np.ones(12).reshape(4,3)

# creating an array of random numbers between 0-1 with 4 rows 3 columns  

np.random.seed(0)
x=np.random.random((4,3))

L=3
r=4
eps=0.5

for n in range(3):
    for i in range(L):


        if i==0:
            x[n+1,i]=(1-eps)*logPresent(r,x) + 0.5*eps*(logLast(r,x)+logNext(r,x))

        elif i==L-1:
              x[n+1,i]=(1-eps)*logPresent(r,x) + 0.5*eps*(logPrev(r,x) + logInit(r,x))

        elif i > 0 and i < L - 1:
             x[n+1,i]=(1-eps)*logPresent(r,x) + 0.5*eps*(logPrev(r,x) +logNext(r,x))

        print(x)

这确实给出了输出。这里是:

[[0.5488135  0.71518937 0.60276338]
 [0.94538775 0.82547604 0.64589411]
 [0.43758721 0.891773   0.96366276]
 [0.38344152 0.79172504 0.52889492]]

[[0.5488135  0.71518937 0.60276338]
 [0.94538775 0.82547604 0.92306303]
 [0.2449672  0.49731638 0.96366276]
 [0.38344152 0.79172504 0.52889492]]

[[0.5488135  0.71518937 0.60276338]
 [0.94538775 0.82547604 0.92306303]
 [0.2449672  0.49731638 0.29789622]
 [0.75613708 0.93368134 0.52889492]]  

但我很确定这不是我想要的。

如果你能找出一个正确的方法来用代码迭代和循环 CML 方程?建议我必须做出的改变。非常感谢!!

您必须考虑要进行的迭代和循环来模拟这个方程。这可能很乏味,但这是您可以建议我对代码进行一些更改的唯一方法。

【问题讨论】:

  • 您知道验证结果的方法吗?顺便说一句,我得到的结果与您提供的 3 个数组不同。共 12 个
  • 如果您使用x=np.zeros((N+1, L)); x[0, :] = np.random.random(L),您的网格中的哪些元素被更新会变得更加清晰。您只需在第一步初始化网格
  • 如果这解决了您的问题,请考虑投票

标签: python arrays numpy loops iteration


【解决方案1】:

我觉得你的计算很好。您可以通过沿空间维度使用矢量化和重用中间结果y 来提高速度。我稍微重组了你的程序,但本质上它和以前做的一样。对我来说,结果看起来是合理的。该图显示了第一行中的随机初始向量,随着时间的推移(从上到下),耦合开始发挥作用,形成小岛和模式。

import numpy as np
import matplotlib.pyplot as plt

L = 128  # grid size
N = 128  # time steps 
r = 4
eps = 0.5

# Create random values for the initial time step
np.random.seed(0)
x = np.zeros((N+1, L))
x[0, :] = np.random.random(L)

# Create a helper matrix to save and reuse part of the calculations
y = np.zeros((N, L))

# Indices for previous, present, next position for every point on the grid
idx_present = np.arange(L)        #   0, 1, ..., L-2, L-1
idx_next = (idx_present + 1) % L  #   1, 2, ..., L-1, 0
idx_prev = (idx_present - 1) % L  # L-1, 0, ..., L-3, L-2

def log_vector(rr, xx):
    return rr * xx * (1 - xx)

# Loop over the time steps
for n in range(N):
    # Compute y once for the whole time step and reuse it 
    # to build the next time step with coupling the neighbours
    y[n, :] = log_vector(rr=r, xx=x[n, :])
    x[n+1, :] = (1-eps)*y[n,idx_present] + 0.5*eps*(y[n,idx_prev]+y[n,idx_next])

# Plot the results
plt.imshow(x)

【讨论】:

  • 对此我感激不尽。这正是我过去几周一直在寻找的。我以前无法考虑索引 CML,就像您在此处所做的那样 (idx_present, next, prev) 。在绘图时,我想了好几天,关于用一个点和一些东西来绘制数组的每个元素。即使参考了许多文档,也无法想到这种绘制方式。你的答案很完美!非常感谢!
  • @sceleronomic 我有很多这样的模拟要执行,我想要关于学习什么以计算科学内容以及如何在文档中浏览这些内容的建议(它们也很长) .我熟悉通过 C 编程的基础知识,所以我知道介绍性概念。在给定的科学数据上开发编码器逻辑(就像您可以设法考虑 idx_present、next prev 等)是我担心的第一件事,而绘制它是我担心的第二件事。
  • @DharilShah 很高兴能帮助您。网络上有很多课程和资料可以帮助您成为更好的程序员(教科书、edX、HackerRank、Udemy、Brilliant、YouTube、numpy、matplotlib 的文档和食谱……)。但我想最好的方法就是继续练习。如果您有自己感兴趣的主题,并且有一些想法如何通过代码分析它,那么您就可以开始了。如果你在路上卡住了,StackOverflow 会叫你出去;)
猜你喜欢
  • 2017-11-20
  • 2015-07-13
  • 1970-01-01
  • 2017-04-01
  • 2013-12-05
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 2021-01-17
相关资源
最近更新 更多