PLearn 0.1
Public Types | Public Member Functions | Protected Attributes | Friends
PLearn::TTensor< T > Class Template Reference

#include <Tensor.h>

Collaboration diagram for PLearn::TTensor< T >:
Collaboration graph
[legend]

List of all members.

Public Types

typedef TTensorElementIterator< T > iterator
typedef
TTensorSubTensorIterator< T > 
subTensorIterator

Public Member Functions

 TTensor ()
 TTensor (IVec len)
 TTensor (const IVec &len, const T &init_value)
int resize (const IVec &len)
 don't call this from a subTensor !!
int ndims () const
IVec width () const
IVec sizes () const
int size (int k) const
int totalElements () const
int linearIndex (const IVec &pos) const
int linearIndex (const vector< int > &pos) const
TTensor< T > subTensor (const IVec &from, const IVec &len)
TTensor< T > DEPRECATEDsubTensor (const IVec &from, const IVec &len, bool throw_useless_dimensions=true)
TTensor< T > operator[] (int i)
void selectDimensions (const IVec &dim)
T & operator() (const IVec &pos) const
T & operator() (const vector< int > &pos) const
void fill (const T &val)
T * data () const
IVec lastElementPos () const
iterator begin ()
 returns an iterator over elements
iterator end ()
subTensorIterator getSubTensorIterator (const IVec &v)
 returns an iterator over subTensors

Protected Attributes

int offset_
IVec stride_
IVec width_
PP< Storage< T > > storage

Friends

class TTensorElementIterator< T >
class TTensorSubTensorIterator< T >

Detailed Description

template<class T>
class PLearn::TTensor< T >

Definition at line 178 of file Tensor.h.


Member Typedef Documentation

template<class T>
typedef TTensorElementIterator<T> PLearn::TTensor< T >::iterator

Definition at line 189 of file Tensor.h.

template<class T>
typedef TTensorSubTensorIterator<T> PLearn::TTensor< T >::subTensorIterator

Definition at line 190 of file Tensor.h.


Constructor & Destructor Documentation

template<class T>
PLearn::TTensor< T >::TTensor ( ) [inline]

Definition at line 192 of file Tensor.h.

        :offset_(0), stride_(0), width_(0)
    {}
template<class T>
PLearn::TTensor< T >::TTensor ( IVec  len) [inline]

Definition at line 196 of file Tensor.h.

References i, j, and PLearn::TTensor< T >::resize().

        :offset_(0), stride_(0), width_(len)
    { 
        stride_.resize(len.size());
        for(int i=0;i<ndims();i++)
        {
            stride_[i]=1;
            for(int j=i-1;j>=0;j--)
                stride_[i]*=len[j];
        }
        storage = new Storage<T>(totalElements());
    }

Here is the call graph for this function:

template<class T>
PLearn::TTensor< T >::TTensor ( const IVec len,
const T &  init_value 
) [inline]

Definition at line 209 of file Tensor.h.

References i, j, and PLearn::TTensor< T >::resize().

        :offset_(0), stride_(0), width_(len)
    { 
        stride_.resize(len.size());
        for(int i=0;i<ndims();i++)
        {
            stride_[i]=1;
            for(int j=i-1;j>=0;j--)
                stride_[i]*=len[j];
        }
        storage = new Storage<T>(totalElements());
        fill(init_value);
    }

Here is the call graph for this function:


Member Function Documentation

template<class T>
iterator PLearn::TTensor< T >::begin ( ) [inline]

returns an iterator over elements

Definition at line 382 of file Tensor.h.

    { 
        return iterator(this, IVec((unsigned)ndims(),0));
    }
template<class T>
T* PLearn::TTensor< T >::data ( ) const [inline]

Definition at line 363 of file Tensor.h.

References PLERROR.

    {
#ifdef BOUNDCHECK
        if(!storage)
            PLERROR("IN TTensor::data()\nAttempted to get a pointer to the data of an empty matrix");
#endif
        return storage->data+offset_; 
    }
template<class T>
TTensor<T> PLearn::TTensor< T >::DEPRECATEDsubTensor ( const IVec from,
const IVec len,
bool  throw_useless_dimensions = true 
) [inline]

