Can't use commons math complex numbers

Hello.
I recently downloaded commons math liubrary for java and added it to processing. I can import it fine, but when I try writing code the NullPointerException error is sent.
I tried creating a simple complex function:

float g(float x){
  float output = 0;
  output = sin(x * (3 / PI));
  return output;
}

Complex[] g_hat(float t_start, float t_stop, float t_step){
  float duration = (t_stop - t_start) / t_step;
  
  Complex output[] = new Complex[(int)duration];
  
  for(float t = t_start; t < t_stop; t += t_step){
    Complex exponent = Complex.I.multiply(-TWO_PI * (1 / 3) * t);
    Complex val = exponent.exp().multiply(g(t));
    output[int((t * t_step) - t_start)] = val;
  }
  
  return output;
}

this should make a “wrapped” version of g(x) around the origin of the complex plane. when I call the function everything is fine:

Complex[] data_points;
data_points = g_hat(0, 10, 1);

but after that:

print((float)data_points[9].getReal());

is… also ok. The problem is that I want the third input (in this case 1) to be a decimal, e.g 0.1. but when I do that the print line throws an exception… The window is not responding and after I manually stop the process I get in the log:

Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help ? Troubleshooting.

What can I do?
Thanks in advance for any help, Arad.

1 Like

i cut it down to a simple array for test,
and one problem i see is your array index calculation.
pls play with this code where i use some println…
to see what you are doing:

float t_start = 0/*5*/, t_stop = 10, t_step= 0.1;
float duration = (t_stop - t_start) / t_step;
println("duration "+duration+" (int)duration "+(int)duration);

float output[] = new float[(int)duration];
int myidx = 0;
for (float t = t_start; t < t_stop; t += t_step) {
  int youridx = int((t * t_step) - t_start);
  println("t "+nf(t,1,1)+" youridx "+youridx+" myidx "+myidx);
  output[youridx] = t;
  output[myidx] = t;
  myidx++;
}
// now try to set t_start to 5

2 Likes

The problem could be solved by making this next adjustment:

int youridx = int((t - t_start)*t_step);

Kf

2 Likes