forum.alglib.net http://forum.alglib.net/ |
|
Adaptive Quadrature: autogk_d1 example http://forum.alglib.net/viewtopic.php?f=2&t=1656 |
Page 1 of 1 |
Author: | TerraG [ Sat Apr 05, 2014 4:36 pm ] |
Post subject: | Adaptive Quadrature: autogk_d1 example |
When looking at the example: Code: #include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <math.h> #include "integration.h" using namespace alglib; void int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr) { // this callback calculates f(x)=exp(x) y = exp(x); } int main(int argc, char **argv) { // // This example demonstrates integration of f=exp(x) on [0,1]: // * first, autogkstate is initialized // * then we call integration function // * and finally we obtain results with autogkresults() call // double a = 0; double b = 1; autogkstate s; double v; autogkreport rep; autogksmooth(a, b, s); alglib::autogkintegrate(s, int_function_1_func); autogkresults(s, v, rep); printf("%.2f\n", double(v)); // EXPECTED: 1.7182 return 0; } I know I can look up the varibles and especially methods in the cpp file, but perhaps someone can briefly tell me what I need to know. May questions: 1. Code: int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr) Function "int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr)" should get 5 input parameters -> where are they passed? and what is "xminusa" and "bminusx" for? 2. Code: autogksmooth(a, b, s); What does this method do? Furthermore, what is autogkstate s for? 3. Code: alglib::autogkintegrate(s, int_function_1_func); I guess here the integration takes place. Again: what is autogkstate s for? 4. Code: autogkresults(s, v, rep); I guess here the results of the integration are returned, right? What is returned exactly? Is "v" the total result and is "rep" a total "report" about the integration? Generally I just want to integrate a function over known integration limits and get the total result, e.g. as a double. Furthermore I need to pass one parameter/variable into the function at runtime - lets imagine a function like Code: y= exp(x + passed) where passed is a variable/object that is passed at runtime.I hope someone enlightens me here a little. greetings TerraG |
Author: | Sergey.Bochkanov [ Sun Apr 06, 2014 8:00 am ] |
Post subject: | Re: Adaptive Quadrature: autogk_d1 example |
1. int_function_1_func is called in the internals of autogkintegrate (or, to be more precise, on of the functions called by autogkintegrate). As for parameters: a) x clearly is a current evaluation point b) xminusa = x - a, where a is left boundary of the interval. This quantity can be used if you integrate function with singularity at the left boundary - algorithm will place points too close to a, so x-a will be more precise than just x (remember that computer uses finite-precision arithmetics). c) bminusx = b - x, here b is right boundary d) y is a place to store function value e) ptr is additional parameter which can be passed to autogkintegrate, and which will be forwarded to int_function_1_func during each call. It can be used to pass some data to int_function_1_func, as you asked in (4). If you do not specify ptr during call of autogkintegrate, NULL value will be assigned by default. See declaration of autogkintegrate to find out how to specify ptr. 2, 3. autogksmooth prepares autogkstate - special structure which stores state of the ALGLIB integrator. Before you start integration, you should create integrator object, initialize it with integration interval, then call function which performs actual integration, and only then you will get integration result. There are strong architectural reasons to use such scheme instead of simpler one (just one integration function). Say, with such approach it is easy to add new features to integrator without breaking backward compatibility. 4.a. yes, you are right - v is integral value, and rep contains some information about integration process (see ALGLIB Reference Manual for description of its fields). 4.b. in my reply to (1) I already mentioned that you can pass pointer value to autogkintegrate, and this pointer will be forwarded to target function as ptr parameter. So you can create arbitrarily large data structure in memory, create pointer, convert it to void*, pass to autogkintegrate, which will pass it to your target function, which will perform backward conversion from void* to pointer to your structure. |
Author: | TerraG [ Sun Apr 06, 2014 9:24 am ] |
Post subject: | Re: Adaptive Quadrature: autogk_d1 example |
Thx for your reply. I still have one question: Lets say I have a function like void int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr) { // this callback calculates f(x)=exp(x) y = (exp(x+b))*c; } and b,c are input parameters that must be passed at runtime. How would I pass b and c by a pointer (last input parameter)? How would I define the pointer exactly for passing the runtime values? |
Author: | Sergey.Bochkanov [ Mon Apr 07, 2014 5:54 am ] |
Post subject: | Re: Adaptive Quadrature: autogk_d1 example |
You may write code like that: Code: MAIN FUNCTION:
double bc[2] = { b, c }; ... alglib::autogkintegrate(state, func, (void*)(&bc[0])); SYSTEM FUNCTION: double *p_bc = (double*)ptr; // p_bc[0] is b, p_bc[1] is c |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |