NAG Libraries and Component Object Modules (COM)
By David Sayers PhD, Numerical Algorithms Group
NAG library routines may be called from a variety of environments. Information on calling the library from Java is available on our web site. Our PC DLL implementations in particular are used in many different ways and we give examples of use from Excel, Visual Basic Delphi and .NET with our Fortran product. Our web site also contains an article which reports on the implementation of some of the NAG library routines as ActiveX components. It is the component aspect of NAG software that this article will discuss in greater detail, taking one specific example from the Fortran DLL.
Software components are popular because a complicated application may be split into smaller components, each offering a different service or functionality. Each component is independent and the internals of one have little or no effect on the internals of another, so each component may be written by separate programmers. Moreover any component may be used in other applications, an easy form of software re-use. This article illustrates how anyone with a modest understanding of Visual Basic may construct an in process COM component using the NAG DLLs. This component may then be used within other, larger applications. In our case we construct a component that makes available the NAG random number generators. These are then called, for illustrative purposes, directly from PowerPoint.
In Visual Basic start a new project and choose ActiveX DLL. You will have a Class Module within this project. In order to create the COM component we shall place VB code within this module. First we rename the class module so that it will be unique and self-explanatory, since applications needing the random number functionality will need to reference the component. To do this make sure that the properties window is visible. You may right click on 'Class1' in the Project window or alternatively display the window from the View menu. Change the name in the properties window to say 'NAGRandom'.
At this point I like to save the project, giving the project and other files meaningful names, but also placing them in a newly created directory. (Its surprising how easy it is to clash existing information if you don't do this.)
As I intend to use the Fortran DLLs as the source of random number routines I then type 'Option Base 1' at the head of the class module. This is a useful facility since now any VB arrays use exactly the same storage conventions and have the same dimensions and sizes as the corresponding Fortran routines.
Now copy the G05 'Declare' statements provided in vb.decs with the NAG DLLs and paste them into the module. Make these private to the class by inserting the word 'Private'. You should now have a module beginning:
Option Base 1 Option Explicit Private Declare Function G05KAF Lib "DLL20DDS.DLL" (igen As Long, iseed As Long) As Double Private Declare Sub G05KBF Lib "DLL20DDS.DLL" (igen As Long, iseed As Long) Private Declare Sub G05KCF Lib "DLL20DDS.DLL" (igen As Long, iseed As Long)
and containing all of the Declare statements for the routines you wish to use.
It suffices to simply declare the necessary class variables:
Dim igen As Long Dim iseed(4) As Long
by typing these statements into the module.
Initialisation facilities are provided by then typing in the statements:
Private Sub Class_Initialize() igen = -999 ' to trap no call to InitialiseSteam End Sub Public Sub InitialiseStream(istream As Long, myrepeatable As Boolean) igen = istream If myrepeatable Then iseed(1) = 12345 iseed(2) = 23456 iseed(3) = 34567 iseed(4) = 45678 Call G05KBF(igen, iseed(1)) Else Call G05KCF(igen, iseed(1)) 'unrepeatable End If End Sub
and finally the random number capabilities are added by typing in small wrappers around the NAG routine calls.
Public Function Uniform_01() As Double Uniform_01 = G05KAF(igen, iseed(1)) End Function
Save the project and from the file menu issue the 'Make xxx.dll' command, where xxx relates to the project name.
If all has gone well you should now have a COM file called NAGRandom.DLL. If you have typed in just those statements I have described then this will give you the ability to initialise a stream of random numbers and generate uniform numbers in (0,1) from that stream. You may now save the project and close down Visual Basic.
We may check the COM file by creating a very simple PowerPoint slide. Create a new PowerPoint project and make sure that the Control Toolbox is visible by checking the View\Toolbars menu. From the Control Toolbox place a Command Button and then Label onto the slide. The command button will be used to initialise the stream and the Label will be used to generate another number in the sequence and to display that number.
To do this we need to associate a small amount of code to each control. Select the command button and then press the View Code button on the Command Toolbox. Type in the code:
Private Sub CommandButton1_Click() Set NAG = New NAGRandom Call NAG.InitialiseStream(1, False) End Sub
You can also type in the code for the Label:
Private Sub Label1_Click() Dim r As Double r = NAG.Uniform_01() Label1.Caption = Str$(r) End Sub
We need to insert the declaration for NAG in a module, which we create from the Insert menu of the Visual Basic Window.
Type in this module the declaration:
Global NAG As NAGRandom
Finally we need to Reference the newly created NAGRandom.DLL COM file, which we do from the Tools\References menus of the Visual Basic Window. You may need to use the Browse Button of the resulting menu to locate this file.
Now you may save and close the Visual Basic Window of PowerPoint. Start the slide show from the Slide Show menu and click the Command Button. This initialises the generator. Click the label once. This should generate and display a random number. Click again to generate and display a second number.
The COM component should be delivering random numbers as requested. The component may be used in a very similar manner across the Microsoft range of products.