"Julia" (External Evaluation System)
Details
- Julia Version 0.7 and higher is supported. For ExternalFunction support, Julia 1.2 or higher is required.
- To configure Julia for use in the Wolfram Language, follow the instructions from the Configure Julia for ExternalEvaluate workflow.
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:
-
Bool True | False Boolean True/False values Int8, Int16, Int32, etc. Integer integer Float16, Float32, etc. Real real number Complex{Float16}, Complex{Float32}, etc. Complex complex number (returning from Julia only) String String string of characters Array List list of objects Dict Association associative array Set List unordered set without duplicates Tuple List fixed-length container Date, DateTime ![TemplateBox[{DateObject, paclet:ref/DateObject}, RefLink, BaseStyle -> {3ColumnTableMod}] TemplateBox[{DateObject, paclet:ref/DateObject}, RefLink, BaseStyle -> {3ColumnTableMod}]](Files/Julia.en/1.png)
dates, date intervals NaN Indeterminate not-a-number nothing Null no return value
Examples
open all close allBasic Examples (1)
Evaluate 2+2 in Julia and return the result:
ExternalEvaluate["Julia", "2+2"]ExternalEvaluate["Julia", "[1,2,3,4]"]Type > and select Julia from the drop-down menu to get a code cell that uses ExternalEvaluate to evaluate:
Dates are returned from Julia as DateObject expressions:
ExternalEvaluate["Julia", "using Dates; DateTime(2022)"]Scope (20)
Evaluate a Boolean statement in Julia and return the result:
ExternalEvaluate["Julia", "true || false"]Concatenate strings in Julia and return the result:
ExternalEvaluate["Julia", "\"hello\" * \" \" * \"world\""]Dictionaries in Julia are returned as associations:
ExternalEvaluate["Julia", "Dict(\"one\"=>1, \"two\"=>2, \"three\"=>3)"]Arrays in Julia are returned as List:
ExternalEvaluate["Julia", "[1 2 3;4 5 6]"]Session Options (9)
"ReturnType" (3)
For the Julia evaluation system, the default return type is "Expression":
ExternalEvaluate["Julia", "2+2"] === ExternalEvaluate["Julia" -> "Expression", "2+2"]Numbers, strings, lists and associations are automatically imported for the "Expression" return type:
ExternalEvaluate["Julia", "[1, 2, 3]"]MatchQ[%, {__Integer}]The return type of "String" returns a string of the result by calling the Julia function repr:
ExternalEvaluate["Julia" -> "String", "[1, 2, 'א']"]ExternalEvaluate["Julia", "repr([1, 2, 'א'])"]"Version" (1)
You can use "Version" to make sure only a particular Julia version is going to be used:
ExternalEvaluate[{"Julia", "Version" -> "1.7"}, "repr(VERSION)"]You can specify a minor or patch version:
ExternalEvaluate[{"Julia", "Version" -> "1.8.2"}, "repr(VERSION)"]"Evaluator" (1)
Start a Julia session using a specified "Evaluator":
session = StartExternalSession[{"Julia", "Evaluator" -> "C:\\Users\\WolframUser\\AppData\\Local\\Programs\\Julia-1.8.2\\bin\\julia.exe"}]Check the evaluator used in the session:
session["Evaluator"]DeleteObject[session]"SessionProlog" (1)
"SessionEpilog" (1)
"Prolog" (1)
Command Options (10)
"Command" (3)
When only a string of Julia code is provided, the command is directly executed:
ExternalEvaluate["Julia", "2+2"]The above is equivalent to writing the command using this form:
ExternalEvaluate["Julia", <|"Command" -> "2+2"|>]Use a File wrapper to run the code in a file:
ExternalEvaluate["Julia", {
<|"Command" -> File["ExampleData/add.jl"]|>,
<|"Command" -> "add2(3)"|>
}]The above is equivalent to writing the command using this form:
ExternalEvaluate["Julia", {
File["ExampleData/add.jl"],
"add2(3)"
}]Put code in a CloudObject:
cloudObj = CloudExport["print(\"Hello World!\")", "Text", CloudObject["hello-world-jl"]]Evaluate directly from the cloud:
ExternalEvaluate["Julia", <|"Command" -> cloudObj|>]The above is equivalent to writing the command using this form:
ExternalEvaluate["Julia", cloudObj]"ReturnType" (1)
By default, the command is executed using the "ReturnType" specified during the session creation:
ExternalEvaluate[
{"Julia", "ReturnType" -> "String"},
"Dict('a'=> 2)"
]Specifying a "ReturnType" in the command overrides the "ReturnType" for the session:
ExternalEvaluate[
{"Julia", "ReturnType" -> "String"},
{
<|"Command" -> "Dict('a'=> 2)"|>,
<|"Command" -> "Dict('a'=> 2)", "ReturnType" -> "Expression"|>
}
]"Arguments" (2)
Use "Arguments" to call the result of the evaluation with arguments:
ExternalEvaluate[
"Julia",
<|"Command" -> "+", "Arguments" -> {0, 5}|>
]When a non-list argument is provided, a single argument is passed to the function:
ExternalEvaluate[
"Julia",
<|"Command" -> "repr", "Arguments" -> 1|>
]If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:
ExternalEvaluate[
"Julia",
{
<|"Command" -> "repr", "Arguments" -> 1|>,
<|"Command" -> "repr", "Arguments" -> {1}|>,
<|"Command" -> "repr", "Arguments" -> {{1}}|>
}
]You can define a function inside "Command" and directly call it with "Arguments":
ExternalEvaluate[
"Julia",
<|"Command" -> "f(a, b)= a+b", "Arguments" -> {1, 2}|>
]The same result can be achieved by using a Rule:
ExternalEvaluate[
"Julia",
"f(a, b)= a+b" -> {1, 3}
]You can also pass arguments by creating an ExternalFunction:
session = StartExternalSession["Julia"]function = ExternalFunction[session, "f(a, b)= a+b"]function[1, 3]DeleteObject[session]"Constants" (1)
"TemplateArguments" (3)
When running a command, you can inline a TemplateExpression:
x = RandomReal[]
ExternalEvaluate["Julia", "1 + <* x *>"]You can explicitly fill TemplateSlot using "TemplateArguments":
ExternalEvaluate[
"Julia",
<|"Command" -> "2 + ``", "TemplateArguments" -> 1|>
]When a non-list argument is provided, a single template argument is passed to the template:
ExternalEvaluate[
"Julia",
<|"Command" -> "repr(``)", "TemplateArguments" -> 1|>
]If you need to pass a list as the first argument, you must wrap it with an extra list explicitly:
ExternalEvaluate[
"Julia",
{
<|"Command" -> "repr(``)", "TemplateArguments" -> 1|>,
<|"Command" -> "repr(``)", "TemplateArguments" -> {1}|>,
<|"Command" -> "repr(``)", "TemplateArguments" -> {{1}}|>
}
]You can name template slots and use an Association to pass named arguments to the template:
ExternalEvaluate[
"Julia",
<|"Command" -> "collect(range(`start`, `length`, `end`))", "TemplateArguments" -> <|"start" -> 1, "end" -> 10, "length" -> 10|>|>
]Applications (2)
Use the mean function from the Statistics package in Julia:
session = StartExternalSession["Julia"];
ExternalEvaluate[session, "using Pkg; Pkg.add(\"Statistics\")"];
mean = ExternalEvaluate[session, "using Statistics; mean"]mean[Range[50]]DeleteObject[session]Use the OrdinaryDiffEq package in Julia to solve a radioactive decay problem:
session = StartExternalSession["Julia"];
ExternalEvaluate[session, "using Pkg; Pkg.add(\"OrdinaryDiffEq\")"];{t, u} = ExternalEvaluate[session, "using OrdinaryDiffEq
c1 = 5.730
u0=1.0
tspan = (0.0, 1.0)
radioactivedecay(u,p,t) =-c1*u
prob = ODEProblem(radioactivedecay,u0,tspan)
sol = solve(prob,Tsit5())
[sol.t,sol.u]
"];Plot the solution in Wolfram Language:
ListLinePlot[Transpose[{t, u}],
PlotLabel -> "Carbon-14 half-life",
PlotStyle -> Red,
Epilog -> Point[Transpose[{t, u}]],
ImageSize -> 400,
Frame -> True,
FrameLabel -> {"Time in thousands of years", "Percentage left"}
]Properties & Relations (2)
Define a Wolfram Language function that calls a Julia function:
session = StartExternalSession["Julia"];sqrt = ExternalFunction[session, "sqrt"]sqrt[4]DeleteObject[session]A NumericArray is sent to Julia as an array with the same element type:
session = StartExternalSession["Julia"]This shows the native Julia type for the incoming array:
juliaTypeOf = ExternalFunction[session, "typeof"];juliaTypeOf[NumericArray[Range[100], "UnsignedInteger16"]]DeleteObject[session]Related Guides
Related Workflows
- Configure Julia for ExternalEvaluate
History
Introduced in 2020 (12.1)