diff --git a/mat.h b/mat.h index 78ead3f..d50c384 100644 --- a/mat.h +++ b/mat.h @@ -409,6 +409,10 @@ public: const typename LA_traits::normtype nonsymmetry() const; const typename LA_traits::normtype nonhermiticity() const; + //!print sorted list of matrix elements + void printsorted(std::ostream &s, int direction=1, bool absvalue=false, bool stable=false, typename LA_traits::normtype thr=0, int topmax=0) const; + + #ifndef NO_STRASSEN //! Strassen's multiplication (better than \f$\mathacal{O}(n^3)\f$, analogous syntax to \see NRMat::gemm() ) void strassen(const T beta, const NRMat &a, const char transa, const NRMat &b, const char transb, const T alpha); @@ -1582,6 +1586,16 @@ for(int i=0; i +void NRMat::printsorted(std::ostream &s, int direction, bool absvalue, bool stable, typename LA_traits::normtype thr, int topmax) const { +NRMat indexmap(nn*mm,2); +NRVec tmp(*this); +int ii=0; +for(int i=0; ij;} //this can be used for compact storage of matrices, which are actually not symmetric, but one triangle of them is redundant const typename LA_traits::normtype norm(const T scalar = (T)0) const; void axpy(const T alpha, const NRSMat &x); // this+= a*x + inline const T amax() const; inline const T amin() const; @@ -186,6 +187,9 @@ public: void storesubmatrix(const int from, const NRSMat &rhs); void storesubmatrix(const NRVec &selection, const NRSMat &rhs); + //!print sorted list of matrix elements + void printsorted(std::ostream &s, int direction=1, bool absvalue=false, bool stable=false, typename LA_traits::normtype thr=0, int topmax=0) const; + inline operator T*(); inline operator const T*() const; @@ -1245,6 +1249,17 @@ for(int i=0; i +void NRSMat::printsorted(std::ostream &s, int direction, bool absvalue, bool stable, typename LA_traits::normtype thr, int topmax) const +{ +NRMat indexmap(NN2,2); +NRVec tmp(*this); +int ii=0; +for(int i=0; i objects and scalars diff --git a/t.cc b/t.cc index 1a69781..5d21575 100644 --- a/t.cc +++ b/t.cc @@ -3440,7 +3440,7 @@ cout < m; cin >>m; @@ -3451,6 +3451,37 @@ cout < v(12); +v.randomize(1.); +cout < v(4); +v.randomize(1.); +cout < v(4,5); +v.randomize(1.); +cout < &perm, bool stable=false); + void printsorted(std::ostream &s, int direction=1, bool absvalue=false, bool stable=false, typename LA_traits::normtype thr=0, int topmax=0, const NRMat *indexmap=NULL) const; //! apply given function to each element NRVec& call_on_me(T (*_F)(const T &) ){ @@ -583,6 +584,35 @@ return r; } +template +void NRVec::printsorted(std::ostream &s, int direction, bool absvalue, bool stable, typename LA_traits::normtype thr, int topmax, const NRMat *indexmap) const +{ +NRPerm perm(nn); perm.identity(); +NRVec_from1 tmp; +NRVec_from1::normtype> tmp2; +if(absvalue) + { + tmp2.resize(nn); + for(int i=1; i<=nn; ++i) tmp2[i] = MYABS((*this)[i-1]); + tmp2.sort(direction,perm,stable); + } +else + { + tmp= *this; + tmp.sort(direction,perm,stable); + } +int ii=1; +for(int i=1; i<=nn; ++i) + { + if(topmax && ii>topmax) break; + if(thr!=0 && (absvalue?tmp2[i]:MYABS(tmp[i]))