Re: friendly challenge 3
- To: mathgroup at smc.vnet.net
- Subject: [mg35094] Re: friendly challenge 3
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Tue, 25 Jun 2002 03:39:31 -0400 (EDT)
- References: <af6hk6$lkc$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Andrzej, m = Table[Random[Integer, {1, 9}], {30}, {30}]; n = m + Transpose[m]; sign2[M_?MatrixQ] := Count[Eigenvalues[M], _?(# > 0 &)] - Count[Eigenvalues[M], _?(# < 0 &)] sign3[M_?MatrixQ] :=Plus@@Sign[Eigenvalues[M]] sign4[M_?MatrixQ] :=Plus@@Sign[Eigenvalues[N[M]]] Timings, sign2[n]//Timing {18.4 Second,2} sign3[n]//Timing {8.9 Second,2} sign4[n]//Timing {0.05 Second,2} -- Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Andrzej Kozlowski" <andrzej at platon.c.u-tokyo.ac.jp> wrote in message news:af6hk6$lkc$1 at smc.vnet.net... > 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 > >