0
$\begingroup$

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?!

$\endgroup$
  • 1
    $\begingroup$ SparseArray[{{n_, n_} :> n + n (1 - 1/(\[Pi] n^2)), {m_, n_} /; m != n :> 1/(n - m)}, {10, 10}] // Normal $\endgroup$ – march Jul 24 '16 at 18:16
  • $\begingroup$ @march you always surprise me with your answers. Please let me understand step by step your solution $\endgroup$ – Unbelievable Jul 24 '16 at 18:18
  • $\begingroup$ 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. $\endgroup$ – march Jul 24 '16 at 18:45
  • 1
    $\begingroup$ 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. $\endgroup$ – J. M. will be back soon Jul 25 '16 at 2:18
  • $\begingroup$ @ 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. $\endgroup$ – Unbelievable Jul 25 '16 at 6:36
1
$\begingroup$

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

enter image description here

$\endgroup$
  • $\begingroup$ @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. $\endgroup$ – march Aug 4 '16 at 16:44
  • $\begingroup$ I'm glad that you did not, but that is tempered by my bias against "singular they". But we can agree to disagree. :) $\endgroup$ – J. M. will be back soon Aug 4 '16 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.