Page 2 of 2
Random Number Generator
Let's look at a slightly more involved example  a random number generator. The library has lots of different types of random number generator but let's do something simple and generate an array of ten random numbers. First we need to create a state object which is used to control how the base random number generators are set up.
int fail=0; int[] seed = {12345}; G05.G05State state=new G05.G05State( 1,1,seed,out fail);
The first two parameters set the generator type. In this case it sets up the NAG basic generator (a standard congruential generator) . The seed sets the initial values for the generator  different generators need different numbers of seeds but the basic generator only needs one. The fail variable is used to signal an error and we should really test that it is zero i.e. no error before continuing.
Once the state object is setup we can obtain a vector  in this case of ten random values:
int n = 10; double[] x=new double[n]; G05.g05sq(n, 0.0, 1.0,state,x,out fail);
The random values are in the range 0 to 1 and are returned in x. Notice that fail is used to indicate an error and again we should test it before moving on.
As a slightly more complex example let's generate a random orthogonal matrix  a task that is more difficult than you might think at first statement. The task might be difficult but the way that you use the library is simple enough.
You need a status object to initialize the random number generator as before and an array to hold the result:
double[,] matrix = new double[n, n]; G05.g05px("Right","Initialize", n, n, state, matrix, out fail);
The method has a number of parameters that make it more useful. You can opt for the generated matrix to multiply the supplied matrix on the left or right using the first parameter or you can set it to be initialized to the identity using the second.
To test that we really do have an orthogonal matrix we can multiple it by its transpose and expect to see the identity. To do this we make use of a method that forms the expression:
aAB+bC
where A,B and C are matrices and a and b are scalars. Either of the matrices can also be transposed in the operation. For example, to form AA^{T} you would use:
double[,] resultMatrix = new double[n, n]; F06.f06ya("N", "T", n, n, n, 1, matrix, matrix, 0, resultMatrix, out fail);
where the first two parameters specify that the first matrix is to be left alone and the second transposed. As we only want to multiply two matrices a is set to 1 and b set to zero  the result is returned in the third matrix i.e. resultMatrix and any errors are reported using fail.
Online help  click to enlarge
Reliable Number Crunching
So that's all there is to using the NAG library for .NET. As the actual number crunching code is compiled from the original Fortran library you can be sure that once the transfer is made from the .NET language of your choice to one of the methods then it will be as efficient, robust and correct as possible. It will certainly be more efficient that the equivalent method implemented in C# say. The use of static methods to wrap the numerical methods seems like the best way to do the job. Where needed the library does make use of .NET mechanisms to get things done  for example delegates are used to pass evaluation functions.
Getting to know the library is a matter of looking in the class of routines that you are interested in and working through what methods are provided. Even though the examples given earlier are focused on using random number generators and matrices the general principles are the same for the majority of the methods.
For more information visit: NAG Library for .NET
If you would like to be informed about new articles on I Programmer you can either follow us on Twitter or Facebook or you can subscribe to our weekly newsletter.
Linq and XML
XML, which is all about tree structured data, and Linq, which is all about querying collections, might not seem to fit together, but they work together just fine.

XML in C#  Using XElement
.NET has some really easytouse facilities for creating and editing XML. Many of these facilities were introduced to make Linq to XML work better, but you can make use of them in more general situati [ ... ]
 Other Articles 
<ASIN:1449380344>
<ASIN:0123745144>
