Simbody  3.6
CompositeNumericalTypes.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATRIX_COMPOSITE_NUMERICAL_TYPES_H_
2 #define SimTK_SIMMATRIX_COMPOSITE_NUMERICAL_TYPES_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2005-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
107 
108 namespace SimTK {
109 
110 // These are CNT "depths". 0 means the corresponding CNT is a scalar,
111 // 1 means it is a composite with scalar elements, 2 means a composite
112 // with composite elements, and 3 means a composite with depth-2
113 // composite elements. Beyond that the user will have to diambiguate
114 // operations by using named routines rather than operators.
115 enum {
121 };
122 
136 template <class K> class CNT : private K {
137 public:
138  typedef K T;
139  typedef typename K::TNeg TNeg;
140  typedef typename K::TWithoutNegator TWithoutNegator;
141  typedef typename K::TReal TReal;
142  typedef typename K::TImag TImag;
143  typedef typename K::TComplex TComplex;
144  typedef typename K::THerm THerm;
145  typedef typename K::TPosTrans TPosTrans;
146  typedef typename K::TSqHermT TSqHermT;
147  typedef typename K::TSqTHerm TSqTHerm;
148  typedef typename K::TElement TElement;
149  typedef typename K::TRow TRow; // type of a row or column
150  typedef typename K::TCol TCol;
151 
152  // These are the results of calculations and should be packed regardless
153  // of the spacing of this CNT.
154  typedef typename K::TSqrt TSqrt; // also turns unit^2 to unit
155  typedef typename K::TAbs TAbs;
156  typedef typename K::TStandard TStandard; // packed, StdNumbers
157  typedef typename K::TInvert TInvert; // also turns units into 1/units
158  typedef typename K::TNormalize TNormalize; // TODO: what effect on units?
159 
160  typedef typename K::Scalar Scalar; // quantity< units, <unitlessScalar> >
161  typedef typename K::ULessScalar ULessScalar; // <number> or negator<number>
162  typedef typename K::Number Number; // <real>, <complex> or <conjugate>
163  typedef typename K::StdNumber StdNumber; // <real>, <complex>
164  typedef typename K::Precision Precision; // float, double
165 
166  typedef typename K::ScalarNormSq ScalarNormSq; // type of conjugate square of underlying scalar or
167  // numeric value (squares the units too)
168 
169  template <class P> struct Result {
170  typedef typename K::template Result<P>::Mul Mul;
171  typedef typename K::template Result<P>::Dvd Dvd;
172  typedef typename K::template Result<P>::Add Add;
173  typedef typename K::template Result<P>::Sub Sub;
174  };
175 
176  // Shape-preserving element substitution
177  template <class P> struct Substitute {
178  typedef typename K::template Substitute<P>::Type Type;
179  };
180 
181  enum {
182  NRows = K::NRows,
183  NCols = K::NCols,
184  RowSpacing = K::RowSpacing,
185  ColSpacing = K::ColSpacing,
186  NPackedElements = K::NPackedElements,
187  NActualElements = K::NActualElements,
188  NActualScalars = K::NActualScalars,
189  ImagOffset = K::ImagOffset,
190  RealStrideFactor = K::RealStrideFactor,
191  ArgDepth = K::ArgDepth,
192  IsScalar = K::IsScalar, // scalar with units, real, complex, conjugate, negator
193  IsULessScalar = K::IsULessScalar, // real, complex, conjugate, negator
194  IsNumber = K::IsNumber, // real, complex, conjugate
195  IsStdNumber = K::IsStdNumber, // real, complex
196  IsPrecision = K::IsPrecision, // real (float, double)
197  SignInterpretation = K::SignInterpretation // 1 normally, -1 if elements are negated
198  };
199 
200  static const Scalar* getData(const T& t) { return t.getData(); }
201  static Scalar* updData(T& t) { return t.updData(); }
202 
203  static const TReal& real(const T& t) { return t.real(); }
204  static TReal& real(T& t) { return t.real(); }
205  static const TImag& imag(const T& t) { return t.imag(); }
206  static TImag& imag(T& t) { return t.imag(); }
207 
208  // We expect to be able to negate and transpose (hermitian or
209  // positional) with just type casting; no need for help from class
210  // K except to tell us the appropriate types.
211  static const TNeg& negate(const T& t)
212  { return reinterpret_cast<const TNeg&>(t); }
213  static TNeg& negate(T& t)
214  { return reinterpret_cast<TNeg&>(t); }
215 
216  static const THerm& transpose(const K& t)
217  { return reinterpret_cast<const THerm&>(t); }
218  static THerm& transpose(K& t)
219  { return reinterpret_cast<THerm&>(t); }
220 
221  static const TPosTrans& positionalTranspose(const K& t)
222  { return reinterpret_cast<const TPosTrans&>(t); }
224  { return reinterpret_cast<TPosTrans&>(t); }
225 
226  // If the underlying scalars of this CNT are negator<N> for some numeric type N,
227  // this method removes the negator<>, effectively negating the entire CNT. You
228  // can still deal with the sign correctly by using the above enum SignInterpretation
229  // which will be -1 in that case, 1 if there was no negator<> to remove. Note:
230  // I'm not talking about TWithoutNegator::SignInterpretation -- that one is guaranteed
231  // to be 1! T::SignInterpretation is the one you want.
232  static const TWithoutNegator& castAwayNegatorIfAny(const T& t)
233  {return reinterpret_cast<const TWithoutNegator&>(t);}
235  {return reinterpret_cast<TWithoutNegator&>(t);}
236 
237  static ScalarNormSq scalarNormSqr(const K& t) {return t.scalarNormSqr();}
238 
239  static TSqrt sqrt(const K& t) {return t.sqrt();}
240  static TAbs abs(const K& t) {return t.abs();}
241  static TStandard standardize(const K& t) {return t.standardize();}
242  static TNormalize normalize(const K& t) {return t.normalize();}
243  static TInvert invert(const K& t) {return t.invert();}
244 
245  static K getInfinity() {return K::getInfinity();}
246  static K getNaN() {return K::getNaN();}
247 
249  static bool isNaN(const K& t) {return t.isNaN();}
252  static bool isInf(const K& t) {return t.isInf();}
254  static bool isFinite(const K& t) {return t.isFinite();}
255 
263  template <class K2> static bool
264  isNumericallyEqual(const K& t1, const K2& t2)
265  { return t1.isNumericallyEqual(t2);}
266  template <class K2> static bool
267  isNumericallyEqual(const K& t1, const K2& t2, double tol)
268  { return t1.isNumericallyEqual(t2,tol);}
269  static double getDefaultTolerance() {return K::getDefaultTolerance();}
270 
271 };
272 
273 } // namespace SimTK
274 
275 #endif // SimTK_SIMMATRIX_COMPOSITE_NUMERICAL_TYPES_H_
SimTK::CNT::abs
static TAbs abs(const K &t)
Definition: CompositeNumericalTypes.h:240
SimTK::CNT::IsStdNumber
@ IsStdNumber
Definition: CompositeNumericalTypes.h:195
SimTK::CNT::getData
static const Scalar * getData(const T &t)
Definition: CompositeNumericalTypes.h:200
SimTK::CNT::invert
static TInvert invert(const K &t)
Definition: CompositeNumericalTypes.h:243
SimTK::CNT::sqrt
static TSqrt sqrt(const K &t)
Definition: CompositeNumericalTypes.h:239
SimTK::CNT::Number
K::Number Number
Definition: CompositeNumericalTypes.h:162
SimTK::CNT::TSqHermT
K::TSqHermT TSqHermT
Definition: CompositeNumericalTypes.h:146
SimTK::CNT::TElement
K::TElement TElement
Definition: CompositeNumericalTypes.h:148
SimTK::CNT::isInf
static bool isInf(const K &t)
This is true if at least one element contains a +Infinity or -Infinity and no element contains a NaN.
Definition: CompositeNumericalTypes.h:252
SimTK::CNT::TImag
K::TImag TImag
Definition: CompositeNumericalTypes.h:142
SimTK::CNT::TSqTHerm
K::TSqTHerm TSqTHerm
Definition: CompositeNumericalTypes.h:147
SimTK::CNT::ULessScalar
K::ULessScalar ULessScalar
Definition: CompositeNumericalTypes.h:161
SimTK::CNT::negate
static const TNeg & negate(const T &t)
Definition: CompositeNumericalTypes.h:211
common.h
SimTK::CNT::transpose
static THerm & transpose(K &t)
Definition: CompositeNumericalTypes.h:218
SimTK
This is a System that represents the dynamics of a particle moving along a smooth surface.
Definition: Assembler.h:37
SimTK::CNT::updData
static Scalar * updData(T &t)
Definition: CompositeNumericalTypes.h:201
SimTK::CNT::TInvert
K::TInvert TInvert
Definition: CompositeNumericalTypes.h:157
SimTK::CNT::RowSpacing
@ RowSpacing
Definition: CompositeNumericalTypes.h:184
SimTK::CNT::imag
static TImag & imag(T &t)
Definition: CompositeNumericalTypes.h:206
SimTK::MAX_RESOLVED_DEPTH
@ MAX_RESOLVED_DEPTH
Definition: CompositeNumericalTypes.h:120
SimTK::CNT::transpose
static const THerm & transpose(const K &t)
Definition: CompositeNumericalTypes.h:216
SimTK::CNT::ScalarNormSq
K::ScalarNormSq ScalarNormSq
Definition: CompositeNumericalTypes.h:166
SimTK::CNT::Precision
K::Precision Precision
Definition: CompositeNumericalTypes.h:164
SimTK::CNT::Substitute::Type
K::template Substitute< P >::Type Type
Definition: CompositeNumericalTypes.h:178
SimTK::CNT::Scalar
K::Scalar Scalar
Definition: CompositeNumericalTypes.h:160
SimTK::COMPOSITE_COMPOSITE_DEPTH
@ COMPOSITE_COMPOSITE_DEPTH
Definition: CompositeNumericalTypes.h:118
SimTK::CNT::Result::Mul
K::template Result< P >::Mul Mul
Definition: CompositeNumericalTypes.h:170
SimTK::CNT::negate
static TNeg & negate(T &t)
Definition: CompositeNumericalTypes.h:213
SimTK::CNT::isNumericallyEqual
static bool isNumericallyEqual(const K &t1, const K2 &t2, double tol)
Definition: CompositeNumericalTypes.h:267
SimTK::CNT::Result::Sub
K::template Result< P >::Sub Sub
Definition: CompositeNumericalTypes.h:173
SimTK::CNT::positionalTranspose
static const TPosTrans & positionalTranspose(const K &t)
Definition: CompositeNumericalTypes.h:221
SimTK::CNT::TCol
K::TCol TCol
Definition: CompositeNumericalTypes.h:150
SimTK::CNT::NCols
@ NCols
Definition: CompositeNumericalTypes.h:183
SimTK::CNT::isNumericallyEqual
static bool isNumericallyEqual(const K &t1, const K2 &t2)
CNTs are expected to support an "==" operator for exact, bitwise equality.
Definition: CompositeNumericalTypes.h:264
SimTK::CNT::TNormalize
K::TNormalize TNormalize
Definition: CompositeNumericalTypes.h:158
SimTK::CNT::getDefaultTolerance
static double getDefaultTolerance()
Definition: CompositeNumericalTypes.h:269
SimTK::CNT::positionalTranspose
static TPosTrans & positionalTranspose(K &t)
Definition: CompositeNumericalTypes.h:223
SimTK::CNT::updCastAwayNegatorIfAny
static TWithoutNegator & updCastAwayNegatorIfAny(T &t)
Definition: CompositeNumericalTypes.h:234
SimTK::CNT::TPosTrans
K::TPosTrans TPosTrans
Definition: CompositeNumericalTypes.h:145
SimTK::CNT::StdNumber
K::StdNumber StdNumber
Definition: CompositeNumericalTypes.h:163
SimTK::CNT::real
static TReal & real(T &t)
Definition: CompositeNumericalTypes.h:204
SimTK::CNT::standardize
static TStandard standardize(const K &t)
Definition: CompositeNumericalTypes.h:241
SimTK::CNT::scalarNormSqr
static ScalarNormSq scalarNormSqr(const K &t)
Definition: CompositeNumericalTypes.h:237
SimTK::CNT::Result::Dvd
K::template Result< P >::Dvd Dvd
Definition: CompositeNumericalTypes.h:171
SimTK::CNT::isNaN
static bool isNaN(const K &t)
This is true if any element contains a NaN anywhere.
Definition: CompositeNumericalTypes.h:249
SimTK::CNT::TRow
K::TRow TRow
Definition: CompositeNumericalTypes.h:149
SimTK::CNT::ImagOffset
@ ImagOffset
Definition: CompositeNumericalTypes.h:189
SimTK::CNT
Specialized information about Composite Numerical Types which allows us to define appropriate templat...
Definition: CompositeNumericalTypes.h:136
SimTK::CNT::TComplex
K::TComplex TComplex
Definition: CompositeNumericalTypes.h:143
SimTK::CNT::ColSpacing
@ ColSpacing
Definition: CompositeNumericalTypes.h:185
SimTK::COMPOSITE_3_DEPTH
@ COMPOSITE_3_DEPTH
Definition: CompositeNumericalTypes.h:119
SimTK::CNT::IsNumber
@ IsNumber
Definition: CompositeNumericalTypes.h:194
SimTK::CNT::IsPrecision
@ IsPrecision
Definition: CompositeNumericalTypes.h:196
SimTK::CNT::imag
static const TImag & imag(const T &t)
Definition: CompositeNumericalTypes.h:205
SimTK::CNT::Result::Add
K::template Result< P >::Add Add
Definition: CompositeNumericalTypes.h:172
SimTK::CNT::NPackedElements
@ NPackedElements
Definition: CompositeNumericalTypes.h:186
SimTK::CNT::THerm
K::THerm THerm
Definition: CompositeNumericalTypes.h:144
SimTK::CNT::RealStrideFactor
@ RealStrideFactor
Definition: CompositeNumericalTypes.h:190
SimTK::CNT::Result
Definition: CompositeNumericalTypes.h:169
SimTK::CNT::TAbs
K::TAbs TAbs
Definition: CompositeNumericalTypes.h:155
SimTK::CNT::IsScalar
@ IsScalar
Definition: CompositeNumericalTypes.h:192
SimTK::CNT::IsULessScalar
@ IsULessScalar
Definition: CompositeNumericalTypes.h:193
SimTK::CNT::getNaN
static K getNaN()
Definition: CompositeNumericalTypes.h:246
SimTK::CNT::Substitute
Definition: CompositeNumericalTypes.h:177
SimTK::CNT::real
static const TReal & real(const T &t)
Definition: CompositeNumericalTypes.h:203
SimTK::CNT::castAwayNegatorIfAny
static const TWithoutNegator & castAwayNegatorIfAny(const T &t)
Definition: CompositeNumericalTypes.h:232
SimTK::CNT::TStandard
K::TStandard TStandard
Definition: CompositeNumericalTypes.h:156
SimTK::CNT::NActualScalars
@ NActualScalars
Definition: CompositeNumericalTypes.h:188
SimTK::SCALAR_DEPTH
@ SCALAR_DEPTH
Definition: CompositeNumericalTypes.h:116
SimTK::CNT::NRows
@ NRows
Definition: CompositeNumericalTypes.h:182
SimTK::CNT::ArgDepth
@ ArgDepth
Definition: CompositeNumericalTypes.h:191
SimTK::CNT::TSqrt
K::TSqrt TSqrt
Definition: CompositeNumericalTypes.h:154
SimTK::CNT::T
K T
Definition: CompositeNumericalTypes.h:138
SimTK::CNT::TReal
K::TReal TReal
Definition: CompositeNumericalTypes.h:141
SimTK::CNT::TWithoutNegator
K::TWithoutNegator TWithoutNegator
Definition: CompositeNumericalTypes.h:140
SimTK::SCALAR_COMPOSITE_DEPTH
@ SCALAR_COMPOSITE_DEPTH
Definition: CompositeNumericalTypes.h:117
SimTK::CNT::isFinite
static bool isFinite(const K &t)
This is true only if no element has any entry that it NaN or Infinity.
Definition: CompositeNumericalTypes.h:254
SimTK::CNT::TNeg
K::TNeg TNeg
Definition: CompositeNumericalTypes.h:139
SimTK::CNT::NActualElements
@ NActualElements
Definition: CompositeNumericalTypes.h:187
SimTK::CNT::getInfinity
static K getInfinity()
Definition: CompositeNumericalTypes.h:245
SimTK::CNT::normalize
static TNormalize normalize(const K &t)
Definition: CompositeNumericalTypes.h:242
SimTK::CNT::SignInterpretation
@ SignInterpretation
Definition: CompositeNumericalTypes.h:197