Skip to content

Commit 8387261

Browse files
committed
Polishing touches
1 parent 967d26d commit 8387261

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

docs/src/lecture_04/interaction.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
# Interaction with other languages
22

3-
One of the most significant advantages of Julia is its speed. As we discussed in the section [Why julia?](@ref Why-julia?), Julia is fast out-of-box without the necessity to do any additional steps. As a result, Julia solves the so-called Two-Language problem:
3+
One of the most significant advantages of Julia is its speed. As we discussed in the [introduction](@ref Why-julia?), Julia is natively fast, and no additional speed-up modifications are required. Therefore, Julia solves the two-language problem:
44

55
> Users are programming in a high-level language such as R and Python, but the performance-critical parts of the code have to be rewritten in C/Fortran for performance.
66
7-
Since Julia is fast enough, most of the libraries are written in pure Julia, and there is no need to use C/Fortran for performance. However, there are many high-quality, mature libraries for numerical computing already written in C and Fortran. It would be a shame if it will not be possible to use them in Julia.
7+
Thus, most Julia libraries are written purely in Julia without using C or Fortran. However, many high-quality libraries for numerical computing are already written in C and Fortran. To use them, Julia makes it simple and efficient to call C and Fortran functions.
88

9-
To allow easy use of this existing code, Julia makes it simple and efficient to call C and Fortran functions. Julia has a **no boilerplate** philosophy: functions can be called directly from Julia without any glue code generation or compilation – even from the interactive prompt. This is accomplished just by making an appropriate call with the `ccall` syntax, which looks like an ordinary function call. Moreover, it is possible to pass Julia functions to native C functions that accept function pointer arguments. This section will show one example of the interaction between Julia and C. Extensive description of all provided functionality can be found in the [official manual](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/).
9+
Julia follows the **no boilerplate** philosophy: functions can be called directly from Julia without any glue code generation or compilation – even from the interactive prompt. This is accomplished by making calls with the `ccall` syntax, which looks like an ordinary function call. It is also possible to pass Julia functions to native C functions that accept function pointer arguments.
1010

11-
The following example is taken from the official manual. Consider the situation that we want to use the `qsort` function from the standard C library. The `qsort` function sorts an array and is declared as follows.
11+
This section shows one example of the interaction between Julia and C. Extensive description of all provided functionality is in the [official manual](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/). Consider the situation where we want to use the `qsort` function from the standard C library. The `qsort` function sorts an array and is declared as follows.
1212

1313
```c
1414
void qsort(void *base, size_t nitems, size_t size,
1515
int (*compare)(const void*, const void*))
1616
```
1717
18-
The `base` is the pointer to the first element of the array to be sorted. The `nitems` is the number of elements in the array pointed by `base`. The `size` is the size in bytes of each element in the array. Finally, the `compare` is the function that compares two elements. The `compare` function should return a negative integer if the first argument is less than the second, a positive integer if the first argument is greater than the second, and otherwise zero. Such a Julia function can be defined as follows.
18+
The `base` is the pointer to the first element of the input array, `nitems` is the number of elements in this array, and `size` is the size in bytes of each element. Finally, `compare` is the function that compares two elements: it should return a negative integer if the first argument is smaller than the second, a positive integer if the first argument is greater than the second, and zero otherwise. Such a Julia function can be defined as follows.
1919
2020
```julia
2121
mycompare(a, b)::Cint = sign(a - b)
2222
```
2323

24-
Since the `qsort` function expects that the return type of the `compare` function is C `int`, we annotate the return type to be `Cint`. In order to pass this function to C, we obtain its address using the macro `@cfunction`.
24+
Since the `qsort` function expects that the return type of the `compare` function is C `int`, we annotate the return type to be `Cint`. To pass this function to C, we obtain its address using the `@cfunction` macro. It requires three arguments: the Julia function, its return type, and the tuple of its input argument types.
2525

2626
```julia
2727
mycompare_c = @cfunction(mycompare, Cint, (Ref{Cdouble}, Ref{Cdouble}))
2828
```
2929

30-
The `@cfunction` macro requires three arguments: the Julia function, the return type, and the tuple of the input argument types. Finally, we can use the `ccall` function to call the `qsort` function.
30+
Finally, we use the `ccall` function to call the `qsort` function.
3131

