00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "LOCA_Parameter_Vector.H"
00043
00044 #include "Teuchos_TestForException.hpp"
00045
00046 LOCA::ParameterVector::ParameterVector() :
00047 x(),
00048 l()
00049 {
00050 }
00051
00052 LOCA::ParameterVector::ParameterVector(const LOCA::ParameterVector& source) :
00053 x(source.x),
00054 l(source.l)
00055 {
00056 }
00057
00058 LOCA::ParameterVector*
00059 LOCA::ParameterVector::clone() const
00060 {
00061 LOCA::ParameterVector* y = new LOCA::ParameterVector(*this);
00062 return y;
00063 }
00064
00065 LOCA::ParameterVector::~ParameterVector()
00066 {
00067 }
00068
00069 int
00070 LOCA::ParameterVector::addParameter(string label, double value)
00071 {
00072 unsigned int size = x.size();
00073 x.push_back(value);
00074 l.push_back(label);
00075 return (size);
00076 }
00077
00078 bool
00079 LOCA::ParameterVector::init(double value)
00080 {
00081 for (unsigned int i = 0; i < x.size(); i ++)
00082 x[i] = value;
00083 return true;
00084 }
00085
00086 bool
00087 LOCA::ParameterVector::scale(double value)
00088 {
00089 for (unsigned int i = 0; i < x.size(); i ++)
00090 x[i] *= value;
00091 return true;
00092 }
00093
00094 bool
00095 LOCA::ParameterVector::scale(const LOCA::ParameterVector& p)
00096 {
00097
00098 if (this->x.size() != p.x.size())
00099 return false;
00100
00101 for (unsigned int i = 0; i < x.size(); i ++)
00102 x[i] *= p[i];
00103 return true;
00104 }
00105
00106 bool
00107 LOCA::ParameterVector::update(double alpha,
00108 const LOCA::ParameterVector& alphaVector,
00109 double b)
00110 {
00111 if (x.size() != alphaVector.x.size())
00112 return false;
00113
00114 for (unsigned int i = 0; i < x.size(); i ++) {
00115 x[i] *= b;
00116 x[i] += alpha*alphaVector[i];
00117 }
00118 return true;
00119 }
00120
00121 LOCA::ParameterVector&
00122 LOCA::ParameterVector::operator=(const LOCA::ParameterVector& source)
00123 {
00124 x = source.x;
00125 l = source.l;
00126 return *this;
00127 }
00128
00129 double&
00130 LOCA::ParameterVector::operator[] (unsigned int i)
00131 {
00132 TEST_FOR_EXCEPTION(i >= x.size(),
00133 std::out_of_range,
00134 "Error: LOCA::ParameterVector::operator[]: " <<
00135 " Index " << i << " is out of range!");
00136 return x[i];
00137 }
00138
00139 const double&
00140 LOCA::ParameterVector::operator[] (unsigned int i) const
00141 {
00142 TEST_FOR_EXCEPTION(i >= x.size(),
00143 std::out_of_range,
00144 "Error: LOCA::ParameterVector::operator[]: " <<
00145 " Index " << i << " is out of range!");
00146 return x[i];
00147 }
00148
00149 void
00150 LOCA::ParameterVector::setValue(unsigned int i, double value)
00151 {
00152 TEST_FOR_EXCEPTION(i >= x.size(),
00153 std::out_of_range,
00154 "Error: LOCA::ParameterVector::setValue(): " <<
00155 " Index " << i << " is out of range!");
00156
00157 x[i] = value;
00158 return;
00159 }
00160
00161 void
00162 LOCA::ParameterVector::setValue(string label, double value)
00163 {
00164 for (unsigned int i = 0; i < x.size(); i++) {
00165 if (l[i] == label) {
00166 x[i] = value;
00167 return;
00168 }
00169 }
00170
00171 TEST_FOR_EXCEPTION(true,
00172 std::invalid_argument,
00173 "Error: LOCA::ParameterVector::setValue(): " <<
00174 " Label " << label << " is not valid!");
00175 }
00176
00177 double
00178 LOCA::ParameterVector::getValue(unsigned int i) const
00179 {
00180 TEST_FOR_EXCEPTION(i >= x.size(),
00181 std::out_of_range,
00182 "Error: LOCA::ParameterVector::getValue(): " <<
00183 " Index " << i << " is out of range!");
00184 return x[i];
00185 }
00186
00187 double
00188 LOCA::ParameterVector::getValue(string label) const
00189 {
00190 for (unsigned int i = 0; i < x.size(); i++) {
00191 if (l[i] == label)
00192 return x[i];
00193 }
00194
00195 TEST_FOR_EXCEPTION(true,
00196 std::invalid_argument,
00197 "Error: LOCA::ParameterVector::getValue(): " <<
00198 " Label " << label << " is not valid!");
00199 return 0.0;
00200 }
00201
00202 int
00203 LOCA::ParameterVector::getIndex(string label) const
00204 {
00205 for (unsigned int i = 0; i < x.size(); i++) {
00206 if (l[i] == label)
00207 return i;
00208 }
00209
00210 TEST_FOR_EXCEPTION(true,
00211 std::invalid_argument,
00212 "Error: LOCA::ParameterVector::getIndex(): " <<
00213 " Label " << label << " is not valid!");
00214 return -1;
00215 }
00216
00217 double*
00218 LOCA::ParameterVector::getDoubleArrayPointer()
00219 {
00220 return &x[0];
00221 }
00222
00223 bool
00224 LOCA::ParameterVector::isParameter(string label) const
00225 {
00226 for (unsigned int i = 0; i < x.size(); i++) {
00227 if (l[i] == label)
00228 return true;
00229 }
00230 return false;
00231 }
00232
00233 string
00234 LOCA::ParameterVector::getLabel(unsigned int i) const
00235 {
00236 return l[i];
00237 }
00238
00239 int
00240 LOCA::ParameterVector::length() const
00241 {
00242 return x.size();
00243 }
00244
00245 void
00246 LOCA::ParameterVector::print(ostream& stream) const
00247 {
00248 stream << "LOCA::ParameterVector \n(size = " << x.size() << ")";
00249 for (unsigned int i = 0; i < x.size(); i++) {
00250 stream << "\n " << i << " " << l[i] << " = " << x[i];
00251 }
00252 stream << std::endl;
00253 }
00254
00255 ostream&
00256 operator<<(ostream& stream, const LOCA::ParameterVector& p)
00257 {
00258 p.print(stream);
00259 return stream;
00260 }
00261
00262 const vector<double>&
00263 LOCA::ParameterVector::getValuesVector() const
00264 {
00265 return x;
00266 }
00267
00268 const vector<string>&
00269 LOCA::ParameterVector::getNamesVector() const
00270 {
00271 return l;
00272 }