Cantera  2.2.1
vec_functions.h
Go to the documentation of this file.
1 /**
2  * @file vec_functions.h
3  * Templates for operations on vector-like objects.
4  */
5 /*
6  * Copyright 2001 California Institute of Technology
7  */
8
9 #ifndef CT_VEC_FUNCTIONS_H
10 #define CT_VEC_FUNCTIONS_H
11
12 #include "ct_defs.h"
13 #include "utilities.h"
14 #include <functional>
15 #include <iostream>
16 #include <cstring>
17
18 namespace Cantera
19 {
20 //! Templated function that copies the first n entries from x to y.
21 /*!
22  *
23  *
24  * The templated type is the type of x and y
25  *
26  * @param n Number of elements to copy from x to y
27  * @param x The object x, of templated type const T&
28  * @param y The object y, of templated type T&
29  * @deprecated Unused. To be removed after Cantera 2.2.
30  */
31 template<class T>
32 inline void copyn(size_t n, const T& x, T& y)
33 {
34  warn_deprecated("copyn", "To be removed after Cantera 2.2.");
35  std::copy(x.begin(), x.begin() + n, y.begin());
36 }
37
38 //! Divide each element of x by the corresponding element of y.
39 /*!
40  * This function replaces x[n] by x[n]/y[n], for 0 <= n < x.size()
41  *
42  * @param x Numerator object of the division operation with template type T
43  * At the end of the calculation, it contains the result.
44  * @param y Denominator object of the division template type T
45  * @deprecated Unused. To be removed after Cantera 2.2.
46  */
47 template<class T>
48 inline void divide_each(T& x, const T& y)
49 {
50  warn_deprecated("divide_each", "To be removed after Cantera 2.2.");
51  std::transform(x.begin(), x.end(), y.begin(),
52  x.begin(), std::divides<typename T::value_type>());
53 }
54
55 //! Multiply each element of x by the corresponding element of y.
56 /*!
57  * This function replaces x[n] by x[n]*y[n], for 0 <= n < x.size()
58  * This is a templated function with just one template type.
59  *
60  * @param x First object of the multiplication with template type T
61  * At the end of the calculation, it contains the result.
62  * @param y Second object of the multiplication with template type T
63  * @deprecated Unused. To be removed after Cantera 2.2.
64  */
65 template<class T>
66 inline void multiply_each(T& x, const T& y)
67 {
68  warn_deprecated("multiply_each", "To be removed after Cantera 2.2.");
69  std::transform(x.begin(), x.end(), y.begin(),
70  x.begin(), std::multiplies<typename T::value_type>());
71 }
72
73 //! Multiply each element of x by scale_factor.
74 /*!
75  * This function replaces x[n] by x[n]*scale_factor, for 0 <= n < x.size()
76  *
77  * @param x First object of the multiplication with template type T
78  * At the end of the calculation, it contains the result.
79  * @param scale_factor scale factor with template type S
80  * @deprecated Unused. To be removed after Cantera 2.2.
81  */
82 template<class T, class S>
83 inline void scale(T& x, S scale_factor)
84 {
85  warn_deprecated("scale", "To be removed after Cantera 2.2.");
86  scale(x.begin(), x.end(), x.begin(), scale_factor);
87 }
88
89 //! Return the templated dot product of two objects
90 /*!
91  * Returns the sum of x[n]*y[n], for 0 <= n < x.size().
92  *
93  * @param x First object of the dot product with template type T
94  * At the end of the calculation, it contains the result.
95  * @param y Second object of the dot product with template type T
96  * @deprecated Unused. To be removed after Cantera 2.2.
97  */
98 template<class T>
99 inline doublereal dot_product(const T& x, const T& y)
100 {
101  warn_deprecated("dot_product", "To be removed after Cantera 2.2.");
102  return std::inner_product(x.begin(), x.end(), y.begin(), 0.0);
103 }
104
105 //! Returns the templated dot ratio of two objects
106 /**
107  * Returns the sum of x[n]/y[n], for 0 <= n < x.size().
108  *
109  * @param x First object of the dot product with template type T
110  * At the end of the calculation, it contains the result.
111  * @param y Second object of the dot product with template type T
112  * @deprecated Unused. To be removed after Cantera 2.2.
113  */
114 template<class T>
115 inline doublereal dot_ratio(const T& x, const T& y)
116 {
117  warn_deprecated("dot_ratio", "To be removed after Cantera 2.2.");
118  return _dot_ratio(x.begin(), x.end(), y.begin(), 0.0);
119 }
120
121 //! Returns a templated addition operation of two objects
122 /**
123  * Replaces x[n] by x[n] + y[n] for 0 <= n < x.size()
124  *
125  * @param x First object of the addition with template type T
126  * At the end of the calculation, it contains the result.
127  * @param y Second object of the addition with template type T
128  * @deprecated Unused. To be removed after Cantera 2.2.
129  */
130 template<class T>
131 inline void add_each(T& x, const T& y)
132 {
133  warn_deprecated("add_each", "To be removed after Cantera 2.2.");
134  std::transform(x.begin(), x.end(), y.begin(),
135  x.begin(), std::plus<typename T::value_type>());
136 }
137
138 //! Templated dot ratio class
139 /*!
140  * Calculates the quantity:
141  *
142  * S += x[n]/y[n]
143  *
144  * The first templated type is the iterator type for x[] and y[].
145  * The second templated type is the type of S.
146  *
147  * @param x_begin InputIter type, indicating the address of the
148  * first element of x
149  * @param x_end InputIter type, indicating the address of the
150  * last element of x
151  * @param y_begin InputIter type, indicating the address of the
152  * first element of y
153  * @param start_value S type, indicating the type of the
154  * accumulation result.
155  * @deprecated Unused. To be removed after Cantera 2.2.
156  */
157 template<class InputIter, class S>
158 inline doublereal _dot_ratio(InputIter x_begin, InputIter x_end,
159  InputIter y_begin, S start_value)
160 {
161  warn_deprecated("_dot_ratio", "To be removed after Cantera 2.2.");
162  for (; x_begin != x_end; ++x_begin, ++y_begin) {
163  start_value += *x_begin / *y_begin;
164  }
165  return start_value;
166 }
167
168 //! Finds the entry in a vector with maximum absolute
169 //! value, and return this value.
170 /*!
171  * @param v Vector to be queried for maximum value, with template type T
172  *
173  * @return Returns an object of type T that is the maximum value,
174  * @deprecated Unused. To be removed after Cantera 2.2.
175  */
176 template<class T>
177 inline T absmax(const std::vector<T>& v)
178 {
179  warn_deprecated("absmax", "To be removed after Cantera 2.2.");
180  int n = v.size();
181  T maxval = 0.0;
182  for (int i = 0; i < n; i++) {
183  maxval = std::max(std::abs(v[i]), maxval);
184  }
185  return maxval;
186 }
187
188 //! Write a vector to a stream
189 template <class T>
190 inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v)
191 {
192  size_t n = v.size();
193  for (size_t i = 0; i < n; i++) {
194  os << v[i];
195  if (i != n-1) {
196  os << ", ";
197  }
198  }
199  return os;
200 }
201
202 }
203
204 #endif
doublereal _dot_ratio(InputIter x_begin, InputIter x_end, InputIter y_begin, S start_value)
Templated dot ratio class.
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.
Definition: utilities.h:378
void add_each(T &x, const T &y)
Returns a templated addition operation of two objects.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...
This file contains definitions of terms that are used in internal routines and are unlikely to need m...
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
void multiply_each(OutputIter x_begin, OutputIter x_end, InputIter y_begin)
Multiply each entry in x by the corresponding entry in y.
Definition: utilities.h:234
doublereal absmax(InputIter begin, InputIter end)
The maximum absolute value (templated version)
Definition: utilities.h:305
void copyn(size_t n, const T &x, T &y)
Templated function that copies the first n entries from x to y.
Definition: vec_functions.h:32
doublereal dot_ratio(const T &x, const T &y)
Returns the templated dot ratio of two objects.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:158
doublereal dot_product(const T &x, const T &y)
Return the templated dot product of two objects.
Definition: vec_functions.h:99