Let's say we have the sum:
$$\sum_{i: P_i < f(i)}x_i P_i$$ where $x = (x_1,.., x_k) \in N^k$ and $f$ is a function from $N$ to a finite set $P \subset N$ s.t $P_i \in P\; \forall i$ and also $|P|=k$.
Assume also $P_i<P_{i+1}$.
I want to compute the gradient with respect to $x$.
I was thinking that I could define a vector $P_v = (P_1, .., P_q,0..,0) \in P^q, \; q \leq k$. This is basically the vector that has $P_v(i)=P_i \iff P_i < f(i)$ and 0 everywhere else.
$$\sum_{i: P_i < f(i)}x_i P_i= x \cdot P_v$$
Now I think the gradient with respect to x is : $\nabla(x \cdot P_v)=P_v$