# Get rid of infinity in matrix elements (by separate definition of diagonal and off-diagonal elements)

I want to generate a matrix in which the elements have the below rule to be generated:

Table[KroneckerDelta[n, m] (m + n (1 - 1/(\[Pi] n^2))) +
(1 - KroneckerDelta[n, m]) (1/( n - m)),
{n, 1, 10}, {m, 1, 10}];


As we know the matrix has some diagonal elements which are determined by KroneckerDelta[n, m](m + n (1 - 1/(\[Pi] n^2))) and off-diagonal elements which are defined by (1 - KroneckerDelta[n, m]) (1/( n - m))

Problem

When m=n, (1/( n - m)) gets infinity for diagonal elements. But we know that the duty of (1 - KroneckerDelta[n, m]) is preventing generation diagonal elements with 1/(n-m)! how can I solve this problem?!

• SparseArray[{{n_, n_} :> n + n (1 - 1/(\[Pi] n^2)), {m_, n_} /; m != n :> 1/(n - m)}, {10, 10}] // Normal – march Jul 24 '16 at 18:16
• @march you always surprise me with your answers. Please let me understand step by step your solution – Unbelievable Jul 24 '16 at 18:18
• SparseArray is nice because it allows you to define the matrix elements using Patterns. So {n_, n_} :> n + n (1 - 1/(\[Pi] n^2)) means replace all entries indexed as (n,n) (i.e. diagonal entries) with n + n (1 - 1/(\[Pi] n^2)), and {m_, n_} /; m != n :> 1/(n - m)} uses Condition to replace entry (m,n) with 1/(n - m) only if m and n are unequal, and so it never evaluates 1/(n - m) with n==m. Otherwise, look up the documentation for SparseArray. – march Jul 24 '16 at 18:45
• Why insist on using KroneckerDelta[]? Either of Table[If[n == m, m + n (1 - 1/(π n^2)), 1/(n - m)], {n, 10}, {m, 10}] or ToeplitzMatrix[Prepend[1/Range[9], 0], Prepend[-1/Range[9], 0]] + DiagonalMatrix[Table[2 n - 1/(π n), {n, 10}]] works. – J. M. is away Jul 25 '16 at 2:18
• @ march, I have seen SparseArray but I did not know its duty! with your solution I have been familiar to that with its detail. It is so flexible. – Unbelievable Jul 25 '16 at 6:36

SparseArray is nice because it allows you to define the matrix elements using Patterns. So

{n_, n_} :> n + n (1 - 1/(π n^2))


means replace all entries indexed as $(n,n)$ (i.e. diagonal entries) with n + n (1 - 1/(π n^2)), and

{m_, n_} /; m != n :> 1/(n - m)}


uses Condition to replace entry $(m,n)$ with 1/(n - m) only if m and n are unequal, and so it never evaluates 1/(n - m) with n == m.

Therefore,

mat = SparseArray[{{n_, n_} :> n + n (1 - 1/(π n^2)), {m_, n_} /; m != n :> 1/(n - m)}, {10, 10}] // Normal;


Alternatively, use J. M.'s solution that he posted in the comments:

mat = Table[If[n == m, m + n (1 - 1/(π n^2)), 1/(n - m)], {n, 10}, {m, 10}];


or

mat = ToeplitzMatrix[Prepend[1/Range[9], 0], Prepend[-1/Range[9], 0]]
+ DiagonalMatrix[Table[2 n - 1/(π n), {n, 10}]];


Then:

mat // MatrixForm


• @J.M. I never assume! And as much as people find it annoying, I'm a fan of using "they" as a gender-less pronoun. – march Aug 4 '16 at 16:44
• I'm glad that you did not, but that is tempered by my bias against "singular they". But we can agree to disagree. :) – J. M. is away Aug 4 '16 at 16:47