Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 00025 00026 #ifndef MLN_ACCU_RMS_HH 00027 # define MLN_ACCU_RMS_HH 00028 00032 00033 # include <mln/core/concept/meta_accumulator.hh> 00034 # include <mln/accu/internal/base.hh> 00035 # include <mln/math/sqrt.hh> 00036 00037 00038 namespace mln 00039 { 00040 00041 namespace accu 00042 { 00043 00044 00050 // 00051 template <typename T, typename V> 00052 struct rms : public mln::accu::internal::base<V, rms<T,V> > 00053 { 00054 typedef T argument; 00055 00056 rms(); 00057 00060 void init(); 00061 void take_as_init_(const T& p); 00062 void take(const T& p); 00063 void take(const rms<T,V>& other); 00065 00067 V to_result() const; 00068 00071 V hook_value_() const; 00072 00075 bool is_valid() const; 00076 00077 protected: 00078 00079 V v_; 00080 unsigned count_; 00081 }; 00082 00083 00084 namespace meta 00085 { 00086 00088 struct rms : public Meta_Accumulator< rms > 00089 { 00090 template <typename T, typename V> 00091 struct with 00092 { 00093 typedef accu::rms<T,V> ret; 00094 }; 00095 }; 00096 00097 } // end of namespace mln::accu::meta 00098 00099 00100 # ifndef MLN_INCLUDE_ONLY 00101 00102 template <typename T, typename V> 00103 inline 00104 rms<T,V>::rms() 00105 { 00106 init(); 00107 } 00108 00109 template <typename T, typename V> 00110 inline 00111 void 00112 rms<T,V>::init() 00113 { 00114 v_ = literal::zero; 00115 count_ = 0; 00116 } 00117 00118 template <typename T, typename V> 00119 inline 00120 void 00121 rms<T,V>::take_as_init_(const T& t) 00122 { 00123 v_ += t * t; 00124 ++count_; 00125 } 00126 00127 template <typename T, typename V> 00128 inline 00129 void 00130 rms<T,V>::take(const T& t) 00131 { 00132 v_ += t * t; 00133 ++count_; 00134 } 00135 00136 template <typename T, typename V> 00137 inline 00138 void 00139 rms<T,V>::take(const rms<T,V>& other) 00140 { 00141 v_ += other.v_; 00142 count_ += other.count_; 00143 } 00144 00145 template <typename T, typename V> 00146 inline 00147 V 00148 rms<T,V>::to_result() const 00149 { 00150 if (count_ == 0) 00151 return V(0); 00152 return math::sqrt<V>(v_ / count_); 00153 } 00154 00155 template <typename T, typename V> 00156 inline 00157 V 00158 rms<T,V>::hook_value_() const 00159 { 00160 return v_; 00161 } 00162 00163 template <typename T, typename V> 00164 inline 00165 bool 00166 rms<T,V>::is_valid() const 00167 { 00168 return true; 00169 } 00170 00171 # endif // ! MLN_INCLUDE_ONLY 00172 00173 } // end of namespace mln::accu 00174 00175 } // end of namespace mln 00176 00177 00178 #endif // ! MLN_ACCU_RMS_HH