00001
00002
00003
00004
00005
00006
00007 #ifndef CALC_MTX_H
00008 #define CALC_MTX_H
00009
00010
00011
00012
00013
00014 #include <iomanip>
00015
00016 #include "Base.h"
00017
00018
00019
00020
00021
00022 namespace calc {
00023
00024
00025
00026
00032
00033
00044 class MtxErr:public Err {
00045
00046 public:
00047
00048
00049
00051 enum ERR_ENUM {
00052 INIT=0,
00053 MEMORY,
00054 SINGULAR,
00055 ERR_CNT
00056 };
00057
00058
00059
00066 MtxErr(enum MtxErr::ERR_ENUM indErr):
00067 Err() {
00068 const char* const CLASS_NAME="Mtx";
00069 const char* const ERR_STR[ERR_CNT]={
00070 "object initialization error",
00071 "memory allocation error",
00072 "matrix is singular",
00073 };
00074 setError(indErr,CLASS_NAME,ERR_STR[indErr]);
00075 }
00076
00077
00078
00079 private:
00080
00081 static const char* const CLASS_NAME;
00082 static const char* const ERR_STR[MtxErr::ERR_CNT];
00083 };
00084
00085
00086
00087
00088
00090
00100 template <typename aType>
00101 class Mtx:public Base {
00102
00103 public:
00104
00105
00106
00108 enum VTYPE {
00109 ROW_DATA=0,
00110 COLUMN_DATA,
00111 TOTAL_DATA,
00112 VTYPE_CNT
00113 };
00114
00115
00116
00117 Mtx(const aType *array, int nRows, int nCols);
00118 Mtx(aType var);
00119 Mtx(const Mtx<aType>& mt);
00120
00124 Mtx(void):Base() {varInit();}
00128 ~Mtx(void) {if(aMtx) deleteMem(aMtx);}
00129
00130
00131
00132 aType high (void) const;
00133 aType low (void) const;
00134 aType average (void) const;
00135 aType absolute (void) const;
00136 aType determinant (void) const throw (MtxErr);
00137 aType *copyArray (aType *array) const;
00138 Mtx<aType> operator! (void) const throw (MtxErr);
00139 Mtx<aType> operator~ (void) const;
00140 Mtx<aType> operator* (const Mtx<aType>& mt) const;
00141 const Mtx<aType> operator= (const Mtx<aType>& mt);
00142 const Mtx<aType> operator= (aType x);
00143 bool operator== (const Mtx<aType>& mt) const;
00144 bool operator!= (const Mtx<aType>& mt) const;
00145
00146
00147
00161 int getCount(enum Mtx<aType>::VTYPE vType) const
00162 {return ((!nogo)&&(vType>=0)&&(vType<VTYPE_CNT)?nCount[vType]:-1);}
00163
00164
00165
00184 operator const aType* (void) const {return (nogo?(aType*)(0):aMtx[0]);}
00204 operator const aType* const* (void) const {return (nogo?(aType**)(0):aMtx);}
00205
00206
00207
00218 Mtx<aType> operator+(void) const {return Mtx<aType>(*this);}
00233 Mtx<aType> operator+(const Mtx<aType>& mt) const {return sumMtx(mt,1);}
00248 Mtx<aType> operator+=(const Mtx<aType>& mt) {return sumEqMtx(mt,1);}
00249
00250
00251
00262 Mtx<aType> operator-(void) const {return prdMtx((aType)(-1));}
00277 Mtx<aType> operator-(const Mtx<aType>& mt) const {return sumMtx(mt,-1);}
00292 const Mtx<aType> operator-=(const Mtx<aType>& mt) {return sumEqMtx(mt,-1);}
00293
00294
00295
00309 Mtx<aType> operator*(aType scalar) const
00310 {return prdMtx(scalar);}
00324 Mtx<aType> operator/(aType scalar) const
00325 {return prdMtx((aType)(1)/scalar);}
00338 const Mtx<aType> operator*=(aType scalar) {return prdEqMtx(scalar);}
00351 const Mtx<aType> operator/=(aType scalar) {return prdEqMtx((aType)(1)/scalar);}
00352
00353
00354
00368 template <typename bType>
00369 friend std::ostream& operator<<(std::ostream& ostr, const Mtx<bType>& mt)
00370 {return mt.outMtx(ostr);}
00371
00372
00373
00388 template <typename bType>
00389 friend Mtx<bType> operator*(bType scalar, const Mtx<bType>& mt)
00390 {return mt.prdMtx(scalar);}
00402 template <typename bType>
00403 friend bType fabs(const Mtx<bType>& mt)
00404 {return mt.absolute();}
00405
00406 static const int DEF_PRINT_WIDTH;
00407
00408 private:
00409
00410
00411
00412 virtual void varInit (void);
00413
00414 aType **newMem (int nRows, int nCols) const;
00415 void deleteMem (aType **ar) const;
00416 bool adjustMtx (int nRows, int nCols);
00417 std::ostream& outMtx (std::ostream& ostr) const;
00418 Mtx<aType> sumMtx (const Mtx<aType>& mt, int sign) const;
00419 Mtx<aType> prdMtx (aType scalar) const;
00420
00421 const Mtx<aType> sumEqMtx (const Mtx<aType>& mt, int sign);
00422 const Mtx<aType> prdEqMtx (aType scalar);
00423
00434 bool varAlloc(int nRows, int nCols)
00435 {return (!(aMtx=newMem(nRows,nCols)));}
00436
00437
00438
00439 aType **aMtx;
00440 int nCount[VTYPE_CNT];
00441 };}
00442
00443
00444
00446 template <typename aType>
00447 const int calc::Mtx<aType>::DEF_PRINT_WIDTH=14;
00448
00449
00450
00451
00452
00453 #endif
00454
00455
00456
00457
00458
00459 #include "Mtx.cpp"
00460
00461