using delegate

  • Thread starter ASP.NET explorer
  • Start date
A

ASP.NET explorer

The following piece of code (between comment /***************/ line )works
fine. You can test is in console/win form/web page. Just print strResult.

/**************************************************/
public delegate double DlgFn(double x);

public static double f1(double x) {
return Math.Exp(-(x * x)); //function to be integrated by Simpson's
rule
}
public class Integral {
public static double Integrate(DlgFn f, double a, double b, int n) {
double h = (b - a) / n;//step_size
double sum = 0;
sum += f(a) + f(b);
for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h);
for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h);
return Math.Round((h / 3) * sum, 5);
}
}
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1,
100).ToString();
/**************************************************/

However it does not enable me to extend my application further.

I want to replace the last line of code,
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString();
with the following lines:

double lLim= 0; double uLim= 1;
double n =100; // step no.
DlgFn myFn = new DlgFn(Integral.Integrate);
string strResult =myFn(f1, lLim, uLim, new).ToString();

The reason for doing so is because the variables, lLim, uLim and n will be
used in other methods in application code.

I eagerly look forward for any useful hints/help that could get me move
ahead.

Thanks,
 
D

DeveloperX

The following piece of code (between comment /***************/ line )works
fine. You can test is in console/win form/web page. Just print strResult.

/**************************************************/
public delegate double DlgFn(double x);

public static double f1(double x) {
return Math.Exp(-(x * x)); //function to be integrated by Simpson's
rule
}
public class Integral {
public static double Integrate(DlgFn f, double a, double b, int n) {
double h = (b - a) / n;//step_size
double sum = 0;
sum += f(a) + f(b);
for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h);
for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h);
return Math.Round((h / 3) * sum, 5);
}
}
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1,
100).ToString();
/**************************************************/

However it does not enable me to extend my application further.

I want to replace the last line of code,
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1, 100).ToString();
with the following lines:

double lLim= 0; double uLim= 1;
double n =100; // step no.
DlgFn myFn = new DlgFn(Integral.Integrate);
string strResult =myFn(f1, lLim, uLim, new).ToString();

The reason for doing so is because the variables, lLim, uLim and n will be
used in other methods in application code.

I eagerly look forward for any useful hints/help that could get me move
ahead.

Thanks,

I've defined a second delegate with a signature identical to your
Integrate method. Does that help? Test2 is the meat and veg.

using System;
namespace RunLotsOfThreads2
{
public class Integral
{
public delegate double DlgFn(double x);
public delegate double DlgFn2(DlgFn f, double a, double b, int n);

public static double f1(double x)
{
return Math.Exp(-(x * x)); //function to be integrated by Simpson's
rule
}


public static double Integrate(DlgFn f, double a, double b, int n)
{
double h = (b - a) / n;//step_size
double sum = 0;
sum += f(a) + f(b);
for (int i = 1; i <= (n - 1); i += 2) sum += 4 * f(a + i * h);
for (int i = 2; i <= (n - 1); i += 2) sum += 2 * f(a + i * h);
return Math.Round((h / 3) * sum, 5);
}
public static void test()
{
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1,
100).ToString();
Console.WriteLine(strResult);
}
public static void test2()
{
double lLim= 0; double uLim= 1;
int n =100; // step no.
DlgFn2 myFn = new DlgFn2(Integral.Integrate);
string strResult =myFn(new DlgFn(Integral.f1), lLim, uLim,
n).ToString();
Console.WriteLine(strResult);
}
}
}
 
A

ASP.NET explorer

Still can't grasp why two delegates? The code should somehow be more crisp.

As I am newbie to using delegates, I was having hard time. But your code did
solve my problem and so did help me move ahead for now.

Many thanks !
 
P

Peter Duniho

Still can't grasp why two delegates? The code should somehow be more
crisp.

I admit, I'm not really clear on what issue it is you're trying to solve.
But if the reply from DeveloperX solved it, perhaps it really was as
simple as having to declare a second delegate type.

If so, then the answer to "why two delegates" is that the delegate type
requires that the "signature" of the method used to create the new
delegate matches exactly with that declared in the delegate type. The
"signature" being things like the return value and the parameter list.

You can't use the first delegate type you declared with any method except
one that returns a double and takes exactly one double as a parameter. If
you want to wrap the Integrate() method in a delegate, you need a new
delegate type that matches *that* method's signature (returns a double,
takes as parameters one DlgFn delegate, two doubles, and an int).

If you still have questions, perhaps you could try to be a little more
clear about how it is you want the code to work and why it is the code
you've already got doesn't do what you want.

Pete
 
B

Bill Butler

I want to replace the last line of code,
string strResult = Integral.Integrate(new DlgFn(f1), 0, 1,
100).ToString();
with the following lines:

double lLim= 0; double uLim= 1;
double n =100; // step no.
DlgFn myFn = new DlgFn(Integral.Integrate);
string strResult =myFn(f1, lLim, uLim, new).ToString();

I think you got all dyslexic when you slapped this together

First you try to create a DlgFn out of Integral.Integrate which has the
wrong signature.
Then instead of passing myFn into Integrate you passed f1 to myFn
Then you passed 'new' instead of 'n' as your last param

Let me attempt to disentangle

I assume that you meant some thing like this
DlgFn myFn = new DlgFn(f1)
string strResult =Integral.Integrate(myFn , lLim, uLim,
n).ToString();

but that has a problem as well.
The last param to Integrate takes an int and you are passing a double.
So here we go again

DlgFn myFn = new DlgFn(f1)
string strResult =Integral.Integrate(myFn , lLim, uLim,
(int)n).ToString();

Is that what you were looking for?

Bill
 
A

ASP.NET explorer

Hi Bill,

A belated thanks for suggestions. All of you put me on right path and I am
moving ahead with some knowledge.

----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top