获取 1d Rust ndarray 的差异
get diff of 1d rust ndarray
我觉得这段代码应该可以工作:
use ndarray::{Array1, s}; // 0.15.4
fn main() {
let x = Array1::<f64>::range(0.0, 10.0, 1.0);
println!("{:?}", x);
println!("{:?}", x.slice(s![1..]));
println!("{:?}", x.slice(s![..-1]));
println!("{:?}", x.slice(s![1..]) - x.slice(s![..-1])); // fails to compile when this line is uncommented
}
如果我评论最后的印刷品,前 3 行印刷品确实有效。我在这里遗漏了什么吗?
Rust Playground Implementation
documentation 解释说:
Let A be an array or view of any kind. Let B be an array with owned
storage (either Array or ArcArray). Let C be an array with mutable
data (either Array, ArcArray or ArrayViewMut). The following
combinations of operands are supported for an arbitrary binary
operator denoted by @ (it can be +, -, *, / and so on).
- &A @ &A which produces a new Array
- B @ A which consumes B, updates it with the result, and returns it
- B @ &A which consumes B, updates it with the result, and returns it
- C @= &A which performs an arithmetic operation in place
You can use slicing to create a view of a subset of the data in the
array. Slicing methods include .slice(), .slice_mut(), .slice_move(),
and .slice_collapse().
所以你想用&x.slice(s![1..]) - &x.slice(s![..-1])
(注意两个&
)。
我觉得这段代码应该可以工作:
use ndarray::{Array1, s}; // 0.15.4
fn main() {
let x = Array1::<f64>::range(0.0, 10.0, 1.0);
println!("{:?}", x);
println!("{:?}", x.slice(s![1..]));
println!("{:?}", x.slice(s![..-1]));
println!("{:?}", x.slice(s![1..]) - x.slice(s![..-1])); // fails to compile when this line is uncommented
}
如果我评论最后的印刷品,前 3 行印刷品确实有效。我在这里遗漏了什么吗?
Rust Playground Implementation
documentation 解释说:
Let A be an array or view of any kind. Let B be an array with owned storage (either Array or ArcArray). Let C be an array with mutable data (either Array, ArcArray or ArrayViewMut). The following combinations of operands are supported for an arbitrary binary operator denoted by @ (it can be +, -, *, / and so on).
- &A @ &A which produces a new Array
- B @ A which consumes B, updates it with the result, and returns it
- B @ &A which consumes B, updates it with the result, and returns it
- C @= &A which performs an arithmetic operation in place
You can use slicing to create a view of a subset of the data in the array. Slicing methods include .slice(), .slice_mut(), .slice_move(), and .slice_collapse().
所以你想用&x.slice(s![1..]) - &x.slice(s![..-1])
(注意两个&
)。