mridul1809's blog

By mridul1809, history, 21 month(s) ago, In English

Hey everyone!

I have been trying to implement a Matrix class that contains the operations required frequently when facing problems of that domain.

So here it is : Link

I would like to invite everyone to use this for their convenience.

The implementation is to best of my knowledge but I am sure it's not perfect or complete. So if you can think of any improvements or additions please comment or DM or add a pull request!

I also intend to add templates regarding other topics as well. Please suggest me a few topics to work on.

Thank You! :)

EDIT1 : Hey everyone! Sorry I was unavailable for a couple of months and was not able to include the feed-backs I received from you guys! I will start working on them ASAP.

EDIT2 : Hey! I have made quite a few changes and added more operators & functions. Please checkout! :D

PS: Add watching to the repository to find out latest updates. Also I'll be adding other templates as well! Star the repository if you find it helpful! Thanks :)

 
 
 
 
  • Vote: I like it
  • +5
  • Vote: I do not like it

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

I think, method to calculate determinant for square matrices can be useful.

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Nice work bro

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

In your template, all operations are being computed modulo $$$mod$$$, but unary $$$-$$$ is not. So, that will be an issue.

»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

For large matrices this is fine, but for small (2x2 for example) matrices the cost of allocating and deallocating memory on the heap would be significant. I usually use template<int N, int M> struct Matrix { std::array<std::array<int, M>, N> A; }, or even template<int N, int M> struct Matrix : std::array<std::array<int, M>, N> { } (the latter way has the advantage of having operator == and operator [] implemented).

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

One of the most useful and well written templates I have seen. mridul1809

»
21 month(s) ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Better overload the assignment operations, i.e. +=,-=,*= and ^=, and implement them without creating a new matrix in order to save time and memory. And what about other operations like transpose ?

  • »
    »
    19 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Hey! I have added the suggested functionalities! Please checkout :D

»
21 month(s) ago, # |
  Vote: I like it 0 Vote: I do not like it

Great work bro.

»
19 months ago, # |
  Vote: I like it 0 Vote: I do not like it

How about determinant and inverse matrix?

»
5 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Because of using vector, you can also use swap(this->A, C.A); (which is constant complexity) instead of this->A = C.A

There is a trick for such matrix multiplication that you should use last for-loop for last dimention, in your case, swapping j-loop with k-loop is friendly cache and work faster

In matrix multiplication, you should reduce the amount of modulo you use

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

This is very good template and I like it. It would be better if it could work with float numbers. it casts product in multiplication function to long long and decimal part is lost.

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Recommendation: in dynamic programming matrix optimization, matrix size is usually static and quite small, almost always than 10x10. This would allow the compiler to perform additional optimizations and remove bound checks in some cases, and also data could be aligned better. So what about adding StaticMatrix<T, N, M> and using std::array instead of std::vector?

Another possible optimization, if speed matters: try AVX for matrix addition and maybe matrix multiplication.