namespace ewalena  0.2.15
ewalena is not an acronym
include/ewalena/base/tensor_tools.h
Go to the documentation of this file.
00001 // -------------------------------------------------------------------
00002 // @author Toby D. Young
00003 // @version $Id: tensor.h 808 2012-06-27 18:32:20Z oneliefleft $
00004 //
00005 // Copyright 2012 namespace ewalena authors. All rights reserved.
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions
00009 // are met:
00010 //
00011 // 1. Redistributions of source code must retain the above copyright
00012 //    notice, this list of conditions and the following disclaimer.
00013 //
00014 // 2. Redistributions in binary form must reproduce the above
00015 //    copyright notice, this list of conditions and the following
00016 //    disclaimer in the documentation and/or other materials provided
00017 //    with the distribution.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE NAMEPSACE EWALENA AUTHORS ``AS
00020 // IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023 // NAMESPACE EWALENA AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00024 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00026 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00028 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00030 // OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // The views and conclusions contained in the software and
00033 // documentation are those of the authors and should not be
00034 // interpreted as representing official policies, either expressed or
00035 // implied, of the namespace ewalena authors.
00036 // -------------------------------------------------------------------
00037 
00038 #include <cassert>
00039 #include <map>
00040 #include <cmath>
00041 #include <iostream>
00042 
00043 #ifndef __ewalena_tensor_tools_h
00044 #define __ewalena_tensor_tools_h
00045 
00046 #ifdef EWALENA_HAVE_CONFIG_H
00047 #include <ewalena/base/config.h>
00048 #endif
00049 
00050 #include <ewalena/base/tensor.h>
00051 
00052 #include <ewalena/lac/matrix.h>
00053 
00054 namespace ewalena
00055 {
00056   
00057   namespace TensorTools
00058   {
00059 
00068     class VoightMap
00069     {
00070       
00071     public:
00072       
00076       typedef std::map<std::pair<const unsigned int, const unsigned int>, const unsigned int> IndexSetMap;
00077       
00082       static IndexSetMap voight_map;
00083       
00087       typedef IndexSetMap::iterator VoightMapIterator;
00088       
00092       inline
00093         VoightMap operator* () const
00094       {
00095         return (*this);
00096       }
00097       
00101       inline
00102         VoightMapIterator begin () const
00103       {
00104         assert (!voight_map.empty ());
00105         return voight_map.begin ();
00106       }
00107       
00111       inline
00112         VoightMapIterator end () const
00113       {
00114         assert (!voight_map.empty ());
00115         return voight_map.end ();
00116       }
00117       
00122       inline
00123         unsigned int operator () (const unsigned int i, const unsigned int j) const
00124       {
00125         VoightMapIterator iterator = voight_map.find ({i, j});
00126         assert (iterator != voight_map.end ()); 
00127 
00128         return (iterator)->second;
00129       }      
00130 
00131     };
00132 
00133     /*-------------- Inline and Other Functions -----------------------*/
00134 
00140     template <int dim, typename ValueType>
00141       Matrix<ValueType> voight_fold (const Tensor<dim, 2, ValueType> &T)
00142     {
00143       Matrix<ValueType> M (T.voight_components ());
00144       
00145       TensorTools::VoightMap voight_map; 
00146 
00147       for (unsigned int i=0; i<dim; ++i)  
00148         for (unsigned int j=i; j<dim; ++j)  
00149           {
00150             assert (T(i,j) == T(j,i));
00151             M(0,voight_map(i,j)) += T(i,j); 
00152           }
00153 
00154       return M; 
00155     } 
00156     
00168     template <int dim, typename ValueType>
00169       Matrix<ValueType> voight_fold (const Tensor<dim, 3, ValueType> &T,
00170                                      const unsigned int               symmetry_direction = 0)
00171     {
00172        Matrix<ValueType> M; 
00173 
00174        TensorTools::VoightMap voight_map; 
00175 
00176        switch (symmetry_direction) 
00177         { 
00178         case 0: 
00179           M.reinit (T.voight_components ().first, T.voight_components ().second); 
00180 
00181           for (unsigned int i=0; i<dim; ++i)  
00182             for (unsigned int j=0; j<dim; ++j)  
00183               for (unsigned int k=j; k<dim; ++k)  
00184                 { 
00185                   assert (T(i,j,k) == T(i,k,j));  
00186                   M(i,voight_map(j, k)) += T(i,j,k);  
00187                 } 
00188           break; 
00189           
00190         case 1: 
00191           M.reinit (T.voight_components ().second, T.voight_components ().first); 
00192           
00193           for (unsigned int i=0; i<dim; ++i)  
00194             for (unsigned int j=0; j<dim; ++j)  
00195               for (unsigned int k=j; k<dim; ++k)  
00196                 { 
00197                   assert (T(i,j,k) == T(j,i,k)); 
00198                   M(voight_map(i, j), k) += T(i,j,k);  
00199                 } 
00200           
00201           break; 
00202           
00203         default: 
00204           assert (false); 
00205         } 
00206        
00207        return M;
00208     }  
00209     
00215     template <int dim, typename ValueType>
00216       Matrix<ValueType> voight_fold (const Tensor<dim, 4, ValueType> &T)
00217     {
00218       Matrix<ValueType> M (T.voight_components ());      
00219       TensorTools::VoightMap voight_map;
00220 
00221       for (unsigned int i=0; i<dim; ++i) 
00222         for (unsigned int j=i; j<dim; ++j) 
00223           for (unsigned int k=0; k<dim; ++k) 
00224             for (unsigned int l=k; l<dim; ++l) 
00225               {
00226                 assert (T(i,j,k,l) == T(j,i,l,k));
00227                 M(voight_map(i, j), voight_map(k, l)) += T(i,j,k,l); 
00228               }
00229       
00230        return M;
00231     }  
00232 
00233   } /* namespace TensorTools */
00234 
00235 } /* namespace ewalena */
00236   
00237 #endif /* __ewalena_tensor_tools_h */
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines