00001 /* 00002 liban - base function to do usefull things related to simulation and scientific work 00003 Copyright (C) 1999-2005 Stephane Magnenat <stephane at magnenat dot net> 00004 Copyright (c) 2004-2005 Antoine Beyeler <antoine dot beyeler at epfl dot ch> 00005 Copyright (C) 2005 Laboratory of Intelligent Systems, EPFL, Lausanne 00006 See AUTHORS for details 00007 00008 This program is free software. You can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #ifndef __AN_NUMERIC_H 00024 #define __AN_NUMERIC_H 00025 00030 namespace An 00031 { 00037 class ExpDecay 00038 { 00039 public: 00044 ExpDecay(double t = 5.0, double delta = 1.0, double init = 0.0) 00045 { 00046 tau = t; 00047 dt = delta; 00048 value = init; 00049 syncFactor(); 00050 } 00051 00052 // Operations. 00054 void add(double f) { value += f; } 00056 void step() { value *= factor; } 00059 void step(double thisDt) { setDt(thisDt); step(); } 00061 double getValueAndStep() { double tmp = getValue(); step(); return tmp; } 00064 double getValueAndStep(double thisDt) { setDt(thisDt); return getValueAndStep(); } 00065 00066 // Accessors. 00069 void setTau(double t) { if (t != tau) { tau = t; syncFactor(); } } 00071 double getTau() const { return tau; } 00074 void setDt(double delta) { if (delta != dt) { dt = delta; syncFactor(); } } 00076 double getDt() const { return dt; } 00079 void setValue(double val) { value = val; } 00081 double getValue() const { return value; } 00082 00083 // Operators. 00086 void operator = (double val) { setValue(val); } 00088 void operator += (double f) { add(f); } 00090 void operator -= (double f) { add(-f); } 00092 operator double() const { return getValue(); } 00093 00094 protected: 00095 double tau; 00096 double factor; 00097 double value; 00098 double dt; 00099 00101 void syncFactor() { factor = (tau - dt * 0.5) / (tau + dt * 0.5); } 00102 }; 00103 } 00104 00105 #endif