Chemical Data Processing Library C++ API - Version 1.1.1
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 
43  template <typename A>
45  {
46 
47  typedef A ArrayType;
48  typedef typename A::ValueType ValueType;
49  typedef typename A::SizeType SizeType;
50 
51  template <typename T>
52 
53  static T dot(const ArrayType& a1, const ArrayType& a2)
54  {
55  return innerProd(a1, a2);
56  }
57 
58  template <typename T>
59  static T norm2(const ArrayType& a)
60  {
61  T scale = T();
62  T ssq = T(1);
63  SizeType size = a.getSize();
64 
65  if (size == SizeType(0))
66  return T();
67 
68  else if (size == SizeType(1))
69  return TypeTraits<ValueType>::abs(a(0));
70 
71  for (SizeType i = 0; i < size; i++) {
72  const ValueType& x = a(i);
73 
74  if (x != ValueType()) {
76 
77  if (scale < ax) {
78  ssq = 1 + ssq * (scale / ax) * (scale / ax);
79  scale = ax;
80 
81  } else {
82  ssq += (ax / scale) * (ax / scale);
83  }
84  }
85  }
86 
87  return (scale * TypeTraits<T>::sqrt(ssq));
88  }
89 
90  template <typename T>
91  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
92  {
93  y.plusAssign(alpha * x);
94  }
95 
96  static void clear(ArrayType& a)
97  {
98  a.clear(ValueType());
99  }
100 
101  static void assign(ArrayType& a1, const ArrayType& a2)
102  {
103  a1.assign(a2);
104  }
105 
106  template <typename T>
107  static void multiply(ArrayType& a, const T& v)
108  {
109  a *= v;
110  }
111 
112  static void sub(ArrayType& a1, const ArrayType& a2)
113  {
114  a1.minusAssign(a2);
115  }
116  };
117 
118  template <typename V>
120  {
121 
123  typedef V VectorType;
124  typedef typename V::ValueType ValueType;
125  typedef typename ArrayType::SizeType SizeType;
126 
127  template <typename T>
128  static T dot(const ArrayType& a1, const ArrayType& a2)
129  {
130  T result = T();
131 
132  for (typename ArrayType::ConstElementIterator it1 = a1.getElementsBegin(), it2 = a2.getElementsBegin(), end1 = a1.getElementsEnd(); it1 != end1; ++it1, ++it2)
133  result += innerProd(*it1, *it2);
134 
135  return result;
136  }
137 
138  template <typename T>
139  static T norm2(const ArrayType& a)
140  {
141  T scale = T();
142  T ssq = T(1);
143 
144  for (typename ArrayType::ConstElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it) {
145  const typename VectorType::ConstPointer vx = it->getData();
146  typename VectorType::SizeType dim = it->getSize();
147 
148  for (typename VectorType::SizeType i = 0; i < dim; i++) {
149  const ValueType& x = vx[i];
150 
151  if (x != ValueType()) {
153 
154  if (scale < ax) {
155  ssq = 1 + ssq * (scale / ax) * (scale / ax);
156  scale = ax;
157 
158  } else {
159  ssq += (ax / scale) * (ax / scale);
160  }
161  }
162  }
163  }
164 
165  return (scale * TypeTraits<T>::sqrt(ssq));
166  }
167 
168  template <typename T>
169  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
170  {
171  typename ArrayType::ElementIterator it2 = y.getElementsBegin();
172  VectorType tmp;
173 
174  for (typename ArrayType::ConstElementIterator it1 = x.getElementsBegin(), end1 = x.getElementsEnd(); it1 != end1; ++it1, ++it2) {
175  tmp.assign(*it1);
176  tmp *= alpha;
177 
178  it2->plusAssign(tmp);
179  }
180  }
181 
182  static void clear(ArrayType& a)
183  {
184  for (typename ArrayType::ElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it)
185  it->clear(ValueType());
186  }
187 
188  static void assign(ArrayType& a1, const ArrayType& a2)
189  {
190  a1 = a2;
191  }
192 
193  template <typename T>
194  static void multiply(ArrayType& a, const T& v)
195  {
196  for (typename ArrayType::ElementIterator it = a.getElementsBegin(), end = a.getElementsEnd(); it != end; ++it)
197  *it *= v;
198  }
199 
200 
201  static void sub(ArrayType& a1, const ArrayType& a2)
202  {
204 
205  for (typename ArrayType::ElementIterator it1 = a1.getElementsBegin(), end1 = a1.getElementsEnd(); it1 != end1; ++it1, ++it2)
206  it1->minusAssign(*it2);
207  }
208  };
209 
210  template <typename V>
211  struct MinimizerVariableArrayTraits<std::vector<V> >
212  {
213 
214  typedef std::vector<V> ArrayType;
215  typedef V VectorType;
216  typedef typename V::ValueType ValueType;
217  typedef typename ArrayType::size_type SizeType;
218 
219  template <typename T>
220  static T dot(const ArrayType& a1, const ArrayType& a2)
221  {
222  T result = T();
223 
224  for (typename ArrayType::const_iterator it1 = a1.begin(), it2 = a2.begin(), end1 = a1.end(); it1 != end1; ++it1, ++it2)
225  result += innerProd(*it1, *it2);
226 
227  return result;
228  }
229 
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::const_iterator it = a.begin(), end = a.end(); 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 
260  template <typename T>
261  static void axpy(const T& alpha, const ArrayType& x, ArrayType& y)
262  {
263  typename ArrayType::iterator it2 = y.begin();
264  VectorType tmp;
265 
266  for (typename ArrayType::const_iterator it1 = x.begin(), end1 = x.end(); it1 != end1; ++it1, ++it2) {
267  tmp.assign(*it1);
268  tmp *= alpha;
269 
270  it2->plusAssign(tmp);
271  }
272  }
273 
274  static void clear(ArrayType& a)
275  {
276  for (typename ArrayType::iterator it = a.begin(), end = a.end(); it != end; ++it)
277  it->clear(ValueType());
278  }
279 
280  static void assign(ArrayType& a1, const ArrayType& a2)
281  {
282  a1 = a2;
283  }
284 
285  template <typename T>
286  static void multiply(ArrayType& a, const T& v)
287  {
288  for (typename ArrayType::iterator it = a.begin(), end = a.end(); it != end; ++it)
289  *it *= v;
290  }
291 
292 
293  static void sub(ArrayType& a1, const ArrayType& a2)
294  {
295  typename ArrayType::const_iterator it2 = a2.begin();
296 
297  for (typename ArrayType::iterator it1 = a1.begin(), end1 = a1.end(); it1 != end1; ++it1, ++it2)
298  it1->minusAssign(*it2);
299  }
300  };
301  } // namespace Math
302 } // namespace CDPL
303 
304 #endif // CDPL_MATH_MINIMIZERVARIABLEARRAYTRAITS_HPP
CDPL::Math::MinimizerVariableArrayTraits::sub
static void sub(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:112
CDPL::Math::TypeTraits
Definition: TypeTraits.hpp:171
VectorArray.hpp
Definition of the class CDPL::Math::VectorArray.
CDPL::Math::MinimizerVariableArrayTraits::SizeType
A::SizeType SizeType
Definition: MinimizerVariableArrayTraits.hpp:49
CDPL::Math::innerProd
VectorInnerProduct< E1, E2 >::ResultType innerProd(const VectorExpression< E1 > &e1, const VectorExpression< E2 > &e2)
Definition: VectorExpression.hpp:504
CDPL::Util::Array< V >::SizeType
std::size_t SizeType
The type of objects stored by the array.
Definition: Array.hpp:110
CDPL::Util::Array::getElementsEnd
ConstElementIterator getElementsEnd() const
Returns a constant iterator pointing to the end of the array.
Definition: Array.hpp:896
CDPL::Math::MinimizerVariableArrayTraits::assign
static void assign(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:101
CDPL::Math::MinimizerVariableArrayTraits::axpy
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Definition: MinimizerVariableArrayTraits.hpp:91
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::multiply
static void multiply(ArrayType &a, const T &v)
Definition: MinimizerVariableArrayTraits.hpp:194
CDPL::Math::VectorArray
An array for storing generic vector objects.
Definition: VectorArray.hpp:49
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::ValueType
V::ValueType ValueType
Definition: MinimizerVariableArrayTraits.hpp:216
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::norm2
static T norm2(const ArrayType &a)
Definition: MinimizerVariableArrayTraits.hpp:139
CDPL::Util::Array::getElementsBegin
ConstElementIterator getElementsBegin() const
Returns a constant iterator pointing to the beginning of the array.
Definition: Array.hpp:884
CDPL::Math::MinimizerVariableArrayTraits::norm2
static T norm2(const ArrayType &a)
Definition: MinimizerVariableArrayTraits.hpp:59
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::dot
static T dot(const ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:220
TypeTraits.hpp
Definition of type traits.
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::assign
static void assign(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:280
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::VectorType
V VectorType
Definition: MinimizerVariableArrayTraits.hpp:215
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::SizeType
ArrayType::SizeType SizeType
Definition: MinimizerVariableArrayTraits.hpp:125
V
CDPL::Util::Array< V >::ConstElementIterator
StorageType::const_iterator ConstElementIterator
A constant random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:125
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::axpy
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Definition: MinimizerVariableArrayTraits.hpp:169
CDPL::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::SizeType
ArrayType::size_type SizeType
Definition: MinimizerVariableArrayTraits.hpp:217
CDPL::Math::MinimizerVariableArrayTraits
Definition: MinimizerVariableArrayTraits.hpp:45
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::ArrayType
std::vector< V > ArrayType
Definition: MinimizerVariableArrayTraits.hpp:214
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::dot
static T dot(const ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:128
CDPL
The namespace of the Chemical Data Processing Library.
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::multiply
static void multiply(ArrayType &a, const T &v)
Definition: MinimizerVariableArrayTraits.hpp:286
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::clear
static void clear(ArrayType &a)
Definition: MinimizerVariableArrayTraits.hpp:274
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::axpy
static void axpy(const T &alpha, const ArrayType &x, ArrayType &y)
Definition: MinimizerVariableArrayTraits.hpp:261
CDPL::Math::MinimizerVariableArrayTraits::clear
static void clear(ArrayType &a)
Definition: MinimizerVariableArrayTraits.hpp:96
CDPL::Math::MinimizerVariableArrayTraits::dot
static T dot(const ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:53
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::ArrayType
VectorArray< V > ArrayType
Definition: MinimizerVariableArrayTraits.hpp:122
CDPL::Math::MinimizerVariableArrayTraits::multiply
static void multiply(ArrayType &a, const T &v)
Definition: MinimizerVariableArrayTraits.hpp:107
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::norm2
static T norm2(const ArrayType &a)
Definition: MinimizerVariableArrayTraits.hpp:231
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::assign
static void assign(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:188
CDPL::Math::MinimizerVariableArrayTraits::ArrayType
A ArrayType
Definition: MinimizerVariableArrayTraits.hpp:47
CDPL::Util::Array< V >::ElementIterator
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:137
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::ValueType
V::ValueType ValueType
Definition: MinimizerVariableArrayTraits.hpp:124
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::clear
static void clear(ArrayType &a)
Definition: MinimizerVariableArrayTraits.hpp:182
CDPL::Math::MinimizerVariableArrayTraits::ValueType
A::ValueType ValueType
Definition: MinimizerVariableArrayTraits.hpp:48
CDPL::Math::MinimizerVariableArrayTraits< std::vector< V > >::sub
static void sub(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:293
CDPL::Chem::AtomType::A
const unsigned int A
A generic type that covers any element except hydrogen.
Definition: AtomType.hpp:617
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::VectorType
V VectorType
Definition: MinimizerVariableArrayTraits.hpp:123
CDPL::Math::MinimizerVariableArrayTraits< VectorArray< V > >::sub
static void sub(ArrayType &a1, const ArrayType &a2)
Definition: MinimizerVariableArrayTraits.hpp:201