00001
00002
00003
00004
00005
00006
00007 #ifndef USERVECTOR_H
00008 #define USERVECTOR_H
00009
00010
00011 #ifndef TNT_NO_BOUNDS_CHECK
00012 #define TNT_BOUNDS_CHECK
00013 #endif
00014 #include <tnt_vec.h>
00015
00016 template <class Type> class DenseVector;
00017
00032 template <class Type> class UserVector {
00040 friend ostream& operator << (ostream& out, const UserVector<Type>& v) {
00041 v.print(out);
00042 return out;
00043 }
00044
00045 friend void operator>>(const UserVector<Type>& v, Type* out) {
00046 for (int i=0; i<v.dim(); ++i) out[i]=v(i);
00047 }
00048
00049 friend istream& operator >> (istream& in, UserVector<Type>& v) {
00050 for (int i=0; i<v.dim(); i++) in >> v[i];
00051 return in;
00052 }
00053
00054 public:
00058 UserVector() { }
00059
00062 virtual ~UserVector() { }
00063
00069 virtual Pointer<UserVector<Type> > getemptycopy(int n) const=0;
00070
00075 virtual Pointer<UserVector<Type> > getemptycopy() const {
00076 return getemptycopy(dim());
00077 }
00078
00082 virtual Pointer<UserVector<Type> > getcopy() const {
00083 Pointer<UserVector<Type> > ret(getemptycopy());
00084 *ret=*this;
00085 return ret;
00086 }
00087
00092 virtual Pointer<UserVector<Type> > getcopy(const UserVector<int>& block) const {
00093 Pointer<UserVector<Type> > ret(getemptycopy(block.size()));
00094 for (int i=0; i<block.size(); i++) ret->SetElement(i, (*this)(block(i)));
00095 return ret;
00096 }
00097
00102 virtual int dim() const=0;
00103
00108 int size() const { return dim(); }
00109
00113 virtual void resize(const int n) {
00114 assert(n>=0);
00115 }
00116
00124 virtual Type& operator[](const int i)=0;
00125
00131 virtual Type operator[](const int i) const {
00132 return (*this)(i);
00133 }
00134
00142 virtual Type operator()(const int i) const=0;
00143
00149 virtual DenseVector<Type> operator()(const UserVector<int>& block) const {
00150 return DenseVector<Type>(*this, block);
00151 }
00152
00159 virtual DenseVector<Type> operator()(const int low, const int up) const {
00160 return DenseVector<Type>(*this, low, up);
00161 }
00162
00168 virtual void SetElement(const int i, const Type& v) {
00169 (*this)[i]=v;
00170 }
00171
00176 virtual void set_block(const UserVector<Type>& v, const UserVector<int>& block) {
00177 for (int i=0; i<block.size(); i++) (*this)[block[i]]=v(i);
00178 }
00179
00182 virtual operator const Pointer<Type>() const=0;
00183
00189 virtual UserVector<Type>& operator=(const UserVector<Type>& v)=0;
00190
00196 virtual UserVector<Type>& operator=(const Type& v)=0;
00197
00203 virtual void set(const Type* v, int n) {
00204 for (int i=0; i<n; i++) SetElement(i, v[i]);
00205 }
00206
00212 void set(const Type* v) { set(v, dim()); }
00213
00219 virtual UserVector<Type>& AddMult(Type a, const UserVector<Type>& v) {
00220 return (*this)+=a*v;
00221 }
00222
00228 virtual UserVector<Type>& operator+=(const UserVector<Type>& v)=0;
00229
00235 virtual UserVector<Type>& operator-=(const UserVector<Type>& v)=0;
00236
00242 virtual UserVector<Type>& operator*=(const Type& v)=0;
00243
00249 virtual UserVector<Type>& operator/=(const Type& v)=0;
00250
00255 virtual bool operator>=(const Type& v) const {
00256 for (int i=0; i<dim(); i++) if ((*this)(i)<v) return false;
00257 return true;
00258 }
00259
00264 virtual bool operator==(const Type& v) const {
00265 for (int i=0; i<dim(); i++) if ((*this)(i)!=v) return false;
00266 return true;
00267 }
00268
00273 virtual bool operator==(const UserVector<Type>& v) const {
00274 assert(v.dim()==dim());
00275 for (int i=0; i<dim(); i++) if ((*this)(i)!=v(i)) return false;
00276 return true;
00277 }
00278
00283 virtual Type operator*(const UserVector<Type>& v) const {
00284 assert(v.dim()==dim());
00285 Type val=Type();
00286 for (int i=0; i<dim(); i++) val+=(*this)(i)*v(i);
00287 return val;
00288 }
00289
00294 virtual DenseVector<Type> operator*(const Type& v) const {
00295 return DenseVector<Type>(*this)*v;
00296 }
00297
00304 friend DenseVector<Type> operator*(const Type& v1, const UserVector<Type>& v2) {
00305 return v2 * v1;
00306 }
00307
00313 virtual DenseVector<Type> operator+(const UserVector<Type>& v) const {
00314 return DenseVector<Type>(*this) + v;
00315 }
00316
00320 virtual const UserVector<Type>& operator+() const {
00321 return *(const UserVector<Type>*)this;
00322 }
00323
00327 virtual UserVector<Type>& operator+() {
00328 return *this;
00329 }
00330
00336 virtual DenseVector<Type> operator-(const UserVector<Type>& v) const {
00337 return DenseVector<Type>(*this)-v;
00338 }
00339
00344 virtual DenseVector<Type> operator-() const {
00345 return -DenseVector<Type>(*this);
00346 }
00347
00355 virtual void diagmult(UserVector<Type>& y, const UserVector<Type>& v) const {
00356 y=diagmult(v);
00357 }
00358
00366 virtual DenseVector<Type> diagmult(const UserVector<Type>& v) const {
00367 return DenseVector<Type>(*this).diagmult(v);
00368 }
00369
00373 virtual Type sq_norm2() const {
00374 Type ret=0;
00375 for (int i=0; i<dim(); i++) ret+=(*this)(i)*(*this)(i);
00376 return ret;
00377 }
00378
00379 virtual Type dist(const UserVector<Type>& v) const {
00380 return (Type)sqrt((*this-v).sq_norm2());
00381 }
00382
00386 virtual Type mean_value() const {
00387 Type val=0;
00388 for (int i=0; i<dim(); i++) val+=(*this)(i);
00389 val*=(Type)(1./dim());
00390 return val;
00391 }
00392
00397 virtual Type standard_deviation() const {
00398 Type mval(mean_value());
00399 Type val=0;
00400 for (int i=0; i<dim(); i++) val+=(mval-(*this)(i))*(mval-(*this)(i));
00401 val*=(Type)(1./dim());
00402 return (Type)sqrt(val);
00403 }
00404
00409 void set_random(const Type lb, const Type ub) {
00410 assert(lb<=ub);
00411 for (int i=0; i<dim(); i++) (*this)[i]=random(lb, ub);
00412 }
00413
00418 void set_random(const UserVector<Type>& low, const UserVector<Type>& up) {
00419 for (int i=0; i<dim(); i++) (*this)[i]=random(low(i), up(i));
00420 }
00421
00427 void set_random(const Type lb, const Type ub, const double sparsity) {
00428 assert(0<=sparsity && sparsity<=1);
00429 assert(lb<=ub);
00430 (*this)=0;
00431 int ne=(dim()*sparsity);
00432 for (int i=0; i<ne; i++)
00433 (*this)[random((int)(dim()/ne)*i,(int)(dim()/ne)*(i+1))]=random(lb, ub);
00434 }
00435
00440 virtual void print(ostream& out) const {
00441 for (int i=0; i<dim(); i++) out << (*this)(i) << " ";
00442 out << endl;
00443 }
00444
00445 };
00446
00449 template <class Type>
00450 class DenseVector : public UserVector<Type> {
00451 public:
00457 static Pointer<DenseVector<Type> > random(const int n, const Type lb, const Type ub) {
00458 Pointer<DenseVector<Type> > ret=new DenseVector<Type>(n);
00459 ret->set_random(lb, ub);
00460 return ret;
00461 }
00462
00469 static Pointer<DenseVector<Type> > random(const int n, const Type lb, const Type ub, const double sparsity) {
00470 Pointer<DenseVector<Type> > ret=new DenseVector<Type>(n);
00471 ret->set_random(lb, ub, sparsity);
00472 return ret;
00473 }
00474
00479 static Pointer<DenseVector<Type> > random(const DenseVector<Type> low, const DenseVector<Type> up) {
00480 Pointer<DenseVector<Type> > ret(new DenseVector<Type>(low.dim()));
00481 ret->set_random(low, up);
00482 return ret;
00483 }
00484
00485 protected:
00488 TNT::Vector<Type> x;
00489
00490 public:
00495 DenseVector(int n=0, const Type& v=Type())
00496 : x(n, v)
00497 { }
00498
00502 DenseVector(const DenseVector<Type>& v)
00503 : x(v.x)
00504 { }
00505
00509 DenseVector(const UserVector<Type>& v)
00510 : x(v.dim())
00511 { for (int i=0; i<dim(); ++i) x[i]=v(i);
00512 }
00513
00520 DenseVector(const UserVector<Type>& v, const int low, const int up)
00521 : x(up-low+1)
00522 { for (int i=low; i<=up; i++) x[i-low]=v(i);
00523 }
00524
00529 DenseVector(const UserVector<Type>& v, const UserVector<int>& block)
00530 : x(block.dim())
00531 { for (int i=0; i<block.size(); i++) x[i]=v(block(i));
00532 }
00533
00538 DenseVector(const vector<DenseVector<Type>* >& v, const vector<DenseVector<int> >& block)
00539 : x(0)
00540 { int s=0; for (int i=0; i<block.size(); i++) s+=block[i].size();
00541 resize(s);
00542 for (int k=0; k<block.size(); k++) if (v[k]) set_block(*v[k], block[k]);
00543 }
00544
00545 DenseVector(const vector<Pointer<UserVector<Type> > >& v, const vector<DenseVector<int> >& block)
00546 : x(0)
00547 { int s=0; for (int i=0; i<block.size(); i++) s+=block[i].size();
00548 resize(s);
00549 for (int k=0; k<block.size(); k++) if (v[k]) set_block(*v[k], block[k]);
00550 }
00551
00552 DenseVector(const vector<Pointer<DenseVector<Type> > >& v, const vector<DenseVector<int> >& block)
00553 : x(0)
00554 { int s=0; for (int i=0; i<block.size(); i++) s+=block[i].size();
00555 resize(s);
00556 for (int k=0; k<block.size(); k++) if (v[k]) set_block(*v[k], block[k]);
00557 }
00558
00563 DenseVector(const vector<DenseVector<Type> >& v, const vector<DenseVector<int> >& block)
00564 : x(0)
00565 { int s=0; for (int i=0; i<block.size(); i++) s+=block[i].size();
00566 resize(s);
00567 for (int k=0; k<block.size(); k++) set_block(v[k], block[k]);
00568 }
00569
00574 DenseVector(const Type* v, const int n)
00575 : x(n, v)
00576 { }
00577
00581 DenseVector(const TNT::Vector<Type>& x_)
00582 : x(x_)
00583 { }
00584
00585 Pointer<UserVector<Type> > getemptycopy(int n) const {
00586 return new DenseVector<Type>(n);
00587 }
00588
00589 Pointer<UserVector<Type> > getemptycopy() const {
00590 return new DenseVector<Type>(dim());
00591 }
00592
00593 Pointer<UserVector<Type> > getcopy() const {
00594 return new DenseVector<Type>(*this);
00595 }
00596
00597 Pointer<UserVector<Type> > getcopy(const UserVector<int>& block) const {
00598 return new DenseVector<Type>(*this, block);
00599 }
00600
00601 int dim() const {
00602 return x.dim();
00603 }
00604
00605 void resize(const int n) {
00606 UserVector<Type>::resize(n);
00607 TNT::Vector<Type> tmp(x);
00608 x.newsize(n);
00609 int i=0;
00610 for (; i<MIN(n, tmp.size()); i++) x[i]=tmp[i];
00611 for (; i<n; i++) x[i]=Type();
00612 }
00613
00614 Type& operator[](const int i) {
00615 return x[i];
00616 }
00617
00618 Type operator[](const int i) const {
00619 return x[i];
00620 }
00621
00622 Type operator()(const int i) const {
00623 return x[i];
00624 }
00625
00626 void SetElement(const int i, const Type& v) {
00627 x[i]=v;
00628 }
00629
00630 DenseVector<Type> operator()(const UserVector<int>& block) const {
00631 return DenseVector<Type>(*this, block);
00632 }
00633
00634 DenseVector<Type> operator()(const int low, const int up) const {
00635 return DenseVector<Type>(*this, low, up);
00636 }
00637
00642 DenseVector<Type>& operator=(const DenseVector<Type>& v) {
00643 x=v.x;
00644 return *this;
00645 }
00646
00647 DenseVector<Type>& operator=(const UserVector<Type>& v) {
00648 assert(dim()==v.dim());
00649 for (int i=0; i<dim(); i++) x[i]=v(i);
00650 return *this;
00651 }
00652
00653 DenseVector<Type>& operator=(const Type& v) {
00654 x=v;
00655 return *this;
00656 }
00657
00658 virtual UserVector<Type>& AddMult(Type a, const UserVector<Type>& v) {
00659 assert(dim()==v.dim());
00660 for (int i=0; i<dim(); i++) x[i]+=a*v(i);
00661 return *this;
00662 }
00663
00668 DenseVector<Type>& operator+=(const DenseVector<Type>& v) {
00669 x=x+v.x;
00670 return *this;
00671 }
00672
00673 DenseVector<Type>& operator+=(const UserVector<Type>& v) {
00674 assert(dim()==v.dim());
00675 for (int i=0; i<dim(); i++) x[i]+=v(i);
00676 return *this;
00677 }
00678
00683 DenseVector<Type>& operator-=(const DenseVector<Type>& v) {
00684 x=x-v.x;
00685 return *this;
00686 }
00687
00688 DenseVector<Type>& operator-=(const UserVector<Type>& v) {
00689 assert(dim()==v.dim());
00690 for (int i=0; i<dim(); i++) x[i]-=v(i);
00691 return *this;
00692 }
00693
00694 DenseVector<Type>& operator*=(const Type& v) {
00695 for (int i=0; i<dim(); i++) x[i]*=v;
00696 return *this;
00697 }
00698
00699 DenseVector<Type>& operator/=(const Type& v) {
00700 for (int i=0; i<dim(); i++) x[i]/=v;
00701 return *this;
00702 }
00703
00704 DenseVector<Type> operator*(const Type& v) const {
00705 DenseVector<Type> ret(*this);
00706 ret*=v;
00707 return ret;
00708 }
00709
00716 friend DenseVector<Type> operator*(const Type& v1, const DenseVector<Type>& v2) {
00717 return v2 * v1;
00718 }
00719
00724 Type operator*(const DenseVector<Type>& v) const {
00725 return TNT::dot_prod(x, v.x);
00726 }
00727 using UserVector<Type>::operator*;
00728
00733 DenseVector<Type> operator+(const DenseVector<Type>& v) const {
00734 return DenseVector<Type>(x+v.x);
00735 }
00736
00737 DenseVector<Type> operator+(const UserVector<Type>& v) const {
00738 DenseVector<Type> ret(*this);
00739 return (ret+=v);
00740 }
00741
00742 const UserVector<Type>& operator+() const {
00743 return *(const DenseVector<Type>*)this;
00744 }
00745 using UserVector<Type>::operator+;
00746
00751 DenseVector<Type> operator-(const DenseVector<Type>& v) const {
00752 return DenseVector<Type>(x-v.x);
00753 }
00754
00755 DenseVector<Type> operator-(const UserVector<Type>& v) const {
00756 DenseVector<Type> ret(*this);
00757 return (ret-=v);
00758 }
00759
00760 DenseVector<Type> operator-() const {
00761 DenseVector<Type> ret(*this);
00762 ret*=-1;
00763 return ret;
00764 }
00765
00770 DenseVector<Type> diagmult(const DenseVector<Type>& v) const {
00771 return DenseVector<Type>(x*v.x);
00772 }
00773
00774 DenseVector<Type> diagmult(const UserVector<Type>& v) const {
00775 DenseVector<Type> ret(*this);
00776 for (int i=0; i<dim(); i++) ret[i]*=v(i);
00777 return ret;
00778 }
00779 using UserVector<Type>::diagmult;
00780
00783 operator const Pointer<Type>() const {
00784 return (const Pointer<Type>)Pointer<Type>(x.begin(), false);
00785 }
00786
00789
00790
00791
00792
00797 void print(ostream& out) const {
00798 for (int i=0; i<dim(); i++) out << x[i] << " ";
00799 out << endl;
00800 }
00801
00802 };
00803
00804 typedef DenseVector<double> dvector;
00805 typedef DenseVector<int> ivector;
00806
00807 class SparseMatrix2;
00808 class IntervalVector;
00809
00813 template <class Type>
00814 class SparseVector : public UserVector<Type> {
00815 friend class SparseMatrix;
00816 friend class IntervalVector;
00817 public:
00823 static Pointer<SparseVector<Type> > random(const int n, const Type lb, const Type ub) {
00824 Pointer<SparseVector<Type> > ret=new SparseVector<Type>(n);
00825 ret->set_random(lb, ub);
00826 return ret;
00827 }
00828
00835 static Pointer<SparseVector<Type> > random(const int n, const Type lb, const Type ub, const double sparsity) {
00836 Pointer<SparseVector<Type> > ret=new SparseVector<Type>(n);
00837 ret->set_random(lb, ub, sparsity);
00838 return ret;
00839 }
00840
00843 struct VectorElement {
00846 int index;
00849 Type value;
00852 VectorElement* next;
00853 };
00854
00855 protected:
00858 VectorElement* head;
00861 VectorElement* last;
00862
00865 int dim_;
00866
00874 VectorElement* GetElement(const int i, bool create_if_not_exists=true) {
00875 assert(i<dim());
00876 if (last && (last->index<i)) {
00877 if (! create_if_not_exists) return NULL;
00878 last=last->next=new VectorElement;
00879 last->index=i;
00880 last->value=Type();
00881 last->next=0;
00882 return last;
00883 }
00884 VectorElement* p=head;
00885 for (VectorElement* q=p->next; q; p=q, q=q->next) {
00886 if (q->index==i) return q;
00887 if (q->index>i) {
00888 if (! create_if_not_exists) return NULL;
00889 p->next=new VectorElement;
00890 p->next->index=i;
00891 p->next->value=Type();
00892 p->next->next=q;
00893 return p->next;
00894 }
00895 }
00896 if (! create_if_not_exists) return NULL;
00897 last=p->next=new VectorElement;
00898 last->index=i;
00899 last->value=Type();
00900 last->next=0;
00901 return last;
00902 }
00903
00909 void clear(VectorElement* v) {
00910 VectorElement* next=v->next;
00911 for (VectorElement* p=next; next; p=next) { next=next->next; delete p; }
00912 last=v;
00913 last->next=NULL;
00914 }
00915
00916 public:
00917 const VectorElement* gethead() { return head; }
00918
00922 SparseVector(int n=0)
00923 : dim_(n), head(new VectorElement), last(0)
00924 { assert(n>=0);
00925 head->next=NULL;
00926 }
00927
00933 SparseVector(int n, int i, Type v)
00934 : dim_(n), head(new VectorElement), last(0)
00935 { assert(n>=0);
00936 head->next=NULL;
00937 SetElement(i, v);
00938 }
00939
00947 SparseVector(int n, int i, int j, Type vi, Type vj)
00948 : dim_(n), head(new VectorElement), last(0)
00949 { assert(n>=0);
00950 head->next=NULL;
00951 SetElement(i, vi);
00952 SetElement(j, vj);
00953 }
00954
00958 SparseVector(const SparseVector<Type>& v)
00959 : dim_(v.dim()), head(new VectorElement), last(0)
00960 { head->next=NULL;
00961 for (VectorElement* p=v.head->next; p; p=p->next) {
00962 if (! p->value) continue;
00963 if (last) last=last->next=new VectorElement;
00964 else { last=new VectorElement; last->next=NULL; }
00965 if (!head->next) head->next=last;
00966 last->index=p->index;
00967 last->value=p->value;
00968 }
00969 if (last) last->next=0;
00970 }
00971
00975 SparseVector(const UserVector<Type>& v)
00976 : dim_(v.dim()), head(new VectorElement), last(0)
00977 { head->next=NULL;
00978 double value;
00979 for (int i=0; i<v.dim(); i++) {
00980 if (fabs(value=v(i))<rtol) continue;
00981 if (last) last=last->next=new VectorElement;
00982 else { last=new VectorElement; last->next=NULL; }
00983 if (!head->next) head->next=last;
00984 last->index=i;
00985 last->value=value;
00986 }
00987 if (last) last->next=0;
00988 }
00989
00995 SparseVector(const UserVector<Type>& v, const int low, const int up)
00996 : dim_(up-low+1), head(new VectorElement), last(NULL)
00997 { head->next=NULL;
00998 double value;
00999 for (int i=low; i<=up; i++) {
01000 if (fabs(value=v(i))<rtol) continue;
01001 if (last) last=last->next=new VectorElement;
01002 else { last=new VectorElement; last->next=NULL; }
01003 if (!head->next) head->next=last;
01004 last->index=i-low;
01005 last->value=value;
01006 }
01007 if (last) last->next=0;
01008 }
01009
01014 SparseVector(const UserVector<Type>& v, const UserVector<int>& block)
01015 : dim_(block.size()), head(new VectorElement), last(NULL)
01016 { head->next=NULL;
01017 for (int i=0; i<block.size(); i++) SetElement(i, v(block(i)));
01018 }
01019
01024 SparseVector(const vector<Pointer<UserVector<Type> > >& v, const vector<DenseVector<int> >& block)
01025 : dim_(0), head(new VectorElement), last(NULL)
01026 { head->next=NULL;
01027 for (int k=0; k<block.size(); k++) dim_+=block[k].size();
01028 for (int k=0; k<block.size(); k++)
01029 if (v[k])
01030 for (int i=0; i<block[k].size(); i++) SetElement(block[k](i), (*v[k])(i));
01031 }
01032
01037 SparseVector(const Type* v, const int& n)
01038 : dim_(n), head(new VectorElement), last(head)
01039 { head->next=NULL;
01040 for (int i=0; i<n; i++) {
01041 if (fabs(v[i])<rtol) continue;
01042 last=last->next=new VectorElement;
01043 if (!head->next) head->next=last;
01044 last->index=i;
01045 last->value=v[i];
01046 }
01047 if (last) last->next=NULL;
01048 }
01049
01050 SparseVector(const int& n, const int& nz, const int* indices, const double* elements)
01051 : dim_(n), head(new VectorElement), last(head)
01052 { head->next=NULL;
01053 for (int i=0; i<nz; ++i) {
01054 if (i) assert(indices[i-1]<indices[i]);
01055 last=last->next=new VectorElement;
01056 if (!head->next) head->next=last;
01057 last->index=indices[i];
01058 last->value=elements[i];
01059 }
01060 if (last) last->next=NULL;
01061 }
01062
01063 Pointer<UserVector<Type> > getemptycopy(int n) const {
01064 return new SparseVector<Type>(n);
01065 }
01066
01067 Pointer<UserVector<Type> > getemptycopy() const {
01068 return new SparseVector<Type>(dim());
01069 }
01070
01071 Pointer<UserVector<Type> > getcopy() const {
01072 return new SparseVector<Type>(*this);
01073 }
01074
01075 Pointer<UserVector<Type> > getcopy(const UserVector<int>& block) const {
01076 return new SparseVector<Type>(*this, block);
01077 }
01078
01083 ~SparseVector() {
01084 clear(head);
01085 delete head;
01086 }
01087
01092 int dim() const { return dim_; }
01093
01099 void resize(const int n) {
01100 UserVector<Type>::resize(n);
01101 dim_=n;
01102 if ((last && last->index<n) || (!head->next)) return;
01103 for (VectorElement* p=head; p; p=p->next)
01104 if (p->next && p->next->index>=n) {
01105 clear(p);
01106 last=p;
01107 return;
01108 }
01109 }
01110
01118 void SetElement(int i, const Type& v, bool test_for_zero) {
01119 if (test_for_zero && fabs(v)<rtol) {
01120 DelElement(i);
01121 return;
01122 }
01123 GetElement(i)->value=v;
01124 }
01125
01126 void SetElement(const int i, const Type& v) { SetElement(i, v, true); }
01127
01131 void DelElement(int i) {
01132 for (VectorElement* p=head->next, *q=head; p && p->index<=i; q=p, p=p->next)
01133 if (p->index==i) {
01134 q->next=p->next;
01135 delete p;
01136 if (last==p) last=q->next;
01137 return;
01138 }
01139 }
01140
01141 Type& operator[](const int i) {
01142 return GetElement(i)->value;
01143 }
01144
01145 Type operator[](const int i) const {
01146 return (*this)(i);
01147 }
01148
01149 Type operator()(const int i) const {
01150 assert(i<dim());
01151 if (last && (last->index<i)) return Type();
01152 for (VectorElement* p=head->next; p && p->index<=i; p=p->next)
01153 if (p->index==i) return p->value;
01154 return Type();
01155 }
01156 using UserVector<Type>::operator();
01157
01158
01159 UserVector<Type>& operator=(const UserVector<Type>& v) {
01160 assert(v.dim()==dim());
01161 for (int i=0; i<v.dim(); i++) SetElement(i, v(i));
01162 return *this;
01163 }
01164
01169 SparseVector<Type>& operator=(const SparseVector<Type>& v) {
01170 assert(v.dim()==dim());
01171 clear(head);
01172 for (VectorElement* p=v.head->next; p; p=p->next) {
01173 if (! p->value) continue;
01174 last=last->next=new VectorElement;
01175 last->index=p->index;
01176 last->value=p->value;
01177 }
01178 if (last==head) last=NULL;
01179 else last->next=NULL;
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206 return *this;
01207 }
01208
01209 UserVector<Type>& operator=(const Type& v) {
01210 if (fabs(v)<rtol) {
01211 clear(head); last=NULL;
01212 return *this;
01213 }
01214 for (int i=0; i<dim(); i++) SetElement(i, v, false);
01215 return *this;
01216 }
01217
01218 void set(const Type* v, int n) {
01219 clear(head);
01220 VectorElement* p=head;
01221 for (int i=0; i<n; i++, v++)
01222 if (fabs(*v)>rtol) {
01223 p=p->next=new VectorElement;
01224 p->index=i;
01225 p->value=*v;
01226 }
01227 p->next=NULL;
01228 last=p;
01229 }
01230
01231 void set(const Type* v) { set(v, dim()); }
01232
01233 UserVector<Type>& AddMult(Type a, const UserVector<Type>& v) {
01234 assert(dim()==v.dim());
01235 int i=0;
01236 VectorElement* p=head;
01237 if (p->next)
01238 for (; p->next->index < i && i < dim(); i++)
01239 if (fabs(v(i))>0) {
01240 VectorElement* r=p->next;
01241 p=p->next=new VectorElement;
01242 p->index=i;
01243 p->value=a*v(i);
01244 p->next=r;
01245 }
01246 for (; p->next && i<v.dim(); i++) {
01247 if (p->next->index == i) {
01248 p->next->value+=a*v(i);
01249 p=p->next;
01250 }
01251 else if (fabs(v(i))>0) {
01252 VectorElement* r=p->next;
01253 p=p->next=new VectorElement;
01254 p->index=i;
01255 p->value=a*v(i);
01256 p->next=r;
01257 }
01258 }
01259 for (; i<v.dim(); i++)
01260 if (fabs(v(i))>0) {
01261 p=p->next=new VectorElement;
01262 p->index=i;
01263 p->value=a*v(i);
01264 }
01265 p->next=NULL;
01266 if (p!=head) last=p;
01267 return *this;
01268 }
01269
01270 UserVector<Type>& operator+=(const UserVector<Type>& v) {
01271 assert(dim()==v.dim());
01272 int i=0;
01273 VectorElement* p=head;
01274 if (p->next)
01275 for (; p->next->index < i && i < dim(); i++)
01276 if (fabs(v(i))>0) {
01277 VectorElement* r=p->next;
01278 p=p->next=new VectorElement;
01279 p->index=i;
01280 p->value=v(i);
01281 p->next=r;
01282 }
01283 for (; p->next && i<v.dim(); i++) {
01284 if (p->next->index == i) {
01285 p->next->value+=v(i);
01286 p=p->next;
01287 }
01288 else if (fabs(v(i))>0) {
01289 VectorElement* r=p->next;
01290 p=p->next=new VectorElement;
01291 p->index=i;
01292 p->value=v(i);
01293 p->next=r;
01294 }
01295 }
01296 for (; i<v.dim(); i++)
01297 if (fabs(v(i))>0) {
01298 p=p->next=new VectorElement;
01299 p->index=i;
01300 p->value=v(i);
01301 }
01302 p->next=NULL;
01303 last=p;
01304 return *this;
01305 }
01306
01307 SparseVector<Type>& AddMult(Type a, const SparseVector<Type>& v) {
01308 assert(dim()==v.dim());
01309 VectorElement* p=head;
01310 VectorElement* q=v.head->next;
01311 while(p->next && q)
01312 if (p->next->index == q->index) {
01313 p->next->value+=a*q->value;
01314 p=p->next; q=q->next;
01315 }
01316 else
01317 if (p->next->index < q->index) p=p->next;
01318 else {
01319 VectorElement* r=p->next;
01320 p=p->next=new VectorElement;
01321 p->index=q->index;
01322 p->value=a*q->value;
01323 p->next=r;
01324 q=q->next;
01325 }
01326 for(; q; q=q->next) {
01327 p=p->next=new VectorElement;
01328 p->index=q->index;
01329 p->value=a*q->value;
01330 p->next=NULL;
01331 }
01332 if (!p->next) last=p;
01333 return *this;
01334 }
01335
01340 SparseVector<Type>& operator+=(const SparseVector<Type>& v) {
01341 assert(dim()==v.dim());
01342 VectorElement* p=head;
01343 VectorElement* q=v.head->next;
01344 while(p->next && q)
01345 if (p->next->index == q->index) {
01346 p->next->value+=q->value;
01347 p=p->next; q=q->next;
01348 }
01349 else
01350 if (p->next->index < q->index) p=p->next;
01351 else {
01352 VectorElement* r=p->next;
01353 p=p->next=new VectorElement;
01354 p->index=q->index;
01355 p->value=q->value;
01356 p->next=r;
01357 q=q->next;
01358 }
01359 for(; q; q=q->next) {
01360 p=p->next=new VectorElement;
01361 p->index=q->index;
01362 p->value=q->value;
01363 p->next=NULL;
01364 }
01365 if (!p->next) last=p;
01366 return *this;
01367 }
01368
01369 UserVector<Type>& operator-=(const UserVector<Type>& v) {
01370 assert(dim()==v.dim());
01371 int i=0;
01372 VectorElement* p=head;
01373 if (p->next)
01374 for (; p->next->index < i && i < dim(); i++)
01375 if (fabs(v(i))>0) {
01376 VectorElement* r=p->next;
01377 p=p->next=new VectorElement;
01378 p->index=i;
01379 p->value=-v(i);
01380 p->next=r;
01381 }
01382 for (; p->next && i<v.dim(); i++) {
01383 if (p->next->index == i) {
01384 p->next->value-=v(i);
01385 p=p->next;
01386 }
01387 else if (fabs(v(i))>0) {
01388 VectorElement* r=p->next;
01389 p=p->next=new VectorElement;
01390 p->index=i;
01391 p->value=-v(i);
01392 p->next=r;
01393 }
01394 }
01395 for (; i<v.dim(); i++)
01396 if (fabs(v(i))>0) {
01397 p=p->next=new VectorElement;
01398 p->index=i;
01399 p->value=-v(i);
01400 }
01401 p->next=NULL;
01402 last=p;
01403 return *this;
01404 }
01405
01409 SparseVector<Type>& operator-=(const SparseVector<Type>& v) {
01410 assert(dim()==v.dim());
01411 VectorElement* p=head;
01412 VectorElement* q=v.head->next;
01413 while(p->next && q)
01414 if (p->next->index == q->index) {
01415 p->next->value-=q->value;
01416 p=p->next; q=q->next;
01417 }
01418 else
01419 if (p->next->index < q->index) p=p->next;
01420 else {
01421 VectorElement* r=p->next;
01422 p=p->next=new VectorElement;
01423 p->index=q->index;
01424 p->value=-q->value;
01425 p->next=r;
01426 q=q->next;
01427 }
01428 for(; q; q=q->next) {
01429 p=p->next=new VectorElement;
01430 p->index=q->index;
01431 p->value=-q->value;
01432 p->next=NULL;
01433 }
01434 if (!p->next) last=p;
01435 return *this;
01436 }
01437
01438 UserVector<Type>& operator*=(const Type& v) {
01439 if (v==0.) {
01440 clear(head);
01441 last=NULL;
01442 return *this;
01443 }
01444 for (VectorElement* p=head->next; p; p=p->next) p->value*=v;
01445 return *this;
01446 }
01447
01448 UserVector<Type>& operator/=(const Type& v) {
01449 for (VectorElement* p=head->next; p; p=p->next) p->value/=v;
01450 return *this;
01451 }
01452
01453 DenseVector<Type> operator*(const Type& v) const {
01454 DenseVector<Type> ret(dim());
01455 for (VectorElement* p=head->next; p; p=p->next) ret[p->index]=v*p->value;
01456 return ret;
01457 }
01458
01459 bool operator>=(const Type& v) const {
01460 if (v<-rtol && last && last->index<dim()-1) return false;
01461 for (VectorElement* p=head->next; p; p=p->next) if (p->value+rtol<v) return false;
01462 return true;
01463 }
01464
01465 bool operator==(const Type& v) const {
01466 if (fabs(v)>rtol && last && last->index<dim()-1) return false;
01467 for (VectorElement* p=head->next; p; p=p->next)
01468 if (fabs(p->value-v)>rtol) return false;
01469 return true;
01470 }
01471
01472 bool operator==(const UserVector<Type>& v) const {
01473 assert(v.dim()==dim());
01474 VectorElement* p=head->next;
01475 int i=0;
01476 if (p)
01477 while (p->index>i && i<dim())
01478 if (fabs(v(i++))>rtol) return false;
01479 for (; p && i<dim(); i++)
01480 if (p->index==i)
01481 if (fabs(p->value-v(i))>=rtol) return false;
01482 else p=p->next;
01483 else
01484 if (fabs(v(i))>=rtol) return false;
01485 while (i<dim()) if (fabs(v(i++))>=rtol) return false;
01486 return true;
01487 }
01488
01489 Type operator*(const UserVector<Type>& v) const {
01490 assert(v.dim()==dim());
01491 Type val=0;
01492 for (VectorElement* p=head->next; p; p=p->next)
01493 val+=p->value*v(p->index);
01494 return val;
01495 }
01496
01501 Type operator*(const SparseVector<Type>& v) const {
01502 assert(v.dim()==dim());
01503 Type val=0;
01504 for (VectorElement* p=head->next, *q=v.head; p && q; )
01505 if (p->index==q->index) {
01506 val+=p->value * q->value;
01507 p=p->next; q=q->next;
01508 }
01509 else if (p->index < q->index) p=p->next;
01510 else q=q->next;
01511 return val;
01512 }
01513
01519 friend SparseVector<Type> operator*(const Type& v1, const SparseVector<Type>& v2) {
01520 SparseVector<Type> ret(v2);
01521 for (VectorElement* p=ret.head->next; p; p=p->next)
01522 p->value*=v1;
01523 return ret;
01524 }
01525
01530 SparseVector<Type> operator+(const SparseVector<Type>& v) const {
01531 assign(v.dim()==dim());
01532 VectorElement* p1=head->next;
01533 VectorElement* p2=v.head->next;
01534 SparseVector<Type> ret(dim());
01535 ret.last=ret.head;
01536 while(p1 && p2) {
01537 ret.last=ret.last->next=new VectorElement;
01538 if (p1->index==p2->index) {
01539 ret.last->index=p1->index;
01540 ret.last->value=p1->value+p2->value;
01541 p1=p1->next; p2=p2->next;
01542 }
01543 else if (p1->index<p2->index) {
01544 ret.last->index=p1->index;
01545 ret.last->value=p1->value;
01546 p1=p1->next;
01547 }
01548 else {
01549 ret.last->index=p2->index;
01550 ret.last->value=p2->value;
01551 p2=p2->value;
01552 }
01553 }
01554 for (p1+=p2; p1; p1=p1->next) {
01555 ret.last=ret.last->next=new VectorElement;
01556 ret.last->index=p1->index;
01557 ret.last->value=p1->value;
01558 }
01559 return ret;
01560 }
01561 using UserVector<Type>::operator+;
01562
01567 SparseVector<Type> operator-(const SparseVector<Type>& v) const {
01568 assign(v.dim()==dim());
01569 VectorElement* p1=head->next;
01570 VectorElement* p2=v.head->next;
01571 SparseVector<Type> ret(dim());
01572 if (p1 || p2) ret.last=ret.head;
01573 else return ret;
01574 while(p1 && p2) {
01575 ret.last=ret.last->next=new VectorElement;
01576 if (p1->index==p2->index) {
01577 ret.last->index=p1->index;
01578 ret.last->value=p1->value-p2->value;
01579 p1=p1->next; p2=p2->next;
01580 }
01581 else if (p1->index<p2->index) {
01582 ret.last->index=p1->index;
01583 ret.last->value=p1->value;
01584 p1=p1->next;
01585 }
01586 else {
01587 ret.last->index=p2->index;
01588 ret.last->value=-p2->value;
01589 p2=p2->value;
01590 }
01591 }
01592 for (; p1; p1=p1->next) {
01593 ret.last=ret.last->next=new VectorElement;
01594 ret.last->index=p1->index;
01595 ret.last->value=p1->value;
01596 }
01597 for (; p2; p2=p2->next) {
01598 ret.last=ret.last->next=new VectorElement;
01599 ret.last->index=p2->index;
01600 ret.last->value=-p2->value;
01601 }
01602 return ret;
01603 }
01604 using UserVector<Type>::operator-;
01605
01610 void diagmult(SparseVector<Type> y, const SparseVector<Type>& v) const {
01611 assert(dim()==v.dim());
01612 assert(dim()==y.dim());
01613 VectorElement* p1=head->next;
01614 VectorElement* p2=v.head->next;
01615 y=0;
01616 if (p1 && p2) y.last=y.head;
01617 else return;
01618 while (p1 && p2)
01619 if (p1->index==p2->index) {
01620 y.last=y.last->next=new VectorElement;
01621 y.last->index=p1->index;
01622 y.last->value=p1->value * p2->value;
01623 p1=p1->next; p2=p2->next;
01624 }
01625 else if (p1->index < p2->index)
01626 p1=p1->next;
01627 else p2=p2->next;
01628 }
01629
01630 void diagmult(UserVector<Type>& y, const UserVector<Type>& v) const {
01631 assert(dim()==v.dim());
01632 assert(dim()==y.dim());
01633 y=0;
01634 for (VectorElement* p=head->next; p; p=p->next)
01635 y[p->index]=p->value * v(p->index);
01636 }
01637 using UserVector<Type>::diagmult;
01638
01639 void set_block(const UserVector<Type>& v, const UserVector<int>& block) {
01640 for (int i=0; i<block.size(); i++) SetElement(block(i), v(i));
01641 }
01642
01643 operator const Pointer<Type>() const {
01644 Pointer<Type> ret(new Type[dim()]);
01645 VectorElement* p=head->next;
01646 for (int i=0; i<dim(); i++)
01647 if (p && p->index==i) {
01648 ret[i]=p->value;
01649 p=p->next;
01650 } else ret[i]=Type();
01651 return ret;
01652 }
01653
01654 Type sq_norm2() const {
01655 Type val=0;
01656 for (VectorElement* p=head->next; p; p=p->next) val+=p->value*p->value;
01657 return val;
01658 }
01659
01660 void print(ostream& out) const {
01661 for (VectorElement* p=head->next; p; p=p->next) out << p->index << ": " << p->value << " ";
01662 out << endl;
01663 }
01664
01665 };
01666
01667
01668 #ifdef FILIB_AVAILABLE
01669 class IntervalVector : public DenseVector<interval<double> > {
01670 public:
01671 IntervalVector(int n=0, const double& v=0.)
01672 : DenseVector<interval<double> >(n, v)
01673 { }
01674
01675 IntervalVector(const IntervalVector& v)
01676 : DenseVector<interval<double> >(v)
01677 { }
01678
01679 IntervalVector(const UserVector<double>& v)
01680 : DenseVector<interval<double> >(v.dim())
01681 { for (int i=0; i<dim(); i++) x[i]=v(i);
01682 }
01683
01684 IntervalVector(const UserVector<double>& v1, const UserVector<double>& v2)
01685 : DenseVector<interval<double> >(v1.dim())
01686 { for (int i=0; i<dim(); i++) x[i]=interval<double>(v1(i), v2(i));
01687 }
01688
01689 IntervalVector(const IntervalVector& v, const int low, const int up)
01690 : DenseVector<interval<double> >(v, low, up)
01691 { }
01692
01693 IntervalVector(const IntervalVector& v, const UserVector<int>& block)
01694 : DenseVector<interval<double> >(v, block)
01695 { }
01696
01697 IntervalVector(const vector<IntervalVector>& v, const vector<DenseVector<int> >& block)
01698 { int s=0; for (int i=0; i<block.size(); i++) s+=block[i].size();
01699 resize(s);
01700 for (int k=0; k<block.size(); k++) set_block(v[k], block[k]);
01701 }
01702
01703 IntervalVector& operator=(const IntervalVector& v) {
01704 DenseVector<interval<double> >::operator=(v);
01705 return *this;
01706 }
01707 using DenseVector<interval<double> >::operator=;
01708
01709 interval<double> operator*(const UserVector<double>& v) const {
01710 assert(v.dim()==dim());
01711 interval<double> val;
01712 for (int i=0; i<dim(); i++) val+=(*this)(i)*v(i);
01713 return val;
01714 }
01715
01716 interval<double> operator*(const SparseVector<double>& v) const {
01717 assert(v.dim()==dim());
01718 interval<double> val;
01719 for (SparseVector<double>::VectorElement* p=v.head->next; p; p=p->next)
01720 val+=(*this)(p->index)*p->value;
01721 return val;
01722 }
01723 using DenseVector<interval<double> >::operator*;
01724
01725 void AddMult(const double& a, const UserVector<double>& v) {
01726 for (int i=0; i<v.dim(); i++) (*this)[i]+=a*v(i);
01727 }
01728
01729 void AddMult(const double& a, const SparseVector<double>& v) {
01730 for (SparseVector<double>::VectorElement* p=v.head->next; p; p=p->next)
01731 (*this)[p->index]+=a*p->value;
01732 }
01733 using DenseVector<interval<double> >::AddMult;
01734
01735 IntervalVector diagmult(const UserVector<double>& v) const {
01736 IntervalVector ret(*this);
01737 for (int i=0; i<dim(); i++) ret[i]*=v(i);
01738 return ret;
01739 }
01740 using DenseVector<interval<double> >::diagmult;
01741 };
01742 #endif
01743
01746 class Project {
01747 public:
01755 static dvector project(const dvector& x, const dvector& lower, const dvector& upper) {
01756 dvector xp(x.dim());
01757 project(xp, x, lower, upper);
01758 return xp;
01759 }
01760
01768 static void project(dvector& xp, const dvector& x, const dvector& lower, const dvector& upper) {
01769 for (int j=0; j<x.dim(); j++)
01770 if (x(j) < lower(j)) xp[j]=lower(j);
01771 else if (x(j) > upper(j)) xp[j]=upper(j);
01772 else xp[j]=x(j);
01773 }
01774
01775 };
01776
01779 class Round {
01780 public:
01788 static dvector round(const dvector& x, vector<int>& indices, const dvector& lower, const dvector& upper) {
01789 dvector xr(x.dim());
01790 round(xr, x, indices, lower, upper);
01791 return xr;
01792 }
01793
01801 static void round(dvector& xr, const dvector& x, vector<int>& indices, const dvector& lower, const dvector& upper) {
01802 xr=x;
01803 for (vector<int>::iterator i(indices.begin()); i!=indices.end(); i++)
01804 xr[*i] = project(closestint(x(*i)), lower(*i), upper(*i));
01805 }
01806 };
01807
01808
01809 #endif // USERVECTOR_H