[Date Index]
[Thread Index]
[Author Index]
Re: large lists with variable and if condition (vrs. 8.0.4)
*To*: mathgroup at smc.vnet.net
*Subject*: [mg124714] 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:54:11 -0500 (EST)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
This is fast:
Timing[
b = Map[If[# != 0, .75 (1 - #^2), 0] &,
Clip[Outer[Subtract, data, data]/x, {-1, 1}, {0, 0}], {2}] +
SparseArray[{Band[{1, 1}] -> .75}, {leg, leg}];]
but it depends on zeroes occurring ONLY on the diagonal in the Outer
result. That's a fair bet for the random data posted, but I'm not sure the
poster wanted that limitation.
In addition to the codes I already posted, I'll add this one:
Clear[f, mat4, x]
f[x_] = Piecewise[{{.75 (1 - x^2), -1 < x < 1}}];
mat4[bw_Real] := Map[f, Outer[Subtract, data, data]/bw, {2}]
Timing[four = mat4[0.1];]
{0.040412, Null}
Bobby
On Wed, 01 Feb 2012 02:50:09 -0600, Bill Rowe <readnews at sbcglobal.net>
wrote:
> On 1/31/12 at 5:39 AM, kristophs.post at web.de (Chris) wrote:
>
>> 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}];
>
> a faster way to construct this table would be
>
> list = (data - Reverse[data])/g;
>
> This avoids using Part which can slow things down.
>
>> (*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.
>
> Mathematica is not evaluating this because it cannot. It is
> impossible to determine whether
>
> Abs[(data[[j]]-data[[i]])/x]
>
> is less than one if x is not defined.
>
>> 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.
>
> Here too, the use of Part can be avoided.
>
> Using your code I get with x = .5:
>
> In[34]:= Timing[
> 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}];]
>
> Out[34]= {0.053099,Null}
>
> Using functional constructs I get:
>
> In[35]:= Timing[
> b = Map[If[# != 0, .75 (1 - #^2), 0] &,
> Clip[Outer[Subtract, data, data]/x, {-1, 1}, {0, 0}], {2}] +
> SparseArray[{Band[{1, 1}] -> .75}, {leg, leg}];]
>
> Out[35]= {0.027723,Null}
>
> and
>
> In[36]:= list2 == b
>
> Out[36]= True
>
> Note, things will be slower if you defer assigning a value to x
> then later assign a value using a replacement rule. Pattern
> matching definitely tends to slow things down. Faster would be
> to use Block to temporarily assign a value to x. That is:
>
> In[37]:= d = list2/g;
>
> In[38]:= Timing[c = d /. g -> 1;]
>
> Out[38]= {0.015705,Null}
>
> In[39]:= Timing[y = Block[{g = 1}, d];]
>
> Out[39]= {0.004637,Null}
>
> In[40]:= c == y
>
> Out[40]= True
>
>
--
DrMajorBob at yahoo.com
Prev by Date:
**Re: Plotter for complex polynomials (complex coefficients)**
Next by Date:
**Re: large lists with variable and if condition (vrs. 8.0.4)**
Previous by thread:
**Re: large lists with variable and if condition (vrs. 8.0.4)**
Next by thread:
**Re: large lists with variable and if condition (vrs. 8.0.4)**
| |