Santi Peñate-Vera
Santi Peñate-Vera

Reputation: 1186

Matrix of matrices in Eigen C++

I'm creating a circuit analysis library in C++ (also to learn C++, so I'm very new to it).

After getting familiar with Eigen, I'd like to have a matrix where each cell hosts a 3x3 complex matrix.

So far I've tried this very simple prove of principle:

typedef Eigen::MatrixXcd cx_mat;
typedef Eigen::SparseMatrix<cx_mat> sp_mat_mat;

void test(cx_mat Z1){
   sp_mat_mat Y(2, 2);

    Y(0, 0) = Z1;
    Y(2, 2) = Z1;

    cout << "\n\nY:\n" << Y << endl;
}

Testing this simple example fails as a probable consequence of Eigen expecting a number instead of a structure.

As a matter of fact the matrix of matrices is prompt to be sparse, hence the sparse matrix structure.

Is there any way to make this work?

Any help is appreciated.

Upvotes: 1

Views: 3148

Answers (3)

Goodies
Goodies

Reputation: 2069

You asked "sparse matrix structure. Is there any way to make this work?" I would say no, because it is not easy to translate a circuit design into a "matrix of matrices" in the first place.. if you want to simulate something, you choose a representation close to it,. In case of an electronic circuit diagram, the schema in memory should IMHO be a directed graph, with linked-list items. At each node/junction, there is a matrix representing the behaviour of a particular component input to output transfer (eg resistor, capacitor, transistor) and you propagate the signal through the matrices assigned to each component. The transformed signal eventually arrives at an output, through the connections in your connected graph. In software, it should work similarly.. Suggested further reading: https://core.ac.uk/download/pdf/53745212.pdf

Upvotes: 0

Nhan Nguyen Minh
Nhan Nguyen Minh

Reputation: 1

Have you found a way to create a matrix of matrices? I see that we can use a 2-D array to create a matrix of matrices. For example,

#include <Eigen/Dense>
MatrixXd A; 
MatrixXd B;
A = MatrixXd::Random(3, 3);
B = MatrixXd::Random(3, 4);
C = MatrixXd::Random(4, 4);

MatrixXd D[2][2];
D[0][0] = A;
D[0][1] = B; D[1][0] = B.transpose();
D[1][1] = C;

I don't know if this way is memory-efficient or not. Let's check it out.

Upvotes: 0

Deepfreeze
Deepfreeze

Reputation: 1805

I don't believe Eigen will give you a way to make this work. I you think about the other functions which are connected to Matrix or Sparse matrix, like:

  • inverse()
  • norm()
  • m.row()*m.col()

what should Eigen do when a matrix element number is replaced by a matrix?

What I can understand is that you want to have a data structure that stores your Eigen::MatrixXcd in an memory efficient way.

You could also realize this using the map container:

#include <map>

typedef Eigen::MatrixXcd cx_mat;
cx_mat Z1;

std::map<int,Eigen::MatrixXcd> sp_mat_mat;
int cols = 2;
sp_mat_mat[0*cols+0]=Z1;
sp_mat_mat[2*cols+2]=Z1;

Less memory efficient, but perhaps easier to access would be using the vector container:

#include <vector>

std::vector<std::vector<Eigen::MatrixXcd>> mat_mat;

Upvotes: 3

Related Questions