wolframscience.com

A New Kind of Science: The NKS Forum : Powered by vBulletin version 2.3.0 A New Kind of Science: The NKS Forum > Applied NKS > how does this work?
  Last Thread   Next Thread
Author
Thread Post New Thread    Post A Reply
fbitonti


Registered: Oct 2007
Posts: 3

how does this work?

Hello, I apologize if this isn't the correct forum for this kind of question but I hope someone will be able to help me. I am writing a java program that will let me do CA simulations I am in the process of validating the results against Mathematica and i noticed that my results are not quite the same. this is my Mathematica output.

x = CellularAutomaton[110, {0, 0, 0, 1, 0, 0, 0, 0}, 5];
MatrixForm[x]

{"0", "0", "0", "1", "0", "0", "0", "0"},
{"0", "0", "1", "1", "0", "0", "0", "0"},
{"0", "1", "1", "1", "0", "0", "0", "0"},
{"1", "1", "0", "1", "0", "0", "0", "0"},
{"1", "1", "1", "1", "0", "0", "0", "1"},
{"0", "0", "0", "1", "0", "0", "1", "1"}

however the output from my java debugger looks nothing like this.

this is my java code

public static int[] newMatrix;
public static int xc;
public static int jc;
public static int check;
public static int masterCount;

public static cellularAutomata calcCA( cellularAutomata x,int evolun) {
rows = x.sizeCA();

if(newMatrix==null)
{
newMatrix = new int[rows];

}

int caLOOP = 0;
xc = 0;
jc = 0;
while (xc < evolun){
jc = 0;
masterCount = 0;
//print($x);
while(jc < x.sizeCA()-1){
check = jc - 1;
if(check < 0){
check = 0;
}

if(x.getNumber(check) == 1 && x.getNumber(jc+1) == 1 && x.getNumber(jc) == 1){

newMatrix[jc] = i;
// x.setValue(jc,i);

}
if(x.getNumber(check) == 1 && x.getNumber(jc+1) == 0 && x.getNumber(jc) == 1){

newMatrix[jc] = j;
// x.setValue(jc,j);

}
if(x.getNumber(check) == 1 && x.getNumber(jc+1) == 1 && x.getNumber(jc) == 0){

newMatrix[jc] = k;
// x.setValue(jc,k);


}
if(x.getNumber(check) == 1 && x.getNumber(jc+1) == 0 && x.getNumber(jc) == 0){

newMatrix[jc] = l;
// x.setValue(jc,l);

}
if(x.getNumber(check) == 1 && x.getNumber(jc+1) == 0 && x.getNumber(jc) == 1){

newMatrix[jc] = m;
// x.setValue(jc,m);
}
if(x.getNumber(check) == 0 && x.getNumber(jc+1) == 0 && x.getNumber(jc) == 1){

newMatrix[jc] = n;
// x.setValue(jc,n);
}
if(x.getNumber(check) == 0 && x.getNumber(jc+1) == 1 && x.getNumber(jc) == 0){

newMatrix[jc] = o;
// x.setValue(jc,o);
}
if(x.getNumber(check) == 0 && x.getNumber(jc+1) == 0 && x.getNumber(jc) == 0){

newMatrix[jc] = p;
// x.setValue(jc,p);

}

jc= jc + 1;


}




xc = xc + 1;
jc = 0;

while(caLOOP < x.sizeCA()-1){

x.setValue(caLOOP,newMatrix[caLOOP]);
caLOOP = caLOOP + 1;
}
caLOOP = 0;



}



return new cellularAutomata(newMatrix);
}

if anyone can tell me where i went wrong I would appreciate it. I think it has something to o with how I treat the edges of the matrix because I only want to calculate for the width of the initial conditions. because the first three lines are always correct.

Report this post to a moderator | IP: Logged

Old Post 12-10-2007 03:19 AM
fbitonti is offline Click Here to See the Profile for fbitonti Edit/Delete Message Reply w/Quote
Todd Rowland
Wolfram Research
Maryland

Registered: Oct 2003
Posts: 103

Your guess is right. The boundaries are not being handled properly in the java code. To match the Mathematica input you need wrap around boundaries.

(Assuming x.sizeCA() is the number of columns.)

At the left boundary, the code fragment

check = jc - 1;
if(check < 0){
check = 0;
}

