Chemical Data Processing Library C++ API - Version 1.4.0
VectorArrayFunctions.hpp
Go to the documentation of this file.
1 /*
2  * VectorArrayFunctions.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_MATH_VECTORARRAYFUNCTIONS_HPP
30 #define CDPL_MATH_VECTORARRAYFUNCTIONS_HPP
31 
32 #include <cstddef>
33 #include <cmath>
34 
36 #include "CDPL/Math/Matrix.hpp"
37 
38 
39 namespace CDPL
40 {
41 
42  namespace Math
43  {
44 
53  template <typename T, std::size_t Dim, typename T1>
55  {
56  typedef CVector<T, Dim> VecType;
57  typedef CMatrix<T1, Dim, Dim> MtxType;
58 
59  typedef typename VectorArray<VecType>::ElementIterator Iterator;
60  typedef typename VecType::Pointer VecDataPointer;
61  typedef typename MtxType::ConstArrayPointer MtxDataPointer;
62 
63  T tmp[Dim];
64  MtxDataPointer xform_data = xform.getData();
65 
66  for (Iterator it = va.getElementsBegin(), end = va.getElementsEnd(); it != end; ++it) {
67  VecDataPointer vec = it->getData();
68 
69  for (std::size_t i = 0; i < Dim; i++) {
70  tmp[i] = T();
71 
72  for (std::size_t j = 0; j < Dim; j++)
73  tmp[i] += vec[j] * xform_data[i][j];
74  }
75 
76  for (std::size_t i = 0; i < Dim; i++)
77  vec[i] = tmp[i];
78  }
79  }
80 
90  template <typename T, std::size_t Dim, typename T1>
92  {
93  typedef CVector<T, Dim> VecType;
94  typedef CMatrix<T1, Dim + 1, Dim + 1> MtxType;
95 
96  typedef typename VectorArray<VecType>::ElementIterator Iterator;
97  typedef typename VecType::Pointer VecDataPointer;
98  typedef typename MtxType::ConstArrayPointer MtxDataPointer;
99 
100  T tmp[Dim];
101  MtxDataPointer xform_data = xform.getData();
102 
103  for (Iterator it = va.getElementsBegin(), end = va.getElementsEnd(); it != end; ++it) {
104  VecDataPointer vec = it->getData();
105 
106  for (std::size_t i = 0; i < Dim; i++) {
107  tmp[i] = T();
108 
109  for (std::size_t j = 0; j < Dim; j++)
110  tmp[i] += vec[j] * xform_data[i][j];
111 
112  tmp[i] += xform_data[i][Dim];
113  }
114 
115  for (std::size_t i = 0; i < Dim; i++)
116  vec[i] = tmp[i];
117  }
118  }
119 
129  template <typename T, std::size_t Dim, typename T1>
131  {
132  if (va.isEmpty())
133  return false;
134 
135  ctr.clear();
136 
137  typedef typename VectorArray<CVector<T, Dim> >::ConstElementIterator Iterator;
138 
139  for (Iterator it = va.getElementsBegin(), end = va.getElementsEnd(); it != end; ++it)
140  ctr.plusAssign(*it);
141 
142  ctr /= va.getSize();
143 
144  return true;
145  }
146 
158  template <typename T, std::size_t Dim>
160  {
161  typedef CVector<T, Dim> VecType;
162  typedef typename VectorArray<VecType>::SizeType ArraySizeType;
163 
164  ArraySizeType num_elem = std::min(va1.getSize(), va2.getSize());
165 
166  if (num_elem == 0)
167  return T();
168 
169  typedef typename VectorArray<VecType>::ConstElementIterator Iterator;
170  typedef typename VecType::ConstPointer VecDataPointer;
171 
172  T sd = T();
173 
174  for (Iterator it1 = va1.getElementsBegin(), it2 = va2.getElementsBegin(), end = it1 + num_elem; it1 != end; ++it1, ++it2) {
175  VecDataPointer vec1 = it1->getData();
176  VecDataPointer vec2 = it2->getData();
177 
178  for (std::size_t i = 0; i < Dim; i++) {
179  T diff = vec1[i] - vec2[i];
180 
181  sd += diff * diff;
182  }
183  }
184 
185  return std::sqrt(sd / num_elem);
186  }
187 
202  template <typename T, std::size_t Dim, typename T1>
204  {
205  typedef CVector<T, Dim> VecType;
206  typedef CMatrix<T1, Dim + 1, Dim + 1> MtxType;
207 
208  typedef typename VectorArray<VecType>::SizeType ArraySizeType;
209 
210  ArraySizeType num_elem = std::min(va1.getSize(), va2.getSize());
211 
212  if (num_elem == 0)
213  return T();
214 
215  typedef typename VectorArray<VecType>::ConstElementIterator Iterator;
216  typedef typename VecType::ConstPointer VecDataPointer;
217  typedef typename MtxType::ConstArrayPointer MtxDataPointer;
218 
219  T sd = T();
220  MtxDataPointer xform_data = va1_xform.getData();
221 
222  for (Iterator it1 = va1.getElementsBegin(), it2 = va2.getElementsBegin(), end = it1 + num_elem; it1 != end; ++it1, ++it2) {
223  VecDataPointer vec1 = it1->getData();
224  VecDataPointer vec2 = it2->getData();
225 
226  for (std::size_t i = 0; i < Dim; i++) {
227  T tmp = T();
228 
229  for (std::size_t j = 0; j < Dim; j++)
230  tmp += vec1[j] * xform_data[i][j];
231 
232  tmp += xform_data[i][Dim] - vec2[i];
233  sd += tmp * tmp;
234  }
235  }
236 
237  return std::sqrt(sd / num_elem);
238  }
239  } // namespace Math
240 } // namespace CDPL
241 
242 #endif // CDPL_MATH_VECTORARRAYFUNCTIONS_HPP
Definition of matrix data types.
Definition of class CDPL::Math::VectorArray.
Fixed-size dense matrix of dimensions M N backed by a 2D C-array (no dynamic allocation).
Definition: Matrix.hpp:1987
ArrayPointer getData()
Returns a mutable pointer-to-row to the contiguous 2D element array.
Definition: Matrix.hpp:2154
Fixed-size vector of dimension N backed by a C-array (no dynamic allocation).
Definition: Vector.hpp:1876
void clear(const ValueType &v=ValueType())
Sets every element of the vector to the value v.
Definition: Vector.hpp:2294
CVector & plusAssign(const VectorExpression< E > &e)
Adds the elements of the vector expression e to this vector without intermediate temporary.
Definition: Vector.hpp:2229
SizeType getSize() const
Returns the fixed element count N.
Definition: Vector.hpp:2016
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
StorageType::iterator ElementIterator
A mutable random access iterator used to iterate over the elements of the array.
Definition: Array.hpp:137
std::size_t SizeType
The type of objects stored by the array.
Definition: Array.hpp:110
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
bool calcCentroid(const VectorArray< CVector< T, Dim > > &va, CVector< T1, Dim > &ctr)
Calculates the centroid of the array elements.
Definition: VectorArrayFunctions.hpp:130
QuaternionVectorAdapter< E > vec(QuaternionExpression< E > &e)
Creates a mutable Math::QuaternionVectorAdapter view of the quaternion expression e.
Definition: QuaternionAdapter.hpp:372
T calcRMSD(const VectorArray< CVector< T, Dim > > &va1, const VectorArray< CVector< T, Dim > > &va2)
Calculates the root-mean-square distance between the corresponding elements of va1 and va2.
Definition: VectorArrayFunctions.hpp:159
void transform(VectorArray< CVector< T, Dim > > &va, const CMatrix< T1, Dim, Dim > &xform)
Transforms each -dimensional vector in the array with the -dimensional square matrix xform.
Definition: VectorArrayFunctions.hpp:54
The namespace of the Chemical Data Processing Library.