0
$\begingroup$

I am using Mathematica 12.2 with MacOS, and I do this:

randFunct=Function[n,Module[{da=CreateDataStructure@"DynamicArray"},
  Do[da["Append",RandomReal[]],n];
  Developer`ToPackedArray@da
]];

Below we see this function returns a DataStructure, and we can use Normal to make the result a List we can work with. randFunc example Next I use the above Function in FunctionCompile.

With[{randFunct=randFunct},
  rand=FunctionCompile[Function[{Typed[n,"MachineInteger"]},randFunct[n]]]
];

I find it strange that this version returns a list we can work with. rand example

Anyway I like the two step process above. First I write a pure function and ensure it works correctly. Then I use the pure function in CompileFunction, and it takes a while to compile.

$\endgroup$
1
  • 1
    $\begingroup$ Yes, I am surprised, but rather more with the uncompiled version than the compiled one. The reason for my surprise is the fact that in the last line of your code you convert the dynamic array into packed array. Returning a packed array in a compiled function is standard, and should also work in the uncompiled version. In fact that it returns a dynamic array appears to me to be a bug. $\endgroup$ Oct 1, 2023 at 12:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.