Chemical Data Processing Library C++ API - Version 1.4.0
MinimizerVariableArrayTraits.hpp
Go to the documentation of this file.
1 /*
2  * MinimizerVariableArrayTraits.hpp
3  *
4  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
28 #ifndef CDPL_MATH_MINIMIZERVARIABLEARRAYTRAITS_HPP
29 #define CDPL_MATH_MINIMIZERVARIABLEARRAYTRAITS_HPP
30 
31 #include <vector>
32 
34 #include "CDPL/Math/TypeTraits.hpp"
35 
36 
37 namespace CDPL
38 {
39 
40  namespace Math
41  {
42 
53  template <typename A>
55  {
56 
60  typedef A ArrayType;
61 
65  typedef typename A::ValueType ValueType;
66 
70  typedef typename A::SizeType SizeType;
71 
79  template <typename T>
80  static T dot(const ArrayType& a1, const ArrayType& a2)
81  {
82  return innerProd(a1, a2);
83  }
84 
91  template <typename T>
92  static T norm2(const ArrayType& a)
93  {
94  T scale = T();
95  T ssq = T(1);
96  SizeType size = a.getSize();
97 
98  if (size == SizeType(0))
99  return T();
100 
101  else if (size == SizeType(1))
102  return TypeTraits<ValueType>::abs(a(0));
103 
104  for (SizeType i = 0; i < size; i++) {
105  const ValueType& x = a(i);
106 
107  if (x != ValueType()) {
109 
110  if (scale < ax) {
111  ssq = 1 + ssq * (scale / ax) * (scale / ax);
112  scale = ax;
113 
114  } else {
115  ssq += (ax / scale) * (ax / scale);
116  }
117  }
118  }
119 
120  return (scale * TypeTraits<T>::sqrt(ssq));
121  }
122 
130  template <typename T>
131  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
132  {
133  y.plusAssign(alpha * x);
134  }
135 
140  static void clear(ArrayType& a)
141  {
142  a.clear(ValueType());
143  }
144 
150  static void assign(ArrayType& a1, const ArrayType& a2)
151  {
152  a1.assign(a2);
153  }
154 
161  template <typename T>
162  static void multiply(ArrayType& a, const T& v)
163  {
164  a *= v;
165  }
166 
172  static void sub(ArrayType& a1, const ArrayType& a2)
173  {
174  a1.minusAssign(a2);
175  }
176  };
177 
182  template <typename V>
184  {
185 
190 
194  typedef V VectorType;
195 
199  typedef typename V::ValueType ValueType;
200 
204  typedef typename ArrayType::SizeType SizeType;
205 
213  template <typename T>
214  static T dot(const ArrayType& a1, const ArrayType& a2)
215  {
216  T result = T();
217 
218  for (typename ArrayType::ConstElementIterator it1 = a1.getElementsBegin(), it2 = a2.getElementsBegin(), end1 = a1.getElementsEnd(); it1 != end1; ++it1, ++it2)
219  result += innerProd(*it1, *it2);
220 
221  return result;
222  }
223 
230  template <typename T>
231  static T norm2(const ArrayType& a)
232  {
233  T scale = T();
234  T ssq = T(1);
235 
236  for (typename ArrayType::ConstElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it) {
237  const typename VectorType::ConstPointer vx = it->getData();
238  typename VectorType::SizeType dim = it->getSize();
239 
240  for (typename VectorType::SizeType i = 0; i < dim; i++) {
241  const ValueType& x = vx[i];
242 
243  if (x != ValueType()) {
245 
246  if (scale < ax) {
247  ssq = 1 + ssq * (scale / ax) * (scale / ax);
248  scale = ax;
249 
250  } else {
251  ssq += (ax / scale) * (ax / scale);
252  }
253  }
254  }
255  }
256 
257  return (scale * TypeTraits<T>::sqrt(ssq));
258  }
259 
267  template <typename T>
268  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
269  {
270  typename ArrayType::ElementIterator it2 = y.getElementsBegin();
271  VectorType tmp;
272 
273  for (typename ArrayType::ConstElementIterator it1 = x.getElementsBegin(), end1 = x.getElementsEnd(); it1 != end1; ++it1, ++it2) {
274  tmp.assign(*it1);
275  tmp *= alpha;
276 
277  it2->plusAssign(tmp);
278  }
279  }
280 
285  static void clear(ArrayType& a)
286  {
287  for (typename ArrayType::ElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it)
288  it->clear(ValueType());
289  }
290 
296  static void assign(ArrayType& a1, const ArrayType& a2)
297  {
298  a1 = a2;
299  }
300 
307  template <typename T>
308  static void multiply(ArrayType& a, const T& v)
309  {
310  for (typename ArrayType::ElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it)
311  *it *= v;
312  }
313 
319  static void sub(ArrayType& a1, const ArrayType& a2)
320  {
322 
323  for (typename ArrayType::ElementIterator it1 = a1.getElementsBegin(), end1 = a1.getElementsEnd(); it1 != end1; ++it1, ++it2)
324  it1->minusAssign(*it2);
325  }
326  };
327 
332  template <typename V>
333  struct MinimizerVariableArrayTraits<std::vector<V> >
334  {
335 
339  typedef std::vector<V> ArrayType;
340 
344  typedef V VectorType;
345 
349  typedef typename V::ValueType ValueType;
350 
354  typedef typename ArrayType::size_type SizeType;
355 
363  template <typename T>
364  static T dot(const ArrayType& a1, const ArrayType& a2)
365  {
366  T result = T();
367 
368  for (typename ArrayType::const_iterator it1 = a1.begin(), it2 = a2.begin(), end1 = a1.end(); it1 != end1; ++it1, ++it2)
369  result += innerProd(*it1, *it2);
370 
371  return result;
372  }
373 
380  template <typename T>
381  static T norm2(const ArrayType& a)
382  {
383  T scale = T();
384  T ssq = T(1);
385 
386  for (typename ArrayType::const_iterator it = a.begin(), end = a.end(); it != end; ++it) {
387  const typename VectorType::ConstPointer vx = it->getData();
388  typename VectorType::SizeType dim = it->getSize();
389 
390  for (typename VectorType::SizeType i = 0; i < dim; i++) {
391  const ValueType& x = vx[i];
392 
393  if (x != ValueType()) {
395 
396  if (scale < ax) {
397  ssq = 1 + ssq * (scale / ax) * (scale / ax);
398  scale = ax;
399 
400  } else {
401  ssq += (ax / scale) * (ax / scale);
402  }
403  }
404  }
405  }
406 
407  return (scale * TypeTraits<T>::sqrt(ssq));
408  }
409 
417  template <typename T>
418  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
419  {
420  typename ArrayType::iterator it2 = y.begin();
421  VectorType tmp;
422 
423  for (typename ArrayType::const_iterator it1 = x.begin(), end1 = x.end(); it1 != end1; ++it1, ++it2) {
424  tmp.assign(*it1);
425  tmp *= alpha;
426 
427  it2->plusAssign(tmp);
428  }
429  }
430 
435  static void clear(ArrayType& a)
436  {
437  for (typename ArrayType::iterator it = a.begin(), end = a.end(); it != end; ++it)
438  it->clear(ValueType());
439  }
440 
446  static void assign(ArrayType& a1, const ArrayType& a2)
447  {
448  a1 = a2;
449  }
450 
457  template <typename T>
458  static void multiply(ArrayType& a, const T& v)
459  {
460  for (typename ArrayType::iterator it = a.begin(), end = a.end(); it != end; ++it)
461  *it *= v;
462  }
463 
469  static void sub(ArrayType& a1, const ArrayType& a2)
470  {
471  typename ArrayType::const_iterator it2 = a2.begin();
472 
473  for (typename ArrayType::iterator it1 = a1.begin(), end1 = a1.end(); it1 != end1; ++it1, ++it2)
474  it1->minusAssign(*it2);
475  }
476  };
477 
478  } // namespace Math
479 } // namespace CDPL
480 
481 #endif // CDPL_MATH_MINIMIZERVARIABLEARRAYTRAITS_HPP
Definition of type traits.
Definition of class CDPL::Math::VectorArray.
Array data type for the ordered storage of vector objects.
Definition: VectorArray.hpp:49
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:128
ConstElementIterator getElementsEnd() const
Returns a constant iterator pointing to the end of the array.
Definition: Array.hpp:907
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:140
ConstElementIterator getElementsBegin() const
Returns a constant iterator pointing to the beginning of the array.
Definition: Array.hpp:895
std::size_t SizeType
An unsigned integral type used to represent sizes and indices.
Definition: Array.hpp:113
constexpr unsigned int A
Generic type that covers any element except hydrogen.
Definition: AtomType.hpp:637
constexpr unsigned int V
Specifies Vanadium.
Definition: AtomType.hpp:177
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
VectorInnerProduct< E1, E2 >::ResultType innerProd(const VectorExpression< E1 > &e1, const VectorExpression< E2 > &e2)
Returns the inner (dot) product of the vector expressions e1 and e2.
Definition: VectorExpression.hpp:1033
CDPL_VIS_API void scale(TriangleMesh3D &mesh, double scale_x, double scale_y, double scale_z, std::size_t vtx_offs=0, std::size_t vtx_count=0)
Scales the vertices of mesh by the per-axis factors (scale_x, scale_y, scale_z).
The namespace of the Chemical Data Processing Library.
VectorArray< V > ArrayType
The vector-array type.
Definition: MinimizerVariableArrayTraits.hpp:189
static void sub(ArrayType &a1, const ArrayType &a2)
Subtracts a2 from a1 element-wise ( ).
Definition: MinimizerVariableArrayTraits.hpp:319
V::ValueType ValueType
The scalar value type stored in the array elements.
Definition: MinimizerVariableArrayTraits.hpp:199
static void clear(ArrayType &a)
Sets all vector elements in a to the default-constructed ValueType.
Definition: MinimizerVariableArrayTraits.hpp:285
static void multiply(ArrayType &a, const T &v)
Multiplies every element of a by the scalar v.
Definition: MinimizerVariableArrayTraits.hpp:308
static void assign(ArrayType &a1, const ArrayType &a2)
Copies the contents of a2 into a1.
Definition: MinimizerVariableArrayTraits.hpp:296
ArrayType::SizeType SizeType
The size type used by the vector array.
Definition: MinimizerVariableArrayTraits.hpp:204
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Performs the in-place BLAS-style axpy operation .
Definition: MinimizerVariableArrayTraits.hpp:268
V VectorType
The vector type of the array elements.
Definition: MinimizerVariableArrayTraits.hpp:194
static T norm2(const ArrayType &a)
Computes the Euclidean (L2) norm of a using a numerically stable scaling algorithm.
Definition: MinimizerVariableArrayTraits.hpp:231
static T dot(const ArrayType &a1, const ArrayType &a2)
Computes the inner product (dot product) of two vector arrays.
Definition: MinimizerVariableArrayTraits.hpp:214
static void assign(ArrayType &a1, const ArrayType &a2)
Copies the contents of a2 into a1.
Definition: MinimizerVariableArrayTraits.hpp:446
static void clear(ArrayType &a)
Sets all vector elements in a to the default-constructed ValueType.
Definition: MinimizerVariableArrayTraits.hpp:435
V VectorType
The vector type of the array elements.
Definition: MinimizerVariableArrayTraits.hpp:344
ArrayType::size_type SizeType
The size type used by the vector array.
Definition: MinimizerVariableArrayTraits.hpp:354
std::vector< V > ArrayType
The vector-array type.
Definition: MinimizerVariableArrayTraits.hpp:339
static T norm2(const ArrayType &a)
Computes the Euclidean (L2) norm of a using a numerically stable scaling algorithm.
Definition: MinimizerVariableArrayTraits.hpp:381
static void sub(ArrayType &a1, const ArrayType &a2)
Subtracts a2 from a1 element-wise ( ).
Definition: MinimizerVariableArrayTraits.hpp:469
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Performs the in-place BLAS-style axpy operation .
Definition: MinimizerVariableArrayTraits.hpp:418
V::ValueType ValueType
The scalar value type stored in the array elements.
Definition: MinimizerVariableArrayTraits.hpp:349
static T dot(const ArrayType &a1, const ArrayType &a2)
Computes the inner product (dot product) of two vector arrays.
Definition: MinimizerVariableArrayTraits.hpp:364
static void multiply(ArrayType &a, const T &v)
Multiplies every element of a by the scalar v.
Definition: MinimizerVariableArrayTraits.hpp:458
Traits template that adapts arbitrary variable-array types to the linear-algebra operations required ...
Definition: MinimizerVariableArrayTraits.hpp:55
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Performs the in-place BLAS-style axpy operation .
Definition: MinimizerVariableArrayTraits.hpp:131
static T dot(const ArrayType &a1, const ArrayType &a2)
Computes the inner product (dot product) of two variable arrays.
Definition: MinimizerVariableArrayTraits.hpp:80
static void multiply(ArrayType &a, const T &v)
Multiplies every element of a by the scalar v.
Definition: MinimizerVariableArrayTraits.hpp:162
static void sub(ArrayType &a1, const ArrayType &a2)
Subtracts a2 from a1 element-wise ( ).
Definition: MinimizerVariableArrayTraits.hpp:172
A::ValueType ValueType
The scalar value type stored by the array.
Definition: MinimizerVariableArrayTraits.hpp:65
static void assign(ArrayType &a1, const ArrayType &a2)
Copies the contents of a2 into a1.
Definition: MinimizerVariableArrayTraits.hpp:150
A ArrayType
The variable-array type.
Definition: MinimizerVariableArrayTraits.hpp:60
static void clear(ArrayType &a)
Sets all elements of a to the default-constructed ValueType.
Definition: MinimizerVariableArrayTraits.hpp:140
A::SizeType SizeType
The size type used by the array.
Definition: MinimizerVariableArrayTraits.hpp:70
static T norm2(const ArrayType &a)
Computes the Euclidean (L2) norm of a using a numerically stable scaling algorithm.
Definition: MinimizerVariableArrayTraits.hpp:92
static RealType abs(ConstReference t)
Returns the absolute value of t (std::abs for signed types, the identity for unsigned types).
Definition: TypeTraits.hpp:142
T RealType
The real-valued type (identical to ValueType for scalar traits).
Definition: TypeTraits.hpp:96
Primary traits template for scalar arithmetic value types.
Definition: TypeTraits.hpp:307