views:

184

answers:

6

Question pertains to series type math problems:

x^1+x^2+x^3+...+x^n

please note, this is just a simple example. each iteration of n could be more complex. i am not actually looking to calculate this equation.. just curious about what constructs are available..

i know i could write a simple loop or use recursion to accomplish this. But i remember reading about some construct in c# that will pre-compile such a statement for later execution. This was years ago, and it might have been using reflection.. can't recall.

are there any other interesting ways to accomplish this?

+7  A: 

To calculate x^n use Math.Pow:

Math.Pow(x, n)

If you want to calculate the sum you could use a loop or LINQ. I don't think there's anything wrong with a simple loop here:

double total = 0;
for (int i = 1; i <= n; ++i)
{
    total += Math.Pow(x, i);
}
Console.WriteLine(total);

You can write this in LINQ but I don't see any particularly strong reason to do so. Perhaps you could expand on what features you are looking for? Are you looking for better performance?

Since your question is tagged 'mathematical-optimization' you might also want to optimize it by finding a shortcut. In this specific case it is a geometric series so you can use the formula:

alt text

Or in C#:

static double geometricSeries(double a, double r, int n)
{
    return a * (1 - Math.Pow(r, n + 1)) / (1 - r);
}

In other more complex cases finding the formula might be more difficult.

Mark Byers
very interesting (about geometric series). how can that formula be plugged into c#?
Sonic Soul
@Sonic Soul: Updated. Note that this counts an extra term corresponding to (a*r^0) which you didn't include in your example so you will have to subtract that term if you want only from r^1 upwards. To solve the example you gave I *think* you want to call `geometricSeries(1, x, n) - 1` .
Mark Byers
A: 
int total = 0;
for(int i = 1; i <= n; i++)
    total += Math.Pow(x, i);
Bang Dao
+3  A: 

Well you might be talking about using a delegate for deferred execution. But in many cases it's the same as writing a method. For example, let's start with the "simple" way of doing it:

public static double SumExponents(double x, int n)
{
    double total = 0;
    for (int i = 1; i <= n; i++)
    {
         total += Math.Pow(x, i);
    }
    return total;
}

This can be written using LINQ as:

public static double SumExponents(double x, int n)
{
    return Enumerable.Range(1, n)
                     .Select(i => Math.Pow(x, i))
                     .Sum();
}

You could then write this as a single lambda expression:

Func<double, int, double> func = (x, n) => Enumerable.Range(1, n)
                                              .Select(i => Math.Pow(x, i))
                                              .Sum();

Is that the sort of thing you were thinking of? If not, please clarify your question. It's not really obvious what you're looking for.

Jon Skeet
thanks Jon. was just curious if there were other ways to accomplish this that i wasn't think of..
Sonic Soul
@Sonic: Was that the sort of suggestion you were thinking of then? Or are you still looking for more alternatives?
Jon Skeet
yep. still digesting the geometric series example, but this re-assures me that i'm not missing on some other neat ways. thanks!
Sonic Soul
+1  A: 

There's nothing specific to C# about geometric progression. You can compute this sum in O(1) time. (Assuming power operation takes constant time.)

In your case, the formula would be

x*(x^n - 1)/(x - 1)
Nikita Rybak
A: 

As well as select\sum, you can also use Aggregate for folding sequences.

int n;
double x;
double result = Enumerable.Range(1, n)
    .Aggregate(0.0, (acc, i) => acc + Math.Pow(x, i));
Lee
+5  A: 

I understand that your example is intentionally trivial. However, if what you're really trying to calculate is still a polynomial, then you should definitely use Horner scheme. Here's a C# implementation.

Bolo
+1 for Horner's scheme. I was going to answer this myself. Much more efficient and in general more accurate than calculating a sum of pow() invocations.
Peter G.
+1 for Horner's scheme. More efficient than sum of powers (need only n multiplications and n additions, no need for powers at all) and is more numerically stable than sum of powers and geometric progression (think what happens when r is very close to 1).
Krystian
very nice, thanks
Sonic Soul