diff --git a/mat.h b/mat.h index 8a86dc0..47136d6 100644 --- a/mat.h +++ b/mat.h @@ -267,7 +267,7 @@ inline T & NRMat::operator()(const int i, const int j) { #ifdef DEBUG if (*count != 1) laerror("Mat lval use of (,) with count > 1"); - if (i<0 || i>=nn || j<0 || j>mm) laerror("Mat (,) out of range"); + if (i<0 || i>=nn || j<0 || j>=mm) laerror("Mat (,) out of range"); if (!v) laerror("(,) for unallocated Mat"); #endif #ifdef MATPTR @@ -280,7 +280,7 @@ template inline const T & NRMat::operator()(const int i, const int j) const { #ifdef DEBUG - if (i<0 || i>=nn || j<0 || j>mm) laerror("Mat (,) out of range"); + if (i<0 || i>=nn || j<0 || j>=mm) laerror("Mat (,) out of range"); if (!v) laerror("(,) for unallocated Mat"); #endif #ifdef MATPTR @@ -547,13 +547,51 @@ return r; - - - // I/O template extern ostream& operator<<(ostream &s, const NRMat &x); template extern istream& operator>>(istream &s, NRMat &x); +//optional indexing from 1 +//all possible constructors have to be given explicitly, other stuff is inherited +//with exception of the operator() which differs +template +class NRMat_from1 : public NRMat { +public: + NRMat_from1(): NRMat() {}; + explicit NRMat_from1(const int n): NRMat(n) {}; + NRMat_from1(const NRMat &rhs): NRMat(rhs) {}; //be able to convert the parent class transparently to this + NRMat_from1(const T &a, const int n, const int m): NRMat(a,n,m) {}; + NRMat_from1(const T *a, const int n, const int m): NRMat(a,n,m) {}; + + inline const T& operator() (const int i, const int j) const + { +#ifdef DEBUG + if (i<1 || i>NRMat::nn || j<1 || j>NRMat::mm) laerror("Mat (,) out of range"); + if (!NRMat::v) laerror("(,) for unallocated Mat"); +#endif +#ifdef MATPTR + return NRMat::v[i-1][j-1]; +#else + return NRMat::v[(i-1)*NRMat::mm+j-1]; +#endif + } + inline T& operator() (const int i, const int j) + { +#ifdef DEBUG + if (*NRMat::count != 1) laerror("Mat lval use of (,) with count > 1"); + if (i<1 || i>NRMat::nn || j<1 || j>NRMat::mm) laerror("Mat (,) out of range"); + if (!NRMat::v) laerror("(,) for unallocated Mat"); +#endif +#ifdef MATPTR + return NRMat::v[i-1][j-1]; +#else + return NRMat::v[(i-1)*NRMat::mm+j-1]; +#endif + } +}; + + +