Re: [eigen] using views for symmetric matricies |

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

*To*: eigen <eigen@xxxxxxxxxxxxxxxxxxx>*Subject*: Re: [eigen] using views for symmetric matricies*From*: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>*Date*: Thu, 8 Mar 2018 17:38:18 +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=z4jN013hq3vZRMpULBdTx8gCeIRu15Kn/F36KL1NHiw=; b=oUiRiTDkDtZia5AwUnTXjOALZQyKdl8DRI5UCd42LWF+lK5/bMYvYw1xMdfrjsV/Z5 1YnM+/7MabTRhipp1BOU2P+WZPRcSFyRfgHF76bzvckN/7prdIWQmDGJ0lsMHFksQdg3 v2gSEh9BzvN8zp9B+x8ntQyBeAR4cHmANo/RjtTR4JbEBcMelg2KQGuyWnxDc11QLr9H ercMVbp5KqXXjW6i63Gk00ACJvJnKAlH14c/goaWFfvYnVEXj5ccHOXq9EDROMRKAGlM 1eUyKLJqnIOnBtDpDwczTh7noBKWV5ozlucg++GDj2lgjiL+uiXpWhkmp9ohu+Y30/xr g2Vg==

Hi,

triangularView on the left-hand-side of an assignment works as a writing mask, so the diagonal and upper-left triangular part of m1 is left unchanged. Either write:

or

2 - The line:

is invalid and should give you a compiler error (a static assert seems to be missing in Eigen) because selfadjointView only accept Lower of Upper parameters, no Strictly* nor Unit* stuff. If the diagonal of m1 is already zero, then using Lower is fine.

let me answer a few shortcomings.

1 - In:

m1.triangularView<StrictlyLower >() = m2+m3;

m1.setZero();

m1.triangularView<StrictlyLower >() = m2+m3;

m1 = (m2+m3).triangularView<StrictlyLower >();

m4 = m1.selfadjointView<StrictlyLower >();

I hope these two explanations answer your questions.

Gael.

On Mon, Mar 5, 2018 at 7:39 PM, Smith, Louis <Louis_Smith@xxxxxxxxxxxxxxxxxx> wrote:

Hello,

I have a very basic question about views. Thanks in advance for being understanding, I'm sure this a totally elementary problem that just shows my lack of understanding, but this is the appropriate forum for getting help with this library. Right?*

I'm trying to use views the way I perceive that they are being used in the quick reference to, for example, only compute and store half the entries in a symmetric matrix, then have later operations view the matrix produced in this way as symmetric. I'm working with things this way as a substitute for writing nested for-loops where the inner loop's index is constrained to rolling over only one of the triangles of the array I am either getting compile time or runtime errors for all of it, with code that works if the triangularView and selfadjointView statements I included are removed. The question is how can I use views to only compute and store half of a symmetric matrix, while still using the visitors and reductions that are part of the Matrix class? Following are some examples I tried that do not work.

I use the following function to get a matrix of pairwise distances:

// takes a nxd data matrix, returns an nxn matrix containing pairwise distances// use formula (a - b)^2 = a^2 + b^2 -2a*b.MatrixXd pairwise_dists(const Ref<const MatrixXd>& data){const VectorXd data_sq = data.rowwise().squaredNorm();MatrixXd distances;distances = data_sq.rowwise().replicate(data. rows())+ data_sq.transpose().colwise().replicate (data.rows())- 2..*data*data.transpose();distances.diagonal().setZero(); // prevents nans from occurring along diag.distances = distances.cwiseSqrt();return distances;}I would like to do something like:

// takes a nxd data matrix, returns an nxn matrix containing pairwise distances// use formula (a - b)^2 = a^2 + b^2 -2a*b.MatrixXd pairwise_dists(const Ref<const MatrixXd>& data){const VectorXd data_sq = data.rowwise().squaredNorm();MatrixXd distances;distances.triangularView<StrictlyLower>() = data_sq.rowwise().replicate(data. rows())+ data_sq.transpose().colwise().replicate (data.rows())- 2..*data*data.transpose();distances = distances.cwiseSqrt();return distances;}

And then when I use this function later, for example in a reduction:

VectorXd rho = (-0.5*pairwise_dists(scdata)).array ().exp().rowwise().sum();Would become something like:

VectorXd rho = (-0.5*pairwise_dists(scdata).selfadjointView<StrictlyLower, array().exp().rowZeroDiag>()). wise ().sum();

Adding the triangular view statement to the function 'pairwise_dists' compiles but gives a runtime error whenever the function is called by my code, whereas using the 'selfadjointView' statement I give causes a compile error. Changing the template parameters to only <StrictlyLower> or changing both template parameters to <Lower> do not fix the issue.

Even in a toy example I seem to be having issues:

Matrix3f m0, m1, m2, m3, m4;m2 << 1,2,3,4,5,6,7,8,9;m3 << 1,0,1,3,0,0,0,4,0;

m0 = m2 + m3;m1.triangularView<StrictlyLower >() = m2+m3;cout << m0 << "\nNo triangular view\n";cout << m1 << "\nS. Lower triangular view\n";m4 = m1.selfadjointView<StrictlyLower >(); //+ m3;cout << m4 << "\nself-adjoint lower view\n";The above code will compile and run, but m4 is not a dense symmetric matrix like I'd expect it to be. It has uninitialised values on the diagonal and upper triangle, as though selfadjointView didn't do anything.

If I uncomment the addition operation on the line defining m4, the code no longer compiles.

I hope this is enough information to germinate a response. Thanks in advance for taking the time to read it.

Regards,

Louis

*Sorry if this is not true. It really seems like many of the posts to this mailing list are higher level discussions, comments on use, or bug reports, so if there's a more correct place to mail basic use questions I'd happily direct subsequent questions there.

**Follow-Ups**:**Re: [eigen] using views for symmetric matricies***From:*Christoph Hertzberg

**References**:**[eigen] using views for symmetric matricies***From:*Smith, Louis

**Messages sorted by:**[ date | thread ]- Prev by Date:
**[eigen] Eigen Internals** - Next by Date:
**Re: [eigen] Eigen Internals** - Previous by thread:
**[eigen] using views for symmetric matricies** - Next by thread:
**Re: [eigen] using views for symmetric matricies**

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