The Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.


Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Topic: List Replace Problems
Replies: 8   Last Post: Aug 30, 2005 4:44 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
wes

Posts: 24
Registered: 12/13/04
Re: List Replace Problems
Posted: Aug 28, 2005 11:29 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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.




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.