double Angle(Pointf3 a, Pointf3 b) { double da = sqrt(Squared(a) * Squared(b)); if(da == 0) return 0; da = (a ^ b) / da; if(da <= -1) return M_PI; else if(da >= 1) return 0; else return acos(da); }
CryptoPP::Integer RecoverX (const CryptoPP::Integer& y) const { auto y2 = y.Squared (); auto xx = (y2 - CryptoPP::Integer::One())*(d*y2 + CryptoPP::Integer::One()).InverseMod (q); auto x = a_exp_b_mod_c (xx, (q + CryptoPP::Integer (3)).DividedBy (8), q); if (!(x.Squared () - xx).Modulo (q).IsZero ()) x = a_times_b_mod_c (x, I, q); if (x.IsOdd ()) x = q - x; return x; }
double RMSE(const DataVector &data, const PredictVector &predict, size_t len) { assert(data.size() >= len); assert(predict.size() >= len); double s = 0; double c = 0; for (size_t i = 0; i < data.size(); ++i) { s += Squared(predict[i] - data[i]->label) * data[i]->weight; c += data[i]->weight; } return std::sqrt(s / c); }
bool BVaabb::TestIntersection(BoundingVolume* pBV) const { Real R = pBV->GetRadius(); const auto& S = pBV->GetCenter(); Real dist_squared = R * R; const auto& C1 = mAABB.GetMin(); const auto& C2 = mAABB.GetMax(); if (S.x < C1.x) dist_squared -= Squared(S.x - C1.x); else if (S.x > C2.x) dist_squared -= Squared(S.x - C2.x); if (S.y < C1.y) dist_squared -= Squared(S.y - C1.y); else if (S.y > C2.y) dist_squared -= Squared(S.y - C2.y); if (S.z < C1.z) dist_squared -= Squared(S.z - C1.z); else if (S.z > C2.z) dist_squared -= Squared(S.z - C2.z); return dist_squared > 0; }
void Camera::Update() { direction = Unit(target - location); distance_delta = -(location ^ direction); viewing_distance = double(min(width_mm, height_mm) / (2e3 * tan(viewing_angle / 2.0))); Pointf3 up = Orthogonal(upwards, direction); if(Squared(up) <= 1e-10) up = Orthogonal(FarthestAxis(direction), direction); straight_up = Unit(up); straight_right = direction % straight_up; camera_matrix = Matrixf3(straight_right, straight_up, direction, location); invcam_matrix = Matrixf3Inverse(camera_matrix); double dw = viewing_distance * view_px * 2e3 / width_mm; double dh = viewing_distance * view_py * 2e3 / height_mm; double z_times = far_distance - near_distance; z_delta = -near_distance / z_times; transform_matrix = invcam_matrix * Matrixf3( Pointf3(dw / z_times, 0, 0), Pointf3(0, dh / z_times, 0), Pointf3(shift_x, shift_y, 1) / z_times); }
void GBDT::SquareLossProcess(DataVector *d, size_t samples, int i) { #ifdef USE_OPENMP #pragma omp parallel for #endif for (size_t j = 0; j < samples; ++j) { ValueType p = Predict(*(*d)[j], i); (*d)[j]->target = (*d)[j]->label - p; } if (g_conf.debug) { double s = 0; double c = 0; DataVector::iterator iter = d->begin(); for ( ; iter != d->end(); ++iter) { ValueType p = Predict(**iter, i); s += Squared((*iter)->label - p) * (*iter)->weight; c += (*iter)->weight; } std::cout << "rmse: " << std::sqrt(s / c) << std::endl; } }
NAMESPACE_UPP static void sQuadratic(LinearPathConsumer& t, const Pointf& p1, const Pointf& p2, const Pointf& p3, double qt, int lvl) { if(lvl < 16) { PAINTER_TIMING("Quadratic approximation"); Pointf d = p3 - p1; double q = Squared(d); if(q > 1e-30) { Pointf pd = p2 - p1; double u = (pd.x * d.x + pd.y * d.y) / q; if(u <= 0 || u >= 1 || SquaredDistance(u * d, pd) > qt) { Pointf p12 = Mid(p1, p2); Pointf p23 = Mid(p2, p3); Pointf div = Mid(p12, p23); sQuadratic(t, p1, p12, div, qt, lvl + 1); sQuadratic(t, div, p23, p3, qt, lvl + 1); return; } } } t.Line(p3); }
bool GetImpurity(DataVector *data, size_t len, int index, ValueType *value, double *impurity, double *gain) { *impurity = std::numeric_limits<double>::max(); *value = kUnknownValue; *gain = 0; #ifndef USE_OPENMP std::sort(data->begin(), data->begin() + len, TupleCompare(index)); #else __gnu_parallel::sort(data->begin(), data->begin() + len, TupleCompare(index)); #endif size_t unknown = 0; double s = 0; double ss = 0; double c = 0; while (unknown < len && (*data)[unknown]->feature[index] == kUnknownValue) { s += (*data)[unknown]->target * (*data)[unknown]->weight; ss += Squared((*data)[unknown]->target) * (*data)[unknown]->weight; c += (*data)[unknown]->weight; unknown++; } if (unknown == len) { return false; } double fitness0 = c > 1? (ss - s*s/c) : 0; if (fitness0 < 0) { // std::cerr << "fitness0 < 0: " << fitness0 << std::endl; fitness0 = 0; } s = 0; ss = 0; c = 0; for (size_t j = unknown; j < len; ++j) { s += (*data)[j]->target * (*data)[j]->weight; ss += Squared((*data)[j]->target) * (*data)[j]->weight; c += (*data)[j]->weight; } double fitness00 = c > 1? (ss - s*s/c) : 0; double ls = 0, lss = 0, lc = 0; double rs = s, rss = ss, rc = c; double fitness1 = 0, fitness2 = 0; for (size_t j = unknown; j < len-1; ++j) { s = (*data)[j]->target * (*data)[j]->weight; ss = Squared((*data)[j]->target) * (*data)[j]->weight; c = (*data)[j]->weight; ls += s; lss += ss; lc += c; rs -= s; rss -= ss; rc -= c; ValueType f1 = (*data)[j]->feature[index]; ValueType f2 = (*data)[j+1]->feature[index]; if (AlmostEqual(f1, f2)) continue; fitness1 = lc > 1? (lss - ls*ls/lc) : 0; if (fitness1 < 0) { // std::cerr << "fitness1 < 0: " << fitness1 << std::endl; fitness1 = 0; } fitness2 = rc > 1? (rss - rs*rs/rc) : 0; if (fitness2 < 0) { // std::cerr << "fitness2 < 0: " << fitness2 << std::endl; fitness2 = 0; } double fitness = fitness0 + fitness1 + fitness2; if (g_conf.feature_costs && g_conf.enable_feature_tunning) { fitness *= g_conf.feature_costs[index]; } if (*impurity > fitness) { *impurity = fitness; *value = (f1+f2)/2; *gain = fitness00 - fitness1 - fitness2; } } return *impurity != std::numeric_limits<double>::max(); }
Pointf3 Orthogonal(Pointf3 v, Pointf3 against) { return v - against * ((v ^ against) / Squared(against)); }
double TransferMatrixFunctions::T_hat(double na_, double nb_) const { return exp(-Squared(na_-nb_))*exp(-(m_potential->Value(na_) + m_potential->Value(nb_))/2); }
double TransferMatrixFunctions::T(double ea_, double eb_) const { return exp(-Squared(ea_-eb_)); }
double HarmonicPotential::Value(double eta_) const // The equation of a harmonic potential { return (m_parameters.sigma/m_parameters.kappa)*Squared(eta_); }
void Delaunay::AddHull(int i) { #ifdef DELDUMP RLOG("AddHull(" << i << ": " << points[i] << ")"); #endif ASSERT(tihull >= 0); Pointf newpt = points[i]; int hi = tihull; int vf = -1, vl = -1; bool was_out = true, fix_out = false; int im = -1; double nd2 = 1e300; do { const Triangle& t = triangles[hi]; Pointf t1 = At(t, 1), t2 = At(t, 2), tm = (t1 + t2) * 0.5; double d2 = Squared(t1 - newpt); if(d2 <= epsilon2) { #ifdef DELDUMP RLOG("-> too close to " << t[1] << ": " << t1); #endif return; // too close } if(d2 < nd2) { im = hi; nd2 = d2; } if((t2 - t1) % (newpt - tm) > epsilon2) { #ifdef DELDUMP RLOG("IN[" << hi << "], was_out = " << was_out); #endif if(was_out) vf = hi; if(!fix_out) vl = hi; was_out = false; } else { #ifdef DELDUMP RLOG("OUT[" << hi << "], was_out = " << was_out); #endif was_out = true; if(vl >= 0) fix_out = true; } hi = t.Next(1); } while(hi != tihull); if(vf < 0) { // collinear, extend fan Triangle& tm = triangles[im]; int in = tm.Next(2); Triangle& tn = triangles[in]; int j = tm[1]; int ia = triangles.GetCount(), ib = ia + 1; #ifdef DELDUMP RLOG("collinear -> connect " << im << " -> " << ia << " -> " << ib << " -> " << in); #endif triangles.Add().Set(-1, i, j); triangles.Add().Set(-1, j, i); LINK(ia, 0, ib, 0); LINK(ia, 2, ib, 1); LINK(ia, 1, im, 2); LINK(ib, 2, in, 1); } else { Triangle& tf = triangles[vf]; Triangle& tl = triangles[vl]; int xfn = tf.Next(2), xln = tl.Next(1); int xf = triangles.GetCount(), xl = xf + 1; #ifdef DELDUMP RLOG("adding vertex " << i << ": " << points[i] << " to hull between " << vf << " and " << vl); #endif triangles.Add().Set(-1, tf[1], i); triangles.Add().Set(-1, i, tl[2]); tihull = xf; tf[0] = i; for(int f = vf; f != vl; triangles[f = triangles[f].Next(1)][0] = i) ; LINK(xf, 0, vf, 2); LINK(xl, 0, vl, 1); LINK(xf, 2, xfn, 1); LINK(xl, 1, xln, 2); LINK(xf, 1, xl, 2); } }
void GBDT::Fit(DataVector *d) { delete[] trees; trees = new RegressionTree[g_conf.iterations]; size_t samples = d->size(); if (g_conf.data_sample_ratio < 1) { samples = static_cast<size_t>(d->size() * g_conf.data_sample_ratio); } Init(*d, d->size()); for (size_t i = 0; i < g_conf.iterations; ++i) { std::cout << "iteration: " << i << std::endl; if (samples < d->size()) { #ifndef USE_OPENMP std::random_shuffle(d->begin(), d->end()); #else __gnu_parallel::random_shuffle(d->begin(), d->end()); #endif } if (g_conf.loss == SQUARED_ERROR) { for (size_t j = 0; j < samples; ++j) { ValueType p = Predict(*(*d)[j], i); (*d)[j]->target = (*d)[j]->label - p; } if (g_conf.debug) { double s = 0; double c = 0; DataVector::iterator iter = d->begin(); for ( ; iter != d->end(); ++iter) { ValueType p = Predict(**iter, i); s += Squared((*iter)->label - p) * (*iter)->weight; c += (*iter)->weight; } std::cout << "rmse: " << std::sqrt(s / c) << std::endl; } } else if (g_conf.loss == LOG_LIKELIHOOD) { for (size_t j = 0; j < samples; ++j) { ValueType p = Predict(*(*d)[j], i); (*d)[j]->target = static_cast<ValueType>(LogitLossGradient((*d)[j]->label, p)); } if (g_conf.debug) { Auc auc; DataVector::iterator iter = d->begin(); for ( ; iter != d->end(); ++iter) { ValueType p = Logit(Predict(**iter, i)); auc.Add(p, (*iter)->label); } std::cout << "auc: " << auc.CalculateAuc() << std::endl; } } trees[i].Fit(d, samples); } // Calculate gain delete[] gain; gain = new double[g_conf.number_of_feature]; for (size_t i = 0; i < g_conf.number_of_feature; ++i) { gain[i] = 0.0; } for (size_t j = 0; j < iterations; ++j) { double *g = trees[j].GetGain(); for (size_t i = 0; i < g_conf.number_of_feature; ++i) { gain[i] += g[i]; } } }