See Part I for instructions on basic arithmetic operations and Part II for working with matrices
Using R and Python code in Julia
There can be times where you want to use your R or Python code as the libraries to perform certain operations may not be available in Julia.
However, using 2 languages to perform a certain operation can be a challenge. Hence, Julia provides an option to use R and Python code by using the “PyCall” and “RCall” packages.
How to use Python code in Julia
Here’s how you can use your Python code in Julia.
# Install the "PyCall" package for Python
using Pkg
Pkg.add("PyCall")
using PyCall
## Importing numpy package from Python
np = pyimport("numpy")
## Using numpy package in Julia
np.array([1,2,3])
Output:
3-element Vector{Int64}:
1
2
3
How to use R code in Julia
Let’s look at an example of using R in Julia.
# Install "RCall" package for R
Pkg.add("RCall")
using RCall
# There are several ways to call R code
# Method 1
# Calling the mean() function from R
rcall(:mean, [1,2,3])
# Method 2 for user-defined functions
R"""
power_4 <- function(x)
{
return(x^4)
}"""
## Calling the defined function
rcall(:power_4, 5)
Output:
RObject{RealSxp}
[1] 2
RObject{RealSxp}
[1] 625
For further details on using the RCall package, you can refer here.
Python vs Julia – Comparison of syntax for basic operations
Feature | Python | Julia |
Column vector | [1, 2, 3] | [1,2,3] |
Row vector | [[1], [2], [3]] | [1 2 3] |
Sequence of numbers | range(1, 10,2) | [1:2:10; ] |
Matrix | [[1,2] , [3,4]] | [1 2; 3 4] |
Random numbers | np.random.rand(4,5) | rand(4,5) |
Mutation | inplace =True | ! |
Matrix to vector | A.flatten() | A[:] |
Conjugate of a matrix | A.conj() | A’ |
Element wise operation | map() | . |
Power | var**2 | var^2 |
Indexing | Starts from 0 | Starts from 1 |
Maximum value | max(A) | maximum(A) |
Minimum value | min(A) | minimum(A) |
For loop | for i in range(n): # command | for i in 1:n # command end |
While loop | While i <=n: | While i <=n |
If-else | If i <=m: # command Else: #command | if i <=m # command else # command end |
Temporary function | Y = lambda x: x**5 | Y = x -> x^5 |
Function | def func(y, x): return y + 3*x | func(y, x) = y + 3x OR function func(y, x) return y + 3x end |
Visit QuantInsti for additional insight on this article: https://blog.quantinsti.com/julia-syntax/.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.