How to use Bags in FunctionCompile?

11

4

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]

M.R.

Posted 2020-01-11T00:50:28.470

Reputation: 30 727

What is your question here? – MarcoB – 2020-01-11T16:51:31.607

1How to FunctionCompile the given Function. – M.R. – 2020-01-13T16:53:10.220

1@MarcoB and in doing so give an example of how to handle the things involved: Nothing, lists that grow to unknown lengths, bags, ... – M.R. – 2020-01-13T17:21:49.817

1I 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. – Arnoud Buzing – 2021-01-19T15:38:03.487

2I'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. – Arnoud Buzing – 2021-01-19T19:52:11.850

No answers