SCOREC core
Parallel unstructured mesh tools
mthTensor.h
Go to the documentation of this file.
1 /******************************************************************************
2 
3  Copyright 2015 Scientific Computation Research Center,
4  Rensselaer Polytechnic Institute. All rights reserved.
5 
6  This work is open source software, licensed under the terms of the
7  BSD license as described in the LICENSE file in the top-level directory.
8 
9 *******************************************************************************/
10 
11 #ifndef MTH_TENSOR_H
12 #define MTH_TENSOR_H
13 
14 #include "mthMatrix.h"
15 #include <pcu_util.h>
16 
20 namespace mth {
21 
27 template <class T>
28 class Tensor : public Matrix<T>
29 {
30  public:
32  Tensor() : Matrix<T>() {}
34  Tensor(unsigned d) : mth::Matrix<T>(d,d)
35  {
36  PCU_ALWAYS_ASSERT((d==2) || (d==3));
37  }
39  Tensor(Tensor<T> const& b) : mth::Matrix<T>() {copy(b);}
41  Tensor(Matrix<T> const& b) : mth::Matrix<T>() {copy(b);}
44  {
45  (*this).copy(b);
46  return *this;
47  }
50  {
51  Tensor<T> r;
52  r.copy(b);
53  return r;
54  }
57  {
58  Tensor<T> r(dim());
59  for (unsigned i=0; i < dim(); ++i)
60  for (unsigned j=0; j < dim(); ++j)
61  r(i,j) = (*this)(i,j) + b(i,j);
62  return r;
63  }
66  {
67  Tensor<T> r(dim());
68  for (unsigned i=0; i < dim(); ++i)
69  for (unsigned j=0; j < dim(); ++j)
70  r(i,j) = (*this)(i,j) - b(i,j);
71  return r;
72  }
75  {
76  Tensor<T> r(dim());
77  for (unsigned i=0; i < dim(); ++i)
78  for (unsigned j=0; j < dim(); ++j)
79  {
80  r(i,j) = (*this)(i,0) * b(0,j);
81  for (unsigned k=1; k < dim(); ++k)
82  r(i,j) += (*this)(i,k) * b(k,j);
83  }
84  return r;
85  }
87  Tensor<T> operator*(T const& s)
88  {
89  Tensor<T> r(dim());
90  for (unsigned i=0; i < dim(); ++i)
91  for (unsigned j=0; j < dim(); ++j)
92  r(i,j) = (*this)(i,j) * s;
93  return r;
94  }
96  Tensor<T> operator/(T const& s)
97  {
98  Tensor<T> r(dim());
99  for (unsigned i=0; i < dim(); ++i)
100  for (unsigned j=0; j < dim(); ++j)
101  r(i,j) = (*this)(i,j) / s;
102  return r;
103  }
106  void resize(unsigned d)
107  {
108  PCU_ALWAYS_ASSERT((d==2) || (d==3));
109  this->columns = d;
110  this->elems.resize(d*d);
111  }
115  void resize(unsigned m, unsigned n)
116  {
117  PCU_ALWAYS_ASSERT(m==n);
118  PCU_ALWAYS_ASSERT((m==2) || (m==3));
119  this->columns = m;
120  this->elems.resize(m*m);
121  }
123  unsigned dim() const {return this->columns;}
124  private:
125  void copy(Tensor<T> const& b)
126  {
127  unsigned d = b.dim();
128  resize(d);
129  for (unsigned i=0; i < d; ++i)
130  for (unsigned j=0; j < d; ++j)
131  (*this)(i,j) = b(i,j);
132  }
133  void copy(Matrix<T> const& b)
134  {
135  unsigned m = b.rows();
136  unsigned n = b.cols();
137  resize(m,n);
138  for (unsigned i=0; i < m; ++i)
139  for (unsigned j=0; j < m; ++j)
140  (*this)(i,j) = b(i,j);
141  }
142 };
143 
144 }
145 
146 #endif
compile-time (static) matrix
Definition: mthMatrix.h:24
run-time (dynamic) tensor
Definition: mthTensor.h:29
Tensor< T > & operator=(Tensor< T > const &b)
assignment operator
Definition: mthTensor.h:43
Tensor()
default constructor
Definition: mthTensor.h:32
Tensor< T > operator*(Tensor< T > const &b)
multiply a tensor by a tensor
Definition: mthTensor.h:74
Tensor< T > operator/(T const &s)
divide a tensor by a scalar
Definition: mthTensor.h:96
Tensor< T > operator=(Matrix< T > const &b)
assignent to a matrix
Definition: mthTensor.h:49
void resize(unsigned m, unsigned n)
resize this tensor
Definition: mthTensor.h:115
Tensor< T > operator*(T const &s)
multiply a tensor by a scalar
Definition: mthTensor.h:87
Tensor(unsigned d)
construct with dimension d
Definition: mthTensor.h:34
Tensor< T > operator+(Tensor< T > const &b)
add a tensor to a tensor
Definition: mthTensor.h:56
Tensor(Tensor< T > const &b)
copy constructor
Definition: mthTensor.h:39
Tensor(Matrix< T > const &b)
copy from a matrix
Definition: mthTensor.h:41
void resize(unsigned d)
resize this tensor to dimension d
Definition: mthTensor.h:106
Tensor< T > operator-(Tensor< T > const &b)
subtract a tensor from a tensor
Definition: mthTensor.h:65
unsigned dim() const
get the dimension of this tensor
Definition: mthTensor.h:123
Small compile-time and run-time linear algebra matrices.
All MTH functions are contained in this namespace.