|
| 1 | +(* ::Package:: *) |
| 2 | + |
| 3 | +SimplifyFlag=True; |
| 4 | + |
| 5 | + |
| 6 | +$StepCounter = 0; |
| 7 | +$RuleList = {}; |
| 8 | +$RuleColor = Red; |
| 9 | +$ConditionColor = Blue; |
| 10 | + |
| 11 | + |
| 12 | +Int[u_,x_Symbol,flag_] := |
| 13 | + If[flag===Step, |
| 14 | + Block[{$ShowSteps=True}, Int[u,x]], |
| 15 | + If[flag===Steps, |
| 16 | + Block[{$ShowSteps=True}, |
| 17 | + FixedPoint[ |
| 18 | + Function[CellPrint[ExpressionCell[#,"Input"]]; |
| 19 | + ReplaceAll[#,{Defer[Int]->Int,Defer[Dist]->Dist,Defer[Subst]->Subst}]],Int[u,x]]], |
| 20 | + If[flag===Stats, |
| 21 | + Block[{$ShowSteps=False,$StepCounter=0,$RuleList={}}, |
| 22 | + With[{result=Int[u,x]}, |
| 23 | + Print[{$StepCounter,Length[$RuleList],LeafCount[u],LeafCount[result],N[Length[$RuleList]/LeafCount[u],4]}]; |
| 24 | + result]], |
| 25 | + Int[u,x]]]] |
| 26 | + |
| 27 | + |
| 28 | +(* If func is a function defined using properly defined transformation rules, |
| 29 | + StepFunction[func] modifies the rules to display steps when the control |
| 30 | + variable $ShowSteps is True. *) |
| 31 | +StepFunction[func_] := |
| 32 | + Module[{lst=DownValues[func]}, |
| 33 | + Block[{ShowStep,SimplifyFlag}, |
| 34 | + Monitor[Do[ |
| 35 | + lst[[i]]=ModifyRule[i,lst[[i]],SimplifyFlag], |
| 36 | + {i,1,Length[lst]}], |
| 37 | + ProgressIndicator[i,{1,Length[lst]+1},ImageSize->{500,20}]]]; |
| 38 | + ClearDownValues[func]; |
| 39 | + SetDownValues[func,lst]; |
| 40 | + Unprotect[func]] |
| 41 | + |
| 42 | +StepFunction[func1_,func2_] := |
| 43 | + Block[{lst,num=0,ShowStep,SimplifyFlag}, |
| 44 | + lst=Map[Function[ModifyRule[num++,#, SimplifyFlag]],DownValues[func1]]; |
| 45 | + ClearDownValues[func1]; |
| 46 | + SetDownValues[func2,ReplaceAll[lst,{func1->func2}]]] |
| 47 | + |
| 48 | + |
| 49 | +(* rule is an expression of the form RuleDelayed[lhs,rhs]. |
| 50 | + flag has the value SimplifyFlag. ModifyRule[rule,flag] |
| 51 | + formats the rule's left hand side as a string (lhsStrg) in InputForm, |
| 52 | + formats the rule's conditions as a string (condStrg) in StandardForm, |
| 53 | + formats the rule's let statements as a string (letStrg) in StandardForm, |
| 54 | + splices the conditions string and let strings together as a string (condStrg) in StandardForm, |
| 55 | + formats the rule's right hand side as a string (rhsStrg) in InputForm, |
| 56 | + then it returns rule with the body replaced with the expression |
| 57 | + ShowStep[num,condStrg,lhsStrg,rhsStrg,rhs] /; flag. *) |
| 58 | +ModifyRule[num_,rule_RuleDelayed, flag_] := |
| 59 | + Module[{lhsStrg,rhsStrg,condStrg,letStrg}, |
| 60 | + If[Not[FreeQ[Hold[rule],ShowStep]] || |
| 61 | + Not[FreeQ[Hold[rule],Identity]] || |
| 62 | + Not[FreeQ[Hold[rule],DeactivateTrig]] || |
| 63 | + Not[FreeQ[Hold[rule],Defer[Int]]] || |
| 64 | + Not[FreeQ[Hold[rule],Unintegrable]] || |
| 65 | + Not[FreeQ[Hold[rule],CannotIntegrate]] || |
| 66 | + Not[FreeQ[Hold[rule],Preprocess]], |
| 67 | + rule, |
| 68 | + lhsStrg=FormatLhs[rule]; |
| 69 | + If[rule[[2,0]]===Condition, |
| 70 | + condStrg=FormatConditions[Extract[rule,{2,2},Defer]]; |
| 71 | + If[rule[[2,1,0]]===With || rule[[2,1,0]]===Module || rule[[2,1,0]]===Block, |
| 72 | + letStrg=FormatLets[Extract[rule,{2,1,1},Defer]]; |
| 73 | + If[rule[[2,1,2,0]]===Condition, |
| 74 | + condStrg=SpliceConditionString[condStrg,letStrg,FormatConditions[Extract[rule,{2,1,2,2},Defer]]]; |
| 75 | + rhsStrg=FormatRhs[Extract[rule,{2,1,2,1},Defer]]; |
| 76 | + WrapCondition[ReplacePart[rule, ShowStep[num,condStrg,lhsStrg,rhsStrg,Extract[rule,{2,1,2,1},Hold]], {2,1,2,1}], flag], |
| 77 | + condStrg=SpliceConditionString[condStrg,letStrg,""]; |
| 78 | + rhsStrg=FormatRhs[Extract[rule,{2,1,2},Defer]]; |
| 79 | + WrapCondition[ReplacePart[rule, ShowStep[num,condStrg,lhsStrg,rhsStrg,Extract[rule,{2,1,2},Hold]], {2,1,2}], flag]], |
| 80 | + condStrg=SpliceConditionString[condStrg,"",""]; |
| 81 | + rhsStrg=FormatRhs[Extract[rule,{2,1},Defer]]; |
| 82 | + WrapCondition[ReplacePart[rule, ShowStep[num,condStrg,lhsStrg,rhsStrg,Extract[rule,{2,1},Hold]], {2,1}], flag]], |
| 83 | + If[rule[[2,0]]===With || rule[[2,0]]===Module || rule[[2,0]]===Block, |
| 84 | + letStrg=FormatLets[Extract[rule,{2,1},Defer]]; |
| 85 | + If[rule[[2,2,0]]===Condition, |
| 86 | + condStrg=FormatConditions[Extract[rule,{2,2,2},Defer]]; |
| 87 | + condStrg=SpliceConditionString["",letStrg,condStrg]; |
| 88 | + rhsStrg=FormatRhs[Extract[rule,{2,2,1},Defer]]; |
| 89 | + WrapCondition[ReplacePart[rule, ShowStep[num,condStrg,lhsStrg,rhsStrg,Extract[rule,{2,2,1},Hold]], {2,2,1}], flag], |
| 90 | + condStrg=SpliceConditionString["",letStrg,""]; |
| 91 | + rhsStrg=FormatRhs[Extract[rule,{2,2},Defer]]; |
| 92 | + WrapCondition[ReplacePart[rule, ShowStep[num,condStrg,lhsStrg,rhsStrg,Extract[rule,{2,2},Hold]], {2,2}], flag]], |
| 93 | + rhsStrg=FormatRhs[Extract[rule,2,Defer]]; |
| 94 | + WrapCondition[ReplacePart[rule, ShowStep[num,"",lhsStrg,rhsStrg,Extract[rule,2,Hold]], 2], flag]]]]] |
| 95 | + |
| 96 | + |
| 97 | +WrapCondition[rule_RuleDelayed,condition_] := |
| 98 | + ReplacePart[ReplacePart[rule,Append[Extract[rule,{2},Hold],condition],{2}],Condition,{2,0}] |
| 99 | + |
| 100 | + |
| 101 | +(* rule is a rule as an expression of the form RuleDelayed[lhs,rhs]. |
| 102 | + FormatLhs[rule] returns returns a string for the lhs of the rule in InputForm with the |
| 103 | + pattern tags "_." and "_" removed from the dummy variable names. *) |
| 104 | +FormatLhs[rule_] := |
| 105 | + Module[{lhs=Extract[rule,{1,1},Defer],conditions,func,var}, |
| 106 | + ( If[rule[[2,0]]===Condition, |
| 107 | + conditions=Extract[rule,{2,2},Defer]; |
| 108 | + If[conditions[[1,0]]===FunctionOfQ, |
| 109 | + func=conditions[[1,1]]; |
| 110 | + var=conditions[[1,2]]; |
| 111 | + lhs=ReplaceVariable[lhs,var,func], |
| 112 | + If[conditions[[1,0]]===And && MemberQ[conditions,FunctionOfQ,{3},Heads->True], |
| 113 | + func=conditions[[1,Position[conditions,FunctionOfQ,{3},1][[1,2]],1]]; |
| 114 | + var=conditions[[1,Position[conditions,FunctionOfQ,{3},1][[1,2]],2]]; |
| 115 | + lhs=ReplaceVariable[lhs,var,func]]]] ); |
| 116 | + DropDefer[StringReplace[ToString[lhs, InputForm],{"_Symbol"->"", "_."->"", "_"->""}]]] |
| 117 | + |
| 118 | +(* ReplaceVariable[lhs,var,func] returns lhs with the var replaced by F[func] *) |
| 119 | +ReplaceVariable[lhs_,var_,func_] := |
| 120 | + Block[{F}, |
| 121 | + If[PatternEqualQ[lhs[[1,1]],var], |
| 122 | + ReplacePart[lhs,F[func],{1,1}], |
| 123 | + If[PatternEqualQ[lhs[[1,1,1]],var], |
| 124 | + ReplacePart[lhs,F[func],{1,1,1}], |
| 125 | + If[PatternEqualQ[lhs[[1,1,2]],var], |
| 126 | + ReplacePart[lhs,F[func],{1,1,2}], |
| 127 | + If[PatternEqualQ[lhs[[1,1,3]],var], |
| 128 | + ReplacePart[lhs,F[func],{1,1,3}], |
| 129 | + Print["Function of expression variable not found: ",lhs," ",var," ",func]; |
| 130 | + Abort[]]]]]] |
| 131 | + |
| 132 | +PatternEqualQ[pattern_,var_] := |
| 133 | + Head[pattern]===Pattern && pattern[[1]]===var |
| 134 | + |
| 135 | + |
| 136 | +(* rhs is an expression of the form Defer[...] where ... is the right hand side of a rule. |
| 137 | + FormatRhs[rhs] returns a string for the rhs of the rule in InputForm. *) |
| 138 | +FormatRhs[rhs_] := |
| 139 | + Block[{SubstFor,Simp,Rt,F}, |
| 140 | + DropDefer[ToString[ |
| 141 | + ReplaceAll[ |
| 142 | + ReplaceAll[rhs,{ |
| 143 | + SubstFor[v_,u_,x_] -> F[x], |
| 144 | + Rt[u_,2] -> Sqrt[u], |
| 145 | + Rt[u_,n_] -> u^(1/n)}], |
| 146 | + Simp[u_,x_] -> u], |
| 147 | + InputForm]]] |
| 148 | + |
| 149 | + |
| 150 | +(* strg is a string of the form "Defer[...]". DropDefer[strg] returns the string "...", with |
| 151 | + all occurrences of the string "Int[" replaced with "Integrate" and "Dif" with "D" so they will |
| 152 | + display using integral and differential signs when the string is converted to StandardForm. *) |
| 153 | +DropDefer[strg_] := |
| 154 | + StringDrop[StringDrop[StringReplace[strg,{ |
| 155 | + "Int["->"Integrate[", |
| 156 | + "Dif["->"D[" |
| 157 | +(*, "sin["->"Sin[", "cos["->"Cos[", "tan["->"Tan[", "cot["->"Cot[", "sec["->"Sec[", "csc["->"csc[", |
| 158 | + "sinh["->"Sinh[", "cosh["->"Cosh[", "tanh["->"Tanh[", "coth["->"Coth[", "sech["->"Sech[", "csch["->"csch[" *) |
| 159 | + }],6],-1] |
| 160 | + |
| 161 | + |
| 162 | +(* conditions is an expression comprising the conditions on a rule. FormatConditions[conditions] |
| 163 | + replaces expressions of the form Not[FalseQ[u]] with u in conditions, |
| 164 | + deletes expressions of the form FreeQ[u,x], FunctionOfQ[u,v,x] and EasyDQ[u,x] in conditions, |
| 165 | + replaces expressions of the form Rt[u,2] with Sqrt[u] and Rt[u,n] with u^(1/n) in conditions, |
| 166 | + and then returns the conditions as a string formatted in Mathematica's StandardForm. *) |
| 167 | +FormatConditions[conditions_] := |
| 168 | + If[conditions[[1,0]]===Not && conditions[[1,1,0]]===FalseQ, |
| 169 | + FormatConditions[Extract[conditions,{1,1,1},Defer]], |
| 170 | + If[conditions[[1,0]]===FreeQ, |
| 171 | + "", |
| 172 | + If[conditions[[1,0]]===And && MemberQ[conditions,FreeQ,{3},Heads->True], |
| 173 | + FormatConditions[DeleteCondition[FreeQ,conditions]], |
| 174 | + If[conditions[[1,0]]===FunctionOfQ, |
| 175 | + "", |
| 176 | + If[conditions[[1,0]]===And && MemberQ[conditions,FunctionOfQ,{3},Heads->True], |
| 177 | + FormatConditions[DeleteCondition[FunctionOfQ,conditions]], |
| 178 | + If[conditions[[1,0]]===EasyDQ, |
| 179 | + "", |
| 180 | + If[conditions[[1,0]]===And && MemberQ[conditions,EasyDQ,{3},Heads->True], |
| 181 | + FormatConditions[DeleteCondition[EasyDQ,conditions]], |
| 182 | + ToConditionString[conditions] <> ","]]]]]]] |
| 183 | + |
| 184 | +DeleteCondition[func_,conditions_] := |
| 185 | + If[Quiet[Head[Extract[conditions,{1,3},Defer]]===Extract], |
| 186 | + If[Position[conditions,func,{3},1][[1,2]]==1, |
| 187 | + Extract[conditions,{1,2},Defer], |
| 188 | + Extract[conditions,{1,1},Defer]], |
| 189 | + Delete[conditions,{1,Position[conditions,func,{3},1][[1,2]]}]] |
| 190 | + |
| 191 | + |
| 192 | +(* let is a body of a With, Module or Block statement. FormatLets[let] returns the assignment as a |
| 193 | + string in the form "var=expression, then" formatted in Mathematica's StandardForm. *) |
| 194 | +FormatLets[let_] := |
| 195 | + If[MatchQ[let,Defer[{u_}]], |
| 196 | + If[let[[1,1,0]]===Set && |
| 197 | + let[[1,1,2,0]]===Block && |
| 198 | + Extract[let,{1,1,2,1},Defer]===Defer[{$ShowSteps=False}], |
| 199 | + If[let[[1,1,2,2,0]]===Simplify, |
| 200 | + ToConditionString[Extract[let,{1,1,1},Defer]] <> "=" <> |
| 201 | + ToConditionString[Extract[let,{1,1,2,2,1},Defer]] <> ", then", |
| 202 | + ToConditionString[Extract[let,{1,1,1},Defer]] <> "=" <> |
| 203 | + ToConditionString[Extract[let,{1,1,2,2},Defer]] <> ", then"], |
| 204 | + ToConditionString[Extract[let,{1,1},Defer]] <> ", then"], |
| 205 | + ToConditionString[let] <> ", then"] |
| 206 | + |
| 207 | + |
| 208 | +(* conditions is an expression comprising the conditions on a rule. ToConditionString[conditions] |
| 209 | + replace calls in the conditions on the function Rt[u,2] with Sqrt[u] and Rt[u,n] with u^(1/n), |
| 210 | + and then returns the conditions as a string formatted in Mathematica's StandardForm. *) |
| 211 | +ToConditionString[conditions_] := |
| 212 | + ToString[ReplaceAll[conditions,{ |
| 213 | + Rt[u_,2]->Sqrt[u], |
| 214 | + Rt[u_,n_]->u^(1/n) |
| 215 | + }],StandardForm] |
| 216 | + |
| 217 | + |
| 218 | +(* cond1, lets and cond2 are strings. SpliceConditionString[cond1,lets,cond2] concatenates and |
| 219 | + returns the condition and let strings along with the "if" and "let" to make a human readable |
| 220 | + condition string. *) |
| 221 | +SpliceConditionString[cond1_,lets_,cond2_] := |
| 222 | + If[cond2==="", |
| 223 | + If[lets==="", |
| 224 | + If[cond1==="", |
| 225 | + "", |
| 226 | + "If " <> cond1], |
| 227 | + If[cond1==="", |
| 228 | + "Let " <> lets, |
| 229 | + "If " <> cond1 <> " let " <> lets]], |
| 230 | + If[lets==="", |
| 231 | + If[cond1==="", |
| 232 | + "If " <> cond2, |
| 233 | + "If " <> cond1 <> " if " <> cond2], |
| 234 | + If[cond1==="", |
| 235 | + "Let " <> lets <> " if " <> cond2, |
| 236 | + "If " <> cond1 <> " let " <> lets <> " if " <> cond2]]] |
| 237 | + |
| 238 | + |
| 239 | +(* condStrg, lhsStrg and rhsStrg are the strings required to display the rule being applied. |
| 240 | + rhs is the expression on the right side of the rule (i.e. the consequent of the rule). |
| 241 | + If $ShowSteps is True, ShowStep[num,condStrg,lhsStrg,rhsStrg,rhs] displays the rule being applied, |
| 242 | + sets SimplifyFlag to False to turn off further simplification, and release the hold on the rhs |
| 243 | + of the rule. *) |
| 244 | +ShowStep[condStrg_,lhsStrg_,rhsStrg_,rhs_] := ( |
| 245 | + If[IntegerQ[$StepCounter], |
| 246 | + $StepCounter=$StepCounter+1]; |
| 247 | + If[$ShowSteps===True, |
| 248 | + Print["Rule: ",Style[condStrg,$ConditionColor]]; |
| 249 | + Print[" ",Style[ToExpression["Defer["<>lhsStrg<>"]"],$RuleColor],Style[" \[LongRightArrow] ",Bold],Style[ToExpression["Defer["<>rhsStrg<>"]"],$RuleColor]]; |
| 250 | + Block[{SimplifyFlag=False}, |
| 251 | + ReplaceAll[ReleaseHold[rhs],{Unintegrable->Defer[Int],CannotIntegrate->Defer[Int]}]], |
| 252 | + ReleaseHold[rhs]] ) |
| 253 | + |
| 254 | +ShowStep[num_,condStrg_,lhsStrg_,rhsStrg_,rhs_] := ( |
| 255 | + If[IntegerQ[$StepCounter], |
| 256 | + $StepCounter=$StepCounter+1]; |
| 257 | + If[Head[$RuleList]===List && Not[MemberQ[$RuleList,num]], |
| 258 | + $RuleList=Append[$RuleList,num]]; |
| 259 | + If[$ShowSteps===True, |
| 260 | + Print["Rule ",num+1,": ",Style[condStrg,$ConditionColor]]; |
| 261 | + Print[" ",Style[ToExpression["Defer["<>lhsStrg<>"]"],$RuleColor],Style[" \[LongRightArrow] ",Bold],Style[ToExpression["Defer["<>rhsStrg<>"]"],$RuleColor]]; |
| 262 | + Block[{SimplifyFlag=False}, |
| 263 | + ReplaceAll[ReleaseHold[rhs],{Unintegrable->Defer[Int],CannotIntegrate->Defer[Int]}]], |
| 264 | + ReleaseHold[rhs]] ) |
| 265 | + |
| 266 | + |
| 267 | +(* Note: Clear[func] also eliminates 2-D display of functions like Integrate. *) |
| 268 | +ClearDownValues[func_Symbol] := ( |
| 269 | + Unprotect[func]; |
| 270 | + DownValues[func]={}; |
| 271 | + Protect[func]) |
| 272 | + |
| 273 | + |
| 274 | +(* Note: A bug in earlier versions of Mathematica prevents setting more than 529 DownValues using a simple assignment! *) |
| 275 | +SetDownValues[func_Symbol,lst_List] := ( |
| 276 | + Unprotect[func]; |
| 277 | + ( If[$VersionNumber>=8, |
| 278 | + DownValues[func]=lst, |
| 279 | + DownValues[func]=Take[lst,Min[529,Length[lst]]]; |
| 280 | + Scan[Function[ReplacePart[ReplacePart[#,#[[1,1]],1],SetDelayed,0]],Drop[lst,Min[529,Length[lst]]]]] ); |
| 281 | + Protect[func]) |
0 commit comments