I would like to pass a Vector -> f64
function to some function go
, but I run into troubles.
fn go<D, S, F>(f: F)
where D: Dim,
S: Storage<f64, D>,
F: Fn(&Vector<f64, D, S>) -> f64 {
let n = 4;
let m = DMatrix::<f64>::new_random(n,n);
println!("{:8.4}", f(&m.column(0)));
}
fn norm_square<D: Dim, S: Storage<f64, D>>(x: &Vector<f64, D, S>) -> f64 {
x.iter().fold(0., |b, xi| b + xi.powi(2))
}
go(norm_square);
Which raises this error:
error[E0283]: type annotations required: cannot resolve `_: nalgebra::base::dimension::Dim`
| go(norm_square);
| ^^
note: required by `go`
| / fn go<D, S, F>(f: F)
| | where D: Dim,
| | S: Storage<f64, D>,
| | F: Fn(&Vector<f64, D, S>) -> f64 {
...
| | println!("{:8.4}", f(&m.column(0)));
| | }
| |_____^
error[E0308]: mismatched types
| println!("{:8.4}", f(&m.column(0)));
| ^^^^^^^^^^^^ expected type parameter, found struct `nalgebra::base::dimension::Dynamic`
|
= note: expected type `&nalgebra::base::matrix::Matrix<f64, D, nalgebra::base::dimension::U1, S>`
found type `&nalgebra::base::matrix::Matrix<f64, nalgebra::base::dimension::Dynamic, nalgebra::base::dimension::U1, nalgebra::base::matrix_slice::SliceStorage<'_, f64, nalgebra::base::dimension::Dynamic, nalgebra::base::dimension::U1, nalgebra::base::dimension::U1, nalgebra::base::dimension::Dynamic>>`
I tried specifying parameters for go
but not in a successful way. I’m not even quite sure it could actually work, because of the dynamic dimension. My vectors will all be of the same dimension so I guess it might be possible specify that n
as a type parameter.
I noted that the following works
fn apply<D, S, F>(x: &Vector<f64, D, S>, f: F) -> f64
where D: Dim,
S: Storage<f64, D>,
F: Fn(&Vector<f64, D, S>) -> f64
{
f(x)
}
let n = 4;
let m = DMatrix::<f64>::new_random(n,n);
println!("{}", apply(&m.column(0), norm_square));
I suppose it is because it is able to unify the D for its parameters since they are both specified by the type?