Numerical Analysis for .NET

During my ongoing work on a computational project for university, I recently discovered the need to perform some serious numerical analysis from my C# code.  Unfortunately, I must admit that the .NET world only now seems to be catching up in terms of the free and open source libraries it offers for various tasks, and initially I was disheartened to find that there seemed to be nothing available for doing calculations on large (sparse) matrices. After a fair deal of searching, only a couple of somewhat incomplete and no longer maintained matrix libraries turned up. Being an avid user of StackOverflow, however, I decided that if anyone was aware of some library that could do what I needed, I would most likely find them there.

The result was much better than for what I was even hoping. dnAnalytics is a general-purpose package for numerical analysis in .NET that does almost everything for which I might possibly ask – and from my first impressions, does it very well indeed. This wonderful find is a well-maintained, fully open-source, library with great API documentation (not a wholly unexpected thing, but surprisingly uncommon among so many open source projects). There are several features that stand out as particularly impressive. One undoubtedly is I/O classes for Matlab and delimited files (among other formats). What is more, the library seems to offer both a fully managed version and one that wraps the Intel® Math Kernel Library. I’m not sure how the performance compares between the two (I haven’t yet tried the latter), but it is surely nice to have the pair of options available, quite similarly to how you have alternatives of cryptographic algorithms in the .NET BCL, that is to say, a) a fully managed version, v) a version based on top of the Windows Crypto API, c) a version that uses the CNG (Next Generation) API introduced with Vista. Perhaps what appeals to me the greatest about this library is that the developers have clearly gone to an effort to make it user-friendly, not only with regards to the documentation, but also by adding an interface friendly to F# coders (likely to be a language of choice for future mathematical/scientific programming), and even visual debuggers for Visual Studio (possibly the only library to date I’ve seen include them).

My particular usage of the library requires me to use the linear algebra (specifically, sparse matrix) classes. Although I must point out that the specific algorithm that I was intending to employ for the project was not available (see my later discussion), it did include a host of other ones, primarily focusing on direct and iterative matrix decomposition, which would appear to be quite handy in many circumstances. I haven’t yet had a chance to play with the other areas of the library, but I have noticed that it offers some statistical functions and methods as well as a number of modern pseudo-RNG algorithms such as the Mersenne Twister.

To conclude, I should come back to the point that the most important part of the analysis I require was not (at least directly) contained by the library – finding the eigenvalues or eigendecomposition of large (1000s of rows/columns) matrices, which happens to be in relation to spectral theory, in case you’re curious. Even so, being such a complex field and one fraught with difficulties when it comes to implementation (numerical instability is a huge problem), I was not surprised to find that an implementation of the Arnoldi or Lanczos algorithm was not present. Fortunately, after a bit more searching around (by this point I knew specifically what I was looking for), I came across the ARPACK library, written in the archaic Fortran77 language. It did however seem to be exactly what I was looking for: a set of fast routines to find the eigenvalues of large (either dense or sparse) matrices of various types. After only a small amount of pain messing about with MinGW, I managed to get the code compiled nicely into a DLL. At this point, I am of course perfectly able just to use the P/Invoke capabilities of .NET and do some hackery to integrate the ARPACK stuff with my existing code and dnAnalytics. Yet, I am also inclined to do this whole task properly and basically write a managed wrapper for ARPACK that is tightly conforms with dnAnalytics. I could then perhaps submit these wrapper types (along with a few unit tests?) as a repository patch to the dnAnalytics team in the hope that they’ll take it and add it to the next release. As with most other projects at this time, I will have to see what time permits me, though I would certainly hope to contribute something substantial to what truly is a terrific project that I would love to see expand further.

