Constructing a large tridiagonal matrix with alternating signs

What is the best way to construct a large tridiagonal matrix, in the following form (notably with alternating signs)?

$\mathbf M = \begin{pmatrix} 0 & a & 0 & 0 & 0 & \cdots \\ a & 0 & -a & 0 & 0&\cdots \\ 0 & -a & 0 & a & 0 &\cdots \\ 0 & 0 & a & 0 & -a &\cdots \\ \vdots & \vdots & \vdots & \vdots & \vdots & \ddots \end{pmatrix}$

I was using code like that shown below to produce constant sign diagonals of $a$ above and below the main diagonal for an $n\times n$ matrix.

DiagonalMatrix[Array[a &, n - 1], -1]   DiagonalMatrix[Array[a &, n - 1], 1]  

Any help is appreciated.

Replay

Here is a way using Band:

SparseArray[{Band[{1, 2}, {4, 5}] -> {a, -a},
          Band[{2, 1}, {5, 4}] -> {a, -a}}, {5, 5}] // MatrixForm

$$\left( \begin{array}{ccccc} 0 & a & 0 & 0 & 0 \\ a & 0 & -a & 0 & 0 \\ 0 & -a & 0 & a & 0 \\ 0 & 0 & a & 0 & -a \\ 0 & 0 & 0 & -a & 0 \\ \end{array} \right)$$

To make it easier to modify the size and the pattern of elements on the off-diagonals, I'd suggest something like this (incorporating J.M.'s suggestion):

With[{n = 7, pattern = {a, -a}},
 SparseArray[{Band[{1, 2}, {-2, -1}] -> pattern,
   Band[{2, 1}, {-1, -2}] -> pattern}, {n, n}]]

The negative indices are counted from the end, so that we don't need to use the dimension n in the Band specification. The reason I specify the beginning and end of each band is that only in this case will a cyclic repetition of pattern occur, as desired. If the end specification is omitted, pattern will only be used to fill the first few entries of the Band.

Here are two alternatives to Jens's procedure.

With Band[]:

With[{n = 7, a = 1},
     SparseArray[{Band[{1, 2}] -> #, Band[{2, 1}] -> #}] & @
     PadRight[{}, n - 1, {a, -a}]]

Without Band[]:

With[{n = 7, a = 1},
     SparseArray[{{j_, k_} /; Abs[j - k] == 1 && EvenQ[Max[j, k]] -> a,
                  {j_, k_} /; Abs[j - k] == 1 && OddQ[Max[j, k]] -> -a}, {n, n}]]

Both should yield

$$\begin{pmatrix} 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 1 & 0 & -1 & 0 & 0 & 0 & 0 \\ 0 & -1 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & -1 & 0 & 0 \\ 0 & 0 & 0 & -1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & -1 \\ 0 & 0 & 0 & 0 & 0 & -1 & 0 \\ \end{pmatrix}$$

You can do it with DiagonalMatrix easily as well:

n = 7;
DiagonalMatrix[-a*(-1)^Range[n - 1], -1] + DiagonalMatrix[-a*(-1)^Range[n - 1], 1]

Constructing a large tridiagonal matrix with alternating signs

fun[n_, a_] := SparseArray[{{i_, j_} /; j == i + 1 :> a (-1)^(i - 1),
   {i_, j_} /; j == i - 1 :> a (-1)^(i)}, {n, n}]

e.g.

Grid[Partition[(fun[#, a] // MatrixForm) & /@ Range[2, 10], 3]]

Constructing a large tridiagonal matrix with alternating signs

Category: matrix Time: 2016-07-29 Views: 12

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.153 (s). 12 q(s)