MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Can Divisors Be Compile?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg59610] Re: Can Divisors Be Compile?
  • From: albert <awnl at arcor.de>
  • Date: Sun, 14 Aug 2005 04:38:00 -0400 (EDT)
  • References: <ddk8kj$1bq$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

hi mike,

there are two pitfalls:

> dvs = Compile[{{a, _Integer} }, Module[{w}, w = {0}; Length[ w =
>        Divisors[a]]; w ]];
> dvs[6]-> {1, 2, 3, 6}
> 
> Hear I've trick mathematica by using Length[].

I guess you say this because this way Mathematica is not complaining like here:

In[1]:= dvs = Compile[{{a, _Integer}}, Divisors[a]]

Out[1]= CompiledFunction[{a}, Divisors[a], -CompiledCode-]

In[2]:= dvs[6]

CompiledFunction::cfse:
   Compiled expression {1, 2, 3, 6} should be a machine-size integer.

CompiledFunction::cfex:
   External evaluation error at instruction 2; proceeding with uncompiled
     evaluation.

Out[2]= {1, 2, 3, 6}

This could also be achieved like this (see the help of Compile for the last
argument):

In[3]:= dvs = Compile[{{a, _Integer}}, Divisors[a], {{Divisors[_], _Integer,
1}}];

In[4]:= dvs[6]

Out[4]= {1, 2, 3, 6}

> I'm looking for example of a compiled Divisors[].

... but still is not a compiled version of Divisors, as you can see by
inspecting the compiled function, which contains an "external" call back to
mathematica for Divisors. This CompiledFunction will not evaluate any
faster than just calling Divisors by it's own. If you want to call Divisors
within a more complicated expression, it might help though. Anyway my
experience is that compiled-functions which have to do callbacks to
mathematica are only in rare cases faster than ordinary code.

Albert 


  • Prev by Date: Re: Defining functions using the output of an other function
  • Next by Date: Re: what does this parentheses mean?
  • Previous by thread: Can Divisors Be Compile?
  • Next by thread: Defining functions using the output of an other function