# GAMS Support Wiki

### Site Tools

gams:model_a_band_matrix

# How do I model a band matrix?

A band matrix can be modeled efficiently in GAMS using circular leads (`++`) and circular lags (`--`), see chapter lag and lead operator of the GAMS User's Guide for more information. Using circular leads and lags, the first and last members of the set are assumed to be adjacent, so as to form a circular sequence of members. These operators only work with ordered sets.

Find below an example, where the generation of a model with the usage of circular leads (equation `eq`) was ~50 times faster than the original formulation using `sum` - statements and lots of comparisons (equation `eq1` and `eq2`). Both models are identical.

```set k / k1*k300/
t / t1*t700/;
alias (t,tt,ll, t2);

parameter l(k); l(k) = uniformInt(1,6);

variable y(k,t), z(k,t), obj;
equation defobj;
defobj.. obj =e= 0;
set ktl(k,t),
kl(k,ll);
kl(k,ll) = ord(ll) <= l(k);

equation eq;
eq(k,t).. sum(kl(k,ll), y(k,t++(ord(ll)-1))) =l= z(k,t++(l(k)-1));

model m1 /eq, defobj/;
solve m1 min obj using lp;

*******************************************************************
*original formulation

equation eq1, eq2;

eq1(k,t)\$(ord(t)<=card(t)-l(k)+1)..
sum(t2\$((ord(t2)>=ord(t)) and(ord(t2)<ord(t)+l(k))),y(k,t2))
=l=
sum(t2\$(ord(t2)=l(k)+ord(t)-1),Z(k,t2));
eq2(k,t)\$(ord(t)> card(t)-l(k)+1) ..
sum(t2\$(ord(t2)>=ord(t)),Y(k,t2)) +
sum(t2\$(ord(t2)<l(k)-card(t)+ord(t)),Y(k,t2))
=l=
sum(t2\$(ord(t2)=l(k)-card(t)+ord(t)-1),Z(k,t2));

model m2 /defobj, eq1, eq2/;
solve m2 min obj using lp;
``` 