Logo

Programming-Idioms

  • Dart
  • Java
  • Java
  • Python

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.

from math import cos
def f(a, b, c, d):
    return e * (a + (b * c) + cos(d))
a = list(map(f, a, b, c, d))
import math
for i in xrange(len(a)):
	a[i] = e*(a[i] + b[i] + c[i] + math.cos(a[i]))
from math import cos
f = lambda a, b, c, d: \
    e * (a + (b * c) + cos(d))
a = list(map(f, a, b, c, d))
import math
a = [e*(a[i] + b[i] + c[i] + math.cos(d[i])) for i in range(len(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