6
$\begingroup$

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

That is, diagonal matrix with changing sign on the main diagonal.

$\endgroup$
2
  • $\begingroup$ Is "a" scalar or is it allowed to be another Matrix? $\endgroup$ Mar 28, 2023 at 17:10
  • 1
    $\begingroup$ @JosephDoggie scalar $\endgroup$
    – Anixx
    Mar 28, 2023 at 18:34

3 Answers 3

9
$\begingroup$

If u is the matrix:

DiagonalMatrixQ[u] && AllTrue[Ratios@Diagonal@u, # === -1 &]

would return True for compliant matrices.

$\endgroup$
4
  • $\begingroup$ Just pointing out the obvious for OP in case it proves a later pitfall - if OP were unstatedly treating a as strictly positive, this solution will yield True when the first element is positive (unlike your example) $\endgroup$
    – Anti Earth
    Mar 29, 2023 at 8:54
  • $\begingroup$ I checked with u and -u so both cases are covered. Only alternating signs are required. @AntiEarth $\endgroup$
    – Syed
    Mar 29, 2023 at 8:57
  • $\begingroup$ Yep, that's what I'm stating - OP may have had an unstated additional requirement that a is positive (why else start with -a instead of a in their example matrix?). In that scenario, your pattern matches their example matrices (first element negative) but also matches more (first element positive) $\endgroup$
    – Anti Earth
    Mar 29, 2023 at 8:58
  • $\begingroup$ I can work with what's presented and stated. But this is an acute observation. Thanks. $\endgroup$
    – Syed
    Mar 29, 2023 at 9:00
6
$\begingroup$

I'm assuming that the matrix can be of any size, and that $a$ can stand for anything:

check[u_] := DiagonalMatrixQ[u] &&
             Apply[SameQ, Diagonal[u]*(-1)^Range[Length[u]]]

check @ {{-a,  0,  0,  0,  0,  0},
         { 0,  a,  0,  0,  0,  0},
         { 0,  0, -a,  0,  0,  0},
         { 0,  0,  0,  a,  0,  0},
         { 0,  0,  0,  0, -a,  0},
         { 0,  0,  0,  0,  0,  a}}
(*    True    *)

check @ {{-a,  0,  0,  0,  0,  0},
         { 0,  a,  0,  0,  0,  0},
         { 0,  0, -a,  0,  0,  0},
         { 0,  0,  0,  a,  0,  0},
         { 0,  0,  0,  0, -a,  0},
         { 0,  0,  0,  0,  0,  b}}
(*    False    *)
$\endgroup$
2
$\begingroup$
mat = {{-a, 0, 0, 0, 0, 0}, {0, a, 0, 0, 0, 0}, {0, 0, -a, 0, 0, 
   0}, {0, 0, 0, a, 0, 0}, {0, 0, 0, 0, -a, 0}, {0, 0, 0, 0, 0, a}}

diag = Table[(-1)^n*a, {n, 1, Length@mat}]
(DiagonalMatrix[diag]) === mat

(* True *)
$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.