Rust 在二维数组中赋值
我正在用Rust编写一些代码(主要是作为POC)。该代码获取一个2D数组,将其传递给第二个函数进行矩阵运算(我知道有一个标准库来做这项工作,但我想习惯于如何工作) 问题在于,对二维阵列的指定会导致问题 我的代码如下所示Rust 在二维数组中赋值,rust,Rust,我正在用Rust编写一些代码(主要是作为POC)。该代码获取一个2D数组,将其传递给第二个函数进行矩阵运算(我知道有一个标准库来做这项工作,但我想习惯于如何工作) 问题在于,对二维阵列的指定会导致问题 我的代码如下所示 fn main() { // first create a couple of arrays - these will be used // for the vectors let line1: [i32; 4] = [4, 2, 3, 3];
fn main()
{
// first create a couple of arrays - these will be used
// for the vectors
let line1: [i32; 4] = [4, 2, 3, 3];
let line2: [i32; 4] = [3, 4, 5, 7];
let line3: [i32; 4] = [2, 9, 6, 2];
let line4: [i32; 4] = [5, 7, 2, 4];
// create two holding arrays and assign
let array_one = [line1, line3, line4, line2];
let array_two = [line2, line1, line3, line4];
// let's do the multiply
let result = matrix_multiply(&array_one, &array_two);
println!("{:?}", result);
}
fn matrix_multiply(vec1:&[&[i32;4];4], vec2:&[&[i32;4];4]) -> [[i32; 4];4]
{
// we need to deference the parameters passed in
let vec_one:[[i32;4];4] = vec1;
let vec_two:[[i32;4];4] = vec2;
// we need to store the sum
let mut sum = 0;
// we need to create the arrays to put the results into
let mut result = [[0i32; 4]; 4];
// loop through the two vectors
for vone in 0..4
{
for vtwo in 0..4
{
for k in 0..4
{
sum = sum + vec1[[vone].k] * vec2[[k].vtwo];
}
result[[vec_one].vec_two] = sum;
sum = 0;
}
}
return result;
}
我也尝试过result[vec_one][vec_two]=sum
,但在编译时,似乎分配给数组时出现了问题
我做错了什么?我相信这是你的错误(至少有一个错误):
)
我还根据当前的社区实践(大括号定位、间距等),使您的代码更加地道,并修复了访问数组的奇怪语法。如果您编写
let line1: [i32; 4] = [4, 2, 3, 3];
let line2: [i32; 4] = [3, 4, 5, 7];
let line3: [i32; 4] = [2, 9, 6, 2];
let line4: [i32; 4] = [5, 7, 2, 4];
// create two holding arrays and assign
let array_one = [line1, line3, line4, line2];
array\u one
的类型将是[[i32;4];4]
,因为行式数组被复制到array\u one
中。通过&array\u one
借用这个函数,您可以得到&[[i32;4];4]
类型的东西,这与&[&[T;4];4]
类型的东西(您尝试调用的函数所期望的)非常不同
选项1——创建[&[T;4];4]
:
let array_one = [&line1, &line3, &line4, &line2];
some_function(&array_one);
...
fn some_function(matrix: &[&[i32;4];4]) {...}
选项2——更改功能性质:
let array_one = [line1, line3, line4, line2];
some_function(&array_one);
...
fn some_function(matrix: &[[i32;4];4]) {...}
如果你对处理任意大小的多维数组感兴趣,也许你会发现我的实验很有用。它基本上尝试为两个或更多维度提供类似于Box
、&[T]
和&mut[T]
的类型。下面是一个未经测试的示例,只是为了了解我的想法:
extern crate multiarray;
use multiarray::*;
fn main() {
// the backing memory will be a dynamically allocated
// linear array with 4 elements in row-major (C-style) order.
let mut matrix = Array2D::new([2, 2], 0i32);
matrix[[0,0]] = 1; matrix[[0,1]] = 2;
matrix[[1,0]] = 3; matrix[[1,1]] = 4;
let mut square = Array2D::new([2, 2], 0i32);
// the borrow methods create reference-like proxies
mat_product(matrix.borrow(), matrix.borrow(),
square.borrow_mut());
}
fn mat_product(a: Array2DRef<i32>, b: Array2DRef<i32>,
mut c: Array2DRefMut<i32>) {
let rows = a.extents()[0]; // extent of 1st dimension
let intr = a.extents()[1]; // extent of 2nd dimension
let cols = b.extents()[1]; // extent of 2nd dimension
assert!(intr == b.extents()[0]);
assert!(rows == c.extents()[0]);
assert!(cols == c.extents()[1]);
for i in 0..rows {
// the i-th row of a and c...
let a_row_i = a.eliminated_dim(0, i);
let mut c_row_i = c.reborrow_mut().eliminated_dim(0, i);
for j in 0..cols {
c_row_i[j] = dot_product(a_row_i, b.eliminated_dim(1, j));
// ^^^j-th column of b^^^
}
}
}
fn dot_product(a: Array1DRef<i32>, b: Array1DRef<i32>) -> i32 {
a.zip(b).fold(0, |acc,(&x,&y)| acc + x * y)
}
extern板条箱多阵列;
使用多数组::*;
fn main(){
//备份内存将是动态分配的
//具有4个元素的行主(C样式)顺序的线性阵列。
让mut-matrix=Array2D::new([2,2],0i32);
矩阵[[0,0]]=1;矩阵[[0,1]]=2;
矩阵[[1,0]]=3;矩阵[[1,1]]=4;
让mut square=Array2D::new([2,2],0i32);
//借用方法创建类似引用的代理
mat_乘积(matrix.borrow(),matrix.borrow(),
正方形。借用_mut());
}
fn mat_乘积(a:Array2DRef,b:Array2DRef,
mut c:Array2DRefMut){
设rows=a.extensts()[0];//第一维度的范围
设intr=a.extensts()[1];//第二维的范围
设cols=b.extensts()[1];//第二维的范围
断言!(intr==b.extensts()[0]);
断言!(行==c.extensts()[0]);
断言!(cols==c.extensts()[1]);
对于0..行中的i{
//a和c的第i行。。。
设a_row_i=a.dim(0,i);
让mut c_row_i=c.reborrow_mut().employed_dim(0,i);
对于0..cols中的j{
c_row_i[j]=点积(a_row_i,b.dim(1,j));
//^^^^b的第j列^^^
}
}
}
fn点积(a:Array1DRef,b:Array1DRef)->i32{
a、 拉链(b)。折叠(0,| acc,(&x,&y)| acc+x*y)
}
在问题中包含错误信息。感谢您给出如此详细的解释。我喜欢生锈:)
extern crate multiarray;
use multiarray::*;
fn main() {
// the backing memory will be a dynamically allocated
// linear array with 4 elements in row-major (C-style) order.
let mut matrix = Array2D::new([2, 2], 0i32);
matrix[[0,0]] = 1; matrix[[0,1]] = 2;
matrix[[1,0]] = 3; matrix[[1,1]] = 4;
let mut square = Array2D::new([2, 2], 0i32);
// the borrow methods create reference-like proxies
mat_product(matrix.borrow(), matrix.borrow(),
square.borrow_mut());
}
fn mat_product(a: Array2DRef<i32>, b: Array2DRef<i32>,
mut c: Array2DRefMut<i32>) {
let rows = a.extents()[0]; // extent of 1st dimension
let intr = a.extents()[1]; // extent of 2nd dimension
let cols = b.extents()[1]; // extent of 2nd dimension
assert!(intr == b.extents()[0]);
assert!(rows == c.extents()[0]);
assert!(cols == c.extents()[1]);
for i in 0..rows {
// the i-th row of a and c...
let a_row_i = a.eliminated_dim(0, i);
let mut c_row_i = c.reborrow_mut().eliminated_dim(0, i);
for j in 0..cols {
c_row_i[j] = dot_product(a_row_i, b.eliminated_dim(1, j));
// ^^^j-th column of b^^^
}
}
}
fn dot_product(a: Array1DRef<i32>, b: Array1DRef<i32>) -> i32 {
a.zip(b).fold(0, |acc,(&x,&y)| acc + x * y)
}