Is this notation feature a kind of "variable shadowing"?

116 Views Asked by At

I'm a programmer, hence the term "shadowing" in the title.

In C++ programming, you can do the following:

void foo() {
    int i = 5;
    {
        int i = 6;
    }
}

The two is here are separate variables. The i in the inner scope is said to "shadow" the other i.

I'm reading this paper, equation 21:

enter image description here

enter image description here

Here's how I read the equation:

"for each i in [0, N], a triple-nested sum (depending on i) should equal 0"

The problem is that the "triple-nested sum" introduces another variable that is also called i. Is this notation equivalent to the shadowing in the C++ snippet above?

3

There are 3 best solutions below

0
On

That looks terrible and should never have happened. When you see the terms $\Phi_i$, $p_{i, j}^k$, $x_i$ and $\hat G_{i, j}^k$, there is no way of knowing which of those $i$'s is a summation index, and which of them correspond to the $\partial x_i$ from the left-hand side of the equality.

So the person who wrote it may have known very well how the expression worked, and think it obvious which $i$'s are summation variables and which aren't, or the ambiguity may not have crossed their mind at all. Either way, I would consider it illegible as it stands, but maybe I would consider it "barely legible" if I had actually read all of the paper and knew what the equation was about.

To answer your actual question, yes, there is definitely something very shadowing-like going on here. The big problem is that they then probably use $i$ to refer to both the local and the global variable at different points in the same expression (in the same "scope", to use a programming term), with no objective way of telling which is which.

0
On

I think I got it.

In $\partial x_i$, the $x_i$ refers to one of the arguments of f (in equation 7). Equation 7 "invisibly" uses $x_i$, as $G_{i, j}^k$ is defined in terms of $x_i$. And indeed the other usages of i are shadowing.

0
On

Yes, here we have some kind of variable shadowing. First of all note there is no ambiguity regarding the expression (21), but the readability might be affected by the multiple use of the variable $i$.

We have the following situation \begin{align*} \frac{\partial f}{\partial x_i}&=\sum_{k=1}^K\sum_{i=1}^N\sum_{j\in\Phi_i}2p_{i,j}^k\left(x_i^k-x_j^k-\hat{G}_{i,j}^k\right)\tag{1}\\ &=\sum_{k=1}^K\left(\sum_{i=1}^N\sum_{j\in\Phi_i}2p_{i,j}^k\left(x_i^k-x_j^k-\hat{G}_{i,j}^k\right)\right)\tag{2}\\ &=\sum_{k=1}^K\sum_{\color{blue}{l}=1}^N\sum_{j\in\Phi_\color{blue}{l}}2p_{\color{blue}{l},j}^k\left(x_\color{blue}{l}^k-x_j^k-\hat{G}_{\color{blue}{l},j}^k\right)\tag{3}\\ \end{align*}

  • The subscript variable $i$ at the left-hand side of (1) is a free variable.

  • The index variable $i$ at the right hand side is a bound variable according to the scope of the $\sum$-symbol. The scope of the bound variable is indicated by the open brackets in (2).

  • We can clarify the situation for instance by using a different bound variable $l$ instead of $i$ as shown in (3).

Note: The scope of the sigma operator $\sum$ is solely defined via arithmetic precedence rules. The scope is given by the expression that follows immediately the $\sum$ and is valid respecting the arithmetic precedence rules up to an operator with precedence level equal to '$+$' or up to the end if no such operator follows.