INTRODUCTION
One of the features Julia has to offer is Interoperability. Language interoperability could be defined as the ability for two or more languages to interact or communicate for effective data transmission in a system.
P.S: I wrote on Interoperability; check it out!
With the help of various packages, Julia can call several programming languages. JuliaInterop is a GitHub organization that has created several packages that can be used to integrate Julia with different languages. In this article, we will be looking at integration with Python, R, and C++.
1. Python
It is possible to call Python from Julia using PyCall. Then to install PyCall, run the command in the Julia REPL.
using Pkg
Pkg.add("PyCall")
PyCall will download the Miniconda installer and create a separated conda environment just for Julia all by itself.
An example
julia> usig PyCall
>>> math = pyimport("math")
>>> math.sin(math.pi / 4) # returns ≈ 1/√2 = 0.70710678...
2. R
To inter-operate Julia with the R language, the RCall package is used. Run the following commands on the Julia REPL
using Pkg
Pkg.add("RCall")
This will automatically install R using Conda if R is not detected. For further customization, check out the documentation page
Example
julia> using RCall # This will initialize the R process in the background
julia> num = 7
julia> print(num)
7
Macros transfer can also occur between variables in the R and Julia environments. The copied variable will have the same name as the original.
julia> x = 57
1
julia> @rput x
57
More methods can be found on the official website.
3. C++
It is possible to call C++ into Julia with the package Cxx. To install the Cxx
package, run the following command on the Julia REPL.
using Pkg
Pkg.add("Cxx")
For further installation guides (like system requirements), visit the repository README for more information.
A simple example of embedding a C++ function in Julia
# include headers
julia> using Cxx
julia> cxx""" #include<iostream> """
# Declare the function
julia> cxx"""
void mycppfunction() {
int z = 0;
int y = 5;
int x = 10;
z = x*y + 2;
std::cout << "The number is " << z << std::endl;
}
"""
# Convert C++ to Julia function
julia> julia_function() = @cxx mycppfunction()
julia_function (generic function with 1 method)
# Run the function
julia> julia_function()
The number is 52
N.B: Most of the examples were extracted from the official repository pages of these packages. Check out Juliainterop for more examples and guides on other languages that can be called into Julia.
CONCLUSION
It has been seen how possible it is to call some programming languages from Julia. This also establishes the fact that Julia is a friendly language and can be the go-to when it comes to building a system with multiple languages (Interoperability).
Top comments (2)
This is beautiful.
Thank you!