MathGroup Archive 1995

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

Search the Archive

DiadonalQ

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1876] DiadonalQ
  • From: Allan Hayes <hay at haystack.demon.co.uk>
  • Date: Sat, 12 Aug 1995 22:49:14 -0400

Dave Wagner <wagner at bullwinkle.cs.Colorado.EDU>
in [mg1741]
Count Dracula <lk3a at kelvin.seas.virginia.edu>
in [mg1840]
Posted testsfor a matrix being diagonal.
The following, though not part of a wider testing style, is simpler  
and quicker for this purpose.

diagonalQ2[m_] := DiagonalMatrix[Transpose[m,{1,1}]] == m

It depends on Transpose[m,{1,1}] giving the diagonal of m.

Allan Hayes
De Montfort University Leicester
hay at haystack.demon.co.uk

*********

COMPARISONS: on a NeXT Color Turbo

(*Wagner*)
DiagonalQ[m_] :=
And @@ Flatten[ MapIndexed[ #1==0 || Equal @@ #2 &, m, {2} ]]

(*Dracula*)
diagonalQ[m_?MatrixQ] :=
  Block[{n, row},
    n = Length[m];
    row = Table[0, {n - 1}];
    And @@ Map[Equal[#1, row]&, MapThread[Drop, {m,  
Partition[Range[n], 1]}]]
       ] /; SameQ @@ Dimensions[m]

(1)
mat = DiagonalMatrix[Range[100]]

DiagonalQ[mat]//Timing
diagonalQ[mat]//Timing
diagonalQ2[mat]//Timing
	{3.56667 Second, True}
	{0.133333 Second, True}
	{0.0666667 Second, True}
	
(2)
nd = Table[Random[Integer],{100},{100}];

DiagonalQ[nd]//Timing
diagonalQ[nd]//Timing
diagonalQ2[nd]//Timing
	{5.51667 Second, False}
	{0.1 Second, False}
	{0.0666667 Second, False}
	
(3)
nz1 = Table[0,{100},{100}];
nz1[[100,99]] = 1;

DiagonalQ[nz1]//Timing
diagonalQ[nz1]//Timing
diagonalQ2[nz1]//Timing
	{3.63333 Second, False}
	{0.116667 Second, False}
	{0.0666667 Second, False}
(4)
nz2 = Table[0,{100},{100}];
nz2[[1,2]] = 1;

DiagonalQ[nz2]//Timing
diagonalQ[nz2]//Timing
diagonalQ2[nz2]//Timing
	{3.43333 Second, False}
	{0.116667 Second, False}
	{0.0666667 Second, False}


  • Prev by Date: pictures
  • Next by Date: MS-DOS Version
  • Previous by thread: pictures
  • Next by thread: MS-DOS Version