## We have moved the forum to https://forum.ngsolve.org . This is an archived version of the topics until 05/05/23. All the topics were moved to the new forum and conversations can be continued there. This forum is just kept as legacy to not invalidate old links. If you want to continue a conversation just look for the topic in the new forum.

#### Notice

The forum is in read only mode.

# Dense Matrix - c++ mlngs

2 years 7 months ago #3535 by jhauser
Hallo!

Currently I'm working on dense system matrices and I'm not sure which data typ is best to use for that kind of problem.
Sure, typically FlatMatrix and Matrix are used in such cases. However your KrylovSpaceSolver is a child of BaseMatrix. Since FlatMatrix and Matrix are children of MatExpr, and not BaseMatrix, those solvers can't be used. I was thinking about still using a SparseMatrix ( with losts of entries) to be able to use the solvers. But I have no idea if that will result in more memory consumption that simply using FlatMatrix.

Could you please tell me what the preferred matrix type should be for dense system matrices?

Thank you all!
Julia
2 years 7 months ago #3554
Hi,
I would use the Matrix class to store dense matrices. You could wrap you dense matrix in a BaseMatrix to use it in a routine that expects BaseMatrix.
You can even do this in Python, you only need to implement Mult, Height and Width functions:
Code:
class MyBaseMatrix(BaseMatrix): def __init__(self): super().__init__() def Height(self): return ... def Width(self): return ... def Mult(self, x, y): # implement y = mat * x

the incoming vectors are BaseVector. You can use the x.FV() to get a flatvector from the basevector

Best Christopher
2 years 6 months ago - 2 years 6 months ago #3575 by jhauser
Replied by jhauser on topic Dense Matrix - c++ mlngs
Hi!
I have my code in c++ (kinda included in the mylittleNGSolve structure for easy installing) Therefore I tried to simply make a child class of BaseMatrix as you adviced. (but in c++)
However, since the constructors of BaseMatrix are all protected, the constructor of my child class does not work.

For example: Put MyBaseMatrix2.hpp into the folder 1_Basic/4_utility_functions (of mlngs) and put an "#include "myBaseMatrix2.hpp"" in the beginning of myAssembling.cpp and "MyBaseMatrix2<> sys_matrix(ne,ne);" in line 35 of the same file. While installing, you won't be having problems, but if you then run 4_utility_functions/assembling.py with python3, you'll get the error
"ImportError: [location of installation of myngspy.so]: undefined symbol: _ZN4ngla13MyBaseMatrix2IdED1Ev".

However DiagonalMatrix (in linalg/special_matrix.hpp) seems to be implemented in the pretty same way, which confuses me.

Could you please tell me how to write a proper matrix class in c++? Or is there a simple error in "myBasisMatrix2.hpp"?

Thank you again, Julia
##### Attachments:
Last edit: 2 years 6 months ago by jhauser.
2 years 6 months ago #3580
You have to define your functions, the destructor and the createvectors are only declared but not defined.

Best
Christopher
2 years 5 months ago #3644 by jhauser
Replied by jhauser on topic Dense Matrix - c++ mlngs
Oh, yes. That's right. However it didn't fix the problem. The same error (_ZN4ngla13MyBaseMatrix2IdED1Ev) was still there.
I got it fixed by putting

template class MyBaseMatrix2<double>;

into the end of the .cpp file. Otherwise python seems to have problems to find the class.