euraad
euraad

Reputation: 2846

Call GNU Octave functions in C?

I want to use matrix algebra and optimization. I have tested different C and C++ libraries for matrix algebra but the problem with those is they cannot handle garbage data as good as GNU Octave does. Garbage data in C and C++ goes low to like e-8 but in GNU Octave it will be pushed down way to low as e-17. That's very useful if you planning to use garbage data from e.g measurement in calculations. They don't effect nothing of your results.

But GNU Octave have a C++ API, which I don't really understand how to use. But I want to use C and call GNU Octave functions from C.

Is that possible that I can create a struct that contains a 2D array and dimensions, and send it to GNU Octave and I will return a struct again that have the result and the dimension e.g solution.

Upvotes: 2

Views: 2758

Answers (1)

rahnema1
rahnema1

Reputation: 15837

There is a c mex interface. However the octave interpreter must be embedded and initialized before any mex function can be called. As of Octave 4.4 octave_main as suggested by the linked answer has been deprecated and some other changes also are needed for it to be useful for mex programs. So I have prepared a c++ source file calloctave.cc containing the functions mexCallOctave and free_arg_list and its header calloctave.h.

calloctave.cc

// calloctave.cc

#include "interpreter.h"
#include "mxarray.h"
#include "parse.h"

extern "C"
int
mexCallOctave (int nargout, mxArray *argout[], int nargin,
               mxArray *argin[], const char *fname)
{

  static octave::interpreter embedded_interpreter;
  if (!embedded_interpreter.initialized())
    embedded_interpreter.execute ();

  octave_value_list args;

  args.resize (nargin);

  for (int i = 0; i < nargin; i++)
    args(i) = mxArray::as_octave_value (argin[i]);

  bool execution_error = false;

  octave_value_list retval;


  retval = octave::feval (fname, args, nargout);

  int num_to_copy = retval.length ();

  if (nargout < retval.length ())
    num_to_copy = nargout;

  for (int i = 0; i < num_to_copy; i++)
    {
      argout[i] = new mxArray (retval(i));
    }

  while (num_to_copy < nargout)
    argout[num_to_copy++] = nullptr;

  return execution_error ? 1 : 0;
}

extern "C"
void 
free_arg_list (int nargs, mxArray* arglist[])
{
    for(int i = 0; i < nargs; i++)
            delete arglist[i];
}

calloctave.h

// calloctave.h
#pragma once
#include "mex.h"

#if defined  (__cplusplus)
extern "C" {
#endif

int
mexCallOctave (int nargout, mxArray *argout[], int nargin,
               mxArray *argin[], const char *fname);
void 
free_arg_list (int nargs, mxArray* arglist[]);

#if defined  (__cplusplus)
}
#endif

Here is a basic introduction into mex files. You can compile an example hello world program adding the option --verbose as mkoctfile --mex --verbose hello.c to get the list of compiler options that you need to use them for compilation of your actual programs. Note that because calloctave.cc is a c++ source it should be compiled using a c++ compiler such as g++. In the following example a m function "myfunction" is called. It gets one input and produces one output. mexCallOctave is used for calling the octave function and it has the same signature as mexCallMATLAB.

myfunction.m

% myfunction.m
function out=  myfunction( a )
    out = sum(a);
endfunction

main.c

//main.c
#include <stdio.h>
#include "calloctave.h"   
int main()
{
    double input_data[] = {0,1,2,3,4,5,6,7,8,9,10};

    const int nargin = 1;
    const int nargout = 1;
    mxArray* rhs[nargin];
    mxArray* lhs[nargout];

    // allocate mex array
    rhs[0] = mxCreateDoubleMatrix( 10, 1, mxREAL);
    double* rhs_ptr = mxGetPr( rhs[0] );

    // copy data from input buffer to mex array
    for (int i = 0 ; i < 10; i++)
        rhs_ptr[i] = input_data[i];

    // call octave function
    mexCallOctave(nargout, lhs, nargin, rhs, "myfunction");

    double* lhs_ptr = mxGetPr( lhs[0] );

    double output_data = *lhs_ptr;

    // show the result
    printf ("result = %f", output_data);

    // free memory

    mxDestroyArray(rhs[0]);
    free_arg_list(nargout, lhs);
}

Upvotes: 5

Related Questions