Logo

Programming-Idioms

  • Kotlin
  • C++
  • 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.

import math
a = [e*(a[i] + b[i] + c[i] + math.cos(d[i])) for i in range(len(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))
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
for i in xrange(len(a)):
	a[i] = e*(a[i] + b[i] + c[i] + math.cos(a[i]))
#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