12.07.2015 Views

Excel Add-in Development in C/C++: Applications in ... - F9

Excel Add-in Development in C/C++: Applications in ... - F9

Excel Add-in Development in C/C++: Applications in ... - F9

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example <strong>Add</strong>-<strong>in</strong>s and F<strong>in</strong>ancial <strong>Applications</strong> 351}r2 * = TWO_PI;sample_zero = r1 * cos(r2);return r1 * s<strong>in</strong>(r2);Both the above functions perform the same task but <strong>in</strong> very different ways. The first cantake static or volatile <strong>in</strong>puts and always returns a pair of samples. The second returns as<strong>in</strong>gle sample but is volatile. This gives the spreadsheet developer less control than withthe first. It would be possible to modify the second so that it took a trigger argument,which would then obviate the need for it to be declared as volatile.It is a straightforward exercise to generalise the Box-Muller functions above to, optionally,generate samples us<strong>in</strong>g the more efficient polar rejection method. (See Clewlow andStrickland (1998) for details.)10.3 MATRIX FUNCTIONS – EIGENVALUESAND EIGENVECTORS<strong>Excel</strong> has a number of useful matrix rout<strong>in</strong>es, <strong>in</strong> particular MMULT(), MINVERSE(),MDETERM(), TRANSPOSE() and SUMPRODUCT(). As well as this, the way that <strong>Excel</strong> treatsrange references <strong>in</strong> array formulae greatly extends its matrix capabilities. Nevertheless,there are a number of matrix operations which, though not as fundamental as these, arevaluable for those analys<strong>in</strong>g l<strong>in</strong>ear systems. Perhaps the most useful is the calculation ofeigenvectors and eigenvalues. The follow<strong>in</strong>g example function takes a square symmetric(real) N×N matrix and returns an N×(N + 1) array conta<strong>in</strong><strong>in</strong>g the eigenvectors andeigenvalues. The code is conta<strong>in</strong>ed <strong>in</strong> the CD ROM and is based on the Jacobi algorithmpublished <strong>in</strong> section 11.1 of Numerical Recipes <strong>in</strong> <strong>C++</strong>. (The code for the Jacobi algorithmitself is omitted from the XllMatrix.cpp source code module <strong>in</strong> the exampleproject on the CD ROM. However, it can easily be <strong>in</strong>serted <strong>in</strong>to one of the memberfunctions of the class, d_matrix. Seetheread me file on the CD ROM for details.)The <strong>in</strong>tention here is not to provide a comprehensive set of functions that will attemptto f<strong>in</strong>d the eigenvectors and values of any matrix. As NRC expla<strong>in</strong>s very well, this isa complex subject. The <strong>in</strong>tention of this example is to show how to bridge from <strong>Excel</strong>ranges to C/<strong>C++</strong> matrices <strong>in</strong> a safe and efficient way.Function nameDescriptioneigen_system (exported)EigenSystem (registered with <strong>Excel</strong>)Takes a square symmetrical range, or array, conta<strong>in</strong><strong>in</strong>g onlynumbers. Returns a square matrix whose columns are theeigenvectors of the <strong>in</strong>put matrix, with an extra row at the bottomconta<strong>in</strong><strong>in</strong>g the correspond<strong>in</strong>g eigenvalues. Output is sorted <strong>in</strong>descend<strong>in</strong>g size of eigenvalue from left to right.Prototype xloper * __stdcall eigen_system(xl_array *);Type str<strong>in</strong>g"RK"

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!