Skip to main content
added 108 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Here is a different attempt to work around the Listable attribute of Plus and Times.

doPE[c_, x_, grid_] := Internal`InheritedBlock[{Plus, Times}, ClearAttributes[{Plus, Times}, Listable];{Flat, Listable}]; Hold @@ { c /. x -> grid} //.   { L_List a___ * n__(n_?NumericQ | n_List) * L_List :> RuleCondition[na * # &Thread[n /@* L], L_List a___ + n__(n_?NumericQ | n_List) + L_List :> RuleCondition[na + # &Thread[n /@+ L],   a___ + f_*L_List + f_*M_List :> a + f Thread[L + M]  } } ] 

Test:

c = (Sinh[1/2 (g x^4 + x^2)] + p^3 x/(g - x^3)) g^2 x^2; c2 = -5832. - 432. x^2 - 1728. f x^3 - 12. (32. + 72. d1f + 144. f^2 - 27. p^4) x^4 - 24. f (32. + 72. d1f - 27. p^4) x^5; grid = {1., 2., 3., 4.}; doPE[c, x, grid] doPE[c2, x, grid] 
Hold[g^2 {1., 4., 9., 16.}   ((p^3 {1., 2., 3., 4.})/(g + {-1., -8., -27., -64.}) + Sinh[1/2 ({1., 4., 9., 16.} + g {1., 16., 81., 256.})])] Hold[f Hold[{-17286264., -138247560., -466569720., -11059212744.} +   (32. + 72. d1f + 144. f^2 - 27. p^4)f {-121728., -19213824., -97246656., -3072110592.} +  f (32. + 72. d1f - 27. p^4) {-24., -768., -5832., -24576.} +  (32. + 72. d1f + 144. f^2 - 27. p^4) {-626412., -7560192., -9720972., -127443072.}] 

Here is a different attempt to work around the Listable attribute of Plus and Times.

doPE[c_, x_, grid_] := Internal`InheritedBlock[{Plus, Times}, ClearAttributes[{Plus, Times}, Listable]; Hold @@ {c /. x -> grid} //. { L_List * n__?NumericQ :> RuleCondition[n * # & /@ L], L_List + n__?NumericQ :> RuleCondition[n + # & /@ L] } ] 

Test:

c = (Sinh[1/2 (g x^4 + x^2)] + p^3 x/(g - x^3)) g^2 x^2; c2 = -5832. - 432. x^2 - 1728. f x^3 - 12. (32. + 72. d1f + 144. f^2 - 27. p^4) x^4 - 24. f (32. + 72. d1f - 27. p^4) x^5; grid = {1., 2., 3., 4.}; doPE[c, x, grid] doPE[c2, x, grid] 
Hold[g^2 {1., 4., 9., 16.}   ((p^3 {1., 2., 3., 4.})/(g + {-1., -8., -27., -64.}) + Sinh[1/2 ({1., 4., 9., 16.} + g {1., 16., 81., 256.})])] Hold[f {-1728., -13824., -46656., -110592.} +   (32. + 72. d1f + 144. f^2 - 27. p^4) {-12., -192., -972., -3072.} +  f (32. + 72. d1f - 27. p^4) {-24., -768., -5832., -24576.} + {-6264., -7560., -9720., -12744.}] 

Here is a different attempt to work around the Listable attribute of Plus and Times.

doPE[c_, x_, grid_] := Internal`InheritedBlock[{Plus, Times}, ClearAttributes[{Plus, Times}, {Flat, Listable}]; Hold @@ { c /. x -> grid //.   { a___ * (n_?NumericQ | n_List) * L_List :> a * Thread[n * L], a___ + (n_?NumericQ | n_List) + L_List :> a + Thread[n + L],   a___ + f_*L_List + f_*M_List :> a + f Thread[L + M]  } } ] 

Test:

c = (Sinh[1/2 (g x^4 + x^2)] + p^3 x/(g - x^3)) g^2 x^2; c2 = -5832. - 432. x^2 - 1728. f x^3 - 12. (32. + 72. d1f + 144. f^2 - 27. p^4) x^4 - 24. f (32. + 72. d1f - 27. p^4) x^5; grid = {1., 2., 3., 4.}; doPE[c, x, grid] doPE[c2, x, grid] 
Hold[g^2 {1., 4., 9., 16.} ((p^3 {1., 2., 3., 4.})/(g + {-1., -8., -27., -64.}) + Sinh[1/2 ({1., 4., 9., 16.} + g {1., 16., 81., 256.})])] Hold[{-6264., -7560., -9720., -12744.} + f {-1728., -13824., -46656., -110592.} + f (32. + 72. d1f - 27. p^4) {-24., -768., -5832., -24576.} +  (32. + 72. d1f + 144. f^2 - 27. p^4) {-12., -192., -972., -3072.}] 
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Here is a different attempt to work around the Listable attribute of Plus and Times.

doPE[c_, x_, grid_] := Internal`InheritedBlock[{Plus, Times}, ClearAttributes[{Plus, Times}, Listable]; Hold @@ {c /. x -> grid} //. { L_List * n__?NumericQ :> RuleCondition[n * # & /@ L], L_List + n__?NumericQ :> RuleCondition[n + # & /@ L] } ] 

Test:

c = (Sinh[1/2 (g x^4 + x^2)] + p^3 x/(g - x^3)) g^2 x^2; c2 = -5832. - 432. x^2 - 1728. f x^3 - 12. (32. + 72. d1f + 144. f^2 - 27. p^4) x^4 - 24. f (32. + 72. d1f - 27. p^4) x^5; grid = {1., 2., 3., 4.}; doPE[c, x, grid] doPE[c2, x, grid] 
Hold[g^2 {1., 4., 9., 16.} ((p^3 {1., 2., 3., 4.})/(g + {-1., -8., -27., -64.}) + Sinh[1/2 ({1., 4., 9., 16.} + g {1., 16., 81., 256.})])] Hold[f {-1728., -13824., -46656., -110592.} + (32. + 72. d1f + 144. f^2 - 27. p^4) {-12., -192., -972., -3072.} + f (32. + 72. d1f - 27. p^4) {-24., -768., -5832., -24576.} + {-6264., -7560., -9720., -12744.}]