# Fast Vector Math library for .Net

This topic is 3779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello I am searching for a non-commercial fast vector algebra library, which has bindings for .Net and implements some object-oriented features like operator overloading. It would also be good if it uses SIMD instructions. All .Net math libraries that I found are either commercial or they don't support a nice interface. Maybe I have to port a C/C++ lib on my own. But I don't want to reinvent the wheel. :( Thanks in advance. [Edited by - teichgraf on February 29, 2008 4:05:41 AM]

##### Share on other sites
Microsoft.DirectX reference.

##### Share on other sites
You can take a look at SlimDX, which contains a rather full featured math library. Most of it is implemented in C#, whereas some is passed on to D3DX through C++/CLI.

In our case, we found that even though D3DX uses asm and per-machine optimizations to make its library as fast as possible, it was still beneficial for us to reimplement a lot of the functionality because the cost of interop and marshaling was so high.

##### Share on other sites
Thanks for the replies!
I thought that I have to implement it. :-(
Fortunately I don't need a full featured math lib.

So what could be the fastest way to code a vector3d / vector2d class with a nice interface to use.
The nicest would be if I have a VectorN class with all operators and Vector3 / Vector2 which are derived from VectorN. So i don't have to implement the operators twice. But I think this is also slow because of the virtual calls and for-loops in the operators.
Maybe it could be the fastest to implement the Vector3 / Vector2 separate as structs or sealed classes?

How could I implement them for float and double without having separate classes. With C# generics I don't know how to do this like in C++. ?

I would appreciate it if I could get some advice on this.

##### Share on other sites
The problem with a VectorN abstract base class is that the operators would take VectorNs as parameters. However, a Vector3 cannot be added to a Vector2. The same goes for most operations. This means that you'd really want one operator to take two Vector3s and another to take two Vector2s, but none to take one Vector3 and one Vector2. However, all you'd have is ones that take both VectorNs which could be given one of each vector type.

##### Share on other sites
One other problem, possibly greater, is that classes have hidden overhead (the ability to inherit and implement interfaces comes at a cost). Moreover, they cannot be allocated on the stack, and have worse locality of reference when compared to structs (which are value objects).

OpenTK also comes with a fairly full-featured math library (Vectors, Matrices, Quaternions). As a bonus, it contains both single precision and double precision structs, which will come in handy in the future when video cards will support 64bit precision. It also works on all platforms, not just windows. No SIMD though.

If you are using .Net 3.0, you can use WPF math functions too (full-featured, too).

[Edited by - Fiddler on March 2, 2008 10:25:21 AM]

##### Share on other sites

OpenTK sounds interesseting. But I miss a Matrix33 and a VectorN class. Maybe I could extent the OpenTK lib.
At the moment I use Tao.OpenGl for rendering. So it should be no problem to switch to OpenTK.

By the way, how could one use SIMD instructions in C#?
unsafe{	__asm	{	}}

??

##### Share on other sites
I am on a same or similar search. The only reason it might be similar and not the same is that I am looking for one written with managed code and not just with an interface. (This might not fit your speed requirement?)

So far, the most promising I have found in the price range we are discussing are the Sharp3d Math Library at CodePlex from ekampf (forgot the person's name). It is written in C# and has online suggestions, one of which is to convert calls to fields instead of properties for a 15-20% speed improvement (if I remember the suggestion correctly).

There is also a great discussion of a C# Vector Type at CodeProject by Richard Potter which has lots of good information and feedback with other people's opinions on how it should be coded (much of which the author took). The boxing/unboxing problem when structs are used with collection classes is solved in the NetFramework 2.0 with generics (methinks).

Good luck!
-- Geoff

##### Share on other sites
Another possible problem I see with a generic class is that unless you specialize pretty much all basic methods, you will end up with a lot of loops "for (int i = 0; i < vector_size; i++) ..." wich are not a really good thing for performance. I don't know how much smart is the compiler, nor generics have some features to avoid this, but if you must specialize the operations then you loose the gain of a generic vector anyway...

##### Share on other sites
Thank for the infos.

@Geoff C: I would also prefer a managed version.
I have also found the Sharp3D lib. at Codeplex and many other. But I think the project is dead (see the source code commits). ?
The Codeproject implementation looks nice.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631712
• Total Posts
3001849
×