# Mapping over two indices with a condition

How can I use Map over two indices with a condition?

I am trying to calculate second derivative of an eigenvalue, $\lambda_i(x)$, of $n \times n$ matrix $M(x)$ with this expression (other terms in the actual expression are not relevant to my calculation):

$\frac{\partial^2 \lambda_i (x)}{\partial x^2} = 2\sum \limits_{j \neq i}^n \frac{\langle u_j|A| v_j\rangle^2}{\lambda_i - \lambda_j}$

$u_j$ ($v_j$) is the left (right) eigenvector of ith eigenvalue, matrix $A$ is the second derivative of $M$ with respect to $x$.

For example, the relevant function to find derivative of first eigenvalue for a $6\times 6$ matrix is this:

deriv[r_] := 2*Total[(leftEv[r]][[1,All]].derMatrix.rightEv[r][[#, All]])^2/(eigenP[r]][[1]] - eigenP[r][[#]]) & /@ {2, 3, 4, 5, 6}]


leftEv and rightEv return left and right eigenvectors, eigenP returns eigenvalues, derMatrix is a constant matrix, just numbers.

I want to find derivative of other eigenvalues in one call of the function (because I will call this function over and over), so that that function should return an array of all second derivatives of eigenvalues. But the condition $i \neq j$ prevents me to use second map over indices. Is there an easier, compact way to do this?

To be more illustrative, you can consider this $6 \times 6$ toy matrix:

matrix[x_]: = {{1-x^2/2,2,3,-1,-2,-3},{0,2-x^2/2,4,6,8,1},{3,2,1-x^2/2,-3,-2,-1},{5,2,1,9-x^2/2,1,2},{7,1,4,7,8+x^2/2,1},{9,6,3,1,6,7-x^2/2}}

derMatrix = {{-1,0,0,0,0,0},{0,-1,0,0,0,0},{0,0,-1,0,0,0},{0,0,0,-1,0,0},{0,0,0,0,1,0},{0,0,0,0,0,-1}}

• Later on, I have solved as this: deriv[r_] := Table[2*Total[Boole[#!=j](leftEv[r]][[1,All]].derMatrix.rightEv[r][[#, All]])^2/(eigenP[r]][[1]] - Boole[#!=j]*eigenP[r][[#]]) & /@ {1,2, 3, 4, 5, 6}],{i,1,6,1}]. There should be an easier way. – gurluk Nov 13 '15 at 16:13