MathGroup Archive 2012

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

Search the Archive

Re: Compile function and AppendTo for lists (vrs.

  • To: mathgroup at
  • Subject: [mg124704] Re: Compile function and AppendTo for lists (vrs.
  • From: Fred Simons <f.h.simons at>
  • Date: Wed, 1 Feb 2012 03:50:50 -0500 (EST)
  • Delivered-to:
  • References: <>

Hi Patrick,

Many thanks for your prompt reaction.  Here are two comments.

  Op 31-1-2012 11:39, Patrick Scheibe schreef:
> Hi Fred,
> some short notes:
>> appendBag2a = Compile[{{k, _Integer, 0}}, Module[{p = Bag[Most[{0}]]},
>>       Do[StuffBag[p, Bag[{1, i}]], {i, 1, k}];
>>       Table[BagPart[BagPart[p, i], All], {i, 1, k}]]];
>> That works fine and fast,
> But look that your type p is clearly wrong. p is a bag of bags of
> Integer and has to be initialized with
> Internal`Bag[Most[{Internal`Bag[{1}]}]]
> We can only guess about the reasons why it works anyway. If it is
> implemented with pointers, than it seems the integers of your p-Bag are
> used to store the pointers.
Clearly wrong is not quite clearly wrong. Indeed, with

p = Bag[Most[{0}]]}

we define p to be a bag with argument a list of integers. The fact that the implementation works suggests that in such a situation not only integers but also bags with integers are allowed as elements of the list.

>> The problem seems to be the initialization of the
>> locale variable p. I tried compilation with the definitions p=Bag[],
>> p=Bag[{}], p=Bag[Rest[{0}]], p=Bag[Rest[{{0,0}}]], or e.g.
> Now you know how.
No, not really.  Inspired by your remarks, I did some further tests with 
compiling functions containing a Bag, and it seems to be that that the 
compiled function runs smoothly only when the argument of Bag is a list 
of reals or a list of integers (maybe containing bags of reals or bags 
of integers). So when it is a list of a list of integers, as in the 
function I was trying to compile, the compiled function will always 
proceed with the uncompiled version.

In your function appendBag2 you use a Bag with Bag's inside the argument 
list. That is a very interesting idea. However, there is a big 
disadvantage: at the and the resulting expression will contain many 
Bag's. Since each Bag points to a raw expression stored somewhere, the 
amount of memory that is needed can be tremendous. In some of my tests 
indeed the kernel closed because of lack of memory.  So my feeling is 
that it is better not to use this technique and instead to flatten the 
tensor to a list, and at the end use the function Partition fo find the 
tensor you are looking for. In this way, only one Bag is needed, thus 
not requiring an excessive amount of memory, and the compiled function 
will run smoothly.
> Cheers
> Patrick
Once again many thanks and best wishes,


  • Prev by Date: Re: large lists with variable and if condition (vrs. 8.0.4)
  • 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: Compile function and AppendTo for lists (vrs. 8.0.4)