mm-659 === Subject: Re: ExpToTrig with Real Numbers >A calculation gives me the following as a part of the output that I see >on the screen: (i = Imaginary) >-0.262484 i Exp[-0.951249 i t] + 0.262484 i Exp[0.951249 i t] >I want to apply ExpToTrig and , of course, end up with 0 for the Cosine >part and - .262484*2 Sin [0.951249] for the Sine part ... >instead I get (which I decided to look at output with InputForm) >(-0.2624844042359744*I)*Cos[0.9512492197250393*t] + >(0.26248440423597375*I)*Cos[0.9512492197250397*t] - > 0.2624844042359744*Sin[0.9512492197250393*t] - >0.26248440423597375*Sin[0.9512492197250397*t] >I can see from this that the coefficients and exponent numbers are very >close but not identical although , I think, in theory they should be. >I'm not worried about the 10th decimal place. How do i get the result >I want in a general way? Here is what I get on your expression after applying ExpToTrig and Chop I am not quite sure how you get the expression you posted In[27]:= $Version -0.262484* I* Exp[-0.951249 i* t]+0.262484 *I Exp[0.951249 i *t]//ExpToTrig// ComplexExpand//FullSimplify//Chop//InputForm Out[27]= 5.1 for Microsoft Windows (January 27, 2005) Out[28]//InputForm= (0.524968*I)*Sinh[0.951249*i*t] Hope this helps Pratik . -- Pratik Desai ...Moderation, as well as Regularity of Thinking, so much to be wished for in the Heads of those who imagine they come into the World only to watch and govern itâs Motion Gulliver's Travels by Jonathan Swift === Subject: Re: ExpToTrig with Real Numbers Clear[approx]; approx[expr_, n_:14]:= expr/.x_?NumericQ:>Round[10^n*x]/10.^n; expr=(-0.2624844042359744*I)* Cos[0.9512492197250393*t]+ (0.26248440423597375*I)* Cos[0.9512492197250397*t]- 0.2624844042359744* Sin[0.9512492197250393*t]- 0.26248440423597375* Sin[0.9512492197250397*t]; approx[expr]//Chop -0.52496880847194*Sin[0.95124921972504*t] Bob Hanlon === > Subject: ExpToTrig with Real Numbers > A calculation gives me the following as a part of the output that I see > on the screen: (i = Imaginary) > -0.262484 i Exp[-0.951249 i t] + 0.262484 i Exp[0.951249 i t] > I want to apply ExpToTrig and , of course, end up with 0 for the Cosine > part and - .262484*2 Sin [0.951249] for the Sine part ... > instead I get (which I decided to look at output with InputForm) > (-0.2624844042359744*I)*Cos[0.9512492197250393*t] + > (0.26248440423597375*I)*Cos[0.9512492197250397*t] - > 0.2624844042359744*Sin[0.9512492197250393*t] - > 0.26248440423597375*Sin[0.9512492197250397*t] > I can see from this that the coefficients and exponent numbers are very > close but not identical although , I think, in theory they should be. > I'm not worried about the 10th decimal place. How do i get the result > I want in a general way? === Subject: Re: Re: Replacement equivalence? >Hmm... cant get it to work as stated. Can you try the idea in this >benchmark test > ClearAll[f,a,b,c,i]; f[b_,c_]:=1/(c+b^2); > v=Table[{i,f[b,i]/.b^2->16},{i,-6,6}]; ListPlot[v,PlotJoined->True]; This seems to work Clear[b, x, c] f[b_,c_]:=1/(c+b^2); ListPlot[Table[{i, Replace[f[b, i], b -> (b = 4 .89ì b = -4 )]}, {i, -6, 6}], PlotJoined -> True] Hope this helps Pratik === Subject: Re: Extracting information from lists Tony, To extract the positions of the first element of runs of a given integer within a list, a very simple approach is to let a regex do all the dirty work: In[23]:= posFn[x:{__Integer},elem_Integer]:= First/@StringPosition[StringJoin[ToString/@x], RegularExpression[ToString[elem]<>+],Overlaps->False]; In[25]:= posFn[{11791681165111876111171},1] Out[25]= {1,5,8,12,18,23} In[26]:= posFn[{79168116511187611117},1] Out[26]= {3,6,10,16} In[27]:= posFn[{79168116511187611117},8] Out[27]= {5,13} An equivalent formulation using Mathematica string expressions would be: posFnAlt[x:{__Integer},elem_Integer]:= First/@StringPosition[StringJoin[ToString/@x],ToString[elem].., Overlaps->False]; dkr === Subject: Re: Extracting information from lists dojefh$fmb$1@smc.vnet.net... | 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 | Hi Tony, The following set of functions will do what you want: In[1]:= searchRuns[lst_List] := Module[{data = lst, flag = False}, (Flatten[#1, 2] & )[ Rest[Reap[For[cnt = 1, cnt < Length[data], cnt++, If[data[[cnt]] == data[[cnt + 1]], If[flag == False, Sow[{data[[cnt]], cnt}]; flag = True], flag = False]]]]]] In[2]:= searchNoRuns[lst_List] := Module[{data = lst, flag = False}, (Flatten[#1, 2] & )[Rest[Reap[For[cnt = 1, cnt < Length[data], cnt++, If[data[[cnt]] == data[[cnt + 1]], flag = True, If[flag == False, Sow[{data[[cnt]], cnt}], flag = False]]]]]]] In[3]:= findRuns[lst_List] := Module[{data = lst, ru, noru, st}, ru = searchRuns[data]; noru = searchNoRuns[data]; st = Union[First[Transpose[ru]]]; Union[ru, Cases[noru, {(x_)?(Intersection[{#1}, st] != {} & ), y_}]]] In[4]:= findRuns[{7, 9, 1, 6, 8, 1, 1, 6, 5, 1, 1, 1, 8, 7, 6, 1, 1, 1, 1, 7}][[All,2]] Out[4]= {3, 6, 10, 16} In[5]:= data = {7, 9, Pi, {E, I}, {E, I}, 1, 6, 6, 6, 8, Pi, a, a, 1, 1, 6, c, 5, word, 1, 1, 1, 8, word, word, 7, 6, 1, 1, 1, 1, 7, 2.7`20., 2.7, 2.7, 3, 3}; In[6]:= findRuns[data] Out[6]= {{1, 20}, {1, 28}, {2.7`20., 33}, {3, 36}, {6, 7}, {6, 27}, {word, 19}, {word, 24}, {a, 12}, {{E, I}, 4}} /J.M. === Subject: Re: Extracting information from lists Tony, The following extracts the positions of the first elements of runs of all integers, provided the integer has at least one run whose length exceeds one. Thus if In[1]:= egList={1,1,2,2,2,1,2,3,1,1,1,3,2,2}; the extracted positions will be {1,3,6,7,9,13}, corresponding to the beginning of the runs of 1 and 2. In[2]:= s1={First[#],Length[#]}&/@Split[egList] Out[2]= {{1,2},{2,3},{1,1},{2,1},{3,1},{1,3},{3,1},{2,2}} The first element of each sublist gives the integer involved in the run, and the second element gives the length of the run. In[3]:= s2=FoldList[#1+Last@#2&,1,Most@s1] Out[3]= {1,3,6,7,8,9,12,13} s2 gives the positions of the first elements of all runs, including those corresponding to integers (in this case the integer 3) which don't have any runs exceeding one in length. In[6]:= s3=Cases[s1,{a_,b_Integer}/;b>1:>a]//Union Out[6]= {1,2} s3 contains each integer which has a maximal run exceeding one in length. In[8]:= Pick[s2,s1,{a_Integer,_}/;MemberQ[s3,a]] Out[8]= {1,3,6,7,9,13} Finally we pick only those positions in s2 which correpond to integers having a maximal run exceeding one in length. Putting it all together: In[9]:= fn[x:{__Integer}]:=Module[{s1,s2,s3}, s1={First[#],Length[#]}&/@Split[x]; s2=FoldList[#1+Last@#2&,1,Most@s1]; s3=Cases[s1,{a_,b_Integer}/;b>1:>a]//Union; Pick[s2,s1,{a_Integer,_}/;MemberQ[s3,a]] ]; In[10]:= fn[egList] Out[10]= {1,3,6,7,9,13} In[11]:= fn[{7,9,1,6,8,1,1,6,5,1,1,1,8,7,6,1,1,1,1,7}] Out[11]= {3,6,10,16} In[12]:= fn[{1,2,3}] Out[12]= {} dkr === Subject: Re: Extracting information from lists Tony, Here is one method, but there may be shorter methods. (I changed the first 6 in your list to 1 to correspond with your result.) list = {7, 9, 1, 1, 8, 1, 1, 6, 5, 1, 1, 1, 8, 7, 6, 1, 1, 1, 1, 7}; dupstartpositions[list_?VectorQ] := Module[{lengths, startpositions}, lengths = Length /@ Split[list]; startpositions = Drop[FoldList[Plus, 1, lengths], -1]; Inner[If[#1 > 1, #2, Null] &, lengths, startpositions, List] /. Null -> Sequence[] ] dupstartpositions[list] {3, 6, 10, 16} David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ 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 === Subject: Re: Extracting information from lists li = {7, 9, 1, 6, 8, 1, 1, 6, 5, 1, 1, 1, 8, 7, 6, 1, 1, 1, 1, 7}; lolo = {0, 2, 1, 1, 0, 1, 1, 0, 0, 2, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0}; lulu = {a, b, d, d, d, d, c, f, g, t, d, d, e, r, d, d, d, e, r, d, d, e, f, r,f}; Here are three function with different degrees of generalisation: (in the 3 functions, the parameter n is the max length of the series of 1 considered) this one answer exactly to your question: fct1[li_, n_] := Module[{inter, res, pourN}, pourN[l_List, i_Integer] := Array[Position[Split[l], Table[1, {#}]] &, {i}] // Flatten; inter = pourN[li, n]; res = (Length[#] + 1 &) /@ ((Take[Split[li], # - 1] // Flatten) & /@ inter) ]; example: in > fct1[li, 5] out > {3, 6, 10, 16} but some problems arise when there are some series of '1' of same length in the list and when these series are in disorder (example in the list lolo): in > fct1[lolo, 5] out > {3, 6, 12, 19, 28, 15, 22} when this result is given, whe know the position of the series but we ignore the length of each one... this one answer your question in giving: {the number of 1 in the serie, the position of the serie} fct2[lis_, n_] := Module[{inter1, inter2, res, pourN}, pourN[l_List, i_Integer] := Array[({Position[Split[l], Table[ 1, {#}]], #} // Flatten) &, {i}]; inter1 = pourN[lis, n] // Select[#, Length[#] >= 2 &] &; inter2 = Table[{#[[i]], Last[#]}, {i, 1, Length[#] - 1}] & /@ inter1 // Flatten[#, 1] &; res = {#[[2]], Length[#[[1]]] + 1} & /@ ({Take[Split[ lis], #[[1]] - 1] // Flatten, #[[2]]} & /@ inter2) ]; for example: in > fct2[li, 5] out > {{1, 3}, {2, 6}, {3, 10}, {4, 16}} this one is the more general: it gives the positions of all the series of character 's' with max length 'n' in the list with their positions: fct3[lis_, s_, n_] := Module[{inter1, inter2, res, pourN}, pourN[l_List, i_Integer] := Array[({Position[Split[l], Table[ s, {#}]], #} // Flatten) &, {i}]; inter1 = pourN[lis, n] // Select[#, Length[#] >= 2 &] &; inter2 = Table[{#[[i]], Last[#]}, {i, 1, Length[#] - 1}] & /@ inter1 // Flatten[#, 1] &; res = {#[[2]], Length[#[[ 1]]] + 1} & /@ ({Take[Split[ lis], #[[1]] - 1] // Flatten, #[[2]]} & /@ inter2) ]; for example: in > fct3[lulu, d, 5] out > {{2, 11}, {2, 20}, {3, 15}, {4, 3}} in > fct3[lulu, d, 2] out > {{2, 11}, {2, 20}} Rudy === Subject: Re: Making a new definition of Equal work with lists as well doohbr$kua$1@smc.vnet.net... | | 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 | Hi Dan, A quick way is to use alternative patterns as in In[1]:= Unprotect[Equal]; ((a_CL) | {a_CL}) == ((b_CL) | {b_CL}) := a[[1]] - a[[2]] == b[[1]] - b[[2]] Protect[Equal]; In[4]:= CL[2, 3] == CL[4, 5] CL[2, 3] == CL[2, 4] Out[4]= True Out[5]= False In[6]:= {CL[2, 3]} == {CL[4, 5]} {CL[2, 3]} == {CL[2, 4]} Out[6]= True Out[7]= False In[8]:= CL[2, 3] == {CL[4, 5]} {CL[2, 3]} == CL[2, 4] Out[8]= True Out[9]= False /J.M. === Subject: Re: 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? There are two ways I can think of to accomplish this. First, you can use SetAttributes to give Equal the Listable attribute, i.e., SetAttributes[Equal, Listable] then {CL[2,3]} == {CL[4,5]} will evaluate to {True}. But, making Equal Listable may have some undesired side effects on other usages of Equal I haven't considered. Modifying commonly used built-in functions in this manner is risky. An alternative would be to add another definition for equal, i.e., Equal[{a_CL},{b_CL}]:= Equal[a,b] Doing things this way prevents side effects since this definition only operates on lists with one element with head CL -- To reply via email subtract one hundred and four === Subject: Lightweight Structs - Practical Example (Data Types in Mathematica) I've followed the various recent and older threads on implementing: - abstract data types (ADTs) - typedefs, structs, record-types (a la C language) - object-oriented programming (OOP) constructs, such as classes I find *multiple* instantiations of a particular named structure-type to be useful in calculations-oriented notebooks. For instance, in defining a structure called Sensor, containing various state/parameter fields, I can carry about the structure instances mine, yours, etc, without needing to explicitly marshall together all the instance-associated variables for each calculation (function invocation). Here's how I implement the above (after reviewing a number of posted suggestions): A named struct employs Mathematica's subscripted variables feature, composed as so: Sensor[ID][fieldname] where we use an arbitrary variable name (Sensor) for the structure (typedef) name, unique ID subscript (for each of multiple instantiations), and fieldname sub-subscripts. Sensor[mine][radius] = 6; Sensor[yours][radius] = 7; Then the named struct instance is passed around as Sensor[ID], whose Head is Sensor. p = Sensor[mine]; q = Sensor[yours]; The struct's member data (fields) can be accessed by name: In> p@radius Out> 6 In> q@radius Out> 7 Furthermore, since the Head of the struct instance is Sensor, separately defined Sensor member functions can take a struct-typed pattern argument: myFunction[s_Sensor] := Module[{}, Print[s@radius]] In> myFunction[p] Out> 6 Can define a constructor, which returns Sensor[id] handle: makeSensor[id_] := Module[{}, Sensor[id]@radius = 0; Sensor[id]] I've found the following pointers from others useful/interesting: http://library.wolfram.com/infocenter/Conferences/4680/ - Maeder's ADT material http://library.wolfram.co.jp/infocenter/Conferences/5773/ - OOP Package by Orestis Vantzos, which implements Classes. === Subject: Re: Questions regarding MatrixExp, and its usage > Now you should know that in general this is not going to hold > in all of complex plane (but will hold in most). >> which the equality holds in most of the complex plane. Clearly >> in every sense it holds just as often as it does not. Sorry about >> that; I replied too quickly. >> Andrzej Kozlowski > One more correction is needed: in a certain obvious sense the > equation does not hold more often than it holds since Exp is a > surjective mapping of the compelx plane to itself which covers it > infinitely many times (the fibre is Z - the integers). > Because of the holidays I am now constanlty in a rush and can't > find enough free time even to write a proper reply! > Andrzej Kozlowski Actually, even the above is not strictly correct: Exp is a surjective mapping form the complex plane to the complex plane minus the point 0. Now that I have a little bit of time I can try to analyse the entire problem more carefully. (In fact, I have not taught complex analysis for over 15 years and I have become a little bit rusty. So when I first saw this post I thought the problem lied in the branch most of the complex plane. Of course I was completely wrong in this respect). Let's again define the function f[x_, y_] := E^(x*y) - E^(y*Log[E^x]) We want to investigate where in the complex plane this is 0. This is by definition the same as f[x, y] E^(x*y) - (E^x)^y First, this is going to be zero for any real x and and an arbitrary y: ComplexExpand[f[x,y],{y}] 0 Secondly, suppose we have any pair of complex numbers a,b where f[a,b] ==0. That is: a /: f[a, b] = 0; Then we have ExpandAll[FullSimplify[ f[a + 2*Pi*I, b]]] E^(a*b + 2*I*b*Pi) - (E^a)^b This will be zero if an only if b is an integer: Simplify[%,Element[b,Integers]] 0 So for every pair (a,b) for which the identity holds and b is not an integer we can generate uncountably many pairs for which it does not hold by simply adding 2*Pi*I to a. For example: f[2,3/4] 0 FullSimplify[f[2 + 2*Pi*I, 3/4]] (-1 - I)*E^(3/2) On the other hand, we can get pairs of complex numbers for which the identity holds provided the imaginary part of the first complex number is not large: f[1,2+3I] 0 However, for complex numbers with large imaginary part: Simplify[f[1 + 12*I, 2 + 3*I]] (-E^(-34 + 27*I))* (-1 + E^(12*Pi)) it is easy in this way to give a complete description of the pairs (a,b) for which f is 0, but I will skip it and turn to matrices. In this case, while I am not 100% sure, I tend to believe the situation to be quite analogous. We are interested in the equation MatrixExp[B*p]==MatrixPower[MatrixExp[B],p] I believe this will hold for real matrices B and (probably) all complex p but will not hold in general. In fact I believe most what I and proofs would be more complicated. Let's just illustrate this in the case of a 2 by 2 random matrix. B=Array[Random[Integer,{1,6}]&,{2,2}] {{6,1},{5,1}} Let's take some complex p, e.g. 1+I In[65]:= N[MatrixExp[B*(1 + I)]]==N[MatrixPower[MatrixExp[B],1+I]] Out[65]= True To produce a case where the relationship does not hold just imitate the procedure for complex numbers given above. First we add to 2Pi * times the identity matrix to B: Z = B + 2 Pi*I IdentityMatrix[2]; For p take any non-integer number, real or complex: N[MatrixExp[Z*(1/2)]]==N[MatrixPower[MatrixExp[Z],1/2]] False Andrzej Kozlowski === Subject: Re: Questions regarding MatrixExp, and its usage >> Now you should know that in general this is not going to hold in >> all of complex plane (but will hold in most). > which the equality holds in most of the complex plane. Clearly in > every sense it holds just as often as it does not. Sorry about > that; I replied too quickly. > Andrzej Kozlowski One more correction is needed: in a certain obvious sense the equation does not hold more often than it holds since Exp is a surjective mapping of the compelx plane to itself which covers it infinitely many times (the fibre is Z - the integers). Because of the holidays I am now constanlty in a rush and can't find enough free time even to write a proper reply! Andrzej Kozlowski === Subject: Re: Questions regarding MatrixExp, and its usage > 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 ...? This can't possibly be true for arbitrary square complex matrices since it is not even true for matrices of dimension 1, that is complex numbers. In other words, it is not true that Exp[a p]== Exp[a]^p, were a and p are arbitrary complex numbers. In fact, Mathematica alone can find for you an example where this is not true. To see that let's define a function f of two variables: f[a_, b_] := Exp[a*b] - Exp[a]^b If the identity you held for all complex numbers f would have to be identically zero. However, we can get Mathematica to find an example when it is not: FindInstance[f[a, b] != 0, {a, b}] {{a -> -(47/10) + (181*I)/10, b -> 91/10 + (122*I)/5}} Since this seems a little hard to verify without Mathematica and since mathematica is sometimes wrong ;-) it may be more convincing to construct an example by hand. In fact it is pretty easy. All you need is the well known identity: Exp[I *A] = Cos[A]+ I *Sin[A] Put A = -I*Pi. We get Exp[-I*Pi] = -1 Put A = -Pi/2. We get Exp[-Pi/2*I]= -I But now note that: Exp[-Pi/2*I] == Exp[-Pi*I *(1/2)] so if the identity is true than -I == Exp[-Pi/2*I] == Exp[-Pi*I]^ (1/2) = (-1)^(1/2) == I. Thus we get a contradiction. Andrzej Kozlowski === Subject: Re: Questions regarding MatrixExp, and its usage > Now you should know that in general this is not going to hold in > all of complex plane (but will hold in most). which the equality holds in most of the complex plane. Clearly in every sense it holds just as often as it does not. Sorry about that; I replied too quickly. Andrzej Kozlowski === Subject: Re: Questions regarding MatrixExp, and its usage > 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 ...? >> >> This can't possibly be true for arbitrary square complex matrices >> since it is not even true for matrices of dimension 1, that is >> complex numbers. >> In other words, it is not true that Exp[a p]== Exp[a]^p, were a >> and p are arbitrary complex numbers. In fact, Mathematica alone >> can find for you an example where this is not true. To see that >> let's define a function f of two variables: >> f[a_, b_] := Exp[a*b] - Exp[a]^b >> If the identity you held for all complex numbers f would have to >> be identically zero. However, we can get Mathematica to find an >> example when it is not: >> FindInstance[f[a, b] != 0, {a, b}] >> {{a -> -(47/10) + (181*I)/10, >> b -> 91/10 + (122*I)/5}} > Why not try the opposite, find instance where the equation holds > In[278]:= > Clear[f,a,b] > f[a_,b_]:=Exp[a*b]-Exp[a]^b > sol1=First[FindInstance[f[a,b]==0,{a,b}]]; > {c,d}/.sol1; > (*Just on a hunch I tried to look at the phase of the complex > numbers a and b*) > Arg[c]//N > Arg[d]//N > Out[282]= > 2.92216 > Out[283]= > 2.97644 > Does this have any significance? ... > But why is this true (maybe I am missing something quite simple > here)? > In[126]:= > Out[126]= > True Well, ..., everybody knows (or should know) this holds for all reals: Simplify[Exp[a*b]-Exp[a]^b,Element[{a,b},Reals]] 0 So would expect it to hold in some part of the complex plane, wouldn't you? In fact, you should expect it to hold in most of the complex plane, but not all. So it is not at all surprising that you can find complex instances where the equation does hold and that it is a bit harder to find instances wehre it does not hold. In fact, one can analyse this more carefully just by looking at the documentation for Power in Mathematica and noting that for two complex numbers x, y, y^x is defined as the the principal value of Exp[y Log[b]] . So Exp[a]^b is the same as the principal value of Exp[b Log[Exp[a]]]. So this will fail to be equal to Exp[b a] precisely when Log[Exp[a]] is not equal to a. Now you should know that in general this is not going to hold in all of complex plane (but will hold in most). Take up any book on complex analysis and look for the definition of Log. In particular you will understand that: Log[Exp[I*Pi]] == Log[Exp[(-I)*Pi]] True or Log[Exp[6 Pi*I]] == Log[Exp[4 Pi*I]] True and so on. Andrzej Kozlowski === Subject: Re: Questions regarding MatrixExp, and its usage Hi Pratik, >mathgroup@smc.vnet.net === >Subject: Re: 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: Re: 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 === Subject: Re: preparing multiple choice questions I will read all the help about the functions you used. baris >-----Original Message----- >Cc: mathgroup@smc.vnet.net === >Subject: Re: preparing multiple choice questions > >> 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. > >> Is there a way of doing this in Mathematica? >Try the RandomPermutation function in the Combinatorica package: ><< DiscreteMath`Combinatorica` >ans = Table[Mod[i, 5], {i, 30}] /. {1 -> a, 2 -> b, 3 -> c, 4 -> d, 0 -> e} >randAns = RandomPermutation[ans] === Subject: Re: preparing multiple choice questions > 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. > Is there a way of doing this in Mathematica? Try the RandomPermutation function in the Combinatorica package: << DiscreteMath`Combinatorica` ans = Table[Mod[i, 5], {i, 30}] /. {1 -> a, 2 -> b, 3 -> c, 4 -> d, 0 -> e} randAns = RandomPermutation[ans] === Subject: Re: 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 Here is one way: You want 6 of each of the 5 possible correct aswers, so thye set of answers is unique. In[12]:= answers={a,a,a,a,a,a,b,b,b,b,b,b,c,c,c,c,c,c,d,d,d,d,d,d,e,e,e,e,e,e} Out[12]= {a,a,a,a,a,a,b,b,b,b,b,b,c,c,c,c,c,c,d,d,d,d,d,d,e,e,e,e,e,e} You can get a random permutation of these for a given test In[13]:= testanswers=RandomPermutation[answers] Out[13]= {b,c,d,e,c,d,c,b,b,d,e,c,a,a,e,a,b,a,e,d,c,b,a,a,d,c,b,e,d,e} Finally In[14]:= Table[{i,Part[testanswers,i]},{i,1,Length[testanswers]}] Out[14]= {{1,b},{2,c},{3,d},{4,e},{5,c},{6,d},{7,c},{8,b},{9,b},{10,d},{11,e},{12, c},{13,a},{14,a},{15,e},{16,a},{17,b},{18,a},{19,e},{20,d},{21,c},{22, b},{23,a},{24,a},{25,d},{26,c},{27,b},{28,e},{29,d},{30,e}} LP === Subject: Re: preparing multiple choice questions Combinatorica has functions that will do this. For example, In[9]:= << DiscreteMath`Combinatorica` In[10]:= Last[{Range[30], {}} //. {{A_, S_} :> ({Complement[A, #1], Append[S, #1]} & )[ RandomKSubset[A, 6]] /; Length[A] >= 6}] Out[10]= {{5, 8, 12, 17, 25, 27}, {15, 16, 22, 24, 28, 29}, {2, 4, 9, 13, 18, 20}, {1, 6, 7, 19, 23, 26}, {3, 10, 11, 14, 21, 30}} There is probably a more direct way to do this,. Ken Levasseur UMass Lowell > 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: Re: preparing multiple choice questions doof2r$kd9$1@smc.vnet.net... | | | 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 | | Hi Baris, The following function will do what you want: In[1]:= correctAnswers[]:= Module[{occurences={0,0,0,0,0},results},While[occurences!={6,6,6,6,6}, results=Table[{q,{a,b,c,d,e}[[Random[Integer,{1,5}]]]},{q,1,30}]; occurences=Length/@Split@Sort@results[[All,2]]; ]; results] In[2]:= correctAnswers[] Out[2]= {{1,e},{2,e},{3,b},{4,b},{5,e},{6,d},{7,c},{8,d},{9,d},{10,d},{11,b},{12, a},{13,a},{14,c},{15,c},{16,e},{17,a},{18,c},{19,a},{20,b},{21,e},{22, c},{23,b},{24,d},{25,d},{26,a},{27,e},{28,c},{29,a},{30,b}} /J.M. CreatedBy='Mathematica 5.2' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 2674, 78]*) (*NotebookOutlinePosition[ 3317, 100]*) (* CellTagsIndexPosition[ 3273, 96]*) (*WindowFrame->Normal*) Notebook[{ Cell[BoxData[ (correctAnswers[] := Module[{occurences = {0, 0, 0, 0, 0}, results}, While[occurences != {6, 6, 6, 6, 6}, [IndentingNewLine]results = Table[{q, {a, b, c, d, e}[([Random[Integer, {1, 5}]])]}, {q, 1, 30}]; occurences = Length /@ Split@(Sort@ results[([All, 2])]);[IndentingNewLine]]; [IndentingNewLine]results])], Input], Cell[CellGroupData[{ Cell[BoxData[ (correctAnswers[])], Input], Cell[BoxData[ ({{1, e}, {2, e}, {3, b}, {4, b}, {5, e}, {6, d}, {7, c}, {8, d}, {9, d}, {10, d}, {11, b}, {12, a}, {13, a}, {14, c}, {15, c}, {16, e}, {17, a}, {18, c}, {19, a}, {20, b}, {21, e}, {22, c}, {23, b}, {24, d}, {25, d}, {26, a}, {27, e}, {28, c}, {29, a}, {30, b}})], Output] }, Open ]] }, FrontEndVersion->5.2 for Microsoft Windows, ScreenRectangle->{{0, 1280}, {0, 717}}, WindowSize->{770, 559}, WindowMargins->{{0, Automatic}, {Automatic, 0}} ] (******************************************************************* End of Mathematica Notebook file. *******************************************************************) === Subject: Re: 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? The techniques you tried above will not work since the requirement the frequency of each choice be equal is incompatible with randomness. A uniform distribution over the integers 1 to 5 with enough samples will result in *apporoximately* equal numbers of each but not *exactly* equal numbers of each integer. If you require equal numbers of each sample, you can achieve some semblence of randomness by using a random permutation. This could be done as follows In[2]:= ans = {a, b, c, d, e}; << DiscreteMath` In[4]:= Flatten[ans[[#]]&/@ Partition[ RandomPermutation[Mod[Range[30], 5, 1]], 5]] Out[4]= {d, a, e, b, a, c, b, e, e, c, a, b, e, c, b, a, c, d, c, d, c, a, a, d, b, e, d, e, b, d} -- To reply via email subtract one hundred and four