MathGroup Archive 2009

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

Search the Archive

Re: Re: Selecting left hand sides from assignments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97972] Re: [mg97803] Re: [mg97614] Selecting left hand sides from assignments
  • From: "E. Martin-Serrano" <eMartinSerrano at telefonica.net>
  • Date: Fri, 27 Mar 2009 05:32:29 -0500 (EST)

Hi again,

The following works as I wanted: it takes apart *lhs* and *rhs* of *Set*
expressions while keeping the relation between the *lhs* to the *rhs* for
further use. 

But, to get it to work one has to start working with a fresh kernel as to
ensure the *lhs*(s) have not been assigned any value before the sequence of
assignments *assign* is sent to the function *SplitSetSides*. However, since
I am to use it as a meta-programming tool (not to be include in end user
programs) this is not a big drawback, though a bit artificial. 

The first sublist in *Out[4]* contains the *lhs*(s) while the second
contains the *rhs*(s).  


In[1]:= Clear@frhs
frhs[rhs_][symbols_] := Module[{step},
  step = Fold[Flatten[#1, Infinity, #2] &, rhs, symbols];
  step = Union@Flatten@FixedPoint[Apply[List, #, Infinity] &, step];
  step = List @@ step;
  SymbolName[#] & /@ Select[Flatten[step], Head[#] === Symbol &]
  ]

In[2]:= Clear@SplitSetSides
SplitSetSides[holdassignments_] :=
 Quiet@Module[{symbols = Symbol[#] & /@ Names["System`*"]},
   {SymbolName[#] & /@ (List @@ 
       Map[First, Map[Unevaluated, holdassignments]]), 
    frhs[#][symbols] & /@ 
     Apply[List, Map[Hold, holdassignments, {1}]]}]

In[3]:= assign =
  Hold[a1 = Log[b1*c1^12], a2 = Sqrt[b2*c2], 
   a3 = Log[b1*c1^12]*Sqrt[a2*c2]^b3*c3 ];

In[4]:= SplitSetSides[assign]

Out[4]= {{"a1", "a2",
  "a3"}, {{"a1", "b1", "c1"}, {"a2", "b2", "c2"}, {"a3", "c3", "b1", 
   "c1", "a2", "c2", "b3"}}}

For now, this solution seems to be good enough for my needs. For instance,
it will help me in building the data dependency graph for a large notebook
meant to make dense computations involving many dynamic interrelated
objects. The simplification of the data dependency graph helps in improving
efficiency by eliminating superfluous or redundant computation paths which
would slow down significantly the performance. Also would help in pointing
out circularities. And, although it needs some additional testing it seems
to be working ok under my conditions. 

One thing which is still left (among many others) is to take out the names
in *symbols* which do not correspond to true symbols (operators) in the list
generated by the expression *symbols = Symbol[#] & /@ Names["System`*"]*} in
the module of the function *SplitSetSides*. 

Any feed back will be appreciated.  

I hope it will be of any use to someone else.  

Cheers and many thanks for your help.

E. Martin-Serrano

 
------------------------------------------------------------



>I need help on the following.



>From the list of assignments:

 

>assignmentslist = {LHS1 = RHS1, LHS2 = RHS2, LHS3 = RHS3, ..., LHSi = RHSi,
..., LHSn = RSHn}



>I need to extract all the left hand sides (symbols) within a list like:

 

>lhslist = {LHS, LHS2, LHS3, ..., LHSi, ..., LHSn}



>Where the left hand sides are all symbols and the right hand sides are any
expression.



>E. Martin-Serrano
-------------------------------------------





  • Prev by Date: Re: How do I hide all input in a Mathematica notebook?
  • Next by Date: Re: solving nonlinear simultaneous equations
  • Previous by thread: Re: Kernel is quitting on me
  • Next by thread: Re: Don't understand the response