Exemplo n.º 1
0
QVariant
RootsModel::data(const QModelIndex &index, int role) const
{
    int i = index.row();

    if (role != Qt::DisplayRole && role < Qt::UserRole)
        return QVariant();

    if (i < 0 || i > length)
        return QVariant();
    else
    {
        rdpe_t root_module;
        mpc_rmod (root_module, m_roots.at(i)->value);
        int digits = (rdpe_Esp (root_module) - rdpe_Esp (m_roots.at(i)->radius)) / LOG2_10;
        char * buffer = NULL;

        switch (role)
        {
            case SHORT_APPROXIMATION:
                digits = 4;
                // fallthrough
            case Qt::DisplayRole:
                buffer = new char[2 * digits + 15];
                if (m_roots[i]->get_imag_part() > 0)
                    gmp_sprintf (buffer, "%.*Ff + %.*Ffi", digits, mpc_Re (m_roots[i]->value),
                                 digits, mpc_Im (m_roots[i]->value));
                else
                    gmp_sprintf (buffer, "%.*Ff %.*Ffi", digits, mpc_Re (m_roots[i]->value),
                                 digits, mpc_Im (m_roots[i]->value));
                return QString(buffer);
                break;

            case STATUS:
                return QString(MPS_ROOT_STATUS_TO_STRING (m_roots[i]->status));

            case RADIUS:
                return QString("%1").arg(m_roots[i]->get_radius());

            case ROOT:
                return QVariant::fromValue((void*) m_roots[i]);

            default:
                qDebug() << "Invalid role";
                return QVariant();
        }
    }
}
Exemplo n.º 2
0
/**
 * @brief Print an approximation to stdout (or whatever the output
 * stream currently selected in the mps_context is).
 *
 * @param s A pointer to the current mps_context.
 * @param i The index of the approxiomation that shall be printed.
 * @param num The number of zero roots.
 */
