# Creating an array that works like a torus

• 03-15-2014, 01:05 AM
mrjacko
Creating an array that works like a torus
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.
Code:

```     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--) {  // this will work from the initial value of the array to the one before the one selected.                         if ((agentPosition[i] - agentPosition[j]) <= r) {                              numposition++;        // this sums the number of positions within the radius of the chosen position.                             total1 += agentPosition[j];  // this sums up all the values within the radius below to the total1.                         } 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.     }     }```
• 03-15-2014, 01:19 AM
jim829
Re: Creating an array that works like a torus
First, your code is extremely difficult to read. Mainly because it has too many comments and the comments are not always necessary. Only
comment what a section of code does if it is not apparent. Same is true for a statement.

Now it sounds like you want to use a circular array. If I am correct about what you want you need special logic to handle wrap around. You can also employ a circular linked list.

Regards,
Jim
• 03-15-2014, 01:44 AM
mrjacko
Re: Creating an array that works like a torus
I have deleted most of the comments and tidied the code up.

How would I go about using a wrap around? from what I am reading it will not be possible if i have used the array from the java default library. what would be the best option going forward? change to a circular linked list?
• 03-15-2014, 01:57 AM
jim829
Re: Creating an array that works like a torus
Let's say you are near the end of the array a of size 10 and you want four values. So you want a[8], a[9], a[0], a[1].

Notice that you can use the % operator using the length of the array as the modulus. So if J is 8 and len = 10, a[j % len], a[(j+1) %len], a[(j+2)%len], a[(j+3) %len]. You may want to use a variable instead of 1,2,3,etc.

I have only provided enough to give you an idea of how one might approach it. You may need to experiment with the logic or develop your own method.

Regards,
Jim
• 03-15-2014, 09:45 AM
JosAH
Re: Creating an array that works like a torus
If you define torus coordinates as: one element after the right most element is the left element and one element before the left most element is the right element, to simple methods will do:

Code:

```int left(int i, int n) { return (i+n-1)%n; } int right(int i, int n) { return (i+1)%n; }```
kind regards,

Jos