# Product and sum operator

**URL:** <https://discourse.julialang.org/t/product-and-sum-operator/19743>\
**Category:** New to Julia\
**Created:** [January 17, 2019, 12:46pm UTC](https://discourse.julialang.org/t/product-and-sum-operator/19743 "2019-01-17T12:46:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ana814](https://avatars.discourse-cdn.com/v4/letter/a/e47c2d/32.png) [@Ana814](https://discourse.julialang.org/u/Ana814)\
**Post date:** [January 17, 2019, 12:46pm UTC](https://discourse.julialang.org/t/product-and-sum-operator/19743/1 "2019-01-17T12:46:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 17, 2019, 12:54pm UTC](https://discourse.julialang.org/t/product-and-sum-operator/19743/2 "2019-01-17T12:54:55Z")

</div>

> [@Ana814](#):
>
> 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?

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.
