As Yuyao pointed out, there is no standard deviation function built into Eigen (at the moment). You can compute this for a single vector using the following (generally, prefer using Array
if you are working more on element-wise operations):
Eigen::ArrayXd vec;
double std_dev = std::sqrt((vec - vec.mean()).square().sum()/(vec.size()-1));
(Since there was an edit request: Note that for an un-biased estimation of the standard deviation you need to divide by vec.size()-1
: [1])
If you want to compute the column-wise std-dev of a whole array, the following should work:
Eigen::Index N = 20, M = 10;
Eigen::ArrayXd angles = Eigen::ArrayXd::LinSpaced(N, -M_PI/2, M_PI/2);
Eigen::ArrayXXd s1(N, M);
for(Eigen::Index i=0; i< s1.cols(); ++i)
{
s1.col(i) = (i+1)*sin(angles+i);
}
Eigen::Array<double, 1, Eigen::Dynamic> std_dev = ((s1.rowwise() - s1.colwise().mean()).square().colwise().sum()/(M-1)).sqrt();
std::cout << std_dev << "\n\n";
s1.rowwise() /= std_dev;
std::cout << s1 << "\n\n";
vec
represents the entire population (as opposed to a sampled sub-portion of it). This is because when you have the entire population, you'd have an exact calculation of the standard deviation, as opposed to an estimate with various biases that needs correcting. Because the OP doesn't specify either way, both solutions should be mentioned. – Dramatist