Math Question

Q

Quackker12

is there a function in ansi c that does this?

sigma(2^n/n!,0,infinity,n);

syntax
sigma(expression, start, end, variable);

may be expression could be a function....

I know you can do this with maple/mathlab, but i don't have it. I was
hoping there was a function or a library I could get.

TIA

(e-mail address removed)
 
D

Darrell Grainger

is there a function in ansi c that does this?

sigma(2^n/n!,0,infinity,n);

syntax
sigma(expression, start, end, variable);

may be expression could be a function....

I know you can do this with maple/mathlab, but i don't have it. I was
hoping there was a function or a library I could get.

There is nothing in the Standard C library but I would image there are
libraries available to do what you want. You might want to see if there
are any math newsgroups. I would guess they might be able to help you. I'm
assuming you don't want to buy a commercial library and are hoping for a
free solution.

If you are willing to buy something to solve the problem then maybe buying
Maple or MatLab would be your best bet. Most people I work with go for
MatLab and its C callable library.
 
J

James Hu

is there a function in ansi c that does this?

sigma(2^n/n!,0,infinity,n);

syntax
sigma(expression, start, end, variable);

may be expression could be a function....

You don't really describe the semantics, but...
I know you can do this with maple/mathlab, but i don't have it. I was
hoping there was a function or a library I could get.

Ok, but, YOMAD:

double
c_sigma(double (*sigma_expr)(int n), int start, int end)
{
double x = 0;
int n = start;

if (start > end) {
n = end;
end = start;
}

for (;;) {
x += sigma_expr(n);
if (n++ == end) break;
}
}

-- James
 
S

saro

Quackker12 said:
is there a function in ansi c that does this?

sigma(2^n/n!,0,infinity,n);

syntax
sigma(expression, start, end, variable);

may be expression could be a function....

I know you can do this with maple/mathlab, but i don't have it. I was
hoping there was a function or a library I could get.

TIA

(e-mail address removed)

There is no standard function as defined by standard C library to do
this. You got to implement yours ( or use google to get the relevant
library ;) ) .
 
R

Rob Thorpe

James Hu said:
You don't really describe the semantics, but...

Ok, but, YOMAD:

double
c_sigma(double (*sigma_expr)(int n), int start, int end)
{
double x = 0;
int n = start;

if (start > end) {
n = end;
end = start;
}

for (;;) {
x += sigma_expr(n);
if (n++ == end) break;
}
}

But if the poster really needs to do sums *to infinity* it is much
harder, and can only really be done by analysis of the function.
 
P

P.J. Plauger

is there a function in ansi c that does this?

sigma(2^n/n!,0,infinity,n);

syntax
sigma(expression, start, end, variable);

may be expression could be a function....

I know you can do this with maple/mathlab, but i don't have it. I was
hoping there was a function or a library I could get.

Assuming this notation stands for the infinite sum of 2^n/n!, I'd
say that it looks dangerously close to exp(2.0). If so, see exp.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
G

Grumble

Quackker12 said:
is there a function in ansi c that does this?

sigma(2^n/n!,0,infinity,n);

Inf
e^x = Sum x^n / n!
n=0

Thus,

Inf
Sum 2^n / n! = e^2
n=0

This series converges very fast.

#include <stdio.h>

int main(void)
{
double i=1, term=1, sum=0, prev;

do
{
prev = sum;
sum += term;
term *= 2/i;
++i;
}
while (sum != prev);

printf("sum=%.12f\n", sum);

return 0;
}
 
J

James Hu

But if the poster really needs to do sums *to infinity* it is much
harder, and can only really be done by analysis of the function.

The implementation provides a sufficient approximation if you do two
things:

(1) #define infinity INT_MAX
(2) Choose a C implementation with an INT_MAX that is sufficiently
large.

-- James
 
R

Rob Thorpe

James Hu said:
The implementation provides a sufficient approximation if you do two
things:

(1) #define infinity INT_MAX
(2) Choose a C implementation with an INT_MAX that is sufficiently
large.

-- James

An infinite sum is a mathematical idea. In general, it can't be
calculated precisely without using analysis. It could be done
approximately by using a large number of interations of a floating
point calculation, but this wouldn't work well, in some cases. An
integer based calculation would not work, in general.
 
Q

Quackker12

Grumble said:
Inf
e^x = Sum x^n / n!
n=0

Thus,

Inf
Sum 2^n / n! = e^2
n=0

This series converges very fast.

#include <stdio.h>

int main(void)
{
double i=1, term=1, sum=0, prev;

do
{
prev = sum;
sum += term;
term *= 2/i;
++i;
}
while (sum != prev);

printf("sum=%.12f\n", sum);

return 0;
}


What If I wanted to do

INF
Sum (-1)^n * (x^(2*n+1))/(2*n+1)!
n=0

which is sin(x), by the way. Calculus class; just making sure teacher
was right (or proving it to myself).

(e-mail address removed)
 
K

Keith Thompson

An infinite sum is a mathematical idea. In general, it can't be
calculated precisely without using analysis. It could be done
approximately by using a large number of interations of a floating
point calculation, but this wouldn't work well, in some cases. An
integer based calculation would not work, in general.

