.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:

function out = function_name(args)
%get input
%return value
//To call the function: out = function_name(in);
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:

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


Matrix myMx(2, 4);

Get Matrix Dimensions:

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

Set/Get Matrix Elements:

myMx(1,2) = 1;
myMx(0,1) = 1;

Matrix Operations:

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';
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:

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

Row Sums:

y1sum = sum(y1);
RowVector y1sum = (y1.sum()).row(0);

Matrices Combination:

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.insert(B, 0, columns);

Call Matlab Function:

stat = 0;
ipv = norcdf(stat);
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:

tr = trace(Mx_a);
sm = sum(sum(Mx_a));
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++)
		return value;
double tr = trace(A);
double sm = sumall(A);

To compile the cpp source file:

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:

Some useful links:
http://wiki.octave.org/Tips_and_tricks#C.2B.2B (Octave C++ Quick Start)



GNU Octave Beginner’s Guide

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

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