00001 #pragma ident "$Id: ConstraintSystem.cpp 2939 2011-10-23 19:55:11Z yanweignss $"
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "ConstraintSystem.hpp"
00031 #include <vector>
00032
00033 namespace gpstk
00034 {
00035
00036 ConstraintSystem& ConstraintSystem::removeConstraint(
00037 const Constraint& constraint )
00038 {
00039 ConstraintList backupList;
00040
00041 for(ConstraintList::iterator it= constraintList.begin();
00042 it!=constraintList.end();
00043 ++it)
00044 {
00045 bool isEqual(true);
00046
00047 if(it->header.prefit!=constraint.header.prefit) isEqual=false;
00048 if(it->header.variance!=constraint.header.variance) isEqual=false;
00049 if(it->body != constraint.body) isEqual = false;
00050
00051 if( !isEqual ) backupList.push_back(*it);
00052 }
00053
00054 clearConstraint();
00055
00056 for(ConstraintList::iterator it= backupList.begin();
00057 it!=backupList.end();
00058 ++it)
00059 {
00060 addConstraint(*it);
00061 }
00062
00063 return (*this);
00064
00065 }
00066
00067
00068
00069 ConstraintSystem& ConstraintSystem::setConstraint(const VariableSet& varSet,
00070 const Vector<double>& prefit)
00071 {
00072
00073 const int size = varSet.size();
00074
00075 if( prefit.size()!=size )
00076 {
00077 Exception e("The input size doesn't match.");
00078 GPSTK_THROW(e);
00079 }
00080
00081 clearConstraint();
00082
00083 int i(0);
00084
00085 for(VariableSet::const_iterator it = varSet.begin();
00086 it != varSet.end();
00087 ++it)
00088 {
00089 Constraint constraint;
00090 constraint.header.prefit = prefit[i];
00091 constraint.body[*it] = 1.0;
00092
00093 addConstraint(constraint);
00094
00095 i++;
00096 }
00097
00098 return (*this);
00099
00100 }
00101
00102
00103
00104 ConstraintSystem& ConstraintSystem::setConstraint(const VariableSet& varSet,
00105 const Vector<double>& prefit,
00106 const Matrix<double>& design)
00107 {
00108
00109 const int size = varSet.size();
00110
00111 if( (prefit.size()!=size) || design.rows()!=size || design.cols()!=size)
00112 {
00113 Exception e("The input size doesn't match.");
00114 GPSTK_THROW(e);
00115 }
00116
00117 clearConstraint();
00118
00119 std::vector<Variable> vars;
00120 for(VariableSet::const_iterator it = varSet.begin();
00121 it != varSet.end();
00122 ++it)
00123 {
00124 vars.push_back(*it);
00125 }
00126
00127 for(int i=0; i<vars.size(); i++)
00128 {
00129 VariableDataMap dataMap;
00130 for(int k=0;k<size;k++)
00131 {
00132 if(design[i][k]!=0.0) dataMap[ vars[k] ] = design[i][k];
00133 }
00134
00135 Constraint constraint;
00136 constraint.header.prefit = prefit[i];
00137 constraint.body = dataMap;
00138
00139 addConstraint(constraint);
00140 }
00141
00142 return (*this);
00143
00144
00145 }
00146
00147
00148
00149 ConstraintSystem& ConstraintSystem::constraintMatrix(
00150 const VariableSet& allVar,
00151 Vector<double>& prefit,
00152 Matrix<double>& design,
00153 Matrix<double>& covariance )
00154 throw(InvalidConstraintSystem)
00155 {
00156 const int rowSize = constraintList.size();
00157 const int colSize = allVar.size();
00158
00159 prefit.resize(rowSize,0.0);
00160 design.resize(rowSize,colSize,0.0);
00161 covariance.resize(rowSize,rowSize,0.0);
00162
00163 int irow(0);
00164
00165 for(ConstraintList::iterator it=constraintList.begin();
00166 it!=constraintList.end();
00167 ++it)
00168 {
00169 prefit[irow] = it->header.prefit;
00170 covariance[irow][irow] = it->header.variance;
00171
00172 VariableDataMap dataMap = it->body;
00173
00174
00175 for(VariableDataMap::iterator itv=dataMap.begin();
00176 itv!=dataMap.end();
00177 ++itv)
00178 {
00180 VariableSet::const_iterator itt = allVar.find(itv->first);
00181 if(itt==allVar.end())
00182 {
00183 InvalidConstraintSystem e("The variable not exist in the input");
00184 GPSTK_THROW(e);
00185 }
00186
00187 int icol(0);
00188 for(VariableSet::const_iterator itt2=allVar.begin();
00189 itt2!=allVar.end();
00190 ++itt2)
00191 {
00192 if(itt == itt2) break;
00193 icol++;
00194 }
00195
00196 design[irow][icol] = itv->second;
00197 }
00198
00199
00200 irow++;
00201 }
00202
00203 return (*this);
00204
00205 }
00206
00207
00208
00209 ConstraintSystem& ConstraintSystem::addConstraintList(
00210 const ConstraintList& equationList)
00211 {
00212 for(ConstraintList::const_iterator it = equationList.begin();
00213 it != equationList.end();
00214 ++it)
00215 {
00216 addConstraint(*it);
00217 }
00218
00219 return (*this);
00220 }
00221
00222 }
00223
00224
00225
00226