-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathroots_mex.cpp
28 lines (26 loc) · 939 Bytes
/
roots_mex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "mex.h"
#include "Polynomial/Polynomial.hpp"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
if ( nlhs != 1 || nrhs != 1 )
{
mexErrMsgTxt("usage: roots = roots_mex(coeffs)");
}
int m = mxGetM(prhs[0]);
int n = mxGetN(prhs[0]);
if ( m != 1 )
{
mexErrMsgTxt("input must be of size [1 n+1] for polynomial of degree n");
}
double lb = mxGetScalar(prhs[1]);
double ub = mxGetScalar(prhs[2]);
double *coeffs = (double*)mxGetData(prhs[0]);
Eigen::VectorXd coef(n);
for ( int i = 0; i < n; i++ ) coef[i] = coeffs[i];
Polynomial::Polynomial<Eigen::Dynamic> poly(coef);
std::vector<double> real_roots;
poly.realRoots(real_roots);
plhs[0] = mxCreateDoubleMatrix(1,real_roots.size(),mxREAL);
double *roots = (double*)mxGetData(plhs[0]);
for ( int i = 0; i < real_roots.size(); i++ ) roots[i] = real_roots[i];
}