Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Indexed Slot in a Rule - How to do it??

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100326] Re: Indexed Slot in a Rule - How to do it??
  • From: Simon <simonjtyler at gmail.com>
  • Date: Mon, 1 Jun 2009 07:07:17 -0400 (EDT)
  • References: <gvtme0$gct$1@smc.vnet.net>

Hi again,

you got me a bit curious about this...  so here's what I've got now:
First, a function to find all symbols of the form symb followed by
something.  This could be streamlined and generalised a bit...

In[1]:= findSymbols[symb_String,expr_]:=findSymbols
[{symb,CharacterRange["0","9"]},expr]
In[2]:= findSymbols[{symb_String,exts:{__String}},expr_]:=Module[{x},
  x=Union@Flatten@FixedPoint[Apply[List,#,\[Infinity]]&,expr];
  x=SymbolName[#]&/@Select[Flatten[x],Head[#]===Symbol&];
  Symbol/@Select[x,StringMatchQ[#,symb~~exts..]&]]

In[3]:= test={x1,x2,x2a+x33,E^x5,Sin[x1 x2+x3 x4]}

In[4]:= findSymbols["x",test]
findSymbols[{"x",{"2","a"}},test]
Out[4]= {x1,x2,x3,x33,x4,x5}
Out[5]= {x2,x2a}

Then a function that takes the expression, extracts the relevant
symbols and turns it into the type of function that you wanted

In[6]:= T[expr_]:=Apply[Function[Evaluate@findSymbols
["x",expr],Evaluate@expr],#]&

In[7]:= T[test]@{a,b,c,d,e,f}
Out[7]= {a,b,d+x2a,E^f,Sin[a b+c e]}

Simon


  • Prev by Date: Re: treatment on C[i]
  • Next by Date: Re: RandomReal gets stuck
  • Previous by thread: Re: Indexed Slot in a Rule - How to do it??
  • Next by thread: Re: Indexed Slot in a Rule - How to do it??