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

Create 2d array from matrix product of 1d arrays #1298

Open
ModProg opened this issue Jun 25, 2023 · 1 comment
Open

Create 2d array from matrix product of 1d arrays #1298

ModProg opened this issue Jun 25, 2023 · 1 comment

Comments

@ModProg
Copy link

ModProg commented Jun 25, 2023

I have two vectors, and want to multiply them to a 2d matrix by transposing one of them.

Currently I am using nalgebra + ndarray for that:

let a = // the 1d ndarray
let a = Vector::from(a.to_vec()); // convert to nalgebra
let a_adjoint = a.adjoint(); // transpose
let b = a * a_adjoint;  // create matrix in nalgebra
let b = Array2::from_shape_fn((a_len, a_len), |i| b [i]);

Both better integration with nalgebra (adding some From implementations through a feature flag) or supporting this operation directly would help.

Maybe there is a better way to do this I'm not seeing.

@adamreichold
Copy link
Collaborator

How about doing this directly with from_shape_fn, e.g.

let b = Array::from_shape_fn((a_len, a_len), |(i, j)| a[i] * a[j]);

If you want a point-free version, then I think this requires broadcasting, e.g.

let aa = a.broadcast((a_len, a_len)).unwrap();
let b = &aa * &aa.t());

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