Definition at line 305 of file Tensor.h.

References PLearn::TinyVector< T, N, TTrait >::empty(), i, PLearn::TTensor< T >::offset_, PLERROR, PLearn::TinyVector< T, N, TTrait >::push_back(), PLearn::TTensor< T >::selectDimensions(), and PLearn::TTensor< T >::width_.

    {
        TTensor<T> subt = *this;
        subt.width_ = len;
        subt.offset_ = linearIndex(from);
        
        IVec idx;
        for(int i=0;i<ndims();i++)
        {
            if(from[i]<0 || from[i]+len[i]>width_[i] || len[i]<0)
                PLERROR("TTensor::subTensor : at index %i : from, len, width = %i %i %i",i,from[i],len[i],width_[i]);
            if(len[i]>1 || !throw_useless_dimensions)
                idx.push_back(i);      
        }
        // if idx is empty, it is because all lengths are 1 since a single element was selected,
        // so we need to create a single dimension.
        if(idx.empty())
            idx.push_back(0);
        subt.selectDimensions(idx);
        return subt;
    }

Here is the call graph for this function:

template<class T>
iterator PLearn::TTensor< T >::end ( ) [inline]

Definition at line 389 of file Tensor.h.

    { 
        return iterator(this,lastElementPos());
    }
template<class T>
void PLearn::TTensor< T >::fill ( const T &  val) [inline]

Definition at line 356 of file Tensor.h.

References i.

    {
        int te=totalElements();
        for(int i=0;i<te;i++)
            (*storage)[i]=val;
    }
template<class T>
subTensorIterator PLearn::TTensor< T >::getSubTensorIterator ( const IVec v) [inline]

returns an iterator over subTensors

Definition at line 395 of file Tensor.h.

    { 
        return subTensorIterator(this, v);
    }
template<class T>
IVec PLearn::TTensor< T >::lastElementPos ( ) const [inline]

Definition at line 372 of file Tensor.h.

References i, and PLearn::TinyVector< T, N, TTrait >::resize().

    {
        IVec v;
        v.resize(ndims());
        for(int i=0;i<ndims();i++)
            v[i]=(width_[i]-1);
        return v;
    }

Here is the call graph for this function:

template<class T>
int PLearn::TTensor< T >::linearIndex ( const IVec pos) const [inline]

Definition at line 254 of file Tensor.h.

References i.

    {
        int idx=0;
        for(int i=0;i<ndims();i++)
            idx+=pos[i]*stride_[i];
        return idx;
    }
template<class T>
int PLearn::TTensor< T >::linearIndex ( const vector< int > &  pos) const [inline]

Definition at line 262 of file Tensor.h.

References i.

    {
        int idx=0;
        for(int i=0;i<ndims();i++)
            idx+=pos[i]*stride_[i];
        return idx;
    }
template<class T>
int PLearn::TTensor< T >::ndims ( ) const [inline]

Definition at line 238 of file Tensor.h.

{ return width_.size();}
template<class T>
T& PLearn::TTensor< T >::operator() ( const IVec pos) const [inline]

Definition at line 353 of file Tensor.h.

{return (*storage)[linearIndex(pos)];}
template<class T>
T& PLearn::TTensor< T >::operator() ( const vector< int > &  pos) const [inline]

Definition at line 354 of file Tensor.h.

{return (*storage)[linearIndex(pos)];}
template<class T>
TTensor<T> PLearn::TTensor< T >::operator[] ( int  i) [inline]

Definition at line 327 of file Tensor.h.

References PLearn::TinyVector< T, N, TTrait >::push_back().

    {
        IVec from;
        IVec len;
        from.push_back(i);
        len.push_back(0);
        for(int k=1; k<ndims(); k++)
        {
            from.push_back(0);
            len.push_back(size(k));
        }
        return subTensor(from,len);
    }

Here is the call graph for this function:

template<class T>
int PLearn::TTensor< T >::resize ( const IVec len) [inline]

don't call this from a subTensor !!

Definition at line 224 of file Tensor.h.

References i, j, PLearn::TinyVector< T, N, TTrait >::resize(), and PLearn::TinyVector< T, N, TTrait >::size().

