.m Function to .oct File – Syntax Translation Table


One of the things Octave is superior to Matlab is Octave’s C++ code interface – Oct-Files. With Oct-Files, you can use a similar syntax in C++ as in the m language, which is much more convenient than the Matlab’s .mex extension. When your .m file is slow than you can bear, you’d better try to port it to .oct for a performance boost. However, there is not enough documents about Oct-Files. Here I just want to share a simple table for translating .m function to C++ for dynamic link .oct file.
The following note collects some commonly used syntax based on my own experience.

Function Define:

%matlab
function out = function_name(args)
%get input
args1=args(1);
args2=args(2);
...
%return value
out=args1;
end
//Cpp
//To call the function: out = function_name(in);
#include<octave/oct.h>
DEFUN_DLD (function_name, args, nargout, "function description")
{
//get input length
int nargin = args.length();
//get scalar input
double args0=args(0).scalar_value();
double args1=args(1).scalar_value();
/*
//get matrix input
Matrix y1(args(0).matrix_value());
Matrix y2(args(1).matrix_value());
*/
...
//return value, use octave_value_list to return an array
//see: https://www.gnu.org/software/octave/doc/interpreter/Character-Strings-in-Oct_002dFiles.html#Character-Strings-in-Oct_002dFiles
return octave_value(args0);
}

Matrix Define:

%Matlab
myMx = zeros(2,4);
//Cpp
dim_vector dv(2);
dv(0) = 2; dv(1) = 4;
Matrix myMx(dv);//myMx.fill(0.0);

or

Matrix myMx(2, 4);

Get Matrix Dimensions:

%Matlab
dv = size(myMx);
//Cpp
dim_vector dv=myMx.dims();
//nrows = dv(0);//or myMx.rows();
//ncols = dv(1);//or myMx.cols();

Set/Get Matrix Elements:

%Matlab
myMx(1,2) = 1;
//Cpp
myMx(0,1) = 1;

Matrix Operations:

%Matlab
Mx_c = Mx_a+Mx_b;
Mx_c = Mx_a-Mx_b;
Mx_c = Mx_a * Mx_b;
Mx_c = Mx_a / Mx_b;
Mx_at = Mx_a';
//Cpp
Matrix Mx_c = Mx_a + Mx_b;
Matrix Mx_c = Mx_a - Mx_b;
Matrix Mx_c = Mx_a * Mx_b;
Matrix Mx_c = Mx_a * Mx_b.inverse();
Matrix Mx_at = Mx_a.transpose();

Get Matrix Rows/Columns:

%Matlab
Mx_j = Mx(j,:);
Mx_k = Mx(:,k);
//Cpp
RowVector Mx_j=Mx.row(j-1);
ColumnVector Mx_j=Mx.column(k-1);

Row Sums:

%Matlab
y1sum = sum(y1);
//Cpp
RowVector y1sum = (y1.sum()).row(0);

Matrices Combination:

//Cpp
int rows, columns, columns2;
// initialize row and column values to something
Matrix A(rows,columns);
Matrix B(rows,columns2);
// initialize the contents of A and B to something
A.resize(rows,columns+columns2);
A.insert(B, 0, columns);

Call Matlab Function:

%Matlab
stat = 0;
ipv = norcdf(stat);
disp(ipv);
//Cpp
#include<octave/parse.h>
double stat = 0.0;
octave_value_list ipv =  feval ("normcdf", octave_value(stat), nargout);
feval ("disp", octave_value(ipv(0).scalar_value()), nargout);

Self-Defined Functions:

%Matlab
tr = trace(Mx_a);
sm = sum(sum(Mx_a));
//Cpp
double trace(Matrix mx)
{
	double value = 0.0;
	dim_vector dv = mx.dims();
	int p = dv(0);
	for(int i=0;i<p;i++)//starts from 0 here!
	{
		value += mx(i,i);
	}
	return value;
}
double sumall(Matrix mx)
{
	double value = 0.0;
	dim_vector dv = mx.dims();
	int pr = dv(0);
	int pc = dv(1);
	for (int ir1 = 0 ; ir1 < pr ; ir1++)
		for (int ic1 = 0 ; ic1 < pc ; ic1++)
		{
			value+=mx(ir1,ic1);
		}
		return value;
}
double tr = trace(A);
double sm = sumall(A);

To compile the cpp source file:

cd('D:\Octave\yourtestcodefolder');
mkoctfile octtest.cc;

Other Things to Note:
You can check the Cpp files folder (e.g.,”D:\Octave\Octave3.6.4_gcc4.6.2\include\octave-3.6.4\octave”) for more Octave .oct APIs.
C++ index starts from 0.
Integer divided by integer is integer in Cpp by default.

Source Code:
https://bzstat.googlecode.com/svn/trunk/blogpost/OctTest/octtest.cc

Some useful links:
http://wiki.octave.org/Tips_and_tricks#C.2B.2B (Octave C++ Quick Start)
http://octave.sourceforge.net/coda/c58.html
http://www.chemie.fu-berlin.de/chemnet/use/info/octave/liboctave_5.html#SEC10
!http://score.ime.usp.br/mm/homework/oct_cook.html#matrix
!http://www.gnu.org/software/octave/doc/interpreter/Oct_002dFiles.html#Oct_002dFiles
http://octave.sourceforge.net/doxygen/html/annotated.html

http://octave.1599824.n4.nabble.com/Tutorial-on-Oct-files-td1617194.html
http://octave.1599824.n4.nabble.com/vec-and-trace-in-the-API-td4660473.html

http://www.mathias-michel.de/download/howto-octave-c++.pdf

GNU Octave Beginner’s Guide

Gnu Octave Version 3.0.1 Manual: A High-Level Interactive Language For Numerical Computations

Advertisements
This entry was posted in matlab/octave, programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s