3
$\begingroup$

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

  • 0
    Or $\forall i \in \{0, 1, \ldots, noutput\\_ items-1\}.\ out_i = in_i^2$.2012-03-26

2 Answers 2

5

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

0

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