And yet <math,h> declares a number of functions that are (or can be)
defined in terms of infinite sums. It's often possible to compute
such functions to as much precision (and accuracy!) as needed.
 
P

P.J. Plauger

And yet <math,h> declares a number of functions that are (or can be)
defined in terms of infinite sums. It's often possible to compute
such functions to as much precision (and accuracy!) as needed.

But not by summing a large number of terms.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
R

Rich Gibbs

Keith Thompson said the following, on 04/29/04 17:47:
And yet <math,h> declares a number of functions that are (or can be)
defined in terms of infinite sums. It's often possible to compute
such functions to as much precision (and accuracy!) as needed.

We are wandering rather OT for this group, but I think you (and James
Hu) are missing the point that Rob is making.

You are quite right that there are functions in the standard library
that can be defined by infinite series; some of them may use the
evaluation of a truncated series to calculate function values. (This is
not always, though, the best way to skin that particular cat.)

The OP's _example_ function, as PJ Plauger has noted elsewhere in this
thread, is in fact exp(2.0). But, as I read his question, he asked
about a _general_ method for evaluating an infinite series. Whether or
not this evaluation is feasible for any particular case is a question
for mathematical, and not programming, analysis.
 
J

James Hu

Keith Thompson said the following, on 04/29/04 17:47:

We are wandering rather OT for this group, but I think you (and James
Hu) are missing the point that Rob is making.

I don't think I missed Rob's point. Apparently you and Rob did
not read the word "approximation" in my post.

Certainly analysis can determine whether or not a particular
series is convergent or divergent, and can sometimes yield
a closed form. However, the original poster did not post
his true intent, so I felt free to apply an artistic license
to interpret the question that yielded a semi-topical answer.

Many numerical methods do rely on iteration of terms and testing
successive terms for convergence. Irregardless, if the OP wanted
a general symbolic math library, then this is the wrong newsgroup
to ask the question. If the poster wanted to know how to pass
a function that evaluates an expression to another function, then
I think my answer was spot on.

Now having said this, my first response to Rob's point was
half-way tounge in cheek, and apparently this point was also
lost since he snipped my signature in that post. I used the
term "sufficiently large MAX_INT", and I think that alone should
have clued readers in that I was not entirely serious.

-- James
 
R

Rob Thorpe

James Hu said:
Keith Thompson said the following, on 04/29/04 17:47:
(e-mail address removed) (Rob Thorpe) writes:
[...]

An infinite sum is a mathematical idea. In general, it can't be
calculated precisely without using analysis. It could be done
approximately by using a large number of interations of a floating
point calculation, but this wouldn't work well, in some cases. An
integer based calculation would not work, in general.


And yet <math,h> declares a number of functions that are (or can be)
defined in terms of infinite sums. It's often possible to compute
such functions to as much precision (and accuracy!) as needed.

We are wandering rather OT for this group, but I think you (and James
Hu) are missing the point that Rob is making.

I don't think I missed Rob's point. Apparently you and Rob did
not read the word "approximation" in my post.

Certainly analysis can determine whether or not a particular
series is convergent or divergent, and can sometimes yield
a closed form. However, the original poster did not post
his true intent, so I felt free to apply an artistic license
to interpret the question that yielded a semi-topical answer.

Many numerical methods do rely on iteration of terms and testing
successive terms for convergence. Irregardless, if the OP wanted
a general symbolic math library, then this is the wrong newsgroup
to ask the question. If the poster wanted to know how to pass
a function that evaluates an expression to another function, then
I think my answer was spot on.

Now having said this, my first response to Rob's point was
half-way tounge in cheek, and apparently this point was also
lost since he snipped my signature in that post. I used the
term "sufficiently large MAX_INT", and I think that alone should
have clued readers in that I was not entirely serious.

-- James


Ah yes, sorry, I missed the approximately and the sig.
 
R

Rich Gibbs

James Hu said the following, on 04/30/04 00:37:
Keith Thompson said the following, on 04/29/04 17:47:
(e-mail address removed) (Rob Thorpe) writes:
[...]


An infinite sum is a mathematical idea. In general, it can't be
calculated precisely without using analysis. It could be done
approximately by using a large number of interations of a floating
point calculation, but this wouldn't work well, in some cases. An
integer based calculation would not work, in general.


And yet <math,h> declares a number of functions that are (or can be)
defined in terms of infinite sums. It's often possible to compute
such functions to as much precision (and accuracy!) as needed.

We are wandering rather OT for this group, but I think you (and James
Hu) are missing the point that Rob is making.


I don't think I missed Rob's point. Apparently you and Rob did
not read the word "approximation" in my post.
[snip]
If the poster wanted to know how to pass
a function that evaluates an expression to another function, then
I think my answer was spot on.

Agreed. I was really referring to your second post, not the first. I
should have said so.
Now having said this, my first response to Rob's point was
half-way tounge in cheek, and apparently this point was also
lost since he snipped my signature in that post. I used the
term "sufficiently large MAX_INT", and I think that alone should
have clued readers in that I was not entirely serious.

OK, you're right, I did overlook the sig -- sorry about that. (I still
have some concern about this sort of "tongue in cheek" answer, however,
because I think it likely that some folks will take it seriously.)
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top