# Calling an external C++ function

**URL:** <https://discourse.mc-stan.org/t/calling-an-external-c-function/31785>\
**Category:** General\
**Tags:** rstan\
**Created:** [June 18, 2023, 5:09pm UTC](https://discourse.mc-stan.org/t/calling-an-external-c-function/31785 "2023-06-18T17:09:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apteroti](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/apteroti/32/16513_2.png) [@apteroti](https://discourse.mc-stan.org/u/apteroti)\
**Post date:** [June 18, 2023, 5:09pm UTC](https://discourse.mc-stan.org/t/calling-an-external-c-function/31785/1 "2023-06-18T17:09:21Z")

</div>

I’m trying to calculate the eigenvalues of a NON-symmetric matrix. I’m using rstan, which doesn’t have access to whatever is the latest version of stan with the eigenvalues function. So apparently, I have to write my own C++ function to do a simple eigendecomposition and call that function from stan. There is no damn information about stan’s C++ library. Is stan c++ a header only? If not, what are the dependencies? How can I compile it? My eigendecomposition function is 1 line of code using the Eigen library (TuxFamily). This is the same library that Stan uses for doing math operations. Can I pass my function to rstan without compiling stan c++? rstan’s stan version is 2.26, shall I go and unearth the Neolithic stan 2.26 c++ headers and whatnot? or the newest version will do?

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [June 18, 2023, 9:08pm UTC](https://discourse.mc-stan.org/t/calling-an-external-c-function/31785/2 "2023-06-18T21:08:15Z")

</div>

Your best bet is probably to use the experimental versions of RStan which can be installed from source, since even wiring up a C++ implementation of the eigenvalues function won’t get you too far since they are complex numbers for non-symmetric cases

---

<div class="post-metadata">

**Author:** ![apteroti](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/apteroti/32/16513_2.png) [@apteroti](https://discourse.mc-stan.org/u/apteroti)\
**Post date:** [June 18, 2023, 10:19pm UTC](https://discourse.mc-stan.org/t/calling-an-external-c-function/31785/3 "2023-06-18T22:19:16Z")

</div>

Thanks for the suggestion. First I’ll try pystan, if the stan version is archaic in Python as well, then I’ll try the experimental one. My matrix is a square positive matrix (some variation of Markov matrix) it won’t give me complex values upon decomposition, but if it is going to be a pain in the neck, I’ll give the experimental a shot.
