8 cost_func::cost_func(const char* param)
10 int len = strlen(param);
13 memcpy(tmp, param, len + 1);
14 degree = std::count(tmp, tmp + len, ',');
15 factor = new double[degree + 1];
16 for (int i = degree ; i > 0 ; i--) {
17 char* next = strchr(tmp, ',');
19 std::istringstream(tmp) >> factor[i];
22 std::istringstream(tmp) >> factor[0];
25 cost_func::~cost_func()
30 cost_func& cost_func::operator=(const cost_func& ref)
35 factor = new double[degree + 1];
36 memcpy(factor, ref.factor, (degree + 1) * sizeof *factor);
41 double cost_func::operator()(double amount) const
43 double result = factor[degree];
44 for (int i = degree - 1; i >= 0 ; i--)
45 result = amount * result + factor[i];
49 std::string cost_func::to_string()
51 std::ostringstream oss;
52 std::reverse_copy(factor + 1, factor + degree + 1,
53 std::ostream_iterator<double>(oss, ", "));