void Pages::oauth(Document *doc){ if(path != "/oauth/yt" && path != "/oauth/yt/unlink") return; if(!Session::user()) return doc->redirect("/login?redirect=/account"); Youtube yt(Session::user().id); if(path == "/oauth/yt"){ std::string error = cgi("error"); std::string code = cgi("code"); if (!error.empty() || code.empty()) return doc->redirect("/account"); doc->setHtml("html/bsod.tpl", "Linking your YouTube account"); if (!code.empty() && yt.link(code)){ doc->dict()->SetValue("TITLE", "Success"); doc->dict()->SetValue("MESSAGE", "Your YouTube account is now linked to your EqBeats account."); } else { doc->dict()->SetValue("TITLE", "There was a problem"); doc->dict()->SetValue("MESSAGE", "Try again in a few minutes. If it still doesn't work, drop us a note."); } } else if (path == "/oauth/yt/unlink"){ yt.unlink(); doc->redirect("/account"); } }
OpenANN::Environment::State DoublePoleBalancing::rk4(const State& s, double force, const State& der) { const double TIME_DELTA = 0.01; State result(6); double hh = TIME_DELTA * 0.5, h6 = TIME_DELTA / 6.0; State dym(6); State dyt(6); State yt(6); yt = s + hh * der; dyt = derivative(yt, force); dyt(0) = yt(1); dyt(2) = yt(3); dyt(4) = yt(5); yt = s + hh * dyt; dym = derivative(yt, force); dym(0) = yt(1); dym(2) = yt(3); dym(4) = yt(5); yt = s + TIME_DELTA * dym; dym += dyt; dyt = derivative(yt, force); dyt(0) = yt(1); dyt(2) = yt(3); dyt(4) = yt(5); result = s + h6 * (der + dyt + 2.0 * dym); return result; }
void MyWidget::process() { ui->plainTextEdit->clear(); variant = ui->comboBox->currentIndex(); print(QString("Variant %1").arg(variant)); print(QString("[%1; %2]").arg(x0(), 0, 'g', 5).arg(xFinish(), 0, 'g', 3)); //calc qreal x = x0(), y = y0(), z = z0(); for (int i = 1; x < xFinish() || qFuzzyCompare(x, xFinish()); ++i) { qreal k1 = h() * f(x, y, z); qreal q1 = h() * g(x, y, z); qreal k2 = h() * f(x + h() / 2.0, y + k1 / 2.0, z + q1 / 2.0); qreal q2 = h() * g(x + h() / 2.0, y + k1 / 2.0, z + q1 / 2.0); qreal k3 = h() * f(x + h() / 2.0, y + k2 / 2.0, z + q2 / 2.0); qreal q3 = h() * g(x + h() / 2.0, y + k2 / 2.0, z + q2 / 2.0); qreal k4 = h() * f(x + h(), y + k3, z + q3); qreal q4 = h() * g(x + h(), y + k3, z + q3); print(QString("#%1").arg(i)); print(QString("y(%1) = %2").arg(x, 0, 'g', 5).arg(y, 0, 'g', 5)); print(QString("y_ex(%1) = %2").arg(x, 0, 'g',5).arg(yt(x), 0, 'g', 5)); print(QString("error(%1) = %2").arg(x, 0, 'g',5).arg(ypo(y, x), 0, 'g', 5)); x += h(); y += (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0; z += (q1 + 2.0 * q2 + 2.0 * q3 + q4) / 6.0; } }
void TEpidemModel::RungeKutta(const TFltV& y, const TFltV& dydx, double x, double h, TFltV& SirOutV) { const int n = y.Len(); IAssert(y.Len() == n && dydx.Len() == n); TFltV dym(n), dyt(n), yt(n); int i; double hh=h*0.5; double h6=h/6.0; double xh=x+hh; for (i=0; i < n; i++) { yt[i]=y[i]+hh*dydx[i]; } GetDerivs(xh, yt, dyt); for (i=0; i<n; i++) { yt[i]=y[i]+hh*dyt[i]; } GetDerivs(xh,yt,dym); for (i=0; i<n; i++) { yt[i]=y[i]+h*dym[i]; dym[i] += dyt[i]; } GetDerivs(x+h,yt,dyt); SirOutV.Clr(false); for (i=0; i<n; i++) { SirOutV.Add(y[i]+h6 * (dydx[i]+dyt[i]+2.0*dym[i])); } }
int main() { size_t n = 1000; std::vector<double> x, y; const double w = 0.05; const double a = n/2; for (size_t i=0; i<n; i++) { x.push_back(i); y.push_back(a*sin(w*i)); } std::vector<double> xt(2), yt(2); plt::title("Tangent of a sine curve"); plt::xlim(x.front(), x.back()); plt::ylim(-a, a); plt::axis("equal"); // Plot sin once and for all. plt::named_plot("sin", x, y); // Prepare plotting the tangent. plt::Plot plot("tangent"); plt::legend(); for (size_t i=0; i<n; i++) { if (i % 10 == 0) { update_window(x[i], y[i], a*w*cos(w*x[i]), xt, yt); // Just update data for this plot. plot.update(xt, yt); // Small pause so the viewer has a chance to enjoy the animation. plt::pause(0.1); } } }
Adaboost<MatType, WeakLearner>::Adaboost(const MatType& data, const arma::Row<size_t>& labels, int iterations, size_t classes, const WeakLearner& other) { // note: put a fail safe for the variable 'classes' or // remove it entirely by using unique function. int i, j, k; double rt, alphat = 0.0, zt; // To be used for prediction by the Weak Learner for prediction. arma::Row<size_t> predictedLabels(labels.n_cols); // Use tempData to modify input Data for incorporating weights. MatType tempData(data); // Build the classification Matrix yt from labels arma::mat yt(predictedLabels.n_cols, classes); // Build a classification matrix of the form D(i,l) // where i is the ith instance // l is the lth class. buildClassificationMatrix(yt, labels); // ht(x), to be loaded after a round of prediction every time the weak // learner is run, by using the buildClassificationMatrix function arma::mat ht(predictedLabels.n_cols, classes); // This matrix is a helper matrix used to calculate the final hypothesis. arma::mat sumFinalH(predictedLabels.n_cols, classes); sumFinalH.fill(0.0); // load the initial weights into a 2-D matrix const double initWeight = (double) 1 / (data.n_cols * classes); arma::mat D(data.n_cols, classes); D.fill(initWeight); // D.print("The value of D after initialization."); // Weights are to be compressed into this rowvector // for focussing on the perceptron weights. arma::rowvec weights(predictedLabels.n_cols); // weights.print("This is the value of weight just after initialization."); // This is the final hypothesis. arma::rowvec finalH(predictedLabels.n_cols); // now start the boosting rounds for (i = 0; i < iterations; i++) { std::cout<<"Run "<<i<<" times !\n"; // Initialized to zero in every round. rt = 0.0; zt = 0.0; // Build the weight vectors D.print("The value of D in each iteration, just before going for training."); buildWeightMatrix(D, weights); // D.print("This is the value of D, before sending off to modify data"); // call the other weak learner and train the labels. WeakLearner w(other, tempData, weights, labels); w.Classify(tempData, predictedLabels); //Now from predictedLabels, build ht, the weak hypothesis buildClassificationMatrix(ht, predictedLabels); // Now, start calculation of alpha(t) using ht // begin calculation of rt for (j = 0;j < ht.n_rows; j++) { for (k = 0;k < ht.n_cols; k++) rt += (D(j,k) * yt(j,k) * ht(j,k)); } // end calculation of rt alphat = 0.5 * log((1 + rt) / (1 - rt)); // end calculation of alphat // now start modifying weights for (j = 0;j < D.n_rows; j++) { for (k = 0;k < D.n_cols; k++) { // we calculate zt, the normalization constant zt += D(j,k) * exp(-1 * alphat * yt(j,k) * ht(j,k)); D(j,k) = D(j,k) * exp(-1 * alphat * yt(j,k) * ht(j,k)); // adding to the matrix of FinalHypothesis sumFinalH(j,k) += (alphat * ht(j,k)); } } // normalization of D D = D / zt; } // Iterations are over, now build a strong hypothesis // from a weighted combination of these weak hypotheses. // This step of storing it in a temporary row vector can be improved upon ? arma::rowvec tempSumFinalH; arma::uword max_index; for (i = 0;i < sumFinalH.n_rows; i++) { tempSumFinalH = sumFinalH.row(i); tempSumFinalH.max(max_index); finalH(i) = max_index; } // labels.print("These are the labels."); finalH.print("This is the final hypothesis."); int counterror = 0; for (i = 0; i < labels.n_cols; i++) if(labels(i) != finalH(i)) { std::cout<<i<<"th prediction not correct!\n"; counterror++; } std::cout<<"There are "<<counterror<<" number of misclassified records.\n"; std::cout<<"The error rate is: "<<(double)counterror/labels.n_cols; //finalH is the final hypothesis. }
void graph(FILE * fp) { static char buf[BUFLNG], arg[BUFLNG / 2], xtype[16], ytype[16]; static double xa, ya, xap, yap, xmin, xmax, ymin, ymax; static double xs = -NSCALE, ys = -NSCALE; int n, c; char *s, *p; double x, y, lpt, th, dt, lscale, rad; int is_grid, old_lbl = 0; char xory; h *= fct; w *= fct; for (n = 0; (s = fgets(buf, BUFLNG, fp));) { s = getarg(s, arg); if (s == NULL || *arg == '#'); /* comment line */ else if ((!is_t && strcmp(arg, "x") == 0) || (is_t && strcmp(arg, "y") == 0)) { s = gettyp(s, xtype); if (sscanf(s, "%lf %lf %lf", &xmin, &xmax, &xa) != 3) xa = xmin; if (strncmp(xtype, "log", 3) == 0) { xmin = log10(xmin); xmax = log10(xmax); xa = log10(xa); is_xlog = (xtype[3] == '*') ? -1 : 1; } xfct = xl / (xmax - xmin); xap = (xa - xmin) * xfct; x00 = -xmin * xfct; } else if ((!is_t && strcmp(arg, "y") == 0) || (is_t && strcmp(arg, "x") == 0)) { s = gettyp(s, ytype); if (sscanf(s, "%lf %lf %lf", &ymin, &ymax, &ya) != 3) ya = ymin; if (strncmp(ytype, "log", 3) == 0) { ymin = log10(ymin); ymax = log10(ymax); ya = log10(ya); is_ylog = (ytype[3] == '*') ? -1 : 1; } yfct = (yl) ? yl / (ymax - ymin) : 0; yap = (ya - ymin) * yfct; y00 = -ymin * yfct; } else if ((!is_t && strncmp(arg, "xscale", 6) == 0) || (is_t && strncmp(arg, "yscale", 6) == 0)) { is_grid = *(arg + 6); if (type < 0 || (ya != ymin && ya != ymax)) { plot(0.0, yap, 3); plot(xl, yap, 2); } ys = yap - h - MSCALE; while ((s = getarg(s, p = arg)) != NULL) { if (*p != '"') { x = atof((is_number(*p)) ? p : p + 1); if (strncmp(xtype, "mel", 3) == 0) x = argapf(x / nz(xmax, xmin), atof(xtype + 3)) * nz(xmax, xmin); else if (is_xlog) x = log10(x); x = (x - xmin) * xfct; lscale = (*p == 's') ? LSCALE / 2 : LSCALE; if (*p != '\\' && *p != '@') { plot(x, yap, 3); plot(x, yap + lscale, 2); if (type > 0 && !is_grid && yap == 0) { plot(x, yl, 3); plot(x, yl - lscale, 2); } } else if (*p == '\\') ++p; } if (is_number(*p) || *p++ == '"') _symbol(x - sleng(p, h, w) / 2, ys - ysadj(), p, h, w, 0.0); } } else if ((!is_t && strncmp(arg, "yscale", 6) == 0) || (is_t && strncmp(arg, "xscale", 6) == 0)) { is_grid = *(arg + 6); if (type < 0 || (xa != xmin && xa != xmax)) { plot(xap, 0.0, 3); plot(xap, yl, 2); } while ((s = getarg(s, p = arg)) != NULL) { if (*p != '"') { y = atof((is_number(*p)) ? p : p + 1); if (strncmp(ytype, "mel", 3) == 0) y = argapf(y / nz(ymax, ymin), atof(ytype + 3)) * nz(ymax, ymin); else if (is_ylog) y = log10(y); y = (y - ymin) * yfct; lscale = (*p == 's') ? LSCALE / 2 : LSCALE; if (*p != '\\' && *p != '@') { plot(xap, y, 3); plot(xap + lscale, y, 2); if (type > 0 && !is_grid && xap == 0) { plot(xl, y, 3); plot(xl - lscale, y, 2); } } else if (*p == '\\') ++p; } if (is_number(*p) || *p++ == '"') { x = xap - sleng(p, h, w) - MSCALE; if (x < xs) xs = x; _symbol(x, y - h * 0.5, p, h, w, 0.0); } } } else if (strcmp(arg + 1, "grid") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); if ((!is_t && (*arg == 'x')) || (is_t && (*arg == 'y'))) { ybuf[0] = 0; ybuf[1] = yl; while ((s = getarg(s, arg)) != NULL) { x = atof(arg); if (is_xlog) x = log10(x); xbuf[0] = xbuf[1] = (x - xmin) * xfct; draw_fig0(xbuf, ybuf, 2, wf, hf, fct); } } else { xbuf[0] = 0; xbuf[1] = xl; while ((s = getarg(s, arg)) != NULL) { y = atof(arg); if (is_ylog) y = log10(y); ybuf[0] = ybuf[1] = (y - ymin) * yfct; draw_fig0(xbuf, ybuf, 2, wf, hf, fct); } } n = 0; } else if (strcmp(arg + 1, "circle") == 0) { xory = *arg; s = getarg(s, arg); x = xt(atof(arg)); s = getarg(s, arg); y = yt(atof(arg)); swap(&x, &y); x = xfct * x + x00; y = yfct * y + y00; while ((s = getarg(s, arg)) != NULL) { if ((!is_t && xory == 'x') || (is_t && xory == 'y')) rad = xt(atof(arg)) * xfct; else rad = yt(atof(arg)) * yfct; pntstyl(ptyp); circle(x, y, rad, rad, 0., 360.); } } else if (strcmp(arg, "circle") == 0) { s = getarg(s, arg); x = xt(atof(arg)); s = getarg(s, arg); y = yt(atof(arg)); swap(&x, &y); x = xfct * x + x00; y = yfct * y + y00; while ((s = getarg(s, arg)) != NULL) { rad = atof(arg); pntstyl(ptyp); circle(x, y, rad, rad, 0., 360.); } } else if (strcmp(arg + 1, "name") == 0) { s = getname(s, p = arg + 1); if ((!is_t && *arg == 'x') || (is_t && *arg == 'y')) _symbol((xl - sleng(s, h, w)) / 2, (*p) ? -atof(p) - h : ys - h - NSCALE, s, h, w, 0.0); else _symbol((*p) ? -atof(p) : xs - MSCALE, (yl - sleng(s, h, w)) / 2, s, h, w, 90.0); } else if (strncmp(arg, "title", 5) == 0 || strncmp(arg, "print", 5) == 0) { sscanf(s, "%lf %lf", &x, &y); swap(&x, &y); if (*arg == 'p') { x = xfct * xt(x) + x00; y = yfct * yt(y) + y00; } s = gettxt_fig(s); th = getarg(s + strlen(s) + 1, arg) ? atof(arg) : 0; if (*(arg + 5)) { x -= rx(LADJ * h / 2, h / 2, th); y -= ry(LADJ * h / 2, h / 2, th); } _symbol(x, y, s, h, w, th); } else if (strcmp(arg, "eod") == 0 || strcmp(arg, "EOD") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); n = 0; } else if (strcmp(arg, "pen") == 0) { n = flush(xbuf, ybuf, n, wf, hf, fct); pen(atoi(s)); } else if (strcmp(arg, "join") == 0) { n = flush(xbuf, ybuf, n, wf, hf, fct); join(atoi(s)); } else if (strcmp(arg, "csize") == 0) { if (sscanf(s, "%lf %lf", &h, &w) != 2) w = h; } else if (strcmp(arg, "hight") == 0) { if (sscanf(s, "%lf %lf", &mh, &mw) != 2) mw = mh; } else if (strcmp(arg, "line") == 0) { n = flush(xbuf, ybuf, n, wf, hf, fct); if (sscanf(s, "%d %lf", <ype, &lpt) != 2) { if (ltype > 0) lpt = lpit[ltype - 1]; } if (--ltype >= 0) mode(lmod[ltype], lpt); } else if (strcmp(arg, "italic") == 0) italic(atof(s)); else if (strcmp(arg, "mark") == 0) { while (*s == ' ' || *s == '\t') ++s; if (*s == '\\' && *(s + 1) == '0') *label = '\0'; else strcpy(label, s); } else if (strcmp(arg, "paint") == 0) { sscanf(s, "%d %lf %lf", &ptyp, &dhat, &that); } else if (strcmp(arg, "clip") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); for (n = 0; (s = getarg(s, arg)) != NULL; ++n) { x = xt(atof(arg)); if ((s = getarg(s, arg)) == NULL) break; y = yt(atof(arg)); swap(&x, &y); xbuf[n] = xfct * x + x00; ybuf[n] = yfct * y + y00; } if (n == 0) { xclip0 = yclip0 = 0; xclip1 = xl; yclip1 = yl; swap(&xclip1, &yclip1); } else if (n == 2) { xclip0 = xbuf[0]; yclip0 = ybuf[0]; xclip1 = xbuf[1]; yclip1 = ybuf[1]; } n = 0; } else if (strcmp(arg, "box") == 0) { draw_fig0(xbuf, ybuf, n, wf, hf, fct); for (n = 0; (s = getarg(s, arg)) != NULL; ++n) { x = xt(atof(arg)); if ((s = getarg(s, arg)) == NULL) break; y = yt(atof(arg)); swap(&x, &y); xbuf[n] = xfct * x + x00; ybuf[n] = yfct * y + y00; } if (n == 2) { xbuf[2] = xbuf[1]; ybuf[3] = ybuf[2] = ybuf[1]; ybuf[1] = ybuf[0]; xbuf[3] = xbuf[0]; n = 4; } polyg(xbuf, ybuf, n, wf, hf, fct); n = 0; } else { x = xt(atof(arg)); s = getarg(s, arg); y = yt(atof(arg)); swap(&x, &y); xbuf[n] = x = xfct * x + x00; ybuf[n] = y = yfct * y + y00; if (is_in(x, y) && ((s = getarg(s, arg)) || *label || old_lbl > 0)) { c = 0; if (s || *label) { if (s == NULL) s = getarg(label, arg); if (*arg == '\\' && (abs(c = atoi(arg + 1))) < 16) mark(abs(c), &x, &y, 1, mh, 1); else if (abs(c) == 16) { pntstyl(ptyp); circle(x, y, mh / 2, mh / 2, 0., 360.); } else { if (c) { *arg = c; *(arg + 1) = '\0'; } _symbol(x - LADJ * h / 2, y - w / 2, arg, h, w, atof(s)); } } if (c > 0) n = flush(xbuf, ybuf, n, wf, hf, fct); if ((c > 0 || old_lbl > 0) && n) { dt = atan2(y - ybuf[0], x - xbuf[0]); if (old_lbl > 0) { xbuf[0] += MADJ * mh * cos(dt); ybuf[0] += MADJ * mh * sin(dt); } if (c > 0) { xbuf[1] -= MADJ * mh * cos(dt); ybuf[1] -= MADJ * mh * sin(dt); } draw_fig0(xbuf, ybuf, 2, wf, hf, fct); xbuf[0] = x; ybuf[0] = y; n = 0; } old_lbl = c; } if (++n >= BUFLNG) n = flush(xbuf, ybuf, n, wf, hf, fct); } } draw_fig0(xbuf, ybuf, n, wf, hf, fct); }
void YoungTableauTest::Init(void) { Add("Test 1", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(4, 5); yt.SortRows(A, 20); Logger().Print(A, 20, yt.Columns()); yt.SortColumns(A, 20); Logger().Print(A, 20, yt.Columns()); ASSERT1(yt.Verify(A, 20)); }); Add("Test 2", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt1(4, 5, A, 20); Logger() << yt1; ASSERT1(yt1.Verify()); Test::YoungTableau<int> yt2(4, 5); yt2.Init(A, 20); Logger() << yt2; ASSERT1(yt2.Verify()); yt2.Create(A, 20); Logger().Print(A, 20, yt2.Columns()); ASSERT1(yt2.Verify(A, 20)); }); Add("Test 3", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(5, 4, A, 20); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 4", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(3, 7); yt.Push(A, 20); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 5", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(7, 3); yt.Init(A, 20); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 6", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(2, 13); yt.Init(A, 20); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 7", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(2, 19); yt.Push(A, 20); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 8", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(1, 20); yt.Init(A, 20); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 9", [&]() { int A[20]; for (int i = 0; i < 20; i++) { A[i] = 20 - i; } Test::YoungTableau<int> yt(1, 21, A, 20); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 10", [&]() { int A = 1; Test::YoungTableau<int> yt(1, 21); yt.Push(&A, 1); Logger() << yt; ASSERT1(yt.Verify()); }); Add("Test 11", [&]() { for (int i = 0; i < 100; i++) { int lenA = 1 + Test::Random::Next(); unique_ptr<int[]> A(new int[lenA]); int lenB = 1 + Test::Random::Next(); unique_ptr<int[]> B(new int[lenB]); for (int j = 0; j < lenA; j++) { A[j] = Test::Random::Next(); } for (int j = 0; j < lenB; j++) { B[j] = Test::Random::Next(); } int len = max(500, max(lenA, lenB) >> 3); int cols = Test::Random::Next(1, len >> 3); int rows = Test::Random::Next(1, (len / cols) << 3); Logger().WriteInformation("Run %d, tableau %d X %d, , %d elements and then push %d elements\n", i, rows, cols, lenA, lenB); Test::YoungTableau<int> yt(rows, cols); yt.Push(A.get(), lenA); ASSERT1(yt.Verify()); yt.Push(B.get(), lenB); ASSERT1(yt.Verify()); } });
void OrganizedData::process(RawData* raw, int nBlock, double pTest, int nSupport) { int nData = raw->nData, nDim = raw->nDim - 1; this->nSupport = nSupport; this->nBlock = nBlock; this->nDim = nDim; train = field<mat>(nBlock,2); test = field<mat>(nBlock,2); support = field<mat>(1,2); mat xm(nSupport,nDim), ym(nSupport,1); vec mark(nData); mark.fill(0); printf("Randomly selecting %d supporting point ...\n", nSupport); for (int i = 0; i < nSupport; i++) { int pos = IRAND(0, nData - 1); while (mark[pos] > 0) pos = IRAND(0, nData - 1); mark[pos] = 1; for (int j = 0; j < nDim; j++) xm(i, j) = raw->X(pos,j); ym(i,0) = raw->X(pos,nDim); } support(0,0) = xm; xm.clear(); support(0,1) = ym; ym.clear(); cout << "Partitioning the remaining data into " << nBlock << " cluster using K-Mean ..." << endl; vvd _remain; for (int i = 0; i < nData; i++) if (!mark(i)) { rowvec R = raw->X.row(i); _remain.push_back(r2v(R)); } mat remaining = v2m(_remain); mark.clear(); RawData* remain = new RawData(remaining); KMean* partitioner = new KMean(remain); Partition* clusters = partitioner->cluster(nBlock); cout << "Packaging training/testing data points into their respective cluster" << endl; for (int i = 0; i < nBlock; i++) { cout << "Processing block " << i + 1 << endl; int bSize = (int) clusters->member[i].size(), tSize = (int) floor(bSize * pTest), pos = 0, counter = 0; mark = vec(bSize); mark.fill(0); if (bSize > tSize) // if we can afford to draw tSize test points from this block without depleting it ... { mat xt(tSize,nDim), yt(tSize,1); for (int j = 0; j < tSize; j++) { pos = IRAND(0, bSize - 1); while (mark[pos] > 0) pos = IRAND(0, bSize - 1); mark[pos] = 1; pos = clusters->member[i][pos]; for (int t = 0; t < nDim; t++) xt(j, t) = remain->X(pos,t); yt(j,0) = remain->X(pos,nDim); } bSize -= tSize; nTest += tSize; test(i,0) = xt; xt.clear(); test(i,1) = yt; yt.clear(); } nTrain += bSize; mat xb(bSize,nDim), yb(bSize,1); //cout << remain->X.n_rows << endl; for (int j = 0; j < (int)mark.n_elem; j++) if (mark[j] < 1) { for (int t = 0; t < nDim; t++) { xb(counter,t) = remain->X(clusters->member[i][j],t); } yb(counter++,0) = remain->X(clusters->member[i][j],nDim); } train(i,0) = xb; xb.clear(); train(i,1) = yb; yb.clear(); mark.clear(); printf("Done ! nData[%d] = %d, nTrain[%d] = %d, nTest[%d] = %d .\n", i, (int) clusters->member[i].size(), i, train(i,0).n_rows, i, (int) test(i,0).n_rows); } }
void arrangement::compute_ACScellBeginEnd() { int ncr_begin = -1; int ncr_end = -1; Walk_pl walk_pl(nonCriticalRegions); // get NCR start Rational xt(target_centre.x()); Rational yt(target_centre.y()); Conic_point_2 pt(xt,yt); Arrangement_2::Vertex_handle v = insert_point(nonCriticalRegions, pt, walk_pl); try { ncr_begin = v->face()->data(); nonCriticalRegions.remove_isolated_vertex(v); } catch (const std::exception exn) { target_centre = QPoint(target_centre.x()+1, target_centre.y()+1); compute_ACScellBeginEnd(); return; } // get NCR end Rational xe(target_centre_end.x()); Rational ye(target_centre_end.y()); Conic_point_2 pe(xe,ye); Arrangement_2::Vertex_handle w = insert_point(nonCriticalRegions, pe, walk_pl); try { ncr_end = w->face()->data(); nonCriticalRegions.remove_isolated_vertex(w); } catch (const std::exception exn) { target_centre_end = QPoint(target_centre_end.x()+1, target_centre_end.y()+1); compute_ACScellBeginEnd(); return; } // retrieve ACScell for (int i = 0; i < (int) ACScells.size(); i++) if (ACScells[i].NCR == ncr_begin) { Rational x(manipulator_centre.x()); Rational y(manipulator_centre.y()); Conic_point_2 p(x,y); Walk_pl walk(ACScells[i].arr); Arrangement_2::Vertex_handle u = insert_point(ACScells[i].arr, p, walk); try { if (!u->face()->is_unbounded()) { ACScells[i].arr.remove_isolated_vertex(u); AcscellBegin = i; break; } } catch (const std::exception exn) { manipulator_centre = QPoint(manipulator_centre.x()+1, manipulator_centre.y()+1); compute_ACScellBeginEnd(); return; } } for (int i = 0; i < (int) ACScells.size(); i++) if (ACScells[i].NCR == ncr_end) { Rational x(manipulator_centre_end.x()); Rational y(manipulator_centre_end.y()); Conic_point_2 p(x,y); Walk_pl walk(ACScells[i].arr); Arrangement_2::Vertex_handle u = insert_point(ACScells[i].arr, p, walk); try { if (!u->face()->is_unbounded()) { ACScells[i].arr.remove_isolated_vertex(u); AcscellEnd = i; break; } } catch (const std::exception exn) { manipulator_centre_end = QPoint(manipulator_centre_end.x()+1, manipulator_centre_end.y()+1); compute_ACScellBeginEnd(); return; } } }
Transformation Boonas:: parseParams(int start, int argc, char** argv, Transformation matrix) { QString currIn; for(int i = start; i < (argc - 1); i += 2) // iterate through parameters skiping every other { if(strcmp(argv[i], "XT") == 0) { currIn = argv[i + 1]; matrix = xt(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "YT") == 0) { currIn = argv[i + 1]; matrix = yt(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "ZT") == 0) { currIn = argv[i + 1]; matrix = zt(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "XS") == 0) { currIn = argv[i + 1]; matrix = xs(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "YS") == 0) { currIn = argv[i + 1]; matrix = ys(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "ZS") == 0) { currIn = argv[i + 1]; matrix = zs(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "US") == 0) { currIn = argv[i + 1]; matrix = us(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "XD") == 0) { currIn = argv[i + 1]; matrix = xd(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "YD") == 0) { currIn = argv[i + 1]; matrix = yd(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "ZD") == 0) { currIn = argv[i + 1]; matrix = zd(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "XR") == 0) { currIn = argv[i + 1]; matrix = xr(currIn.toFloat(), matrix); } else if(strcmp(argv[i], "YR") == 0) { currIn = argv[i + 1]; matrix = yr(currIn.toFloat(), matrix); } else // MUST BE ZR { currIn = argv[i + 1]; matrix = zr(currIn.toFloat(), matrix); } } return matrix; }
void Boonas::doRotate(QString outfile, QString vectx, QString vecty, QString vectz, QString px, QString py, QString pz, QString divisions, QString degrees) { Point centerP(px.toFloat(), py.toFloat(), pz.toFloat()); Vector centerV(vectx.toFloat(), vecty.toFloat(), vectz.toFloat()); Transformation matrix; float theta; float phi; float angleOfRot = degrees.toFloat() / divisions.toFloat(); // translate to x matrix = xt(-px.toFloat(), matrix); matrix = yt(-py.toFloat(), matrix); matrix = zt(-pz.toFloat(), matrix); if(vectz.toFloat() == 0) { if((vectx.toFloat() == 1) && (vectz.toFloat() == 0)) { theta = 1.57079633; } else { theta = 0.0; } } else { theta = atan(vectx.toFloat() / vectz.toFloat()); } matrix = yr(-theta, matrix); phi = atan(vecty.toFloat() / (sqrt((vectx.toFloat() * vectx.toFloat()) + (vectz.toFloat() * vectz.toFloat())))); matrix = xr(phi, matrix); // apply matrix doMult(matrix); // do rotates and divisions along Z doZrm(outfile, angleOfRot, divisions.toFloat()); // have to re-read from a temp file after this delete orig; orig = new LNHHolder(); readTfile(outfile); //unrotate matrix = Transformation(); matrix = xr(-phi, matrix); matrix = yr(theta, matrix); // untranslate matrix = xt(px.toFloat(), matrix); matrix = yt(py.toFloat(), matrix); matrix = zt(pz.toFloat(), matrix); // apply matrix doMult(matrix); return; }