See discussions, stats, and author profiles for this publication at: https://www.researchgate.
net/publication/290434658
Calling octave functions from c++
Technical Report · February 2016
DOI: 10.13140/RG.2.1.2146.8240
CITATIONS                                                                                              READS
0                                                                                                      5,061
1 author:
            Josimar Chire
            University of São Paulo
            56 PUBLICATIONS 65 CITATIONS
                SEE PROFILE
Some of the authors of this publication are also working on these related projects:
              Quantum Inspired Evolutionary Algorithm for Large Scale Optimization View project
              Analyzing QIEA-R model View project
 All content following this page was uploaded by Josimar Chire on 14 January 2016.
 The user has requested enhancement of the downloaded file.
           Calling octave functions from c++
                      Josimar Edinson Chire Saire
                          jecs89@gmail.com
                             January 14, 2016
                                  Abstract
   Many experiments are done to test ideas related to engineering, sciences
and most of the people uses Matlab and works on Windows. Octave is a pro-
posal similar to Matlab for Linux, and it does not have a tool for exportation.
Then I was looking for how to call Octave functions from c++ and I want to
share how to do it.
Contents
1 Introduction       2
2 How to             2
3 Results            2
                 1
Josimar Edinson Chire Saire                                       Learning Everyday
jecs89@gmail.com                                                   January 14, 2016
1    Introduction
Octave is a proposal made for Linux, the syntax is similar to Matlab. Useful to test
ideas with no need to code it in c++, although it is slower than c++, sometimes
functionality is better than performance. If you have used some specific function
from statistics, signal or others libraries you will need time to code or look for this
function in c++ . Therefore, if you can use Octave functions in your c++ code it
will save time.
2    How to
If you want to try you can install Octave from terminal:
sudo apt-get install octave
   I was looking for and I found solution here:
http://comments.gmane.org/gmane.comp.gnu.octave.general/48618
After I tried to understand better about Makefile using:
http://mrbook.org/blog/tutorials/make/
   Here is the Makefile that I used:
COMMON_CXX = -Wall -Wextra -std=c++11 -O4
OCT_CXX = ‘mkoctfile -p CXXFLAGS‘ $(COMMON_CXX)
all:
CXXFLAGS="$(OCT_CXX)" \
mkoctfile --link-stand-alone embedded.cpp -o embedded
3    Results
I tried with example from Octave:
#include   <iostream>
#include   <octave/oct.h>
#include   <octave/octave.h>
#include   <octave/parse.h>
#include   <octave/toplev.h>
using namespace std;
int main (void)
{
  string_vector argv (2);
                                          2
Josimar Edinson Chire Saire                                    Learning Everyday
jecs89@gmail.com                                                January 14, 2016
    argv(0) = "embedded";
    argv(1) = "-q";
    octave_main (2, argv.c_str_vec (), 1);
    octave_idx_type n = 2;
    octave_value_list in;
    for (octave_idx_type i = 0; i < n; i++)
      in(i) = octave_value (5 * (i + 2));
    octave_value_list out = feval ("gcd", in, 1);
    if (! error_state && out.length () > 0)
      std::cout << "GCD of ["
                << in(0).int_value ()
                << ", "
                << in(1).int_value ()
                << "] is " << out(0).int_value ()
                << std::endl;
    else
      std::cout << "invalid\n";
    clean_up_and_exit (0);
}
                          Figure 1: Octave function call
    These warnings are for some troubles with octave libraries that I had. I solved
it reinstalling Octave.
    But I wanted to use some functionality from c++11 and It worked:
#include   <iostream>
#include   <octave/oct.h>
#include   <octave/octave.h>
#include   <octave/parse.h>
#include   <octave/toplev.h>
                                        3
                         Josimar Edinson Chire Saire                                 Learning Everyday
                         jecs89@gmail.com                                             January 14, 2016
                         #include <random>
                         using namespace std;
                         int main (void)
                         {
                             default_random_engine rng( random_device{}() );
                             uniform_real_distribution<double> dist( 0, 35 );
                             cout << "HHHHHHHHHHH      " << dist(rng) << endl;
                             string_vector argv (2);
                             argv(0) = "embedded";
                             argv(1) = "-q";
                             octave_main (2, argv.c_str_vec (), 1);
                             octave_idx_type n = 2;
                             octave_value_list in;
                             for (octave_idx_type i = 0; i < n; i++)
                               in(i) = octave_value (5 * (i + 2));
                             octave_value_list out = feval ("gcd", in, 1);
                             if (! error_state && out.length () > 0)
                               std::cout << "GCD of ["
                                         << in(0).int_value ()
                                         << ", "
                                         << in(1).int_value ()
                                         << "] is " << out(0).int_value ()
                                         << std::endl;
                             else
                               std::cout << "invalid\n";
                             clean_up_and_exit (0);
                         }
                                     Figure 2: Octave function call and c++11 function call
View publication stats