Implementation of Mahalanobis distances using Eigen
Is it effecient

Imposable to tell as we have no idea how stuff in Eigen is implemented.

Comments on code:

There should be some logic in the ordering of includes. So you can easily find ones that have been included or spot missing ones (or know where to put knew ones).

#include &lt;iostream&gt;
#include &lt;Eigen/Dense&gt;
#include &lt;fstream&gt;
#include &lt;Eigen/Cholesky&gt;


This set seems to be completely random. Personally (though the exact orders does not matter and you can pick your own scheme) I put them in groups (so in this case there would be an Eigen group and a standard lib group). Then I order the groups most specific to most general. Within the groups I am less pedantic but usually it is alphabetical unless there is some good reason to use another scheme.

#include &lt;Eigen/Cholesky&gt;
#include &lt;Eigen/Dense&gt;

#include &lt;iostream&gt;
#include &lt;fstream&gt;     // I usually group all the stream stuff together
                       // Then all containers then all the others in the standard lib.
                       // 


Avoid the using-directive. In non trivial programs it causes more problems than it is worth. For short programs the extra cost of prefixing objects (in terms of typing) is negligible so prefix your types and objects (don't be lazy). (I always use std::cout not cout). If you feel this is too much work then selectively bring types/objects into the current namespace with a using statement.

using namespace Eigen;
using namespace std;



Prefer to prefix types/objects with their namespace
If you must selectively bring types/objects into current namespace

using std::cout;
using std::endl;


If you must do this then scope the using to minimize polution.

Try to not use using namespace X;


Does x change in the following code? If not then pass by const reference to indicate that it is not modified.

VectorXd mah(MatrixXd&amp; x){


One variable per line:

 int p = x.cols(), n=x.rows();


This is a nasty trick to temporarily comment out blocks.

/*  ofstream file("test1.txt");
    if (file.is_open()){
            file &lt;&lt;  m &lt;&lt; '\n';
    }
/**/


It is so easily broken that I would avoid it.
If you want to comment out a block then use #if 0

// To uncomment block just replace 0 with 1.
#if 0
    ofstream file("test1.txt");
    if (file.is_open()){
            file &lt;&lt;  m &lt;&lt; '\n';
    }
#endif