Using Excel, Visual Basic and NAG's Numerical Libraries â getting ...
Using Excel, Visual Basic and NAG's Numerical Libraries â getting ...
Using Excel, Visual Basic and NAG's Numerical Libraries â getting ...
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Using</strong> <strong>Excel</strong>, <strong>Visual</strong> <strong>Basic</strong> <strong>and</strong><br />
NAG’s <strong>Numerical</strong> <strong>Libraries</strong> – <strong>getting</strong> started<br />
Marcin Krzysztofik<br />
NAG Financial Mathematics Day<br />
Manchester Business School<br />
2 nd February 2011<br />
Experts in numerical algorithms<br />
<strong>and</strong> HPC services
Agenda<br />
• <strong>Using</strong> NAG Library in <strong>Excel</strong><br />
• Shall I use NAG C Library or NAG Fortran Library<br />
• Examples<br />
2
Why NAG & <strong>Excel</strong><br />
• Microsoft Office <strong>Excel</strong> is the most widely used<br />
spreadsheet application in the world.<br />
• Easy to use, intuitive, practical.<br />
• However it’s not a statistical data analysis package...<br />
• And that’s where NAG being great at serious<br />
numerical calculations comes in!<br />
3
Why you don’t need to be a C/Fortran programmer<br />
• The main library is provided in Fortran <strong>and</strong> C.<br />
• Fortran is a programming language especially suited<br />
to scientific computing <strong>and</strong> numerical analysis, also<br />
very popular in high-performance computing.<br />
• C is one of the most popular programming<br />
languages, know for its speed, flexibility <strong>and</strong><br />
readability.<br />
• The underlying programming language for <strong>Excel</strong> is<br />
VBA, which is relatively easy to learn <strong>and</strong> use.<br />
4
Why you don’t need to be a C/Fortran programmer<br />
NAG does all the programming (in Fortran or C),<br />
so all you need to know is<br />
how to call the NAG functions in VBA!<br />
5
NAG C Library vs. NAG Fortran Library<br />
A function can be called either from NAG Fortran<br />
Library or NAG C Library. What are the differences<br />
• Essentially they are the same;<br />
• NAG provides declare statements for the Fortran<br />
Library <strong>and</strong> defines C types in VBA;<br />
VB6 FL headers<br />
VB6 CL headers<br />
• C <strong>and</strong> VBA arrays start with 0, while Fortran arrays<br />
with 1;<br />
• C arrays need transposing.<br />
6
The IFAIL parameter<br />
IFAIL in NAG Fortran Library serves two purposes:<br />
• On input it determines what action the Library<br />
routine takes when an error is detected:<br />
<br />
<br />
<br />
-1 (Soft Fail & Noisy Exit) – error message <strong>and</strong> continue;<br />
0 (Hard Fail) – error message <strong>and</strong> stop;<br />
1 (Soft Fail & Quiet Exit) – no error message <strong>and</strong> continue.<br />
• On output it informs what kind of error has occurred,<br />
depending on the function in use (look into the<br />
documentation for details).<br />
• The C Library has an equivalent type NagError<br />
7
How to call a simple function<br />
• S17AEF (s17aec) – calculates the value of Bessel<br />
function J_0(x);<br />
• Note:<br />
<br />
<br />
<br />
<br />
Declaration statements in VB (C types, no types for<br />
Fortran)<br />
Calling straight from the spreadsheet<br />
Calling through a wrapper function<br />
Error h<strong>and</strong>ling<br />
8
How to h<strong>and</strong>le callbacks<br />
• A callback is a piece of executable code that is passed<br />
as an argument to other code.<br />
• D01BAF (d01bac) – calculates one-dimensional<br />
Gaussian quadrature<br />
• Note:<br />
<br />
<br />
<strong>Visual</strong> <strong>Basic</strong> AddressOf operator to h<strong>and</strong>le callbacks<br />
Argument Passing ByRef in Fortran <strong>and</strong> ByVal in C<br />
9
Debugging<br />
• When writing your own code consider:<br />
<br />
<br />
<br />
How the variables are declared<br />
NAG doesn’t accept Variants<br />
Integers are declared as Long<br />
How arguments are passed (ByRef/ ByVal)<br />
What the sizes <strong>and</strong> dimensions of arrays are<br />
If the arrays are numbered from 0 or 1<br />
<br />
<br />
If arrays need to be transposed<br />
If IFAIL is set properly<br />
10
How to pass arrays as arguments<br />
• F03AFF (f03aff) – calculates the determinant of a real<br />
matrix <strong>and</strong> performs LU factorization.<br />
• Note:<br />
<br />
<br />
<br />
Reading the array from the worksheet (Range->Double)<br />
Checking if the matrix is a square matrix<br />
The result of the factorization is not shown. How can it be<br />
shown<br />
11
Further applications of NAG <strong>Libraries</strong><br />
• Option Pricing:<br />
Option_Pricing_Example.xls<br />
• Portfolio Optimization:<br />
Portfolio_Optimization_Example.xls<br />
• Wavelet Transforms, ODE’s, PDE’s, <strong>Numerical</strong><br />
Differentiation, Linear Algebra, Matrix Operations,<br />
Statistical Analysis <strong>and</strong> many others...<br />
12
Conclusions<br />
• Calling NAG routines from <strong>Excel</strong> is very simple due to<br />
very extensive but concise documentation files;<br />
• <strong>Using</strong> NAG <strong>Libraries</strong> does not require knowledge of<br />
Fortran or C;<br />
• NAG & <strong>Excel</strong> together allow for a lot of flexibility <strong>and</strong><br />
individual approach concerning the GUI.<br />
13
References<br />
• NAG & <strong>Excel</strong> webpage<br />
http://www.nag.co.uk/numeric/nag<strong>and</strong>excel.asp<br />
• Product documentation<br />
http://www.nag.co.uk/support_documentation.asp<br />
14
Questions<br />
• www.nag.co.uk<br />
• support@nag.co.uk<br />
• Marcin.Krzysztofik@nag.co.uk<br />
Thank you for your attention!<br />
15