3
$\begingroup$

Is there a way to create a matrix $q$ of dimension $d$ with constraints on the indices given by:

$$d\longrightarrow dimension$$

$i,j $ are indices

$$q_{i,j}=\begin{cases} -b & j=i+d,\\ c & j=i+1,\\ l & j=d+i\pm1. \end{cases}$$

$b,c,l$ are constants.

$\endgroup$
5
  • $\begingroup$ Define the function q[i, j] and then matrix = Array[q, {d, d}] $\endgroup$ Commented Sep 7, 2021 at 21:53
  • $\begingroup$ @Michael E2 I have done like: Format[q[a_, b_]] := Subscript[q, Row@{a, b}]; d = 2; q[i_, i_ + d] := -b q[i_, i_ + 1] := c q[i_, i_ + d + 1] := l Array[q, {d, d}] Not working $\endgroup$ Commented Sep 7, 2021 at 22:01
  • $\begingroup$ is $d$ symbolic or an explicit integer? $\endgroup$ Commented Sep 7, 2021 at 22:46
  • $\begingroup$ It’s just an interger 1,2,3,4…. anything $\endgroup$ Commented Sep 7, 2021 at 22:50
  • $\begingroup$ If the matrix is $d \times d$, how can there be components with $j = i + d$ or $j = d + i + 1$? $\endgroup$ Commented Sep 8, 2021 at 12:53

2 Answers 2

4
$\begingroup$

This seems like a good job for Piecewise:

q[i_, j_] := Piecewise[{{-b, j == i + d}, {c, j == i + 1}, 
                        {l, j == d + i + 1}, {l, j == d + i - 1}}]

Now you can build the matrix:

d = 8; n = 15; (mat = Array[q, {n, n}]) // MatrixForm

which gives the same answer as cvgmt.

$\endgroup$
3
$\begingroup$

Maybe need to chack the definition of such matrix,it seems not right.

d = 8;
n = 15;
q=SparseArray[{{i_, j_} /; j == i + 1 -> 
   c, {i_, j_} /; j == i + d -> -b, {i_, j_} /; 
    j == d + i + 1 || j == d + i - 1 -> l}, {n, n}]
q//MatrixForm

Or

d = 8;
n = 15;
m = SparseArray[{Band[{1, 1+1}] -> c, Band[{1, 1 + d}] -> -b, 
   Band[{1, d + 1 + 1}] -> l, Band[{1, d + 1 - 1}] -> l}, {n, n}]
m // MatrixForm
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.