Implementation of Octave functions in Julia

Hello everyone,

I wonder if there is a way to implement Matlab functions via Octave in Julia. I know there exists Matlab.jl but for using it you need to install a Matlab version in your computer.

I ask this question because I want to use a library for robotic learning only available as Matlab/Octave. The idea is to implement it as completely free open-source code without the need of reimplementations.

I will be very grateful if you could help me :slight_smile:

2 Likes

Maybe this question is too obvious, but… if you wan to use a library available for Matlab/Octave in a “completely free open-source code without the need of reimplementations”, why don’t just use Octave?

Octave is completely free open-source, and you woudn’t need to reimplement anything. I’m pretty sure that using the library directly in Octave will work far better than trying to use Julia or any other engine with Octave code, except maybe Matlab.

EDIT: After re-reading, I understand your point better: You want to use the Octave functions as part of your study, but the rest you want to do it in Julia, right?

If that’s the case, I don’t know of a straightforward solution. There have been some attempts in the past, e.g. MatlabCompat.jl, but I don’t know of any one which has not been abandoned. If you say what’s the library you want to use, maybe someone can tell if there is a convenient alternative for Julia.

2 Likes

Hi @heliosdrm, thanks for your answer.

Indeed, I would like to build a benchmark in Julia. I already can integrate libraries from python and my own one in Julia. However, sadly some labs still only offer libraries developed for Matlab, which decreases their reachability to all the community.

I have already taken a look at MatlabCompat.jl, but it seems that the authors stop their maintenance in Julia 0.4.

So, I hope there are other solutions to integrate octave instead of just Matlab.

https://github.com/JuliaInterop/MATLAB.jl ?

Hi @dpsanders thanks for your answer.

The problem is that you can use this package without having a MATLAB license. That is why I am looking for integration, that in case I need to download it I know that is free open-source software :slight_smile:

Ah you’re right, sorry about that.

This
https://julia-users.narkive.com/M85LtrGP/running-octave-scrips-from-julia

suggests that you may be able to use oct2py and PyCall.
(It wasn’t working then but maybe is now?)

1 Like

A better answer would be “rewrite it in Julia” since you will gain performance and generality.

1 Like

And are you aware that Julia has sophisticated packaged for robotics? https://juliarobotics.org/

1 Like

Thanks, I will take a look at oct2py. And thanks for suggesting me the robotics package. Maybe, I will be able to contribute to it in the future :wink:

[Note also the MATLAB to Julia translator | MATLAB to Julia converter translator that was helpful (but not complete) for me for my first professional Julia project (migrating from MATLAB). Getting oct2py to work at the time would have helped, while I just called/used MATLAB (Octave wouldn’t have fully worked because of Toolbox code, I had to find substitutes for).]

I could (indirectly) call Octave from Julia, using oct2py, but not even, at first, directly from Python (or R).

julia> using PyCall

julia> o = pyimport("oct2py").Oct2Py();

julia> A = o.hw(1)  # or as strict as Julia would have been about types: o.hw([1 2; 3 4]);
Hello world!
2×2 Array{Float64,2}:
 2.0  3.0
 4.0  5.0

shell> less hw.m

function result = hw(A)    
  disp("Hello world!");
  result = A + [1 2; 3 4];
end

Be sure to first to set-up PyCall.jl and:

$ sudo apt-get install octave
$ pip3 install oct2py

DO NOT use the octave snap (maybe it’s 32-bit, as the julia snap, that I also had a problem with, when using with e.g. PyCall, for that reason, conflicting with 64-bit Python), then I couldn’t even call Octave from Python, and note pip3 unlike using pip as documented, as PyCall uses Python3 by default.

Since Julia and Octave (and R) are row-major, but Python is column-major [EDIT: other way around, either would have been a potential problem], I wasn’t sure if this was as fast as calling R to call Octave, but I got errors trying that way (more trouble than it was worth to me):

