| 1 |
#include "polynomialsolver.h" |
| 2 |
#include "mpsolveworker.h" |
| 3 |
#include "root.h" |
| 4 |
#include <QDebug> |
| 5 |
#include <QRegExp> |
| 6 |
#include <QStringList> |
| 7 |
|
| 8 |
#include <QDebug> |
| 9 |
|
| 10 |
static double LOG2_10 = log(10) / log(2); |
| 11 |
|
| 12 |
using namespace xmpsolve; |
| 13 |
|
| 14 |
PolynomialSolver::PolynomialSolver(QObject *parent) : |
| 15 |
QObject(parent) |
| 16 |
{ |
| 17 |
m_mpsContext = NULL; |
| 18 |
m_currentPoly = NULL; |
| 19 |
m_worker.connect(&m_worker, SIGNAL(finished()), |
| 20 |
this, SLOT(workerExited())); |
| 21 |
m_errorMessage = ""; |
| 22 |
} |
| 23 |
|
| 24 |
PolynomialSolver::~PolynomialSolver() |
| 25 |
{ |
| 26 |
if (m_currentPoly) |
| 27 |
mps_polynomial_free (m_mpsContext, m_currentPoly); |
| 28 |
if (m_mpsContext) |
| 29 |
mps_context_free(m_mpsContext); |
| 30 |
} |
| 31 |
|
| 32 |
int |
| 33 |
PolynomialSolver::solvePolFile(QString selectedFile, mps_algorithm selected_algorithm, int required_digits, |
| 34 |
mps_output_goal goal) |
| 35 |
{ |
| 36 |
QByteArray stringData = selectedFile.toLatin1().data(); |
| 37 |
|
| 38 |
m_mpsContext = mps_context_new(); |
| 39 |
m_worker.setMpsContext(m_mpsContext); |
| 40 |
|
| 41 |
// Parse the stream specified by the user |
| 42 |
mps_polynomial * poly = mps_parse_file (m_mpsContext, stringData.data()); |
| 43 |
|
| 44 |
if (mps_context_has_errors (m_mpsContext) || !poly) { |
| 45 |
m_errorMessage = tr("Error while solving the given pol file: %1"). |
| 46 |
arg(mps_context_error_msg(m_mpsContext)); |
| 47 |
mps_context_free (m_mpsContext); |
| 48 |
m_mpsContext = NULL; |
| 49 |
return -1; |
| 50 |
} |
| 51 |
else |
| 52 |
mps_context_set_input_poly (m_mpsContext, poly); |
| 53 |
|
| 54 |
// Select the options selected by the user |
| 55 |
mps_context_select_algorithm(m_mpsContext, selected_algorithm); |
| 56 |
mps_context_set_output_prec(m_mpsContext, required_digits * LOG2_10); |
| 57 |
mps_context_set_output_goal(m_mpsContext, goal); |
| 58 |
|
| 59 |
m_worker.start(); |
| 60 |
return mps_context_get_degree (m_mpsContext); |
| 61 |
} |
| 62 |
|
| 63 |
int |
| 64 |
PolynomialSolver::solvePolFileFromContent(QString content, mps_algorithm selected_algorithm, int required_digits, |
| 65 |
mps_output_goal goal) |
| 66 |
{ |
| 67 |
QByteArray stringData = content.toLatin1().data(); |
| 68 |
|
| 69 |
m_mpsContext = mps_context_new(); |
| 70 |
m_worker.setMpsContext(m_mpsContext); |
| 71 |
|
| 72 |
// Parse the stream specified by the user |
| 73 |
mps_polynomial * poly = mps_parse_string (m_mpsContext, stringData.data()); |
| 74 |
|
| 75 |
if (mps_context_has_errors (m_mpsContext) || !poly) { |
| 76 |
m_errorMessage = tr("Error while solving the given pol file: %1"). |
| 77 |
arg(mps_context_error_msg(m_mpsContext)); |
| 78 |
mps_context_free (m_mpsContext); |
| 79 |
m_mpsContext = NULL; |
| 80 |
return -1; |
| 81 |
} |
| 82 |
else |
| 83 |
mps_context_set_input_poly (m_mpsContext, poly); |
| 84 |
|
| 85 |
// Select the options selected by the user |
| 86 |
mps_context_select_algorithm(m_mpsContext, selected_algorithm); |
| 87 |
mps_context_set_output_prec(m_mpsContext, required_digits * LOG2_10); |
| 88 |
mps_context_set_output_goal(m_mpsContext, goal); |
| 89 |
|
| 90 |
m_worker.start(); |
| 91 |
return mps_context_get_degree (m_mpsContext); |
| 92 |
} |
| 93 |
|
| 94 |
int |
| 95 |
PolynomialSolver::solvePoly(mps_polynomial * poly, PolynomialBasis basis, |
| 96 |
mps_algorithm selected_algorithm, |
| 97 |
int required_digits, mps_output_goal goal) |
| 98 |
{ |
| 99 |
m_currentPoly = poly; |
| 100 |
|
| 101 |
mps_context_set_input_poly(m_mpsContext, MPS_POLYNOMIAL (m_currentPoly)); |
| 102 |
mps_context_select_algorithm(m_mpsContext, selected_algorithm); |
| 103 |
mps_context_set_output_prec(m_mpsContext, required_digits * LOG2_10); |
| 104 |
mps_context_set_output_goal(m_mpsContext, goal); |
| 105 |
|
| 106 |
// One might want to uncomment this for debugging purposes. |
| 107 |
// mps_context_add_debug_domain (m_mpsContext, MPS_DEBUG_TRACE); |
| 108 |
|
| 109 |
m_worker.setMpsContext (m_mpsContext); |
| 110 |
|
| 111 |
m_worker.start(); |
| 112 |
return mps_context_get_degree (m_mpsContext); |
| 113 |
} |
| 114 |
|
| 115 |
int |
| 116 |
PolynomialSolver::solvePoly(QString inputString, PolynomialBasis basis, |
| 117 |
mps_algorithm selected_algorithm, |
| 118 |
int required_digits, mps_output_goal goal) |
| 119 |
{ |
| 120 |
if (! m_mpsContext) |
| 121 |
m_mpsContext = mps_context_new(); |
| 122 |
|
| 123 |
QByteArray inputStringData = inputString.toLatin1(); |
| 124 |
mps_polynomial * poly = (mps_polynomial *) mps::Polynomial::fromString (m_mpsContext, inputStringData.data()); |
| 125 |
|
| 126 |
// If the user has chosen the Chebyshev basis, keep the coefficients but |
| 127 |
// change the basis of the parsed polynomial. |
| 128 |
if (basis == CHEBYSHEV) { |
| 129 |
mpq_t real_coeff, imag_coeff; |
| 130 |
mps_monomial_poly *mpoly = MPS_MONOMIAL_POLY (poly); |
| 131 |
|
| 132 |
mps_chebyshev_poly* cpoly = mps_chebyshev_poly_new (m_mpsContext, |
| 133 |
poly->degree, poly->structure); |
| 134 |
|
| 135 |
// Copy the coefficients -- every polynomial parsed from a command line has |
| 136 |
// rational coefficients. |
| 137 |
mpq_init (real_coeff); |
| 138 |
mpq_init (imag_coeff); |
| 139 |
|
| 140 |
for (int i = 0; i <= poly->degree; i++) { |
| 141 |
mps_monomial_poly_get_coefficient_q (m_mpsContext, mpoly, i, real_coeff, imag_coeff); |
| 142 |
mps_chebyshev_poly_set_coefficient_q (m_mpsContext, cpoly, i, real_coeff, imag_coeff); |
| 143 |
} |
| 144 |
|
| 145 |
mpq_clear (real_coeff); |
| 146 |
mpq_clear (imag_coeff); |
| 147 |
|
| 148 |
poly = MPS_POLYNOMIAL (cpoly); |
| 149 |
mps_polynomial_free (m_mpsContext, MPS_POLYNOMIAL (mpoly)); |
| 150 |
} |
| 151 |
|
| 152 |
if (poly && (! mps_context_has_errors(m_mpsContext)) && poly->degree != 0) { |
| 153 |
return solvePoly(poly, basis, selected_algorithm, required_digits, goal); |
| 154 |
} |
| 155 |
else { |
| 156 |
mps_context_free (m_mpsContext); |
| 157 |
m_mpsContext = NULL; |
| 158 |
|
| 159 |
m_errorMessage = tr("There is a syntax error in the specified the polynomial"); |
| 160 |
|
| 161 |
if (poly && poly->degree == 0) { |
| 162 |
m_errorMessage = tr("Constant polynomials have no roots"); |
| 163 |
} |
| 164 |
|
| 165 |
return -1; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
QString |
| 170 |
PolynomialSolver::errorMessage() |
| 171 |
{ |
| 172 |
return m_errorMessage; |
| 173 |
} |
| 174 |
|
| 175 |
void |
| 176 |
PolynomialSolver::workerExited() |
| 177 |
{ |
| 178 |
int n = mps_context_get_degree (m_mpsContext); |
| 179 |
|
| 180 |
/* Save the roots in a list */ |
| 181 |
QList<Root*> roots; |
| 182 |
|
| 183 |
/* Prepare some space to save the roots */ |
| 184 |
mpc_t * results = NULL; |
| 185 |
rdpe_t * radii = NULL; |
| 186 |
|
| 187 |
mps_context_get_roots_m(m_mpsContext, &results, &radii); |
| 188 |
|
| 189 |
for (int i = 0; i < n; i++) |
| 190 |
{ |
| 191 |
mps_root_status status = mps_context_get_root_status (m_mpsContext, i); |
| 192 |
Root* r = new Root(results[i], radii[i], status); |
| 193 |
roots.append(r); |
| 194 |
} |
| 195 |
|
| 196 |
for (int i = 0; i < mps_context_get_zero_roots (m_mpsContext); i++) |
| 197 |
{ |
| 198 |
Root * r = new Root(); |
| 199 |
|
| 200 |
mpc_init2 (r->value, 0); |
| 201 |
mpc_set_ui (r->value, 0U, 0U); |
| 202 |
rdpe_set (r->radius, rdpe_zero); |
| 203 |
|
| 204 |
roots.append(r); |
| 205 |
} |
| 206 |
|
| 207 |
mpc_vclear (results, n); |
| 208 |
|
| 209 |
if (results != NULL) |
| 210 |
free (results); |
| 211 |
if (radii != NULL) |
| 212 |
free (radii); |
| 213 |
|
| 214 |
// Update the internal model with the approximations |
| 215 |
m_rootsModel.setRoots(roots); |
| 216 |
|
| 217 |
/* Set the roots somewhere and then called |
| 218 |
* the solved() signal */ |
| 219 |
solved(); |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
PolynomialSolver::abortComputations() |
| 224 |
{ |
| 225 |
m_worker.abortComputation(); |
| 226 |
} |
| 227 |
|
| 228 |
unsigned long int |
| 229 |
PolynomialSolver::CPUTime() |
| 230 |
{ |
| 231 |
return m_worker.CPUTime(); |
| 232 |
} |
| 233 |
|
| 234 |
RootsModel *PolynomialSolver::rootsModel() |
| 235 |
{ |
| 236 |
return &m_rootsModel; |
| 237 |
} |