00001
00002 #ifndef _MTAY_HH_
00003 #define _MTAY_HH_
00004
00005 #include <iostream>
00006 #include <vector>
00007 #include "dataref"
00008 #include "matrix.hh"
00009 #include "std_except.hh"
00010
00011 class MTayElementIndex;
00012
00013 class MTaylor {
00014 typedef Matrix MType;
00015
00016 friend ostream& operator << (ostream&, const MTaylor&);
00017 friend void uplus(MTaylor&, const MTaylor&);
00018 friend void uminus(MTaylor&, const MTaylor&);
00019 friend void mplus(MTaylor&, const MTaylor&,const MTaylor&);
00020 friend void mminus(MTaylor&, const MTaylor&,const MTaylor&);
00021 friend void times(MTaylor&, const MTaylor&,const MTaylor&);
00022 friend void mtimes(MTaylor&, const MTaylor&,const MTaylor&);
00023 friend void ldivide(MTaylor&, const MTaylor&,const MTaylor&);
00024 friend void mldivide(MTaylor&, const MTaylor&,const MTaylor&);
00025 friend void mldividefac(MTaylor&, const MTaylor&,const MTaylor&,
00026 MTaylor::MType& fac, vector<int>& piv);
00027 friend void rdivide(MTaylor&, const MTaylor&,const MTaylor&);
00028 friend void mrdivide(MTaylor&, const MTaylor&,const MTaylor&);
00029 friend void transpose(MTaylor&, const MTaylor&);
00030 friend void ctranspose(MTaylor&, const MTaylor&);
00031 friend void horzcat(MTaylor&, const vector<MTaylor>&);
00032 friend void vertcat(MTaylor&, const vector<MTaylor>&);
00033 friend void horzcat(MTaylor&, const vector<const MTaylor*>&);
00034 friend void vertcat(MTaylor&, const vector<const MTaylor*>&);
00035 friend void cross(MTaylor&, const MTaylor&,const MTaylor&);
00036 friend void sincos(MTaylor&, MTaylor&, const MTaylor&);
00037 friend void mpower(MTaylor&, const MTaylor&, double);
00038 friend void sumall(MTaylor&, const MTaylor&);
00039 friend void exp(MTaylor&, const MTaylor&);
00040 friend void log(MTaylor&, const MTaylor&);
00041
00042 friend void subsref(MTaylor&, const MTaylor&, const MTaylor::MType&);
00043 friend void subsref(MTaylor&, const MTaylor&, const MTaylor::MType&,
00044 const MTaylor::MType&);
00045 friend void subsasgn(MTaylor&, const MTaylor&, const MTaylor::MType&,
00046 const MTaylor&);
00047 friend void subsasgn(MTaylor&, const MTaylor&, const MTaylor::MType&,
00048 const MTaylor::MType&,const MTaylor&);
00049
00050 friend MTaylor::MType size(const MTaylor&);
00051 friend int size(const MTaylor&,int);
00052 friend int length(const MTaylor&);
00053 friend bool isScalar(const MTaylor&);
00054
00055 friend MTaylor uplus(const MTaylor&);
00056 friend MTaylor uminus(const MTaylor&);
00057 friend MTaylor mplus(const MTaylor&,const MTaylor&);
00058 friend MTaylor mminus(const MTaylor&,const MTaylor&);
00059 friend MTaylor times(const MTaylor&,const MTaylor&);
00060 friend MTaylor mtimes(const MTaylor&,const MTaylor&);
00061 friend MTaylor ldivide(const MTaylor&,const MTaylor&);
00062 friend MTaylor mldivide(const MTaylor&,const MTaylor&);
00063 friend MTaylor mldividefac(const MTaylor&,const MTaylor&,
00064 MTaylor::MType& fac, vector<int>& piv);
00065 friend MTaylor rdivide(const MTaylor&,const MTaylor&);
00066 friend MTaylor mrdivide(const MTaylor&,const MTaylor&);
00067 friend MTaylor transpose(const MTaylor&);
00068 friend MTaylor ctranspose(const MTaylor&);
00069 friend MTaylor horzcat(const vector<MTaylor>&);
00070 friend MTaylor vertcat(const vector<MTaylor>&);
00071 friend MTaylor horzcat(const vector<const MTaylor*>&);
00072 friend MTaylor vertcat(const vector<const MTaylor*>&);
00073 friend MTaylor cross(const MTaylor&,const MTaylor&);
00074 friend MTaylor sin(const MTaylor&);
00075 friend MTaylor cos(const MTaylor&);
00076 friend MTaylor sincos(MTaylor&, const MTaylor&);
00077 friend MTaylor mpower(const MTaylor&, double);
00078 friend MTaylor sumall(const MTaylor&);
00079 friend MTaylor exp(const MTaylor&);
00080 friend MTaylor log(const MTaylor&);
00081
00082 friend MTaylor subsref(const MTaylor&, const MTaylor::MType&);
00083 friend MTaylor subsref(const MTaylor&, const MTaylor::MType&,
00084 const MTaylor::MType&);
00085 friend MTaylor subsasgn(const MTaylor&, const MTaylor::MType&,
00086 const MTaylor&);
00087 friend MTaylor subsasgn(const MTaylor&, const MTaylor::MType&,
00088 const MTaylor::MType&, const MTaylor&);
00089
00090 friend bool operator == (const MTaylor&, const MTaylor&);
00091 friend bool operator != (const MTaylor&, const MTaylor&);
00092 friend bool operator < (const MTaylor&, const MTaylor&);
00093 friend bool operator > (const MTaylor&, const MTaylor&);
00094 friend bool operator <= (const MTaylor&, const MTaylor&);
00095 friend bool operator >= (const MTaylor&, const MTaylor&);
00096
00097 friend class MTayElementIndex;
00098
00099 public:
00100 MTaylor()
00101 : ref(new MTaylorData) {}
00102 MTaylor(const MTaylor& mtay)
00103 : ref(mtay.ref) {}
00104 MTaylor(const MType& val);
00105 MTaylor(MType& val);
00106 MTaylor(const vector<MType>& cv);
00107 MTaylor(double val);
00108 MTaylor(int d, const MType& val);
00109 MTaylor(int rows, int cols, int deg, double *v)
00110 : ref(new MTaylorData(rows, cols, deg, v)) {}
00111 ~MTaylor() {}
00112
00113 MTaylor& operator = (const MTaylor& tay) {
00114 ref = tay.ref; return *this; }
00115
00116 bool IsEmpty() const { return ref->la == 0; }
00117 bool IsScalar() const { return ref->m * ref->n == 1; }
00118 int Degree() const { return ref->d; }
00119 void Reserve(int deg);
00120 void Resize(int deg);
00121 void Clear();
00122 void ExtractData(double*) const;
00123
00124 vector<MType> Taylor2Vector() const;
00125 MTayElementIndex operator [] (int i);
00126 MTayElementIndex operator [] (int i) const;
00127
00128 MTaylor& operator += (const MTaylor& tay) {
00129 *this = mplus(*this,tay); return *this; }
00130 MTaylor& operator -= (const MTaylor& tay) {
00131 *this = mminus(*this,tay); return *this; }
00132
00133 private:
00134
00135 class MTaylorData {
00136 public:
00137 int m, n, d, da, la;
00138 double *re;
00139
00140 MTaylorData()
00141 : m(0), n(0), d(-1), da(-1), la(0), re(NULL) {}
00142 MTaylorData(int rows, int cols, int deg, double *v);
00143 MTaylorData(const MTaylorData& mtay);
00144 MTaylorData& operator = (const MTaylorData& mtay);
00145
00146 ~MTaylorData();
00147 };
00148
00149 DataRef<MTaylorData> ref;
00150
00151 void Realloc(int deg);
00152 };
00153
00154 class MTayElementIndex {
00155 public:
00156 MTayElementIndex(MTaylor *tayptr, int k)
00157 : tay(tayptr), ctay(tayptr), ind(k) {}
00158 MTayElementIndex(const MTaylor *tayptr, int k)
00159 : tay(NULL), ctay(tayptr), ind(k) {}
00160 operator MTaylor::MType() const;
00161 void operator = (const MTaylor::MType& val);
00162 void operator = (const MTayElementIndex&);
00163 private:
00164 MTaylor *tay;
00165 const MTaylor *ctay;
00166 int ind;
00167 };
00168
00169 class MTaylorException : public StandardException {
00170 public:
00171 MTaylorException() : StandardException() {};
00172 MTaylorException(const string& fname, int ln,
00173 const string& msg = string())
00174 : StandardException(fname, ln, head()+msg) {}
00175 const char *head() const {return NULL;};
00176 };
00177
00178 class MTaylorInvalidDimensionException : public StandardException {
00179 public:
00180 MTaylorInvalidDimensionException() : StandardException() {};
00181 MTaylorInvalidDimensionException(const string& fname, int ln,
00182 const string& msg = string())
00183 : StandardException(fname, ln, head()+msg) {}
00184 const char *head() const {return "Cannot allocate mtaylor: Invalid dimension(s).";};
00185 };
00186
00187 class MTaylorEmptyException : public StandardException {
00188 public:
00189 MTaylorEmptyException() : StandardException() {};
00190 MTaylorEmptyException(const string& fname, int ln,
00191 const string& msg = string())
00192 : StandardException(fname, ln, head()+msg) {}
00193 const char *head() const {return "Illegal operation. MTaylor is empty.";};
00194 };
00195
00196 class MTaylorRowDimException : public StandardException {
00197 public:
00198 MTaylorRowDimException() : StandardException() {};
00199 MTaylorRowDimException(const string& fname, int ln,
00200 const string& msg = string())
00201 : StandardException(fname, ln, head()+msg) {}
00202 const char *head() const {return "MTaylor row dimension mismatch";};
00203 };
00204
00205 class MTaylorColDimException : public StandardException {
00206 public:
00207 MTaylorColDimException() : StandardException() {};
00208 MTaylorColDimException(const string& fname, int ln,
00209 const string& msg = string())
00210 : StandardException(fname, ln, head()+msg) {}
00211 const char *head() const {return "MTaylor column dimension mismatch";};
00212 };
00213
00214 inline MTayElementIndex
00215 MTaylor::operator [] (int i) {
00216 return MTayElementIndex(this,i);
00217 }
00218
00219 inline MTayElementIndex
00220 MTaylor::operator [] (int i) const {
00221 return MTayElementIndex(this,i);
00222 }
00223
00224 inline int
00225 length(const MTaylor& a) {
00226 return a.ref->m * a.ref->n;
00227 }
00228
00229 inline bool
00230 isScalar(const MTaylor& a) {
00231 return (a.ref->m * a.ref->n == 1);
00232 }
00233
00234 inline int
00235 size(const MTaylor& a, int dim) {
00236 if (dim == 1)
00237 return a.ref->m;
00238 else
00239 return a.ref->n;
00240 }
00241
00242 inline MTaylor
00243 uplus(const MTaylor& a) {
00244 MTaylor c;
00245 uplus(c,a);
00246 return c;
00247 }
00248
00249 inline MTaylor
00250 uminus(const MTaylor& a) {
00251 MTaylor c;
00252 uminus(c,a);
00253 return c;
00254 }
00255
00256 inline MTaylor
00257 mplus(const MTaylor& a, const MTaylor& b) {
00258 MTaylor c;
00259 mplus(c,a,b);
00260 return c;
00261 }
00262
00263 inline MTaylor
00264 mminus(const MTaylor& a, const MTaylor& b) {
00265 MTaylor c;
00266 mminus(c,a,b);
00267 return c;
00268 }
00269
00270 inline MTaylor
00271 times(const MTaylor& a, const MTaylor& b) {
00272 MTaylor c;
00273 times(c,a,b);
00274 return c;
00275 }
00276
00277 inline MTaylor
00278 mtimes(const MTaylor& a, const MTaylor& b) {
00279 MTaylor c;
00280 mtimes(c,a,b);
00281 return c;
00282 }
00283
00284 inline MTaylor
00285 ldivide(const MTaylor& a, const MTaylor& b) {
00286 MTaylor c;
00287 ldivide(c,a,b);
00288 return c;
00289 }
00290
00291 inline MTaylor
00292 mldivide(const MTaylor& a, const MTaylor& b) {
00293 MTaylor c;
00294 mldivide(c,a,b);
00295 return c;
00296 }
00297
00298 inline MTaylor
00299 mldividefac(const MTaylor& a, const MTaylor& b, MTaylor::MType& fac,
00300 vector<int>& piv) {
00301 MTaylor c;
00302 mldividefac(c,a,b,fac,piv);
00303 return c;
00304 }
00305
00306 inline MTaylor
00307 rdivide(const MTaylor& a, const MTaylor& b) {
00308 MTaylor c;
00309 rdivide(c,a,b);
00310 return c;
00311 }
00312
00313 inline MTaylor
00314 mrdivide(const MTaylor& a, const MTaylor& b) {
00315 MTaylor c;
00316 mrdivide(c,a,b);
00317 return c;
00318 }
00319
00320 inline MTaylor
00321 transpose(const MTaylor& a) {
00322 MTaylor c;
00323 transpose(c,a);
00324 return c;
00325 }
00326
00327 inline MTaylor
00328 ctranspose(const MTaylor& a) {
00329 MTaylor c;
00330 ctranspose(c,a);
00331 return c;
00332 }
00333
00334 inline MTaylor
00335 horzcat(const vector<MTaylor>& av) {
00336 MTaylor c;
00337 horzcat(c,av);
00338 return c;
00339 }
00340
00341 inline MTaylor
00342 vertcat(const vector<MTaylor>& av) {
00343 MTaylor c;
00344 vertcat(c,av);
00345 return c;
00346 }
00347
00348 inline MTaylor
00349 horzcat(const vector<const MTaylor*>& av) {
00350 MTaylor c;
00351 horzcat(c,av);
00352 return c;
00353 }
00354
00355 inline MTaylor
00356 vertcat(const vector<const MTaylor*>& av) {
00357 MTaylor c;
00358 vertcat(c,av);
00359 return c;
00360 }
00361
00362 inline MTaylor
00363 cross(const MTaylor& a, const MTaylor& b) {
00364 MTaylor c;
00365 cross(c,a,b);
00366 return c;
00367 }
00368
00369 inline MTaylor
00370 sin(const MTaylor& a) {
00371 MTaylor s,c;
00372 sincos(s,c,a);
00373 return s;
00374 }
00375
00376 inline MTaylor
00377 cos(const MTaylor& a) {
00378 MTaylor s,c;
00379 sincos(s,c,a);
00380 return c;
00381 }
00382
00383 inline MTaylor
00384 sincos(MTaylor& c, const MTaylor& a) {
00385 MTaylor s;
00386 sincos(s,c,a);
00387 return s;
00388 }
00389
00390 inline MTaylor
00391 mpower(const MTaylor& a, double b) {
00392 MTaylor c;
00393 mpower(c,a,b);
00394 return c;
00395 }
00396
00397 inline MTaylor
00398 sumall(const MTaylor& a) {
00399 MTaylor c;
00400 sumall(c,a);
00401 return c;
00402 }
00403
00404 inline MTaylor
00405 exp(const MTaylor& a) {
00406 MTaylor c;
00407 exp(c,a);
00408 return c;
00409 }
00410
00411 inline MTaylor
00412 log(const MTaylor& a) {
00413 MTaylor c;
00414 log(c,a);
00415 return c;
00416 }
00417
00418 inline MTaylor
00419 subsref(const MTaylor& a, const MTaylor::MType& ind) {
00420 MTaylor c;
00421 subsref(c,a,ind);
00422 return c;
00423 }
00424
00425 inline MTaylor
00426 subsref(const MTaylor& a, const MTaylor::MType& ind,
00427 const MTaylor::MType& jnd) {
00428 MTaylor c;
00429 subsref(c,a,ind,jnd);
00430 return c;
00431 }
00432
00433 inline MTaylor
00434 subsasgn(const MTaylor& a, const MTaylor::MType& ind, const MTaylor& b) {
00435 MTaylor c;
00436 subsasgn(c,a,ind,b);
00437 return c;
00438 }
00439
00440 inline MTaylor
00441 subsasgn(const MTaylor& a, const MTaylor::MType& ind,
00442 const MTaylor::MType& jnd, const MTaylor& b) {
00443 MTaylor c;
00444 subsasgn(c,a,ind,jnd,b);
00445 return c;
00446 }
00447
00448 inline MTaylor
00449 operator + (const MTaylor& a) {
00450 return uplus(a);
00451 }
00452
00453 inline MTaylor
00454 operator - (const MTaylor& a) {
00455 return uminus(a);
00456 }
00457
00458 inline MTaylor
00459 operator + (const MTaylor& a, const MTaylor& b) {
00460 return mplus(a,b);
00461 }
00462
00463 inline MTaylor
00464 operator - (const MTaylor& a, const MTaylor& b) {
00465 return mminus(a,b);
00466 }
00467
00468 inline MTaylor
00469 operator * (const MTaylor& a, const MTaylor& b) {
00470 return mtimes(a,b);
00471 }
00472
00473 inline MTaylor
00474 operator / (const MTaylor& a, const MTaylor& b) {
00475 return mrdivide(a,b);
00476 }
00477
00478 inline bool
00479 operator != (const MTaylor& a, const MTaylor& b) {
00480 return ! (a == b);
00481 }
00482
00483 inline bool
00484 operator <= (const MTaylor& a, const MTaylor& b) {
00485 return ! (a > b);
00486 }
00487
00488 inline bool
00489 operator >= (const MTaylor& a, const MTaylor& b) {
00490 return ! (a < b);
00491 }
00492
00493 #endif