MathGroup Archive 2000

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

Search the Archive

Re: Summary: List element manipulation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25469] Re: Summary: List element manipulation
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 2 Oct 2000 22:26:50 -0400 (EDT)
  • References: <8r19qf$hvt@smc.vnet.net> <8r6oij$1c@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Martin,
Here are some timings and a new method that is faster on the tests below,
but
which is adversely affected when the structure of the data is more
complicated
than a list of numbers.

data1 = Range[1000000];

Previous methods

MapAt[# - 1 &, data1, -1]; // Timing // First

        7.91 Second

data1 /. {h___, t_} -> {h, t - 1}; // Timing // First

        8.73 Second

New method

oneOffLast[x_] :=
  Module[{y = x}, y[[-1]] = y[[-1]] - 1; y]

oneOffLast[data1]; // Timing // First

        0.44 Second

We could use an anonymous function:

Module[{y = #}, y[[-1]] = y[[-1]] - 1; y] &[data1] // Timing // First

        0.43 Second



Slightly more complicated data

data2 = Prepend[Range[1000000], {0}];

The previous  methods are slightly affected:

MapAt[# - 1 &, data2, -1]; // Timing // First

        4.01 Second

data2 /. {h___, t_} -> {h, t - 1}; // Timing // First

        6.26 Second

The new one is much slower on data2 than on data1(though still faster than
the previous methods.

oneOffLast[data2]; // Timing // First

        2.14 Second



Introducing a symbol has much the same adverse effect on the new method :

Remove[a]

data3 = Prepend[Range[1000000], a];

oneOffLast[data3]; // Timing // First

        2.69 Second

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Martin Rommel" <rommel at semitest.com> wrote in message
news:8r6oij$1c at smc.vnet.net...
> Thanks to the many helpful people that replied with their insight.
> The straightforward fix to my problematic
>
> > MapAt[Decrement, Range[116, 166, 8], -1]
> >
> >     Decrement::"rvalue": "164 is not a variable with a value, so its
value
> > cannot be changed."
>
> is using a function # - 1& instead of Decrement (which is not a
mathematical
> function but instead listed under programming assignments in the help
> browser)
>
> MapAt[# - 1&, Range[116, 166, 8], -1]
>
> This was suggested by the news group veterans David Park, Andrzej
Kozlowski,
> Hartmut Wolf and Allan Hayes.
>
> Matt Johnson suggested an alternative using a rule (it is always
interesting
> to see things done differently)
>
> Range[116, 166, 8] /. {h___, t_} -> {h, t - 1}
>
> Special thanks to Andrzej Kozlowski for additonal explainations but the
most
> in depth coverage came from my compatriot Hartmut Wolf.
>
> With hindsight I don't understand why I was so fixated on employing
> Decrement.
>
> Martin
>
>




  • Prev by Date: Hold, HoldForm, ReleaseHold when Plotting multiple functions
  • Next by Date: Re: Bug: Mathematica multipies statements together
  • Previous by thread: Summary: List element manipulation
  • Next by thread: Re: Summary: List element manipulation