16 #ifndef CT_UTILITIES_H
17 #define CT_UTILITIES_H
28 template<
class T>
struct timesConstant :
public std::unary_function<T, double> {
68 inline doublereal
dot4(
const V& x,
const V& y)
70 return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3];
86 inline doublereal
dot5(
const V& x,
const V& y)
88 return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3] +
104 inline doublereal
dot6(
const V& x,
const V& y)
106 return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3] +
107 x[4]*y[4] + x[5]*y[5];
131 template<
class InputIter,
class InputIter2>
132 inline doublereal
dot(InputIter x_begin, InputIter x_end,
135 return inner_product(x_begin, x_end, y_begin, 0.0);
154 template<
class InputIter,
class OutputIter,
class S>
155 inline void scale(InputIter begin, InputIter end,
156 OutputIter out, S scale_factor)
192 template<
class InputIter,
class OutputIter,
class S>
194 OutputIter out, S scale_factor)
196 for (; begin != end; ++begin, ++out) {
197 *out += scale_factor * *begin;
227 template<
class InputIter,
class OutputIter>
231 for (; x_begin != x_end; ++x_begin, ++y_begin) {
232 *x_begin *= *y_begin;
263 template<
class InputIter>
266 for (; begin != end; ++begin) {
295 template<
class InputIter>
296 inline doublereal
absmax(InputIter begin, InputIter end)
298 doublereal
amax = 0.0;
299 for (; begin != end; ++begin)
300 if (fabs(*begin) > amax) {
334 template<
class InputIter,
class OutputIter>
338 doublereal sum = accumulate(begin, end, 0.0);
339 for (; begin != end; ++begin, ++out) {
369 template<
class InputIter,
class OutputIter>
373 for (; x_begin != x_end; ++x_begin, ++y_begin) {
374 *x_begin /= *y_begin;
390 template<
class InputIter,
class OutputIter>
391 inline void sum_each(OutputIter x_begin, OutputIter x_end,
394 for (; x_begin != x_end; ++x_begin, ++y_begin) {
395 *x_begin += *y_begin;
430 template<
class InputIter,
class OutputIter,
class IndexIter>
432 OutputIter result, IndexIter index)
434 for (; begin != end; ++begin, ++index) {
435 *(result + *index) = *begin;
466 template<
class InputIter,
class RandAccessIter,
class IndexIter>
468 RandAccessIter data, IndexIter index)
470 for (; mult_begin != mult_end; ++mult_begin, ++index) {
471 *(data + *index) *= *mult_begin;
501 template<
class InputIter,
class OutputIter,
class IndexIter>
503 OutputIter result, IndexIter index)
505 for (; begin != end; ++begin, ++index) {
506 *(result + *index) /= *begin;
524 template<
class InputIter>
525 inline doublereal
sum_xlogx(InputIter begin, InputIter end)
527 doublereal sum = 0.0;
528 for (; begin != end; ++begin) {
529 sum += (*begin) * std::log(*begin +
Tiny);
552 template<
class InputIter1,
class InputIter2>
553 inline doublereal
sum_xlogQ(InputIter1 begin, InputIter1 end,
556 doublereal sum = 0.0;
557 for (; begin != end; ++begin, ++Q_begin) {
558 sum += (*begin) * std::log(*Q_begin +
Tiny);
577 template<
class OutputIter>
578 inline void scale(
int N,
double alpha, OutputIter x)
580 scale(x, x+N, x, alpha);
589 template<
class D,
class R>
592 return ((((((c[6]*x + c[5])*x + c[4])*x + c[3])*x +
593 c[2])*x + c[1])*x + c[0]);
601 template<
class D,
class R>
604 return ((((((((c[8]*x + c[7])*x + c[6])*x + c[5])*x + c[4])*x + c[3])*x +
605 c[2])*x + c[1])*x + c[0]);
613 template<
class D,
class R>
616 return ((((((((((c[10]*x + c[9])*x + c[8])*x + c[7])*x
617 + c[6])*x + c[5])*x + c[4])*x + c[3])*x
618 + c[2])*x + c[1])*x + c[0]);
626 template<
class D,
class R>
629 return (((((c[5]*x + c[4])*x + c[3])*x +
630 c[2])*x + c[1])*x + c[0]);
638 template<
class D,
class R>
641 return ((((c[4]*x + c[3])*x +
642 c[2])*x + c[1])*x + c[0]);
650 template<
class D,
class R>
653 return (((c[3]*x + c[2])*x + c[1])*x + c[0]);
670 size_t is = toVec.size();
671 for (
size_t i = 0; i < is; is++) {
678 for (
size_t i = 0; i < is; is++) {
679 toVec[i] =
new D(*(fromVec[i]));