Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16490

Re: Pure functions vs rule based functions inside NIntegrate

From Richard Fateman <fateman@cs.berkeley.edu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Pure functions vs rule based functions inside NIntegrate
Date 2014-01-28 11:08 +0000
Message-ID <lc834b$3uj$1@smc.vnet.net> (permalink)
References <lc2fl5$lnt$1@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


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
>

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Find similar


Thread

Pure functions vs rule based functions inside NIntegrate Yi Wang <tririverwangyi@gmail.com> - 2014-01-26 08:05 +0000
  Re: Pure functions vs rule based functions inside NIntegrate Richard Fateman <fateman@cs.berkeley.edu> - 2014-01-28 11:08 +0000

csiph-web