ivl 679
|
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>. 00002 A C++ template library extending syntax towards mathematical notation. 00003 00004 Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr> 00005 Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr> 00006 00007 ivl is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License 00009 version 3 as published by the Free Software Foundation. 00010 00011 Alternatively, you can redistribute it and/or modify it under the terms 00012 of the GNU General Public License version 2 as published by the Free 00013 Software Foundation. 00014 00015 ivl is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00018 See the GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 and a copy of the GNU Lesser General Public License along 00022 with ivl. If not, see <http://www.gnu.org/licenses/>. */ 00023 00024 #ifndef IVL_ARRAY_DETAILS_ROW_FUNCTIONS_HPP 00025 #define IVL_ARRAY_DETAILS_ROW_FUNCTIONS_HPP 00026 00027 00028 namespace ivl { 00029 00030 template<class S, class T> 00031 class mean_elem_class 00032 { 00033 typename types::promote<S>::type sum; 00034 size_t len; 00035 00036 public: 00037 typedef row_func_details::length_first<false> behavior; 00038 00039 inline 00040 mean_class(size_t len) : sum(S()), len(len) { } 00041 00042 inline 00043 void elem_op(const S& elem) { sum += elem; } 00044 00045 inline 00046 T result() { return T(sum / double(len)); } 00047 }; 00048 00049 00050 template<class T, class S, class K> 00051 typename row_array_type< 00052 row_func_details::elem_row_class<mean_elem_class, T, 00053 types::to_floating<T>::type>::eval_type 00054 mean(const array<T, S, K>& a, size_t dim = 1) 00055 { 00056 return row_array_type< 00057 row_func_details::elem_row_class<mean_elem_class, T, 00058 types::to_floating<T>::type>::produce(a, dim); 00059 } 00060 /* 00061 array<T, S, K>, mean_elem_class, 00062 types::to_floating<T>::type>::eval_type 00063 mean(cost array<T, S, K>& a, size_t dim = 1) 00064 { 00065 return row_array_type<array<T, S, K>, mean_elem_class, 00066 types::to_floating<T>::type>::produce(a, dim); 00067 } 00068 */ 00069 00070 #if 0 00071 00072 row_func_details::elem_row_class<mean_elem_class, S, 00073 typename types::to_floating<S>::type> 00074 00075 template<class A, class S> 00076 class mean_class : 00077 public row_array_type< 00078 row_func_details::elem_row_class<mean_elem_class, S, 00079 typename types::to_floating<S>::type>, A> 00080 { 00081 public: 00082 mean_class(internal::tuple<A, size_t> p) { init_input(p); } 00083 }; 00084 00085 // function declarations for array, array_nd and array_2d 00086 template<class T, class S, class K> 00087 typename mean_class<array<T, S, K>, T>::elem_type mean( 00088 const array<T, S, K>& a) 00089 { 00090 return mean_class<array<T, S, K>, T>::calculate(a); 00091 } 00092 00093 template<class T, class S, class K> 00094 mean_class<array_nd<T, S, K>, T> mean( 00095 const array_nd<T, S, K>& a, size_t dim = 1) 00096 { 00097 return internal::tpl(internal::ref(a), dim); 00098 } 00099 00100 template<class T, class S, class K> 00101 mean_class<array_2d<T, S, K>, T> mean( 00102 const array_2d<T, S, K>& a, size_t dim = 1) 00103 { 00104 return internal::tpl(internal::ref(a), dim); 00105 } 00106 00107 00108 #endif 00109 00110 // -------------------------------------------------------- 00111 00112 template<class S, class T> 00113 class min_elem_class 00114 { 00115 private: 00116 T best; 00117 00118 public: 00119 typedef row_func_details::first_elem_first<false> behavior; 00120 00121 inline 00122 min_class(const T& i) : best(i) { } 00123 00124 inline 00125 void elem_op(const S& elem) { if(elem < best) best = elem; }; 00126 00127 inline 00128 T result() { return best; } 00129 }; 00130 00131 template<class S, class T, class I> 00132 class min_elem_class<S, internal::tuple<T, I> > 00133 { 00134 private: 00135 T best; 00136 I best_idx; 00137 00138 public: 00139 typedef row_func_details::first_elem_first<true> behavior; 00140 00141 inline 00142 min_class(const T& i) : best(i), best_idx(0) { } 00143 00144 inline 00145 void elem_op(const S& elem, I index) 00146 { 00147 if(elem < best) { best = elem; best_idx = index; } 00148 }; 00149 00150 inline 00151 internal::tuple<T, I> result() { return internal::tpl(best, best_idx); } 00152 }; 00153 00154 00155 template<class A, class S> 00156 class min_class : 00157 public row_array_type< 00158 row_func_details::elem_row_class<mean_elem_class, S, S, A> 00159 { 00160 public: 00161 min_class(internal::tuple<A, size_t> p) { init_input(p); } 00162 }; 00163 00164 // function declarations for array, array_nd and array_2d 00165 template<class T, class S, class K> 00166 mean_class<array<T, S, K>, T> mean(const array<T, S, K>& a) 00167 { 00168 return internal::tpl(a, 1); 00169 } 00170 00171 template<class T, class S, class K> 00172 mean_class<array_nd<T, S, K>, T> mean( 00173 const array_nd<T, S, K>& a, size_t dim = 1) 00174 { 00175 return internal::tpl(a, dim); 00176 } 00177 00178 template<class T, class S, class K> 00179 mean_class<array_2d<T, S, K>, T> mean( 00180 const array_2d<T, S, K>& a, size_t dim = 1) 00181 { 00182 return internal::tpl(a, dim); 00183 } 00184 00185 00186 00187 00188 //template<class S, class T> 00189 //class min_row_class : public elem_row_class<min_elem_class, S, T, size_t> 00190 //{ 00191 // 00192 //}; 00193 00194 template<class IN> 00195 class min_class : public 00196 row_array_type<IN, elem_row_class<IN::elem_type, min_elem_class 00197 00198 00199 template<class T, class S, class K> 00200 class min_class : public row_array_type<T, S, K, min_class> 00201 { 00202 private: 00203 const array<T, S, K>& in; 00204 00205 public: 00206 00207 template<class L, class F> 00208 void output(array<T, L, F>& e) const; 00209 00210 // [v,i] = min(... 00211 template<class L, class F, class M, class N> 00212 void output(array<T, L, F>& v, 00213 array<size_t, M, N>& d) const; 00214 00215 00216 }; 00217 00218 // single type 00219 typename row_array_type<T, S, K, min_class>::type min(const array<T, S, K>& a) 00220 { 00221 } 00222 00223 rvalue_default<typename row_array_type<T, S, K, min_class>::type>, > 00224 00225 00226 00227 00228 // 00229 typename row_ 00230 00231 00232 00233 00234 } /* namespace ivl */ 00235 00236 00237 #endif // IVL_ARRAY_DETAILS_ROW_FUNCTIONS_HPP