Re: Function compile problems
- To: mathgroup at smc.vnet.net
- Subject: [mg26155] Re: Function compile problems
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 30 Nov 2000 01:04:00 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <8vvq2p$38n@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, you know, that this what you are doing, ins perfect nonsense ? Fine. It is easy to see that test1 = Compile[{{n, _Integer}}, g[{r_, t_}] := r Cos[n t]; xy = Table[{x, y}, {x, -1, 1, 0.5}, {y, 1, -1, -0.5}]; Map[g[#] &, xy, {2}] // N] and test1[[4]] {{1, 3}, {26, Function[{n}, g[{r_, t_}] := r Cos[n t]], 2, 0, 0, 6, 0, 17}, {26, Function[{n}, xy = Table[{x, y}, {x, -1, 1, 0.5}, {y, 1, -1, -0.5}]], 2, 0, 0, 6, 0, 17}, {26, Function[{n}, Length[xy]], 2, 0, 0, 3, 0, 10}, {44, 10, 11}, {26, Function[{n}, Length[xy\[LeftDoubleBracket]1\[RightDoubleBracket]]], 2, 0, 0, 3, 0, 10}, {44, 10, 12}, {65, 11, 12, 3, 3, 7}, {9, 0, 15}, {14, 15, 13}, {86, 13, 11, 9}, {9, 0, 15}, {14, 15, 14}, {86, 14, 12, -3}, {26, Function[{n}, xy], 2, 0, 0, 3, 3, 8}, {71, 8, 0, 13, 0, 14, 1, 9}, {27, g, 3, 1, 9, 3, 1, 10}, {67, 10, 7}, {49, -5}, {6, 3, 3, 7}} has no effect for time saving because the time consuming parts Table[] and Map[g,_] can't be compiled. Compile[] is to evaluate *numerical* expression, all variables in side a compiled function must be numbers or tensors of numbers, has nobody told you that defining a function is not a numerical operation? The manual say "Types are handeled by compile are : _Integer,_Real,_Complex, True |False Nested lists given as input to a compiled function must be full arrays of numbers " More over, the many Function[] calls in the compiled code will slow down the performance to a level that is below the uncompiled evaluation. The final //N is nonsense, because all data in a compiled function are already numerical values The problem that compile has is, that your first Table[] generate a 3d array of numbers, but you Map[g[#],___] call will remove one degree an only a 2d array remain. Since Compile[] has created the memory for n x m x 2 numbers it can't find out how to store n x m numbers in the same place. In a compiled language like C the assigment double ***a; **a=0.5 would cause a compiler error as well. Removing all the trash in your function on ends up with test2 = Compile[{{n, _Integer}}, Table[x Cos[n y], {x, -1, 1, 0.5}, {y, 1, -1, -0.5}]] with perfect compiled code test2[[4]] {{1, 3}, {9, -1, 5}, {10, 0.5, 3}, {9, 1, 6}, {9, 1, 7}, {37, 5, 8}, {29, 7, 8, 7}, {41, 3, 4}, {19, 1, 7, 5}, {34, 5, 4, 4}, {95, 57, 3, 0, 4, 2, 0, 7}, {29, 6, 7, 6}, {9, 1, 7}, {10, -0.5, 5}, {9, 1, 8}, {9, -1, 9}, {37, 7, 10}, {29, 9, 10, 9}, {41, 5, 6}, {19, 1, 9, 7}, {34, 7, 6, 6}, {95, 57, 3, 0, 6, 2, 0, 9}, {29, 8, 9, 8}, {65, 6, 8, 2, 3, 0}, {9, 0, 9}, {19, 1, 9, 9}, {34, 9, 3, 9}, {19, 0, 5, 10}, {30, 10, 9, 9}, {15, 9, 6}, {86, 9, 6, 14}, {9, 0, 10}, {19, 1, 10, 9}, {34, 9, 5, 9}, {19, 0, 7, 10}, {30, 10, 9, 9}, {15, 9, 7}, {86, 10, 8, -12}, {19, 1, 0, 9}, {34, 9, 7, 9}, {95, 2, 3, 0, 9, 3, 0, 10}, {34, 6, 10, 9}, {66, 9, 0}, {49, -11}, {6, 3, 2, 0}} But since Table[] compile it's argument with out your help it is useless to do it by yourself. Regards Jens "Krautschik, Chris G" wrote: > > I understand that the compile function can be very tricky as not all > Mathematica commands are supported by the Compile function. I have the > following code that seems to work fine when not compiled: > > n = 4; g[{r_, t_}] := r Cos[n t]; > xy = Table[{x, y}, {x, -1, 1, 0.5}, {y, 1, -1, -0.5}]; > Map[g[#] &, xy, {2}] // N > > The output should look as follows: > {{0.653644, 0.416147, -1., 0.416147, 0.653644}, {0.326822, 0.208073, -0.5, > 0.208073, 0.326822}, {0., 0., 0., 0., 0.}, {-0.326822, -0.208073, > 0.5, -0.208073, -0.326822}, {-0.653644, -0.416147, > 1., -0.416147, -0.653644}} > > Now if I compile the same code through the following program: > > test1 = Compile[{{n, _Integer}}, > g[{r_, t_}] := r Cos[n t]; > xy = Table[{x, y}, {x, -1, 1, 0.5}, {y, 1, -1, -0.5}]; > Map[g[#] &, xy, {2}]//N > ]; > > test1[4], I get the following error message: > > CompiledFunction::"cfte": "Compiled expression 0.6536436208636119 > should be a rank 1 tensor of "machine-size real numbers." > > CompiledFunction::"cfex": "External evaluation error at instruction 17; > proceeding with uncompiled evaluation." > > {{0.653644, 0.416147, -1., 0.416147, 0.653644}, {0.326822, 0.208073, -0.5, > 0.208073, 0.326822}, {0., 0., 0., 0., 0.}, {-0.326822, -0.208073, > 0.5, -0.208073, -0.326822}, {-0.653644, -0.416147, > 1., -0.416147, -0.653644}} > > While the output is correct the program was never excecuted in compiled mode > therefore defeating the whole purpose of compiling in the first place. > > Now, if I had used h[x_,y_]:=r Cos[ n t] instead of g and then > Apply[h[#1,#2]&,xy{2}]//N (instead of the Map command) then the program > doesn't run at all in compiled form, although, it works fine in uncompiled > form. > > Any idea of what's going on here? > > Thanks, > Chris