-
Notifications
You must be signed in to change notification settings - Fork 323
How to perform in-place operations on parts of a vector provided an index array/vector? #419
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
Comments
I'm not quite sure what your exact objectives are, so here are a few solutions depending on what you're trying to achieve. (Note that all these examples use This is the simplest example: #[macro_use]
extern crate ndarray;
use ndarray::prelude::*;
fn main() {
let mut vec = Array1::zeros(6);
let mat = Array2::from_elem((3, 3), 1.);
let x = Array1::from_elem(3, 1.);
vec.slice_mut(s![1..;2]).assign(&mat.dot(&x));
} Some notes:
If you want a separate index object, you can create and use a #[macro_use]
extern crate ndarray;
use ndarray::prelude::*;
use ndarray::Slice;
fn main() {
let mut vec = Array1::zeros(6);
let index = Slice::new(1, None, 2);
let mat = Array2::from_elem((3, 3), 1.);
let x = Array1::from_elem(3, 1.);
vec.slice_mut(s![index]).assign(&mat.dot(&x));
} If you want to use 2-D arrays for #[macro_use]
extern crate ndarray;
use ndarray::prelude::*;
use ndarray::Slice;
fn main() {
let mut vec = Array2::zeros((6, 1));
let index = Slice::new(1, None, 2);
let mat = Array2::from_elem((3, 3), 1.);
let x = Array2::from_elem((3, 1), 1.);
vec.slice_mut(s![index, 0..1]).assign(&mat.dot(&x));
} If you really want to avoid creating a temporary array for the result of the #[macro_use]
extern crate ndarray;
use ndarray::prelude::*;
fn main() {
let mut vec = Array1::zeros(6);
let mat = Array2::from_elem((3, 3), 1.);
let x = Array1::from_elem(3, 1.);
azip!(mut vec (vec.slice_mut(s![1..;2])), ref mat (mat.genrows()) in {
*vec = mat.dot(&x);
});
} This iterates over the elements in Does this answer your question? By the way, do you have suggestions to improve the documentation? I've been using Edit: Rereading your question, it sounds like you might be specifically asking about NumPy's "index arrays" feature. Other than |
I would say that this definitely answers my question. I was specifically asking about an equivalent to Numpy's index array feature. It looks like I'll need to use iterations for my use case. Also as far as some of my objectives, I'm looking into implementing a couple of different krylov subspace methods in Rust. One of my use cases with these methods will have block submatrices that reside in your A matrix for Ax=b, and these could be for example elemental stiffness matrices in a finite element calculation. Often these submatrices will not be operating on contiguous sections of your x or b. In Fortran and Python, I am used to being able to use an index array such that these operations are relatively clean from a code point of view. Though, I guess I would need to dig into how gfortran handles this situation to see if it is also creating temporary arrays during compilation. Now that I'm thinking about it I could probably get away with just two temporary variables while iterating over 100k+ of these submatrices. On the documentation side of things, I would say if its possible maybe having something similar to Numpy's Numpy for MATLAB users could be quite useful. Though, I would say instead of Matlab |
First off, I just want to say that I'm still very much getting the hang of ndarray. So far, I understand the basics like how to perform matrix multiplication, slicing, and in place mapping functions. However, I've found the documentation to be at times a little hard to understand.
Now on to my actual question, so in Numpy or Fortran I can easily do something like the following:
It's the being able to assign values to a vector or an array at these indices that I'm trying to get a better hold of using ndarray.
Currently, the only way I can really think of how to do this is the following:
I could make this into function, but I'd like to believe there's a built in way to do this in ndarray that I'm just missing or have over looked that avoids the need for the temporary arrays/vectors creation.
The text was updated successfully, but these errors were encountered: