KernelEvaluate

KernelEvaluate[expr]

evaluates expr in the Wolfram Language kernel, even when called from compiled code.

KernelEvaluate[x,expr]

captures the value of the compiled variable x for use in the evaluation of expr.

KernelEvaluate[{x1,x2,},expr]

captures the values of the xi for use in the evaluation of expr.

Details

  • The expression expr in KernelEvaluate[expr] can use Wolfram Language functions or definitions given by the user.
  • The compiled variables such as x in KernelEvaluate[x,expr] are bound lexically as they are in Function.
  • In KernelEvaluate[x, expr], the evaluation of expr cannot modify the value of x.
  • Typically, KernelEvaluate is wrapped in TypeHint to specify the type of the result.

Examples

open allclose all

Basic Examples  (3)

Use the Wolfram Engine evaluator to compute the AiryAi function from compiled code:

Execute the compiled function:

The result compares with the direct call:

Define a function in the Wolfram Engine:

Call the function from compiled code:

Use a local variable in compiled code:

The result compares with the direct call:

Scope  (4)

Use the result of KernelEvaluate as a function using an "InertExpression" type:

KernelEvaluate allows operations such as Fit to be integrated into the compiler:

Use KernelEvaluate to integrate file operations:

A function called from compiled code does not have to be defined when FunctionCompile is used:

It must be defined for the compiled code to run:

The definition can be changed and the new version will be used:

If there is no definition, there will be a runtime error:

Possible Issues  (2)

A runtime error happens if the output of KernelEvaluate does not match its declared type:

Variables given in KernelEvaluate are bound lexically and not dynamically:

This results in a runtime error because binding is lexical:

Neat Examples  (1)

Integrate NDSolve into compiled code:

Wolfram Research (2023), KernelEvaluate, Wolfram Language function, https://reference.wolfram.com/language/ref/KernelEvaluate.html.

Text

Wolfram Research (2023), KernelEvaluate, Wolfram Language function, https://reference.wolfram.com/language/ref/KernelEvaluate.html.

CMS

Wolfram Language. 2023. "KernelEvaluate." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/KernelEvaluate.html.

APA

Wolfram Language. (2023). KernelEvaluate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/KernelEvaluate.html

BibTeX

@misc{reference.wolfram_2024_kernelevaluate, author="Wolfram Research", title="{KernelEvaluate}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/KernelEvaluate.html}", note=[Accessed: 23-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_kernelevaluate, organization={Wolfram Research}, title={KernelEvaluate}, year={2023}, url={https://reference.wolfram.com/language/ref/KernelEvaluate.html}, note=[Accessed: 23-December-2024 ]}