Re: Pure functions vs rule based functions inside NIntegrate
- To: mathgroup at smc.vnet.net
- Subject: [mg132265] Re: Pure functions vs rule based functions inside NIntegrate
- From: Richard Fateman <fateman at cs.berkeley.edu>
- Date: Tue, 28 Jan 2014 06:13:22 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
- References: <lc2fl5$lnt$1@smc.vnet.net>
On 1/26/2014 12:05 AM, Yi Wang wrote: > Hello, > > As I was told, only pure functions can be compiled. Also, NIntegrate > or NDSolve first compiles a function before doing numerical > operations. It evaluates its first argument to an expression, so the two examples you have here are equivalent. For illustration, see how many times something is printed for... f4[x_] := (Print[x]; Sin[x]); NIntegrate[f4[x],{x,0,100}] f5 = (Print[#]; Sin[#]) & ; NIntegrate[f5[x],{x,0,100}] (Just once) One of the difficulties in using Mathematica is that the semantics of the commands may be poorly documented or specified, and in fact may change between versions. Typical users adopt a trial and error approach and hope that their notions of consistency are operationally correct. Compare the above to Plot[f5[x],{x,0,100}] (many times, at least in version 9). Of course if you truly need lots of fast multi-dimensional numerical integration, there may be better packages available to you. Thus, I would have expected that using pure functions in > NIntegrate or NDSolve could be faster than rule based functions. > > However, in tests I didn't find such a difference. For example, > > f1[x_, y_] := Sin[Sin[x Sin[y]]]; AbsoluteTiming@NIntegrate[f1[x, y], > {x, 0, 100}, {y, 0, 100}] > > and > > f2 = Sin[Sin[#1 Sin[#2]]] & AbsoluteTiming@NIntegrate[f2[x, y], {x, > 0, 100}, {y, 0, 100}] > > takes about the same time. How can I tell if both are compiled, or > neither, and why? > > Thanks! Yi >