Dear all,

I am working with functions from R^n to R with n quite large (fromm n=100 to n=1000)

Dummy example:

for f(x)=(x+y)^2+(x+y)^2+...+(x+y)^2 I create the functions this way:`const ExprNode* e;`

`Variable x(2, "x");`

`e = &(sqr(x[1] + x[0]));`

`for (unsigned int i = 1; i < 1000; i++)`

` e = &(*e + sqr(x[1]+ x[0]));`

`Function *f= new Function(x, *e, "f"); `

However as i grows, each loop execution takes longer and longer, probably due to the concatenation.

Is there a smarter/faster way to programmatically create this type of function?

Regards,

Jeremy Nicola

- Log in to post comments

gilles chabert

Fri, 11/20/2015 - 18:32

Permalink

## Hi Jérémy,

Hi Jérémy,

When you apply the "+" operator between two expressions, they are scanned to detect subexpressions that are in common. This takes a time which is linear in the size of each sub-expression.

That is why the loop takes longer and longer, this is because of the "+" operator.

So the whole loop has a quadratic complexity. To circumvent this in your example, you can simply build the expression "blockwise", like this:

int N=10000;

for (unsigned int i = 1; i < log2(N)+1; i++)

e = &(*e + *e);

This will go much faster.

Gilles