Chemical Data Processing Library C++ API - Version 1.1.1
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 
50  template <typename T, std::size_t Dim, typename T1>
52  {
53  typedef CVector<T, Dim> VecType;
54  typedef CMatrix<T1, Dim, Dim> MtxType;
55 
56  typedef typename VectorArray<VecType>::ElementIterator Iterator;
57  typedef typename VecType::Pointer VecDataPointer;
58  typedef typename MtxType::ConstArrayPointer MtxDataPointer;
59 
60  T tmp[Dim];
61  MtxDataPointer xform_data = xform.getData();
62 
63  for (Iterator it = va.getElementsBegin(), end = va.getElementsEnd(); it != end; ++it) {
64  VecDataPointer vec = it->getData();
65 
66  for (std::size_t i = 0; i < Dim; i++) {
67  tmp[i] = T();
68 
69  for (std::size_t j = 0; j < Dim; j++)
70  tmp[i] += vec[j] * xform_data[i][j];
71  }
72 
73  for (std::size_t i = 0; i < Dim; i++)
74  vec[i] = tmp[i];
75  }
76  }
77 
84  template <typename T, std::size_t Dim, typename T1>
86  {
87  typedef CVector<T, Dim> VecType;
88  typedef CMatrix<T1, Dim + 1, Dim + 1> MtxType;
89 
90  typedef typename VectorArray<VecType>::ElementIterator Iterator;
91  typedef typename VecType::Pointer VecDataPointer;
92  typedef typename MtxType::ConstArrayPointer MtxDataPointer;
93 
94  T tmp[Dim];
95  MtxDataPointer xform_data = xform.getData();
96 
97  for (Iterator it = va.getElementsBegin(), end = va.getElementsEnd(); it != end; ++it) {
98  VecDataPointer vec = it->getData();
99 
100  for (std::size_t i = 0; i < Dim; i++) {
101  tmp[i] = T();
102 
103  for (std::size_t j = 0; j < Dim; j++)
104  tmp[i] += vec[j] * xform_data[i][j];
105 
106  tmp[i] += xform_data[i][Dim];
107  }
108 
109  for (std::size_t i = 0; i < Dim; i++)
110  vec[i] = tmp[i];
111  }
112  }
113 
120  template <typename T, std::size_t Dim, typename T1>
122  {
123  if (va.isEmpty())
124  return false;
125 
126  ctr.clear();
127 
128  typedef typename VectorArray<CVector<T, Dim> >::ConstElementIterator Iterator;
129 
130  for (Iterator it = va.getElementsBegin(), end = va.getElementsEnd(); it != end; ++it)
131  ctr.plusAssign(*it);
132 
133  ctr /= va.getSize();
134 
135  return true;
136  }
137 
138  template <typename T, std::size_t Dim>
140  {
141  typedef CVector<T, Dim> VecType;
142  typedef typename VectorArray<VecType>::SizeType ArraySizeType;
143 
144  ArraySizeType num_elem = std::min(va1.getSize(), va2.getSize());
145 
146  if (num_elem == 0)
147  return T();
148 
149  typedef typename VectorArray<VecType>::ConstElementIterator Iterator;
150  typedef typename VecType::ConstPointer VecDataPointer;
151 
152  T sd = T();
153 
154  for (Iterator it1 = va1.getElementsBegin(), it2 = va2.getElementsBegin(), end = it1 + num_elem; it1 != end; ++it1, ++it2) {
155  VecDataPointer vec1 = it1->getData();
156  VecDataPointer vec2 = it2->getData();
157 
158  for (std::size_t i = 0; i < Dim; i++) {
159  T diff = vec1[i] - vec2[i];
160 
161  sd += diff * diff;
162  }
163  }
164 
165  return std::sqrt(sd / num_elem);
166  }
167 
168  template <typename T, std::size_t Dim, typename T1>
170  {
171  typedef CVector<T, Dim> VecType;
172  typedef CMatrix<T1, Dim + 1, Dim + 1> MtxType;
173 
174  typedef typename VectorArray<VecType>::SizeType ArraySizeType;
175 
176  ArraySizeType num_elem = std::min(va1.getSize(), va2.getSize());
177 
178  if (num_elem == 0)
179  return T();
180 
181  typedef typename VectorArray<VecType>::ConstElementIterator Iterator;
182  typedef typename VecType::ConstPointer VecDataPointer;
183  typedef typename MtxType::ConstArrayPointer MtxDataPointer;
184 
185  T sd = T();
186  MtxDataPointer xform_data = va1_xform.getData();
187 
188  for (Iterator it1 = va1.getElementsBegin(), it2 = va2.getElementsBegin(), end = it1 + num_elem; it1 != end; ++it1, ++it2) {
189  VecDataPointer vec1 = it1->getData();
190  VecDataPointer vec2 = it2->getData();
191 
192  for (std::size_t i = 0; i < Dim; i++) {
193  T tmp = T();
194 
195  for (std::size_t j = 0; j < Dim; j++)
196  tmp += vec1[j] * xform_data[i][j];
197 
198  tmp += xform_data[i][Dim] - vec2[i];
199  sd += tmp * tmp;
200  }
201  }
202 
203  return std::sqrt(sd / num_elem);
204  }
205  } // namespace Math
206 } // namespace CDPL
207 
208 #endif // CDPL_MATH_VECTORARRAYFUNCTIONS_HPP
CDPL::Math::calcRMSD
T calcRMSD(const VectorArray< CVector< T, Dim > > &va1, const VectorArray< CVector< T, Dim > > &va2)
Definition: VectorArrayFunctions.hpp:139
VectorArray.hpp
Definition of the class CDPL::Math::VectorArray.
CDPL::Util::Array< V >::SizeType
std::size_t SizeType
The type of objects stored by the array.
Definition: Array.hpp:110
CDPL::Math::VectorArray
An array for storing generic vector objects.
Definition: VectorArray.hpp:49
CDPL::Math::CMatrix
Definition: Matrix.hpp:1152
CDPL::Math::CMatrix::getData
ArrayPointer getData()
Definition: Matrix.hpp:1243
CDPL::Math::CVector::plusAssign
CVector & plusAssign(const VectorExpression< E > &e)
Definition: Vector.hpp:1242
CDPL::Math::CVector::clear
void clear(const ValueType &v=ValueType())
Definition: Vector.hpp:1278
CDPL::Math::calcCentroid
bool calcCentroid(const VectorArray< CVector< T, Dim > > &va, CVector< T1, Dim > &ctr)
Calculates the centroid of the array elements.
Definition: VectorArrayFunctions.hpp:121
CDPL::Math::CVector
Definition: Vector.hpp:1053
CDPL::Math::vec
QuaternionVectorAdapter< E > vec(QuaternionExpression< E > &e)
Definition: QuaternionAdapter.hpp:237
CDPL::Math::transform
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:51
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::Chem::AtomType::T
const unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
CDPL::Math::CVector::getSize
SizeType getSize() const
Definition: Vector.hpp:1127
CDPL
The namespace of the Chemical Data Processing Library.
Matrix.hpp
Definition of matrix data types.
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