6
$\begingroup$

I want to construct a Toeplitz matrix in Mathematica. Usually it is done in Mathematica using the following command:

ToeplitzMatrix[{a,b,...},{a,y,...}]

I want to have the first and the second list like {a,b,...} and {a,y,...} above to each contain N elements for a given number N (say 5 or 6; I want to keep the option of changing it). I want a=0. From b onwards I want to have (-1)^n nx (starting with n=1) and from y onwards -(-1)^n nx alternatively.

So at the end, it should look like \begin{pmatrix} 0\quad x\quad -2x\quad\dots\\ -x\quad 0\quad x\quad \dots\\ 2x\quad -x\quad 0\quad \dots\\ \dots \end{pmatrix}

I am quite new to all the powers of Mathematica. So some help will be greatly appreciated.

$\endgroup$
5
$\begingroup$
tf[nn_] := With[{n = Range[0, nn]}, ToeplitzMatrix[(-1)^n n x, -(-1)^n n x]]

For the question in the comment:

tf2[nn_] := 
 With[{n = Range[0, nn]}, 
  Module[{expr = (-1)^n Cot@(n x)}, expr[[1]] = 0; ToeplitzMatrix[expr, -expr]]]

tf3[nn_] := 
 With[{n = Range@nn}, 
  With[{expr = Join[{0}, (-1)^n Cot@(n x)]}, ToeplitzMatrix[expr, -expr]]]
$\endgroup$
  • $\begingroup$ Sorry, but it says: $\endgroup$ – Student Jan 28 '16 at 14:39
  • $\begingroup$ "Objects of unequal length in {1,-1,1,-1,1}\ {0,1,2,3,4}\ {[Pi]/12,\ [Pi]/6,[Pi]/4,[Pi]/3,(5\[Pi])/12,[Pi]/2,(7\[Pi])/12,(2\[Pi])/3,\ (3\[Pi])/4,(5\[Pi])/6,(11\[Pi])/12,[Pi],(13\[Pi])/12,(7\[Pi])/6,\ (5\[Pi])/4,(4\[Pi])/3,(17\[Pi])/12,(3\[Pi])/2,(19\[Pi])/12,(5\[\ Pi])/3,(7\[Pi])/4,(11\[Pi])/6,(23\[Pi])/12,2\ [Pi]} cannot be \ combined" $\endgroup$ – Student Jan 28 '16 at 14:39
  • $\begingroup$ Also, here the first element 0 is obtained just by putting 0 for n in (-1)^n nx. But what if I have 0 for the diagonals and say (-1)^n cot(nx), so that I really need to put a second rule for the diagonals? $\endgroup$ – Student Jan 28 '16 at 14:41
  • 1
    $\begingroup$ @Student You forgot to Clear[x] $\endgroup$ – xzczd Jan 28 '16 at 14:41
  • $\begingroup$ Sorry, it is working for what you said. But for the other one I mentioned, I can't do e.g. tf[nn_] := With[{n = Range[0, nn]}, ToeplitzMatrix[{0,(-1)^n Cot[n x]}, {0,-(-1)^n Cot[n x]}]]. $\endgroup$ – Student Jan 28 '16 at 14: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.