MathGroup Archive 2005

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

Search the Archive

Re: List Replace Problems

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60039] Re: List Replace Problems
  • From: albert <awnl at arcor.de>
  • Date: Tue, 30 Aug 2005 04:42:59 -0400 (EDT)
  • References: <deeom3$3rl$1@smc.vnet.net> <dehiaa$boo$1@smc.vnet.net> <detver$cfm$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

> 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.

this should be possible, too, but it is more tricky to be achived. You need
to supply the symbolname (unevaluated) of the variable that holds the list
to your function. You need to modify your original definition for dlst:

>>dlst[u_List] = Do[u[[i]] /= i,  {i, 1, 5}];

this will prevent the first argument from being evaluated:

SetAttributes[dlst,HoldFirst];

and now you need a more evolved pattern to check whether the input is o.k.
Here I check whether it is a symbol whose OwnValue is a list (the part
after the question mark is a pure function whose arguments are
evaluated...). I have also changed the upper value for the do loop to the
length of the list, so it can handle lists of arbitrary length:

dlst[u_Symbol?(MatchQ[#,_List]&)]:=Do[u[[i]]/=i,{i,1,Length[u]}]

if you want the function to return the changed list, you could do this:

dlst[u_Symbol?(MatchQ[#, _List] &)] := (
Do[u[[i]] /= i, {i, 1, Length[u]}]; 
u
)

when you check the MemoryInUse[] before and after use of this function, you
will find that memory still grows, but the growth is less then the
bytecount of the list, so the list is changed in place but there is some
overhead. (Better set $HistoryLength=0 before measureing MemoryInUse,
explanations for this you will find in various recent threads about memory
usage).

cheers,

Albert


  • Prev by Date: Re: my wish list for Mathematica next major version
  • Next by Date: Exporting and importing arbitrary expressions efficiently
  • Previous by thread: Re: List Replace Problems
  • Next by thread: Does ContourPlot behave correctly?