Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to construct a symmetric matrix in nalgebra? #1389

Closed
hunter1992 opened this issue May 6, 2024 · 3 comments
Closed

How to construct a symmetric matrix in nalgebra? #1389

hunter1992 opened this issue May 6, 2024 · 3 comments

Comments

@hunter1992
Copy link

My goal is to construct a symmetric matrix M, which is composed of some known submatrices.

For example, the M I'm gonna construct is

$$ M=\begin{bmatrix} M_{11}&M_{12}\\ M_{21}&M_{22}\\ \end{bmatrix} $$

And the submatrix $M_{ij}$ are some known submatrices:

$$ M_{11} = \begin{bmatrix} a&b\\ b&a\\ \end{bmatrix} $$

$$ M_{12} = \begin{bmatrix} c&d\\ e&f\\ \end{bmatrix} $$

$$ M_{21} = \begin{bmatrix} c&e\\ d&f\\ \end{bmatrix} $$

$$ M_{22} = \begin{bmatrix} g&h\\ h&i\\ \end{bmatrix} $$

That is to say, the final M matrix is:

$$ M= \begin{bmatrix} a&b&c&d\\ b&a&e&f\\ c&e&g&h\\ d&f&h&i\\ \end{bmatrix} $$

I want to know if there is a method in nalgebra that can still efficiently construct M when the size of M is large.

In Python, the numpy has hstack or vstack function to implement my desire.
Is there a function with the same functionality in nalgebra?

@Andlon
Copy link
Sponsor Collaborator

Andlon commented May 6, 2024

#1375 is ready to merge, and it'll probably make it to the next release. Hopefully this will be soon. In the meantime, you'll have to:

  1. construct your output matrix with Matrix::zeros
  2. use e.g. .view_mut(...) to construct a view for each block
  3. populate each view with e.g. view.copy_from(&M_11)

Hopefully the new stack! macro will make this a breeze.

@Andlon
Copy link
Sponsor Collaborator

Andlon commented May 6, 2024

To elaborate, since there's no example in the PR description, you'll be able to write:

let m = stack![ m11, m12;
                m21, m22 ];

@hunter1992
Copy link
Author

@Andlon Thank you very much! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants