Re: stack over flow

*To*: mathgroup at smc.vnet.net*Subject*: [mg13631] Re: stack over flow*From*: tburton at brahea.com (Tom Burton)*Date*: Thu, 06 Aug 1998 11:27:15 GMT*Organization*: Brahea, Inc.*References*: <35c6749c.0@newsread.cioe.com>*Sender*: owner-wri-mathgroup at wolfram.com

Hello, CplexMatindMatval[DiagonalMatrix[Range[500]]] generated a stack overflow in the kernel on my machine (P166 Windows95), version 3.0.1.0x. I suggest that you report this as a possible bug to support at wolfram.com. Meanwhile, if I understand what your function does, here is a suggested replacement that doesn't generate the stack overflow. matNZ[(m_)?MatrixQ] := Module[{ nc = Last[Dimensions[m]], p = Position[Map[#1 != 0 & , m, {2}], True]}, {p /. {i_, j_} :> nc*(i - 1) + j, Extract[m, p]} ] Tom Burton On Mon, 3 Aug 1998 21:46:27 -0500, in comp.soft-sys.math.mathematica you wrote: >Hello: I've written a small function that causes a stack overflow on my >machine. I'm wondering if the same happens in other machines. I'm running >MA v3.0.1 under NT4 (sp3) on a PII400. > >I've re-written the function without the Do[ ] and it works fine, but I'd >like to >understand how a stack overflow occurs in this particular piece of code. >For dense matrices, the function works as expected; for sparse matrices >(eg. DiagonalMatrix[500]) the overflow occurs. > >Here's the code (Basically, the objective is to determine the non-zero >elements and to determine their corresponding row location): > >CplexMatindMatval[m_?MatrixQ]:= >Module[{indx, matindx, matList, rem}, > matList = Flatten[Transpose[m]]; > With[{numRows = Length[m]}, > indx = { }; > Do[ > rem = Mod[k,numRows]; > indx = {indx, If[rem == 0, numRows, rem]} > , > {k, Length[matList]} > ] > ]; > > (* get the position of the nonzero elements only *) > >h[{ > nzElemIndx = Position[(# != 0&)/@ matList,True]//Flatten > }, > (* subtract 1 to get zero-offset indices *) > matindx = Flatten[indx][[nzElemIndx]]-1; > {matindx, matList[[nzElemIndx]]} > ] >] > >Michael Ringor >Purdue University > > Tom Burton