Exemplo n.º 1
0
	void line(const Point2D& p1, const Point2D& p2, Style style = NONE)
	{
		tex += QString("\n\\draw%1 (%2,%3) -- (%4,%5);")
			.arg(style_tex(style))
			.arg(p1.x)
			.arg(p1.y)
			.arg(p2.x)
			.arg(p2.y);
		svg += QString("\n\t<line %1 x1=\"%2\" y1=\"%3\" x2=\"%4\" y2=\"%5\"/>")
			.arg(style_svg(style))
			.arg(scalex(p1.x))
			.arg(scaley(p1.y))
			.arg(scalex(p2.x))
			.arg(scaley(p2.y));
	}
Exemplo n.º 2
0
	void arc(const Point2D& p, double radius, double angle0, double angle1, Style style = NONE)
	{
		tex += QString("\n\\draw%1 (%2,%3) arc (%4:%5:%6);")
			.arg(style_tex(style))
			.arg(p.x)
			.arg(p.y)
			.arg(180/pi*angle0)
			.arg(180/pi*angle1)
			.arg(radius);
		Point2D p1 = p - Point2D::polar(radius, angle0) + Point2D::polar(radius, angle1);
		svg += QString("\n\t<path fill=\"none\" %1 d=\"M%2 %3 A%4 %5 0 0 0 %6 %7\"/>")
			.arg(style_svg(style))
			.arg(scalex(p.x))
			.arg(scaley(p.y))
			.arg(scale(radius))
			.arg(scale(radius))
			.arg(scalex(p1.x))
			.arg(scaley(p1.y));
	}
Exemplo n.º 3
0
	void path(const Point2D& p1, const Point2D& p2, const Point2D& p3, Style style = NONE)
	{
		tex += QString("\n\\draw%1 (%2,%3) -- (%4,%5) -- (%6,%7);")
			.arg(style_tex(style))
			.arg(p1.x)
			.arg(p1.y)
			.arg(p2.x)
			.arg(p2.y)
			.arg(p3.x)
			.arg(p3.y);
		svg += QString("\n\t<path %1 d=\"M%2 %3 L%4 %5 L%6 %7\" fill=\"none\"/>")
			.arg(style_svg(style))
			.arg(scalex(p1.x))
			.arg(scaley(p1.y))
			.arg(scalex(p2.x))
			.arg(scaley(p2.y))
			.arg(scalex(p3.x))
			.arg(scaley(p3.y));
	}
Exemplo n.º 4
0
	void dot(const Point2D& p, Style style = NONE)
	{
		tex += QString("\n\\draw%1 (%2,%3) circle(0.07);")
			.arg(style_tex(style | FILL))
			.arg(p.x)
			.arg(p.y);
		svg += QString("\n\t<circle %1 cx=\"%2\" cy=\"%3\" r=\"3\"/>")
			.arg(style_svg(style | FILL))
			.arg(scalex(p.x))
			.arg(scaley(p.y));
	}
Exemplo n.º 5
0
void doCircle(FILE *stream) {
  float cx, cy, r;
  int c;

  cx = getFloat(stream);
  cy = getFloat(stream);
  r = getFloat(stream);
  c = getColor(getNumber(stream));

  gdImageArc(image, viewx(cx), viewy(cy), 
	     scalex(r)*2, scaley(r)*2, 0, 360, c);
}
Exemplo n.º 6
0
void doFilledEllipse(FILE *stream) {
  float cx, cy, w, h;
  int c;

  cx = getFloat(stream);
  cy = getFloat(stream);
  w = getFloat(stream);
  h = getFloat(stream);
  c = getColor(getNumber(stream));

  gdImageFilledEllipse(image, viewx(cx), viewy(cy), 
		       scalex(w), scaley(h), c);
}
Exemplo n.º 7
0
	void math(const QString text, const Point2D& p, Style style = NONE)
	{
		tex += QString("\n\\node%1 at (%2,%3) {%4};")
			.arg(style_tex(style))
			.arg(p.x)
			.arg(p.y)
			.arg(text);
		html += QString("\n\t<div %1 style=\"left:%2px;top:%3px;\">%4</div>")
			.arg(style_math(style))
			.arg(scalex(p.x, style))
			.arg(scaley(p.y, style))
			.arg(text);
	}
