| 1 |
tim |
1744 |
/* |
| 2 |
|
|
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
| 3 |
|
|
* |
| 4 |
|
|
* Contact: oopse@oopse.org |
| 5 |
|
|
* |
| 6 |
|
|
* This program is free software; you can redistribute it and/or |
| 7 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
|
|
* as published by the Free Software Foundation; either version 2.1 |
| 9 |
|
|
* of the License, or (at your option) any later version. |
| 10 |
|
|
* All we ask is that proper credit is given for our work, which includes |
| 11 |
|
|
* - but is not limited to - adding the above copyright notice to the beginning |
| 12 |
|
|
* of your source code files, and to any copyright notice that you may distribute |
| 13 |
|
|
* with programs based on this work. |
| 14 |
|
|
* |
| 15 |
|
|
* This program is distributed in the hope that it will be useful, |
| 16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
|
|
* GNU Lesser General Public License for more details. |
| 19 |
|
|
* |
| 20 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
|
|
* along with this program; if not, write to the Free Software |
| 22 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 23 |
|
|
* |
| 24 |
|
|
*/ |
| 25 |
|
|
|
| 26 |
|
|
/** |
| 27 |
|
|
* @file Polynomial.hpp |
| 28 |
|
|
* @author tlin |
| 29 |
|
|
* @date 11/01/2004 |
| 30 |
|
|
* @version 1.0 |
| 31 |
|
|
*/ |
| 32 |
|
|
|
| 33 |
|
|
#ifndef MATH_POLYNOMIAL_HPP |
| 34 |
|
|
#define MATH_POLYNOMIAL_HPP |
| 35 |
|
|
|
| 36 |
|
|
#include <list> |
| 37 |
|
|
#include <utility> |
| 38 |
|
|
|
| 39 |
|
|
namespace oopse { |
| 40 |
|
|
|
| 41 |
|
|
template<typename ElemType, int N> pow(ElemType x) { |
| 42 |
|
|
ElemType result(1); |
| 43 |
|
|
|
| 44 |
|
|
for (int i = 0; i < N; ++i) { |
| 45 |
|
|
result *= x; |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
|
|
return result; |
| 49 |
|
|
} |
| 50 |
|
|
|
| 51 |
|
|
|
| 52 |
|
|
template<typename ElemType> |
| 53 |
|
|
class Polynomial { |
| 54 |
|
|
|
| 55 |
|
|
public: |
| 56 |
|
|
|
| 57 |
|
|
typedef int ExponentType; |
| 58 |
|
|
typedef ElemType ConstantType; |
| 59 |
|
|
typedef std::map<ExponentType, ConstantType> PolynomialPairMap; |
| 60 |
|
|
typedef PolynomialPairMap::iterator PolynomialIterator; |
| 61 |
|
|
|
| 62 |
|
|
Polynomial(); |
| 63 |
|
|
|
| 64 |
|
|
template<U> Polynomial(const Polynomial<U>& p); |
| 65 |
|
|
template<U> Polynomial& operator=(const Polynomial<U>& p); |
| 66 |
|
|
|
| 67 |
|
|
/** |
| 68 |
|
|
* |
| 69 |
|
|
*/ |
| 70 |
|
|
ElemType evaluate(const ElemType& x) { |
| 71 |
|
|
ElemType result; |
| 72 |
|
|
double exponent; |
| 73 |
|
|
double constant; |
| 74 |
|
|
|
| 75 |
|
|
for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
| 76 |
|
|
exponent = i->first; |
| 77 |
|
|
constant = i->second; |
| 78 |
|
|
result += pow<exponent>(x) * constant; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
return result; |
| 82 |
|
|
} |
| 83 |
|
|
|
| 84 |
|
|
ElemType evaluateFirstDerivative(const ElemType& x) { |
| 85 |
|
|
ElemType result; |
| 86 |
|
|
double exponent; |
| 87 |
|
|
double constant; |
| 88 |
|
|
|
| 89 |
|
|
for (PolynomialIterator i = polyPairMap_.begin(); i != polyPairMap_.end(); ++i) { |
| 90 |
|
|
exponent = i->first; |
| 91 |
|
|
constant = i->second; |
| 92 |
|
|
result += pow<exponent - 1>(x) * constant * exponent; |
| 93 |
|
|
} |
| 94 |
|
|
|
| 95 |
|
|
return result; |
| 96 |
|
|
} |
| 97 |
|
|
|
| 98 |
|
|
void addPolynomialTerm(int exponent, const ElemType& constant) { |
| 99 |
|
|
|
| 100 |
|
|
} |
| 101 |
|
|
|
| 102 |
|
|
bool getConstant(ExponentType exponent, & constant) { |
| 103 |
|
|
|
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
|
| 107 |
|
|
PolynomialIterator begin() { |
| 108 |
|
|
return polyPairMap_.begin(); |
| 109 |
|
|
} |
| 110 |
|
|
|
| 111 |
|
|
PolynomialIterator end() { |
| 112 |
|
|
return polyPairMap_.end(); |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
PolynomialIterator find(ExponentType exponent) { |
| 116 |
|
|
return polyPairMap_.find(); |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
private: |
| 120 |
|
|
|
| 121 |
|
|
PolynomialPairMap polyPairMap_; |
| 122 |
|
|
}; |
| 123 |
|
|
|
| 124 |
|
|
} //end namespace oopse |
| 125 |
|
|
#endif //MATH_POLYNOMIAL_HPP |