CompositeNumericalTypes.h

Go to the documentation of this file.
00001 #ifndef SimTK_SIMMATRIX_COMPOSITE_NUMERICAL_TYPES_H_
00002 #define SimTK_SIMMATRIX_COMPOSITE_NUMERICAL_TYPES_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTK Simmatrix(tm)                       *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core biosimulation toolkit originating from      *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2005-7 Stanford University and the Authors.         *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00033  * -------------------------------------------------------------------------- */
00034 
00173 #include "SimTKcommon/internal/common.h"
00174     
00175 namespace SimTK {
00176 
00177 // These are CNT "depths". 0 means the corresponding CNT is a scalar,
00178 // 1 means it is a composite with scalar elements, 2 means a composite
00179 // with composite elements, and 3 means a composite with depth-2
00180 // composite elements. Beyond that the user will have to diambiguate
00181 // operations by using named routines rather than operators.
00182 enum  {
00183     SCALAR_DEPTH              = 0,
00184     SCALAR_COMPOSITE_DEPTH    = 1,
00185     COMPOSITE_COMPOSITE_DEPTH = 2,
00186     COMPOSITE_3_DEPTH         = 3,
00187     MAX_RESOLVED_DEPTH        = COMPOSITE_3_DEPTH
00188 };
00189 
00203 template <class K> class CNT : private K {
00204 public:
00205     typedef K                        T;
00206     typedef typename K::TNeg         TNeg;
00207     typedef typename K::TWithoutNegator TWithoutNegator;
00208     typedef typename K::TReal        TReal;
00209     typedef typename K::TImag        TImag;
00210     typedef typename K::TComplex     TComplex;
00211     typedef typename K::THerm        THerm;
00212     typedef typename K::TPosTrans    TPosTrans;
00213     typedef typename K::TSqHermT     TSqHermT;
00214     typedef typename K::TSqTHerm     TSqTHerm;
00215     typedef typename K::TElement     TElement;
00216     typedef typename K::TRow         TRow;          // type of a row or column
00217     typedef typename K::TCol         TCol;
00218 
00219     // These are the results of calculations and should be packed regardless
00220     // of the spacing of this CNT.
00221     typedef typename K::TAbs         TAbs;
00222     typedef typename K::TStandard    TStandard;     // packed, StdNumbers
00223     typedef typename K::TInvert      TInvert;
00224     typedef typename K::TNormalize   TNormalize;
00225 
00226     typedef typename K::Scalar       Scalar;        // <number> or negator<number>
00227     typedef typename K::Number       Number;        // <real>, <complex> or <conjugate>
00228     typedef typename K::StdNumber    StdNumber;     // <real>, <complex>
00229     typedef typename K::Precision    Precision;     // float, double, long double
00230 
00231     typedef typename K::ScalarSq     ScalarSq;      // type of square of scalar or
00232                                                     //   numeric value
00233 
00234     template <class P> struct Result {
00235         typedef typename K::template Result<P>::Mul Mul;
00236         typedef typename K::template Result<P>::Dvd Dvd;;
00237         typedef typename K::template Result<P>::Add Add;
00238         typedef typename K::template Result<P>::Sub Sub;
00239     };
00240 
00241     // Shape-preserving element substitution
00242     template <class P> struct Substitute {
00243         typedef typename K::template Substitute<P>::Type Type;
00244     };
00245 
00246     enum {
00247         NRows               = K::NRows,
00248         NCols               = K::NCols,
00249         RowSpacing          = K::RowSpacing,
00250         ColSpacing          = K::ColSpacing,
00251         NPackedElements     = K::NPackedElements,
00252         NActualElements     = K::NActualElements,
00253         NActualScalars      = K::NActualScalars,
00254         ImagOffset          = K::ImagOffset,
00255         RealStrideFactor    = K::RealStrideFactor,
00256         ArgDepth            = K::ArgDepth,
00257         IsScalar            = K::IsScalar,          // real, complex, conjugate, negator
00258         IsNumber            = K::IsNumber,          // real, complex, conjugate
00259         IsStdNumber         = K::IsStdNumber,       // real, complex
00260         IsPrecision         = K::IsPrecision,       // real (float, double, long double)
00261         SignInterpretation  = K::SignInterpretation // 1 normally, -1 if elements are negated
00262     };
00263 
00264     static const Scalar* getData(const T& t) { return t.getData(); }
00265     static       Scalar* updData(T& t)       { return t.updData(); }
00266 
00267     static const TReal& real(const T& t) { return t.real(); }
00268     static       TReal& real(T& t)       { return t.real(); }
00269     static const TImag& imag(const T& t) { return t.imag(); }
00270     static       TImag& imag(T& t)       { return t.imag(); }
00271 
00272     static K getInfinity() { return K::getInfinity(); }
00273     static K getNaN()      { return K::getNaN();      }
00274 
00275     // We expect to be able to negate and transpose (hermitian or
00276     // positional) with just type casting; no need for help from class
00277     // K except to tell us the appropriate types.
00278     static const TNeg& negate(const T& t)
00279       { return reinterpret_cast<const TNeg&>(t); }
00280     static       TNeg& negate(T& t)
00281       { return reinterpret_cast<TNeg&>(t); }
00282 
00283     static const THerm& transpose(const K& t)
00284       { return reinterpret_cast<const THerm&>(t); }
00285     static       THerm& transpose(K& t)
00286       { return reinterpret_cast<THerm&>(t); }
00287 
00288     static const TPosTrans& positionalTranspose(const K& t)
00289       { return reinterpret_cast<const TPosTrans&>(t); }
00290     static       TPosTrans& positionalTranspose(K& t)
00291       { return reinterpret_cast<TPosTrans&>(t); }
00292 
00293     // If the underlying scalars of this CNT are negator<N> for some numeric type N,
00294     // this method removes the negator<>, effectively negating the entire CNT. You
00295     // can still deal with the sign correctly by using the above enum SignInterpretation
00296     // which will be -1 in that case, 1 if there was no negator<> to remove. Note:
00297     // I'm not talking about TWithoutNegator::SignInterpretation -- that one is guaranteed
00298     // to be 1! T::SignInterpretation is the one you want.
00299     static const TWithoutNegator& castAwayNegatorIfAny(const T& t)
00300         {return reinterpret_cast<const TWithoutNegator&>(t);}
00301     static       TWithoutNegator& updCastAwayNegatorIfAny(T& t)
00302         {return reinterpret_cast<TWithoutNegator&>(t);}
00303 
00304     static ScalarSq   scalarNormSqr(const K& t) {return t.scalarNormSqr();}
00305     static TAbs       abs(const K& t)           {return t.abs();}
00306     static TStandard  standardize(const K& t)   {return t.standardize();}
00307     static TNormalize normalize(const K& t)     {return t.normalize();}
00308     static TInvert    invert(const K& t)        {return t.invert();}
00309 };
00310 
00311 } // namespace SimTK
00312 
00313 #endif // SimTK_SIMMATRIX_COMPOSITE_NUMERICAL_TYPES_H_

Generated on Thu Feb 28 01:34:28 2008 for SimTKcommon by  doxygen 1.4.7