[Math] Convert for-loop into mathematical expression

notationsequences-and-series

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

Best Answer

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\} $$