Page MenuHomec4science

tensor.hh
No OneTemporary

File Metadata

Created
Sun, Aug 4, 14:24

tensor.hh

/**
* @file tensor.hh
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Wed May 01 17:45:41 2013
*
* @brief This is a tensor representation
*
* @section LICENSE
*
* Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* LibMultiScale is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* LibMultiScale is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with LibMultiScale. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __LIBMULTISCALE_TENSOR_HH__
#define __LIBMULTISCALE_TENSOR_HH__
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
class Tensor {
public:
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
Tensor(){
for (UInt i=0; i < 9; ++i) vec[i] = 0.0;
};
Tensor(Real val){
for (UInt i=0; i < 9; ++i) vec[i] = val;
};
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
void operator+=(Tensor & t){
for (UInt i=0; i < 9; ++i) vec[i]+=t(i);
}
void operator-=(Tensor & t){
for (UInt i=0; i < 9; ++i) vec[i] = t(i);
}
template <typename T>
void operator/=(T v){
for (UInt i=0; i < 9; ++i) vec[i]/=v;
}
template <typename T>
void operator*=(T v){
for (UInt i=0; i < 9; ++i) vec[i]*=v;
}
void operator*=(Tensor v){
for (UInt i=0; i < 9; ++i) vec[i]*=v(i);
}
void square(){
for (UInt i=0; i < 9; ++i) vec[i] = vec[i]*vec[i];
}
Real & operator()(UInt i){return vec[i];}
Real operator()(UInt i) const {return vec[i];}
Tensor & operator=(Real v){
for (UInt i=0; i < 9; ++i) vec[i] = v;
return *this;
}
Real * getVec(){return vec;};
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
private:
Real vec[9];
};
/* -------------------------------------------------------------------------- */
inline std::ostream& operator << (std::ostream& os,Tensor & t){
for (UInt i = 0; i < 9; ++i) {
os << t(i) << " ";
}
return os;
}
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__
namespace std {
template <> const inline ::libmultiscale::Tensor & max(const ::libmultiscale::Tensor& a,
const ::libmultiscale::Tensor& b) {
::libmultiscale::Real na = 0., nb = 0.;
for (::libmultiscale::UInt i = 0; i < 9; ++i) {
na += a(i)*a(i);
nb += b(i)*b(i);
}
if (na > nb) return a;
else return b;
}
template <> const inline ::libmultiscale::Tensor & min (const ::libmultiscale::Tensor& a,
const ::libmultiscale::Tensor& b) {
::libmultiscale::Real na = 0., nb = 0.;
for (::libmultiscale::UInt i = 0; i < 9; ++i) {
na += a(i)*a(i);
nb += b(i)*b(i);
}
if (na > nb) return b;
else return a;
}
}
/* -------------------------------------------------------------------------- */
#endif /* __LIBMULTISCALE_TENSOR_HH__ */

Event Timeline