Show all 4 posts from this thread on one page

A New Kind of Science: The NKS Forum (http://forum.wolframscience.com/index.php)
- Applied NKS (http://forum.wolframscience.com/forumdisplay.php?forumid=4)
-- how does this work? (http://forum.wolframscience.com/showthread.php?threadid=1488)


Posted by fbitonti on 12-10-2007 03:19 AM:

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.


Posted by Todd Rowland on 12-11-2007 02:01 PM:

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.


Posted by fbitonti on 12-11-2007 02:06 PM:

Thank you

Thank you very much I will give that a try.


Posted by fbitonti on 12-11-2007 03:16 PM:

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.



Show all 4 posts from this thread on one page

Powered by: vBulletin Version 2.3.0
Copyright © Jelsoft Enterprises Limited 2000 - 2002.