Calling Fortran directly

Coordinator
Dec 21, 2011 at 2:59 PM

It is possible to call Fortran routines directly. The key to doing this is the FP data type. Below is a call to the Cholesky decompostion from http://xlllapack.codeplex.com. Note that it does not allocate any memory, it just uses the array passed in by Excel. Excel arrays are row major unlike Fortran arrays, so the 'L' means upper ('U') in this case.

#include <mkl_lapack.h>
#include "xll/xll.h"

using namespace xll;
using namespace xll;

static AddInX xai_potrf(
  "?xll_potrf", XLL_FP XLL_FP, 
  _T("POTRF"), _T("Matrix"), 
  "LAPACK", "Computes the upper Cholesky factorization of a symmetric (Hermitian) positive-definite matrix."
);

_FP* WINAPI
xll_potrf(_FP* pa)
{
#pragma XLLEXPORT
	static char u('L');

	try {
		ensure (pa->rows == pa->columns);

		int n = pa->rows;
		int info;

		DPOTRF(&u, &n, pa->array, &n, &info);

		ensure (info == 0);

		// fill in lower portion with zeros
		for (xword i = 1; i < n; ++i)
			for (xword j = 0; j < i; ++j)
				index(*pa, i, j) = 0;
	}
	catch (const std::exception& ex) {
		XLL_ERROR(ex.what());

		return 0;
	}

	return pa;
}