9 cost_func::cost_func(const char* param)
11 int len = strlen(param);
14 memcpy(tmp, param, len + 1);
15 degree = std::count(tmp, tmp + len, ',');
16 factor = new double[degree + 1];
17 for (int i = degree ; i > 0 ; i--) {
18 char* next = strchr(tmp, ',');
20 std::istringstream(tmp) >> factor[i];
23 std::istringstream(tmp) >> factor[0];
26 cost_func::~cost_func()
31 cost_func& cost_func::operator=(const cost_func& ref)
36 factor = new double[degree + 1];
37 memcpy(factor, ref.factor, (degree + 1) * sizeof *factor);
42 double cost_func::operator()(double amount) const
44 double result = factor[degree];
45 for (int i = degree - 1; i >= 0 ; i--)
46 result = amount * result + factor[i];
50 std::string cost_func::to_string()
52 std::ostringstream oss;
53 std::reverse_copy(factor + 1, factor + degree + 1,
54 std::ostream_iterator<double>(oss, ", "));