forum.alglib.net
http://forum.alglib.net/

Passing parameters to autogkintegrate in C#
http://forum.alglib.net/viewtopic.php?f=2&t=637
Page 1 of 1

Author:  Doug Jenkins [ Thu Nov 01, 2012 10:01 am ]
Post subject:  Passing parameters to autogkintegrate in C#

I need to pass 3 additional parameters to the autogkintegrate function using C#

I have tried following the recommendations in this thread:
viewtopic.php?f=2&t=386&p=1179&hilit=autogkintegrate+example
but I can't see how to get the values out of the obj object. The code in the thread used "double a_1 = (double)a.GetValue(0);" but GetValue is not recognised as a valid method by my compiler (VS 2010).

My code as it stands is:

Code:
       public static void int_function_1_func(double x, double xminusa, double bminusx, ref double res, object   obj)
        {
           // double r = obj(0);
           // double dna = obj(1);
           
            res = 40 * (1 - Math.Pow((1 - x / dna), 2)) * 2 * Math.Sqrt(r * r - Math.Pow(r - dna + x, 2));
        }
        public double  integrate(double[] parama)
        {
            object obj = parama ;

            double dna = parama[1];
            double a = 0;
            alglib.autogkstate s;
            double v;
            alglib.autogkreport rep;

            alglib.autogksmooth(a, dna, out s);
            alglib.autogkintegrate(s, int_function_1_func, obj );
            alglib.autogkresults(s, out v, out rep);

            return v ;
        }


The code is intended to link to Excel VBA, which it does if the parameters r and dna are replaced with values, so I just need to know how to pass the required values to the autogkintegrate function.

Author:  Doug Jenkins [ Fri Nov 02, 2012 10:21 pm ]
Post subject:  Re: Passing parameters to autogkintegrate in C#

Solved it! (or at least found a method that works).

Ref: http://msdn.microsoft.com/en-us/library/yz2be5wk(VS.80).aspx (boxing and unboxing):

Code:
int i = 123;
object o = (object)i;  // boxing


Code:
o = 123;
i = (int)o;  // unboxing


So my code is now:
Code:
     public static void int_function_1_func(double x, double xminusa, double bminusx, ref double res, object   obj)
        {
           double[] sectdims = (double[])obj;
           double r = sectdims[0];
           double dna = sectdims[1];
           double fcu = sectdims[2];
           
            res = fcu * (1 - Math.Pow((1 - x / dna), 2)) * 2 * Math.Sqrt(r * r - Math.Pow(r - dna + x, 2));
        }
        public double  integrate(double[] parama)
        {
            double dna = parama[1];
            Object obj = (object)parama ;

            double a = 0;
            alglib.autogkstate s;
            double v;
            alglib.autogkreport rep;

            alglib.autogksmooth(a, dna, out s);
            alglib.autogkintegrate(s, int_function_1_func, obj);
            alglib.autogkresults(s, out v, out rep);

            return v ;
        }


Which works!

My question now is, is this the best way, bearing in mind this comment from MS:
"In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, an entirely new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally. For more information, see Performance."?

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/