MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: check if a square matrix is diagonal

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115351] Re: check if a square matrix is diagonal
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Sat, 8 Jan 2011 03:40:18 -0500 (EST)

Why not simply:

diagonalQ[m_] := m == DiagonalMatrix@Diagonal@m

?

Bobby

On Fri, 07 Jan 2011 03:12:40 -0600, Bill Rowe <readnews at sbcglobal.net>  
wrote:

> On 1/6/11 at 2:06 AM, xiaochu at gmail.com (benyaya) wrote:
>
>> What I try to do is extract the diagonal, subtract if from the
>> matrix, then compare the new matrix with 0. My code doesn't work out
>> though, can anyone help? thanks a lot.
>
>> checkIfDiagonalMatrix[m_] = Module[{d, mtemp},
>> d = Dimensions[m];
>> mtemp = DiagonalMatrix[Diagonal[m]] - m;
>> If[mtemp == Table[Table[0, {i, 1, d}], {i, 1, d}],
>> True,
>> False]
>> ]
>
> One immediate problem is Dimensions[m] returns a list.
> Consequently, d cannot be used as the end point for the Table
> iterator. The following will do what you want and has the same
> logic as you have used
>
> checkIfDiagonalMatrix[m_] := Module[{d, mtemp},
>    d = Dimensions[m];
>    mtemp = DiagonalMatrix[Diagonal[m]] - m;
>    If[mtemp == ConstantArray[0,d],
>     True,
>     False]
>    ]
>
> Note, I used SetDelayed (:=) not Set (=)
>
> But I think this logic is doing more work than necessary. I
> would accomplish this as:
>
> diagonalQ[
>    m_] := (ArrayRules[SparseArray@m] /.
>      HoldPattern[{a_, a_} -> _] :> Sequence[]) == {}
>
> If m is a diagonal matrix this should be pretty fast. However,
> it m is a dense array, this might be fairly slow.
>
> What I am doing is using ArrayRules to extract all of the
> non-zero elements of m. Those are returned as rules that look
> like {m,n}->number. I then apply a pattern matching rule that
> deletes all cases where m and n are equal and compare that to
> the empty list {}.
>
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Reduce in Mathematica 5 vs Mathematica 8 (2nd problem)
  • Next by Date: Re: check if a square matrix is diagonal
  • Previous by thread: Re: check if a square matrix is diagonal
  • Next by thread: Re: check if a square matrix is diagonal