简体   繁体   English

希望在数组上重复函数N次

[英]Looking to repeat a function on an array N times

I have a function that takes a 2d array as an input, and outputs the array with the sign of random coordinates flipping. 我有一个函数,将2d数组作为输入,并输出带有随机坐标翻转符号的数组。 I want to repeat this function n times, with it taking the output of the function as the input for the next time. 我想重复此功能n次,将其输出作为下一次的输入。 How am i able to do this? 我该怎么做? The array S is an n*n array of randomised 1s and -1s 数组S是随机1和-1的n * n数组

Thermal<-function(S,t=0.000000000001,k=1){
  #Defing beta
  beta<-1/(k*t)
  #multiplying each point of the array by all its adjacent points 
  #and summing them
  Spointenergy<-S*((S2)+(S3)+S4+S5)
  #Creating a loop over the whole array
    for(i in 1:n){
      for (j in 1:n){
        #defining the change in energy at each point
        dE<-energychange(S,i,j)
        #By default each point does not flip
        accept<-FALSE
        #If energy decreases spin flip occurs 100% of the time
        if (dE<0){
          accept<-TRUE
        }
        #If energy increases a spin flip will occur if w is greater than or equal
        #to u.
        if (dE>=0){
          w<-exp(-beta*dE)
          u<-runif(1,0,1)
          if (w>=u){ 
            accept<-TRUE
        }
        }
      #If no spin flip occurs there is no change in energy
      if (accept==FALSE){
        dE<-0
      }
      #if spin flip does occur then the magnitude of each lattice point is flipped
      #from positive to negative or visa versa
      if (accept==TRUE){ 
        S[i,j]<--S[i,j]
      }
      }
  }
  return(S)
}

Maybe you can add the S to the global environment with <<- and then run a loop: 也许您可以使用<<-将S添加到全局环境中,然后运行循环:

Thermal<-function(S,t=0.000000000001,k=1){
  #Defing beta
  beta<-1/(k*t)
  #multiplying each point of the array by all its adjacent points 
  #and summing them
  Spointenergy<-S*((S2)+(S3)+S4+S5)
  #Creating a loop over the whole array
    for(i in 1:n){
      for (j in 1:n){
        #defining the change in energy at each point
        dE<-energychange(S,i,j)
        #By default each point does not flip
        accept<-FALSE
        #If energy decreases spin flip occurs 100% of the time
        if (dE<0){
          accept<-TRUE
        }
        #If energy increases a spin flip will occur if w is greater than or equal
        #to u.
        if (dE>=0){
          w<-exp(-beta*dE)
          u<-runif(1,0,1)
          if (w>=u){ 
            accept<-TRUE
        }
        }
      #If no spin flip occurs there is no change in energy
      if (accept==FALSE){
        dE<-0
      }
      #if spin flip does occur then the magnitude of each lattice point is flipped
      #from positive to negative or visa versa
      if (accept==TRUE){ 
        S[i,j]<--S[i,j]
      }
      }
  }
  return(S)
  S<<-S
}

for (x in 1:10) {
  Thermal(S)
}
S

Example with iris dataset: 虹膜数据集的示例:

> iris_num <- iris[,-5]
> head(iris_num)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.1         3.5          1.4         0.2
2          4.9         3.0          1.4         0.2
3          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2
6          5.4         3.9          1.7         0.4
> 
> testfunction <- function(iris_num)
+ {
+   iris_num <<- iris_num + 1
+   }
> 
> testfunction(iris_num)
> head(iris_num)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          6.1         4.5          2.4         1.2
2          5.9         4.0          2.4         1.2
3          5.7         4.2          2.3         1.2
4          5.6         4.1          2.5         1.2
5          6.0         4.6          2.4         1.2
6          6.4         4.9          2.7         1.4
> 
> for (x in 1:10) {
+   testfunction(iris_num)
+ }
> 
> head(iris_num)
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1         16.1        14.5         12.4        11.2
2         15.9        14.0         12.4        11.2
3         15.7        14.2         12.3        11.2
4         15.6        14.1         12.5        11.2
5         16.0        14.6         12.4        11.2
6         16.4        14.9         12.7        11.4
> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM