I'm developing an application involving math and physics models, and I'd like to use a Math library for things like Matrices. I'm using C#, and so I was looking for some libraries and found Math.NET. I'm under the impression, from past experience, that for math, using a robust and industry-approved third party library is much better than writing your own code.

It seems good for many purposes, but it does not provide support for Quaternions, which I need to use as a type. Also, I need some functions in Vector and Matrix that also aren't provided, such as rotation matrices and vector rotation functions, and calculating cross products. At the same time, it provides a lot of functions/classes that I simply do not need, which might mean a lot of unnecessary bloat and complexity.

At this rate, should I even bother using the library? Should I write my own math library? Or is it a better idea to stick to the third party library and somehow wrap around it? Perhaps I should make a subclass of the Matrix and Vector type of the library? But isn't that considered bad style?

I've also tried looking for other libraries but unfortunately I couldn't find anything suitable.

  • 4
    Thought experiment: let's say that using the library means you don't have to write 80% of the math code, but it costs your application, say, an extra 2 megabytes in additional code that you won't ever use, and you'll have to write the remaining 20% yourself (with the help of Math.NET). Is it still worth it? The answer to that seems obvious to me. – Robert Harvey May 28 '14 at 6:07
  • 2
    Sounds to me like what you need is a linear algebra library, not a (general) math library – Idan Arye May 28 '14 at 6:33
  • 1
    @RobertHarvey Good point. But how should I go about writing that 20%? If I were to extend the library by making my own subclasses that tackle my needs, won't that make a bigger mess and introduce incompatibilities and bugs? Is this a common thing that people do when facing a situation like this? – 9a3eedi May 28 '14 at 6:35
  • 1
  • 1
    @9a3eedi no clear cut answer. Create the missing bits yourself seems an obvious choice, rewriting the entire library might be called for at times, or find another library. There's cases in which each scenario may be the best solution. – jwenting May 28 '14 at 10:13

Write the classes you need using the library. Once you have proper code and tests in place, offer to contribute those classes back into the library, since MathDotNet seems to be an open source project.

Just to make things clear, using the library to write those functionalities you find missing does not mean to modify the library's source code to patch it on your own. It means importing the library in your code, and using its existing functions to save as much work as possible while developing your new classes. You can later isolate those changes and create a patch for the library out of them.

Also, to clarify license matters: the MIT license of MathDotNet does not force you to contribute your changes. But it is a Good Thing to share, it lifts the burden of maintaining your changes from your own shoulders, and it helps prevent a future where every C# developer has his/her own Quaternion implementation.

  • Makes sense. I've decided to make a static "MathHelper" class with static functions that implement the functions I need. Not sure if this is a good approach but it's simple and it works and does not break compatibility. – 9a3eedi Jun 2 '14 at 3:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.