diff --git a/lieonn.hh b/lieonn.hh index ccf9c27a..4cf44961 100644 --- a/lieonn.hh +++ b/lieonn.hh @@ -3997,37 +3997,46 @@ template bool savep2or3(const char* filename, const vector static inline vector > normalize(const vector >& data, const T& upper = T(1)) { +template static inline vector > > normalize(const vector > >& data, const T& upper = T(1)) { T MM(0), mm(0); bool fixed(false); - for(int k = 0; k < data.size(); k ++) - for(int i = 0; i < data[k].rows(); i ++) - for(int j = 0; j < data[k].cols(); j ++) - if(! fixed || (isfinite(data[k](i, j)) && ! isinf(data[k](i, j)) && ! isnan(data[k](i, j)))) { - if(! fixed) - MM = mm = data[k](i, j); - else { - MM = max(MM, data[k](i, j)); - mm = min(mm, data[k](i, j)); + for(int kk = 0; kk < data.size(); kk ++) + for(int k = 0; k < data[kk].size(); k ++) + for(int i = 0; i < data[kk][k].rows(); i ++) + for(int j = 0; j < data[kk][k].cols(); j ++) + if(! fixed || (isfinite(data[kk][k](i, j)) && + ! isinf(data[kk][k](i, j)) && ! isnan(data[kk][k](i, j)))) { + if(! fixed) + MM = mm = data[kk][k](i, j); + else { + MM = max(MM, data[kk][k](i, j)); + mm = min(mm, data[kk][k](i, j)); + } + fixed = true; } - fixed = true; - } if(MM == mm || ! fixed) return data; auto result(data); - for(int k = 0; k < data.size(); k ++) - for(int i = 0; i < data[k].rows(); i ++) - for(int j = 0; j < data[k].cols(); j ++) { - if(isfinite(result[k](i, j)) && ! isinf(data[k](i, j)) && ! isnan(result[k](i, j))) - result[k](i, j) -= mm; - else - result[k](i, j) = T(0); - assert(T(0) <= result[k](i, j) && result[k](i, j) <= MM - mm); - result[k](i, j) *= upper / (MM - mm); - } + for(int kk = 0; kk < data.size(); kk ++) + for(int k = 0; k < data[kk].size(); k ++) + for(int i = 0; i < data[kk][k].rows(); i ++) + for(int j = 0; j < data[kk][k].cols(); j ++) { + if(isfinite(result[k][k](i, j)) && ! isinf(data[kk][k](i, j)) && ! isnan(result[kk][k](i, j))) + result[kk][k](i, j) -= mm; + else + result[kk][k](i, j) = T(0); + assert(T(0) <= result[kk][k](i, j) && result[kk][k](i, j) <= MM - mm); + result[kk][k](i, j) *= upper / (MM - mm); + } return result; } +template static inline vector > normalize(const vector >& data, const T& upper = T(1)) { + vector > > w; + w.emplace_back(data); + return normalize(w, upper)[0]; +} + template static inline vector > autoLevel(const vector >& data, const int& count = 0) { vector res; res.reserve(data[0].rows() * data[0].cols() * data.size());