1
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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

enter image description here

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

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)$

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – user11712 Jan 13 '14 at 14:18
  • $\begingroup$ @user11712 Okay, give me a minute to update my answer. $\endgroup$ – Mr.Wizard Jan 13 '14 at 14:19
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Jan 13 '14 at 15:21

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.