is telling it to use the leftmost cell as its own left neighbor (which is not right.)

While the rightmost cell is never updated because of these fragments

while(jc < x.sizeCA()-1){

while(caLOOP < x.sizeCA()-1){


Instead, there should be no minus one, and you should replace the above check by using

check=x.size(CA)-1;

to say that the left neighbor of the leftmost cell is the rightmost cell.

Then put in the analogous code for the rightmost cell.

Report this post to a moderator | IP: Logged

Old Post 12-11-2007 02:01 PM
Todd Rowland is offline Click Here to See the Profile for Todd Rowland Click here to Send Todd Rowland a Private Message Click Here to Email Todd Rowland Edit/Delete Message Reply w/Quote
fbitonti


Registered: Oct 2007
Posts: 3

Thank you

Thank you very much I will give that a try.

Report this post to a moderator | IP: Logged

Old Post 12-11-2007 02:06 PM
fbitonti is offline Click Here to See the Profile for fbitonti Edit/Delete Message Reply w/Quote
fbitonti


Registered: Oct 2007
Posts: 3

out of bounds

The size function i am using is always one greater than i need so I have to keep the - 1

I have gotten the left side of the matrix looking the way I want it however the right side is still wrong this is what my code looks like right now


public static int[] newMatrix;
public static int xc;
public static int jc;
public static int check;
public static int checkt;
public static int masterCount;

public static cellularAutomata calcCA( cellularAutomata x,int evolun) {
rows = x.sizeCA();

if(newMatrix==null)
{
newMatrix = new int[rows];

}

int caLOOP = 0;
xc = 0;
jc = 0;
while (xc < evolun){
jc = 0;
masterCount = 0;
//print($x);
while(jc < x.sizeCA()-1){
check = jc - 1;
if(check < 0){
check = x.sizeCA()-1;
}
checkt = jc + 1;
if(jc == x.sizeCA()-1){
checkt = 0;
}

if(x.getNumber(check) == 1 && x.getNumber(checkt) == 1 && x.getNumber(jc) == 1){

newMatrix[jc] = i;
// x.setValue(jc,i);

}
if(x.getNumber(check) == 1 && x.getNumber(checkt) == 0 && x.getNumber(jc) == 1){

newMatrix[jc] = j;
// x.setValue(jc,j);

}
if(x.getNumber(check) == 1 && x.getNumber(checkt) == 1 && x.getNumber(jc) == 0){

newMatrix[jc] = k;
// x.setValue(jc,k);


}
if(x.getNumber(check) == 1 && x.getNumber(checkt) == 0 && x.getNumber(jc) == 0){

newMatrix[jc] = l;
// x.setValue(jc,l);

}
if(x.getNumber(check) == 1 && x.getNumber(checkt) == 0 && x.getNumber(jc) == 1){

newMatrix[jc] = m;
// x.setValue(jc,m);
}
if(x.getNumber(check) == 0 && x.getNumber(checkt) == 0 && x.getNumber(jc) == 1){

newMatrix[jc] = n;
// x.setValue(jc,n);
}
if(x.getNumber(check) == 0 && x.getNumber(checkt) == 1 && x.getNumber(jc) == 0){

newMatrix[jc] = o;
// x.setValue(jc,o);
}
if(x.getNumber(check) == 0 && x.getNumber(checkt) == 0 && x.getNumber(jc) == 0){

newMatrix[jc] = p;
// x.setValue(jc,p);

}

jc= jc + 1;


}




xc = xc + 1;
jc = 0;

while(caLOOP < x.sizeCA()-1){

x.setValue(caLOOP,newMatrix[caLOOP]);
caLOOP = caLOOP + 1;
}
caLOOP = 0;



}



return new cellularAutomata(newMatrix);
}

any suggestions would be great. I appreciate the help with this. I am a student and this is the first time I have written something like this.

Report this post to a moderator | IP: Logged

Old Post 12-11-2007 03:16 PM
fbitonti is offline Click Here to See the Profile for fbitonti Edit/Delete Message Reply w/Quote
Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread


 

wolframscience.com  |  wolfram atlas  |  NKS online  |  web resources  |  contact us

Forum Sponsored by Wolfram Research

© 2004-13 Wolfram Research, Inc. | Powered by vBulletin 2.3.0 © 2000-2002 Jelsoft Enterprises, Ltd. | Disclaimer | Archives