tags:

views:

1030

answers:

4

How can I do some more advanced function. I see that I can do function with public double myFunction(double myParameter) but what if I do want to derive that function?

+1  A: 

Are you thinking of Lambda Expressions?

Basically you can pass a function into a function.

So think of a Sort on an object. Depending on the nature of the object would help determine how the objects are sorted.

But you can still create a generic sort function then pass in how to compare objects.

Chris Brandsma
A: 

If you have written the function, it's already been derived.

And given that it's an int function, I'll assume you don't mean the calculus definition of "derive".

recursive
The verbal form of the derivative operation in calculus is 'differentiate', not 'derive'.
ntownsend
Ah. Duly noted. Learning. I like it.
recursive
+6  A: 

If you're thinking of symbolic manipulation of formulae then you're better off doing your derivations in languages languages like Maple or Mathematica. They're designed for symbolic computation.

EDIT: If Maple and Mathematica are too expensive for you then there are other options. Wikipedia has a fairly complete listing of computer algebra packages. http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems

ntownsend
Yes this is what I would like. Create a function and manipulate it. I need a third party?
Rogez Sanchez
+6  A: 

You can't calculate the exact derivative of a function using a computer program (unless you're doing symbolic math... but that's another, way more complicated, topic).

There are several approaches to computing a numerical derivative of a function. The simplest is the centered three-point method:

  • Take a small number h
  • Evaluate [f(x+h) - f(x-h)] / 2h
  • Voilà, an approximation of f'(x), with only two function evaluations

Another approach is the centered five-point method:

  • Take a small number h
  • Evaluate [f(x-2h) - 8f(x+h) + 8f(x-h) - f(x+2h)] / 12h
  • Voilà, a better approximation of f'(x), but it requires more function evaluations


Another topic is how to implement this using C#. First, you need a delegate that represents a function that maps a subset of the real numbers onto a another subset of the real numbers:

delegate double RealFunction(double arg);

Then, you need a routing that evaluates the derivative:

public double h = 10e-6; // I'm not sure if this is valid C#, I'm used to C++

static double Derivative(RealFunction f, double arg)
{
    double h2 = h*2;
    return (f(x-h2) - 8*f(x-h) + 8*f(x+h) - f(x+h2)) / (h2*6);
}

If you want an object-oriented implementation, you should create the following classes:

interface IFunction
{
    // Since operator () can't be overloaded, we'll use this trick.
    double this[double arg] { get; }
}

class Function : IFunction
{
    RealFunction func;

    public Function(RealFunction func)
    { this.func = func; }

    public double this[double arg]
    { get { return func(arg); } }
}

class Derivative : IFunction
{
    IFunction func;
    public static double h = 10e-6;

    public Derivative(IFunction func)
    { this.func = func; }

    public double this[double arg]
    {
        get
        {
            double h2 = h*2;
            return (
                func[arg - h2] - func[arg + h2] +
                ( func[arg + h]  - func[arg - h] ) * 8
                ) / (h2 * 6);
        }
    }
}
Eduardo León