# Eigen3 migration

**URL:** <https://discuss.avogadro.cc/t/eigen3-migration/2192>\
**Category:** Development\
**Created:** [March 8, 2013, 11:57am UTC](https://discuss.avogadro.cc/t/eigen3-migration/2192 "2013-03-08T11:57:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirill\_Okhotnikov](https://discuss.avogadro.cc/letter_avatar_proxy/v4/letter/k/bbe5ce/32.png) [@Kirill\_Okhotnikov](https://discuss.avogadro.cc/u/Kirill_Okhotnikov)\
**Post date:** [March 8, 2013, 11:57am UTC](https://discuss.avogadro.cc/t/eigen3-migration/2192/1 "2013-03-08T11:57:54Z")

</div>

Dear Avogadro developers,

I want to use Avogadro in my project to visualize molecules and crystal  
structures. In my project, I’m using Eigen3 library, so I decide to get  
into Avogadro code and change it to support Eigen3 API.

I did step-by-step migration, like described here.  
[http://eigen.tuxfamily.org/dox/Eigen2SupportModes.html](http://eigen.tuxfamily.org/dox/Eigen2SupportModes.html) and came to stage  
EIGEN2\_SUPPORT\_STAGE40\_FULL\_EIGEN3\_STRICTNESS, meaning that new code  
should follow 100% Eigen3 API, but in old code still function  
compatible with eigen2 exists (“eigen2\_” prefix)

I have only one part of code, which is not working like before.  
computeGeomInfo() in molecule.cpp

In Eigen3 library LeastSquares module was excluded, so I should write  
the code for the fitting by myself. The investigation of the code showed  
one problematic part.

void Molecule::computeGeomInfo() const  
{  
…  
d-\>center.setZero();  
… //some code not affecting d-\>center variable

```
 // compute radius and the farthest atom
 d->radius = std::numeric_limits<double>::min();
 foreach (Atom *atom, m_atomList) {
   double distanceToCenter = (*atom->pos() -

```

d-\>center).squaredNorm(); //!!! center is Zero  
if(distanceToCenter \> d-\>radius) {  
d-\>radius = distanceToCenter;  
d-\>farthestAtom = atom;  
}  
}  
d-\>radius = sqrt(d-\>radius);

```
     ...... // Calculation of the center after

     // Calculate the center of the molecule too
     foreach (Atom *atom, m_atomList) {
       Vector3d *pos = &(*m_atomPos)[atom->id()];
       d->center += *pos;
       atomPositions[i++] = pos;
     }
     d->center /= static_cast<double>(nAtoms);

     ...

```

I just want to ask some questions.

1. Are you interesting in the contribution (eigen2-\>eigen3).
2. What should I do with geometry info procedure. Should I change the  
radius calculations.

Best regards,  
Kirill.

---

<div class="post-metadata">

**Author:** ![mhanwell](https://discuss.avogadro.cc/user_avatar/discuss.avogadro.cc/mhanwell/32/16_2.png) [@mhanwell](https://discuss.avogadro.cc/u/mhanwell)\
**Post date:** [March 12, 2013, 9:13pm UTC](https://discuss.avogadro.cc/t/eigen3-migration/2192/2 "2013-03-12T21:13:33Z")

</div>

On Fri, Mar 8, 2013 at 6:57 AM, Kirill Okhotnikov  
[kirill.okhotnikov@gmail.com](mailto:kirill.okhotnikov@gmail.com) wrote:

> Dear Avogadro developers,
> 
> I want to use Avogadro in my project to visualize molecules and crystal  
> structures. In my project, I’m using Eigen3 library, so I decide to get  
> into Avogadro code and change it to support Eigen3 API.
> 
> I did step-by-step migration, like described here.  
> [http://eigen.tuxfamily.org/dox/Eigen2SupportModes.html](http://eigen.tuxfamily.org/dox/Eigen2SupportModes.html) and came to stage  
> EIGEN2\_SUPPORT\_STAGE40\_FULL\_EIGEN3\_STRICTNESS, meaning that new code  
> should follow 100% Eigen3 API, but in old code still function  
> compatible with eigen2 exists (“eigen2\_” prefix)
> 
> I have only one part of code, which is not working like before.  
> computeGeomInfo() in molecule.cpp
> 
> In Eigen3 library LeastSquares module was excluded, so I should write  
> the code for the fitting by myself. The investigation of the code showed  
> one problematic part.

Is there a topic branch somewhere? That code is pretty old, and could  
be rewritten. I haven’t used the porting API but it would be nice to  
move to Eigen 3 if it is not too difficult, the Avogadro 2 code is  
using Eigen 3 too, along with most new code bases. Seeing the impact  
of the change would be nice as I haven’t tried porting Avogadro.

Thanks,

Marcus

---

<div class="post-metadata">

**Author:** ![Benoit\_Jacob1](https://discuss.avogadro.cc/letter_avatar_proxy/v4/letter/b/b5ac83/32.png) [@Benoit\_Jacob1](https://discuss.avogadro.cc/u/Benoit_Jacob1)\
**Post date:** [March 18, 2013, 6:45pm UTC](https://discuss.avogadro.cc/t/eigen3-migration/2192/3 "2013-03-18T18:45:40Z")

</div>

Hi,

Glad that you found the Eigen2-\>Eigen3 porting path useful. It’s true that  
Eigen2’s linear regression functions were removed in Eigen3. To complete  
this porting, you have to express these mathematical operations directly in  
terms of linear solving. You can easily do least squares solving with  
Eigen3 using a SVD or QR decomposition (  
[http://eigen.tuxfamily.org/dox/TutorialLinearAlgebra.html](http://eigen.tuxfamily.org/dox/TutorialLinearAlgebra.html) ). I don’t  
actually remember what particular kind of linear regression the Eigen2 code  
here was doing, but I remember that all what we wanted to do here was find  
the plane best fitting the molecule and the function it used,  
fitHyperplane, is explained there,  
[http://eigen.tuxfamily.org/dox-2.0/group\_\_LeastSquares\_\_Module.html](http://eigen.tuxfamily.org/dox-2.0/group __LeastSquares__ Module.html)  
and you can also look at its source code in Eigen2 and easily adapt that to  
Eigen3.

Benoit

2013/3/8 Kirill Okhotnikov [kirill.okhotnikov@gmail.com](mailto:kirill.okhotnikov@gmail.com)

> Dear Avogadro developers,
> 
> I want to use Avogadro in my project to visualize molecules and crystal  
> structures. In my project, I’m using Eigen3 library, so I decide to get  
> into Avogadro code and change it to support Eigen3 API.
> 
> I did step-by-step migration, like described here.  
> [http://eigen.tuxfamily.org/dox/Eigen2SupportModes.html](http://eigen.tuxfamily.org/dox/Eigen2SupportModes.html) and came to stage  
> EIGEN2\_SUPPORT\_STAGE40\_FULL\_EIGEN3\_STRICTNESS, meaning that new code  
> should follow 100% Eigen3 API, but in old code still function  
> compatible with eigen2 exists (“eigen2\_” prefix)
> 
> I have only one part of code, which is not working like before.  
> computeGeomInfo() in molecule.cpp
> 
> In Eigen3 library LeastSquares module was excluded, so I should write  
> the code for the fitting by myself. The investigation of the code showed  
> one problematic part.
> 
> void Molecule::computeGeomInfo() const  
> {  
> …  
> d-\>center.setZero();  
> … //some code not affecting d-\>center variable
> 
> ```
> // compute radius and the farthest atom
> d->radius = std::numeric_limits<double>::min();
> foreach (Atom *atom, m_atomList) {
> double distanceToCenter = (*atom->pos() -
> 
> ```
> 
> d-\>center).squaredNorm(); //!!! center is Zero  
> if(distanceToCenter \> d-\>radius) {  
> d-\>radius = distanceToCenter;  
> d-\>farthestAtom = atom;  
> }  
> }  
> d-\>radius = sqrt(d-\>radius);
> 
> ```
> ...... // Calculation of the center after
> 
> // Calculate the center of the molecule too
> foreach (Atom *atom, m_atomList) {
> Vector3d *pos = &(*m_atomPos)[atom->id()];
> d->center += *pos;
> atomPositions[i++] = pos;
> }
> d->center /= static_cast<double>(nAtoms);
> 
> ...
> 
> ```
> 
> I just want to ask some questions.
> 
> 1. Are you interesting in the contribution (eigen2-\>eigen3).
> 2. What should I do with geometry info procedure. Should I change the  
> radius calculations.
> 
> Best regards,  
> Kirill.
> 
> * * *
> 
> Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester  
> Wave™: Endpoint Security, Q1 2013 and “remains a good choice” in the  
> endpoint security space. For insight on selecting the right partner to  
> tackle endpoint security challenges, access the full report.  
> [http://p.sf.net/sfu/symantec-dev2dev](http://p.sf.net/sfu/symantec-dev2dev)
> 
> * * *
> 
> Avogadro-devel mailing list  
> [Avogadro-devel@lists.sourceforge.net](mailto:Avogadro-devel@lists.sourceforge.net)  
> [avogadro-devel List Signup and Options](https://lists.sourceforge.net/lists/listinfo/avogadro-devel)
