I was interested in testing mpfit as a replacement for another algorithm we've been using internally. In order to target the Silverlight/Moonlight runtimes, our .NET code (C#, C++/CLI, VB.NET, IronPython, etc) must be compiled with a "clr /safe" flag, which means unsafe pointer arithmatic is out. So, I spent a little time yesterday creating a direct port of the C-based mpfit to C# (fairly easily, as C# is a C-style language), with minor changes to remove pointer addressing.

A few things that needed tweaking:

- the (void *) required user-function private variable was replaced by a System.Object reference, which similarly allows a user to cast to their format within the user-function
- the "dvec" derivative vector input signature was changed from double** to IList<IList<double>> (as opposed to double[][]), in order to allow use of my DelimitedArray<T> class (also included).
- similarly, mp_enorm was modified to accept these IList<double> arrays (accessing/assignment via indexers works just like arrays)
- DelimitedArray<T> (insprired by this post) was created to allow safe assignment of a sub-array, within a larger parent array, and is used exlusively with the 'a' vector in mp_qrfac.
- any time a BOOL-like call was made on an integer, I test for zero (e.g. "if (fixed != 0)")
- any null reference array checking via integer values was converted to null checks (e.g. "if (arr != null)")
- structs were converted to classes, which are passed by reference in C#, and therefore retain the same intent of the original dereferenced structs within method calls
- C#-style naming added sparingly to MPFitLib.Test application, and even more sparingly to MPFitLib library, in order to preserve consistency across implementations

Last edited Mar 16, 2011 at 6:46 AM by dcuccia, version 1


No comments yet.