julia> R"""install.packages("RcppOctave")"""
┌ Warning: RCall.jl: Installing package into ‘/home/pharaldsson_sym/R/x86_64-pc-linux-gnu-library/3.6’
│ (as ‘lib’ is unspecified)
│ Warning: package ‘RcppOctave’ is not available (for R version 3.6.3)
└ @ RCall ~/.julia/packages/RCall/g7dhB/src/io.jl:113

and even with finding package in archive (there seems to be a reason it was moved there, but using it could work for you, at least with older version of R installed?):

$ R CMD INSTALL ../Downloads/RcppOctave_0.18.1.tar.gz
* installing to library ‘/home/pharaldsson_sym/R/x86_64-pc-linux-gnu-library/3.6’
ERROR: dependency ‘pkgmaker’ is not available for package ‘RcppOctave’
* removing ‘/home/pharaldsson_sym/R/x86_64-pc-linux-gnu-library/3.6/RcppOctave’

One other option I didn’t look into, it’s possible to call Octave from Sage, and I saw:

Julia is not shipped with SageMath, but it is installed on SageMathCloud at http://cloud.sagemath.com.

2 Likes

Isn’t this the other way around?

5 Likes

Yes: Julia, Fortran, Matlab, Octave and R are all column-major while Python and C are row-major.

3 Likes

It may be helpful to add, you may need to use with Oct2Py:
ENV[“OPENBLAS_NUM_THREADS”]=“1” # https://github.com/xianyi/OpenBLAS/wiki/faq#multi-threaded

Oct2Py worked for me until I got “Session died” (with no line number), and Julia has threading enabled, calling Python and then Octave, but it calling OpenBLAS didn’t handle threads 8for me the inv function).

Also, it might be helpful to use Octave 4.4+ (how?) as while older you get in Ubuntu (18.04), you need that version to pkg install URL in Octave.

I tried both snap and flatpak, and I can get the newer Octave installed, but unclear how to make it work with Oct2Py. You may need:

ENV[“OCTAVE_EXECUTABLE”]= path

flatpak override --filesystem=/tmp org.octave.Octave

This is a bit tangential but just leaving it here. I always wanted to every once in a while try a function in Octave to compare results/implementations/etc. You can now install Octave via BinaryBuilder and start it from within Julia 1.11:

julia> using Octave_jll, OpenBLAS32_jll

julia> withenv("LBT_DEFAULT_LIBS"=>OpenBLAS32_jll.libopenblas_path, "OCTAVE_HOME"=>Octave_jll.artifact_dir) do
           run(Octave_jll.octave_cli())
       end
GNU Octave, version 9.3.0
Copyright (C) 1993-2024 The Octave Project Developers.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  For details, type 'warranty'.

Octave was configured for "aarch64-apple-darwin20".

Home page:            https://octave.org
Support resources:    https://octave.org/support
Improve Octave:       https://octave.org/get-involved

For changes from previous versions, type 'news'.

octave:1> 
10 Likes

Thanks, that looks very cool … but what are we supposed to be able to do with? Does it mean we can exchange variables between Octave and Julia? Apparently not.

octave:1> r = rand(3)
r =

   0.3109   0.9319   0.6959
   0.3175   0.7726   0.9173
   0.2069   0.6140   0.9856

octave:2> typeof(r)
error: 'typeof' undefined near line 1, column 1
octave:3> �H�H

last line shows happens when I tried the arrow key to bring the last typed command (Windows here).

EDIT: I also see that the CPU usage raises to a permanent >20% (in 20 cores machine) when using octave (but not running any command).

No, you have to execute it as a library to share memory and data directly, as opposed to executing as a separate octave process. With a separate process, you can only share data through a file or similar indirect channels.

I once started playing with an OctCall.jll wrapper that let you call liboctave directly, in the same way that PyCall.jl let you call libpython directly. However, a huge pain point is that the Octave API is based on C++. My initial wrappers used Cxx.jl to access it, but that (technically ambitious) package was abandoned. If you wanted to revive it you would need something like CxxWrap.jl, but it is rather painful because of the size and complexity of the Octave API.

3 Likes

Thanks, suspected something like this.
Maybe some day we’ll have an new operator +(C++, --)

1 Like