/* ============= idPolynomial::GetRoots ============= */ int idPolynomial::GetRoots( float *roots ) const { int i, num; idComplex *complexRoots; switch( degree ) { case 0: return 0; case 1: return GetRoots1( coefficient[1], coefficient[0], roots ); case 2: return GetRoots2( coefficient[2], coefficient[1], coefficient[0], roots ); case 3: return GetRoots3( coefficient[3], coefficient[2], coefficient[1], coefficient[0], roots ); case 4: return GetRoots4( coefficient[4], coefficient[3], coefficient[2], coefficient[1], coefficient[0], roots ); } // The Abel-Ruffini theorem states that there is no general solution // in radicals to polynomial equations of degree five or higher. // A polynomial equation can be solved by radicals if and only if // its Galois group is a solvable group. complexRoots = ( idComplex * ) _alloca16( degree * sizeof( idComplex ) ); GetRoots( complexRoots ); for( num = i = 0; i < degree; i++ ) { if( complexRoots[i].i == 0.0f ) { roots[i] = complexRoots[i].r; num++; } } return num; }
void UnionFind::EnumerateSets(std::vector<std::vector<int> >& sets) const { std::vector<int> roots; GetRoots(roots); std::map<int,size_t> rootMap; for(size_t i=0;i<roots.size();i++) rootMap[roots[i]] = i; sets.resize(roots.size()); for(size_t i=0;i<parents.size();i++) sets[rootMap[FindRoot((int)i)]].push_back((int)i); }
// Stringify the data-structure std::string ToString() const { std::stringstream ss; std::set<int> roots = GetRoots(); ss << "{ "; for (std::set<int>::const_iterator rit = roots.begin(); rit != roots.end(); ++rit) { ss << "[ " << *rit ; for (elements_t::const_iterator eit=elements_.begin(); eit!=elements_.end();++eit) { if (*eit != *rit && FindSet(*eit) == *rit) { ss << ", " << *eit; } } ss << " ] "; } ss << "}"; return ss.str(); }
void WelcomePage::ShowItems () { listBox.ResetContent (); if (! SessionWrapper(true)->IsMiKTeXDirect()) { if (listBox.AddString(T_(_T("Installed packages"))) < 0) { FATAL_WINDOWS_ERROR ("CListBox::AddString", 0); } } if (! SessionWrapper(true)->IsMiKTeXDirect()) { if (listBox.AddString(T_(_T("Shortcuts"))) < 0) { FATAL_WINDOWS_ERROR ("CListBox::AddString", 0); } } if (! SessionWrapper(true)->IsMiKTeXDirect()) { if (listBox.AddString(T_(_T("MiKTeX registry settings"))) < 0) { FATAL_WINDOWS_ERROR ("CListBox::AddString", 0); } } if (thoroughly != FALSE) { vector<PathName> vec = GetRoots(); for (vector<PathName>::const_iterator it = vec.begin(); it != vec.end(); ++ it) { if (listBox.AddString(it->ToWideCharString().c_str()) < 0) { FATAL_WINDOWS_ERROR ("CListBox::AddString", 0); } } } }
typename IntrEllipsoid3Ellipsoid3<Real>::Classification IntrEllipsoid3Ellipsoid3<Real>::GetClassification () const { // Get the parameters of ellipsoid0. Vector3<Real> K0 = mEllipsoid0->Center; Matrix3<Real> R0(mEllipsoid0->Axis, true); Matrix3<Real> D0( ((Real)1)/(mEllipsoid0->Extent[0]*mEllipsoid0->Extent[0]), ((Real)1)/(mEllipsoid0->Extent[1]*mEllipsoid0->Extent[1]), ((Real)1)/(mEllipsoid0->Extent[2]*mEllipsoid0->Extent[2])); // Get the parameters of ellipsoid1. Vector3<Real> K1 = mEllipsoid1->Center; Matrix3<Real> R1(mEllipsoid1->Axis, true); Matrix3<Real> D1( ((Real)1)/(mEllipsoid1->Extent[0]*mEllipsoid1->Extent[0]), ((Real)1)/(mEllipsoid1->Extent[1]*mEllipsoid1->Extent[1]), ((Real)1)/(mEllipsoid1->Extent[2]*mEllipsoid1->Extent[2])); // Compute K2. Matrix3<Real> D0NegHalf( mEllipsoid0->Extent[0], mEllipsoid0->Extent[1], mEllipsoid0->Extent[2]); Matrix3<Real> D0Half( ((Real)1)/mEllipsoid0->Extent[0], ((Real)1)/mEllipsoid0->Extent[1], ((Real)1)/mEllipsoid0->Extent[2]); Vector3<Real> K2 = D0Half*((K1 - K0)*R0); // Compute M2. Matrix3<Real> R1TR0D0NegHalf = R1.TransposeTimes(R0*D0NegHalf); Matrix3<Real> M2 = R1TR0D0NegHalf.TransposeTimes(D1)*R1TR0D0NegHalf; // Factor M2 = R*D*R^T. Matrix3<Real> R, D; M2.EigenDecomposition(R, D); // Compute K = R^T*K2. Vector3<Real> K = K2*R; // Transformed ellipsoid0 is Z^T*Z = 1 and transformed ellipsoid1 is // (Z-K)^T*D*(Z-K) = 0. // The minimum and maximum squared distances from the origin of points on // transformed ellipsoid1 are used to determine whether the ellipsoids // intersect, are separated, or one contains the other. Real minSqrDistance = Math<Real>::MAX_REAL; Real maxSqrDistance = (Real)0; int i; if (K == Vector3<Real>::ZERO) { // The special case of common centers must be handled separately. It // is not possible for the ellipsoids to be separated. for (i = 0; i < 3; ++i) { Real invD = ((Real)1)/D[i][i]; if (invD < minSqrDistance) { minSqrDistance = invD; } if (invD > maxSqrDistance) { maxSqrDistance = invD; } } if (maxSqrDistance < (Real)1) { return EC_ELLIPSOID0_CONTAINS_ELLIPSOID1; } else if (minSqrDistance > (Real)1) { return EC_ELLIPSOID1_CONTAINS_ELLIPSOID0; } else { return EC_ELLIPSOIDS_INTERSECTING; } } // The closest point P0 and farthest point P1 are solutions to // s0*D*(P0 - K) = P0 and s1*D*(P1 - K) = P1 for some scalars s0 and s1 // that are roots to the function // f(s) = d0*k0^2/(d0*s-1)^2+d1*k1^2/(d1*s-1)^2+d2*k2^2/(d2*s-1)^2-1 // where D = diagonal(d0,d1,d2) and K = (k0,k1,k2). Real d0 = D[0][0], d1 = D[1][1], d2 = D[2][2]; Real c0 = K[0]*K[0], c1 = K[1]*K[1], c2 = K[2]*K[2]; // Sort the values so that d0 >= d1 >= d2. This allows us to bound the // roots of f(s), of which there are at most 6. std::vector<std::pair<Real,Real> > param(3); param[0] = std::make_pair(d0, c0); param[1] = std::make_pair(d1, c1); param[2] = std::make_pair(d2, c2); std::sort(param.begin(), param.end(), std::greater<std::pair<Real,Real> >()); std::vector<std::pair<Real,Real> > valid; valid.reserve(3); if (param[0].first > param[1].first) { if (param[1].first > param[2].first) { // d0 > d1 > d2 for (i = 0; i < 3; ++i) { if (param[i].second > (Real)0) { valid.push_back(param[i]); } } } else { // d0 > d1 = d2 if (param[0].second > (Real)0) { valid.push_back(param[0]); } param[1].second += param[0].second; if (param[1].second > (Real)0) { valid.push_back(param[1]); } } } else { if (param[1].first > param[2].first) { // d0 = d1 > d2 param[0].second += param[1].second; if (param[0].second > (Real)0) { valid.push_back(param[0]); } if (param[2].second > (Real)0) { valid.push_back(param[2]); } } else { // d0 = d1 = d2 param[0].second += param[1].second + param[2].second; if (param[0].second > (Real)0) { valid.push_back(param[0]); } } } size_t numValid = valid.size(); int numRoots; Real roots[6]; if (numValid == 3) { GetRoots( valid[0].first, valid[1].first, valid[2].first, valid[0].second, valid[1].second, valid[2].second, numRoots, roots); } else if (numValid == 2) { GetRoots( valid[0].first, valid[1].first, valid[0].second, valid[1].second, numRoots, roots); } else if (numValid == 1) { GetRoots( valid[0].first, valid[0].second, numRoots, roots); } else { // numValid cannot be zero because we already handled case K = 0 assertion(false, "Unexpected condition.\n"); return EC_ELLIPSOIDS_INTERSECTING; } for (i = 0; i < numRoots; ++i) { Real s = roots[i]; Real p0 = d0*K[0]*s/(d0*s - (Real)1); Real p1 = d1*K[1]*s/(d1*s - (Real)1); Real p2 = d2*K[2]*s/(d2*s - (Real)1); Real sqrDistance = p0*p0 + p1*p1 + p2*p2; if (sqrDistance < minSqrDistance) { minSqrDistance = sqrDistance; } if (sqrDistance > maxSqrDistance) { maxSqrDistance = sqrDistance; } } if (maxSqrDistance < (Real)1) { return EC_ELLIPSOID0_CONTAINS_ELLIPSOID1; } if (minSqrDistance > (Real)1) { if (d0*c0 + d1*c1 + d2*c2 > (Real)1) { return EC_ELLIPSOIDS_SEPARATED; } else { return EC_ELLIPSOID1_CONTAINS_ELLIPSOID0; } } return EC_ELLIPSOIDS_INTERSECTING; }
void compact(void) { nat g, s, blocks; step *stp; // 1. thread the roots GetRoots((evac_fn)thread); // the weak pointer lists... if (weak_ptr_list != NULL) { thread((void *)&weak_ptr_list); } if (old_weak_ptr_list != NULL) { thread((void *)&old_weak_ptr_list); // tmp } // mutable lists for (g = 1; g < RtsFlags.GcFlags.generations; g++) { bdescr *bd; StgPtr p; for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) { for (p = bd->start; p < bd->free; p++) { thread((StgClosure **)p); } } } // the static objects thread_static(scavenged_static_objects); // the stable pointer table threadStablePtrTable((evac_fn)thread); // the CAF list (used by GHCi) markCAFs((evac_fn)thread); // 2. update forward ptrs for (g = 0; g < RtsFlags.GcFlags.generations; g++) { for (s = 0; s < generations[g].n_steps; s++) { if (g==0 && s ==0) continue; stp = &generations[g].steps[s]; debugTrace(DEBUG_gc, "update_fwd: %d.%d", stp->gen->no, stp->no); update_fwd(stp->blocks); update_fwd_large(stp->scavenged_large_objects); if (g == RtsFlags.GcFlags.generations-1 && stp->old_blocks != NULL) { debugTrace(DEBUG_gc, "update_fwd: %d.%d (compact)", stp->gen->no, stp->no); update_fwd_compact(stp->old_blocks); } } } // 3. update backward ptrs stp = &oldest_gen->steps[0]; if (stp->old_blocks != NULL) { blocks = update_bkwd_compact(stp); debugTrace(DEBUG_gc, "update_bkwd: %d.%d (compact, old: %d blocks, now %d blocks)", stp->gen->no, stp->no, stp->n_old_blocks, blocks); stp->n_old_blocks = blocks; } }