2
$\begingroup$

In FunctionCompile, system does not recognize external functions and external global variables by default. But we can use KernelFunction to call external function. However, I cannot find any method to use variables or arrays in kernel. Is it possible to use them?

$\endgroup$
1
  • 3
    $\begingroup$ Pass them as an argument to the function why not $\endgroup$
    – Jason B.
    Oct 26, 2022 at 21:55

2 Answers 2

3
$\begingroup$

It seems like it is possible:

$numberOfWeekDaysInAYear = 260; (* global variable *)
annualise[days_] := days/$numberOfWeekDaysInAYear;

annualiseC =
  FunctionCompile[
   Function[
    Typed[days, "MachineInteger"],
    Typed[KernelFunction[annualise], {"MachineInteger"} -> "MachineInteger"][days]
    ]
   ];
annualiseC[120] (*6/13*)

$\endgroup$
2
$\begingroup$

Such as you have a global variables a:

a = 7;

You can use it in FunctionCompile with a With like this:

cf = With[{avalue = a}, FunctionCompile[
   Function[Typed[arg, "MachineInteger"], arg + avalue]]]
cf[20]

27

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.