Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Modify a list of pure functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70843] Re: Modify a list of pure functions
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Sat, 28 Oct 2006 23:39:07 -0400 (EDT)
  • References: <ehv8ts$g5i$1@smc.vnet.net>

. wrote:
> Hi all,
> 
> I have a problem where I need to build up a list of pure functions that
> have been modified from a base pure function.  I'm not having much luck
> in getting it to work correctly, and was wondering if anyone had some
> good ideas.  A toy version of the problem is:
> 
> f = Sin[#]&
> listF = Table[(i f[##])&, {i, 5}]
> 
> What I'd hope to get is listF containing:
> {Sin[##]&, (2 Sin[##])&, (3 Sin[##])&, (4 Sin[##])&, (5 Sin[##])&}
> 
> But instead, I get something where neither i nor f have been evaluated.
>  As I say, this is a toy example.  One thing that does partially work
> for the toy case, but does not seem to work for more complicated
> situations is to use ReplaceAll:
> 
> listF2 = Table[{j f[##])& /. j->i, {i,5}]
> 
> This does give:
> {1 f[##1] &, 2 f[##1] &, 3 f[##1]&, 4 f[##1] &, 5 f[##1] &}
> 
> However, all that has been done is the index has been inserted; no
> significant evaluation seems to have occurred.  So this method doesn't
> work in more complicated situations.  For example, suppose insted of
> using the index i directly, it actually referred to a list:
> 
> coeffs = Reverse[ Range[5] ];
> listF3 = Table[ (coeffs[[j]] f[##]) & /. j->i, {i, 5}]
> 
> This just gives:
> {coeffs[[1]]*f[##1] & , coeffs[[2]]*f[##1] & , coeffs[[3]]*f[##1] & ,
> coeffs[[4]]*f[##1] & , coeffs[[5]]*f[##1] & }
> 
> Which is no good if coeffs is a temporary variable.
> 
> Any suggestions would be most appreciated.  Thanks!
> 
Hello,

Your problem is caused by the fact that pure functions are represented 
(in FullForm) as objects with head Function, which has the attribute 
HoldAll. I am not quite sure if this solution is general enough for your 
real problem, but one approach is to inject things inside the function 
using With:

coeffs = Reverse[Range[5]];
listF3 = Table[With[{tmp = coeffs[[j]]}, (tmp f[##]) &], {j, 1, 5}]

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: RE: DisplayTogether & PlotLegend incompatibility
  • Next by Date: Re: DisplayTogether & PlotLegend incompatibility
  • Previous by thread: RE: Modify a list of pure functions
  • Next by thread: DisplayTogether & PlotLegend incompatibility