MathGroup Archive 1995

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

Search the Archive

Re: RealDigits


Count Dracula writes:

> ... But even the naive function below is faster than DiagonalQ :
> 
> 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]
> 
> mat = DiagonalMatrix[Range[150]]
> 
> test := {Timing[diagonalQ[mat]], Timing[DiagonalQ[mat]]}
> 
> Test run on an IBM RS6000:
> 
> In[6]:= test
> 
> Out[6]= {{0.11 Second, True}, {3.57 Second, True}}
>

even faster and certainly simpler:

dQ[m_?MatrixQ] /; SameQ @@ Dimensions[m] :=
        m == DiagonalMatrix[Transpose[m, {1, 1}]]

(note: Transpose[m, {1, 1}] returns the list of diagonal elements.)


In[3]:= test[m_] := {Timing[diagonalQ[m]], Timing[dQ[m]]}

In[4]:= mat1 = DiagonalMatrix[Range[500]];

In[5]:= test[mat1]

Out[5]= {{0.9 Second, True}, {0.666667 Second, True}}


test with a non-diagonal matrix:

In[6]:= mat2 = ReplacePart[mat1, 55, {5, 3}];

In[7]:= test[mat2]

Out[7]= {{0.866667 Second, False}, {0.566667 Second, False}}


Roman Maeder



  • Prev by Date: Re: Functional programming puzzle
  • Next by Date: Re: Replacing terms and expanding one at a time
  • Previous by thread: Re: RealDigits
  • Next by thread: Re: RealDigits