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