3232
```julia
3333
julia> A = [1.3, -2.7, 4.4, 3.1];
@@ -43,7 +43,7 @@ julia> A
4343
4.4
4444
```
4545

46-
Besides C and Fortran that can be called directly using `ccall` function, it is possible to interact with other languages using third-party packages. The following table shows an overview of those packages.
46+
Besides C and Fortran that can be called directly by the `ccall` function, it is possible to interact with other languages by third-party packages. The following table shows an overview of those packages.
4747

4848
| Language | Calling from Julia | Calling Julia |
4949
| :--- | :--- | :--- |
@@ -54,11 +54,11 @@ Besides C and Fortran that can be called directly using `ccall` function, it is
5454
| MATLAB | [MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl) | [Mex.jl](https://github.com/jebej/Mex.jl/) |
5555
| Java | [JavaCall.jl](https://github.com/JuliaInterop/JavaCall.jl) | [JuliaCaller](https://github.com/jbytecode/juliacaller) |
5656

57-
Moreover, other Julia packages provide Julia interface for some well-known libraries from other languages. As an example, we can mention [ScikitLear.jl](https://github.com/cstjean/ScikitLearn.jl), which provides an interface for the [scikit-learn](https://scikit-learn.org/stable/) library from Python or the [RDatasets.jl](https://github.com/JuliaStats/RDatasets.jls) that provides an easy way to load famous R datasets.
57+
Moreover, other Julia packages provide Julia interface for some well-known libraries. We can mention [ScikitLearn.jl](https://github.com/cstjean/ScikitLearn.jl), which provides an interface for the [scikit-learn](https://scikit-learn.org/stable/) library from Python or the [RDatasets.jl](https://github.com/JuliaStats/RDatasets.jl) that provides an easy way to load R datasets.
5858

5959
## RCall.jl
6060

61-
The [RCall.jl](https://github.com/JuliaInterop/RCall.jl) package provides an interface for calling R functions from Julia and passing data between these two languages. The package provides an interactive REPL for the R language that can be accessed from the Julia REPL by typing the `$` symbol. Consequently, it is possible to easily switch between these languages and use functionality provided by both languages simultaneously.
61+
The [RCall.jl](https://github.com/JuliaInterop/RCall.jl) package provides an interface for calling R functions from Julia and passing data between these two languages. The package provides an interactive REPL for the R language that can be accessed from the Julia REPL by the `$` symbol. Consequently, it is possible to switch between these languages and simultaneously use functionalities provided by both languages.
6262

6363
```julia
6464
julia> using RCall, RDatasets
@@ -84,11 +84,11 @@ ggplot($mtcars, aes(x = WT, y = MPG)) + geom_point()
8484
"""
8585
```
8686

87-
Note that we use multiline string syntax, but it is also possible to use standard string syntax. This multiline string syntax is very useful, especially when we want to perform multiple operations in R at once and then just return the result to Julia.
87+
We used multi-line string syntax, which is helpful to perform multiple operations in R at once.
8888

8989
## MATLAB.jl
9090

91-
The [MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl) provides an easy interface for calling Matlab functions and passing data between Julia and Matlab. Consider the situation that you wrote a Matlab function that uses some special functionality that is not available in Julia. MATLAB.jl package provides an interface to call this function directly from Julia, as can be seen in the following example.
91+
[MATLAB.jl](https://github.com/JuliaInterop/MATLAB.jl) provides an easy interface for calling Matlab functions and passing data between Julia and Matlab. Consider the situation that you wrote some Matlab code that is not available in Julia. The MATLAB.jl package provides an interface to call this function directly from Julia.
9292

9393
```julia
9494
using MATLAB, BSON
@@ -97,7 +97,7 @@ X = BSON.load("data.bson")[:X]
9797
mxcall(:MakeVideo, 0, X, "video.gif")
9898
```
9999

100-
The `mxcall` function accepts the name of the function as the first argument and the number of the output variables of that function as the second argument. All other inputs to the `mxcall` function are the input arguments of the Matlab function. The result is the following animation.
100+
The `mxcall` function accepts the name of the function as the first argument and the number of its output as the second argument. All other inputs to the `mxcall` function are the input arguments of the Matlab function. The result is the following animation.
101101

102102
![](../data/Video.gif)
103103

0 commit comments

Comments
 (0)