# Can't use commons math complex numbers

**URL:** <https://discourse.processing.org/t/cant-use-commons-math-complex-numbers/9124>\
**Category:** Libraries\
**Created:** [March 8, 2019, 7:43pm UTC](https://discourse.processing.org/t/cant-use-commons-math-complex-numbers/9124 "2019-03-08T19:43:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aradarbel10](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@aradarbel10](https://discourse.processing.org/u/aradarbel10)\
**Post date:** [March 8, 2019, 7:43pm UTC](https://discourse.processing.org/t/cant-use-commons-math-complex-numbers/9124/1 "2019-03-08T19:43:09Z")

</div>

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:

```auto
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:

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

```

but after that:

```auto
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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 8, 2019, 8:50pm UTC](https://discourse.processing.org/t/cant-use-commons-math-complex-numbers/9124/2 "2019-03-08T20:50:25Z")

</div>

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [March 9, 2019, 8:26am UTC](https://discourse.processing.org/t/cant-use-commons-math-complex-numbers/9124/3 "2019-03-09T08:26:31Z")

</div>

The problem could be solved by making this next adjustment:

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

Kf
