Product and sum operator

Hello!
Suppose that I would like to write a function y = x_1^2 + x_2^2 + … + x_n^2. How can I integrate a sum operator in my function to sum from i = 1 to n?
Alternatively, if I would like to code y = x_1^2 * x_2^2 *… * x_n^2, how can I integrate the product operator to multiply the squares of x from i = 1 to n?

Use a different data structure and call sum(x -> x^2, x). If n is small, known at compile-time, and you want the performance of using local variables rather than a heap-allocated array, use the StaticArrays package.

1 Like