Julia Community

Cover image for Interoperability in Julia
Ifihan Olusheye
Ifihan Olusheye

Posted on • Updated on • Originally published at dev.to

Interoperability in Julia

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")
Enter fullscreen mode Exit fullscreen mode

PyCall will download the Miniconda installer and create a separated conda environment just for Julia all by itself.

An example

julia> using PyCall
>>> math = pyimport("math")
>>> math.sin(math.pi / 4) # returns ≈ 1/√2 = 0.70710678...
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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).

Discussion (6)

Collapse
digital_carver profile image
SundaraRaman R

For Python integration, PythonCall.jl is another option, that may have lower latency and a better interface.

For C++, CxxWrap is another popular option. The newer jluna is for going the other way i.e. for "accessing Julia's unique strengths through C++ ".

Collapse
ifihan profile image
Ifihan Olusheye Author

Thanks for this!

Collapse
kronosthelate profile image
KronosTheLate

Usage of Cxx looks so easy... I was however never able to install it correctly on my windows computer. I just tried again, but it still does not work...

What happens when I try to install Cxx.jl

(@v1.7) pkg&gt; activate --temp
  Activating new project at `C:\Users\DENNIS~1\AppData\Local\Temp\jl_nEoHSj`

(jl_nEoHSj) pkg&gt; add Cxx
    Updating registry at `C:\Users\Dennis Bal\.julia\registries\General`
    Updating git-repo `https://github.com/JuliaRegistries/General.git`
   Resolving package versions...
   Installed Cxx  v0.4.0
    Updating `C:\Users\Dennis Bal\AppData\Local\Temp\jl_nEoHSj\Project.toml`
  [a0b5b9ef] + Cxx v0.4.0
    Updating `C:\Users\Dennis Bal\AppData\Local\Temp\jl_nEoHSj\Manifest.toml`
  [b99e7846] + BinaryProvider v0.5.10
  [a0b5b9ef] + Cxx v0.4.0
  [2a0f44e3] + Base64
  [b77e0a4c] + InteractiveUtils
  [8f399da3] + Libdl
  [56ddb016] + Logging
  [d6f4376e] + Markdown
  [3fa0cd96] + REPL
  [ea8e919c] + SHA
  [6462fe0b] + Sockets
  [4ec0a83e] + Unicode
    Building Cxx  `C:\Users\Dennis Bal\.julia\scratchspaces\44cfe95a-1eb2-52ea-b672-e2afdf69b78f\71728149f62225470a4ae4260536a98a5009997c\build.log`
ERROR: Error building `Cxx`:
ERROR: LoadError: could not load library "LLVM"
The specified module could not be found.
Stacktrace:
 [1] dlopen(s::String, flags::UInt32; throw_error::Bool)
   @ Base.Libc.Libdl .\libdl.jl:117
 [2] dlopen (repeats 2 times)
   @ .\libdl.jl:117 [inlined]
 [3] dlpath(libname::String)
   @ Base.Libc.Libdl .\libdl.jl:240
 [4] top-level scope
   @ C:\Users\Dennis Bal\.julia\packages\Cxx\1RaOv\deps\build.jl:23
 [5] include(fname::String)
   @ Base.MainInclude .\client.jl:451
 [6] top-level scope
   @ none:5
in expression starting at C:\Users\Dennis Bal\.julia\packages\Cxx\1RaOv\deps\build.jl:23
writing path.jl file
Tuning for julia installation at C:\Users\Dennis Bal\.julia\juliaup\julia-1.7.2+0~x64\bin with sources possibly at C:\Users\Dennis Bal\.julia\juliaup\
Enter fullscreen mode Exit fullscreen mode
Collapse
ifihan profile image
Ifihan Olusheye Author

Hi!

You can ask on the Slack workspace or Discourse

Collapse
logankilpatrick profile image
Logan Kilpatrick • Edited on

Another great article @ifihan. One quick suggestion, if you do

` ` `Julia
Enter fullscreen mode Exit fullscreen mode

for your code snippets, it will do syntax highlighting for them (like on GitHub).

Collapse
ifihan profile image
Ifihan Olusheye Author

Thank you very much. I just fixed that