// ********** DO NOT REMOVE THIS BANNER ********** // ORIG-DATE: 29 fev 2000 // -*- Mode : c++ -*- // // SUMMARY : array modelisation // USAGE : LGPL // ORG : LJLL Universite Pierre et Marie Curie, Paris, FRANCE // AUTHOR : Frederic Hecht // E-MAIL : frederic.hecht@ann.jussieu.fr // /* Freefem++ is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. Freefem++ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Freefem++; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef KNM_H_ #define KNM_H_ // version sept 2008 FH. // ---------------------- // une tentative qui ne marche pas // de tableau constant #include #include #include #include #include #include using namespace std; #define const_R R #include inline void Check_Kn(const char * str,const char * file,int line) { cerr << "CHECK_KN: " << str << " in file: " << file << ", line " << line < // version du 21 nov 1999 correction delete // version du 13 nov 1999 // version du 18 mars 99 // F. Hecht // attention les indexations les indexations peuvent changer // puisque que l'on peut prendre la transposer d'une matrice // tableau // mais ils partent de 0 // version corrigee du 15/11/98 // version avec sous tableau --- mars 99 // ------- // remarque du 8 mars 99 FH // class pour prendre des sous-tableau // attention aux PB de continute dans les tableaux // on a supposer que les tableaux multi indices pouvait est vue comme // un tableau continue ce qui est generalement faux quand l'on en // prend un sous tableau // exemple: un tableau 3,5 est numerote comme: // 0 3 6 9 12 // 1 4 7 10 13 // 2 5 8 11 14 // step // indexi n 1 // indexj m n // est le sous tableau 3,3 n'est pas numeroter consecutivement // // Donc la fonction IsVector1() nous dit si un tableau // a un 2 ou 3 indices est ou non consecutif en memoire // // -- ajoute d'une classe RNM_VirtualMatrix // pour modeliser le produit matrice vecteur // x = A*v; via des fonctions virtuelle // ---------------------------------- // version du 6 mars 2001 FH // --- initialisation -- // -------------------------------- // version du 9 oct 2001 FH // ajoute de constructeur par defaut d'une vecteur // + set , pour definir le vecteur // ou l'affectation (bof bof) // --------------------- // version sep 2002 // ajoute operateur >> pour KN et KN_ // -------------------- // version april 2003 // ajoute un gestion auto de // la fonction InternalError pour les matriceVirtuel // -------------------- // version jan 2004 // correction pour go ++ // des operateur #= pour les matrices et tenseurs // ---------------------- // version feb 2004 // v(i(:)) = w // i(1:10) // w=u(i(:)) // // version mars 2004 make small correction // in ITAB operator problem if non type R a defi // ------------------- // Modif pour version avec les Complex mai 2004 // (u,v) donne le produit complex utiliser dans le produit matrice vecteur // (u,conj(v)) donne le produit hermitiene pour le gradient conjugue // // -- de fonction dans le cas real // modif for g++ 4.0 and xlc++ mai 2005 // adding some this-> // mars 2007 // correction in operator operation:b -1*c // aout 2007, // correct y = A*x ; when y is unset // correct y += A*x ; when y is unset // re-correct += sep 2007 // add size of the matrix in VirtualMatrix class. // mars 2010 add unset KNM case ... // sept 2014 add 1/v operator ... // ---------------- namespace RNM { template T Square(const T & r){return r*r;} inline double conj(const double & x){return x;} inline float conj(const float &x){return x;} inline long conj(const long &x){return x;} template inline complex conj(const complex& v){ return std::conj(v);} template inline T Min (const T &a,const T &b){return a < b ? a : b;} template inline T Max (const T &a,const T & b){return a > b ? a : b;} template inline T Abs (const T &a){return a <0 ? -a : a;} template inline void Exchange (T& a,T& b) {T c=a;a=b;b=c;} template inline T Max (const T &a,const T & b,const T & c){return Max(Max(a,b),c);} template inline T Min (const T &a,const T & b,const T & c){return Min(Min(a,b),c);} // specialisation cas complex --- template inline complex Min(const complex &a,complex &b) { return complex(min(a.real(),b.real()),min(a.imag(),b.imag()));} template inline complex Max(const complex &a,const complex &b) { return complex(max(a.real(),b.real()),max(a.imag(),b.imag()));} /*inline complex Min(const complex &a,complex &b) { return complex(Min(real(a),real(b)),Min(imag(a),imag(b)));} inline complex Max(const complex &a,const complex &b) { return complex(Max(real(a),real(b)),Max(imag(a),imag(b)));} */ } // ---- template class KNMK_ ; template class KNM_ ; template class KN_ ; template struct TKN_ ; // KN_ Hermitain template struct ConjKNM_ ;// take the conj of the matrix. template struct notKN_ ; // KN_ not template struct notnotKN_ ; // KN_ not not template class KNMK ; template class KNM ; template class KN ; template class conj_KN_ ; template class Add_KN_; template class Sub_KN_; template class Mulc_KN_; // vector b*a_i template class Divc_KN_;// vector b/a_i template class Add_Mulc_KN_; template class Mul_KNM_KN_; template class Mul_KNMh_KN_; template class DotStar_KN_; template class DotSlash_KN_; template struct outProduct_KN_; template struct if_KN_; template class if_arth_KN_; template struct ifnot_KN_; template struct KN_ITAB; template class F_KN_; #ifndef ffassert #define ffassert assert #endif // gestion des erreur interne -- #ifndef InternalError typedef void (* TypeofInternalErrorRoutine)(const char *) ; static TypeofInternalErrorRoutine &InternalErrorRoutinePtr() { static TypeofInternalErrorRoutine routine=nullptr; return routine; } static void InternalError(const char * str) { if (InternalErrorRoutinePtr() ) (*InternalErrorRoutinePtr())(str); cerr << str; exit(1); } inline void SetInternalErrorRoutine(TypeofInternalErrorRoutine f) { InternalErrorRoutinePtr()=f; } #endif // -- template struct PplusQ { const P & p;const Q & q; PplusQ(const P & pp,const Q & qq) : p(pp),q(qq){} }; template struct RNM_VirtualMatrix { public: Z N,M; RNM_VirtualMatrix(Z nn): N(nn),M(nn) {} RNM_VirtualMatrix(Z nn,Z mm): N(nn),M(mm<0 ? nn: mm ) {} // y += A x virtual void addMatMul(const KN_ & x, KN_ & y) const =0; virtual void addMatTransMul(const KN_ & , KN_ & ) const { InternalError("RNM_VirtualMatrix::addMatTransMul not implemented "); } virtual bool WithSolver() const {return false;} // by default no solver virtual void Solve( KN_ & ,const KN_ & ) const { InternalError("RNM_VirtualMatrix::solve not implemented.\n In FeeeFem++ add instruction like set(A, solver= sparsesolver);\n// where A is the current matrix "); } virtual void SolveT( KN_ & ,const KN_ & ) const { InternalError("RNM_VirtualMatrix::solve trans/herm not implemented.\n In FeeeFem++ add instruction like set(A, solver= sparsesolver);\n// where A is the current matrix "); } #ifdef VersionFreeFem virtual bool ChecknbLine (Z n) const= 0; virtual bool ChecknbColumn (Z m) const =0; #else virtual bool ChecknbLine (Z) const {return true;} virtual bool ChecknbColumn (Z) const {return true;} #endif struct plusAx { const RNM_VirtualMatrix * A; const KN_ x; plusAx( const RNM_VirtualMatrix * B,const KN_ & y) :A(B),x(y) { if(B) { ffassert(B->ChecknbColumn(y.N())); } } }; plusAx operator*(const KN_ & x) const {return plusAx(this,x);} struct plusAtx { const RNM_VirtualMatrix * A; const KN_ x; plusAtx( const RNM_VirtualMatrix * B,const KN_ & y) :A(B),x(y) { if(B) { ffassert(B->ChecknbLine(y.N())); } } }; struct solveAxeqb { const RNM_VirtualMatrix * A; const KN_ b; solveAxeqb( const RNM_VirtualMatrix * B,const KN_ & y) :A(B),b(y) { if(B) {int mu=y.N()/B->M; ffassert(mu>0 && y.N()%mu==0);} } }; struct solveAtxeqb { const RNM_VirtualMatrix * A; const KN_ b; solveAtxeqb( const RNM_VirtualMatrix * B,const KN_ & y) :A(B),b(y) { if(B) { ffassert(B->ChecknbColumn(y.N())); } } }; virtual ~RNM_VirtualMatrix(){} }; //template class MatriceCreuseMulKN_; //template class MatriceCreuseDivKN_; class ShapeOfArray; class FromTo{ public: long from,to; FromTo(long i,long j):from(i),to(j) {K_throwassert(i<=j);} }; class SubArray{ public: const long n,step,start; // SubArray(char nn): n(-1),step(1),start(0) {} explicit SubArray(long nn,long sta=0,long s=1): n(nn),step(s),start(sta) {} SubArray(const FromTo& ft) : n(ft.to-ft.from+1),step(1),start(ft.from) {} SubArray(const ShapeOfArray & ); // all long end() const { return start+ step*n;} long last() const { return start+ step*(n-1);} long len1() const { return step*(n-1);} }; class ShapeOfArray{ protected: public: long n; // n nb of item long step; // step nb of between 2 item long next; // the next array of same type in matrix for subarray // by default no next ( in case of KN, and KNM -next is // a counter of destruction (use in frefem++) ShapeOfArray(const ShapeOfArray & s,long nn): n(s.n),step(s.n),next(nn) {} ShapeOfArray(long nn): n(nn),step(1),next(-1) {} ShapeOfArray(long nn,long s): n(nn),step(s),next(-1) {} ShapeOfArray(long nn,long s,long nextt): n(nn),step(s),next(nextt) {} ShapeOfArray(const ShapeOfArray &old,const SubArray &sub) : n(sub.n),step(old.step*sub.step),next(old.next) { K_throwassert((sub.last())*old.step <= old.last());} // a constructor ShapeOfArray(const ShapeOfArray &old,long stepo,long start) : n(old.n-start),step(old.step*stepo),next(old.next) { K_throwassert(n>=0);} long end() const { return n*step;} long last() const { return (n-1)*step;} long constant() const { return step==0;} bool contiguous() const {return step==1;} long index(long k) const { K_throwassert( (k>=0) && ( (k =0) && ( (k ostream & operator<<(ostream & f,const KN_ & v) ; template istream & operator>>(istream & f, KN_ & v); template istream & operator>>(istream & f, KN & v); template class SetArray { public: R o,step; long n; explicit SetArray(long nn,R oo=R(),R sstep=R(1)): o(oo),n(nn),step(sstep) {} template SetArray(SetArray sa): o(sa.o),n(sa.n),step(sa.step) {} R operator[](long i) const { return i <= n ? o + R(i)*step : R();} long size() const {return n;} }; /// <> template class KN_: public ShapeOfArray { protected: R *v; public: typedef R K; // type of data long N() const {return n;} long M() const {return 1;} bool unset() const { return !v;} void set(R * vv,int nn,int st=1,int nx=-1) {v=vv;n=nn;step=st;next=nx;} void set(KN_ t) {v=t.v;n=t.n;step=t.step;next=t.next;} // FH Add Nov 2016 long size() const{return step?n*step:n;} operator R *() const {return v;} KN_(const KN_ & u) :ShapeOfArray(u),v(u.v){} KN_(const KN_ & U,const SubArray & sa) : ShapeOfArray(U,sa),v(U.v + U.index(sa.start)) {} KN_ operator()(const SubArray & sa) const { return KN_(*this,sa);} // sub array R & operator[](long long i) const {return v[index(i)];} R & operator()(long long i) const {return v[index(i)];} R & operator[](long i) const {return v[index(i)];} R & operator()(long i) const {return v[index(i)];} R & operator[](int i) const {return v[index(i)];} R & operator()(int i) const {return v[index(i)];} R operator,(const KN_ & v) const; // dot product KN_& operator =(const SetArray & u) ; KN_& operator +=(const SetArray & u) ; KN_& operator -=(const SetArray & u) ; KN_& operator *=(const SetArray & u) ; KN_& operator /=(const SetArray & u) ; KN_& operator =(const KN_ & u) ; KN_& operator +=(const KN_ & u) ; KN_& operator -=(const KN_ & u) ; KN_& operator *=(const KN_ & u) ; KN_& operator /=(const KN_ & u) ; KN_& operator = (const_R a) ; KN_& operator +=(const_R a) ; KN_& operator -=(const_R a) ; KN_& operator /=(const_R a) ; KN_& operator *=(const_R a) ; KN_& operator = (R* a) { return operator =(KN_(a,n));} KN_& operator += (R* a) { return operator+=(KN_(a,n));} KN_& operator -= (R* a) { return operator-=(KN_(a,n));} KN_& operator *= (R* a) { return operator*=(KN_(a,n));} KN_& operator /= (R* a) { return operator/=(KN_(a,n));} R min() const ; R max() const ; R sum() const ; double norm() const ; double l2() const ; double l1() const ; double linfty() const ; double lp(double p) const ; template long last(const T &) const; template long first(const T &) const; void map(R (*f)(R )); // apply the f fonction a all element of the array void map(R (*f)(const R& )); // apply the f fonction a all element of the array template void set(R (*f)(const T& ),KN_ & u); // apply the f fonction a all element of the array u KN_& operator =(const DotStar_KN_ & u) ; KN_& operator+=(const DotStar_KN_ & u) ; KN_& operator-=(const DotStar_KN_ & u) ; KN_& operator*=(const DotStar_KN_ & u) ; KN_& operator/=(const DotStar_KN_ & u) ; KN_& operator =(const DotSlash_KN_ & u) ; KN_& operator+=(const DotSlash_KN_ & u) ; KN_& operator-=(const DotSlash_KN_ & u) ; KN_& operator*=(const DotSlash_KN_ & u) ; KN_& operator/=(const DotSlash_KN_ & u) ; KN_& operator =(const if_KN_ & u) ; KN_& operator+=(const if_KN_ & u) ; KN_& operator-=(const if_KN_ & u) ; KN_& operator*=(const if_KN_ & u) ; KN_& operator/=(const if_KN_ & u) ; KN_& operator =(const ifnot_KN_ & u) ; KN_& operator+=(const ifnot_KN_ & u) ; KN_& operator-=(const ifnot_KN_ & u) ; KN_& operator*=(const ifnot_KN_ & u) ; KN_& operator/=(const ifnot_KN_ & u) ; KN_& operator =(const Add_KN_ & u) ; KN_& operator+=(const Add_KN_ & u) ; KN_& operator-=(const Add_KN_ & u) ; KN_& operator*=(const Add_KN_ & u) ; KN_& operator/=(const Add_KN_ & u) ; template KN_& operator = (const KN_ITAB & u); template KN_& operator += (const KN_ITAB & u); template KN_& operator -= (const KN_ITAB & u); template KN_& operator *= (const KN_ITAB & u); template KN_& operator /= (const KN_ITAB & u); KN_ITAB< KN_,const KN_ > operator()(const KN_ &itab) ; KN_ITAB< KN_,const KN_ > operator()(const KN_ &itab) ; KN_ITAB,const KN_ > operator()(const KN_ &itab) const ; KN_ITAB,const KN_ > operator()(const KN_ &itab) const ; KN_& operator =(const Sub_KN_ & u) ; KN_& operator-=(const Sub_KN_ & u) ; KN_& operator+=(const Sub_KN_ & u) ; KN_& operator*=(const Sub_KN_ & u) ; KN_& operator/=(const Sub_KN_ & u) ; KN_& operator =(const Mulc_KN_ & u) ; KN_& operator+=(const Mulc_KN_ & u) ; KN_& operator-=(const Mulc_KN_ & u) ; KN_& operator*=(const Mulc_KN_ & u) ; KN_& operator/=(const Mulc_KN_ & u) ; KN_& operator =(const Divc_KN_ & u) ; KN_& operator+=(const Divc_KN_ & u) ; KN_& operator-=(const Divc_KN_ & u) ; KN_& operator*=(const Divc_KN_ & u) ; KN_& operator/=(const Divc_KN_ & u) ; KN_& operator =(const Add_Mulc_KN_ & u) ; KN_& operator+=(const Add_Mulc_KN_ & u) ; KN_& operator-=(const Add_Mulc_KN_ & u) ; KN_& operator*=(const Add_Mulc_KN_ & u) ; KN_& operator/=(const Add_Mulc_KN_ & u) ; KN_& operator =(const if_arth_KN_ & u) ; KN_& operator+=(const if_arth_KN_ & u) ; KN_& operator-=(const if_arth_KN_ & u) ; KN_& operator*=(const if_arth_KN_ & u) ; KN_& operator/=(const if_arth_KN_ & u) ; KN_& operator =(const Mul_KNM_KN_ & u) ; KN_& operator+=(const Mul_KNM_KN_ & u) ; KN_& operator-=(const Mul_KNM_KN_ & u) ; KN_& operator*=(const Mul_KNM_KN_ & u) ; KN_& operator/=(const Mul_KNM_KN_ & u) ; KN_& operator =(const Mul_KNMh_KN_ & u) ; KN_& operator+=(const Mul_KNMh_KN_ & u) ; KN_& operator-=(const Mul_KNMh_KN_ & u) ; KN_& operator*=(const Mul_KNMh_KN_ & u) ; KN_& operator/=(const Mul_KNMh_KN_ & u) ; // KN_& operator =(const MatriceCreuseMulKN_ & ) ; // KN_& operator +=(const MatriceCreuseMulKN_ & ) ; KN_& operator =(const typename RNM_VirtualMatrix::plusAx & Ax) { if(Ax.A) { ffassert((!this->N() || !this->M()) || !Ax.x.N() || !this->N() || &Ax.x[0] != &this->operator[](0));*this=R(); Ax.A->addMatMul(Ax.x,*this); } return *this;} KN_& operator =(const typename RNM_VirtualMatrix::plusAtx & Ax) { if(Ax.A) { ffassert(!Ax.x.N() || !this->N() || &Ax.x[0] != &this->operator[](0));*this=R(); Ax.A->addMatTransMul(Ax.x,*this); } return *this;} KN_& operator +=(const typename RNM_VirtualMatrix::plusAx & Ax) { if(Ax.A) { ffassert(!Ax.x.N() || !this->N() || &Ax.x[0] != &this->operator[](0)); Ax.A->addMatMul(Ax.x,*this); } return *this;} KN_& operator +=(const typename RNM_VirtualMatrix::plusAtx & Ax) { if(Ax.A) { ffassert(!Ax.x.N() || !this->N() || &Ax.x[0] != &this->operator[](0)); Ax.A->addMatTransMul(Ax.x,*this); } return *this;} KN_& operator =(const typename RNM_VirtualMatrix::solveAxeqb & Ab) { if(Ab.A) { ffassert(&Ab.b[0] != &this->operator[](0));/* *this=R();*/ Ab.A->Solve(*this,Ab.b); } return *this;} KN_& operator =(const typename RNM_VirtualMatrix::solveAtxeqb & Ab) { if(Ab.A) { ffassert(&Ab.b[0] != &this->operator[](0));/* *this=R();*/ Ab.A->SolveT(*this,Ab.b); } return *this;} template KN_& operator = (const F_KN_ & u) ; template KN_& operator += (const F_KN_ & u) ; template KN_& operator -= (const F_KN_ & u) ; template KN_& operator /= (const F_KN_ & u) ; template KN_& operator *= (const F_KN_ & u) ; // KN_& operator =(const MatriceCreuseDivKN_ &) ; friend ostream & operator<< (ostream & f,const KN_ & v) ; KN_(R *u,const ShapeOfArray & s):ShapeOfArray(s),v(u){} KN_(R *u,long nn,long s):ShapeOfArray(nn,s),v(u){} KN_(R *u,long nn,long s,long nextt):ShapeOfArray(nn,s,nextt),v(u){} KN_(R *u,long nn):ShapeOfArray(nn),v(u){} TKN_ t() ; // transpose const TKN_ t() const ; // transpose notKN_ operator!() ; // not const notKN_ operator!() const ; // not // operator KN &(); // operator const KN &() const; private: KN_& operator++(){K_throwassert(next>=0);v += next;return *this;} // ++U KN_& operator--(){K_throwassert(next>=0);v -= next;return *this;} // --U KN_ operator++(int ){K_throwassert(next>=0); KN_ old=*this;v = v +next;return old;} // U++ KN_ operator--(int ){K_throwassert(next>=0); KN_ old=*this;v = v -next;return old;} // U++ KN_(const KN_ & u,long offset) :ShapeOfArray(u),v(&u[offset]){} KN_(const KN_ & u,const ShapeOfArray &sh,long startv=0) :ShapeOfArray(sh*u.step),v(&u[startv]){} KN_(const KN_ & u,long nnext,const ShapeOfArray &sh,long startv=0) :ShapeOfArray(sh.n,sh.step*u.step,nnext),v(&u[startv]){ } // friend class KN_; friend class KNM_; friend class KNMK_; friend class KN; friend class KNM; friend class KNMK; }; template class KNM_: public KN_ { public: ShapeOfArray shapei; ShapeOfArray shapej; public: long IsVector1() const { return (shapei.n*shapej.n) == this->n ;} long N() const {return shapei.n;} long M() const {return shapej.n;} long size() const { return shapei.n*shapej.n;} ConjKNM_ h() ; // take the conj for hermian operator const ConjKNM_ h() const ; // take the conj for hermian operator KNM_(R* u,const ShapeOfArray & s, const ShapeOfArray & si, const ShapeOfArray & sj) : KN_(u,s),shapei(si),shapej(sj){} KNM_(R* u,long nn,long mm) : KN_(u,ShapeOfArray(nn*mm)),shapei(nn,1,nn),shapej(mm,nn,1){} KNM_(R* u,long nn,long mm,long s) : KN_(u,ShapeOfArray(nn*mm,s)),shapei(nn,1,nn),shapej(mm,nn,1){} KNM_(KN_ u,long n,long m) : KN_(u,ShapeOfArray(m*n)),shapei(n,1,n),shapej(m,n,1){ } KNM_(const KN_ &u,const ShapeOfArray & si,const ShapeOfArray & sj,long offset=0) : KN_(&u[offset],si.last()+sj.last()+1,u.step),shapei(si),shapej(sj) {K_throwassert( offset>=0 && this->n+ (this->v-(R*)u) <= u.n);} KNM_(const KN_ &u,const ShapeOfArray & si,const ShapeOfArray & sj,long offset,long nnext) : KN_(&u[offset],si.last()+sj.last()+1,u.step,nnext),shapei(si),shapej(sj) {K_throwassert( offset>=0 && this->n+ (this->v-(R*)u) <= u.n);} KNM_(KNM_ U,const SubArray & si,const SubArray & sj) :KN_(U,SubArray(U.ij(si.len1(),sj.len1())+1,U.ij(si.start,sj.start))), shapei(U.shapei,si),shapej(U.shapej,sj){} KNM_(KNM_ U,const SubArray & sa,const SubArray & si,const SubArray & sj) :KN_(U,SubArray(sa)),shapei(U.shapei,si),shapej(U.shapej,sj){} KNM_(const KNM_ & u) :KN_(u),shapei(u.shapei),shapej(u.shapej) {} KNM_ operator()(const SubArray & sa,const SubArray & sb) const { return KNM_(*this,sa,sb);} // sub array long ij(long i,long j) const { return shapei.index(i)+shapej.index(j);} long indexij(long i,long j) const { return this->index(shapei.index(i)+shapej.index(j));} R & operator()(long i,long j) const { return this->v[indexij(i,j)];} R & operator()(int i,int j) const { return this->v[indexij(i,j)];} KN_ operator()(const SubArray & sa,long j) const { return this->operator()(':',j)(sa);} // sub array KN_ operator()(long i,const SubArray & sb) const { return this->operator()(i,':')(sb);} KN_ operator()(const char,long j ) const // une colonne j ('.',j) { return KN_(&this->v[this->index(shapej.index(j))],shapei*this->step);} KN_ operator()(long i ,const char) const // une ligne i (i,'.') { return KN_(&this->v[this->index(shapei.index(i))],shapej*this->step);} KN_ operator()(const char,int j ) const // une colonne j ('.',j) { return KN_(&this->v[this->index(shapej.index(j))],shapei*this->step);} KN_ operator()(int i ,const char) const // une ligne i (i,'.') { return KN_(&this->v[this->index(shapei.index(i))],shapej*this->step);} KN_ operator()(const char,const char) const // tous { return *this;} KNM_ t() const { return KNM_(this->v,*this,shapej,shapei);} // before { return KNM_(*this,shapej,shapei,v);} KNM_& operator =(const KNM_ & u) ; KNM_& operator =(const_R a) ; KNM_& operator+=(const_R a) ; KNM_& operator-=(const_R a) ; KNM_& operator/=(const_R a) ; KNM_& operator*=(const_R a) ; KNM_& operator+=(const KNM_ & u) ; KNM_& operator-=(const KNM_ & u) ; KNM_& operator*=(const KNM_ & u) ; KNM_& operator/=(const KNM_ & u) ; KNM_ &operator =(const outProduct_KN_ &); KNM_ &operator +=(const outProduct_KN_ &); KNM_ &operator -=(const outProduct_KN_ &); KNM_ &operator /=(const outProduct_KN_ &); // bofbof KNM_ &operator *=(const outProduct_KN_ &); // bofbof KNM_ &operator =(const ConjKNM_ &); KNM_ &operator +=(const ConjKNM_ &); KNM_ &operator -=(const ConjKNM_ &); KNM_ &operator /=(const ConjKNM_ &); // bofbof KNM_ &operator *=(const ConjKNM_ &); // bofbof private: KNM_& operator++() {this->v += this->next;return *this;} // ++U KNM_& operator--() {this->v -= this->next;return *this;} // ++U KNM_ operator++(int ){KNM_ old=*this;this->v = this->v +this->next;return old;} // U++ KNM_ operator--(int ){KNM_ old=*this;this->v = this->v -this->next;return old;} // U-- friend class KN_; // friend class KNM_; friend class KNMK_; friend class KN; friend class KNM; friend class KNMK; }; template struct KN_ITAB { KN_ITAB(const T &vv,const I &iindex) : v(vv),index(iindex) {} T v; I index; KN_ITAB & operator=(const T & t); KN_ITAB & operator+=(const T & t); KN_ITAB & operator-=(const T & t); KN_ITAB & operator*=(const T & t); KN_ITAB & operator/=(const T & t); typename T::R & operator[](long i){ return v[index[i]];} const typename T::R & operator[](long i) const { return v[index[i]];} long N() const { return index.N();} }; template KN_ITAB,const KN_ > KN_::operator()(const KN_ &itab) const { return KN_ITAB,const KN_ > (*this,itab);} template KN_ITAB,const KN_ > KN_::operator()(const KN_ &itab) const { return KN_ITAB,const KN_ > (*this,itab);} template KN_ITAB< KN_,const KN_ > KN_::operator()(const KN_ &itab) { return KN_ITAB,const KN_ > (*this,itab);} template KN_ITAB< KN_,const KN_ > KN_::operator()(const KN_ &itab) { return KN_ITAB,const KN_ > (*this,itab);} template struct TKN_:public KN_ { TKN_(const KN_ &x) : KN_(x) {} }; template struct ConjKNM_:public KNM_ { ConjKNM_(const KNM_ &x) : KNM_(x) {} }; template struct notKN_:public KN_ { notKN_(const KN_ &x) : KN_(x) {} notnotKN_ operator!() ; // not const notnotKN_ operator!() const ; // not }; template struct notnotKN_:public KN_ { notnotKN_(const notKN_ &x) : KN_(x) {} notKN_ operator!() ; // notnot const notKN_ operator!() const ; // notnot }; template TKN_ KN_::t() { return *this;} // transpose template ConjKNM_ KNM_::h() { return *this;} // conj of the matrix template const TKN_ KN_::t() const { return *this;} // transpose template const ConjKNM_ KNM_::h() const { return *this;} // conj of the matrix template notKN_ KN_::operator!() { return *this;} // not template const notKN_ KN_::operator!() const { return *this;} // not template notnotKN_ notKN_::operator!() { return *this;} // not template const notnotKN_ notKN_::operator!() const { return *this;} // not template struct outProduct_KN_ { const KN_ a,b; R c; long N() const {return a.N(); } long M() const {return b.N(); } outProduct_KN_(const KN_ & aa, const KN_ &bb,R cc=(R)1) : a(aa),b(bb),c(cc) {} outProduct_KN_(const KN_ * aa, const KN_ &bb,R cc=(R)1) : a(*aa),b(bb),c(cc) {} outProduct_KN_(const KN_ * aa, const KN_ *bb,R cc=(R)1) : a(*aa),b(*bb),c(cc) {} outProduct_KN_(const Mulc_KN_ & aa,const KN_ & bb) : a(aa.a),b(bb),c(aa.b) {} outProduct_KN_ operator * (R cc) { return outProduct_KN_(a,b,c*cc);} }; template struct if_KN_ { const KN_ & a,&b; R c; if_KN_(const KN_ & aa, const KN_ &bb,R cc=1.) : a(aa),b(bb),c(cc) {} if_KN_ operator * (R cc) { return if_KN_(a,b,c*cc);} }; template struct ifnot_KN_ { const KN_ & a,&b; R c; ifnot_KN_(const KN_ & aa, const KN_ &bb,R cc=1.) : a(aa),b(bb),c(cc) {} ifnot_KN_ operator * (R cc) { return ifnot_KN_(a,b,c*cc);} }; template outProduct_KN_ operator*(const KN_ &a,const TKN_ &b) { return outProduct_KN_(a,b);} template ifnot_KN_ operator*(const KN_ &a,const notKN_ &b) { return ifnot_KN_(b,a);} template ifnot_KN_ operator*(const KN_ &a,const notnotKN_ &b) { return if_KN_(b,a);} template ifnot_KN_ operator*(const notKN_ &b,const KN_ &a) { return ifnot_KN_(b,a);} template ifnot_KN_ operator*(const notnotKN_ &b,const KN_ & a) { return if_KN_(b,a);} template R operator*(const TKN_ &a,const KN_ &b) { return (a,b);} template class KNMK_: public KN_ { friend class KNMK; public: ShapeOfArray shapei; ShapeOfArray shapej; ShapeOfArray shapek; public: long IsVector1() const { return (shapei.n*shapej.n*shapek.n) == this->n ;} long N() const {return shapei.n;} long M() const {return shapej.n;} long K() const {return shapek.n;} long size() const { return shapei.n*shapej.n*shapek.n;} KNMK_(const ShapeOfArray & s, const ShapeOfArray & si, const ShapeOfArray & sj, const ShapeOfArray & sk, R * u) : KN_(u,s),shapei(si),shapej(sj),shapek(sk){} KNMK_(R* u,long n,long m,long k) : KN_(u, ShapeOfArray(n*m*k)),shapei(n,1,n),shapej(m,n,1),shapek(k,n*m,n*m){} // KNMK_(const KN_ & u,long n,long m,long k) // : KN_(ShapeOfArray(n*m*k)),shapei(n,1,n),shapekj(m,n,1),u), // shapek(k,n*m,n*m){}; KNMK_(const KNMK_ &U,const SubArray & si,const SubArray & sj,const SubArray & sk) : KN_(U,SubArray(U.ijk(si.len1(),sj.len1(),sk.len1())+1, U.ijk(si.start,sj.start,sk.start))), shapei(U.shapei,si), shapej(U.shapej,sj), shapek(U.shapek,sk){} KNMK_(const KNMK_ & u) :KN_(u),shapei(u.shapei),shapej(u.shapej),shapek(u.shapek) {} long ijk(long i,long j,long k) const { return shapei.index(i)+shapej.index(j)+shapek.index(k);} long indexijk(long i,long j,long k) const {return this->index(shapei.index(i)+shapej.index(j)+shapek.index(k));} R & operator()(long i,long j,long k) const {return this->v[indexijk(i,j,k)];} R & operator()(int i,int j,int k) const {return this->v[indexijk(i,j,k)];} // pas de tableau suivant KN_ operator()(const char ,long j,long k) const { // le tableau (.,j,k) return KN_(*this,-1,shapei,shapej[j]+shapek[k]);} KN_ operator()(long i,const char ,long k) const { // le tableau (i,.,k) return KN_(*this,-1,shapej,shapei[i]+shapek[k]);} KN_ operator()(long i,long j,const char ) const { // le tableau (i,j,.) return KN_(*this,-1,shapek,shapei[i]+shapej[j]);} KN_ operator()(const char ,int j,int k) const { // le tableau (.,j,k) return KN_(*this,-1,shapei,shapej[j]+shapek[k]);} KN_ operator()(int i,const char ,int k) const { // le tableau (i,.,k) return KN_(*this,-1,shapej,shapei[i]+shapek[k]);} KN_ operator()(int i,int j,const char ) const { // le tableau (i,j,.) return KN_(*this,-1,shapek,shapei[i]+shapej[j]);} // KNM_ operator()(const char ,const char ,long k) const { // le tableau (.,.,k) return KNM_(*this,shapei,shapej,shapek[k],shapek.next);} // step = n*m //attention les suivants ne marche pas KNM_ operator()(const char ,long j,const char ) const { // le tableau (.,j,.) return KNM_(*this,shapei,shapek,shapej[j],-1/*shapej.next*/);} // step = n KNM_ operator()(long i,const char ,const char ) const { // le tableau (i,.,.) return KNM_(*this,shapej,shapek,shapei[i],-1/*shapei.next*/);} // step = 1 KNM_ operator()(const char ,const char ,int k) const { // le tableau (.,.,k) return KNM_(*this,shapei,shapej,shapek[k],shapek.next);} // step = n*m //attention les suivants ne marche pas KNM_ operator()(const char ,int j,const char ) const { // le tableau (.,j,.) return KNM_(*this,shapei,shapek,shapej[j],-1/*shapej.next*/);} // step = n KNM_ operator()(int i,const char ,const char ) const { // le tableau (i,.,.) return KNM_(*this,shapej,shapek,shapei[i],-1/*shapei.next*/);} // step = 1 KNMK_& operator =(const KNMK_ & u) ; KNMK_& operator+=(const KNMK_ & u) ; KNMK_& operator-=(const KNMK_ & u) ; KNMK_& operator/=(const KNMK_ & u) ; KNMK_& operator*=(const KNMK_ & u) ; KNMK_& operator =(const_R a) ; KNMK_& operator+=(const_R a) ; KNMK_& operator-=(const_R a) ; KNMK_& operator/=(const_R a) ; KNMK_& operator*=(const_R a) ; KNMK_ operator()(SubArray si,SubArray sj,SubArray sk) const {return KNMK_(*this,si,sj,sk);} private: // KNMK_& operator++(){v += next;return *this;} // ++U // KNMK_& operator--(){v -= next;return *this;} // --U // KNMK_ operator++(long ){KNMK_ old=*this;v = v +next;return old;} // U++ // KNMK_ operator--(long ){KNMK_ old=*this;v = v -next;return old;} // U-- friend class KNM_; friend class KN_; }; template class KN :public KN_ { public: typedef R K; // explicit KN(const R & u):KN_(new R(uu),1,0) {} KN() : KN_(nullptr,0) {} KN(long nn) : KN_(new R[nn],nn) {} KN(long nn, R * p) : KN_(new R[nn],nn) { KN_::operator=(KN_(p,nn));} KN(long nn,R (*f)(long i) ) : KN_(new R[nn],nn) {for(long i=0;in;i++) this->v[i]=f(i);} KN(long nn,const R & a) : KN_(new R[nn],nn) { KN_::operator=(a);} KN(long nn,long s,const R a) : KN_(new R[nn],nn,s) { KN_::operator=(a);} template KN(const KN_ & s):KN_(new R[s.n],s.n) {for (long i=0;in;i++) this->v[i] = s[i];} template KN(const KN_ & s,R (*f)(S )):KN_(new R[s.n],s.n) {for (long i=0;in;i++) this->v[i] = f(s[i]);} KN(const KN & u):KN_(new R[u.n],u.n) { KN_::operator=(u);} KN(bool ,KN & u):KN_(u) {u.v=0;u.n=0;}// remove copy for return of local KN. // explicit KN(const KN_ & u):KN_(new R[u.n],u.n) // { KN_::operator=(u);} ~KN(){delete [] this->v;} void CheckSet() { if(!(this->n)) {cerr << "Error RNM set array\n";K_throwassert(0); exit(1);}} KN& operator = (R* a) { CheckSet(); return operator =(KN_(a,this->n));} KN& operator += (R* a) { CheckSet(); return operator+=(KN_(a,this->n));} KN& operator -= (R* a) { CheckSet(); return operator-=(KN_(a,this->n));} KN& operator *= (R* a) { CheckSet(); return operator*=(KN_(a,this->n));} KN& operator /= (R* a) { CheckSet(); return operator/=(KN_(a,this->n));} KN& operator =(const SetArray & u) { if(this->unset()) this->set(new R[u.size()],u.size(),0,0); KN_::operator= (u);return *this;} KN& operator +=(const SetArray & u) { if(this->unset()) this->set(new R[u.size()],u.size(),0,0); KN_::operator+= (u);return *this;} KN& operator -=(const SetArray & u) { if(this->unset()) this->set(new R[u.size()],u.size(),0,0); KN_::operator-= (u);return *this;} KN& operator *=(const SetArray & u) { if(this->unset()) this->set(new R[u.size()],u.size(),0,0); KN_::operator*= (u);return *this;} KN& operator /=(const SetArray & u) { if(this->unset()) this->set(new R[u.size()],u.size(),0,0); KN_::operator/= (u);return *this;} KN& operator =(const_R a) { if(this->unset()) this->set(new R[1],1,0,0); KN_::operator= (a);return *this;} KN& operator =(const KN_& a) { if(this->unset()) this->set(new R[a.N()],a.N()); KN_::operator= (a);return *this;} KN& operator =(const KN& a) { if(this->unset()) this->set(new R[a.N()],a.N()); KN_::operator= (a);return *this;} KN& operator =(const Add_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const DotStar_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const if_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const ifnot_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const DotSlash_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const Sub_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const Mulc_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const Divc_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const Add_Mulc_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const if_arth_KN_ & u) { if(this->unset()) this->set(new R[u.a.N()],u.a.N());KN_::operator=(u);return *this;} KN& operator =(const Mul_KNM_KN_ & u) { if(this->unset()) this->set(new R[u.A.N()],u.A.N());KN_::operator=(u);return *this;} KN& operator =(const Mul_KNMh_KN_ & u) { if(this->unset()) this->set(new R[u.A.M()],u.A.M());KN_::operator=(u);return *this;} // KN& operator =(const MatriceCreuseMulKN_ & Ax) // {if(this->unset()) set(new R[Ax.v.N()],Ax.v.N()); KN_::operator=(Ax);return *this;} // KN& operator +=(const MatriceCreuseMulKN_ & Ax) // {if(this->unset()) set(new R[Ax.v.N()],Ax.v.N()); KN_::operator+=(Ax);return *this;} // KN& operator =(const MatriceCreuseDivKN_ & A1x) // { if(this->unset()) set(new R[A1x.v.N()],A1x.v.N());KN_::operator=(A1x);return *this;} // correcton aout 2007 FH add N,M flied in RNM_VirtualMatrix KN& operator =(const typename RNM_VirtualMatrix::plusAx & Ax) { if(this->unset() && Ax.A && Ax.A->N ) this->set(new R[Ax.A->N],Ax.A->N); if(Ax.A) KN_::operator=(Ax);return *this;} KN& operator =(const typename RNM_VirtualMatrix::solveAxeqb & Ab) { if(this->unset()) {this->set(new R[Ab.b.N()],Ab.b.N()); KN_::operator=(R());} KN_::operator=(Ab);return *this;} KN& operator =(const typename RNM_VirtualMatrix::solveAtxeqb & Ab) { if(this->unset()) {this->set(new R[Ab.b.N()],Ab.b.N()); KN_::operator=(R());} KN_::operator=(Ab);return *this;} KN& operator +=(const typename RNM_VirtualMatrix::plusAx & Ax) { if(this->unset() && Ax.A->N) { this->set(new R[Ax.A->N],Ax.A->N); KN_::operator=(R());} KN_::operator+=(Ax); return *this;} KN& operator =(const typename RNM_VirtualMatrix::plusAtx & Ax) { if(this->unset()&&Ax.A->M) this->set(new R[Ax.A->M],Ax.A->M);KN_::operator=(Ax);return *this;} KN& operator +=(const typename RNM_VirtualMatrix::plusAtx & Ax) { if(this->unset()&&Ax.A->M) { this->set(new R[Ax.A->M],Ax.A->M); KN_::operator=(R());} KN_::operator+=(Ax); return *this;} // end correcton FH template KN& operator =(const PplusQ & PQ) { *this=PQ.p; *this+=PQ.q;return *this; } template KN& operator +=(const PplusQ & PQ) { *this+=PQ.p; *this+=PQ.q;return *this; } KN& operator -=(const_R a) { KN_::operator-=(a);return *this;} KN& operator -=(const KN_& a) { KN_::operator-= (a);return *this;} KN& operator -=(const Add_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const DotStar_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const DotSlash_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const Sub_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const Mulc_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const Divc_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const Add_Mulc_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const if_arth_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator -=(const Mul_KNM_KN_ & u) { KN_::operator-=(u);return *this;} KN& operator +=(const_R a) { KN_::operator += (a);return *this;} KN& operator += (const KN_& a) { KN_::operator+= (a);return *this;} KN& operator +=(const Add_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const DotStar_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const DotSlash_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const Sub_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const Mulc_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const Divc_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const Add_Mulc_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const if_arth_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator +=(const Mul_KNM_KN_ & u) { KN_::operator+=(u);return *this;} KN& operator/=(const_R a) { KN_::operator/=(a);return *this;} KN& operator /= (const KN_& a) { KN_::operator/= (a);return *this;} KN& operator /=(const Add_KN_ & u) { KN_::operator/=(u);return *this;} KN& operator /=(const Sub_KN_ & u) { KN_::operator/=(u);return *this;} KN& operator /=(const Mulc_KN_ & u) { KN_::operator/=(u);return *this;} KN& operator /=(const Divc_KN_ & u) { KN_::operator/=(u);return *this;} KN& operator /=(const Add_Mulc_KN_ & u) { KN_::operator/=(u);return *this;} KN& operator /=(const if_arth_KN_ & u) { KN_::operator/=(u);return *this;} KN& operator /=(const Mul_KNM_KN_ & u) { KN_::operator/=(u);return *this;} KN& operator*=(const_R a) { KN_::operator*=(a);return *this;} KN& operator*=(const KN_& a) { KN_::operator*= (a);return *this;} KN& operator *=(const Add_KN_ & u) { KN_::operator*=(u);return *this;} KN& operator *=(const Sub_KN_ & u) { KN_::operator*=(u);return *this;} KN& operator *=(const Mulc_KN_ & u) { KN_::operator*=(u);return *this;} KN& operator *=(const Divc_KN_ & u) { KN_::operator*=(u);return *this;} KN& operator *=(const Add_Mulc_KN_ & u) { KN_::operator*=(u);return *this;} KN& operator *=(const if_arth_KN_ & u) { KN_::operator*=(u);return *this;} KN& operator *=(const Mul_KNM_KN_ & u) { KN_::operator*=(u);return *this;} template KN& operator = (const KN_ITAB & ui) { KN_::operator =(ui); return *this;} template KN& operator += (const KN_ITAB & ui) { KN_::operator +=(ui); return *this;} template KN& operator -= (const KN_ITAB & ui) { KN_::operator -=(ui); return *this;} template KN& operator *= (const KN_ITAB & ui) { KN_::operator *=(ui); return *this;} template KN& operator /= (const KN_ITAB & ui) { KN_::operator /=(ui); return *this;} template KN& operator = (const F_KN_ & ui) { if(this->unset()) this->set(new R[ui.N()],ui.N()); KN_::operator =(ui); return *this;} // two opertor to cast to an array of constant // operator KN_ & () // { return * (KN_*) this;} // operator KN_ const & () const // { return *(const KN_*) this;} // operator KN & () // { return (KN &) *this;} // operator KN const & () const // { return (const KN& ) *this;} void init(long nn) {this->n=nn;this->step=1;this->next=-1;this->v=new R[nn]();} void init() {this->n=0;this->step=1;this->next=-1;this->v=nullptr;} void init(const KN_ & a){init(a.N()); operator=(a);} void resize(long nn) { if ( nn != this->n) { R *vo=this->v; long no=std::min(this->n,nn), so=this->step; ShapeOfArray::init(nn); this->v=new R[this->n]; // copy if(this->v && vo) for(long i=0,j=0;jv[i]=vo[j]; delete [] vo;} }// mars 2010 void destroy(){/*assert(this->next<0);*/ if(this->next++ ==-1) {delete [] this->v; this->v=nullptr;this->n=0;}}// mars 2010 void increment() {/*assert(this->next<0);*/ this->next--;} }; // Array with 2 indices // --------------------- template class KNM: public KNM_{ public: KNM() :KNM_(nullptr,0,0){} KNM(long nn,long mm) :KNM_(new R[nn*mm],nn,mm){} KNM(const KNM & u) // PB si stepi ou stepj nulle :KNM_(new R[u.size()],u.N(),u.M()) { KN_::operator=(u);} explicit KNM(const KNM_ & u) :KNM_(new R[u.size()],u.N(),u.M()) { KNM_::operator=(u);} ~KNM(){delete [] this->v;} KNM& operator=(const KNM_ & u) { if(this->unset()) this->init(u.N(),u.M()) ; KNM_::operator=(u);return *this;} KNM& operator=(const KNM& u) { if(this->unset()) this->init(u.N(),u.M()) ; KNM_::operator=(u);return *this;} KNM& operator=(const_R a) { if(this->unset()) RNM_FATAL_ERROR(" KNM operator=(double)"); KNM_::operator=(a);return *this;} KNM& operator+=(const_R a) { if(this->unset()) RNM_FATAL_ERROR(" KNM operator+=(double)"); KNM_::operator+=(a);return *this;} KNM& operator-=(const_R a) {if(this->unset()) RNM_FATAL_ERROR(" KNM operator-=(double)"); KNM_::operator-=(a);return *this;} KNM& operator/=(const_R a) {if(this->unset()) RNM_FATAL_ERROR(" KNM operator/=(double)"); KNM_::operator/=(a);return *this;} KNM& operator*=(const_R a) {if(this->unset()) RNM_FATAL_ERROR(" KNM operator*=(double)"); KNM_::operator*=(a);return *this;} KNM& operator+=(const KNM_ & u) { if(this->unset()) this->init(u.N(),u.M()) ; KNM_::operator+=(u);return *this;} KNM& operator-=(const KNM_ & u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator-=(u);return *this;} KNM& operator/=(const KNM_ & u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator/=(u);return *this;} KNM& operator*=(const KNM_ & u) { if(this->unset()) this->init(u.N(),u.M()) ; KNM_::operator*=(u);return *this;} KNM &operator =(const outProduct_KN_ & u) { if(this->unset()) this->init(u.N(),u.M()) ; KNM_::operator =(u);return *this;} KNM &operator +=(const outProduct_KN_ & u) { if(this->unset()) this->init(u.N(),u.M()) ; KNM_::operator+=(u);return *this;} KNM &operator -=(const outProduct_KN_ & u) { if(this->unset()) this->init(u.N(),u.M()) ; KNM_::operator-=(u);return *this;} KNM &operator /=(const outProduct_KN_ & u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator/=(u);return *this;} KNM &operator *=(const outProduct_KN_ & u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator*=(u);return *this;} KNM &operator =(const ConjKNM_ &u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator=(u);return *this;} KNM &operator +=(const ConjKNM_ &u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator+=(u);return *this;} KNM &operator -=(const ConjKNM_ &u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator-=(u);return *this;} KNM &operator /=(const ConjKNM_ &u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator/=(u);return *this;} KNM &operator *=(const ConjKNM_ &u) { if(this->unset()) this->init(u.N(),u.M()) ;KNM_::operator*=(u);return *this;} // bofbof // two opertors to cast to un array of constant // operator KNM_ & () // { return * (KNM_*) this;} // operator KNM_ const & () const // { return *(const KNM_*) this;} // operator KNM & () // { return * (KNM*) this;} // operator KNM const & () const // { return *(const KNM*) this;} void init() { // add mars 2010 ... this->n=0;this->step=1;this->next=-1;this->v=nullptr; this->shapei.init(0); this->shapej.init(0);} void init(long nn,long mm) { ShapeOfArray::init(nn*mm); this->shapei.init(nn,1,nn); this->shapej.init(mm,nn,1), this->v=new R[nn*mm];} void resize(long nn,long mm) { long kk=nn*mm; long n = this->shapei.n; long m = this->shapej.n; if( (n !=nn) || ( m != mm)) // correct FH Jav 2012 .. { KNM_ old(*this); long no=std::min(n,nn); long mo=std::min(m,mm); R *vo=this->v; // new mat ShapeOfArray::init(kk); this->v=new R[this->n]; this->shapei.init(nn,1,nn); this->shapej.init(mm,nn,1); if(this->v && vo && n && m) // copy (*this)(SubArray(no),SubArray(mo)) = old(SubArray(no),SubArray(mo)); delete []vo; } } void destroy(){/*assert((bool)(this->next<0)); */ if(this->next++ ==-1) {delete [] this->v; this->v=nullptr;this->n=0;}} void increment() {/*assert((bool)(this->next<0)); */ this->next--;} // void destroy(){delete [] this->v;this->n=0 ;} }; // Array with 3 indices // --------------------- template class KNMK: public KNMK_{ public: KNMK(long n,long m,long k) :KNMK_(new R[n*m*k],n,m,k){} explicit KNMK(const KNMK_ & u) :KNMK_(new R[u.size()],u.N(),u.M(),u.K()) { KNMK_::operator=(u);} KNMK(const KNMK & u) :KNMK_(new R[u.size()],u.N(),u.M(),u.K()) { KNMK_::operator=(u);} ~KNMK(){delete [] this->v;} KNMK& operator=(const KNMK_ & u) { KNMK_::operator=(u);return *this;} KNMK& operator=(const_R a) { KNMK_::operator=(a);return *this;} KNMK& operator+=(const_R a) { KNMK_::operator+=(a);return *this;} KNMK& operator-=(const_R a) { KNMK_::operator-=(a);return *this;} KNMK& operator/=(const_R a) { KNMK_::operator/=(a);return *this;} KNMK& operator*=(const_R a) { KNMK_::operator*=(a);return *this;} KNMK& operator+=(const KNMK_ & u) { KNMK_::operator+=(u);return *this;} // ici jd KNMK& operator-=(const KNMK_ & u) { KNMK_::operator-=(u);return *this;} KNMK& operator*=(const KNMK_ & u) { KNMK_::operator*=(u);return *this;} KNMK& operator/=(const KNMK_ & u) { KNMK_::operator/=(u);return *this;} // two opertor to cast to un array of constant // operator KNMK_ & () // { return * (KNMK_*) this;} // operator KNMK_ const & () const // { return *(const KNMK_*) this;} // operator KNMK & () // { return * (KNMK*) this;} // operator KNMK const & () const // { return *(const KNMK*) this;} }; // ------------- optimization --------------------- template class conj_KN_{public: const KN_ & a; conj_KN_(const KN_ & aa) : a(aa){} }; inline const KN_ conj(const KN_ &a){ return a;} inline const KN_ conj(const KN_ &a){ return a;} inline const KN_ conj(const KN_ &a){ return a;} //template conj_KN_ conj(const KN &a){ return a;} template conj_KN_ conj(const KN_ &a){ return a;} template class DotStar_KN_{public: const KN_ a; const KN_ b; DotStar_KN_(const KN_ & aa,const KN_ & bb) : a(aa),b(bb) {} }; template class DotSlash_KN_{public: const KN_ a; const KN_ b; DotSlash_KN_(const KN_ & aa,const KN_ & bb) : a(aa),b(bb) {} }; template class Add_KN_{public: const KN_ a; const KN_ b; Add_KN_(const KN_ & aa,const KN_ & bb) : a(aa),b(bb) { K_throwassert(SameShape(a,b));} }; template class Sub_KN_{public: const KN_ a; const KN_ b; Sub_KN_(const KN_ & aa,const KN_ & bb) : a(aa),b(bb) { K_throwassert(SameShape(a,b));} }; template class Mulc_KN_ { public: const KN_ a; const_R b; Mulc_KN_(const KN_ & aa,const_R bb) : a(aa),b(bb) {} Mulc_KN_(const Mulc_KN_ & aa,const_R bb) : a(aa.a),b(aa.b*bb) {} Mulc_KN_ operator-() const {return Mulc_KN_(a,-b);} outProduct_KN_ operator*(const TKN_ & bb) { return outProduct_KN_(a,bb,b);} }; template class Divc_KN_ { // // vector b/a_i .. public: const KN_ a; const_R b; Divc_KN_(const_R bb,const KN_ & aa) : a(aa),b(bb) {} // Divc_KN_(const Divc_KN_ & aa,const_R bb) : a(aa.a),b(aa.b*bb) {} Divc_KN_ operator-() const {return Divc_KN_(a,-b);} }; template class Add_Mulc_KN_ { public: const KN_ a,b; const R ca,cb; Add_Mulc_KN_(const Mulc_KN_ & aa,const Mulc_KN_ & bb) : a(aa.a),b(bb.a),ca(aa.b),cb(bb.b) { K_throwassert(SameShape(a,b));} Add_Mulc_KN_(const Mulc_KN_ & aa,const KN_ & bb,const R cbb) : a(aa.a),b(bb),ca(aa.b),cb(cbb) { K_throwassert(SameShape(a,b));} Add_Mulc_KN_(const KN_ & aa,const R caa,const KN_ & bb,const R cbb) : a(aa),b(bb),ca(caa),cb(cbb) { K_throwassert(SameShape(a,b));} }; template class if_arth_KN_ { public: const KN_ a,b,c; if_arth_KN_(const KN_ & aa,const KN_ & bb,const KN_ & cc) : a(aa),b(bb),c(cc){ K_throwassert(SameShape(a,b)&&SameShape(a,c));} }; template class Mul_KNM_KN_ { public: const KNM_ &A; const KN_ &b; Mul_KNM_KN_(const KNM_ &aa,const KN_ &bb) : A(aa),b(bb) {K_throwassert(SameShape(A.shapej,b));} }; template class Mul_KNMh_KN_ { public: const KNM_ &A; const KN_ &b; Mul_KNMh_KN_(const KNM_ &aa,const KN_ &bb) : A(aa),b(bb) {K_throwassert(SameShape(A.shapei,b));} Mul_KNMh_KN_(const KNM &aa,const KN_ &bb) : A(aa),b(bb) {K_throwassert(SameShape(A.shapei,b));} }; ostream & operator<<(ostream & f,const ShapeOfArray & s); template ostream & operator<<(ostream & f,const KN_ & v); template ostream & operator<<(ostream & f,const KNM_ & v); template ostream & operator<<(ostream & f,const KNMK_ & v); template inline ostream & operator<<(ostream & f,const KN & v) { return f << (const KN_ &) v;} template inline ostream & operator<<(ostream & f,const KNM & v) { return f << (const KNM_ &) v;} template inline ostream & operator<<(ostream & f,const KNMK & v) { return f << (const KNMK_ &) v;} template inline Add_KN_ operator+(const KN_ &a,const KN_ &b) { return Add_KN_(a,b);} template inline Sub_KN_ operator-(const KN_ &a,const KN_ &b) { return Sub_KN_(a,b);} template inline Mulc_KN_ operator*(const KN_ &a,const R &b) { return Mulc_KN_(a,b);} template inline Mulc_KN_ operator/(const KN_ &a,const R &b) { return Mulc_KN_(a,R(1)/b);} template inline Mulc_KN_ operator*(const R &b,const KN_ &a) { return Mulc_KN_(a,b);} template inline Divc_KN_ operator/(const R &b,const KN_ &a) { return Divc_KN_(b,a);} template inline Mulc_KN_ operator-(const KN_ &a) { return Mulc_KN_(a,R(-1));} template inline Add_Mulc_KN_ operator+(const Mulc_KN_& a,const Mulc_KN_ &b) { return Add_Mulc_KN_(a,b);} template inline Add_Mulc_KN_ operator-(const Mulc_KN_& a,const Mulc_KN_ &b) { return Add_Mulc_KN_(a,b.a,-b.b);} template inline Add_Mulc_KN_ operator+(const Mulc_KN_& a,const KN_ &b) { return Add_Mulc_KN_(a,b,R(1));} template inline Add_Mulc_KN_ operator-(const Mulc_KN_& a,const KN_ &b) { return Add_Mulc_KN_(a,b,R(-1));} template inline Add_Mulc_KN_ operator+(const KN_ & b,const Mulc_KN_& a) { return Add_Mulc_KN_(a,b,R(1));} // modif FH mars 2007 template inline Add_Mulc_KN_ operator-(const KN_ & a,const Mulc_KN_& b) { return Add_Mulc_KN_(a,R(1),b.a,-b.b);}// modif FH mars 2007 template inline Mul_KNM_KN_ operator*(const KNM_ & A,const KN_ & b) { return Mul_KNM_KN_(A,b);} template inline bool SameShape(const ShapeOfArray & a,const Add_Mulc_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const if_arth_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const Add_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const Sub_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const Mulc_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const Divc_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const DotStar_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const DotSlash_KN_ & b) { return SameShape(a,b.a) ;} template inline bool SameShape(const ShapeOfArray & a,const Mul_KNM_KN_ & b) { return a.n==b.A.N() ;} inline bool SameShape(const ShapeOfArray & ,const RNM_VirtualMatrix::plusAx & ) { return true ;} // pas de test car la matrice peut etre rectangulaire inline bool SameShape(const ShapeOfArray & ,const RNM_VirtualMatrix::plusAtx & ) { return true ;} // pas de test car la matrice peut etre rectangulaire inline bool SameShape(const ShapeOfArray & ,const RNM_VirtualMatrix >::plusAx & ) { return true ;} // pas de test car la matrice peut etre rectangulaire inline bool SameShape(const ShapeOfArray & ,const RNM_VirtualMatrix >::plusAtx & ) { return true ;} // pas de test car la matrice peut etre rectangulaire inline bool SameShape(const ShapeOfArray & ,const double) { return true;} inline bool SameShape(const ShapeOfArray & ,const complex) { return true;} inline bool SameShape(const ShapeOfArray & ,const complex) { return true;} template inline bool SameShape(KNM& m, const outProduct_KN_& p) { return p.a.N()>=m.N() && m.M()>=p.b.N(); } template inline long SameAdress(const KN_ &a, const KN_ &b) { return &a[0]==&b[0];} // bof -bof //template inline // KN_::operator KN &() { return *(KN *) (void *) this;} //template inline // KN_::operator const KN &() const { return *(const KN *) ( const void *) this;} // operateur y=Ax-b ou y=Ax + b pour le GC template PplusQ< typename RNM_VirtualMatrix::plusAx, Mulc_KN_ > operator-(const typename RNM_VirtualMatrix::plusAx & A,const KN_ & B) { return PplusQ< typename RNM_VirtualMatrix::plusAx, Mulc_KN_ >(A,Mulc_KN_(B,R(-1.)));} template PplusQ< typename RNM_VirtualMatrix::plusAx, KN_ > operator+(const typename RNM_VirtualMatrix::plusAx & A,const KN_ & B) { return PplusQ< typename RNM_VirtualMatrix::plusAx, KN_ >(A,B);} template PplusQ< typename RNM_VirtualMatrix::plusAx, Mulc_KN_ > operator-(const typename RNM_VirtualMatrix::plusAx & A,const KN & B) { return PplusQ< typename RNM_VirtualMatrix::plusAx, Mulc_KN_ >(A,Mulc_KN_(B,R(-1.)));} template PplusQ< typename RNM_VirtualMatrix::plusAx, KN_ > operator+(const typename RNM_VirtualMatrix::plusAx & A,const KN & B) { return PplusQ< typename RNM_VirtualMatrix::plusAx, KN_ >(A,B);} template KN_ diagonal(const KNM & A) { K_throwassert(A.N() == A.M()); return KN_(A,SubArray(A.N(),0,A.N()+1));} // to def inv permutation FH mars 2006 class Inv_KN_long{ public: KN_ t; Inv_KN_long(const KN_ & v) : t(v) {} Inv_KN_long( KN_ const * & v) : t(*v) {} operator const KN_ & () const {return t;} }; // For sparce solve to set array to be consecutif (step==1) if neccessarly template class KN_2Ptr { public: // transfo de KN_ peut etre non concecutif (a.step != 1) en // un tableau concecutif en memoire si necessaire // avec recopie du tableau dans le tableau d'origne a la destruction. KN_ a; const KN_ ca; KN c; // tableau copie si non vide KN_2Ptr(KN_ & vv) : a(vv),ca(vv),c() { assert(a.N()); if (ca.step !=1 ) c=ca;} // copy if non consecutif KN_2Ptr(const KN_ & vv) : a(0,0),ca(vv),c() { assert(ca.N()); if (ca.step !=1 ) c=ca; }// copy if non consecutif operator R *() { return c.unset() ? (R *) ca:(R *) c ;} operator const R *() const { return c.unset() ? (R *) ca:(R *) c ;} ~KN_2Ptr() { if(!a.unset() && !c.unset() ) {a=c; } } // recopy }; // correct march 2015 FH ... // add BB type ... template class F_KN_ { public: A (*f)(BB); KN_ b; long N() const {return b.N();} F_KN_( A (*ff)(BB),const KN_ & aa): f(ff),b(aa) {} A operator[](long i) const { return f(b[i]);} bool check(long n) const { return n <= b.N() || b.constant(); } bool constant() const {return b.constant();} }; template inline bool SameShape(const ShapeOfArray & a,const F_KN_ & b) { return !a.step || b.constant() || a.n == b.N() ;} #include "RNM_tpl.hpp" #ifdef K_throwassert #undef K_throwassert #endif #endif