I have create a program that takes a random array which is created by starting from 0 and adding Math.random() (double between 0 and 0.999) n times, and calculates the weighted average of each position within a certain radius. I currently have a program that does this but i was wondering how to create one using a torus. The basic principle is the last element is now equal to the first element and when the first element updates its position it takes into account the difference between the other elements including some of the last elements in the array.

Its not so much help with the coding but with the principle behind it, I cant work out how this would be possible for multiple iterations.

heres the code so far that works for one iteration. After one the code is incorrect and calculates the wrong values.

I think using a circular list or a ring buffer may work but i have little experience with either. Any help on the matter would be much appreciated.

import java.text.DecimalFormat; import java.util.Scanner; /** * Created by jameshales on 12/03/2014. */ public class Torus { public static void main(String[] args) { DecimalFormat df = new DecimalFormat("#.###"); System.out.println("how many numbers of agents on the real line?"); Scanner input = new Scanner(System.in); int n = 0; n=Integer.parseInt(input.nextLine()); double[] agentPosition = new double[n]; double[] newAgentPosition = new double[n]; double[] originalAgentPosition = new double[n]; System.out.println("Please select your desired radius? select 1 normally"); double r = 0; r = input.nextDouble(); int t = 0; // sets t to 0 double epsilon = 0.001; for (int i = 0; i <= n - 1; i++) { if (i > 0) agentPosition[i] = agentPosition[i - 1] + Math.random(); // this equation creates the random array else agentPosition[i] =0.0; } System.arraycopy(agentPosition,0,originalAgentPosition,0,n); while(true) { for (int i = 0; i <= n - 1; i++) { double total1 = agentPosition[i]; double total2 = 0; int numposition = 1; for (int j = i - 1; j >= 0; j--) { if ((agentPosition[i] - agentPosition[j]) <= r) { numposition++; total1 += agentPosition[j]; } else break; // stops the program once it has passed a position of a distance of 1 } for (int k = i + 1; k <= n - 1 ; k++) { if (Math.abs(agentPosition[k] - agentPosition[i]) <= r) { numposition++; total2 += agentPosition[k]; } else break; } for (int j = n - 2; j >= 1; j--) { if (((agentPosition[n-1] + agentPosition[i]) - agentPosition[j]) <= r) { numposition++; total1 += (agentPosition[j] - agentPosition[n - 1]); } else break; } for (int k = 1; k <= n - 2 ; k++) { if (Math.abs((agentPosition[i] - agentPosition[n - 1]) - agentPosition[k]) <= r) { / numposition++; total2 += (agentPosition[n - 1] + agentPosition[k]); } else break; } newAgentPosition[i] = (total1 + total2) / numposition; // this calculates the new weighted average. ( sum of assigned random variable/ sum of position) } for (int i = 0; i <= n - 1; i++){ if (newAgentPosition[i] > originalAgentPosition[n - 1]){ newAgentPosition[i] = newAgentPosition[i] - originalAgentPosition[n - 1]; } if(newAgentPosition[i] < 0) { newAgentPosition[i] = newAgentPosition[i] + originalAgentPosition[n - 1]; } } t++; // This sums up how many iterations it will take. double largestDiff = 0.0; // This assigns largestDiff to 0 for (int i = 0; i <= n-1; i++) { double diff = Math.abs(agentPosition[i] - newAgentPosition[i]); // This calculates the difference between the previous and current array at position i. if(diff > largestDiff) // If the difference between the agents is bigger than 0, assign it to the variable largestDiff. largestDiff = diff; } if(largestDiff <= epsilon){ // This checks if the difference is bigger than the set epsilon, break; // This stops the program if the difference is smaller than epsilon } agentPosition = new double[n]; System.arraycopy(newAgentPosition, 0, agentPosition, 0, n); } for (int i = 0 ; i <= n - 1; i++) { System.out.println(i + ": " + df.format(originalAgentPosition[i]) + "\t->\t" + df.format(agentPosition[i])); } int sumdofclusters = 1; // This sets the sum of clusters to 1 System.out.println("The different clusters are:\n" + df.format(agentPosition[0])); // This prints out the first cluster only. for (int i = 1; i <= n - 1 ; i++) { if(Math.abs(agentPosition[i] - agentPosition[i - 1]) >= epsilon) { // This checks if the element after the element at hand is different by a set epsilon.(how to work out different clusters) sumdofclusters++; // This sums the number of clusters. System.out.println(df.format(agentPosition[i])); // This prints out the different clusters other than the first 1. } } System.out.println("Number of clusters is:" + sumdofclusters); // This prints out the number of clusters. System.out.println("Number of iterations:" + t); // This prints out the number of iterations. } }