Results 1 to 5 of 5
 03152014, 01:05 AM #1Member
 Join Date
 Mar 2014
 Posts
 2
 Rep Power
 0
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.
Java 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[n1] + 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 <= n1; 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. } }
Last edited by mrjacko; 03152014 at 01:39 AM.
 03152014, 01:19 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,642
 Rep Power
 9
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.
Also, please place your code between [code][/code] tags.
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03152014, 01:44 AM #3Member
 Join Date
 Mar 2014
 Posts
 2
 Rep Power
 0
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?
 03152014, 01:57 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,642
 Rep Power
 9
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 03152014, 09:45 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,372
 Blog Entries
 7
 Rep Power
 25
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:
Java Code:int left(int i, int n) { return (i+n1)%n; } int right(int i, int n) { return (i+1)%n; }
JosThe only person who got everything done by Friday was Robinson Crusoe.
Similar Threads

Works in Netbeans, works in Eclipse, doesn't work after being compiled by either
By ZhiMing in forum New To JavaReplies: 18Last Post: 03262014, 11:43 PM 
Creating an array constructor and then creating an object in a main method
By kev3kev3 in forum New To JavaReplies: 4Last Post: 04022012, 01:50 PM 
Creating Array
By gnawk in forum New To JavaReplies: 3Last Post: 03062012, 03:04 AM 
Creating an Array of Objects
By int80 in forum New To JavaReplies: 4Last Post: 08092011, 12:40 PM 
How an array of Threads works...
By Moncleared in forum New To JavaReplies: 6Last Post: 09092009, 01:40 AM
Bookmarks