Commit 8ad45a03 authored by Kenneth Vanhoey's avatar Kenneth Vanhoey

Add tensor features

parent 6b553f26
...@@ -107,6 +107,15 @@ class Tensor ...@@ -107,6 +107,15 @@ class Tensor
*/ */
void identity() ; void identity() ;
/**
* \brief Modifier: set constant values
*
* Sets all values to r
*
* \param r the constant value
*/
void setConst(const REAL& r) ;
/** /**
* \brief Modifier: copy Tensor * \brief Modifier: copy Tensor
* *
......
...@@ -39,6 +39,9 @@ Tensor<SIZE, REAL>::Tensor(const Tensor& T): ...@@ -39,6 +39,9 @@ Tensor<SIZE, REAL>::Tensor(const Tensor& T):
m_order(T.m_order) m_order(T.m_order)
{ {
m_data = new REAL[T.nbElem()] ; m_data = new REAL[T.nbElem()] ;
for (unsigned int i = 0 ; i < T.nbElem() ; ++i)
m_data[i] = T[i] ;
} }
template <unsigned int SIZE, typename REAL> template <unsigned int SIZE, typename REAL>
...@@ -65,24 +68,37 @@ void Tensor<SIZE, REAL>::identity() ...@@ -65,24 +68,37 @@ void Tensor<SIZE, REAL>::identity()
template <unsigned int SIZE, typename REAL> template <unsigned int SIZE, typename REAL>
void void
Tensor<SIZE, REAL>::operator=(const Tensor& T) Tensor<SIZE, REAL>::zero()
{ {
m_order = T.m_order ; for (unsigned int i = 0 ; i < (unsigned int)pow(SIZE,m_order) ; ++i)
m_data = new REAL[T.nbElem()] ; {
for (unsigned int i = 0 ; i < T.nbElem() ; ++i) m_data[i] = 0 ;
m_data[i] = T[i] ; }
} }
template <unsigned int SIZE, typename REAL> template <unsigned int SIZE, typename REAL>
void void
Tensor<SIZE, REAL>::zero() Tensor<SIZE, REAL>::setConst(const REAL& r)
{ {
for (unsigned int i = 0 ; i < (unsigned int)pow(SIZE,m_order) ; ++i) for (unsigned int i = 0 ; i < nbElem() ; ++i)
{ {
m_data[i] = 0 ; m_data[i] = r ;
} }
} }
template <unsigned int SIZE, typename REAL>
void
Tensor<SIZE, REAL>::operator=(const Tensor& T)
{
m_order = T.m_order ;
delete(m_data) ;
m_data = new REAL[T.nbElem()] ;
for (unsigned int i = 0 ; i < T.nbElem() ; ++i)
m_data[i] = T[i] ;
}
template <unsigned int SIZE, typename REAL> template <unsigned int SIZE, typename REAL>
const REAL& const REAL&
Tensor<SIZE, REAL>::operator()(std::vector<unsigned int> p) const Tensor<SIZE, REAL>::operator()(std::vector<unsigned int> p) const
...@@ -153,13 +169,13 @@ template <unsigned int SIZE, typename REAL> ...@@ -153,13 +169,13 @@ template <unsigned int SIZE, typename REAL>
bool bool
Tensor<SIZE, REAL>::incremIndex(std::vector<unsigned int>& p) Tensor<SIZE, REAL>::incremIndex(std::vector<unsigned int>& p)
{ {
unsigned int i = 0 ; int i = p.size() - 1 ;
while (i < p.size()) while (i >= 0)
{ {
p[i] = (p[i] + 1) % SIZE ; p[i] = (p[i] + 1) % SIZE ;
if (p[i] != 0) // if no overflow if (p[i] != 0) // if no overflow
return true ; return true ;
++i ; --i ;
} }
return false ; return false ;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment