Re: Compile nested loops with depending index variables...
- To: mathgroup at smc.vnet.net
- Subject: [mg61471] Re: Compile nested loops with depending index variables...
- From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
- Date: Wed, 19 Oct 2005 23:07:20 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi, it is clear that a[[i]]=a[[i]]+b[[i]] can't be done in a compiled function, because this would require, that the argument a is not evaluated. Using a local variable to store the result require that the array is initialized in the compiled function and for the initialization, you has to run over all subscripts anyway. I think MapThread[F,{a,b},4] will do the job perfectly, without any compilation. Regards Jens "Christoph Lhotka" <lhotka at astro.univie.ac.at> schrieb im Newsbeitrag news:dj4oof$gl6$1 at smc.vnet.net... | First thank you all, but the main problem is still unbsolved. | | My specific problem is, that I have two rectangular arrays say a and b of | about 500 000 numbers (complex or real) each. I want to create a new array of | the same dimension and length, where each element has to be constructed by a | law depending on the position in above lists of the form: | | a[[i,j,k,l]]=F[a[[i,j,k,l]],b[[i,j,k,l]]] | | (where F means some function based on basic arithmetics) | | IT IS NOT NECESSARY TO RUN THROUGH ALL INDICES! SO IF i=I0, j<i, k<j, l<k for | any I0=1,...,n! | | To save computation time I wanted to compile and didn't succeed. Using a small | C++ program and MathLink will speed it up, but transferring millions of | numbers from the kernel to the C program and vice versa seems to slow it down | again. | | I think that it should be a basic feature for any compiler to understand such | a basic code construct like: | | Do[a[[i,j,k,l]]=a[[i,j,k,l]]+b[[i,j,k,l]],{i,1,n},{j,1,i},{k,1,j},{l,1,k}], | | but it doesn't work. | | Won't you agree with me? | | kind regards | | Christoph | | | | | On Sun, 16 Oct 2005 00:17:54 -0400 (EDT) | albert <awnl at arcor.de> wrote: | > Hi Christoph, | > | > this is from the HelpBrowser about Compile: | > | > o Nested lists given as input to a compiled function must be full arrays of | > numbers. | > | > | > the table you construct is not a "full array of numbers", since the length | > of each element is different: | > | > In[4]:= Length /@ Table[{i,j},{i,0,n},{j,0,i}] | > | > Out[4]= {1, 2, 3, 4} | > | > and obviously Compile not only doesn't like this sort of nested lists as | > input but also can't handle these anyware within the compiled code. This is | > not too surprising but not explicitly documented so. So I think if you | > really need to compile your code you need to change it in such a way that | > it constructs a "full array", e.g. the following compiles fine: | > | > fc = Compile[{n}, Table[If[j <= i, {i, j}, {-1, -1}], {i, 0, n}, {j, 0, | > n}]] | > | > you could then reconstruct the original nested list by: | > | > DeleteCases[fc[2], {-1, -1}, Infinity] | > | > It depends on your actual problem whether this is more efficient than not | > compiling, and usually only trying out will give you the answer. Memory | > usage is of course worse than when using the not compiled nested Table... | > | > This is another approach that is probably more memory efficient: | > | > fc = Compile[{{i, _Integer}}, Table[{i, j}, {j, 0, i}]] | > ffc = Function[{n}, Table[fc[i], {i, 0, n}]] | > | > note that using Function instead of ffc[n_] is often slightly more | > efficient | > because you avoid complicated pattern matching. The drawback is that you | > can't do checks about correct input that easily (like in ffc[n_Integer])... | > | > just try what is the most efficient way for your problem, I wouldn't | > exclude | > that not compiling gives you the fastest code in the end... | > | > hth | > | > albert | > | | -- Mag. Christoph Lhotka -- | University of Vienna / Institute for Astronomy | mail. lhotka at astro.univie.ac.at |