MathGroup Archive 1996

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

Search the Archive

Re: How can this MapAt application be done more efficiently

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4215] Re: How can this MapAt application be done more efficiently
  • From: rubin at msu.edu (Paul A. Rubin)
  • Date: Tue, 18 Jun 1996 03:25:48 -0400
  • Organization: Michigan State University
  • Sender: owner-wri-mathgroup at wolfram.com

In article <4pis80$iuo at dragonfly.wolfram.com>,
   Joel Cannon <cannon at alpha.centenary.edu> wrote:
->I have written a function to Drop any zero's from a table of numbers.
->Here is an example:
->
->	In[161]:= dropzero[ {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}} ]
->
->	Out[161]= {{5}, {4, 1}, {3, 2}, {3, 1, 1}}
->
->I would like to learn how to do it more efficiently. In what I am
->doing, the 0's will always occur as the last element of the inner
->lists (although it would be nice to have it work in general).
->
->Here is my function, with another function that it calls:
->
->
->     dropzero[a_] := MapAt[droplast,a, Partition[ 
Map[First,Position[a,0]],1] ]
->
->      droplast[a_] := Drop[a, -1]
->
->It feels to me like two things could be improved.
->
->1. I had to define the function droplast because I couldn't figure out
->how to use Drop[#,-1] with in the MapAt expression and get it to work.
->
->2. Of less importance, 
->the argument "Partition[Map[First,Position[a,0]],1]" seems a bit clumsy.

Substitution proves faster.  I pasted in your functions, and added a 
singleton zero to your example for test purposes.

In[]:=
  Timing[ dropzero[ {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}, {0}} ] ]
Out[]=
  {0.329 Second, {{5}, {4, 1}, {3, 2}, {3, 1, 1}, {}}}
In[]:= 
  dropzero2[ a_ ] := a /. {x___, 0} -> {x}  (* substitution *)
In[]:=
  Timing[ dropzero2[ {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}, {0}} ] ]
Out[]=
  {0.055 Second, {{5}, {4, 1}, {3, 2}, {3, 1, 1}, {}}}

BTW, that's three underscores ("_") after "x" in the definition of 
dropzero2.

Paul

**************************************************************************
* Paul A. Rubin                                  Phone: (517) 432-3509   *
* Department of Management                       Fax:   (517) 432-1111   *
* Eli Broad Graduate School of Management        Net:   RUBIN at MSU.EDU    *
* Michigan State University                                              *
* East Lansing, MI  48824-1122  (USA)                                    *
**************************************************************************
Mathematicians are like Frenchmen:  whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different.                                    J. W. v. GOETHE

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Polynomial problems.
  • Next by Date: Re: manipulating expressions
  • Previous by thread: Re: How can this MapAt application be done more efficiently
  • Next by thread: Re: How can this MapAt application be done more efficiently