[ask] how to use an array of double in Java Processing

hi everyone!

i have some project about using svm the coding using C++, ii wanna use this parameter in my java processing because i needed to control robot using leap motion and the svm as the decision maker. the array of double send error.

in the processing web they said

Processing functions don’t use this datatype, so while they work in the language, you’ll usually have to convert to a float using the (float) syntax before passing into a function

please help! i really stuck

double param1[6][14] = {{1,1,1,1,0.792019601,1,1,-1,-1,-1,-1,-1,-0.792019601,-1},
						{-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
						{-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
						{-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558}};
double param2[6][18] = {{1,1,1,1,0.318464496,1,1,1,0.769380154,-1,-1,-1,-1,-0.087844649,-1,-1,-1,-1},
						{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
						{-0.25701,-0.21729,-0.09348,-0.08653,0.145652,0.062454,-0.01667,0.079085,0.0012,-0.4499,-0.44327,-0.46571,-0.42028,-0.42201,-0.44852,-0.41926,-0.39713,-0.40579},
						{-121085,-116608,-107282,-112798,-0.88837,-0.94401,-107663,-0.90106,-115809,-0.84784,-0.83931,-0.80587,-0.70346,-0.70533,-0.92365,-0.9593,-0.97531,-0.94895},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568}};
double param3[6][19] = {{1,1,1,1,0.456578608,1,1,1,1,-1,-0.187059758,-1,-1,-1,-1,-1,-1,-1,-0.26951885},
						{1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
						{-1.25614,-1.19248,-0.994,-0.98286,-0.66881,-0.87087,-0.65989,-0.84223,-0.7936,-1.22893,-1.19252,-1.29349,-0.98669,-0.98892,-1.18328,-1.36766,-1.32095,-0.24681,0.483479},
						{0.323382,0.354186,0.418358,0.380401,0.487666,0.415735,0.365585,0.359688,0.345772,0.074203,-0.00114,0.093422,0.007426,-0.04313,0.033755,0.055782,0.035636,-0.17976,-0.42344},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{-0.02824,-0.02825,-0.02826,-0.02827,-0.02828,-0.02829,-0.02830,-0.02831,-0.02832,-0.02833,-0.02834,-0.02835,-0.02836,-0.02837,-0.02838,-0.02839,-0.02840,-0.02841,-0.02842}};
double param4[6][20] = {{1,1,1,1,1,1,0.463702921,1,1,1,-1,-1,-0.705748012,-1,-1,-1,-1,-1,-1,-0.75795491},
						{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1},
						{1.017038,1.046933,0.902235,0.736103,0.860032,0.736503,0.724111,0.655464,0.67848,0.713734,1.140196,1.150551,1.115492,1.14235,1.18807,1.222666,1.209121,0.924121,0.126399,-0.54345},
						{-0.37552,-0.41018,-0.42873,-0.4702,-0.46058,-0.45412,-0.49043,-0.37792,-0.39576,-0.40371,-0.06427,-0.05869,-0.03677,-0.11394,-0.1373,-0.14779,-0.13052,-0.01386,0.167516,0.318457},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{-0.24815,-0.24816,-0.24817,-0.24818,-0.24819,-0.24820,-0.24821,-0.24822,-0.24823,-0.24824,-0.24825,-0.24826,-0.24827,-0.24828,-0.24829,-0.24830,-0.24831,-0.24832,-0.24833,-0.24834}};
double param5[6][16] = {{1,1,1,1,1,1,0.883359223,1,-1,-1,-1,-1,-0.883359223,-1,-1,-1},
						{-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1},
						{0.121326,0.140013,0.049562,-0.05429,-0.04718,0.02318,-0.09362,-0.06178,0.403332,0.425519,0.363987,0.382795,0.394049,0.43115,0.31879,0.34725},
						{1.162903,1.107373,1.07764,1.011192,0.96486,1.026602,0.969164,0.978771,0.900346,0.785385,0.929671,0.679563,0.646683,0.83863,0.872239,0.8415},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{-0.98695,-0.98696,-0.98697,-0.98698,-0.98699,-0.98700,-0.98701,-0.98702,-0.98703,-0.98704,-0.98705,-0.98706,-0.98707,-0.98708,-0.98709,-0.98710}};
double param6[6][16] = {{1,1,1,0.339561015,1,1,1,1,-1,-1,-1,-1,-0.339561015,-1,-1,-1},
						{-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,-1},
						{-0.20354,-0.19362,-0.2272,-0.15921,-0.20148,-0.15769,-0.12455,-0.13752,0.110591,0.144587,0.050308,0.079125,0.096368,0.153214,-0.01894,0.024663},
						{0.168873,0.173118,0.18977,0.240751,0.131133,0.113386,0.105415,0.118537,-0.19213,-0.24665,-0.17823,-0.29683,-0.31242,-0.2214,-0.20546,-0.22004},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0.03956,0.03957,0.03958,0.03959,0.03960,0.03961,0.03962,0.03963,0.03964,0.03965,0.03966,0.03967,0.03968,0.03969,0.03970,0.03971}};

void setX1(double x1[14]){
	for(int i=0; i<14; i++){
		param1[4][i] =  x1[i]; 		
	}
}

void setX2(double x1[18]){
	for(int i=0; i<18; i++){
		param2[4][i] =  x1[i]; 		
	}
}

void setX3(double x1[19]){
	for(int i=0; i<19; i++){
		param3[4][i] =  x1[i]; 		
	}
}

void setX4(double x1[20]){
	for(int i=0; i<20; i++){
		param4[4][i] =  x1[i]; 		
	}
}

void setX5(double x1[16]){
	for(int i=0; i<16; i++){
		param5[4][i] =  x1[i]; 		
	}
}

void setX6(double x1[16]){
	for(int i=0; i<16; i++){
		param6[4][i] =  x1[i]; 		
	}
}

int calculate(){
	int resultClass;
	int numInClass[] = {0,0,0,0} ;
	
	double sf1, sf2, sf3, sf4, sf5, sf6;
	
	for(int i=0; i<14; i++){
		sf1 += 	(param1[0][i] * param1[1][i] * (param1[2][i] + param1[3][i]) * param1[4][i]) + param1[5][i];
	}
	
	for(int i=0; i<18; i++){
		sf2 += 	(param2[0][i] * param2[1][i] * (param2[2][i] + param2[3][i]) * param2[4][i]) + param2[5][i];
	}
	
	for(int i=0; i<19; i++){
		sf3 += 	(param3[0][i] * param3[1][i] * (param3[2][i] + param3[3][i]) * param3[4][i]) + param3[5][i];
	}
	
	for(int i=0; i<20; i++){
		sf4 += 	(param4[0][i] * param4[1][i] * (param4[2][i] + param4[3][i]) * param4[4][i]) + param4[5][i];
	}
	
	for(int i=0; i<16; i++){
		sf5 += 	(param5[0][i] * param5[1][i] * (param5[2][i] + param5[3][i]) * param5[4][i]) + param5[5][i];
	}
	
	for(int i=0; i<16; i++){
		sf6 += 	(param6[0][i] * param6[1][i] * (param6[2][i] + param6[3][i]) * param6[4][i]) + param6[5][i];
	}
	
	if(sf1 > 0) numInClass[0]++;
	else numInClass[1]++;
	
	if(sf2 > 0) numInClass[0]++;
	else numInClass[2]++;
	
	if(sf3 > 0) numInClass[0]++;
	else numInClass[3]++;
	
	if(sf4 > 0) numInClass[1]++;
	else numInClass[2]++;
	
	if(sf5 > 0) numInClass[1]++;
	else numInClass[3]++;
	
	if(sf6 > 0) numInClass[2]++;
	else numInClass[3]++;
	
	cout<<"Class 1 : "<<numInClass[0]<<"\n";
	cout<<"Class 2 : "<<numInClass[1]<<"\n";
	cout<<"Class 3 : "<<numInClass[2]<<"\n";
	cout<<"Class 4 : "<<numInClass[3]<<"\n";
	
	int max = 0;
	for(int i=0; i<4; i++){
		if(numInClass[i] > max){
			max = numInClass[i];
			resultClass = i+1;
		}
	}
	
	
	cout<<"\nMax: "<<max;

	return resultClass;
} 

int main(){
	cout<<"\nResult: "<<calculate();
	return 0;	
}

In Java, when we declare a variable as of datatype array, we don’t specify its dimension there.
We merely use empty [] square brackets. :coffee:

So instead of C’s double param1[6][14] = {{.
In Java we remove those dimension values at the left side of the assignment: double param1[][] = {{.
Or even better, place the square brackets right after its datatype: double[][] param1 = {{.

final double[][] param1 = {
  {1, 1, 1, 1, 0.792019601, 1, 1, -1, -1, -1, -1, -1, -0.792019601, -1}, 
  {-1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1}, 
  {-1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1}, 
  {-1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1}, 
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
  {0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558}
};
  1. Processing.org/reference/Array.html
  2. Docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
2 Likes

The code that you provided is in C++. You need to migrate it to Java. For instance, remove the main function. Is not needed as Processing provides one for you under the hood. Also you need to replace;

cout<<"Class 1 : "<<numInClass[0]<<"\n";

for

println("Class 1 : ",numInClass[0],"\n");

You might need to fix some other lines.

Kf

1 Like

@kfrajer, a more precise conversion of cout<<"Class 1 : "<<numInClass[0]<<"\n";
to Processing’s Java Mode would be: println("Class 1 :", numInClass[0]);
Actually, printArray() would be much more succinct: printArray(numInClass); :stuck_out_tongue:

Processing.org/reference/printArray_.html

1 Like

Yes, @GoToLoop, you are right. However, I believe it is better to keep the conversion as simple as possible, specially for somebody coming from C++. However, my conversion sort of fail as I used println together with the new line char ‘\n’. I’d rather wait for the OP to provide an update on his migration before providing any other feedback.

Kf

You’re right. However, I’ve noticed there are 6 2D arrays, named from param1 to param6. :nerd_face:

They can all easily become 1 unified 3D array instead: :flushed:

final double[][][] params = { // [6]x[6]x[]
  { // param1[14]
    { 1, 1, 1, 1, 0.792019601, 1, 1, -1, -1, -1, -1, -1, -0.792019601, -1 }, 
    { -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1 }, 
    { -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1 }, 
    { -1, -1, -1, -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1 }, 
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
    { 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558, 0.033558 }
  }, 

  { // param2[18]
    { 1, 1, 1, 1, 0.318464496, 1, 1, 1, 0.769380154, -1, -1, -1, -1, -0.087844649, -1, -1, -1, -1 }, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, 
    { -0.25701, -0.21729, -0.09348, -0.08653, 0.145652, 0.062454, -0.01667, 0.079085, 0.0012, -0.4499, -0.44327, -0.46571, -0.42028, -0.42201, -0.44852, -0.41926, -0.39713, -0.40579 }, 
    { -121085, -116608, -107282, -112798, -0.88837, -0.94401, -107663, -0.90106, -115809, -0.84784, -0.83931, -0.80587, -0.70346, -0.70533, -0.92365, -0.9593, -0.97531, -0.94895 }, 
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
    { -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568, -0.86568 }
  }, 

  { // param3[19]
    { 1, 1, 1, 1, 0.456578608, 1, 1, 1, 1, -1, -0.187059758, -1, -1, -1, -1, -1, -1, -1, -0.26951885 }, 
    { 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }, 
    { -1.25614, -1.19248, -0.994, -0.98286, -0.66881, -0.87087, -0.65989, -0.84223, -0.7936, -1.22893, -1.19252, -1.29349, -0.98669, -0.98892, -1.18328, -1.36766, -1.32095, -0.24681, 0.483479 }, 
    { 0.323382, 0.354186, 0.418358, 0.380401, 0.487666, 0.415735, 0.365585, 0.359688, 0.345772, 0.074203, -0.00114, 0.093422, 0.007426, -0.04313, 0.033755, 0.055782, 0.035636, -0.17976, -0.42344 }, 
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
    { -0.02824, -0.02825, -0.02826, -0.02827, -0.02828, -0.02829, -0.02830, -0.02831, -0.02832, -0.02833, -0.02834, -0.02835, -0.02836, -0.02837, -0.02838, -0.02839, -0.02840, -0.02841, -0.02842 }
  }, 

  { // param4[20]
    { 1, 1, 1, 1, 1, 1, 0.463702921, 1, 1, 1, -1, -1, -0.705748012, -1, -1, -1, -1, -1, -1, -0.75795491 }, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1 }, 
    { 1.017038, 1.046933, 0.902235, 0.736103, 0.860032, 0.736503, 0.724111, 0.655464, 0.67848, 0.713734, 1.140196, 1.150551, 1.115492, 1.14235, 1.18807, 1.222666, 1.209121, 0.924121, 0.126399, -0.54345 }, 
    { -0.37552, -0.41018, -0.42873, -0.4702, -0.46058, -0.45412, -0.49043, -0.37792, -0.39576, -0.40371, -0.06427, -0.05869, -0.03677, -0.11394, -0.1373, -0.14779, -0.13052, -0.01386, 0.167516, 0.318457 }, 
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
    { -0.24815, -0.24816, -0.24817, -0.24818, -0.24819, -0.24820, -0.24821, -0.24822, -0.24823, -0.24824, -0.24825, -0.24826, -0.24827, -0.24828, -0.24829, -0.24830, -0.24831, -0.24832, -0.24833, -0.24834 }
  }, 

  { // param5[16]
    { 1, 1, 1, 1, 1, 1, 0.883359223, 1, -1, -1, -1, -1, -0.883359223, -1, -1, -1 }, 
    { -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1 }, 
    { 0.121326, 0.140013, 0.049562, -0.05429, -0.04718, 0.02318, -0.09362, -0.06178, 0.403332, 0.425519, 0.363987, 0.382795, 0.394049, 0.43115, 0.31879, 0.34725 }, 
    { 1.162903, 1.107373, 1.07764, 1.011192, 0.96486, 1.026602, 0.969164, 0.978771, 0.900346, 0.785385, 0.929671, 0.679563, 0.646683, 0.83863, 0.872239, 0.8415 }, 
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
    { -0.98695, -0.98696, -0.98697, -0.98698, -0.98699, -0.98700, -0.98701, -0.98702, -0.98703, -0.98704, -0.98705, -0.98706, -0.98707, -0.98708, -0.98709, -0.98710 }
  }, 

  { // param6[16]
    { 1, 1, 1, 0.339561015, 1, 1, 1, 1, -1, -1, -1, -1, -0.339561015, -1, -1, -1 }, 
    { -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1 }, 
    { -0.20354, -0.19362, -0.2272, -0.15921, -0.20148, -0.15769, -0.12455, -0.13752, 0.110591, 0.144587, 0.050308, 0.079125, 0.096368, 0.153214, -0.01894, 0.024663 }, 
    { 0.168873, 0.173118, 0.18977, 0.240751, 0.131133, 0.113386, 0.105415, 0.118537, -0.19213, -0.24665, -0.17823, -0.29683, -0.31242, -0.2214, -0.20546, -0.22004 }, 
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 
    { 0.03956, 0.03957, 0.03958, 0.03959, 0.03960, 0.03961, 0.03962, 0.03963, 0.03964, 0.03965, 0.03966, 0.03967, 0.03968, 0.03969, 0.03970, 0.03971 }
  }
};

This way, this statement: sf1 += (param1[0][i] * param1[1][i] * (param1[2][i] + param1[3][i]) * param1[4][i]) + param1[5][i];

Becomes: sf1 += (params[0][0][i] * params[0][1][i] * (params[0][2][i] + params[0][3][i]) * params[0][4][i]) + params[0][5][i];

1 Like

thank you @GoToLoop and @kfrajer sorry about the code, i’m really new in java programming btw i already tried your code and it works, but still got error in the resultClass am i need it in an array or something

int max = 0;
  for(int i=0; i<4; i++){
    if(numInClass[i] > max){
      max = numInClass[i];
      resultClass = [i] + 1;
    }
  }
  
  println ("\nMax: ",max);

  return resultClass;
} 

What is that line meant to be?! Looks like a syntax error in C and Java. Maybe just i + 1?

its the target class that i wanna get

if i chage it to i+1 the resultClass not initialized.

and if the resultClass in an array like int [] resultClass
the resultClass just doesn’t match with int[]

In the original code you posted it’s an int.

this is my program, but the sf1 trough sf6 still got an error does noot initialize, am i still lose something?

thank you

final double[][] param1 ={
            {1,1,1,1,0.792019601,1,1,-1,-1,-1,-1,-1,-0.792019601,-1},
            {-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
            {-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
            {-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558}
          };
final double[][] param2 ={
            {1,1,1,1,0.318464496,1,1,1,0.769380154,-1,-1,-1,-1,-0.087844649,-1,-1,-1,-1},
            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
            {-0.25701,-0.21729,-0.09348,-0.08653,0.145652,0.062454,-0.01667,0.079085,0.0012,-0.4499,-0.44327,-0.46571,-0.42028,-0.42201,-0.44852,-0.41926,-0.39713,-0.40579},
            {-121085,-116608,-107282,-112798,-0.88837,-0.94401,-107663,-0.90106,-115809,-0.84784,-0.83931,-0.80587,-0.70346,-0.70533,-0.92365,-0.9593,-0.97531,-0.94895},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568}
          };
final double[][] param3 ={
            {1,1,1,1,0.456578608,1,1,1,1,-1,-0.187059758,-1,-1,-1,-1,-1,-1,-1,-0.26951885},
            {1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
            {-1.25614,-1.19248,-0.994,-0.98286,-0.66881,-0.87087,-0.65989,-0.84223,-0.7936,-1.22893,-1.19252,-1.29349,-0.98669,-0.98892,-1.18328,-1.36766,-1.32095,-0.24681,0.483479},
            {0.323382,0.354186,0.418358,0.380401,0.487666,0.415735,0.365585,0.359688,0.345772,0.074203,-0.00114,0.093422,0.007426,-0.04313,0.033755,0.055782,0.035636,-0.17976,-0.42344},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.02824,-0.02825,-0.02826,-0.02827,-0.02828,-0.02829,-0.02830,-0.02831,-0.02832,-0.02833,-0.02834,-0.02835,-0.02836,-0.02837,-0.02838,-0.02839,-0.02840,-0.02841,-0.02842}
          };
final double[][] param4 ={
            {1,1,1,1,1,1,0.463702921,1,1,1,-1,-1,-0.705748012,-1,-1,-1,-1,-1,-1,-0.75795491},
            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1},
            {1.017038,1.046933,0.902235,0.736103,0.860032,0.736503,0.724111,0.655464,0.67848,0.713734,1.140196,1.150551,1.115492,1.14235,1.18807,1.222666,1.209121,0.924121,0.126399,-0.54345},
            {-0.37552,-0.41018,-0.42873,-0.4702,-0.46058,-0.45412,-0.49043,-0.37792,-0.39576,-0.40371,-0.06427,-0.05869,-0.03677,-0.11394,-0.1373,-0.14779,-0.13052,-0.01386,0.167516,0.318457},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.24815,-0.24816,-0.24817,-0.24818,-0.24819,-0.24820,-0.24821,-0.24822,-0.24823,-0.24824,-0.24825,-0.24826,-0.24827,-0.24828,-0.24829,-0.24830,-0.24831,-0.24832,-0.24833,-0.24834}
          };
final double[][] param5 ={
            {1,1,1,1,1,1,0.883359223,1,-1,-1,-1,-1,-0.883359223,-1,-1,-1},
            {-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1},
            {0.121326,0.140013,0.049562,-0.05429,-0.04718,0.02318,-0.09362,-0.06178,0.403332,0.425519,0.363987,0.382795,0.394049,0.43115,0.31879,0.34725},
            {1.162903,1.107373,1.07764,1.011192,0.96486,1.026602,0.969164,0.978771,0.900346,0.785385,0.929671,0.679563,0.646683,0.83863,0.872239,0.8415},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.98695,-0.98696,-0.98697,-0.98698,-0.98699,-0.98700,-0.98701,-0.98702,-0.98703,-0.98704,-0.98705,-0.98706,-0.98707,-0.98708,-0.98709,-0.98710}
          };
final double[][] param6 ={
            {1,1,1,0.339561015,1,1,1,1,-1,-1,-1,-1,-0.339561015,-1,-1,-1},
            {-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,-1},
            {-0.20354,-0.19362,-0.2272,-0.15921,-0.20148,-0.15769,-0.12455,-0.13752,0.110591,0.144587,0.050308,0.079125,0.096368,0.153214,-0.01894,0.024663},
            {0.168873,0.173118,0.18977,0.240751,0.131133,0.113386,0.105415,0.118537,-0.19213,-0.24665,-0.17823,-0.29683,-0.31242,-0.2214,-0.20546,-0.22004},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0.03956,0.03957,0.03958,0.03959,0.03960,0.03961,0.03962,0.03963,0.03964,0.03965,0.03966,0.03967,0.03968,0.03969,0.03970,0.03971}
          };

void setX1(double [] x1){
  for(int i=0; i<14; i++){
    param1[4][i] =  x1[i];     
  }
}

void setX2(double [] x1){
  for(int i=0; i<18; i++){
    param2[4][i] =  x1[i];     
  }
}

void setX3(double [] x1){
  for(int i=0; i<19; i++){
    param3[4][i] =  x1[i];     
  }
}

void setX4(double [] x1){
  for(int i=0; i<20; i++){
    param4[4][i] =  x1[i];     
  }
}

void setX5(double [] x1){
  for(int i=0; i<16; i++){
    param5[4][i] =  x1[i];     
  }
}

void setX6(double [] x1){
  for(int i=0; i<16; i++){
    param6[4][i] =  x1[i];     
  }
}

int calculate(){
  int resultClass;
  int [] numInClass = {0,0,0,0} ;
  
  double  sf1, sf2, sf3, sf4, sf5, sf6;
  
  for(int i=0; i<14; i++){
    ;
  }
  
  for(int i=0; i<18; i++){
    sf2 +=   (param2[0][i] * param2[1][i] * (param2[2][i] + param2[3][i]) * param2[4][i]) + param2[5][i];
  }
  
  for(int i=0; i<19; i++){
    sf3 +=   (param3[0][i] * param3[1][i] * (param3[2][i] + param3[3][i]) * param3[4][i]) + param3[5][i];
  }
  
  for(int i=0; i<20; i++){
    sf4 +=   (param4[0][i] * param4[1][i] * (param4[2][i] + param4[3][i]) * param4[4][i]) + param4[5][i];
  }
  
  for(int i=0; i<16; i++){
    sf5 +=   (param5[0][i] * param5[1][i] * (param5[2][i] + param5[3][i]) * param5[4][i]) + param5[5][i];
  }
  
  for(int i=0; i<16; i++){
    sf6 +=   (param6[0][i] * param6[1][i] * (param6[2][i] + param6[3][i]) * param6[4][i]) + param6[5][i];
  }
  
  if(sf1 > 0) numInClass[0]++;
  else numInClass[1]++;
  
  if(sf2 > 0) numInClass[0]++;
  else numInClass[2]++;
  
  if(sf3 > 0) numInClass[0]++;
  else numInClass[3]++;
  
  if(sf4 > 0) numInClass[1]++;
  else numInClass[2]++;
  
  if(sf5 > 0) numInClass[1]++;
  else numInClass[3]++;
  
  if(sf6 > 0) numInClass[2]++;
  else numInClass[3]++;
  
  printArray(numInClass);
  
  int max = 0;
  for(int i=0; i<4; i++){
    if(numInClass[i] > max){
      max = numInClass[i];
      resultClass = i + 1;
    }
  }
  
  println ("\nMax: ",max);

  return resultClass;
} 

int main(){
  println("\nResult: ", calculate());
  return 0;  
}

yes i use an int, the int [] just example if i change the code

I got it to compile and run making 3 changes. Whether it does what you want is another matter! :smile:

Local variables must be initialised with a value before being accessed. So,

int resultClass; 

should be

int resultClass = 0;

and

double  sf1, sf2, sf3, sf4, sf5, sf6;

should be

double  sf1 = 0, sf2 = 0, sf3 = 0, sf4 = 0, sf5 = 0, sf6 = 0;

And you need to replace main with setup() for Processing purposes. It doesn’t return anything.

void setup(){
  println("\nResult: ", calculate());
}
1 Like

thank you @neilcsmith you’re rock. its okay for now…i’ll try with my main code

1 Like
//fix code
final double[][] param1 ={
            {1,1,1,1,0.792019601,1,1,-1,-1,-1,-1,-1,-0.792019601,-1},
            {-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
            {-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
            {-1,-1,-1,-1,-1,-1,-1,1,1,1,1,1,1,1},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558,0.033558}
          };
final double[][] param2 ={
            {1,1,1,1,0.318464496,1,1,1,0.769380154,-1,-1,-1,-1,-0.087844649,-1,-1,-1,-1},
            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
            {-0.25701,-0.21729,-0.09348,-0.08653,0.145652,0.062454,-0.01667,0.079085,0.0012,-0.4499,-0.44327,-0.46571,-0.42028,-0.42201,-0.44852,-0.41926,-0.39713,-0.40579},
            {-121085,-116608,-107282,-112798,-0.88837,-0.94401,-107663,-0.90106,-115809,-0.84784,-0.83931,-0.80587,-0.70346,-0.70533,-0.92365,-0.9593,-0.97531,-0.94895},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568,-0.86568}
          };
final double[][] param3 ={
            {1,1,1,1,0.456578608,1,1,1,1,-1,-0.187059758,-1,-1,-1,-1,-1,-1,-1,-0.26951885},
            {1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
            {-1.25614,-1.19248,-0.994,-0.98286,-0.66881,-0.87087,-0.65989,-0.84223,-0.7936,-1.22893,-1.19252,-1.29349,-0.98669,-0.98892,-1.18328,-1.36766,-1.32095,-0.24681,0.483479},
            {0.323382,0.354186,0.418358,0.380401,0.487666,0.415735,0.365585,0.359688,0.345772,0.074203,-0.00114,0.093422,0.007426,-0.04313,0.033755,0.055782,0.035636,-0.17976,-0.42344},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.02824,-0.02825,-0.02826,-0.02827,-0.02828,-0.02829,-0.02830,-0.02831,-0.02832,-0.02833,-0.02834,-0.02835,-0.02836,-0.02837,-0.02838,-0.02839,-0.02840,-0.02841,-0.02842}
          };
final double[][] param4 ={
            {1,1,1,1,1,1,0.463702921,1,1,1,-1,-1,-0.705748012,-1,-1,-1,-1,-1,-1,-0.75795491},
            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1},
            {1.017038,1.046933,0.902235,0.736103,0.860032,0.736503,0.724111,0.655464,0.67848,0.713734,1.140196,1.150551,1.115492,1.14235,1.18807,1.222666,1.209121,0.924121,0.126399,-0.54345},
            {-0.37552,-0.41018,-0.42873,-0.4702,-0.46058,-0.45412,-0.49043,-0.37792,-0.39576,-0.40371,-0.06427,-0.05869,-0.03677,-0.11394,-0.1373,-0.14779,-0.13052,-0.01386,0.167516,0.318457},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.24815,-0.24816,-0.24817,-0.24818,-0.24819,-0.24820,-0.24821,-0.24822,-0.24823,-0.24824,-0.24825,-0.24826,-0.24827,-0.24828,-0.24829,-0.24830,-0.24831,-0.24832,-0.24833,-0.24834}
          };
final double[][] param5 ={
            {1,1,1,1,1,1,0.883359223,1,-1,-1,-1,-1,-0.883359223,-1,-1,-1},
            {-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1},
            {0.121326,0.140013,0.049562,-0.05429,-0.04718,0.02318,-0.09362,-0.06178,0.403332,0.425519,0.363987,0.382795,0.394049,0.43115,0.31879,0.34725},
            {1.162903,1.107373,1.07764,1.011192,0.96486,1.026602,0.969164,0.978771,0.900346,0.785385,0.929671,0.679563,0.646683,0.83863,0.872239,0.8415},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {-0.98695,-0.98696,-0.98697,-0.98698,-0.98699,-0.98700,-0.98701,-0.98702,-0.98703,-0.98704,-0.98705,-0.98706,-0.98707,-0.98708,-0.98709,-0.98710}
          };
final double[][] param6 ={
            {1,1,1,0.339561015,1,1,1,1,-1,-1,-1,-1,-0.339561015,-1,-1,-1},
            {-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,1,-1,-1},
            {-0.20354,-0.19362,-0.2272,-0.15921,-0.20148,-0.15769,-0.12455,-0.13752,0.110591,0.144587,0.050308,0.079125,0.096368,0.153214,-0.01894,0.024663},
            {0.168873,0.173118,0.18977,0.240751,0.131133,0.113386,0.105415,0.118537,-0.19213,-0.24665,-0.17823,-0.29683,-0.31242,-0.2214,-0.20546,-0.22004},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0.03956,0.03957,0.03958,0.03959,0.03960,0.03961,0.03962,0.03963,0.03964,0.03965,0.03966,0.03967,0.03968,0.03969,0.03970,0.03971}
          };

