MathGroup Archive 1992

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

Search the Archive

"-Indexed" for MapAt?

  • To: MathGroup at yoda.physics.unc.edu
  • Subject: "-Indexed" for MapAt?
  • From: Robby Villegas <Villegas at knox.bitnet>
  • Date: Wednesday, Nov. 6, 1991

     In version 2, MapIndexed is a useful generalization of Map, but there
appears to be a gap in Mathematica:  where's the generalization of MapAt,
say "MapAtIndexed"?  It would be useful to be able target an _unrestricted_
set of parts of an expression with a function taking (part, partspec),
instead of just the special subsets comprising the _levels_ of an expression.
     I have devised a function which I think works correctly, but I'm not
satisfied with it for reasons of efficiency.  This function takes a list of
part-specifications and uses MapIndexed with a level-specification and an
appropriate setting of the Heads option to make sure that all the listed
parts will be encompassed by the MapIndexed operation.  The trouble, of
course, is that there is a great deal of wasted operating as a lot of
elements covered by the level-specification but not desired as targets,
must be tested and then left untouched.  Please suggest a more efficient
method of implementing a part-selective rather than level-selective
MapIndexed.  Here is the one I currently have:

        MapAtIndexed[f_, expr_, partspecs_List] :=
          Module[{levels = Map[Length, partspecs], minlevel, maxlevel,
                  fselect},
            minlevel = Min[levels]; maxlevel = Max[levels];
            headsQ = MemberQ[Flatten[partspecs], 0];
            fselect[part_, coords_] := If[MemberQ[partspecs, coords],
              f[part, coords], (* else skip: *) part];
            MapIndexed[fselect, expr, {minlevel, maxlevel}, Heads->headsQ]
          ]


Thank you,

                                        Robby Villegas
                                        Knox College
                                        E-mail:  Villegas at Knox.Bitnet





  • Prev by Date: Re: Extracting indices of variable
  • Previous by thread: Re: Extracting indices of variable