Re: [eigen] Efficiently creating a matrix of pairwise vector differences. |

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

*To*: eigen <eigen@xxxxxxxxxxxxxxxxxxx>*Subject*: Re: [eigen] Efficiently creating a matrix of pairwise vector differences.*From*: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>*Date*: Fri, 22 Dec 2017 09:12:02 +0100*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=i1DWYLCs9uev1IXFQCfyGAC+2LYbsHmBjU8RM1SCbK8=; b=f2238lOEZs6UVwEwTqW65JKz6MQEUY2ZzBNHcblUzGXajLmCx6b1No1n+ZOzchKw05 ocLCPlLyrN/dZtMBUxgWIJLWpjv61BEp9z76ua2eYmA/cNAFcWrUzk2oAxtHPhsQV5UF qJoDJde6Hi766HBUCOfXJobCxtC1VdHbkYNUgrMduJhlUIQ1+kUIvhd5mx2mD9cRO7yz ODb0SqQPaRUK/fjKGL3bE4pcS89V/vpxwtwEFgP3nJyIoyrt42FoH4GpeSS9bT+QWsA2 gAwKRhuJ2PIc69SWpbsLBTWVGaaoytTZ31a0izNk8Wh5DhYgwx60WpBr4sCIs102TjK2 oNzQ==

If I got your question right, you end up with a NxN symmetric matrix, so a 250k x 250k matrix, so about 500GB.... this does not sound right. Perhaps you want to compute the differences between the column vectors? or you inverted N and M? Anyway, for that task you can exploit that:

with something like:

VectorXd D2 = data.rowwise().squaredNorm();

MatrixXd dist = D2.rowwise().replicate(N) + D2.transpose().colwise().replicate(N);

dist -= 2.*data.transpose()*data;

that will be considerably faster.

gael

On Thu, Dec 21, 2017 at 12:10 AM, Smith, Louis <Louis_Smith@xxxxxxxxxxxxxxxxxx> wrote:

Hello,

I'm trying to use eigen to compute the distances between m length vectors i and j which are each rows in an NxM matrix (note that M is often much much smaller than N. In my test case N is about 250,000 and M is 6). What I'm currently working with is an _expression_ like:

Matrix Xd data = "" //This works when data is written to cout, so elided.

MatrixXd distances = (data.rowwise() - data.transpose().colwise().

transpose()).norm();

Which gives me the following error:

error: no member named 'transpose' in 'Eigen::VectorwiseOp<Eigen::

Transpose<Eigen::Matrix< double, -1, -1, 0, -1, -1> >, 0>'

MatrixXd distances = (data.rowwise() - data.transpose().colwise().transpose()).norm();

When I get rid of the third transpose I'm then subtracting a column vector from a row vector, which also doesn't work. Calling transpose on the row vector gives a similar error.

What I expect distances to be is an NxN symmetric matrix containing the distances (norms of difference vectors) for each pair of row vectors in data.

Sorry for the newbie question, but I'd really appreciate some insight on this since it seems like there should be a more eigeny way to write this than the double-for loop over the data, which also works but is very slow.

Regards,

Louis

**References**:**[eigen] Efficiently creating a matrix of pairwise vector differences.***From:*Smith, Louis

**Messages sorted by:**[ date | thread ]- Prev by Date:
**[eigen] SparseMatrix StorageIndex issue** - Next by Date:
**Re: [eigen] Efficiently creating a matrix of pairwise vector differences.** - Previous by thread:
**[eigen] Efficiently creating a matrix of pairwise vector differences.** - Next by thread:
**Re: [eigen] Efficiently creating a matrix of pairwise vector differences.**

Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |