Re: large lists with variable and if condition (vrs. 8.0.4)

• To: mathgroup at smc.vnet.net
• Subject: [mg124713] Re: large lists with variable and if condition (vrs. 8.0.4)
• From: DrMajorBob <btreat1 at austin.rr.com>
• Date: Thu, 2 Feb 2012 04:53:50 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```Below is a faster code (mat2):

Timing[list3 =
Table[If[
Abs[(data[[j]] - data[[i]])/x] <
1, .75 (1 - ((data[[j]] - data[[i]])/x)^2) // Evaluate, 0.], {i,
1, leg}, {j, 1, leg}];]
Timing[
mat[bw_Real] := Evaluate[list3 /. x -> bw]]
Timing[one = mat[0.1];]

mat2[bw_Real] :=
Map[If[Abs@# < bw, .75 (1 - (#/bw)^2), 0] &,
Outer[Subtract, data, data], {2}]
Timing[two = mat2[0.1];]

Max@Abs[one - two]

{0.214734, Null} (* time to define list3 *)

{0.116032, Null} (* time to define mat *)

{0.04012, Null} (* time to calculate mat[0.1] *)

{0.000026, Null} (* time to define mat2 *)

{0.037063, Null} (* time to compute mat2[0.1] *)

2.22045*10^-16 (* numerical error *)

The time taken to define list3 and mat were wasted, and no doubt there are
FASTER codes than mat2, besides.

We can also define the function so that "data" is not a fixed vector:

Timing[mat3[data_, bw_Real] :=
Map[If[Abs@# < bw, .75 (1 - (#/bw)^2), 0] &,
Outer[Subtract, data, data], {2}]]
Timing[three = mat3[data, 0.1];]

Max@Abs[three - two]

{0.000033, Null}

{0.036789, Null}

0.

Bobby

On Wed, 01 Feb 2012 02:49:27 -0600, Oleksandr Rasputinov
<oleksandr_rasputinov at ymail.com> wrote:

> On Tue, 31 Jan 2012 10:42:10 -0000, Chris <kristophs.post at web.de> wrote:
>
>> Hi
>>
>> I would like to construct a large list or large table with some data
>> and one unknown variable which I need for optimization purposes in say
>> a for loop. Constructing the whole list at each iteration is time
>> consuming and slows the optimization down.
>>
>> First, I present some code that works just fine in order to get the
>> idea:
>>
>> (*inputs)
>> leg = 100;
>> data = RandomReal[{}, leg];
>>
>> (*constructing the list with the data in which g is a non-specfied
>> variable*)
>> Clear[list, list2, tab];
>> list = Table[(data[[i]] - data[[leg - i + 1]])/g, {i, 1, leg}];
>>
>> (*specifying the function and the result*)
>> tab[k_] := list /. g -> k;
>> tab[1]
>>
>> Now the following code does not work and I don't know why.
>>
>> list2=
>> Table[If[Abs[(data[[j]]-data[[i]])/x]<1.,.75 (1.-((data[[j]]-
>> data[[i]])/x)^2),0.],{i,1,leg},{j,1,leg}];
>> mat[bw_Real]:=list/.x->bw
>>
>> It seems to me, when looking at list2, that Mathematica is not
>> evaluating the when-part of the if-function when the if-part depends
>> on the unknown variable. Hence, an error message.
>>
>> Constructing the list as:
>> mat[bw_Real]:=Table[If[Abs[(data[[j]]-data[[i]])/bw]<1.,.75 (1.-
>> ((data[[j]]-data[[i]])/bw)^2),0.],{i,1,leg},{j,1,leg}];
>> is not an option because it slows things down.
>>
>>
>
> If I understood your question correctly, you simply want the conditional
> expression to be evaluated with the correct values for data[[i]] and
> data[[j]]. This does not happen by default because If has the attribute
> HoldRest, but you can correct the problem by modifying your code as
> follows:
>
> list2 = Table[
>   If[Abs[(data[[j]]-data[[i]])/x]<1.,
>    (* Note use of Evaluate *)
>    Evaluate[.75 (1.-((data[[j]]-data[[i]])/x)^2)],
>    0.
>   ], {i,1,leg},{j,1,leg}
> ];
>
> Evaluate can be used in this way to override the non-evaluation due to
> Attributes (such as HoldRest, HoldFirst, HoldAll, etc.) of any function
> argument. In fact, this is also needed for the definition of mat, because
> SetDelayed has HoldAll:
>
> mat[bw_Real] := Evaluate[list2 /. x -> bw];
>
> Evaluation of mat[bw] for any value is then reasonably fast (or at least
> faster than building the table anew each time).
>

--
DrMajorBob at yahoo.com

```

• Prev by Date: Re: large lists with variable and if condition (vrs. 8.0.4)
• Next by Date: Re: Derivative of experimental data
• Previous by thread: Re: large lists with variable and if condition (vrs. 8.0.4)
• Next by thread: Re: Compile function and AppendTo for lists (vrs.