MPS_PRIVATE void
mps_outroot (mps_context * s, int i, int num)
{
  long out_digit;

  out_digit = (long)(LOG10_2 * s->output_config->prec) + 10;

  /* print format header */
  switch (s->output_config->format)
    {
    case MPS_OUTPUT_FORMAT_COMPACT:
    case MPS_OUTPUT_FORMAT_FULL:
      fprintf (s->outstr, "(");
      break;

    case MPS_OUTPUT_FORMAT_VERBOSE:
      fprintf (s->outstr, "Root(%d) = ", num);
      break;

    default:
      break;
    }

  /* print real part */
  if (i == ISZERO || s->root[i]->attrs == MPS_ROOT_ATTRS_IMAG)
    fprintf (s->outstr, "0");
  else
    mps_outfloat (s, mpc_Re (s->root[i]->mvalue), s->root[i]->drad, out_digit, true);

  /* print format middle part */
  switch (s->output_config->format)
    {
    case MPS_OUTPUT_FORMAT_BARE:
      fprintf (s->outstr, " ");
      break;

    case MPS_OUTPUT_FORMAT_GNUPLOT:
    case MPS_OUTPUT_FORMAT_GNUPLOT_FULL:
      fprintf (s->outstr, "\t");
      break;

    case MPS_OUTPUT_FORMAT_COMPACT:
    case MPS_OUTPUT_FORMAT_FULL:
      fprintf (s->outstr, ", ");
      break;

    case MPS_OUTPUT_FORMAT_VERBOSE:
      if (i == ISZERO || mpf_sgn (mpc_Im (s->root[i]->mvalue)) >= 0)
        fprintf (s->outstr, " + I * ");
      else
        fprintf (s->outstr, " - I * ");
      break;

    default:
      break;
    }

  /* print imaginary part */
  if (i == ISZERO || s->root[i]->attrs == MPS_ROOT_ATTRS_REAL)
    fprintf (s->outstr, "0");
  else
    mps_outfloat (s, mpc_Im (s->root[i]->mvalue), s->root[i]->drad, out_digit,
                  s->output_config->format != MPS_OUTPUT_FORMAT_VERBOSE);

  /* If the output format is GNUPLOT_FORMAT_FULL, print out also the radius */
  if (s->output_config->format == MPS_OUTPUT_FORMAT_GNUPLOT_FULL)
    {
      fprintf (s->outstr, "\t");
      rdpe_out_str_u (s->outstr, s->root[i]->drad);
      fprintf (s->outstr, "\t");
      rdpe_out_str_u (s->outstr, s->root[i]->drad);
    }

  /* print format ending */
  switch (s->output_config->format)
    {
    case MPS_OUTPUT_FORMAT_COMPACT:
      fprintf (s->outstr, ")");
      break;

    case MPS_OUTPUT_FORMAT_FULL:
      fprintf (s->outstr, ")\n");
      if (i != ISZERO)
        {
          rdpe_outln_str (s->outstr, s->root[i]->drad);
          fprintf (s->outstr, "Status: %s, %s, %s\n",
                   MPS_ROOT_STATUS_TO_STRING (s->root[i]->status),
                   MPS_ROOT_ATTRS_TO_STRING (s->root[i]->attrs),
                   MPS_ROOT_INCLUSION_TO_STRING (s->root[i]->inclusion));
        }
      else
        fprintf (s->outstr, " 0\n ---\n");
      break;

    default:
      break;
    }
  fprintf (s->outstr, "\n");

  /* debug info */
  if (s->DOLOG)
    {
      if (i == ISZERO)
        fprintf (s->logstr, "zero root %-4d = 0", num);
      else
        {
          fprintf (s->logstr, "Root %-4d = ", i);
          mpc_out_str_2 (s->logstr, 10, 0, 0, s->root[i]->mvalue);
          fprintf (s->logstr, "\n");
          fprintf (s->logstr, "  Radius = ");
          rdpe_outln_str (s->logstr, s->root[i]->drad);
          fprintf (s->logstr, "  Prec = %ld\n",
                   (long)(mpc_get_prec (s->root[i]->mvalue) / LOG2_10));
          fprintf (s->logstr, "  Approximation = %s\n",
                   MPS_ROOT_STATUS_TO_STRING (s->root[i]->status));
          fprintf (s->logstr, "  Attributes = %s\n",
                   MPS_ROOT_ATTRS_TO_STRING (s->root[i]->attrs));
          fprintf (s->logstr, "  Inclusion = %s\n",
                   MPS_ROOT_INCLUSION_TO_STRING (s->root[i]->inclusion));
          fprintf (s->logstr, "--------------------\n");
        }
    }
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: robol/MPSolve
/**
 * @brief Setup vectors and variables
 */
MPS_PRIVATE void
mps_setup (mps_context * s)
{
  int i;
  mps_polynomial *p = s->active_poly;
  mpf_t mptemp;
  mpc_t mptempc;

  if (s->DOLOG)
    {
      /* fprintf (s->logstr, "Goal      = %5s\n", s->goal); */
      /* fprintf (s->logstr, "Data type = %3s\n", s->data_type); */
      fprintf (s->logstr, "Degree    = %d\n", s->n);
      fprintf (s->logstr, "Input prec.  = %ld digits\n", (long)(s->active_poly->prec
                                                                * LOG10_2));
      fprintf (s->logstr, "Output prec. = %ld digits\n", (long)(s->output_config->prec
                                                                * LOG10_2));
    }

  /* setup temporary vectors */
  if (MPS_IS_MONOMIAL_POLY (p) && MPS_DENSITY_IS_SPARSE (s->active_poly->density))
    {
      mps_monomial_poly *mp = MPS_MONOMIAL_POLY (p);

      for (i = 0; i <= p->degree; i++)
        {
          mp->fap[i] = 0.0;
          mp->fpr[i] = 0.0;
          rdpe_set (mp->dap[i], rdpe_zero);
          cplx_set (mp->fpc[i], cplx_zero);
          rdpe_set (mp->dpr[i], rdpe_zero);
          cdpe_set (mp->dpc[i], cdpe_zero);
        }
    }

  /* Indexes of the first (and only) cluster start from
   * 0 and reach n */
  mps_cluster_reset (s);

  /* set input and output epsilon */
  rdpe_set_2dl (s->eps_in, 1.0, 1 - s->active_poly->prec);
  rdpe_set_2dl (s->eps_out, 1.0, 1 - s->output_config->prec);

  /* precision of each root */
  for (i = 0; i < s->n; i++)
    s->root[i]->wp = 53;

  /* output order info */
  for (i = 0; i < s->n; i++)
    s->order[i] = i;

  /* reset root counts */
  s->count[0] = s->count[1] = s->count[2] = 0;

  /* compute DPE approximations */
  if (MPS_IS_MONOMIAL_POLY (p))
    {
      mps_monomial_poly *mp = MPS_MONOMIAL_POLY (p);

      /* init temporary mp variables */
      mpf_init2 (mptemp, DBL_MANT_DIG);
      mpc_init2 (mptempc, DBL_MANT_DIG);

      /* main loop */
      s->skip_float = false;
      for (i = 0; i <= s->n; i++)
        {
          if (MPS_DENSITY_IS_SPARSE (s->active_poly->density) && !mp->spar[i])
            continue;

          if (MPS_STRUCTURE_IS_REAL (s->active_poly->structure))
            {
              if (MPS_STRUCTURE_IS_RATIONAL (s->active_poly->structure) ||
                  MPS_STRUCTURE_IS_INTEGER (s->active_poly->structure))
                {
                  mpf_set_q (mptemp, mp->initial_mqp_r[i]);
                  mpf_get_rdpe (mp->dpr[i], mptemp);
                  /*#G GMP 2.0.2 bug begin */
                  if (rdpe_sgn (mp->dpr[i]) != mpq_sgn (mp->initial_mqp_r[i]))
                    rdpe_neg_eq (mp->dpr[i]);
                  /*#G GMP bug end */
                }

              if (MPS_STRUCTURE_IS_FP (s->active_poly->structure))
                mpf_get_rdpe (mp->dpr[i], mpc_Re (mp->mfpc[i]));

              cdpe_set_e (mp->dpc[i], mp->dpr[i], rdpe_zero);

              /* compute dap[i] and check for float phase */
              rdpe_abs (mp->dap[i], mp->dpr[i]);
              rdpe_abs (mp->dap[i], mp->dpr[i]);
              if (rdpe_gt (mp->dap[i], rdpe_maxd)
                  || rdpe_lt (mp->dap[i], rdpe_mind))
                s->skip_float = true;
            }
          else if (MPS_STRUCTURE_IS_COMPLEX (s->active_poly->structure))
            {
              if (MPS_STRUCTURE_IS_RATIONAL (s->active_poly->structure) ||
                  MPS_STRUCTURE_IS_INTEGER (s->active_poly->structure))
                {
                  mpc_set_q (mptempc, mp->initial_mqp_r[i], mp->initial_mqp_i[i]);
                  mpc_get_cdpe (mp->dpc[i], mptempc);
                  /*#G GMP 2.0.2 bug begin */
                  if (rdpe_sgn (cdpe_Re (mp->dpc[i])) != mpq_sgn (mp->initial_mqp_r[i]))
                    rdpe_neg_eq (cdpe_Re (mp->dpc[i]));
                  if (rdpe_sgn (cdpe_Im (mp->dpc[i])) != mpq_sgn (mp->initial_mqp_i[i]))
                    rdpe_neg_eq (cdpe_Im (mp->dpc[i]));
                  /*#G GMP bug end */
                }
              else if (MPS_STRUCTURE_IS_FP (s->active_poly->structure))
                mpc_get_cdpe (mp->dpc[i], mp->mfpc[i]);

              /* compute dap[i] */
              cdpe_mod (mp->dap[i], mp->dpc[i]);
              if (rdpe_gt (mp->dap[i], rdpe_maxd)
                  || rdpe_lt (mp->dap[i], rdpe_mind))
                s->skip_float = true;
            }
        }

      /* free temporary mp variables */
      mpf_clear (mptemp);
      mpc_clear (mptempc);

      /* adjust input data type */
      if (MPS_STRUCTURE_IS_FP (s->active_poly->structure) && s->skip_float)
        s->active_poly->structure = MPS_STRUCTURE_IS_REAL (s->active_poly->structure) ?
                                    MPS_STRUCTURE_REAL_BIGFLOAT : MPS_STRUCTURE_COMPLEX_BIGFLOAT;

      /* prepare floating point vectors */
      if (!s->skip_float)
        for (i = 0; i <= MPS_POLYNOMIAL (p)->degree; i++)
          {
            if (MPS_DENSITY_IS_SPARSE (s->active_poly->density) || !mp->spar[i])
              continue;
            if (MPS_STRUCTURE_IS_REAL (s->active_poly->structure))
              {
                mp->fpr[i] = rdpe_get_d (mp->dpr[i]);
                mp->fap[i] = fabs (mp->fpr[i]);
                cplx_set_d (mp->fpc[i], mp->fpr[i], 0.0);
              }
            else
              {
                cdpe_get_x (mp->fpc[i], mp->dpc[i]);
                mp->fap[i] = cplx_mod (mp->fpc[i]);
              }
          }
    }
}