Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #208 Formula with arrays

Given the arrays a,b,c,d of equal length and the scalar e, calculate a = e*(a+b*c+cos(d)).
Store the results in a.

import static java.lang.Math.cos;
int i, n = a.length;
for (i = 0; i < n; ++i)
    a[i] = e * (a[0] + (b[1] * c[2]) + cos(d[3]));
import static java.lang.Math.cos;
import static java.util.stream.IntStream.range;
range(0, a.length)
    .forEach(i -> {
        a[i] = e * (a[0] + (b[1] * c[2]) + cos(d[3]));
    });
#include <math.h>
  for (int i=0; i<n; i++)
    a[i] = e*(a[i]+b[i]*c[i]+cos(d[i]));

Assume n is the length.

New implementation...
< >
tkoenig