Sum with index as a function

I would like to do a sum like the following in gams:

Equation(i) … Sum ( (j = i - a + 1)(ord(j) GE 1)(ord(j) LE i), x(j) ) = b(i) ;

My index j is in terms of i.

How may I do this? Thanks. :slight_smile:

Hi

Here is a little example that you can use to implement your equation. I assume that a is a parameter. (you can’t use a variable in a $-condition in an equation)

set j /1*20/, i /1*20/;

parameter a /-2/, b(i), x(j);

x(j) = uniform(1,10);
b(i) =  Sum (j$(j.val = i.val - a + 1) , x(j));

display b, x;

Note that you can use i.val as long as the set consists of numbers. Otherwise, you have to use ord(i).
You can add additional constraints after the dollar sign (connect with AND or OR).

Cheers
Renger

Hi Renger,

I really appreciate your help. I advanced a lot with this, however I would like to add a condition for the index, and I try the following but it shows error:

set j /120/, i /120/;

parameter a /-2/, b(i), x(j);

x(j) = uniform(1,10);
b(i) = Sum ( j$(j.val = i.val - a + 1)$(ord(j) GE 2) , x(j));

display b, x;


I know you mentioned something about in your message, I have tried on different ways but it shows error. Thanks in advance.

Hello,

Maybe you can try this:

set j /1*20/, i /1*20/;

parameter a /-2/, b(i), x(j);

x(j) = uniform(1,10);
b(i) = Sum ( j$(j.val = i.val - a + 1 and ord(j) GE 2) , x(j));

display b, x;

Hello,

Thanks a lot, it works now! :smiley:

You are welcome :smiley: