16 #ifndef CT_UTILITIES_H
17 #define CT_UTILITIES_H
30 template<
class T>
struct timesConstant :
public std::unary_function<T, double> {
67 inline doublereal
dot4(
const V& x,
const V& y)
69 return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3];
85 inline doublereal
dot5(
const V& x,
const V& y)
87 return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3] +
103 inline doublereal
dot6(
const V& x,
const V& y)
105 return x[0]*y[0] + x[1]*y[1] + x[2]*y[2] + x[3]*y[3] +
106 x[4]*y[4] + x[5]*y[5];
130 template<
class InputIter,
class InputIter2>
131 inline doublereal
dot(InputIter x_begin, InputIter x_end,
134 return inner_product(x_begin, x_end, y_begin, 0.0);
153 template<
class InputIter,
class OutputIter,
class S>
154 inline void scale(InputIter begin, InputIter end,
155 OutputIter out, S scale_factor)
191 template<
class InputIter,
class OutputIter,
class S>
193 OutputIter out, S scale_factor)
195 for (; begin != end; ++begin, ++out) {
196 *out += scale_factor * *begin;
226 template<
class InputIter,
class OutputIter>
230 for (; x_begin != x_end; ++x_begin, ++y_begin) {
231 *x_begin *= *y_begin;
262 template<
class InputIter>
265 for (; begin != end; ++begin) {
294 template<
class InputIter>
295 inline doublereal
absmax(InputIter begin, InputIter end)
297 doublereal
amax = 0.0;
298 for (; begin != end; ++begin)
299 if (fabs(*begin) > amax) {
333 template<
class InputIter,
class OutputIter>
337 doublereal sum = accumulate(begin, end, 0.0);
338 for (; begin != end; ++begin, ++out) {
368 template<
class InputIter,
class OutputIter>
372 for (; x_begin != x_end; ++x_begin, ++y_begin) {
373 *x_begin /= *y_begin;
389 template<
class InputIter,
class OutputIter>
390 inline void sum_each(OutputIter x_begin, OutputIter x_end,
393 for (; x_begin != x_end; ++x_begin, ++y_begin) {
394 *x_begin += *y_begin;
429 template<
class InputIter,
class OutputIter,
class IndexIter>
431 OutputIter result, IndexIter index)
433 for (; begin != end; ++begin, ++index) {
434 *(result + *index) = *begin;
465 template<
class InputIter,
class RandAccessIter,
class IndexIter>
467 RandAccessIter data, IndexIter index)
469 for (; mult_begin != mult_end; ++mult_begin, ++index) {
470 *(data + *index) *= *mult_begin;
500 template<
class InputIter,
class OutputIter,
class IndexIter>
502 OutputIter result, IndexIter index)
504 for (; begin != end; ++begin, ++index) {
505 *(result + *index) /= *begin;
523 template<
class InputIter>
524 inline doublereal
sum_xlogx(InputIter begin, InputIter end)
526 doublereal sum = 0.0;
527 for (; begin != end; ++begin) {
528 sum += (*begin) * std::log(*begin +
Tiny);
551 template<
class InputIter1,
class InputIter2>
552 inline doublereal
sum_xlogQ(InputIter1 begin, InputIter1 end,
555 doublereal sum = 0.0;
556 for (; begin != end; ++begin, ++Q_begin) {
557 sum += (*begin) * std::log(*Q_begin +
Tiny);
576 template<
class OutputIter>
577 inline void scale(
int N,
double alpha, OutputIter x)
579 scale(x, x+N, x, alpha);
588 template<
class D,
class R>
591 return ((((((c[6]*x + c[5])*x + c[4])*x + c[3])*x +
592 c[2])*x + c[1])*x + c[0]);
600 template<
class D,
class R>
603 return ((((((((c[8]*x + c[7])*x + c[6])*x + c[5])*x + c[4])*x + c[3])*x +
604 c[2])*x + c[1])*x + c[0]);
612 template<
class D,
class R>
615 return ((((((((((c[10]*x + c[9])*x + c[8])*x + c[7])*x
616 + c[6])*x + c[5])*x + c[4])*x + c[3])*x
617 + c[2])*x + c[1])*x + c[0]);
625 template<
class D,
class R>
628 return (((((c[5]*x + c[4])*x + c[3])*x +
629 c[2])*x + c[1])*x + c[0]);
637 template<
class D,
class R>
640 return ((((c[4]*x + c[3])*x +
641 c[2])*x + c[1])*x + c[0]);
649 template<
class D,
class R>
652 return (((c[3]*x + c[2])*x + c[1])*x + c[0]);
669 size_t is = toVec.size();
670 for (
size_t i = 0; i < is; i++) {
675 for (
size_t i = 0; i < is; i++) {
677 toVec[i] =
new D(*(fromVec[i]));
doublereal dot6(const V &x, const V &y)
Templated Inner product of two vectors of length 6.
R poly3(D x, R *c)
Templated evaluation of a polynomial of order 3.
void divide_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Templated divide of each element of x by the corresponding element of y.
timesConstant(T c)
Constructor.
void checkFinite(const double tmp)
Check to see that a number is finite (not NaN, +Inf or -Inf)
doublereal dot4(const V &x, const V &y)
Templated Inner product of two vectors of length 4.
R poly5(D x, R *c)
Templated evaluation of a polynomial of order 5.
R poly10(D x, R *c)
Templated evaluation of a polynomial of order 10.
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
Unary operator to multiply the argument by a constant.
double operator()(T x)
Parenthesis operator returning a double.
void multiply_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Multiply each entry in x by the corresponding entry in y.
void resize_each(int m, InputIter begin, InputIter end)
Invoke method 'resize' with argument m for a sequence of objects (templated version) ...
R poly4(D x, R *c)
Evaluates a polynomial of order 4.
void increment_scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
R poly8(D x, R *c)
Templated evaluation of a polynomial of order 8.
void normalize(InputIter begin, InputIter end, OutputIter out)
Normalize the values in a sequence, such that they sum to 1.0 (templated version) ...
R poly6(D x, R *c)
Templated evaluation of a polynomial of order 6.
void scatter_mult(InputIter mult_begin, InputIter mult_end, RandAccessIter data, IndexIter index)
Multiply selected elements in an array by a contiguous sequence of multipliers.
doublereal absmax(InputIter begin, InputIter end)
The maximum absolute value (templated version)
doublereal sum_xlogQ(InputIter1 begin, InputIter1 end, InputIter2 Q_begin)
Compute .
doublereal sum_xlogx(InputIter begin, InputIter end)
Compute .
doublereal dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
static size_t amax(double *x, size_t j, size_t n)
Finds the location of the maximum component in a vector x
void sum_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Increment each entry in x by the corresponding entry in y.
doublereal dot5(const V &x, const V &y)
Templated Inner product of two vectors of length 5.
void deepStdVectorPointerCopy(const std::vector< D * > &fromVec, std::vector< D * > &toVec)
Templated deep copy of a std vector of pointers.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
const doublereal Tiny
Small number to compare differences of mole fractions against.
void scatter_copy(InputIter begin, InputIter end, OutputIter result, IndexIter index)
Copies a contiguous range in a sequence to indexed positions in another sequence. ...
T m_c
Stored constant value of time T.
void scatter_divide(InputIter begin, InputIter end, OutputIter result, IndexIter index)
Divide selected elements in an array by a contiguous sequence of divisors.