mm-709 === Subject: Re: Extracting information from lists > Does anyone know of a function that will allow me to extract the positions > of the first elements of runs of similar elements within a list. For > example, suppose that > list={7,9,1,6,8,1,1,6,5,1,1,1,8,7,6,1,1,1,1,7} > I need a function that will return > {3,6,10,16} > corresponding to the runs {1},{1,1},{1,1,1},{1,1,1,1} within list > Tony For very long lists, the most efficient method is probably something like: 1. convert all integers other than 1 to 0. 2. look for a 0 followed by a 1 by using BitAnd and BitXor. The following function carries out this idea, except that I convert 1->0 and all other integers to 1: runpos[d_] := Module[{m = Sign[Abs[d - 1]]}, SparseArray[BitAnd[Prepend[Most[m], 1], BitXor[1, m]]]] /. SparseArray[_, _, _, a_] :> Flatten[a[[2, 2]]] On your test case we get the correct answer: In[12]:= runpos[list] Out[12]= {3,6,10,16} For a random list of 10^6 integers, we get: In[13]:= data=Table[Random[Integer,{1,10}],{10^6}]; In[14]:= runpos[data];//Timing Out[14]= {0.094 Second,Null} For comparison purposes, note that runpos is significantly faster than Split: In[15]:= Split[data];//Timing Out[15]= {0.562 Second,Null} Any method which first splits the data will be much slower than runpos. A different approach using Compile may be faster. Carl Woll Wolfram Research === Subject: Re: Questions regarding MatrixExp, and its usage >Hi Pratik, === >>Subject: Questions regarding MatrixExp, and its usage >> >Hi Pratik, >My only concern is about the usage of MatrixPower -- all of the >Mathematica online documentation and examples using this function seem to >indicate that it is only valid for an *integer* power p. >Since MatrixExp[aMatrix,p] exists (and is unique) for all square aMatrix >values and any *complex* value of p, I guess that I began wondering >under what conditions this might be equal to > MatrixPower[MatrixExp[aMatrix],p] >? Perhaps mathematically this only holds for *integer* values of p? I >don't know ... >Michael > >> >> === >>Subject: Questions regarding MatrixExp, and its usage >> >>For any arbitrary (possibly complex-valued) square matrix A, >Mathematica enables the computation of the matrix exponential of A via >>In[1]: A={{ some square matrix}}; >In[2]: expA=MatrixExp[A]; >>I was therefore wondering if >>MatrixExp[A p]==(MatrixExp[A]^p) >>where 'p' is an arbitrary complex number, and the '^' operator is my >attempt to denote the matrix power, and *not* an element-by-element >power for each individual matrix entry. Or does such an expression >only hold for real-valued square A matrices? Or am I completely lost >here ...? >>As usual, any and all help would be greatly appreciated! >Michael >> >How about MatrixPower >>matx[A_?MatrixQ, p_]=MatrixPower[MatrixExp[A], p] >>Hope this helps >>Pratik >> > >>You will never know unless you try :-) >>In[10]:= >>p=Random[]+Pi*I >>MatrixPower[MatrixExp[IdentityMatrix[3]],p]//Chop//InputForm >>Out[10]= >>0.982433[InvisibleSpace]+3.14159 [ImaginaryI] >>Out[11]//InputForm= >>{{-2.670947256395083, 0, 0}, {0, -2.670947256395083, 0}, {0, 0, >>-2.670947256395083}} >>In[33]:= >>MatrixExp[IdentityMatrix[3],p]//Chop//InputForm >>Out[33]//InputForm= >>{{-2.6709472563950825, 0, 0}, {0, -2.6709472563950825, 0}, {0, 0, >>-2.6709472563950825}} >>I think in my experience with mathematica if there are some limitation with >>a particular function, the documentation always seems to highlight it >>somewhere, and I did not see any explicit disclaimers regarding the >>limitation for MatrixPower only working with integers. To be perfectly >>honest, I don't know why In[33] works perhaps someone else on the forum can >>help >>Pratik >>Happy Holidays to you! >>PS: I hope you don't mind my posting your reply on the forum >>-- >>Pratik Desai >> >Here's an example that has me concerned: >In[1]: params={theta->Pi^Pi,p->Sqrt[2]}; >In[2]: aa=theta {{Cot[theta],Csc[theta]},{-Csc[theta],-Cot[theta]}}; >In[3]: test1=Simplify[MatrixExp[aa p]/.params]; >In[4]: test2=Simplify[MatrixPower[MatrixExp[aa],p]/.params]; >In[5]: N[test1-test2] >Out[5]: {{-0.230217 + 0. [ImaginaryI], -2.06142 + 0. [ImaginaryI]}, { > 2.06142[InvisibleSpace] + 0. [ImaginaryI], 1.12075[InvisibleSpace] + >0. [ImaginaryI]}} >So ... assuming that all intermediate calculations are done properly, and >that I haven't done anything 'improper', it appears that, in general: > MatrixExp[aMatrix p] != MatrixPower[MatrixExp[aMatrix],p] >for 'p' an arbitrary real number; it only seems to hold for p an integer ... >Does this seem reasonable? I'm somewhat mathematically 'challenged', >although perhaps this is 'intuitive' to others ... >Happy holidays, and joyeuses f.90tes! >Michael I think mathematically n in MatrixPower has to be an integer, refer to http://mathworld.wolfram.com/MatrixPower.html Hope this helps Pratik === Subject: Re: problems with memory usage everything with > Clear[p1]; gets larger and larger and, of course, the calculation > gets slower and slower. I don't understand exactly why > it happens since it reads one line per time and the > lines are very similar to each other. > Shouldn't it just use the same amount of memory in > every step? Your results are likely being saved in the history (In and Out). By default, your In and Out history will essentially contain a complete record of the session. If you always store important formulas and results explicitly for later use, then you may not need a very long history buffer. In that case, set $HistoryLength to something low, say 1 line: $HistoryLength=1; If you prefer, you can explicitly clear In and Out. Just use something like: Unprotect[In,Out]; Clear[In,Out]; Protect[In,Out]; === Subject: preparing multiple choice questions I have been trying to prepare a test of 30 question with multiple choices ranging from a-e. I want to automatically assign the question numbers and and the correct answers using mathematica. All correct answers should be in equal quantity, i.e. 6 a, 6 b, . 6 e. I tried to use Random with Table however it doesnt produce equal number of correct answers. I also tried to look into the Statistical packages. Is there a way of doing this in Mathematica? Could you please froward me to a link if you already know one? All the best Baris Erbas === Subject: Making a new definition of Equal work with lists as well I have defined Equal[a_CL,b_CL] := a[[1]]-a[[2]] == b[[1]]-b[[2]] and then CL[2,3] == CL[4,5] CL[2,3] == CL[2,4] evaluate to True and False respectively, as expected. However, {CL[2,3]} == {CL[4,5]} just stays in that form. What should I do to make list equality use my CL equality? -- Dan Bernstein