       Re: RealDigits

• To: mathgroup at christensen.cybernetics.net
• Subject: [mg1853] Re: [mg1741] RealDigits
• From: Roman Maeder <maeder at inf.ethz.ch>
• Date: Mon, 7 Aug 1995 20:27:27 -0400
• Organization: Computer Science, ETH Zurich

```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]
>
> test := {Timing[diagonalQ[mat]], Timing[DiagonalQ[mat]]}
>
> Test run on an IBM RS6000:
>
> In:= test
>
> Out= {{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:= test[m_] := {Timing[diagonalQ[m]], Timing[dQ[m]]}

In:= mat1 = DiagonalMatrix[Range];

In:= test[mat1]

Out= {{0.9 Second, True}, {0.666667 Second, True}}

test with a non-diagonal matrix:

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

In:= test[mat2]

Out= {{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