13 #include <unsupported/Eigen/Polynomials> 16 while (angle > M_PI) angle -= 2.0 * M_PI;
17 while (angle < -M_PI) angle += 2.0 * M_PI;
23 std::vector<decimal_t> dts;
28 dts.push_back((-c - sqrt(p)) / (2 * b));
29 dts.push_back((-c + sqrt(p)) / (2 * b));
37 std::vector<decimal_t> dts;
45 decimal_t R = (9 * a1 * a2 - 27 * a0 - 2 * a2 * a2 * a2) / 54;
52 dts.push_back(-a2 / 3 + (S + T));
56 dts.push_back(-a2 / 3 + S + S);
57 dts.push_back(-a2 / 3 - S);
60 decimal_t theta = acos(R / sqrt(-Q * Q * Q));
61 dts.push_back(2 * sqrt(-Q) * cos(theta / 3) - a2 / 3);
62 dts.push_back(2 * sqrt(-Q) * cos((theta + 2 * M_PI) / 3) - a2 / 3);
63 dts.push_back(2 * sqrt(-Q) * cos((theta + 4 * M_PI) / 3) - a2 / 3);
71 std::vector<decimal_t> dts;
78 std::vector<decimal_t> ys =
79 cubic(1, -a2, a1 * a3 - 4 * a0, 4 * a2 * a0 - a1 * a1 - a3 * a3 * a0);
86 if (r < 0)
return dts;
91 D = sqrt(0.75 * a3 * a3 - R * R - 2 * a2 +
92 0.25 * (4 * a3 * a2 - 8 * a1 - a3 * a3 * a3) / R);
93 E = sqrt(0.75 * a3 * a3 - R * R - 2 * a2 -
94 0.25 * (4 * a3 * a2 - 8 * a1 - a3 * a3 * a3) / R);
96 D = sqrt(0.75 * a3 * a3 - 2 * a2 + 2 * sqrt(y1 * y1 - 4 * a0));
97 E = sqrt(0.75 * a3 * a3 - 2 * a2 - 2 * sqrt(y1 * y1 - 4 * a0));
100 if (!std::isnan(D)) {
101 dts.push_back(-a3 / 4 + R / 2 + D / 2);
102 dts.push_back(-a3 / 4 + R / 2 - D / 2);
104 if (!std::isnan(E)) {
105 dts.push_back(-a3 / 4 - R / 2 + E / 2);
106 dts.push_back(-a3 / 4 - R / 2 - E / 2);
119 std::vector<decimal_t> ts;
123 return cubic(b, c, d, e);
125 return quad(c, d, e);
127 ts.push_back(-e / d);
136 std::vector<decimal_t> ts;
138 return solve(b, c, d, e, f);
140 Eigen::VectorXd coeff(6);
141 coeff << f, e, d, c, b, a;
142 Eigen::PolynomialSolver<double, 5> solver;
143 solver.compute(coeff);
145 const Eigen::PolynomialSolver<double, 5>::RootsType &r = solver.roots();
146 std::vector<decimal_t> ts;
148 for (
int i = 0; i < r.rows(); ++i) {
149 if (r[i].imag() == 0) {
151 ts.push_back(r[i].real());
163 std::vector<decimal_t> ts;
164 if (a == 0 && b == 0)
165 return solve(c, d, e, f, g);
167 Eigen::VectorXd coeff(7);
168 coeff << g, f, e, d, c, b, a;
169 Eigen::PolynomialSolver<double, 6> solver;
170 solver.compute(coeff);
172 const Eigen::PolynomialSolver<double, 6>::RootsType &r = solver.roots();
173 std::vector<decimal_t> ts;
175 for (
int i = 0; i < r.rows(); ++i) {
176 if (r[i].imag() == 0) {
178 ts.push_back(r[i].real());
207 template <
typename Derived>
208 typename Derived::PlainObject pseudoInverse(
209 Eigen::MatrixBase<Derived>
const &m) {
212 constexpr
auto flags = (Derived::ColsAtCompileTime == Eigen::Dynamic)
213 ? (Eigen::ComputeThinU | Eigen::ComputeThinV)
214 : (Eigen::ComputeFullU | Eigen::ComputeFullV);
215 Eigen::JacobiSVD<typename Derived::PlainObject> m_svd(m, flags);
218 return m_svd.solve(Derived::PlainObject::Identity(m.rows(), m.rows()));
221 template <
typename Derived>
222 typename Derived::PlainObject matrixSquareRoot(
223 Eigen::MatrixBase<Derived>
const &mat,
bool semidefinite_mat =
false) {
224 if (!semidefinite_mat) {
225 Eigen::LLT<typename Derived::PlainObject> cov_chol{mat};
226 if (cov_chol.info() == Eigen::Success)
return cov_chol.matrixL();
228 Eigen::LDLT<typename Derived::PlainObject> cov_chol{mat};
229 if (cov_chol.info() == Eigen::Success) {
230 typename Derived::PlainObject
const L = cov_chol.matrixL();
231 auto const P = cov_chol.transpositionsP();
232 auto const D_sqrt = cov_chol.vectorD().array().sqrt().matrix().asDiagonal();
233 return P.transpose() * L * D_sqrt;
235 return Derived::PlainObject::Zero(mat.rows(), mat.cols());
std::vector< decimal_t > quartic(decimal_t a, decimal_t b, decimal_t c, decimal_t d, decimal_t e)
Quartic equation: .
Definition: math.h:69
decimal_t power(decimal_t t, int n)
Return .
Definition: math.h:197
std::vector< decimal_t > solve(decimal_t a, decimal_t b, decimal_t c, decimal_t d, decimal_t e)
General solver for .
Definition: math.h:117
std::vector< decimal_t > quad(decimal_t b, decimal_t c, decimal_t d)
Quadratic equation: .
Definition: math.h:22
int factorial(int n)
Return .
Definition: math.h:187
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:49
Defines all data types used in this lib.
std::vector< decimal_t > cubic(decimal_t a, decimal_t b, decimal_t c, decimal_t d)
Cubic equation: .
Definition: math.h:35