void setX1(double [] x1){
  for(int i=0; i<14; i++){
    param1[4][i] =  x1[i];     
  }
}

void setX2(double [] x1){
  for(int i=0; i<18; i++){
    param2[4][i] =  x1[i];     
  }
}

void setX3(double [] x1){
  for(int i=0; i<19; i++){
    param3[4][i] =  x1[i];     
  }
}

void setX4(double [] x1){
  for(int i=0; i<20; i++){
    param4[4][i] =  x1[i];     
  }
}

void setX5(double [] x1){
  for(int i=0; i<16; i++){
    param5[4][i] =  x1[i];     
  }
}

void setX6(double [] x1){
  for(int i=0; i<16; i++){
    param6[4][i] =  x1[i];     
  }
}

int calculate(){
  int resultClass = 0;
  int [] numInClass = {0,0,0,0} ;
  
  double sf1 = 0, sf2 = 0, sf3 = 0, sf4 = 0, sf5 = 0, sf6 = 0;
  
  for(int i=0; i<14; i++){
    sf1 +=   (param1[0][i] * param1[1][i] * (param1[2][i] + param1[3][i]) * param1[4][i]) + param1[5][i];
  }
  
  for(int i=0; i<18; i++){
    sf2 +=   (param2[0][i] * param2[1][i] * (param2[2][i] + param2[3][i]) * param2[4][i]) + param2[5][i];
  }
  
  for(int i=0; i<19; i++){
    sf3 +=   (param3[0][i] * param3[1][i] * (param3[2][i] + param3[3][i]) * param3[4][i]) + param3[5][i];
  }
  
  for(int i=0; i<20; i++){
    sf4 +=   (param4[0][i] * param4[1][i] * (param4[2][i] + param4[3][i]) * param4[4][i]) + param4[5][i];
  }
  
  for(int i=0; i<16; i++){
    sf5 +=   (param5[0][i] * param5[1][i] * (param5[2][i] + param5[3][i]) * param5[4][i]) + param5[5][i];
  }
  
  for(int i=0; i<16; i++){
    sf6 +=   (param6[0][i] * param6[1][i] * (param6[2][i] + param6[3][i]) * param6[4][i]) + param6[5][i];
  }
  
  if(sf1 > 0) numInClass[0]++;
  else numInClass[1]++;
  
  if(sf2 > 0) numInClass[0]++;
  else numInClass[2]++;
  
  if(sf3 > 0) numInClass[0]++;
  else numInClass[3]++;
  
  if(sf4 > 0) numInClass[1]++;
  else numInClass[2]++;
  
  if(sf5 > 0) numInClass[1]++;
  else numInClass[3]++;
  
  if(sf6 > 0) numInClass[2]++;
  else numInClass[3]++;
  
  printArray(numInClass);
  
  int max = 0;
  for(int i=0; i<4; i++){
    if(numInClass[i] > max){
      max = numInClass[i];
      resultClass = i + 1;
    }
  }
  
  println ("\nMax: ",max);

  return resultClass;
} 

void setup(){
  println("\nResult: ", calculate());
}

Pedantic: Local variables can’t be accessed before being initialized w/ some value. :face_with_monocle:

We can postpone their initialization. And even let them completely unused! :open_mouth:

Again, avoid absolute verbs like must, have to, gotta, etc. :flushed:

No! You’re right, being pedantic, that local variables must be initialized before being accessed (so yes, that can be delayed as long as the compiler can prove that a variable has been initialized before access). Doesn’t apply in this case.

I’m not stopping the use of absolute verbs with regards to language / compiler rules, etc. because that would be patently absurd!

I didn’t mean for n1 to stop using those verbs, but merely avoiding them when not absolutely sure! :upside_down_face:

Verbs like should or need to are more ambiguous. :smile:

I was absolutely sure! I don’t see the point in going into every little detail of the language spec when someone is new to the language, or being needlessly ambiguous in this case. But, deferring to the rules (note use of “must” :smile:)

Each local variable (§14.4) and every blank final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs.

https://docs.oracle.com/javase/specs/jls/se7/html/jls-16.html

But as you can see, the must is accompanied by a when condition. :stuck_out_tongue:

Pretty much what I’ve stated already: :innocent: