# Convert for-loop into mathematical expression

I'm facing a simple problem actually:

  for (int i = 0; i < noutput_items; i++){
out[i] = in[i] * in[i];
}


When I want to formalize this as a math expression, how do I issue the limits?

$$\sum_{i=0}^{noutput\_items} in_i$$ was my initial guess, but of course it's not a sum. It's a while ago when I last checked how sequences can be expressed with integrals or as sums... Could somebody help me to refresh my memory :)

Best, Marius

-
It is extremely unclear what are $\operatorname{in}[i], \operatorname{out}[i]$ and I assume that nooutput_items is a predefined constant, or it is not clear what purpose does the loop serve? I would advise that you post some more parts of the code. This snippet, as it stands is extremely unhelpful! – user21436 Mar 26 '12 at 11:36
I'd write something like $\operatorname{out} = \operatorname{in}^2$. – martini Mar 26 '12 at 11:40
Or $\forall i \in \{0, 1, \ldots, noutput\\_ items-1\}.\ out_i = in_i^2$. – dtldarek Mar 26 '12 at 11:49

## 2 Answers

If you really want to use a "formal" and "succint" mathematical expression, you can use the Hadamard Product, where the Hadamard product of two matrices is got by mutiplying the corresponding entries.

So if the input is treated as a vector: $\text{inp}$, then the output vector $\text{out}$ is given by

$$\text{out} = \text{inp} \circ \text{inp}$$

where $\circ$ is the Hadamard product.

Note that you can treat a vector as a $1\times n$ matrix.

Of course, this will probably force your readers to try and figure out what a Hadamard product is, and you might as well just write

$$\text{out}[i] = \text{inp}[i]^2 \quad \forall i \in \{0, 1, \dots , n-1\}$$

-

There are different ways. Here's a memory-unaware approach:

Let $n =$ noutput_items. Let $A$ be an $n \times n$ diagonal matrix, representing in[]. That is, the entry $A_{i,i} =$ in[i-1]. Then the matrix $B = AA$ is a diagonal matrix where out[i-1] $= B_{i,i}.$

-