# Dense Matrix - c++ mlngs

2 months 3 weeks 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 months 2 weeks 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:
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 months 3 days ago - 2 months 3 days ago #3575 by jhauser
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:

2 months 1 day ago #3580
You have to define your functions, the destructor and the createvectors are only declared but not defined.

Best
Christopher

1 month 1 week ago #3644 by jhauser
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.