11
$\begingroup$

I was reading this question and it wanted to know how to FunctionCompile this:

Function[x,
 If[IntegerQ @ Sqrt[80892036 + 17994 x (1 + x) (-5995 + 5998 x)], x, 
  Nothing] /@ Range[6694300, 31072325]

I don't know much about FunctionCompile, if it can handle Nothing, if bags work for within them, and if so what Types would they be?

This obviously won’t work, but is a jumping off point:

na = NumericArray[Range[6694300, 31072325], "UnsignedInteger32"];

doit = FunctionCompile[
   Function[{Typed[arg, 
      TypeSpecifier["NumericArray"]["UnsignedInteger32", 1]]},
    Module[{
      f = 
       Typed[Function[Typed[x, "UnsignedInteger32"], 
         Sqrt[80892036 + 17994 x (1 + x) (-5995 + 5998 x)]], 
        "UnsignedInteger32" -> "Real64"];
      iq = 
       Typed[KernelFunction[IntegerQ[f[#]] &], {"Real64"} -> 
         "Boolean"]},
     If[iq@#, #, Nothing] & /@ arg
     ]]];
doit[na]
$\endgroup$
5
  • $\begingroup$ What is your question here? $\endgroup$
    – MarcoB
    Jan 11 '20 at 16:51
  • 1
    $\begingroup$ How to FunctionCompile the given Function. $\endgroup$
    – M.R.
    Jan 13 '20 at 16:53
  • 1
    $\begingroup$ @MarcoB and in doing so give an example of how to handle the things involved: Nothing, lists that grow to unknown lengths, bags, ... $\endgroup$
    – M.R.
    Jan 13 '20 at 17:21
  • 1
    $\begingroup$ I think this will be difficult to do with FunctionCompile in a way that gives you speedup benefits. The expression 80892036 + 17994 x (1 + x) (-5995 + 5998 x) grows quickly so the result can not be stored in machine integers. Which means that even the function compiles and works fast for small inputs, it will be slower when it needs bignum integers. $\endgroup$ Jan 19 at 15:38
  • 2
    $\begingroup$ I've asked the compiler developers and in the next version of WL there will likely be 128-bit integer support. They also gave me sample code which runs the first input in less than a second (versus ~2 hours in a normal WL evaluation). If you @M.R. are interested in seeing this work, please consider signing up for the prerelease program. $\endgroup$ Jan 19 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.