Exemplo n.º 8
0
	void text(const QString text, const Point2D& p, Style style = NONE)
	{
		tex += QString("\n\\node%1 at (%2,%3) {%4};")
			.arg(style_tex(style))
			.arg(p.x)
			.arg(p.y)
			.arg(text);
		svg += QString("\n\t<text %1 x=\"%2\" y=\"%3\">%4</text>")
			.arg(style_svg(style | TEXT))
			.arg(scalex(p.x, style))
			.arg(scaley(p.y, style))
			.arg(text);
	}
Exemplo n.º 9
0
void doArc(FILE *stream) {
  float cx, cy, w, h;
  int s, e, c;

  cx = getFloat(stream);  // center x
  cy = getFloat(stream);  // center y
  w = getFloat(stream);   // width
  h = getFloat(stream);   // height
  s = getNumber(stream);   // start angle
  e = getNumber(stream);   // end angle
  c = getColor(getNumber(stream));   // color

  gdImageArc(image, viewx(cx), viewy(cy), 
	     scalex(w), scaley(h), s, e, c);
}
Exemplo n.º 10
0
void doFilledArc(FILE *stream) {
  float cx, cy, w, h, s;
  int e, c, style;

  cx = getFloat(stream);  // center x
  cy = getFloat(stream);  // center y
  w = getFloat(stream);   // width
  h = getFloat(stream);   // height
  s = getNumber(stream);   // start angle
  e = getNumber(stream);   // end angle
  c = getColor(getNumber(stream));   // color
  style = getNumber(stream);

  gdImageFilledArc(image, viewx(cx), viewy(cy), 
		   scalex(w), scaley(h), s, e, c, style);
}
Exemplo n.º 11
0
int tfont_width(char const *code)
{
	if(code == NULL) {
		return 0;
	}
	while(*code)
	{
		char c = sgetc(&code);
		switch(c)
		{
			case 0:
			case -1:
				return 0;
			case 'a':
				return scalex(sgetn(&code));
		}
	}
	return 0;
}
Exemplo n.º 12
0
Arquivo: tnc.c Projeto: 317070/scipy
/*
 * This routine is a bounds-constrained truncated-newton method.
 * the truncated-newton method is preconditioned by a limited-memory
 * quasi-newton method (this preconditioning strategy is developed
 * in this routine) with a further diagonal scaling
 * (see routine diagonalscaling).
 */
