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 
58  typedef A ArrayType;
60  typedef typename A::ValueType ValueType;
62  typedef typename A::SizeType SizeType;
63 
71  template <typename T>
72  static T dot(const ArrayType& a1, const ArrayType& a2)
73  {
74  return innerProd(a1, a2);
75  }
76 
83  template <typename T>
84  static T norm2(const ArrayType& a)
85  {
86  T scale = T();
87  T ssq = T(1);
88  SizeType size = a.getSize();
89 
90  if (size == SizeType(0))
91  return T();
92 
93  else if (size == SizeType(1))
94  return TypeTraits<ValueType>::abs(a(0));
95 
96  for (SizeType i = 0; i < size; i++) {
97  const ValueType& x = a(i);
98 
99  if (x != ValueType()) {
101 
102  if (scale < ax) {
103  ssq = 1 + ssq * (scale / ax) * (scale / ax);
104  scale = ax;
105 
106  } else {
107  ssq += (ax / scale) * (ax / scale);
108  }
109  }
110  }
111 
112  return (scale * TypeTraits<T>::sqrt(ssq));
113  }
114 
122  template <typename T>
123  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
124  {
125  y.plusAssign(alpha * x);
126  }
127 
132  static void clear(ArrayType& a)
133  {
134  a.clear(ValueType());
135  }
136 
142  static void assign(ArrayType& a1, const ArrayType& a2)
143  {
144  a1.assign(a2);
145  }
146 
153  template <typename T>
154  static void multiply(ArrayType& a, const T& v)
155  {
156  a *= v;
157  }
158 
164  static void sub(ArrayType& a1, const ArrayType& a2)
165  {
166  a1.minusAssign(a2);
167  }
168  };
169 
174  template <typename V>
176  {
177 
181  typedef V VectorType;
183  typedef typename V::ValueType ValueType;
185  typedef typename ArrayType::SizeType SizeType;
186 
194  template <typename T>
195  static T dot(const ArrayType& a1, const ArrayType& a2)
196  {
197  T result = T();
198 
199  for (typename ArrayType::ConstElementIterator it1 = a1.getElementsBegin(), it2 = a2.getElementsBegin(), end1 = a1.getElementsEnd(); it1 != end1; ++it1, ++it2)
200  result += innerProd(*it1, *it2);
201 
202  return result;
203  }
204 
211  template <typename T>
212  static T norm2(const ArrayType& a)
213  {
214  T scale = T();
215  T ssq = T(1);
216 
217  for (typename ArrayType::ConstElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it) {
218  const typename VectorType::ConstPointer vx = it->getData();
219  typename VectorType::SizeType dim = it->getSize();
220 
221  for (typename VectorType::SizeType i = 0; i < dim; i++) {
222  const ValueType& x = vx[i];
223 
224  if (x != ValueType()) {
226 
227  if (scale < ax) {
228  ssq = 1 + ssq * (scale / ax) * (scale / ax);
229  scale = ax;
230 
231  } else {
232  ssq += (ax / scale) * (ax / scale);
233  }
234  }
235  }
236  }
237 
238  return (scale * TypeTraits<T>::sqrt(ssq));
239  }
240 
248  template <typename T>
249  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
250  {
251  typename ArrayType::ElementIterator it2 = y.getElementsBegin();
252  VectorType tmp;
253 
254  for (typename ArrayType::ConstElementIterator it1 = x.getElementsBegin(), end1 = x.getElementsEnd(); it1 != end1; ++it1, ++it2) {
255  tmp.assign(*it1);
256  tmp *= alpha;
257 
258  it2->plusAssign(tmp);
259  }
260  }
261 
266  static void clear(ArrayType& a)
267  {
268  for (typename ArrayType::ElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it)
269  it->clear(ValueType());
270  }
271 
277  static void assign(ArrayType& a1, const ArrayType& a2)
278  {
279  a1 = a2;
280  }
281 
288  template <typename T>
289  static void multiply(ArrayType& a, const T& v)
290  {
291  for (typename ArrayType::ElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it)
292  *it *= v;
293  }
294 
300  static void sub(ArrayType& a1, const ArrayType& a2)
301  {
303 
304  for (typename ArrayType::ElementIterator it1 = a1.getElementsBegin(), end1 = a1.getElementsEnd(); it1 != end1; ++it1, ++it2)
305  it1->minusAssign(*it2);
306  }
307  };
308 
313  template <typename V>
314  struct MinimizerVariableArrayTraits<std::vector<V> >
315  {
316 
318  typedef std::vector<V> ArrayType;
320  typedef V VectorType;
322  typedef typename V::ValueType ValueType;
324  typedef typename ArrayType::size_type SizeType;
325 
333  template <typename T>
334  static T dot(const ArrayType& a1, const ArrayType& a2)
335  {
336  T result = T();
337 
338  for (typename ArrayType::const_iterator it1 = a1.begin(), it2 = a2.begin(), end1 = a1.end(); it1 != end1; ++it1, ++it2)
339  result += innerProd(*it1, *it2);
340 
341  return result;
342  }
343 
350  template <typename T>
351  static T norm2(const ArrayType& a)
352  {
353  T scale = T();
354  T ssq = T(1);
355 
356  for (typename ArrayType::const_iterator it = a.begin(), end = a.end(); it != end; ++it) {
357  const typename VectorType::ConstPointer vx = it->getData();
358  typename VectorType::SizeType dim = it->getSize();
359 
360  for (typename VectorType::SizeType i = 0; i < dim; i++) {
361  const ValueType& x = vx[i];
362 
363  if (x != ValueType()) {
365 
366  if (scale < ax) {
367  ssq = 1 + ssq * (scale / ax) * (scale / ax);
368  scale = ax;
369 
370  } else {
371  ssq += (ax / scale) * (ax / scale);
372  }
373  }
374  }
375  }
376 
377  return (scale * TypeTraits<T>::sqrt(ssq));
378  }
379 
387  template <typename T>
388  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
389  {
390  typename ArrayType::iterator it2 = y.begin();
391  VectorType tmp;
392 
393  for (typename ArrayType::const_iterator it1 = x.begin(), end1 = x.end(); it1 != end1; ++it1, ++it2) {
394  tmp.assign(*it1);
395  tmp *= alpha;
396 
397  it2->plusAssign(tmp);
398  }
399  }
400 
405  static void clear(ArrayType& a)
406  {
407  for (typename ArrayType::iterator it = a.begin(), end = a.end(); it != end; ++it)
408  it->clear(ValueType());
409  }
410 
416  static void assign(ArrayType& a1, const ArrayType& a2)
417  {
418  a1 = a2;
419  }
420 
427  template <typename T>
428  static void multiply(ArrayType& a, const T& v)
429  {
430  for (typename ArrayType::iterator it = a.begin(), end = a.end(); it != end; ++it)
431  *it *= v;
432  }
433 
439  static void sub(ArrayType& a1, const ArrayType& a2)
440  {
441  typename ArrayType::const_iterator it2 = a2.begin();
442 
443  for (typename ArrayType::iterator it1 = a1.begin(), end1 = a1.end(); it1 != end1; ++it1, ++it2)
444  it1->minusAssign(*it2);
445  }
446  };
447 
448  } // namespace Math
449 } // namespace CDPL
450 
451 #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:125
ConstElementIterator getElementsEnd() const
Returns a constant iterator pointing to the end of the array.
Definition: Array.hpp:904
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:137
ConstElementIterator getElementsBegin() const
Returns a constant iterator pointing to the beginning of the array.
Definition: Array.hpp:892
std::size_t SizeType
The type of objects stored by the array.
Definition: Array.hpp:110
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:883
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)
The namespace of the Chemical Data Processing Library.
VectorArray< V > ArrayType
The vector-array type.
Definition: MinimizerVariableArrayTraits.hpp:179
static void sub(ArrayType &a1, const ArrayType &a2)
Subtracts a2 from a1 element-wise ( ).
Definition: MinimizerVariableArrayTraits.hpp:300
V::ValueType ValueType
The scalar value type stored in the array elements.
Definition: MinimizerVariableArrayTraits.hpp:183
static void clear(ArrayType &a)
Sets all vector elements in a to the default-constructed ValueType.
Definition: MinimizerVariableArrayTraits.hpp:266
static void multiply(ArrayType &a, const T &v)
Multiplies every element of a by the scalar v.
Definition: MinimizerVariableArrayTraits.hpp:289
static void assign(ArrayType &a1, const ArrayType &a2)
Copies the contents of a2 into a1.
Definition: MinimizerVariableArrayTraits.hpp:277
ArrayType::SizeType SizeType
The size type used by the vector array.
Definition: MinimizerVariableArrayTraits.hpp:185
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Performs the in-place BLAS-style axpy operation .
Definition: MinimizerVariableArrayTraits.hpp:249
V VectorType
The vector type of the array elements.
Definition: MinimizerVariableArrayTraits.hpp:181
static T norm2(const ArrayType &a)
Computes the Euclidean (L2) norm of a using a numerically stable scaling algorithm.
Definition: MinimizerVariableArrayTraits.hpp:212
static T dot(const ArrayType &a1, const ArrayType &a2)
Computes the inner product (dot product) of two vector arrays.
Definition: MinimizerVariableArrayTraits.hpp:195
static void assign(ArrayType &a1, const ArrayType &a2)
Copies the contents of a2 into a1.
Definition: MinimizerVariableArrayTraits.hpp:416
static void clear(ArrayType &a)
Sets all vector elements in a to the default-constructed ValueType.
Definition: MinimizerVariableArrayTraits.hpp:405
V VectorType
The vector type of the array elements.
Definition: MinimizerVariableArrayTraits.hpp:320
ArrayType::size_type SizeType
The size type used by the vector array.
Definition: MinimizerVariableArrayTraits.hpp:324
std::vector< V > ArrayType
The vector-array type.
Definition: MinimizerVariableArrayTraits.hpp:318
static T norm2(const ArrayType &a)
Computes the Euclidean (L2) norm of a using a numerically stable scaling algorithm.
Definition: MinimizerVariableArrayTraits.hpp:351
static void sub(ArrayType &a1, const ArrayType &a2)
Subtracts a2 from a1 element-wise ( ).
Definition: MinimizerVariableArrayTraits.hpp:439
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Performs the in-place BLAS-style axpy operation .
Definition: MinimizerVariableArrayTraits.hpp:388
V::ValueType ValueType
The scalar value type stored in the array elements.
Definition: MinimizerVariableArrayTraits.hpp:322
static T dot(const ArrayType &a1, const ArrayType &a2)
Computes the inner product (dot product) of two vector arrays.
Definition: MinimizerVariableArrayTraits.hpp:334
static void multiply(ArrayType &a, const T &v)
Multiplies every element of a by the scalar v.
Definition: MinimizerVariableArrayTraits.hpp:428
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:123
static T dot(const ArrayType &a1, const ArrayType &a2)
Computes the inner product (dot product) of two variable arrays.
Definition: MinimizerVariableArrayTraits.hpp:72
static void multiply(ArrayType &a, const T &v)
Multiplies every element of a by the scalar v.
Definition: MinimizerVariableArrayTraits.hpp:154
static void sub(ArrayType &a1, const ArrayType &a2)
Subtracts a2 from a1 element-wise ( ).
Definition: MinimizerVariableArrayTraits.hpp:164
A::ValueType ValueType
The scalar value type stored in the array.
Definition: MinimizerVariableArrayTraits.hpp:60
static void assign(ArrayType &a1, const ArrayType &a2)
Copies the contents of a2 into a1.
Definition: MinimizerVariableArrayTraits.hpp:142
A ArrayType
The variable-array type.
Definition: MinimizerVariableArrayTraits.hpp:58
static void clear(ArrayType &a)
Sets all elements of a to the default-constructed ValueType.
Definition: MinimizerVariableArrayTraits.hpp:132
A::SizeType SizeType
The size type used by the array.
Definition: MinimizerVariableArrayTraits.hpp:62
static T norm2(const ArrayType &a)
Computes the Euclidean (L2) norm of a using a numerically stable scaling algorithm.
Definition: MinimizerVariableArrayTraits.hpp:84
static RealType abs(ConstReference t)
Returns the absolute value of t (std::abs for signed types, the identity for unsigned types).
Definition: TypeTraits.hpp:131
T RealType
The real-valued type (identical to ValueType for scalar traits).
Definition: TypeTraits.hpp:91
Primary traits template for scalar arithmetic value types.
Definition: TypeTraits.hpp:285