/TensorLab.jl

Port of the Tensorlab MATLAB package to Julia

Primary LanguageJuliaMIT LicenseMIT

TensorLab.jl

This package contains Julia bindings for the the Tensorlab library in MATLAB.

You must have MATLAB installed with Tensorlab in its default path, e.g. in pathdef.m.

Installation

julia> # Type the ] character
pkg> add https://github.com/Nikdwal/TensorLab.jl

Getting started

The main functions implemented in Tensorlab can be accessed as a Julia function with the same name. The following Julia code is used to compute the CPD of a 5x5x5 tensor.

A = randn(5,5,5);
options = Options();
options.Display          = true;
options.Initialization   = MatFcn("cpd_rnd");
options.Algorithm        = MatFcn("cpd_als");
options.AlgorithmOptions = Options();
options.AlgorithmOptions.LineSearch = MatFcn("cpd_els");
options.AlgorithmOptions.TolFun     = 1e-12;
options.AlgorithmOptions.TolX       = 1e-12;
Uhat = cpd(A, 10, options)

Or we could have just used cpd(A, 10) without the options, as in Tensorlab. The original MATLAB code is the following.

A = randn(5,5,5);
options.Display = true; 
options.Initialization = @cpd_rnd; 
options.Algorithm = @cpd_als; 
options.AlgorithmOptions.LineSearch = @cpd_els; 
options.AlgorithmOptions.TolFun = 1e-12; 
options.AlgorithmOptions.TolX   = 1e-12; 
Uhat = cpd(A,10,options);

Notes on interoperability with MATLAB

  • Function handles are encoded in the MatFcn type. Thus, whenever you wish to pass a function handle for the MATLAB function f to Tensorlab from Julia, pass it as MatFcn("f"). This is equivalent to @f in MATLAB.
  • The Options object is the proper way to pass a struct with options to Tensorlab. First, initialise an object as options = Options(). From then onwards, you can add fields as you would in MATLAB. If one of those fields is another Options objects, it must be explicitly initialised as such. See the example above.
  • MATLAB code (including Tensorlab) is not type stable, which is one of the reasons it performs poorly. If you want the Julia code that uses results from Tensorlab to achieve high performance, you must give an explicit type declaration to values returned by Tensorlab. In general, this can only be done if you know what values are passed to the library functions, rather than merely what the types of these values are. As a result, ensuring type stability (performance) by explicit type declaration is the user's responsibility.

If any functionality is missing, you may alternatively use the MATLAB.jl package and write the MATLAB code yourself within a Julia environment.