"Julia" (External Evaluation System)

Details

ExternalEvaluate Usage

  • ExternalEvaluate["Julia",code] executes the code string in a Julia REPL and returns the results as a Wolfram Language expression.
  • ExternalEvaluate["Julia""String",code] executes the code string in a Julia REPL and returns the output as a Wolfram Language string.

Data Types

  • The following Julia built-in types are supported:
  • BoolTrue | FalseBoolean True/False values
    Int8, Int16, Int32, etc.Integerinteger
    Float16, Float32, etc.Realreal number
    Complex{Float16}, Complex{Float32}, etc.Complexcomplex number (returning from Julia only)
    StringStringstring of characters
    ArrayListlist of objects
    DictAssociationassociative array
    SetListunordered set without duplicates
    TupleListfixed-length container
    Date, DateTimeTemplateBox[{DateObject, paclet:ref/DateObject}, RefLink, BaseStyle -> {3ColumnTableMod}]dates, date intervals
    NaNIndeterminatenot-a-number
    nothingNullno return value

Usage Notes

  • String templates (<**>) can be used to evaluate and insert Wolfram Language expressions into the Julia code string.

Examples

open allclose all

Basic Examples  (1)

Evaluate 2+2 in Julia and return the result:

Arrays are returned as lists:

Type > and select Julia from the drop-down menu to get a code cell that uses ExternalEvaluate to evaluate:

map(sqrt, 1:1:10)

Dates are returned from Julia as DateObject expressions:

Scope  (20)

Evaluate a Boolean statement in Julia and return the result:

Concatenate strings in Julia and return the result:

Dictionaries in Julia are returned as associations:

Arrays in Julia are returned as List:

Session Options  (9)

"ReturnType"  (3)

For the Julia evaluation system, the default return type is "Expression":

Numbers, strings, lists and associations are automatically imported for the "Expression" return type:

The return type of "String" returns a string of the result by calling the Julia function repr:

"Version"  (1)

You can use "Version" to make sure only a particular Julia version is going to be used:

You can specify a minor or patch version:

"Evaluator"  (1)

Start a Julia session using a specified "Evaluator":

Check the evaluator used in the session:

Close the session:

"SessionProlog"  (1)

"SessionEpilog"  (1)

Use "SessionEpilog" to perform a side effect at the end of a session:

"Prolog"  (1)

Use "Prolog" to perform a side effect before every evaluation:

"Epilog"  (1)

Use "Epilog" to perform a side effect after every evaluation:

Command Options  (10)

"Command"  (3)

When only a string of Julia code is provided, the command is directly executed:

The above is equivalent to writing the command using this form:

Use a File wrapper to run the code in a file:

The above is equivalent to writing the command using this form:

Put code in a CloudObject:

Evaluate directly from the cloud:

The above is equivalent to writing the command using this form:

"ReturnType"  (1)

By default, the command is executed using the "ReturnType" specified during the session creation:

Specifying a "ReturnType" in the command overrides the "ReturnType" for the session:

"Arguments"  (2)

Use "Arguments" to call the result of the evaluation with arguments:

When a non-list argument is provided, a single argument is passed to the function:

If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:

You can define a function inside "Command" and directly call it with "Arguments":

The same result can be achieved by using a Rule:

You can also pass arguments by creating an ExternalFunction:

"Constants"  (1)

Use "Constants" to permanently set global variables before the command runs:

"TemplateArguments"  (3)

When running a command, you can inline a TemplateExpression:

You can explicitly fill TemplateSlot using "TemplateArguments":

When a non-list argument is provided, a single template argument is passed to the template:

If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:

You can name template slots and use an Association to pass named arguments to the template:

Applications  (2)

Use the mean function from the Statistics package in Julia:

Use the function:

Close the session:

Use the OrdinaryDiffEq package in Julia to solve a radioactive decay problem:

Plot the solution in Wolfram Language:

Properties & Relations  (2)

Define a Wolfram Language function that calls a Julia function:

Close the session:

A NumericArray is sent to Julia as an array with the same element type:

This shows the native Julia type for the incoming array:

Close the session: