How to calculate standard deviation using nalgebra?

I have some python code using numpy, and I want to rewrite them into rust, one line of code is np.std(arr, axis=0), how can i calculate this in nalgebra?

There's Matrix::variance, which you can take the square root of to get standard deviation.

1 Like

but the problem is I have a Dvector<Dmatrix>, I want to do a np.std(arr, axis=0) on it, which is very easy in python, the variance method not working on it.

There is DMatrix in nalgebra::base - Rust and DMatrix in nalgebra::base - Rust

You have a vector of matricies? What is arr.ndim? nalgebra is not well suited for higher dimensional tensors AFAIK. You might be better of with ndarray which has var_axis and std_axis

thank you!
my arr is around (640, 512) 2D array, I choose nalgebra simply because people saying nalgebra is faster and more skilled than ndarray, I will check ndarray and related methods.

Is there a reason have DVector<DMatrix> instead of just a DMatrix or was this a typo?

If you have maximally 2 dimensional arrays nalgebra is IMO a bit more ergonomic than ndarray especially for linear algebra operations.

But be aware that rust will generally be more verbose and less ergonomic than python with numpy.

actually original python code was np.array([arr1, arr2]), so I convert it to Dvector, I am dealing this level at most, 2D or list of 2D.
Yes rust is much faster than python, but numpy is written in very fast fortan, I am not sure nalgebra will be faster than it, so I choose the fastest library in rust.
At beginning I was doing converting using cpp and numcpp, but it's too painful to write cpp when you get familiar with rust :laughing:, I tend to rewrite it in rust.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.