Exemple #1
0
static double noise3(int * p, double x, double y, double z)
{
  int X = (int)FASTFLOOR(x) & 255,                   /* FIND UNIT CUBE THAT */
      Y = (int)FASTFLOOR(y) & 255,                   /* CONTAINS POINT.     */
      Z = (int)FASTFLOOR(z) & 255;

  double u, v, w;

  int A = p[X  ]+Y, AA = p[A]+Z, AB = p[A+1]+Z,      /* HASH COORDINATES OF */
      B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z;      /* THE 8 CUBE CORNERS, */

  x -= FASTFLOOR(x);                                  /* FIND RELATIVE X,Y,Z */
  y -= FASTFLOOR(y);                                  /* OF POINT IN CUBE.   */
  z -= FASTFLOOR(z);

  u = FADE(x);                                       /* COMPUTE FADE CURVES */
  v = FADE(y);                                       /* FOR EACH OF X,Y,Z.  */
  w = FADE(z);

  return LERP(w, LERP(v, LERP(u, GRAD(p[AA  ], x    , y    , z     ),  /* AND ADD */
                                 GRAD(p[BA  ], x-1.0, y    , z     )), /* BLENDED */
                         LERP(u, GRAD(p[AB  ], x    , y-1.0, z     ),  /* RESULTS */
                                 GRAD(p[BB  ], x-1.0, y-1.0, z     ))),/* FROM  8 */
                 LERP(v, LERP(u, GRAD(p[AA+1], x    , y    , z-1.0 ),  /* CORNERS */
                                 GRAD(p[BA+1], x-1.0, y    , z-1.0 )), /* OF CUBE */
                         LERP(u, GRAD(p[AB+1], x    , y-1.0, z-1.0 ),
                                 GRAD(p[BB+1], x-1.0, y-1.0, z-1.0 ))));
}
Exemple #2
0
static void logLik_dparam(const OscatsModel *model, OscatsResponse resp,
                          const OscatsPoint *theta, const OscatsCovariates *covariates,
                          GGslVector *grad, GGslMatrix *hes)
{
  gdouble *g = (grad ? grad->v->data : NULL);
  gdouble *H = (hes ? hes->v->data : NULL);
  gdouble p, logit_p, grad_val, hes_val;
  guint *dims = model->shortDims;
  guint j, k, Ndims = model->Ndims;
  guint grad_stride = (grad ? grad->v->stride : 0);
  guint hes_stride = (hes ? hes->v->tda : 0);
  g_return_if_fail(resp <= 1);

#define GRAD(t, j) (g[((t)*Ndims+(j))*grad_stride])
#define HES(t1, j, t2, k) (H[((t1)*Ndims+(j))*hes_stride + ((t2)*Ndims+(k))])

  if (resp)
    for (j=0; j < Ndims; j++)
      if (g_bit_array_get_bit(theta->bin, dims[j]))
      {
        grad_val = 1 / (model->params[PARAM_SLIP*Ndims+j] - 1);
        if (g) GRAD(PARAM_SLIP, j) += grad_val;
        if (H) HES(PARAM_SLIP, j, PARAM_SLIP, j) -= grad_val*grad_val;
      } else {		// alpha_j == 0
        grad_val = 1 / model->params[PARAM_GUESS*Ndims+j];
        if (g) GRAD(PARAM_GUESS, j) += grad_val;
        if (H) HES(PARAM_GUESS, j, PARAM_GUESS, j) -= grad_val*grad_val;
      }
  else 			// !resp
  {
    p = P(model, 0, theta, covariates);
    logit_p = p/(1-p);
    for (j=0; j < Ndims; j++)
      if (g_bit_array_get_bit(theta->bin, dims[j]))
      {
        grad_val = logit_p/(1-model->params[PARAM_SLIP*Ndims+j]);
        GRAD(PARAM_SLIP, j) += grad_val;
        HES(PARAM_SLIP, j, PARAM_SLIP, j) -= grad_val*grad_val;
        grad_val /= (1-p);
        for (k=j+1; k < Ndims; k++)
          if (g_bit_array_get_bit(theta->bin, dims[k]))
          {
            hes_val = grad_val / (1-model->params[PARAM_SLIP*Ndims+k]);
            HES(PARAM_SLIP, j, PARAM_SLIP, k) -= hes_val;
            HES(PARAM_SLIP, k, PARAM_SLIP, j) -= hes_val;
          } else {	// alpha_k == 0
            hes_val = grad_val / model->params[PARAM_GUESS*Ndims+k];
            HES(PARAM_SLIP, j, PARAM_GUESS, k) += hes_val;
            HES(PARAM_GUESS, k, PARAM_SLIP, j) += hes_val;
          }
      } else {		// alpha_j == 0
        grad_val = logit_p / model->params[PARAM_GUESS*Ndims+j];
        GRAD(PARAM_GUESS, j) -= grad_val;
        HES(PARAM_GUESS, j, PARAM_GUESS, j) -= grad_val*grad_val;
        grad_val /= (1-p);
        for (k=j+1; k < Ndims; k++)
          if (g_bit_array_get_bit(theta->bin, dims[k]))
          {
            hes_val = grad_val / (1-model->params[PARAM_SLIP*Ndims+k]);
            HES(PARAM_GUESS, j, PARAM_SLIP, k) += hes_val;
            HES(PARAM_SLIP, k, PARAM_GUESS, j) += hes_val;
          } else {	// alpha_k == 0
            hes_val = grad_val / model->params[PARAM_GUESS*Ndims+k];
            HES(PARAM_GUESS, j, PARAM_GUESS, k) += hes_val;
            HES(PARAM_GUESS, k, PARAM_GUESS, j) += hes_val;
          }
      }
  }
}
void
Style::drawSliderHandle(const QRect &handle, const QStyleOption *option, QPainter *painter, int step) const
{
    OPT_SUNKEN OPT_ENABLED OPT_FOCUS
    bool fullHover = config.scroll.fullHover && !config.btn.backLightHover;

    // shadow
    QPoint xy = handle.topLeft();
    if (step && config.btn.backLightHover)
        fillWithMask(painter, xy, Colors::mid(FCOLOR(Window), CCOLOR(scroll._, Fg), 6-step, step), lights.slider);
    if (sunken)
        xy += QPoint(F(1), 0);
    painter->drawPixmap(xy, shadows.slider[isEnabled][sunken]);

    // gradient
    xy += QPoint(sunken ? F(1) : F(2), F(1));

    QColor bc = hasFocus ? FCOLOR(Highlight) : CCOLOR(scroll._, Bg);
    if (fullHover)
        bc = Colors::mid(bc, CCOLOR(scroll._, Fg), 6-step, step);

    const QPixmap &fill = Gradients::pix(bc, masks.slider.height(), Qt::Vertical, isEnabled ? GRAD(scroll) : Gradients::None);
    fillWithMask(painter, xy, fill, masks.slider);
    if (!fullHover && isEnabled)
    {
        const QColor fc = Colors::mid(bc, hasFocus ? FCOLOR(HighlightedText) : CCOLOR(scroll._, Fg), 6-step, step+3);
        const QSize d = (masks.slider.size() - masks.notch.size())/2;
        xy += QPoint(d.width(), d.height());
        fillWithMask(painter, xy, fc, masks.notch);
    }
}
void
Style::drawDial(const QStyleOptionComplex *option, QPainter *painter, const QWidget *) const
{
    ASSURE_OPTION(dial, Slider);

    OPT_ENABLED OPT_HOVER OPT_FOCUS

    painter->save();
    QRect rect = RECT;
    if (rect.width() > rect.height())
    {
        rect.setLeft(rect.x()+(rect.width()-rect.height())/2);
        rect.setWidth(rect.height());
    }
    else
    {
        rect.setTop(rect.y()+(rect.height()-rect.width())/2);
        rect.setHeight(rect.width());
    }

    int d = qMin(2*rect.width()/5, Dpi::target.SliderThickness);
    int r;
    // angle calculation from qcommonstyle.cpp (c) Trolltech 1992-2007, ASA.
    float a;
    if (dial->maximum == dial->minimum)
        a = M_PI / 2;
    else if (dial->dialWrapping)
        a = M_PI * 3 / 2 - (dial->sliderValue - dial->minimum) * 2 * M_PI /
                                                                (dial->maximum - dial->minimum);
    else
        a = (M_PI * 8 - (dial->sliderValue - dial->minimum) * 10 * M_PI /
                                                                (dial->maximum - dial->minimum)) / 6;

    QPoint cp = rect.center();

    // fallback for small dials ============================
    bool small(rect.width() < 8*Dpi::target.SliderThickness/3);
    if ( small )
    {
        painter->setRenderHint( QPainter::Antialiasing );
        painter->setPen(Qt::NoPen);
        painter->setBrush(QColor(0,0,0,50));
        painter->drawEllipse(rect);
        rect.adjust(F(2), F(1), -F(2), -F(2));
        painter->setBrushOrigin(rect.topLeft());
        const QPixmap &fill = Gradients::pix(FCOLOR(Window), rect.height(), Qt::Vertical, GRAD(scroll));
        painter->setBrush(fill);
        painter->drawEllipse(rect);
        QColor c = hasFocus ? FCOLOR(Highlight) : FCOLOR(WindowText);
        if (!hover)
            c = Colors::mid(FCOLOR(Window), c, 1, 1+isEnabled);
        d = qMax(F(3), d/4);
        r = (rect.width()-d)/2;
        cp += QPoint((int)(r * cos(a)), -(int)(r * sin(a)));
        painter->setPen(QPen(c, d, Qt::SolidLine, Qt::RoundCap));
        painter->drawPoint(cp);
    }

    // the value ==============================================
    QFont fnt = painter->font();
    int h = rect.height()/2;
    h -= 2 * (h - qMin(h, painter->fontMetrics().xHeight())) / 3;
    fnt.setPixelSize( h );
    painter->setFont(fnt);
    painter->setBrush(Qt::NoBrush);
    painter->setPen(Colors::mid(PAL.background().color(), PAL.foreground().color(),!hasFocus,2));
    drawItemText(painter, rect,  Qt::AlignCenter, PAL, isEnabled, QString::number(dial->sliderValue));

    if (small)
        { painter->restore(); return; }

    r = (rect.width()-d)/2;
    cp += QPoint((int)(r * cos(a)), -(int)(r * sin(a)));

    // the huge ring =======================================
    r = d/2; rect.adjust(r,r,-r,-r);
    painter->setPen(FCOLOR(Window).dark(115));
    painter->setRenderHint( QPainter::Antialiasing );
    painter->drawEllipse(rect);
    rect.translate(0, 1);
    painter->setPen(FCOLOR(Window).light(108));
    painter->drawEllipse(rect);

    // the drop ===========================
    rect = QRect(0,0,d,d);
    rect.moveCenter(cp);
    drawSliderHandle(rect, option, painter, hover * 6);

    painter->restore();
}
void
Style::drawComboBox(const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
{
    ASSURE_OPTION(cmb, ComboBox);
    B_STATES
    if ( widget && widget->inherits("WebView") )
    {
        if (!(config.btn.backLightHover || cmb->editable))
        {   // paints hardcoded black text bypassing the style?! grrr...
            QStyleOptionComboBox *_cmb = const_cast<QStyleOptionComboBox*>(cmb);
            _cmb->palette.setColor(config.btn.std_role[Bg], QColor(230,230,230,255));
            _cmb->palette.setColor(config.btn.active_role[Bg], QColor(255,255,255,255));
        }
        widget = 0;
    }

    const int f1 = F(1), f2 = F(2);
    QRect ar;
    const QComboBox *combo = widget ? qobject_cast<const QComboBox*>(widget) : 0;
    QColor c = CONF_COLOR(btn.std, Bg);

    const bool listShown = combo && combo->view() && ((QWidget*)(combo->view()))->isVisible();
    if (listShown) // this messes up hover
        hover = hover || QRect(widget->mapToGlobal(RECT.topLeft()), RECT.size()).contains(QCursor::pos());

    if (isEnabled && (cmb->subControls & SC_ComboBoxArrow) && (!combo || combo->count() > 0))
    {   // do we have an arrow?
        ar = subControlRect(CC_ComboBox, cmb, SC_ComboBoxArrow, widget);
        ar.setBottom(ar.bottom()-f2);
    }

    // the frame
    if ((cmb->subControls & SC_ComboBoxFrame) && cmb->frame)
    {
        if (cmb->editable)
            drawLineEdit(option, painter, widget, false);
        else
        {
            if (!ar.isNull())
            {
                animStep = (appType == GTK || !widget) ? 6*hover : Animator::Hover::step(widget);
                if (listShown)
                    animStep = 6;

                int btn_layer = config.btn.layer;
                Gradients::Type btn_grd = config.btn.gradient;
                config.btn.round = !config.btn.round;

                const bool comboNotSmallerThanTool = config.chooser.layer == Raised || config.chooser.layer == Inlay ||
                                                     config.btn.tool.frame == Sunken || config.btn.tool.frame == Relief;
                if (comboNotSmallerThanTool && widget && qobject_cast<QToolBar*>(widget->parentWidget()))
                {
                    GRAD(btn) = GRAD(btn.tool);
                    config.btn.layer = config.btn.tool.frame;
                }
                else
                {
                    GRAD(btn) = GRAD(chooser);
                    config.btn.layer = config.chooser.layer;
                }

                drawButtonFrame(option, painter, widget, animStep);

                config.btn.round = !config.btn.round;
                config.btn.layer = btn_layer;
                config.btn.gradient = btn_grd;
            }
            else
                shadows.sunken[!config.btn.round][isEnabled].render(RECT, painter);
        }
    }

    // the arrow
    if (!ar.isNull())
    {
        if (!(ar.width()%2) )
            ar.setWidth(ar.width()-1);
        const int dy = ar.height()/4;
        QRect rect = ar.adjusted(0, dy, 0, -dy);

        Navi::Direction dir = Navi::S;
        bool upDown = false;
        if (listShown)
            dir = (config.leftHanded) ? Navi::E : Navi::W;
        else if (combo)
        {
            if (combo->currentIndex() == 0)
                dir = Navi::S;
            else if (combo->currentIndex() == combo->count()-1)
                dir = Navi::N;
            else
                upDown = true;
        }

        painter->save();
        painter->setPen(Qt::NoPen);
        if (cmb->editable)
        {
            if (upDown || dir == Navi::N)
                dir = Navi::S;
            upDown = false; // shall never look like spinbox!
            hover = hover && (cmb->activeSubControls == SC_ComboBoxArrow);
            if (!sunken)
            {
                painter->setBrush(FCOLOR(Base).dark(105));
                rect.translate(0, f2);
                drawArrow(dir, rect, painter);
                rect.translate(0, -f2);
            }
            if (hover || listShown)
                painter->setBrush(FCOLOR(Highlight));
            else
                painter->setBrush( Colors::mid(FCOLOR(Base), FCOLOR(Text)) );
        }
        else if (config.btn.backLightHover)
            painter->setBrush(Colors::mid(c, CONF_COLOR(btn.std, Fg), 6-animStep, 3+animStep));
        else
        {
            c = Colors::mid(c, CONF_COLOR(btn.active, Bg));
            c = Colors::mid(c, CONF_COLOR(btn.active, Bg), 6-animStep, animStep);
            masks.rect[!config.btn.round].render(ar, painter, GRAD(chooser), ori[1], c, RECT.height()-f2, QPoint(0, ar.y() - RECT.y()) );
            painter->setBrush(Colors::mid(c, CONF_COLOR(btn.active, Fg), 1,2));
        }
        if (upDown)
        {
            rect.setBottom(rect.y() + rect.height()/2);
            rect.translate(0, -1);
            drawArrow(Navi::N, rect, painter);
            rect.translate(0, rect.height());
            drawArrow(Navi::S, rect, painter);
        }
        else
        {
            if (dir == Navi::N) // loooks unbalanced otherwise
                rect.translate(0, -f1);
            drawArrow(dir, rect, painter);
        }
        painter->restore();
    }
}
Exemple #6
0
 bool Other<R>::is_grad_nan(Mat<R> a) {
     return GRAD(a).is_nan();
 }
Exemple #7
0
 void Other<R>::copy_grad(Mat<R>* dest, const Mat<R>& source) {
     GRAD(*dest) = GRAD(source).wrapper();
 }
Exemple #8
0
 bool Other<R>::grad_allclose(Mat<R> a, Mat<R> b, double tol) {
     if (a.dims() != b.dims())
         return false;
     return GRAD(a).allclose(GRAD(b), tol);
 }