Re: Tagged list processing
- To: mathgroup at smc.vnet.net
- Subject: [mg86915] Re: Tagged list processing
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 26 Mar 2008 04:52:45 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <fsa5fq$adp$1@smc.vnet.net>
carlos at colorado.edu wrote:
> This is related to a recent question. Best stated through an example.
> I have two flat lists of equal length:
>
> tag={0,0,1,0,0, -3,0,0,0,0, 2,4,1,0,0};
> val={0,0,4,5,6, P,1/2,0,-3.0,a+4, 8,16.4,0,Sin[ang],Cos[ang]};
>
> tag contains only integers whereas val can have numbers or
> expressions that will (eventually) evaluate to a number.
>
> Task. Generate a pointer list to all nz values in tag, and a
> corresponding list of values:
>
> nztag={3,6,11,12,13}; (* may be empty *)
> nzval={4,P,8,16.4,0}; (* may be empty *)
>
> and also build the complementary lists
>
> ztag={1,2,4,5,7,8,9,10,14,15};
> zval={0,0,5,6,1/2,0,-3.0,a+4, Sin[ang],Cos[ang]};
>
> Obviously ztag=Position[tag,0]; but which
> form argument do I use for nztag in Position[tag,form]?
> Can I build zval and nzval with Table for speed ?
> (lists may contain 10^4 to 10^6 items)
>
> Constraint: must work in 4.0 thru 6.0 since some of my
> remote students have legacy academic versions. Thanks.
The following approach should work with version 4.0 and above (I have
discarded a fastest solution that used Pick). On my system, it yields
the four lists in less than 0.2 seconds, starting with two lists of
150,000 elements each.
In[1]:= tag = {0, 0, 1, 0, 0, -3, 0, 0, 0, 0, 2, 4, 1, 0, 0};
val = {0, 0, 4, 5, 6, P, 1/2, 0, -3.0, a + 4, 8, 16.4, 0, Sin[ang],
Cos[ang]};
In[3]:= ztag = Flatten@Position[tag, 0]
Out[3]= {1, 2, 4, 5, 7, 8, 9, 10, 14, 15}
In[4]:= nztag = Complement[Range[Length@tag], ztag]
Out[4]= {3, 6, 11, 12, 13}
In[5]:= (* Alternatively, though slower than above *)
nztag =
Flatten@Position[tag, x_ /; x != 0]
Out[5]= {3, 6, 11, 12, 13}
In[6]:= nzval = val[[nztag]]
Out[6]= {4, P, 8, 16.4, 0}
In[7]:= zval = val[[ztag]]
Out[7]= {0, 0, 5, 6, 1/2, 0, -3., 4 + a, Sin[ang], Cos[ang]}
In[8]:= tag = Flatten@Table[tag, {10^4}];
val = Flatten@Table[val, {10^4}];
In[10]:= First@Timing[ztag = Flatten@Position[tag, 0]]
Out[10]= 0.149143
In[11]:= First@Timing[nztag = Complement[Range[Length@tag], ztag]]
Out[11]= 0.043276
In[12]:= First@Timing[nzval = val[[nztag]]]
Out[12]= 0.004319
In[13]:= First@Timing[zval = val[[ztag]]]
Out[13]= 0.011485
In[14]:= %%%% + %%% + %% + %
Out[14]= 0.208223
In[15]:= Length@tag
Out[15]= 150000
Best regards,
--
Jean-Marc