Mapping onto a held expression
- To: mathgroup at smc.vnet.net
- Subject: [mg6989] Mapping onto a held expression
- From: Tom Burton <tburton at cts.com>
- Date: Thu, 1 May 1997 14:48:36 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
One way to make my Mathematica functions easier to read by non-programmers
is to use Subscript notation. For example, the set of elastic moduli
{Subscript[e,11],Subscript[e,22],Subscript[e,12],..} is easier to read (in
the front end, not here!) than {e11,e22,e12,..}. So I've written a function
toSubcript[var_Symbol,root_] which converts from flat to subscripted
notation. This function examines the variable name to see if it begins with
root. If so, then it makes the rest of the name a subscript. If not, or if
the "variable" is not a symbol, then it returns var. For example,
toSubscript[e11,e] yields Subscript[e,11]
toSubscript[g12,e] yields g12
toSubscript[123,1] yields 123
Now I want to map this function onto all the leaves of an
expression--typically a module definition. For example,
Map[toSubscript[#,e]&,
myfunct[disp_List]/; Length[disp]==2 :=
Module[{e11=3,e22=4}, {e11,e22}.disp],
{-1}]
is supposed to yield
myfunct[disp_List]/; Length[disp]==2 :=
Module[{Subscript[e,11]=3,Subscript[e,22]=4},
{Subscript[e,11],Subscript[e,22]}.disp
]
My problem is that I have been unable to find a way to apply my function to
the leaves of the expression without letting Mathematica attempt to
evaluate the expression. If I wrap my function in Unevaluated[], then this
wrapper comes off part-way through the mapping process and I get a mess. If
I wrap my function in Hold[], then the toSubscript function is mapped, but
remains unevaluated:
Hold[myfunct[Pattern[
(toSubscript[#1, e] & )[
disp],
Blank[(toSubscript[#1, e] & )[
List]]]] /;
Length[(toSubscript[#1, e] & )[
disp]] ==
(toSubscript[#1, e] & )[2] :=
Module[{(toSubscript[#1, e] & )[
e11] =
(toSubscript[#1, e] & )[3],
(toSubscript[#1, e] & )[e22] =
(toSubscript[#1, e] & )[4]},
{(toSubscript[#1, e] & )[e11],
(toSubscript[#1, e] & )[e22]} .
(toSubscript[#1, e] & )[disp]]
This state of affairs leads me to two questions:
(1) Given an expression like the held one above, is there a way to let the
functions toSubscript evaluate within this expression without letting the
expression itself evaluate?
(2) Should I be doing this another way?
I think this concept is useful, perhaps to other people. Any suggestions
would be appreciated. Thanks.
Tom Burton