00001 #include<LEDA/list.h>
00002 #include<scil/global.h>
00003 #include<scil/row.h>
00004 #include<scil/row_constraint.h>
00005
00006 row::row(double d) {
00007
00008 NZ.append(row_entry(nil, d));
00009 }
00010
00011 cons_obj* row::operator== (row r1) {
00012 row r2=(*this)-r1;
00013 r2.normalize();
00014 return new row_constraint(r2, Equal);
00015 }
00016
00017 cons_obj* row::operator<= (row r1) {
00018 row r2=(*this)-r1;
00019 r2.normalize();
00020 return new row_constraint(r2, Less);
00021 }
00022
00023 cons_obj* row::operator>= (row r1) {
00024 row r2=(*this)-r1;
00025 r2.normalize();
00026 return new row_constraint(r2, Greater);
00027 }
00028
00029 row::row(var v) {
00030 NZ.append(row_entry(v,1));
00031 }
00032
00033 row::row(list<row_entry>& L) {
00034 NZ=L;
00035 }
00036
00037 row row::operator* (double d) {
00038 row_entry st;
00039 list<row_entry> NNZ;
00040 forall(st, NZ) {
00041 NNZ.append(row_entry(st.Var, d*st.coeff));
00042 };
00043 return row(NNZ);
00044 }
00045
00046 row row::operator+ (const row& r) {
00047 list<row_entry> NNZ;
00048 list_item l1=r.NZ.first();
00049 row_entry st;
00050 double d;
00051 forall(st, NZ) {
00052 d=0;
00053 while((l1!=nil) && (r.NZ[l1].Var<=st.Var)) {
00054 if(r.NZ[l1].Var!=st.Var) {
00055 NNZ.append(r.NZ[l1]);
00056 } else {
00057 d=r.NZ[l1].coeff;
00058 }
00059 l1=r.NZ.succ(l1);
00060 }
00061 NNZ.append(row_entry(st.Var, st.coeff+d));
00062 }
00063
00064 while(l1!=nil) {
00065 NNZ.append(r.NZ[l1]);
00066 l1=r.NZ.succ(l1);
00067 }
00068 return row(NNZ);
00069 }
00070
00071 row& row::operator+= (const row& r) {
00072 row_entry st;
00073 forall(st, r.NZ) {
00074 NZ.append(st);
00075 }
00076 return *this;
00077 }
00078
00079 row& row::operator-= (const row& r) {
00080 row_entry st;
00081 forall(st, r.NZ) {
00082 NZ.append(row_entry(st.Var, -st.coeff));
00083 }
00084 return *this;
00085 }
00086
00087 void row::normalize() {
00088 NZ.sort();
00089 list_item li=NZ.first_item();
00090 int i=1;
00091 while(li!=nil) {
00092 while((i<NZ.length()) && (NZ[li].Var==NZ[NZ.succ(li)].Var)) {
00093 NZ[li].coeff+=NZ[NZ.succ(li)].coeff;
00094 NZ.del_item(NZ.succ(li));
00095 }
00096 i++;
00097 li=NZ.succ(li);
00098 }
00099 }
00100
00101 row row::operator+ (const var& v) {
00102 return operator+(row(v));
00103 };
00104
00105 row row::operator+ (double d) {
00106 return operator+(row(d));
00107 };
00108
00109 row row::operator- (const row& r) {
00110 list<row_entry> NNZ;
00111 list_item l1=r.NZ.first();
00112 row_entry st;
00113 double d;
00114 forall(st, NZ) {
00115 d=0;
00116 while((l1!=nil) && (r.NZ[l1].Var<=st.Var)) {
00117 if(r.NZ[l1].Var!=st.Var) {
00118 NNZ.append(row_entry(r.NZ[l1].Var, -r.NZ[l1].coeff));
00119 } else {
00120 d=-r.NZ[l1].coeff;
00121 }
00122 l1=r.NZ.succ(l1);
00123 }
00124 NNZ.append(row_entry(st.Var, st.coeff+d));
00125 }
00126 while(l1!=nil) {
00127 NNZ.append(row_entry(r.NZ[l1].Var, -r.NZ[l1].coeff));
00128 l1=r.NZ.succ(l1);
00129 }
00130 return row(NNZ);
00131 }
00132
00133 row operator*(double d, var v) {
00134 return row(v)*d;
00135 };
00136
00137 row operator*(double d, row r) {
00138 return r*d;
00139 };
00140
00141
00142 row::item row::first_item() const {
00143 return NZ.first_item();
00144 };
00145
00146 row::item row::next_item(item i) const {
00147 return NZ.next_item(i);
00148 }
00149
00150 row_entry row::inf(const item i) const {
00151 return NZ.inf(i);
00152 }
00153
00154 int row::size() {
00155
00156 return NZ.size();
00157 }
00158