rust - 如何在 Rust 中访问代数矩阵的各个元素?

标签 rust linear-algebra

我正在尝试访问 Rust 中的代数矩阵的各个元素,但一直出现错误。我也没有在文档中找到任何示例来说明如何访问单个元素——就像我访问一些多维数组一样。

这是我一直在尝试的:

use nalgebra::DMatrix; // 0.21.0

fn main() {
    let b = DMatrix::<f64>::zeros(4, 4);
    println!("{:?}", b[0][1]);
}

当我编译这段代码时,我得到

error[E0608]: cannot index into a value of type `f64`
 --> src/main.rs:5:22
  |
5 |     println!("{:?}", b[0][1]);
  |                      ^^^^^^^

我不确定如何解释此消息,或者我哪里出错了。

最佳答案

检查 the documentation for Matrix::index :

pub fn index<'a, I>(&'a self, index: I) -> I::Output
where
    I: MatrixIndex<'a, N, R, C, S>, 

如果我们看一下 implementers of MatrixIndex ,我们看到许多类型,包括usize ((usize, usize)) 的元组:

println!("{:?}", b[(0, 1)]);

The Rust Programming Language chapter on data types进一步解释元组。

关于rust - 如何在 Rust 中访问代数矩阵的各个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901735/

相关文章:

optimization - 我可以将 "null pointer optimization"用于我自己的非指针类型吗?

c++ - C++/Eigen :compute the orthonormal basis for the null space

rust - Gtk-错误 ** : failed to add UI: Error on line 1 char 1: Document must begin with an element

rust - 结果<bool, T> 模式匹配

string - 将字符串文字与另一个字符串连接起来

python - 为什么 Mathematica 和 Python 在处理奇异矩阵方程时的答案不同?

java - 使用 Jama.Matrix 测试可逆性

unit-testing - 在 Rust 测试中使用外部文件

javascript - 如何从从0,0开始到100,1000结束的指数函数中获取Y值

c++ - C++ 中的高斯消元线性方程