10 Responses to “Numerical Analysis for .NET”

  1. guidupuy says:

    Hi,

    I realize this is a pretty old post, but I was wondering where this project had gone. Like you, I need to do *just* what ARPACK does and would very much like to do it from within my C# project. I have successfully compiled the fortran sources into a dll file, but am getting a bit lost in the original source and am not sure what to P/invoke and how exactly to do it

    Have you completed anything on your side? I gotta say, a managed wrapper sounds like music to my ears…!

    Thanks in advance,
    Guillaume

  2. Noldorin says:

    Hi Guillaume,

    This project hasn’t gone very far I’m afraid! It only was a summer-term coursework at university, so I haven’t touched it for some time now. Saying that, I think ARPACK is an invaluable library, especially for scientists and applied mathematicians, so I am keen to take this somewhere further!

    There exists the excellent Math.NET Numerics project which is nearing 1.0 release now. (And the linear algebra subset: http://numerics.mathdotnet.com/linear-algebra/.) I was thinking of creating an ARPACK wrapper for .NET and integrating it into Math.NET (which provides all the standard fast-matrix/vector stuff). I forget how the data structures are organised, but with any luck this is not too huge a project.

    So I may not have too much time for coding over the next week or two, but if you can wait just a short while I should have something to show you in terms of a managed wrapper. :-) Thanks for reminding me of this, in fact.

    Alex

  3. guidupuy says:

    Hi Alex,

    I did download the Math.NET Numerics library – as you mentioned, it does not include any reference to either the Arnoldi restarted algorithm or the Lanczos method. It looks pretty comprehensive on other subjects though.

    You seem to know what you’re doing. Surely, having an ARAPCK wrapper for .NET would be on tremendous help for me and, I’m sure, a lot of others. Having to dig into old FORTRAN code is quite a pain; it needlessly raises the entry level to a seemingly great library.

    Let me know of how things are coming. I will be looking for news on this comment thread. In the meantime, I will look further into how to P/Invoke this beast ;-)

    Guillaume

  4. guidupuy says:

    Hey Alex,

    Sorry to bother you again with this. I looked more into the code at lunch and it seems like there is no high level method that I can simply invoke to compute, say, the 10 ‘biggest’ (as defined by their eigenvalues) eigenvectors of a symmetric real matrix. Instead, the author coded an example program that set the appropriate variables and put together the right loop to exploit the ssaupd & sseupd subroutines.

    My guess is that I should start from this program and turn it into a subroutine taking as variables just what what I need to be variable in my C# program (my matrix, how many vectors I want, etc.). I’m pretty new to P/invoke and Fortran though, so I’m not sure. How did you originally go about it? Do you have any code excerpt handy that could give me a head start?

    Thanks a bunch,

    Guillaume

  5. Noldorin says:

    Hi Guillaume,

    Yes indeed, there is a significant difference in ideology between the Arpack API and the Math.NET one… as one might expect from old FORTRAN code and a modern object-oriented library! I think it is doable still.

    Will have a look today at getting some sort of demo/basic interop working. Feel free to email me if you want to stay in touch that way. Otherwise, you can simply get notifications on this comment thread.

    Oh, and the great thing about Math.NET Numerics (apart from being pretty complete as you say) is that it has some pretty good backing, including Microsoft Research. In a few versions’ time I think it will be among the best numerical libraries out there!

    Cheers,

    Alex

  6. Noldorin says:

    Update: Math.NET Numerics already makes use of several native providers (in a pluggable fashion) for certain tasks, including MKL and LAPACK, it would seem. With any luck, this may make adding ARPACK support via a wrapper much simpler. :-)

  7. guidupuy says:

    Excellent! Sounds like a good premise. Keep me posted! I will be closely following this thread. ;-)

  8. Noldorin says:

    I received a reply from one of the Math.NET Numerics developers that is quite encouraging:

    http://mathnetnumerics.codeplex.com/discussions/267872

    I’ve had very little time to properly start coding the features yet, however, but in the coming week I certainly ought to have some.

  9. guidupuy says:

    Excellent! For your information, I have managed to do what I wanted to do using P/invoke. However, a clean implementation that is part of Math.NET would be even better (I use their library anyway). I’ll follow your work closely!

  10. Noldorin says:

    A quick update (I’m sorry for the lack of progress on this front): I’ve checked into adding this functionality, and have had more feedback from the Math.NET Numerics team. Basically, it makes sense to wait until they have rewritten their sparse linear algebra code, which is being released very soon. I’ll be getting right on adding ARPACK features to the library after that!

RSS feed for comments on this post. And trackBack URL.

Leave a Reply