Function generator

Hello to all,

I want to make an integrator to compute a physical problem. My function should take a function and return or generate a new function which is the integrated of the argument function.

For example, I represent the function f(x,y,z)=x^2+yz as

1
2
3
double my_function (double x[]) {
    return x[0]*x[0]+x[1]*x[2];
}


Knowing this initial function, I want my code to generate another function (The way in which it is done is not important; any programming structure can be used.) such that F(x,y,z)=(x^3)yz/3+((yz)^2)x/4. Is this possible in C++?

Note: My question is not about taking integrals. If I can find a code that can generate such functions, of course I will give it the procedures to take integrals. So, I just want to learn how I can generate a function.

Thank you
I'm not sure I understand what you are asking. Do you have an example in any other language? Something like this sounds like it would be much easier to accomplish in a dynamic language. But, again, I don't really understand what you want. There are too many ways for me to interpret your question.
No, it's pretty clear. It should perform an integration of a (mathematical) function.

The answer to the question of possibility: Yes, it is possible. However, parsing functions is by no means easy. You would have to be fluent in the math behind it, and in programming itself as well.

However, regarding the functions you should probably first try to find a solution for one-variable functions, and then find a more general one for multiple variable functions.

Also, I don't try to be mean, but if you are actually trying to represent a function as an array I am afraid the problem might be a bit over your head.
Here are my 5ยข.

I think that hanst99 meant symbolic integration in his post. Symbolic integration will be of-course much harder, because you need to develop syntax tree structure for your math functions. C++ does not support code reflection and consequently you can not just browse the contents of one function and using symbolic transformations, produce another one. But nonetheless there are fine features in C++ that will allow you to create elegant interface to the users of your symbolic integration facilities, that are as little intrusive to readability and style as possible in such a language.

If what you aim for is numerical integration, then you can use function objects. A function object can be called, but is actually a data structure like any other. You can store the original function inside the object, and use this function to compute the integral when it is needed.

So, before we go further, what kind of integration do you plan to support? How do you envision its operation, aside from the C++ stuff (performance-wise)?

Regards
Here's a sample of how to perform numeric integration. Bear in mind that it's just an example - the algorithm I've used is probably just about the worst around ;)

I've used standard mathematical notation in the integration function integrate. x and y are the endpoints of the interval to be integrated over. n is the number of subdivisions to be used and h is the length of each subdivision.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

typedef double (*function)(double);

double integrate(function f, double x, double y, int n)
{
	double value = 0.0;
	double h = (y - x) / (double)n;
	for (int i=0; i<n; i++)
		value += h * 0.5 * (f(y) + f(x));
	return value;
}

double xsqr(double x) { return x * x; } // this is the function to be integrated: we could use a different one

int main()
{
	std::cout << integrate(&xsqr, 0.0, 1.0, 10); // print the integral of x^2 from 0 to 1 (1/2)
	std::cin.get();
	return 0;
}


Don't know if this will be helpful, but feel free to take a look :)
Yes, I meant symbolic integration. Cause that's what I thought the author was talking about when he said "generate another function".
You're probably right. That does sound like what he meant. But numeric integration is a much easier option so I thought I'd give the example as it demonstrates a means of integrating without having to parse strings of mathematical expressions and I thought it might good to suggest a simpler approach.

Indeed one can still generate a new function in a manner of speaking using numeric methods. Suppose we want to integrate a function f(x) described by the C++ function double f(double x). Then we could define a new function as follows (using the integration function I put above).

1
2
3
4
double f_int(double x, double a = 0.0, int n = 10)
{
   return f(a) + int(f, a, x, n);
}


I imagine symbolic integration of polynomials not to be too bad, but I wouldn't want to try anything more complex that that ;)
Last edited on
Xander314, i think you are right, but that guy asked you about C++...
from my experience this is C

1
2
std::cout << integrate(&xsqr, 0.0, 1.0, 10); // print the integral of x^2 from 0 to 1 (1/2)
std::cin.get();
Last edited on
Well, I have no experience with it whatsoever, but I think I should then mention the GiNaC library (http://www.ginac.de/ ). This is for lighter symbolic math in C++. Otherwise, creating the stuff from scratch - it is indeed major undertaking.
EDIT: Looking at its license now - GPL. This may not be suitable for commercial development. But you can at least see what C++ tricks they have used under the hood.
Last edited on
Topic archived. No new replies allowed.