Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

Topic: Compile function and AppendTo for lists (vrs. 8.0.4)
Replies: 8   Last Post: Feb 1, 2012 3:50 AM

Advanced Search

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

Posts: 327
Registered: 5/22/07
Re: Compile function and AppendTo for lists (vrs. 8.0.4)
Posted: Jan 26, 2012 3:30 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi,

using Oleks Internal`Bag suggestion, think of a list as a bag of numbers
and you can use this as replacement for the lists in your compile. Using
Oleks append-to version as comparison

appendTo =
Compile[{{k, _Integer, 0}},
Module[{mat = {{0, 0}}, i = 0},
For[i = 1, i <= k, i++, AppendTo[mat, {1, i}]];
Rest[mat]]]

and here is the Internal`Bag implementation

AppendTo[$ContextPath, "Internal`"];
appendBag = Compile[{{k, _Integer}},
Module[{p = Bag[], i = 1, tmpBag},
For[i = 1, i <= k, ++i,
tmpBag = Bag[];
StuffBag[tmpBag, 1];
StuffBag[tmpBag, i];
StuffBag[p, tmpBag];
];
Table[BagPart[BagPart[p, i], All], {i, k}]
]
];

Speedtest shows

In[55]:= First /@ {AbsoluteTiming[appendTo[10^5]],
AbsoluteTiming[appendBag[10^5]]}

Out[55]= {14.559237, 0.149063}

that the bag implementation is about 100 times faster. Unfortunately
there doesn't exist much documantation about this. One place to look is
here http://stackoverflow.com/a/6795762/1078614 where Daniel gives some
insights into the usage.

Hope this helps.

Cheers
Patrick



On Tue, 2012-01-24 at 05:06 -0500, kris wrote:
> Hi
>
> I have some trouble with my code and would like ask for your help. In
> general it is about appending data in form of a list to an existing
> list in a compiled function. As far as I understand Mathematica is not
> supporting what I am asking for. In order to understand the problem in
> more detail I present some "toy" code below.
>
> test=Compile[{{k,_Integer}},
> Module[{mat={}},
> For[i=1,i<=k,i++,
> AppendTo[mat,{1,i}];
> ];
> mat
> ]
> ];
>
> test[2]
> (*which produces an error*)
>
> Appending data in form of numbers for example works just fine but not
> with lists. Can anybody explain why Mathematica does not support
> appending lists to lists for compiled function? As an alternative I
> tried Reap and Sow, which also produces an error.
>
> However, what seems funny is the following code:
>
> mat={};
> test=Compile[{{k,_Integer}},
> For[i=1,i<=k,i++,
> AppendTo[mat,{1,i}];
> ];
> ];
>
> test[2];mat
>
> The above code produces the result that I was looking for in a
> cumbersome way. I would like to prefer compile code which produces the
> same result without calling the list mat again.
>
> Thanks for help I do appreciate it.
> Cheers,
> Kris
>







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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.