void polygon_point(struct _seg *s) { double l, r; if (debug) printf("loop %f %f\n", s->slope, s->offset); l = find_shift(- s->slope, s->offset); r = find_shift( s->slope, - s->offset); if (l < df.l_min) df.l_min = l; if (r < df.r_min) df.r_min = r; if (debug) printf("constraint left: %f %f \n", l, df.l_min); if (debug) printf("constraint right: %f %f \n", r, df.r_min); }
int countNodes(TreeNode* root) { if (!root){return 0;} int max_depth = calcMaxDepth(root, 1); int shift = 0; std::function<bool(TreeNode* , int)> find_shift= [max_depth, &shift, &find_shift](TreeNode* node, int depth)->bool { if (!node) { shift++; return false; } if (depth == max_depth) { return true; } if (find_shift(node->right, depth + 1)) { return true; } else { return find_shift(node->left, depth + 1); } }; find_shift(root, 1); int count = 0; int current_layer_count = 1; for (int i = 0; i < max_depth;i++) { count += current_layer_count; current_layer_count *= 2; } count -= shift; return count; }
int main(void) { FILE * crypt, *decrypted; int shift; //otwieramy dwa pliki if((crypt = fopen("szyfr_v1.txt", "r")) == NULL) { perror("Blad otwarcia pliku"); exit(1); } decrypted = fopen("deszyfr.txt", "w"); shift = find_shift(crypt); //ustalamy przesuniecie rewind(crypt); //wracamy na poczatek pliku decrypt_file(crypt, ahift, decrypted); //odszyfrowujemy printf("Udanie odszyfrowano i zapisano do pliku!"); fclose(crypt); fclose(decrypted); }
static void line_correct_match(GwyContainer *data, GwyRunType run) { GwyDataField *dfield; GwyDataLine *shifts; gint xres, yres, i; gdouble *d, *s; GQuark dquark; g_return_if_fail(run & LINECORR_RUN_MODES); gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield, GWY_APP_DATA_FIELD_KEY, &dquark, 0); g_return_if_fail(dfield && dquark); gwy_app_undo_qcheckpointv(data, 1, &dquark); yres = gwy_data_field_get_yres(dfield); xres = gwy_data_field_get_xres(dfield); d = gwy_data_field_get_data(dfield); shifts = gwy_data_line_new(yres, 1.0, TRUE); s = gwy_data_line_get_data(shifts); s[0] = 0.0; for (i = 1; i < yres; i++) { s[i] = find_shift(xres, d + i*xres, d + (i - 1)*xres); g_printerr("%d %g\n", i, s[i]); } gwy_data_line_cumulate(shifts); for (i = 1; i < yres; i++) gwy_data_field_area_add(dfield, 0, i, xres, 1, s[i]); gwy_data_field_add(dfield, -s[yres-1]/(xres*yres)); g_object_unref(shifts); gwy_data_field_data_changed(dfield); }
/** * @brief Main function of the demonstration program * * Call as ./demo [nvar n ncomp nobs noise] * * @param[in] argc The Number of input arguments * @param[in] argv The array of string argument(s) * @return 0 upon success and a number different than 0 otherwise */ int main(const int argc, const char **argv) { // Get program input parameter const unsigned long nvar = (argc > 1) ? strtoul(argv[1],NULL,10) : DEFAULT_NVAR; const unsigned long n = (argc > 2) ? strtoul(argv[2],NULL,10) : DEFAULT_N; const unsigned long ncomp = (argc > 3) ? strtoul(argv[3],NULL,10) : DEFAULT_NCOMP; const unsigned long nobs = (argc > 4) ? strtoul(argv[4],NULL,10) : DEFAULT_NOBS; const double noise = (argc > 5) ? strtod(argv[5],NULL) : DEFAULT_NOISE; double *T = NULL; // Pointer to templates double *s = NULL; // Pointer to the current observation double *w = NULL; // Pointer to the weights double *corr = NULL; // Pointer to the weighted cross correlation function double *L = NULL; // Pointer to the lookup table clock_t t0, t1; // Clock times double tqr,tneq; // Execution times respectively for the QRL algorithm and for the normal equation unsigned long shift, shiftqr, shiftneq; // Real shift and shift found by the two algorithms unsigned long i; // Allocate arrays T = (double *) calloc(nvar*ncomp,sizeof(double)); s = (double *) calloc(n,sizeof(double)); w = (double *) calloc(n,sizeof(double)); corr = (double *) calloc(nvar,sizeof(double)); L = wcorr_lookup_alloc(nvar, ncomp); // Check that all arrays were correctly allocated if(T == NULL || s == NULL || w == NULL || corr == NULL || L == NULL) { free(T);free(s);free(w);free(corr);free(L); fprintf(stderr, "An error occurs during array allocation\n"); return -1; } // Init the random number generator srand(1); // Build mock templates build_mock_templates(T, nvar, ncomp); // Build the initial lookup table associated with T wcorr_lookup(L, T, nvar, ncomp); // Print the usage we made fprintf(stderr, "Usage: %s nvar=%lu n=%lu ncomp=%lu nobs=%lu noise=%g\n", argv[0], nvar, n, ncomp, nobs, noise); // Check if we will run in zero-optimized mode? if(nvar - n < ncomp) fprintf(stderr, "Running demonstration in classical mode\n"); else fprintf(stderr, "Running demonstration in zero-optimized mode\n"); // Print header line fprintf(stdout, "Observation | Real shift | QRL CPU time | QRL shift | Neq. CPU time | Neq. shift \n"); // Compute weighted phase correlation for each observation for(i = 0; i < nobs; i++) { // Select a random shift shift = rand() % nvar; // Build a mock observation build_mock_observation(s,w,n,T,nvar,ncomp,shift,noise); // Compute the weighted phase correlation function through factorized QR algorithm with lookup tables t0 = clock(); wcorr(corr, L, T, nvar, ncomp, w, s, n, 1, 0.); t1 = clock(); // Get the found shift shiftqr = find_shift(corr, nvar, max); // Get the CPU execution time tqr = clock_diff(t0, t1); // Compute the weighted phase correlation function through normal equations t0 = clock(); wchi2_normeq(corr, T, nvar, ncomp, w, s, n, 1); t1 = clock(); // Get the found shift shiftneq = find_shift(corr, nvar, min); // Get the CPU execution time tneq = clock_diff(t0, t1); // Print result fprintf(stdout, "%11lu | %10lu | %10.5fs | %8lu | %10.5fs | %10lu \n", i, shift, tqr, shiftqr, tneq, shiftneq); // fflush(stdout); } // Free arrays free(T); free(s); free(w); free(corr); wcorr_lookup_free(L); return 0; }