У меня есть ndarray 2-мерный массив, и я пытаюсь умножить строку на то же число, как указано здесь .
Он отлично работает, когда я работаю с2-мерный массив f64
, но я хочу, чтобы он был универсальным для всех типов чисел с плавающей точкой.Вот почему я использую Float
от num-черт.Однако он больше не компилируется.
use ndarray::{Array2, Axis};
fn gauss(pivot: [usize; 2], table: &mut Array2<f64>) {
for i in 0..table.len_of(Axis(0)) {
if i != pivot[0] {
let pivot_n = table[pivot];
let make_zero = table[[i, pivot[1]]];
let mut row_pivot = table.row(pivot[0]).to_owned();
let mut row_make_zero = table.row_mut(i);
row_make_zero *= pivot_n;
row_pivot *= -make_zero;
row_make_zero += &row_pivot;
}
}
}
use num::Float;
use std::ops::MulAssign;
fn gauss_2<T: Float + MulAssign>(pivot: [usize; 2], table: &mut Array2<T>) {
for i in 0..table.len_of(Axis(0)) {
if i != pivot[0] {
let pivot_n = table[pivot];
let make_zero = table[[i, pivot[1]]];
let mut row_pivot = table.row(pivot[0]).to_owned();
let mut row_make_zero = table.row_mut(i);
row_make_zero *= pivot_n;
row_pivot *= -make_zero;
row_make_zero += &row_pivot;
}
}
}
Ошибка, которую он показывает:
error[E0308]: mismatched types
--> src/lib.rs:27:30
|
27 | row_make_zero *= pivot_n;
| ^^^^^^^ expected reference, found type parameter
|
= note: expected type `&ndarray::ArrayBase<_, _>`
found type `T`
error[E0308]: mismatched types
--> src/lib.rs:28:26
|
28 | row_pivot *= -make_zero;
| ^^^^^^^^^^ expected reference, found type parameter
|
= note: expected type `&ndarray::ArrayBase<_, _>`
found type `T`
error[E0368]: binary assignment operation `+=` cannot be applied to type `ndarray::ArrayBase<ndarray::ViewRepr<&mut T>, ndarray::dimension::dim::Dim<[usize; 1]>>`
--> src/lib.rs:29:13
|
29 | row_make_zero += &row_pivot;
| -------------^^^^^^^^^^^^^^
| |
| cannot use `+=` on type `ndarray::ArrayBase<ndarray::ViewRepr<&mut T>, ndarray::dimension::dim::Dim<[usize; 1]>>`
|
= note: an implementation of `std::ops::AddAssign` might be missing for `ndarray::ArrayBase<ndarray::ViewRepr<&mut T>, ndarray::dimension::dim::Dim<[usize; 1]>>`