MathGroup Archive 2011

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

Search the Archive

Re: Transformation Rules

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119724] Re: Transformation Rules
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Sat, 18 Jun 2011 19:57:31 -0400 (EDT)
  • References: <201106181021.GAA15377@smc.vnet.net>
  • Reply-to: drmajorbob at yahoo.com

data = RandomInteger[100, 100]

{18, 72, 61, 11, 100, 45, 41, 52, 12, 85, 29, 81, 55, 11, 68, 19, 41, \
5, 3, 65, 86, 14, 33, 69, 84, 7, 84, 47, 98, 93, 23, 29, 31, 29, 66, \
96, 8, 32, 22, 77, 5, 47, 31, 22, 31, 23, 18, 71, 48, 57, 44, 35, 40, \
68, 56, 65, 28, 41, 27, 89, 49, 100, 68, 3, 99, 100, 88, 14, 58, 100, \
53, 5, 92, 85, 15, 14, 98, 42, 50, 49, 80, 99, 84, 52, 49, 87, 43, \
89, 88, 28, 92, 75, 89, 9, 26, 86, 5, 31, 87, 94}

Rest@FoldList[Max, -Infinity, data]

{18, 72, 72, 72, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100}

data //. {a___, x_, y_, b___} /; x > y :> {a, x, x, b}

{18, 72, 72, 72, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, \
100, 100}

Of the two solutions, FoldList is MUCH, much faster:

data = RandomInteger[10^5, 10^3];
Timing[one = Rest@FoldList[Max, -Infinity, data];]

{0.000885, Null}

Timing[two = data //. {a___, x_, y_, b___} /; x > y :> {a, x, x, b};]

{4.18781, Null}

one == two

True

Bobby

On Sat, 18 Jun 2011 05:21:09 -0500, Stefan Salanski  
<wutchamacallit27 at gmail.com> wrote:

> Hey everyone, I found a sort of intro/tutorial notebook on my hard
> drive that I must have downloaded a while ago.
> "ProgrammingFundamentals.nb". I am not sure of the source, though the
> author appears to be a Mr. Richard Gaylord.
>
> "These notes form the basis of a series of lectures given by the
> author, in which the fundamental principles underlying Mathematica's
> programming language are discussed and illustrated with carefully
> chosen examples. This is not a transcription of those lectures, but
> the note set was used to create a set of transparencies which
> Professor Gaylord showed and spoke about during his lectures. These
> notes formed the basis for both a single 6 hour one-day lecture and a
> series of four 90 minute lectures, delivered to professionals and to
> students."
>
> I sent it to a friend and recommended that he look through and try out
> some of the exercises to become more familiar with Mathematica. It was
> written in a previous version of Mathematica (dunno which, just not
> 8), but still has a lot of great exercises to try out. (the only real
> difference being the new implementation of RandomInteger and
> RandomReal instead of Random[Integer] and Random[Real])
>
> Anyway, I have gotten stumped on one of the Transformation Rule
> exercises which I have restated below:
>
>    Use a transformation rule to take a list of elements and return a
> list of those elements that are greater than all of the preceding
> elements in the list.
>
> How can this be done with transformation rules?
>


-- 
DrMajorBob at yahoo.com


  • Prev by Date: Re: Listbox type progress display?
  • Next by Date: Re: SingularValueDecomposition
  • Previous by thread: Transformation Rules
  • Next by thread: Re: Transformation Rules