How to implement matrices in C# efficiently #275
Proektsoftbg
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
But, why not implement Math.NET? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The most straightforward way to program matrices in C-like languages is to use two-dimensional arrays [ , ]. But it is also very inefficient in respect to engineering analysis.
We can easily implement efficient data structures for different types of matrices through OOP, using inheritance and polymorphism. All matrices will be represented by vector of row vectors. For square symmetric matrices, we will store only the elements in the triangle above and along the main diagonal. So, each row will have different size. Also, we will reuse most of the available vector functionality right away.
By overriding the indexer's getter and setter for each type, we will make all matrix types behave in the same way externally, although they are quite different internally. The huge savings in both memory and time by not storing and processing part of the elements are worth the little overhead introduced by polymorphism.
And the LargeVector class is the same structure we used for the sparse vectors we discussed a few days ago. Initially, it does not reserve any memory until we assign some values. After that, it stores only the non-zero elements, so practically, in this way we get a sparse symmetric matrix.
Beta Was this translation helpful? Give feedback.
All reactions