static tnc_rc tnc_minimize(int n, double x[],
  double *f, double gfull[], tnc_function *function, void *state,
  double xscale[], double xoffset[], double *fscale,
  double low[], double up[], tnc_message messages,
  int maxCGit, int maxnfeval, int *nfeval, int *niter, double eta, double
  stepmx, double accuracy, double fmin, double ftol, double xtol, double
  pgtol, double rescale, tnc_callback *callback)
{
  double fLastReset, difnew, epsmch, epsred, oldgtp,
    difold, oldf, xnorm, newscale,
    gnorm, ustpmax, fLastConstraint, spe, yrsr, yksk,
    *temp = NULL, *sk = NULL, *yk = NULL, *diagb = NULL, *sr = NULL,
    *yr = NULL, *oldg = NULL, *pk = NULL, *g = NULL;
  double alpha = 0.0; /* Default unused value */
  int i, icycle, oldnfeval, *pivot = NULL, frc;
  logical lreset, newcon, upd1, remcon;
  tnc_rc rc = TNC_ENOMEM; /* Default error */

  *niter = 0;

  /* Allocate temporary vectors */
  oldg = malloc(sizeof(*oldg)*n);
   if (oldg == NULL) goto cleanup;
  g = malloc(sizeof(*g)*n);
   if (g == NULL) goto cleanup;
  temp = malloc(sizeof(*temp)*n);
   if (temp == NULL) goto cleanup;
  diagb = malloc(sizeof(*diagb)*n);
   if (diagb == NULL) goto cleanup;
  pk = malloc(sizeof(*pk)*n);
   if (pk == NULL) goto cleanup;

  sk = malloc(sizeof(*sk)*n);
   if (sk == NULL) goto cleanup;
  yk = malloc(sizeof(*yk)*n);
   if (yk == NULL) goto cleanup;
  sr = malloc(sizeof(*sr)*n);
   if (sr == NULL) goto cleanup;
  yr = malloc(sizeof(*yr)*n);
   if (yr == NULL) goto cleanup;

  pivot = malloc(sizeof(*pivot)*n);
   if (pivot == NULL) goto cleanup;

  /* Initialize variables */
  epsmch = mchpr1();

  difnew = 0.0;
  epsred = 0.05;
  upd1 = TNC_TRUE;
  icycle = n - 1;
  newcon = TNC_TRUE;

  /* Uneeded initialisations */
  lreset = TNC_FALSE;
  yrsr = 0.0;
  yksk = 0.0;

  /* Initial scaling */
  scalex(n, x, xscale, xoffset);
  (*f) *= *fscale;

  /* initial pivot calculation */
  setConstraints(n, x, pivot, xscale, xoffset, low, up);

  dcopy1(n, gfull, g);
  scaleg(n, g, xscale, *fscale);

  /* Test the lagrange multipliers to see if they are non-negative. */
  for (i = 0; i < n; i++)
    if (-pivot[i] * g[i] < 0.0)
      pivot[i] = 0;

  project(n, g, pivot);

  /* Set initial values to other parameters */
  gnorm = dnrm21(n, g);

  fLastConstraint = *f; /* Value at last constraint */
  fLastReset = *f; /* Value at last reset */

  if (messages & TNC_MSG_ITER) fprintf(stderr,
    "  NIT   NF   F                       GTG\n");
  if (messages & TNC_MSG_ITER) printCurrentIteration(n, *f / *fscale, gfull,
    *niter, *nfeval, pivot);

  /* Set the diagonal of the approximate hessian to unity. */
  for (i = 0; i < n; i++) diagb[i] = 1.0;

  /* Start of main iterative loop */
  while(TNC_TRUE)
  {
    /* Local minimum test */
    if (dnrm21(n, g) <= pgtol * (*fscale))
    {
      /* |PG| == 0.0 => local minimum */
      dcopy1(n, gfull, g);
      project(n, g, pivot);
      if (messages & TNC_MSG_INFO) fprintf(stderr,
        "tnc: |pg| = %g -> local minimum\n", dnrm21(n, g) / (*fscale));
      rc = TNC_LOCALMINIMUM;
      break;
    }

    /* Terminate if more than maxnfeval evaluations have been made */
    if (*nfeval >= maxnfeval)
    {
      rc = TNC_MAXFUN;
      break;
    }

    /* Rescale function if necessary */
    newscale = dnrm21(n, g);
    if ((newscale > epsmch) && (fabs(log10(newscale)) > rescale))
    {
      newscale = 1.0/newscale;

      *f *= newscale;
      *fscale *= newscale;
      gnorm *= newscale;
      fLastConstraint *= newscale;
      fLastReset *= newscale;
      difnew *= newscale;

      for (i = 0; i < n; i++) g[i] *= newscale;
      for (i = 0; i < n; i++) diagb[i] = 1.0;

      upd1 = TNC_TRUE;
      icycle = n - 1;
      newcon = TNC_TRUE;

      if (messages & TNC_MSG_INFO) fprintf(stderr,
        "tnc: fscale = %g\n", *fscale);
    }

    dcopy1(n, x, temp);
    project(n, temp, pivot);
    xnorm = dnrm21(n, temp);
    oldnfeval = *nfeval;

    /* Compute the new search direction */
    frc = tnc_direction(pk, diagb, x, g, n, maxCGit, maxnfeval, nfeval,
      upd1, yksk, yrsr, sk, yk, sr, yr,
      lreset, function, state, xscale, xoffset, *fscale,
      pivot, accuracy, gnorm, xnorm, low, up);

    if (frc == -1)
    {
      rc = TNC_ENOMEM;
      break;
    }

    if (frc)
    {
      rc = TNC_USERABORT;
      break;
    }

    if (!newcon)
    {
      if (!lreset)
      {
        /* Compute the accumulated step and its corresponding gradient
          difference. */
        dxpy1(n, sk, sr);
        dxpy1(n, yk, yr);
        icycle++;
      }
      else
      {
        /* Initialize the sum of all the changes */
        dcopy1(n, sk, sr);
        dcopy1(n, yk, yr);
        fLastReset = *f;
        icycle = 1;
      }
    }

    dcopy1(n, g, oldg);
    oldf = *f;
    oldgtp = ddot1(n, pk, g);

    /* Maximum unconstrained step length */
    ustpmax = stepmx / (dnrm21(n, pk) + epsmch);

    /* Maximum constrained step length */
    spe = stepMax(ustpmax, n, x, pk, pivot, low, up, xscale, xoffset);

    if (spe > 0.0)
    {
      ls_rc lsrc;
      /* Set the initial step length */
      alpha = initialStep(*f, fmin / (*fscale), oldgtp, spe);

      /* Perform the linear search */
      lsrc = linearSearch(n, function, state, low, up,
        xscale, xoffset, *fscale, pivot,
        eta, ftol, spe, pk, x, f, &alpha, gfull, maxnfeval, nfeval);

      if (lsrc == LS_ENOMEM)
      {
        rc = TNC_ENOMEM;
        break;
      }

      if (lsrc == LS_USERABORT)
      {
        rc = TNC_USERABORT;
        break;
      }

      if (lsrc == LS_FAIL)
      {
        rc = TNC_LSFAIL;
        break;
      }

      /* If we went up to the maximum unconstrained step, increase it */
      if (alpha >= 0.9 * ustpmax)
      {
        stepmx *= 1e2;
        if (messages & TNC_MSG_INFO) fprintf(stderr,
          "tnc: stepmx = %g\n", stepmx);
      }

      /* If we went up to the maximum constrained step,
         a new constraint was encountered */
      if (alpha - spe >= -epsmch * 10.0)
      {
        newcon = TNC_TRUE;
      }
      else
      {
        /* Break if the linear search has failed to find a lower point */
        if (lsrc != LS_OK)
        {
          if (lsrc == LS_MAXFUN) rc = TNC_MAXFUN;
          else rc = TNC_LSFAIL;
          break;
        }
        newcon = TNC_FALSE;
      }
    }
    else
    {
      /* Maximum constrained step == 0.0 => new constraint */
      newcon = TNC_TRUE;
    }

    if (newcon)
    {
      if(!addConstraint(n, x, pk, pivot, low, up, xscale, xoffset))
      {
        if(*nfeval == oldnfeval)
        {
          rc = TNC_NOPROGRESS;
          break;
        }
      }

      fLastConstraint = *f;
    }

    (*niter)++;

    /* Invoke the callback function */
    if (callback)
    {
      unscalex(n, x, xscale, xoffset);
      callback(x, state);
      scalex(n, x, xscale, xoffset);
    }

    /* Set up parameters used in convergence and resetting tests */
    difold = difnew;
    difnew = oldf - *f;

    /* If this is the first iteration of a new cycle, compute the
       percentage reduction factor for the resetting test */
    if (icycle == 1)
    {
      if (difnew > difold * 2.0) epsred += epsred;
      if (difnew < difold * 0.5) epsred *= 0.5;
    }

    dcopy1(n, gfull, g);
    scaleg(n, g, xscale, *fscale);

    dcopy1(n, g, temp);
    project(n, temp, pivot);
    gnorm = dnrm21(n, temp);

    /* Reset pivot */
    remcon = removeConstraint(oldgtp, gnorm, pgtol * (*fscale), *f,
      fLastConstraint, g, pivot, n);

    /* If a constraint is removed */
    if (remcon)
    {
      /* Recalculate gnorm and reset fLastConstraint */
      dcopy1(n, g, temp);
      project(n, temp, pivot);
      gnorm = dnrm21(n, temp);
      fLastConstraint = *f;
    }

    if (!remcon && !newcon)
    {
      /* No constraint removed & no new constraint : tests for convergence */
      if (fabs(difnew) <= ftol * (*fscale))
      {
        if (messages & TNC_MSG_INFO) fprintf(stderr,
          "tnc: |fn-fn-1] = %g -> convergence\n", fabs(difnew) / (*fscale));
        rc = TNC_FCONVERGED;
        break;
      }
      if (alpha * dnrm21(n, pk) <= xtol)
      {
        if (messages & TNC_MSG_INFO) fprintf(stderr,
          "tnc: |xn-xn-1] = %g -> convergence\n", alpha * dnrm21(n, pk));
        rc = TNC_XCONVERGED;
        break;
      }
    }

    project(n, g, pivot);

    if (messages & TNC_MSG_ITER) printCurrentIteration(n, *f / *fscale, gfull,
      *niter, *nfeval, pivot);

    /* Compute the change in the iterates and the corresponding change in the
      gradients */
    if (!newcon)
    {
      for (i = 0; i < n; i++)
      {
        yk[i] = g[i] - oldg[i];
        sk[i] = alpha * pk[i];
      }

      /* Set up parameters used in updating the preconditioning strategy */
      yksk = ddot1(n, yk, sk);

      if (icycle == (n - 1) || difnew < epsred * (fLastReset - *f))
        lreset = TNC_TRUE;
      else
      {
        yrsr = ddot1(n, yr, sr);
        if (yrsr <= 0.0) lreset = TNC_TRUE;
        else lreset = TNC_FALSE;
      }
      upd1 = TNC_FALSE;
    }
  }

  if (messages & TNC_MSG_ITER) printCurrentIteration(n, *f / *fscale, gfull,
    *niter, *nfeval, pivot);

  /* Unscaling */
  unscalex(n, x, xscale, xoffset);
  coercex(n, x, low, up);
  (*f) /= *fscale;

cleanup:
  if (oldg) free(oldg);
  if (g) free(g);
  if (temp) free(temp);
  if (diagb) free(diagb);
  if (pk) free(pk);

  if (sk) free(sk);
  if (yk) free(yk);
  if (sr) free(sr);
  if (yr) free(yr);

  if (pivot) free(pivot);

  return rc;
}
Exemplo n.º 13
0
int tfont_render_glyph(int tx, int ty, char const *code)
{
	if(code == NULL) {
		return 0;
	}
	int advance = 0;
	int x = 0;
	int y = 0;
	int px = 0;
	int py = 0;
	while(*code)
	{
		char c = sgetc(&code);
		switch(c)
		{
			case 0:
			case -1:
				return scale(advance);
			case 'a':
				advance = sgetn(&code);
				break;
			case 'M':
				x = sgetn(&code);
				y = sgetn(&code);
				break;
			case 'm':
				x += sgetn(&code);
				y += sgetn(&code);
				break;
			case 'P':
				x = sgetn(&code);
				y = sgetn(&code);
				line(
					tx + scalex(px), 
					ty - scaley(py), 
					tx + scalex(x), 
					ty - scaley(y));
				break;
			case 'p':
				x += sgetn(&code);
				y += sgetn(&code);
				line(
					tx + scalex(px),
					ty - scaley(py), 
					tx + scalex(x), 
					ty - scaley(y));
				break;
			case 'd':
				dot(
					tx + scalex(x),
					ty - scaley(y));
				break;
			default:
#if TFONT_DEBUG
				printf("Unknown command: %c(%d)?\n", c, c);
#endif
				break;
		}
		px = x;
		py = y;
	}
	return scalex(advance);
}