Referenced by PLearn::TTensor< T >::TTensor().

    {
        width_=len;
        stride_.resize(len.size());
        for(int i=0;i<ndims();i++)
        {
            stride_[i]=1;
            for(int j=i-1;j>=0;j--)
                stride_[i]*=len[j];
        }
        storage = new Storage<T>(totalElements());
        offset_=0;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T>
void PLearn::TTensor< T >::selectDimensions ( const IVec dim) [inline]

Definition at line 341 of file Tensor.h.

References i, PLearn::TinyVector< T, N, TTrait >::push_back(), and PLearn::TinyVector< T, N, TTrait >::size().

Referenced by PLearn::TTensor< T >::DEPRECATEDsubTensor(), and PLearn::TTensor< T >::subTensor().

    {
        IVec newwidth,newstride;
        for(int i=0;i<(signed)dim.size();i++)
        {
            newwidth.push_back(width_[dim[i]]);
            newstride.push_back(stride_[dim[i]]);
        }
        stride_=newstride;
        width_=newwidth;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T>
int PLearn::TTensor< T >::size ( int  k) const [inline]

Definition at line 242 of file Tensor.h.

{ return width_[k]; }
template<class T>
IVec PLearn::TTensor< T >::sizes ( ) const [inline]

Definition at line 241 of file Tensor.h.

{return width_;}
template<class T>
TTensor<T> PLearn::TTensor< T >::subTensor ( const IVec from,
const IVec len 
) [inline]

Definition at line 274 of file Tensor.h.

References PLearn::TinyVector< T, N, TTrait >::empty(), i, PLearn::TTensor< T >::offset_, PLERROR, PLearn::TinyVector< T, N, TTrait >::push_back(), PLearn::TTensor< T >::selectDimensions(), and PLearn::TTensor< T >::width_.

    {
        TTensor<T> subt = *this;
        subt.width_ = len;
        subt.offset_ = linearIndex(from);
        
        IVec idx;
        for(int i=0;i<ndims();i++)
        {
            if(from[i]<0 || from[i]+len[i]>width_[i] || len[i]<0)
                PLERROR("TTensor::subTensor : at index %i : from, len, width = %i %i %i",i,from[i],len[i],width_[i]);
            if(len[i]>0) // skip the 0 dimensions
                idx.push_back(i);      
        }
        // if idx is empty, it is because all lengths are 0: we have a scalar
        if(idx.empty())
            idx.push_back(0);
        subt.selectDimensions(idx);
        return subt;
    }

Here is the call graph for this function:

template<class T>
int PLearn::TTensor< T >::totalElements ( ) const [inline]

Definition at line 244 of file Tensor.h.

References i.

    {
        if(ndims()==0)
            return 0;
        int te=1;
        for(int i=0;i<ndims();i++)
            te*=width_[i];
        return te;
    }
template<class T>
IVec PLearn::TTensor< T >::width ( ) const [inline]

Definition at line 240 of file Tensor.h.

{return width_;}

Friends And Related Function Documentation

template<class T>
friend class TTensorElementIterator< T > [friend]

Definition at line 180 of file Tensor.h.

template<class T>
friend class TTensorSubTensorIterator< T > [friend]

Definition at line 181 of file Tensor.h.


Member Data Documentation

template<class T>
int PLearn::TTensor< T >::offset_ [protected]

the displacement to do with respect to storage->data

Definition at line 183 of file Tensor.h.

Referenced by PLearn::TTensor< T >::DEPRECATEDsubTensor(), and PLearn::TTensor< T >::subTensor().

template<class T>
PP< Storage<T> > PLearn::TTensor< T >::storage [protected]

where the data is really kept

Definition at line 186 of file Tensor.h.

template<class T>
IVec PLearn::TTensor< T >::stride_ [protected]

the linear memory displacement, for each dimension, to the next element in the same dimension

Definition at line 184 of file Tensor.h.

template<class T>
IVec PLearn::TTensor< T >::width_ [protected]

the actual widths of each dimensions of the tensor

Definition at line 185 of file Tensor.h.

Referenced by PLearn::TTensor< T >::DEPRECATEDsubTensor(), and PLearn::TTensor< T >::subTensor().


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines