RE: Re: Fast (compiled) routine for element testing and replacement in large matrices?

• To: mathgroup at smc.vnet.net
• Subject: [mg24027] RE: [mg23978] Re: Fast (compiled) routine for element testing and replacement in large matrices?
• From: "David Park" <djmp at earthlink.net>
• Date: Tue, 20 Jun 2000 03:07:40 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```>
> Hi Gareth ---
>
> This way should be much faster:
>
> d2wreal[w_, l_, u_] := Map[If[(# > l) && (# <= u), 1, 0] &, w, {2}]
>
> With Mathematica 4 on my 450Mhz PIII your example with a matrix size of
> 1000x1000 takes 1.9s.
>
> Regards,
>
> Bob Abraham
>
>
> "Gareth J. Russell" <russell at cerc.columbia.edu> wrote in message
> news:8iccl1\$9oj at smc.vnet.net...
> > Hello,
> >
> > My first post. I am moving from another software package to Mathematica,
> and
> > I often work with very large matrices in the context of simulations, so
> > speed is important. Here is a problem for which I would like a serious
> speed
> > increase.
> >
> > Given a matrix of real numbers, I want to generate a new matrix where
> > values from a certain range are replaced with 1, and the remainder with
> > zero. Matrix size is often on the order of 1000 by 1000 (they are always
> > square).
> >
> > I defined a function that does the comparison using If[],
> > and made it listable:
> >
> > d2wreal[w_,l_,u_] := If[(w>l)&&(D<=u),1,0]
> > SetAttributes[d2wreal,Listable]
> >
> > These are typical times:
> >
> > a = Table[Random[], {i, 1, 100}, {j, 1, 100}];
> > t = Timing[b = d2wreal[a, 0.2, 0.4]][[1]]
> > 0.46666666666666856 Second
> >
> > a = Table[Random[], {i, 1, 1000}, {j, 1, 1000}];
> > t = Timing[b = d2wreal[a, 0.2, 0.4]][[1]]
> > 65.55 Second
> >
> > I tried to generate some compiled alternatives myself, but I
> couldn't find
> > one that was faster. I reckon a 10x speed-up should be
> possible, possibly
> > more. My other software takes only 0.75 seconds for the 1000 by 1000
> matrix
> > function.
> >
> > Can anyone suggest a way to speed this up?
> >
> > Thanks,
> >
> > Gareth
> >
> >
> > ==================================================
> > Dr. Gareth J. Russell
> >
> > NEW ADDRESS and E-MAIL FROM 1ST SEPTEMBER, 1999
> >
> > Center for Environmental Research and Conservation
> >   MC 5556
> >     Columbia University
> >       1200 Amsterdam Avenue
> >         New York, NY 10027, U.S.A.
> >
> >  Phone: ++1 212 854 5094
> >    Fax: ++1 212 854 8188
> > E-mail: russell at cerc.columbia.edu
> >    WWW: http://web.utk.edu/~grussell   (NO CHANGE)

Hi Bob, Gareth, and MathGroup,

This interests me. I want to know why mapping a pure function onto an array
is so much faster than mapping the name of a user defined function.

op1[l_, u_][x_] := If[l <= x <= u, 1, 0]

op2[l_, u_] := If[l <= # <= u, 1, 0] &

a = Table[Random[], {i, 1, 1000}, {j, 1, 1000}];

Map[op1[0.2, 0.4], a, {2}]; // Timing
{16.25 Second, Null}

Map[op2[0.2, 0.4], a, {2}]; // Timing
{0.77 Second, Null}

The Mathematica Book doesn't seem to explain this.

David Park