0

I'm working on implementing a java method based on this correlation formula:

(f * g)[n] = ∑ f[n+m] * g[m], 

the lower bound of the summation 
is m = 0, and the upper bound is M - 1, 
where f is an array of size N 
and g is an array of size M. 
(f * g)[n] is an array of size N - M + 1

So for example if we have these arrays:

f = {1, 2, 3, 4}, g = {10, 20}
The result is this array: 
(f * g)[n] = {1(10) + 2(20), 2(10) + 3(20), 3(10) + 4(20)}
= **{50, 80, 110}**

What I need to do is convert this into Java code. I'm starting by trying to write the code for this example, and then later come up with a more general case. Unfortunately I'm stuck on some of the Java syntax. This is what I currently have:

public static void main(String[] args) {
    double[] array1 = new double[]  {1, 2, 3, 4, 5};
    double[] array2 = new double[] {10, 20}; 
    double[] array3 = new double[3];

    for (int i = 0; i < array1.length; i++) {
        for (int j = 0; j < array2.length; j++) {
            double pos = array1[i];
            double multiply = pos * array2[j];
            array3[i]= pos * multiply;

        }
    }
    System.out.print(Arrays.toString(array3));

}

I'm fairly certain the problem is with

array3[i] = pos * multiply; 

Essentially what I'm trying to do is to store the multiplication in array3's first index. Then as the loop goes on store it into the second and then third index. I know I'm doing it wrong, but I'm not sure how to approach it, and a third for loop seems confusing and impractical (and I'm not even sure if it would work). Thank you!

twobzombie
  • 25
  • 5
  • It seems something wrong with array bounds, not clear enough for me though – Vasily Liaskovsky Jan 29 '18 at 08:11
  • Why aren't you multiplying 5 by anything? – OneCricketeer Jan 29 '18 at 08:12
  • I suggest you use a debugger to step through your code to see what it is doing. Read https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems for some tips to get started. – Code-Apprentice Jan 29 '18 at 08:12
  • 1
    Whean asking about an erroryou get, always **read** and **post** the error. – JB Nizet Jan 29 '18 at 08:12
  • 1
    array3 has 3 elements. And i goes from 0 to 5. How could that work? – JB Nizet Jan 29 '18 at 08:13
  • I am asking for help figuring this out. – twobzombie Jan 29 '18 at 08:13
  • 1
    It looks to me like `array3` should have the same number of elements as `array1` (i.e. 5). It's unclear why you're only doing the summation three times over in your example. – Alnitak Jan 29 '18 at 08:14
  • @twobzombie the error you get tells you what's wrong, but you choose not to read the error, and not to post it, leaving us in the dark, forced to guess what the problem is. Post the error, so that we can tell you how to read it. – JB Nizet Jan 29 '18 at 08:14
  • @Alnitak Yah...I noticed that after posting my comment...deleted now. – Code-Apprentice Jan 29 '18 at 08:16
  • In addition to my suggestions about debugging, work through an example by hand and compare with what your program is doing. – Code-Apprentice Jan 29 '18 at 08:16
  • @Alnitak it is because array3 is of size N - M +1 – twobzombie Jan 29 '18 at 08:19
  • 1
    The error says: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3". So, you're trying to access an invalid index of an array, and this invalid index is 3. The **complete** error also tells you precisely *where* this error happens: at the line `array3[i]= pos * multiply;`. Now, why does it throw such an error? Because it tries to access index 3 of array3, which is of length 3 (and whose last valid index is thus 2). Why does it access element 3, because i goes from 0 included to 5 (the length of array1) included. Everything becomes simpler when you read error messages. – JB Nizet Jan 29 '18 at 08:19
  • Also note that, in case the name "ArrayIndexOutOfBoundsException" is not sufficient to understand what this error means, it's documented: https://docs.oracle.com/javase/8/docs/api/java/lang/ArrayIndexOutOfBoundsException.html – JB Nizet Jan 29 '18 at 08:21
  • @JBNizet Thanks, JB. I understand the error and why it is occurring. What I don't understand is how to make the program work. How to convert f[n+m] * g[m] into Java code. – twobzombie Jan 29 '18 at 08:23
  • 1
    Well, you need to store 5 values. But you're using an array of 3 elements? Wouldn't it be quite logical to use an array of 5 elements? – JB Nizet Jan 29 '18 at 08:24
  • @twobzombie the correction to the bounds of the output array now makes the question make more sense, but it still looks like you've one iteration too few. Why isn't 4*10 + 5*20 present as the fourth value in the output array? – Alnitak Jan 29 '18 at 08:26
  • @Alnitak You are right and I apologize. I wrote f incorrectly. It is only supposed to be {1,2,3,4}. This is my first post on here and I am still learning! – twobzombie Jan 29 '18 at 08:34
  • I've reopened the question because there are multiple issues here with the OP's algorithm, and not specifically with the array bounds exception. – Alnitak Jan 29 '18 at 08:42

1 Answers1

2

You're not setting the output array to the correct length, it needs to be calculated based on the two input lengths (per your N - M + 1) and not hard coded to three. [although I note that with your updated question that three was actually the correct number]

That is also the number of outer iterations you must make. Your out-of-bounds exception is being caused by your iteration from 0 .. N - 1 without taking M into account.

Your are also failing to actually perform any summation. You should set array3[i] to zero as the first operation inside the first loop [*] and then add the multiplication term inside the inner loop.

static double[] array_product(double[] a, double[] b) {
    int n = a.length - b.length + 1;
    double[] result = new double[n];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < b.length; ++j) {
            result[i] += a[i + j] * b[j];
        }
    }

    return result;
}

public static void main(String[] args) {
    double[] array1 = new double[]  {1, 2, 3, 4};
    double[] array2 = new double[] {10, 20};
    double[] result = array_product(array1, array2);

    System.out.print(Arrays.toString(result));
}

[*] although the Java language spec guarantees that a new array is set to 0.0, it's not a bad idea to do it explicitly both for clarity of the code and in case the code is ported to a language which does not make that guarantee.

Alnitak
  • 334,560
  • 70
  • 407
  • 495