MathGroup Archive 2005

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

Search the Archive

Re: List Replace Problems

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60030] Re: List Replace Problems
  • From: wesh <wesh at acm.org>
  • Date: Sun, 28 Aug 2005 23:14:42 -0400 (EDT)
  • References: <deeom3$3rl$1@smc.vnet.net> <dehiaa$boo$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Original Message:

>I want to perform some element by element operations on a class of
>lists. For a given list The operation in a much simplified form can be
>characterized by
>
>a = {1, 3, 5, 7, 9}; 
>Do[a[[i]] /= i, {i, 1, 5}]; 
>a
>
>Out[38]=
>{1, 3/2, 5/3, 7/4, 9/5}
>
>Now, however, if I try to define a function to do this for a general
>list, namely,
>
>dlst[u_List] =
>   Do[u[[i]] /= i,  {i, 1, 5}]; 
>a = {1, 3, 5, 7, 9}; 
>dlst[a]; 
>a
>
>Set::setps: 
>({1, 2, 3, 4, 5}) in assignment of part is not a symbol. ! More ...
>
>Out[39]=
>{1, 3, 5, 7, 9}
>
>I get the above error. It says I'm trying to assign one number to
>another number. Why does Mathematica perform in the first case but 
>refuse to in the second.
>
>I tried
>
>b = Do[ReplacePart[a,  a[[i]]/i, i], {i, 1, 5}]
>
>but it doesn't even bother to return an error message.
>
>
>How, can I get the desired function?
>
>Thanks,
>
>Wesh

----------------------------------------------------------

Thanks to all who replied.

For my problem there seem to be two easy answers. First using MapIndexed
(This is a little more complex than the original problem)

f[k_Integer, i_] :=  If[i != k, 1/(k^2 - i^2), 0]; 
lstIntegrate[lst_List, k_Integer] := MapIndexed[#1*f[k, #2[[1]]] & , 
lst]; 

Testing on a numeric list:
a = {1, 3, 5, 7, 9}; 
lstIntegrate[a, 2]
Out[1] = {1/3, 0, -1, -(7/12), -(3/7)}

Testing on a list of symbols:
b = {u, v, w, x, y, z}; 
lstIntegrate[b, 2]
b
Out[2]=
{u/3, 0, -(w/5), -(x/12), -(y/21), -(z/32)}
Out[3]=
{u, v, w, x, y, z}

The second approach is to form the factors in a separate list and then 
multiply
that list times the the input list. This is only possible if  factors 
are only 
dependent on the index and not on the content of the list position.

factorList = Table[f[2, i], {i, 1, 6}]
Out[4]=
{1/3, 0, -(1/5), -(1/12), (1/21), -(1/32)}

A timing measurement shows the factor list is far superior.

$HistoryLength = 0; 
Timing[Do[lstIntegrate[b, 2], {100000}]; ]
Out[24]=
{8.875*Second, Null}

Timing[Do[factorList*b, {100000}]; ]
Out[25]=
{0.875*Second, Null}

Unfortunately, there didn't seem to be a way to modify the input list in 
place
to save memory. In the actual problem the list elements are large two 
variable 
polynomials and  I was getting the kernel out of memory shutdown notice.


  • Prev by Date: Re: Solution from Reduce as boundaries for Distribution
  • Next by Date: Re: Manipulating the results of Maximize
  • Previous by thread: Re: List Replace Problems
  • Next by thread: Re: List Replace Problems