# How to create automatically some matrices for symbolic manipulation?

I want to create in an automatic way some matrices to do symbolic calculations with them. They look like these:

$$\left( \begin{array}{cccc} a & 0 & b & 0 \\ 0 & 1 & 0 & 0 \\ b & 0 & a & 0 \\ 0 & 0 & 0 & 1 \end{array} \right)$$

So they have entry $a$ in at position $(1,1)$ and $(3,3)$ and entry $b$ at position $(1,3)$ and $(3,1)$ and the rest of the entries are 1 on the diagonal and zero elsewhere. So in general i want a symmetric $n\times n$ matrix with $b$ at position $(i,j)$ and $(j,i)$ and $a$ at position $(i,i)$ and $(j,j)$ How can i create these kind of matrices automatically?

Probably it is possible to write some function T[n,i,j] that gives as value the corresponding matrix.

(I have no insight into possible implementations of the solution, so additional Tags are welcome!)

-

This makes a matrix of arbitrary size n :

f[n_, i_, j_] :=
ReplacePart[
Array[0 &, {n, n}],
{{i, i} -> a, {j, j} -> a, {i, j} -> b, {j, i} -> b, {k_, k_} -> 1 }]
f[4, 1, 3] //Grid


-
+1 for understanding what the OP wanted. However, I still recommend a SparseArray approach. See my updated answer for examples. – Mr.Wizard Jan 13 '14 at 15:34
I have replaced T[...] by f[...] because symbols that begin with a uppercase are normally Mathematica built-in symbols – andre Jan 13 '14 at 17:41

You should take a look at SparseArray and Band:

SparseArray[{
Band[{1, 1}, {4, 4}] -> {a, 1},
{1, 3} -> b, {3, 1} -> b
}] // MatrixForm


$\left( \begin{array}{cccc} a & 0 & b & 0 \\ 0 & 1 & 0 & 0 \\ b & 0 & a & 0 \\ 0 & 0 & 0 & 1 \end{array} \right)$

It seems I misunderstood the output you wanted. Here is a function that mimics andre's output, but uses the more efficient SparseArray format:

f1[n_, i_, j_] :=
SparseArray[
{{i, i} -> a, {j, j} -> a, {i, j} -> b, {j, i} -> b, {k_, k_} -> 1},
{n, n}
]


For greater efficiency we can build a sparse IdentityMatrix and make replacements as andre did:

f2[n_, i_, j_] :=
ReplacePart[
IdentityMatrix[n, SparseArray],
{{i, i} -> a, {j, j} -> a, {i, j} -> b, {j, i} -> b}
]


Syntax is the same:

f2[7, 5, 2] // MatrixForm


$\left( \begin{array}{ccccccc} 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & a & 0 & 0 & b & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & b & 0 & 0 & a & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{array} \right)$

-
To make this general for arbitrary size and position i tried T[n_, i_, j_] := SparseArray[{Band[{1, 1}, {n, n}] -> 1, {i, i} | {j, j} -> a, {i, j} | {j, i} -> b}]which does not seem to work. I think this is now a question on properly defining a function. – user11712 Jan 13 '14 at 14:18
@user11712 Okay, give me a minute to update my answer. – Mr.Wizard Jan 13 '14 at 14:19
@user11712 I see for your Acceptance of andre's answer that I apparently misunderstood what you wanted. Nevertheless I encourage you to use the SparseArray format instead for a matrix with a background value (default zero) as it is more efficient. I shall add a function to my answer that mimics andre's output. – Mr.Wizard Jan 13 '14 at 15:21