std::vector<differential_result> * rk4(double (*function_pointer)(const double t, const double val, const std::vector<void *> & argv), const double initial_value, const double a, const double b, const unsigned int steps, const bool flag_collect, const std::vector<void *> & argv) { const double step = (b - a) / (double) steps; differential_result current_result; std::vector<differential_result> * result = NULL; if (flag_collect) { result = new std::vector<differential_result>(steps, current_result); } else { result = new std::vector<differential_result>(1, current_result); } current_result.time = a; current_result.value = initial_value; for (unsigned int i = 0; i < steps; i++) { double k1 = step * function_pointer(current_result.time, current_result.value, argv); double k2 = step * function_pointer(current_result.time + step / 2.0, current_result.value + k1 / 2.0, argv); double k3 = step * function_pointer(current_result.time + step / 2.0, current_result.value + k2 / 2.0, argv); double k4 = step * function_pointer(current_result.time + step, current_result.value + k3, argv); current_result.value += (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0; current_result.time += step; if (flag_collect) { (*result)[i] = current_result; } else { (*result)[0] = current_result; } } return result; }
void LLVMCompiler::show_assembly(STATE) { if(function_) { llvm::outs() << *function_ << "\n"; llvm::outs() << "\n== x86 assembly ==\n"; // Force it to be compiled function_pointer(state); assembler_x86::AssemblerX86::show_buffer(mci_->address(), mci_->size(), false, NULL); } else { llvm::outs() << "NULL function!\n"; } }
int main(int argc, char **argv) { // declare a function pointer variable void (*function_pointer)(); // point to our function (thats why its called function pointer, right?) function_pointer = greet; // method 1 function_pointer(); // method 2 (*function_pointer)(); return 0; }
std::vector<differential_result> * rkf45(double (*function_pointer)(const double t, const double val, const std::vector<void *> & argv), const double initial_value, const double a, const double b, const double tolerance, const bool flag_collect, const std::vector<void *> & argv) { differential_result current_result; current_result.time = a; current_result.value = initial_value; std::vector<differential_result> * result = new std::vector<differential_result>(); result->push_back(current_result); double h = (b - a) / 10.0; const double hmin = h / 100.0; const double hmax = 100.0 * h; const double br = b - 0.00001 * (double) std::abs(b); const unsigned int iteration_limit = 250; unsigned int iteration_counter = 0; while (current_result.time < b) { const double current_time = current_result.time; const double current_value = current_result.value; if ( (current_time + h) > br ) { h = b - current_result.time; } double k1 = h * function_pointer(current_time, current_value, argv); double y2 = current_value + butcher_table::b2 * k1; double k2 = h * function_pointer(current_time + butcher_table::a2 * h, y2, argv); double y3 = current_value + butcher_table::b3 * k1 + butcher_table::c3 * k2; double k3 = h * function_pointer(current_time + butcher_table::a3 * h, y3, argv); double y4 = current_value + butcher_table::b4 * k1 + butcher_table::c4 * k2 + butcher_table::d4 * k3; double k4 = h * function_pointer(current_time + butcher_table::a4 * h, y4, argv); double y5 = current_value + butcher_table::b5 * k1 + butcher_table::c5 * k2 + butcher_table::d5 * k3 + butcher_table::e5 * k4; double k5 = h * function_pointer(current_time + butcher_table::a5 * h, y5, argv); double y6 = current_value + butcher_table::b6 * k1 + butcher_table::c6 * k2 + butcher_table::d6 * k3 + butcher_table::e6 * k4 + butcher_table::f6 * k5; double k6 = h * function_pointer(current_time + butcher_table::a6 * h, y6, argv); /* Calculate error (difference between Runge-Kutta 4 and Runge-Kutta 5) and new value. */ double err = std::abs(butcher_table::r1 * k1 + butcher_table::r3 * k3 + butcher_table::r4 * k4 + butcher_table::r5 * k5 + butcher_table::r6 * k6); /* Calculate new value. */ double ynew = current_value + butcher_table::n1 * k1 + butcher_table::n3 * k3 + butcher_table::n4 * k4 + butcher_table::n5 * k5; if ( (err < tolerance) || (h < 2.0 * hmin) ) { current_result.value = ynew; if (current_time + h > br) { current_result.time = b; } else { current_result.time = current_time + h; } if (flag_collect) { result->push_back(current_result); } else { (*result)[0] = current_result; } iteration_counter++; } double s = 0.0; if (err != 0.0) { s = 0.84 * std::pow( (tolerance * h / err), 0.25 ); } if ( (s < 0.75) && (h > 2.0 * hmin) ) { h = h / 2.0; } if ( (s > 1.5) && (h * 2.0 < hmax) ) { h = 2.0 * h; } if (iteration_limit >= iteration_counter) { break; } } return result; }
void process(int factor, char* filter, char* ims_name, char* imd_name){ /* Selection du filtre */ float (*function_pointer) (float); if(strcmp(filter,"box") == 0){ printf("box filter\n"); function_pointer = box; } else if(strcmp(filter,"tent") == 0){ printf("tent filter\n"); function_pointer = tent; } else if(strcmp(filter,"gauss") == 0){ printf("gaussian filter\n"); function_pointer = gaussian; } else if(strcmp(filter,"bell") == 0){ printf("bell filter\n"); function_pointer = bell; } else if(strcmp(filter,"mitch") == 0){ printf("Mitchell-Netravali filter\n"); function_pointer = mitchell_netravali; } else { printf("unknown filter,\n filters avalaible: box, tent, gauss, bell, mitch"); assert(false); } pnm ims = pnm_load(ims_name); int height = pnm_get_height(ims); int width = pnm_get_width(ims); int height2 = height * factor; int width2 = width * factor; float wf = factor / 2; pnm imd = pnm_new(width2, height2, PnmRawPpm); /* image contiendra les valeur des pixels de l'image source, image2 contiendra l'image en cours de modification et image3 contiendra les images pivotées dont l'image finale*/ unsigned short * image = (unsigned short *) malloc(width * height * sizeof(unsigned short)); unsigned short * image2 = (unsigned short *) malloc(width2 * height2 * sizeof(unsigned short)); unsigned short * image3 = (unsigned short *) malloc(width2 * height2 * sizeof(unsigned short)); image = pnm_get_channel(ims, image, PnmRed); float left, right, s, l; int i_origin; /* On applique le filtre sur les lignes */ for(int i=0; i < height2; i++){ i_origin = i / factor; for(int l2=0; l2 < width2; l2++){ l = l2 / factor; left = (l - wf) >= 0 ? (l - wf) : 0; right = (l + wf) <= width ? (l + wf) : width; s = 0; for(int k=left; k <= right; k++) s += image[k + i_origin * width] * function_pointer(k-l); if(s > 255) s = 255; else if(s < 0) s = 0; image2[i_origin * width2 + l2] = s; } } /* retourne l'image pour que l'on puisse travailler sur les colonnes */ flip(width2, height2, image3, image2); /* On applique le filtre sur les colonnes */ for(int i=0; i < height2; i++){ for(int l2=0; l2 < width2; l2++){ l = l2 / factor; left = (l - wf) >= 0 ? (l - wf) : 0; right = (l + wf) <= width ? (l + wf) : width; s = 0; for(int k=left; k <= right; k++) s += image3[k + i * width2] * function_pointer(k-l); if(s > 255) s = 255; else if(s < 0) s = 0; image2[i * width2 + l2] = s; } } flip(width2, height2, image3, image2); pnm_set_channel(imd, image3, PnmRed); pnm_set_channel(imd, image3, PnmGreen); pnm_set_channel(imd, image3, PnmBlue); pnm_save(imd, PnmRawPpm, imd_name); free(image); free(image2); free(image3); pnm_free(imd); pnm_free(ims); }