sequence_t wait_until_published( sequence_t sequence, sequence_t lastKnownPublished, const std::chrono::time_point<Clock, Duration>& timeoutTime) const { assert(difference(sequence, lastKnownPublished) > 0); for (sequence_t seq = lastKnownPublished + 1; difference(seq, sequence) <= 0; ++seq) { if (!is_published(seq)) { const std::atomic<sequence_t>* const sequences[1] = { &m_published[seq & m_indexMask] }; sequence_t result = m_waitStrategy.wait_until_published(seq, 1, sequences); if (difference(result, seq) < 0) { // Timeout. seq is the first non-published sequence return seq - 1; } } } return last_published_after(sequence); }
void PolyaevaEV::lab7() { double norm, eps = 0.0001; double* AP = new double[N]; double* DIS = new double[N]; zeroing(x); zeroing(AP); do { double *TEMP = multiplication_matrix_on_vector(A, x); difference(DIS, TEMP, b); double tau = multiplication_of_vectors(DIS, DIS); double tempTau = multiplication_of_vectors(multiplication_matrix_on_vector(A, DIS), DIS); tau = tau/tempTau; if (tau != tau) tau = eps; for (int i = 0; i < N; i++) TEMP[i] = DIS[i]*tau; difference(AP, AP, TEMP); norm = fabs(x[0] - AP[0]); for (int i = 0; i < N; i++) { if (fabs(x[i] - AP[i]) > norm) norm = fabs(x[i] - AP[i]); x[i] = AP[i]; } delete[] TEMP; } while (norm > eps); delete[] AP; delete[] DIS; }
double HistogramBead::calculateWithCutoff( double x, double& df ) const { plumed_dbg_assert(init && periodicity!=unset ); double lowB, upperB, f; lowB = difference( x, lowb ) / width ; upperB = difference( x, highb ) / width; if( upperB<=-cutoff || lowB>=cutoff ) { df=0; return 0; } if( type==gaussian ) { lowB /= sqrt(2.0); upperB /= sqrt(2.0); df = ( exp( -lowB*lowB ) - exp( -upperB*upperB ) ) / ( sqrt(2*pi)*width ); f = 0.5*( erf( upperB ) - erf( lowB ) ); } else if( type==triangular ) { df=0; if( fabs(lowB)<1. ) df = (1 - fabs(lowB)) / width; if( fabs(upperB)<1. ) df -= (1 - fabs(upperB)) / width; if (upperB<=-1. || lowB >=1.) { f=0.; } else { double ia, ib; if( lowB>-1.0 ) { ia=lowB; } else { ia=-1.0; } if( upperB<1.0 ) { ib=upperB; } else { ib=1.0; } f = (ib*(2.-fabs(ib))-ia*(2.-fabs(ia)))*0.5; } } else { plumed_merror("function type does not exist"); } return f; }
void PrivacyManager::setPrivacy( bool defaultIsDeny, const QStringList & allowList, const QStringList & denyList ) { if ( defaultIsDeny != m_defaultDeny ) setDefaultDeny( defaultIsDeny ); // find the DNs no longer in the allow list QStringList allowsToRemove = difference( m_allowList, allowList ); // find the DNs no longer in the deny list QStringList denysToRemove = difference( m_denyList, denyList ); // find the DNs new in the allow list QStringList allowsToAdd = difference( allowList, m_allowList ); // find the DNs new in the deny list QStringList denysToAdd = difference( denyList, m_denyList ); QStringList::ConstIterator end = allowsToRemove.end(); for ( QStringList::ConstIterator it = allowsToRemove.begin(); it != end; ++it ) removeAllow( *it ); end = denysToRemove.end(); for ( QStringList::ConstIterator it = denysToRemove.begin(); it != end; ++it ) removeDeny( *it ); end = allowsToAdd.end(); for ( QStringList::ConstIterator it = allowsToAdd.begin(); it != end; ++it ) addAllow( *it ); end = denysToAdd.end(); for ( QStringList::ConstIterator it = denysToAdd.begin(); it != end; ++it ) addDeny( *it ); }
void compute_angular_forces() { int i, j, k; struct vertex *u, *v, *w; struct point dvu, dvw, normal; double fact, s; s = 0.5 * sin((M_PI - bestangle) / 2.0); for (i = 1; i <= nvertices; i++) { u = &(vertices[i]); for (j = 0; j + 1 < u->valency; j++) { v = &(vertices[u->adj[j]]); for (k = j + 1; k < u->valency; k++) { w = &(vertices[u->adj[k]]); dvu = difference(u->pos, v->pos); dvw = difference(w->pos, v->pos); normal = crossproduct(dvw, dvu); normal = crossproduct(normal, dvw); fact = s * norm(dvw) / norm(normal); u->disp.x += 0.05 * (0.5 * dvw.x + fact * normal.x - dvu.x); u->disp.y += 0.05 * (0.5 * dvw.y + fact * normal.y - dvu.y); u->disp.z += 0.05 * (0.5 * dvw.z + fact * normal.z - dvu.z); } } } }
bool isEar(int u, int v, int w, const std::vector<uint32_t>& vertices) { PointXY p_u = toPointXY(points_.points[vertices[u]]); PointXY p_v = toPointXY(points_.points[vertices[v]]); PointXY p_w = toPointXY(points_.points[vertices[w]]); // Avoid flat triangles. // FIXME: what happens if all the triangles are flat in the X-Y axis? const float eps = 1e-15; PointXY p_uv = difference(p_v, p_u); PointXY p_uw = difference(p_w, p_u); if (crossProduct(p_uv, p_uw) < eps) { ntk_dbg(1) << cv::format("FLAT: (%d, %d, %d)", u, v, w); return false; } // Check if any other vertex is inside the triangle. for (int k = 0; k < vertices.size(); k++) { if ((k == u) || (k == v) || (k == w)) continue; PointXY p = toPointXY(points_.points[vertices[k]]); if (isInsideTriangle(p_u, p_v, p_w, p)) return false; } return true; }
// Check which of keys a and b is closer to the target key. If they are equally // close, the next key from the target to the incrementing direction is // considered closer. All arrays must be of length SHA1_DIGEST_LENGTH. // Return: 1 if a is closer, 0 if b is closer, -1 if a and b are equal. int find_closer_key(unsigned char *target, unsigned char *a, unsigned char *b) { // Calculate differences unsigned char dif_a[SHA1_DIGEST_LENGTH]; int a_bigger_t = difference(dif_a, a, target); unsigned char dif_b[SHA1_DIGEST_LENGTH]; int b_bigger_t = difference(dif_b, b, target); int i; // Check if one is closer for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { if (dif_b[i] > dif_a[i]) return 1; if (dif_b[i] < dif_a[i]) return 0; } // Distances are equal. Checking which one is in incremental direction. // N.B. in this point a and b cant be equal to the target. if (a_bigger_t && !b_bigger_t) return 1; if (!a_bigger_t && b_bigger_t) return 0; // Check if a or b is bigger for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { if (b[i] > a[i]) { return 1; } else if (b[i] < a[i]) return 0; } // a and b are equal return -1; }
TEST(bind_with_result, return_from_function_pointer1) { { int diff = weos::bind<int>(&difference, 1, 2) (); ASSERT_EQ(difference(1, 2), diff); } { int diff = weos::bind<int>(&difference, weos::placeholders::_2, weos::placeholders::_1) (1, 2); ASSERT_EQ(difference(2, 1), diff); } for (int counter = 0; counter < 100; ++counter) { int diff = weos::bind<int>(&difference, weos::placeholders::_1, 1) ( counter); ASSERT_EQ(counter - 1, diff); } for (int counter = 0; counter < 100; ++counter) { int diff = weos::bind<int>(&difference, 0, weos::placeholders::_4) ( 0, 1, 2, counter); ASSERT_EQ(-counter, diff); } }
int main(){ string buffer("buffer.txt"); string solution("solution.txt"); //for (unsigned int test_number = 0; test_number <= 100000000; test_number) { #ifdef READ T w, h; ifstream in_stream(buffer); in_stream >> w >> h; matrix<T> source(w, h); for (auto& i : source) { in_stream >> i; } in_stream.close(); #else const T w = 1000, h = 1000; matrix<T> source = gen_sourse_data(w, h, 5); #endif #ifdef WRITE ofstream out_stream(buffer); out_stream << w << ' ' << h; T j = 0; for (const auto& i : source) { if ((j++ % w) == 0) out_stream << '\n'; out_stream << i << ' '; } out_stream.close(); #endif #ifdef SOLVE cout << "w = " << w << "; h = " << h << ";" << endl; auto start_time = clock(); matrix<T> source_copy(source);//эта матрица будет преобразована в такую, что в каждом квадрате 3х3 будет не больше 3 текстур matrix< array<pair<T, bool>, 3> > res = get_textures_arrangement(source_copy); double time = (clock() - start_time) / double(1000); auto source_from_res = res_to_source(res); auto diff1 = difference(source, source_from_res); auto diff2 = difference(source, source_copy); cout << "time is " << time << " second" << endl; cout << "full difference is " << diff1 << " = " << (diff1 / (float)(w * h)) * 100 << "%" << endl; cout << "difference between source and 3x3 condition source is " << diff2 << " = " << (diff2 / (float)(w * h)) * 100 << "%" << endl << endl; #ifdef PRINT_SOLUTION ofstream solution_stream(solution); cout.rdbuf(solution_stream.rdbuf()); cout << "w = " << w << "; h = " << h << ";" << endl; cout << "time is " << time << " second" << endl; cout << "full difference is " << diff1 << " = " << (diff1 / (float)(w * h)) * 100 << "%" << endl; cout << "difference between source and 3x3 condition source is " << diff2 << " = " << (diff2 / (float)(w * h)) * 100 << "%" << endl << endl; print(source, res); solution_stream.close(); #endif #endif //} system("pause"); }
/******************************************************************* * * NAME : optimal_step_length() * * DESCRIPTION : Calulates the optimal step length using * Newtons method. */ double MC_Brute_Force::optimal_step_length() { double min, max, tolerance; min = 0.01; max = 3; tolerance = 0.1; while ((max - min) > tolerance) { if (difference(min) * difference((min + max) / 2) < 0) max = (min + max) / 2; else min = (min + max) / 2; } return (min + max) / 2; }
int manhattan(Node n1, Node n2){ int diff=0; for(int i=1;i<9;i++){ diff += difference(n1,n2,i); } return diff; }
void compute_local_repulsive_forces() { int i, j, k; struct vertex *u, *v, *w; struct point delta; double fact; for (i = 1; i <= nvertices; i++) { w = &(vertices[i]); for (j = 0; j + 1 < w->valency; j++) { v = &(vertices[w->adj[j]]); for (k = j + 1; k < w->valency; k++) { u = &(vertices[w->adj[k]]); delta = difference(v->pos, u->pos); if (delta.x == 0.0) delta.x = 0.1 * (drand48() - 0.5); if (delta.y == 0.0) delta.y = 0.1 * (drand48() - 0.5); if (delta.z == 0.0 && !flat) delta.z = 0.1 * (drand48() - 0.5); fact = - 1.0 / squarednorm(delta); v->disp.x -= delta.x * fact; v->disp.y -= delta.y * fact; v->disp.z -= delta.z * fact; u->disp.x += delta.x * fact; u->disp.y += delta.y * fact; u->disp.z += delta.z * fact; } } } }
int main(int argc, char* argv[]){ Bmp *bmpA = loadbmp("img/hello.bmp"); // Save a copy bmpA->saveCopy("img/hellocopy"); // Invert filter invert(bmpA); bmpA->saveCopy("img/helloinvert"); Bmp *bmpB = loadbmp("img/rainbow.bmp"); Bmp *bmpC = loadbmp("img/rainbow2.bmp"); // Difference filter difference(bmpB, bmpC); bmpB->saveCopy("img/rainbowdifference.bmp"); //medianFilter(bmpB, 3); //bmpB->saveCopy("img/median3x3"); //medianFilter(bmpC, 7); //bmpC->saveCopy("img/median7x7"); return 0; }
ForwardIterator max_adjacent_difference(ForwardIterator first, ForwardIterator last) { typedef typename std::iterator_traits<ForwardIterator>::value_type value_type; ForwardIterator result(first); if (first == last) return result; ForwardIterator previous(first); ++first; if (first == last) return result; value_type result_difference(*first - *previous); previous = first; ++first; while (first != last) { value_type difference(*first - *previous); if (result_difference < difference) { result_difference = difference; result = previous; } previous = first; ++first; } return result; }
void balanceleft (avltree ** n, short adjust) { short dif; dif = difference ((*n)->left); if (dif == 0) { rotateright (n); /* both subtrees of left child of n have same height */ ((*n)->height) -= adjust; /* 'decrease' height of current node */ ((*n)->right->height) += adjust; /* 'increase' height of right subtree */ } else { if (dif > 0) { rotateright (n); /* left subtree of left child of n is higher */ (*n)->right->height -= 2; } else { /* right subtree of left child of n is higher */ rotateleft (&(*n)->left); /* pointer to n->left */ rotateright (n); ++((*n)->height); /* increase height of current node */ (*n)->right->height -= 2; --((*n)->left->height); /* decrease height of left subtree */ } } }
static void longdivide( MLDiv const &x, MLDiv const &y, MLDiv &q, MLDiv &r, UBIG n, UBIG m ) { MLDiv d; MLDiv dq; MLDiv tr; UBIG f; int k; UBIG qt; assert( 2 <= m && m <= n && n <= w ); f = b / ( y.d[m-1] + 1 ); product( tr, x, f ); product( d, y, f ); zero( q ); for( k = n-m; k >= 0; --k ) { assert( 2 <= m && m <= (k+m) && (k+m) <= n && n <= w ); qt = trial( tr, d, k, m ); product( dq, d, qt ); if( smaller( tr, dq, k, m ) ) { --qt; product( dq, d, qt ); } q.d[k] = qt; difference( tr, dq, k, m ); } quotient( r, tr, f ); }
std::pair<int, int> ValuesSimplex<T>::find_longest_edge() const { int i0 = 0; int i1 = 1; T max = Blas<T>::nrm2(value_dim, &matrix[0], 1); for (int i = 1; i < domain_dim; ++i) { const T new_max = Blas<T>::nrm2(value_dim, &matrix[i * value_dim], 1); if (new_max > max) { max = new_max; i1 = i + 1; } } std::vector<T> difference(value_dim); for (int i = 0; i < domain_dim; ++i) { for (int j = i + 1; j < domain_dim; ++j) { Blas<T>::copy(value_dim, &matrix[i * value_dim], 1, &difference[0], 1); Blas<T>::axpy(value_dim, -1, &matrix[j * value_dim], 1, &difference[0], 1); const T new_max = Blas<T>::nrm2(value_dim, &difference[0], 1); if (new_max > max) { max = new_max; i0 = i + 1; i1 = j + 1; } } } return std::make_pair(i0, i1); // fact: i0 < i1 }
static void compute_farthest (struct edge_list *edge_list, int n_exprs, sbitmap *st_avout, sbitmap *st_avin, sbitmap *st_antin, sbitmap *kill, sbitmap *farthest) { int x, num_edges; basic_block pred, succ; num_edges = NUM_EDGES (edge_list); auto_sbitmap difference (n_exprs), temp_bitmap (n_exprs); for (x = 0; x < num_edges; x++) { pred = INDEX_EDGE_PRED_BB (edge_list, x); succ = INDEX_EDGE_SUCC_BB (edge_list, x); if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun)) bitmap_copy (farthest[x], st_avout[pred->index]); else { if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun)) bitmap_clear (farthest[x]); else { bitmap_and_compl (difference, st_avout[pred->index], st_antin[succ->index]); bitmap_not (temp_bitmap, st_avin[succ->index]); bitmap_and_or (farthest[x], difference, kill[succ->index], temp_bitmap); } } } }
int main() { struct clinkedlist* last; last = NULL; int choice; int value; int p, s, l, d; while(1) { printf("Choices:\n"); printf("1. Insert at beginning\n"); printf("2. Print all the nodes\n"); printf("3. Print Smallest\n"); printf("4. Print largest\n"); printf("5. Print largest - smallest\n"); printf("6. Exit\n"); printf("Enter your choice:\n"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter value to be inserted:\n"); scanf("%d", &value); last = insert_beg(last,value); break ; case 2: print_all(last); break ; case 3: s = smallest(last); if (s != 0) { printf("%d\n", s); break; } printf("List is Empty!!\n"); break; case 4: l = largest(last); if (l != 0) { printf("%d\n", l); break; } printf("List is Empty!!\n"); break; case 5: d = difference(last); if (d != 0) { printf("%d\n", d); break; } printf("List is Empty!!\n"); break; case 6: exit(1); default: printf("Invalid Choice!\n"); } } return 0; }
UMatrix<T>& UMatrix<T> :: operator-(const UMatrix<T>& source) { // if(m_dimension != source.m_dimension) throw Exception(5); UMatrix<T> difference(m_dimension); for(int i=0; i<m_dimension; i++) { for(int j=0; j<m_dimension-i; j++) { difference(i,j) = m_ptr_to_data[i][j] - source(i,j); } } copy(difference); return *this; }
void deletemin (avltree ** n, void **dataptr) { avltree *temp; short dif; if ((*n)->left != NULL) /* keep going for leftmost node */ deletemin (&(*n)->left, dataptr); else { /* leftmost node found */ *dataptr = (*n)->data; /* get pointer to data */ temp = *n; *n = (*n)->right; /* return pointer to right subtree */ free (temp); /* of leftmost node */ } if (*n != NULL) { fixheight (*n); dif = difference (*n); if (dif > 1) /* deletion caused left subtree to be too high */ balanceleft (n, -1); else if (dif < -1) /* deletion caused right subtree to be too high */ balanceright (n, -1); } }
void HazePerfection::detectionPeak() { morphologicalErode(maskDataset); morphologicalReconstruction(erodeDataset, maskDataset); difference(maskDataset, erodeDataset); const char *pszFormat = "GTiff"; const char *binaryfile = m_binaryfilename.c_str(); GDALDriver *poDrive = (GDALDriver*)GDALGetDriverByName(pszFormat); binaryDataset = poDrive->Create(binaryfile, nXSize, nYSize, 1, GDT_Float32, NULL); binaryDataset->SetGeoTransform(sGeoTrans); binaryDataset->SetProjection(hotDataset->GetProjectionRef()); float *binaryPixelsData = new float[nXSize*nYSize]; float *tempPixelsData = new float[nXSize*nYSize]; totalChangeDataset->GetRasterBand(1)->RasterIO(GF_Read, 0, 0, nXSize, nYSize, binaryPixelsData, nXSize, nYSize, GDT_Float32, 0, 0); maxChangeDataset->GetRasterBand(1)->RasterIO(GF_Read, 0, 0, nXSize, nYSize, tempPixelsData, nXSize, nYSize, GDT_Float32, 0, 0); for (int i = 0; i < nYSize; i++) { for (int j = 0; j < nXSize; j++) { if (binaryPixelsData[i*nXSize + j]>=thresholdTC && tempPixelsData[i*nXSize + j]>=thresholdMC) binaryPixelsData[i*nXSize + j] = 1.0; else { binaryPixelsData[i*nXSize + j] = 0.0; } } } binaryDataset->GetRasterBand(1)->RasterIO(GF_Write, 0, 0, nXSize, nYSize, binaryPixelsData, nXSize, nYSize, GDT_Float32, 0, 0); delete[]binaryPixelsData; delete[]tempPixelsData; }
/*! * \brief Compares two images, and if they are different enough(1% of the * screen), returns true * \details Uses a loop through all the x pixels and the y pixels, and compares * them. The "X loop" is done in parallel using TBB. * \param oldImage the old image * \param newImage the new image, if this returns true, it will be saved. * \return True if the images differ. */ bool QSnapper::imagesDiffer(const QImage oldImage, const QImage newImage) { bool exceedsDiffenceLimit = false; if(oldImage.width() == newImage.width() && oldImage.height() == newImage.height()) { std::atomic_int difference(0); const int height = oldImage.height(); const int width = oldImage.width(); const int differenceLimit = (height * width) / 100; tbb::parallel_for(tbb::blocked_range<int>(0, width), [&](const tbb::blocked_range<int> &range) { for(int i = range.begin(); i != range.end(); ++i) { for(int j = 0; j < height; ++j) { if(exceedsDiffenceLimit) return; if(oldImage.pixel(i, j) != newImage.pixel(i, j)) ++difference; if(difference > differenceLimit) { exceedsDiffenceLimit = true; } } } }); } else { return true; } return exceedsDiffenceLimit; }
float difference(const Descriptor* desc1, const Descriptor* desc2, Descriptor& desc, float (*diff) (float, float) ) { // assuming desc is valid if( desc2 == 0){ desc.clear(); desc.m_na = desc1->size_a(); desc.m_nz = desc1->size_z(); desc.m_nr = desc1->size_r(); Matrixf *pm = new Matrixf( * desc1->getPage(0) ); pm->clone(); desc.m_vHistPtr.push_back(pm); return 0.0f; } else if( desc1 == desc2 ){ desc.clear(); desc.m_na = desc1->size_a(); desc.m_nz = desc1->size_z(); desc.m_nr = desc1->size_r(); Matrixf *pm = new Matrixf( desc.m_na * desc.m_nz, desc.m_nr ); pm->setAllZeros(); desc.m_vHistPtr.push_back(pm); return 0.0f; } else return difference(*desc1, *desc2, desc, diff); }
void main() { int k; SLinkList s; difference(s,k); ListTraverse(s,k,visit); }
double ConstraintDifference::grad(double *param) { double deriv=0.; if (param == param1()) deriv += -1; if (param == param2()) deriv += 1; if (param == difference()) deriv += -1; return scale * deriv; }
/** * The distance between the point of \c origin and the closest point to * the plane orthogonal to the axis of dimension \c dim and crossing \c * key. * * Given any 2 points 'origin' and 'key', the result of distance_to_plane * must always be less or equal to the result of distance_to_key. * * \return The resulting distance. */ distance_type distance_to_plane(dimension_type, dimension_type dim, const key_type& origin, const key_type& key) const { return math::euclid_distance_to_plane <key_type, difference_type, DistanceType>(dim, origin, key, difference()); }
/** * Compute the distance between the point of \c origin and the \c key. * \return The resulting distance. */ distance_type distance_to_key(dimension_type rank, const key_type& origin, const key_type& key) const { return math::euclid_distance_to_key <key_type, difference_type, DistanceType>(rank, origin, key, difference()); }
double UtcTime::difference( const UtcTime* other_time ) const { if( ! other_time ) { return 0.0; } return difference( *other_time ); }
bool MemoryLeakWarning::UsageIsNotBalanced() { TInt allocatedCells(User::CountAllocCells()); if(_impl->iExpectedLeaks != 0) { TInt difference(Abs(_impl->iInitialAllocCells - allocatedCells)); return difference != _impl->iExpectedLeaks; } return allocatedCells != _impl->iInitialAllocCells; }