the temperamental loop or something wrong with my expression
- To: mathgroup at smc.vnet.net
- Subject: [mg80534] the temperamental loop or something wrong with my expression
- From: Jack Yu <Jack.Yu at astro.cf.ac.uk>
- Date: Fri, 24 Aug 2007 02:06:10 -0400 (EDT)
Hi I have defined a function whose value I want to know for a range of two of its arguments. So to save time, I use a Do loop. However, after a few loops, it just doesn't produce any more result, even though at the top it still says: 'Running ...'. I know that when I do this by manually changing the arguments without using a Do loop, they all evaluate. So I thought it's to do with Do, and after reading somewhere in this forum that For and While are faster, I tried them as well, but they still become stuck after a few loops. The best I can do using a loop is to split the range of the variables into smaller ones; for some reason, this sometimes work, but sometimes it still doesn't work. The quickest I can do is actually just do them individually without using a loop. I find this a bit puzzling and would really appreciate if anyone can help me with this. The Notebook is here: *********************************************** In[1]:= delconj[eq_] := Apply[Plus,(Conjugate[ Simplify[ Table[Expand[eq][[i]],{i,1, Length[Expand[eq]]}]]]) /. {Conjugate[x_]\[Rule]x}]; In[2]:= conjSphericalHarmonicY[l_,m_,\[Theta]_,\[Phi]_]:= delconj[1+SphericalHarmonicY[l,m,\[Theta],\[Phi]]]-1; In[3]:= conjY[eq_]:= Conjugate[eq] /. Conjugate[x_]\[Rule]x; In[4]:= expandaa[eq_]:=(aaa=eq;bbb=CoefficientList[aaa,f]; Apply[Plus,Table[bbb[[i]]f^(i-1),{i,1,Length[bbb]}]]); In[5]:= \!\(\(approxF\[Alpha]\[Alpha][f_, \[Theta]_, \[Phi]_] := 1\/12\ \((Cos[\[Theta]]\^2\ \((3 + Cos[4\ \[Phi]])\) + 4\ \((1 + Cos[\[Theta]]\^4)\)\ Cos[\[Phi]]\^2\ Sin[\[Phi]]\^2)\)\ f\ \^2 + \(1\/147456\) \((\((\(-11278\) - 9307\ Cos[2\ \[Theta]] + 94\ Cos[4\ \[Theta]] + 11\ Cos[6\ \[Theta]] + 128\ \((\(-5\) + Cos[2\ \[Theta]])\)\ Cos[ 2\ \[Phi]]\ Sin[\[Theta]]\^4 - 64\ \((\(-37\) + 5\ Cos[2\ \[Theta]])\)\ Cos[ 4\ \[Phi]]\ Sin[\[Theta]]\^4 - 32\ Cos[6\ \[Phi]]\ Sin[\[Theta]]\^6)\)\ f\^4)\);\)\) In[7]:= \!\(\(approxF\[Alpha]\[Beta][f_, \[Theta]_, \[Phi]_] := 1\/384\ \((\(-35\) - 28\ Cos[2\ \[Theta]] - Cos[4\ \[Theta]] + 8\ Sin[\[Theta]]\^4\ \((Cos[ 4\ \[Phi]] + \@3\ Sin[ 4\ \[Phi]])\))\)\ f\^2 + \(-\(\(1\/6144\) \((\ \[ImaginaryI]\ \((16\ \@3\ Cos[ 5\ \[Phi]]\ Sin[\[Theta]]\^5 - \@3\ Cos[\[Phi]]\ \ \((42\ Sin[\[Theta]] + 27\ Sin[3\ \[Theta]] + Sin[5\ \[Theta]])\) + \((42\ Sin[\[Theta]] + 27\ Sin[3\ \[Theta]] + Sin[5\ \[Theta]])\)\ Sin[\[Phi]] + 32\ \((7 + Cos[2\ \[Theta]])\)\ Sin[\[Theta]]\^3\ Sin[ 3\ \[Phi]] + 16\ Sin[\[Theta]]\^5\ Sin[ 5\ \[Phi]])\)\ f\^3)\)\)\) + \(1\/147456\) \((\((5702 \ - 86\ Cos[4\ \[Theta]] - 7\ Cos[6\ \[Theta]] - 16\ Sin[\[Theta]]\^4\ \((\(-41\)\ Cos[2\ \[Phi]] + 95\ Cos[4\ \[Phi]] + 2\ Cos[ 6\ \[Phi]]\ Sin[\[Theta]]\^2 + \@3\ \((41\ Sin[ 2\ \[Phi]] + 95\ Sin[4\ \[Phi]])\))\) + Cos[2\ \[Theta]]\ \((4631 + 16\ Sin[\[Theta]]\^4\ \((\(-Cos[2\ \[Phi]]\) + 7\ Cos[ 4\ \[Phi]] + \@3\ \((Sin[2\ \[Phi]] + 7\ Sin[ 4\ \[Phi]])\))\))\))\)\ f\^4)\);\)\) In[8]:= a\[Alpha]\[Alpha][f_,l_,m_]:= Integrate[ Expand[Simplify[ Sin[\[Theta]]* conjSphericalHarmonicY[l, m,\[Theta],\[Phi]]approxF\[Alpha]\[Alpha][ f,\[Theta],\[Phi]]]],{\[Theta],0,Pi},{\[Phi],0,2Pi}]; In[10]:= a\[Alpha]\[Beta][f_,l_,m_]:= Integrate[ Expand[Simplify[ Sin[\[Theta]]* conjSphericalHarmonicY[l,m,\[Theta],\[Phi]]approxF\[Alpha]\[Beta][ f,\[Theta],\[Phi]]]],{\[Theta],0,Pi},{\[Phi],0,2Pi}]; In[12]:= \!\(\(a\[Beta]\[Beta][f_, l_, m_] := Exp[\(-I\)*m*\((\(2 Pi\)\/3)\)] a\[Alpha]\[Alpha][f, l, m];\)\) In[13]:= \!\(\(a\[Gamma]\[Gamma][f_, l_, m_] := Exp[\(-I\)*m*\((\(4 Pi\)\/3)\)] a\[Alpha]\[Alpha][f, l, m];\)\) In[15]:= \!\(\(a\[Beta]\[Gamma][f_, l_, m_] := Exp[\(-I\)*m*\((\(2 Pi\)\/3)\)] a\[Alpha]\[Beta][f, l, m];\)\) In[17]:= \!\(\(a\[Gamma]\[Alpha][f_, l_, m_] := Exp[\(-I\)*m*\((\(4 Pi\)\/3)\)] a\[Alpha]\[Beta][f, l, m];\)\) In[19]:= a\[Alpha]\[Gamma][f_,l_,m_]:= (-1)^m*conjY[a\[Alpha]\[Beta][f,l,m]]; In[21]:= \!\(\(a\[Beta]\[Alpha][f_, l_, m_] := \((\(-1\))\)^m* Exp[\(-I\)*m*\((\(2 Pi\)\/3)\)] conjY[a\[Alpha]\[Beta][f, l, m]];\)\) In[23]:= \!\(\(a\[Gamma]\[Beta][f_, l_, m_] := \((\(-1\))\)^m* Exp[\(-I\)*m*\((\(4 Pi\)\/3)\)] conjY[a\[Alpha]\[Beta][f, l, m]];\)\) In[24]:= \!\(\(aET[f_, l_, m_] := \ \(1\/\(3 \@ 2\)\) \((a\[Alpha]\[Alpha][f, l, m] - 2 a\[Beta]\[Beta][f, l, m] + a\[Gamma]\[Gamma][f, l, m] + a\[Alpha]\[Beta][f, l, m] - 2 a\[Beta]\[Alpha][f, l, m] + a\[Gamma]\[Beta][f, l, m] - 2 a\[Beta]\[Gamma][f, l, m] + \((a\[Alpha]\[Gamma][f, l, m] + a\[Gamma]\[Alpha][f, l, m])\))\);\)\) In[34]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,6,2]]]]] Out[34]= \!\({53.921514`8.183307071248283\ Second, \(\((3\ \[ImaginaryI] - \@3)\)\ \ f\^4\ \@\(\[Pi]\/910\)\)\/4752}\) Do[Print["aET(f,",l,m,")=", expandaa[FullSimplify[ComplexExpand[aET[f,l,m]]]]],{l,0,6},{m,0,l}] aET(f,\[InvisibleSpace]0\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]1\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]1\[InvisibleSpace]1\ \[InvisibleSpace]")="\[InvisibleSpace]\(1\/168\ \((\(-\[ImaginaryI]\) + \ \@3)\)\ f\^3\ \@\[Pi]\)\), SequenceForm[ "aET(f,", 1, 1, ")=", Times[ Rational[ 1, 168], Plus[ Complex[ 0, -1], Power[ 3, Rational[ 1, 2]]], Power[ f, 3], Power[ Pi, Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]2\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]2\[InvisibleSpace]1\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]2\[InvisibleSpace]2\ \[InvisibleSpace]")="\[InvisibleSpace]\(1\/864\ \((\(-3\)\ \[ImaginaryI] + \ \@3)\)\ f\^4\ \@\(\[Pi]\/5\)\)\), SequenceForm[ "aET(f,", 2, 2, ")=", Times[ Rational[ 1, 864], Plus[ Complex[ 0, -3], Power[ 3, Rational[ 1, 2]]], Power[ f, 4], Power[ Times[ Rational[ 1, 5], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]3\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]3\[InvisibleSpace]1\ \[InvisibleSpace]")="\[InvisibleSpace]\(1\/72\ \((\(-\[ImaginaryI]\) + \@3)\)\ \ f\^3\ \@\(\[Pi]\/14\)\)\), SequenceForm[ "aET(f,", 3, 1, ")=", Times[ Rational[ 1, 72], Plus[ Complex[ 0, -1], Power[ 3, Rational[ 1, 2]]], Power[ f, 3], Power[ Times[ Rational[ 1, 14], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]3\[InvisibleSpace]2\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]3\[InvisibleSpace]3\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]4\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]4\[InvisibleSpace]1\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]4\[InvisibleSpace]2\ \[InvisibleSpace]")="\[InvisibleSpace]\(\[ImaginaryI]\ \((\[ImaginaryI] + \ \@3)\)\ f\^4\ \@\(\[Pi]\/5\)\)\/3168\), SequenceForm[ "aET(f,", 4, 2, ")=", Times[ Complex[ 0, Rational[ 1, 3168]], Plus[ Complex[ 0, 1], Power[ 3, Rational[ 1, 2]]], Power[ f, 4], Power[ Times[ Rational[ 1, 5], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]4\[InvisibleSpace]3\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]4\[InvisibleSpace]4\ \[InvisibleSpace]")="\[InvisibleSpace]\(-\(\(17\ \[ImaginaryI]\ \((\(-\ \[ImaginaryI]\) + \@3)\)\ f\^4\ \@\(\[Pi]\/35\)\)\/3168\)\)\), SequenceForm[ "aET(f,", 4, 4, ")=", Times[ Complex[ 0, Rational[ -17, 3168]], Plus[ Complex[ 0, -1], Power[ 3, Rational[ 1, 2]]], Power[ f, 4], Power[ Times[ Rational[ 1, 35], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]5\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]5\[InvisibleSpace]1\ \[InvisibleSpace]")="\[InvisibleSpace]\(\((\(-\[ImaginaryI]\) + \@3)\)\ f\^3\ \ \@\(\[Pi]\/55\)\)\/1008\), SequenceForm[ "aET(f,", 5, 1, ")=", Times[ Rational[ 1, 1008], Plus[ Complex[ 0, -1], Power[ 3, Rational[ 1, 2]]], Power[ f, 3], Power[ Times[ Rational[ 1, 55], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]5\[InvisibleSpace]2\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]5\[InvisibleSpace]3\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]5\[InvisibleSpace]4\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]5\[InvisibleSpace]5\ \[InvisibleSpace]")="\[InvisibleSpace]\(1\/72\ \((3 + \[ImaginaryI]\ \@3)\)\ \ f\^3\ \@\(\[Pi]\/154\)\)\), SequenceForm[ "aET(f,", 5, 5, ")=", Times[ Rational[ 1, 72], Plus[ 3, Times[ Complex[ 0, 1], Power[ 3, Rational[ 1, 2]]]], Power[ f, 3], Power[ Times[ Rational[ 1, 154], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]6\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 Do[Print["aET(f,",l,m,")=", expandaa[FullSimplify[ComplexExpand[aET[f,l,m]]]]],{l,6,8},{m,0,l}] aET(f,\[InvisibleSpace]6\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]6\[InvisibleSpace]1\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]6\[InvisibleSpace]2\ \[InvisibleSpace]")="\[InvisibleSpace]\(\((3\ \[ImaginaryI] - \@3)\)\ f\^4\ \ \@\(\[Pi]\/910\)\)\/4752\), SequenceForm[ "aET(f,", 6, 2, ")=", Times[ Rational[ 1, 4752], Plus[ Complex[ 0, 3], Times[ -1, Power[ 3, Rational[ 1, 2]]]], Power[ f, 4], Power[ Times[ Rational[ 1, 910], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]6\[InvisibleSpace]3\[InvisibleSpace])=\[InvisibleSpace]\ 0 \!\(\* InterpretationBox[\("aET(f,"\[InvisibleSpace]6\[InvisibleSpace]4\ \[InvisibleSpace]")="\[InvisibleSpace]\(-\(\(\[ImaginaryI]\ \((\(-\ \[ImaginaryI]\) + \@3)\)\ f\^4\ \@\(\[Pi]\/91\)\)\/3168\)\)\), SequenceForm[ "aET(f,", 6, 4, ")=", Times[ Complex[ 0, Rational[ -1, 3168]], Plus[ Complex[ 0, -1], Power[ 3, Rational[ 1, 2]]], Power[ f, 4], Power[ Times[ Rational[ 1, 91], Pi], Rational[ 1, 2]]]], Editable->False]\) aET(f,\[InvisibleSpace]6\[InvisibleSpace]5\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]6\[InvisibleSpace]6\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]7\[InvisibleSpace]0\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]7\[InvisibleSpace]1\[InvisibleSpace])=\[InvisibleSpace]\ 0 aET(f,\[InvisibleSpace]7\[InvisibleSpace]2\[InvisibleSpace])=\[InvisibleSpace]\ 0 $Aborted In[38]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,7,3]]]]] Out[38]= {54.478757 Second,0} In[39]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,7,4]]]]] Out[39]= {76.970704 Second,0} In[40]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,7,5]]]]] Out[40]= {60.329395 Second,0} In[42]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,7,6]]]]] Out[42]= {32.853831 Second,0} In[43]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,7,7]]]]] Out[43]= {32.139781 Second,0} In[44]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,0]]]]] Out[44]= {69.877317 Second,0} In[45]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,1]]]]] Out[45]= {91.335676 Second,0} In[46]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,2]]]]] Out[46]= {88.233249 Second,0} In[47]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,3]]]]] Out[47]= {77.997297 Second,0} In[48]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,4]]]]] Out[48]= {79.343742 Second,0} In[49]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,5]]]]] Out[49]= {62.095294 Second,0} In[50]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,6]]]]] Out[50]= {49.984301 Second,0} In[51]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,7]]]]] Out[51]= {33.587836 Second,0} In[52]:= AbsoluteTiming[expandaa[FullSimplify[ComplexExpand[aET[f,8,8]]]]] Out[52]= {32.811034 Second,0} ******************************************** aET[f,l,m] is the function I want to evaluate for {l,0,8} and for each l, {m,-l,l}. As can be seen in the first Do loop, it stopped at aET[f,6,0]; I deleted 'Aborted'. And as can be seen in the second Do loop, it stopped at aET[f,7,2]. I finished the rest just by using the expression expandaa[FullSimplify[ComplexExpand[aET[f,l,m]]]] again and again. When I was doing this I noticed that the above expression sometimes gets stuck and I have to do it one step at a time. e.g aET[f,l,m], then ComplexExpand[aET[f,l,m]], then FullSimplify[ComplexExpand[aET[f,l,m]]] and so on, then the whole expression will work afterwards. This is also very strange.