RE: friendly challenge 3
- To: mathgroup at smc.vnet.net
- Subject: [mg35112] RE: [mg35076] friendly challenge 3
- From: "DrBob" <majort at cox-internet.com>
- Date: Tue, 25 Jun 2002 03:41:05 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
How's this? Needs["Statistics`DescriptiveStatistics`"] ClearAll[sign2, bt, tst] sign2[M_?MatrixQ] := Count[Eigenvalues[M], _?(# > 0 &)] - Count[Eigenvalues[M], _?(# < 0 &)] bt[M_?MatrixQ] := Plus @@ (Sign /@ Eigenvalues[M]) tst[k_Integer] := (m = Table[Random[Integer, {1, 9}], {k}, {k}]; n = m + Transpose[m]; First[Timing[#[n]]] & /@ {sign2, bt} ) tst[k_Integer, i_Integer] := Module[{t}, t = Mean /@ Transpose[tst[k] & /@ Range[i]]; {t, t[[1]]/t[[2]]} ] tst[30, 10] {{0.808 Second, 0.3344 Second}, 2.41627} tst[40, 20] {{2.5693 Second, 1.15175 Second}, 2.23078} tst[50, 10] {{7.1828 Second, 3.1859 Second}, 2.25456} Bobby Treat -----Original Message----- From: Andrzej Kozlowski [mailto:andrzej at platon.c.u-tokyo.ac.jp] To: mathgroup at smc.vnet.net Subject: [mg35112] [mg35076] friendly challenge 3 While the season for "friendly challenges" lasts, here is something that has just come up in my own work. Let's define the signature of a symmetric matrix as the number of positive eigenvalues - the number of negative ones. I need an efficient function to compute this. There is the obvious and rather pedestrian one: sign2[M_?MatrixQ] := Count[Eigenvalues[M], _?(# > 0 &)] - Count[Eigenvalues[M], _?(# < 0 &)] For example, let's construct a symmetric matrix of random integers (all matrices I am considering have integer entries): m = Table[Random[Integer, {1, 9}], {30}, {30}]; n = m + Transpose[m]; the above sign2 gives: In[4]:= sign2[n]//Timing Out[4]= {3.5 Second,0} My best function, sign1 gives (on 400 mghz PowerBOok G4) In[5]:= sign1[n]//Timing Out[5]= {1.44 Second,0} Nearly two and a half times as fast. Can anyone do better? Andrzej