examples/tensor/tensor.cpp

00001 #include <iostream>
00002 #include <cstdarg>
00003 
00004 class TensorIndex
00005         {
00006         public:
00007                 int dim, id, siblingCount;
00008                 const TensorIndex *nextIndex;
00009 
00010                 // Constructors
00011                 TensorIndex(): dim(0), id(0), siblingCount(0), nextIndex(NULL) { }
00012 
00013                 TensorIndex(const TensorIndex &tensorIndex, bool variate = false):
00014                         dim(tensorIndex.dim), id((variate?-1:1)*tensorIndex.id),
00015                         siblingCount(tensorIndex.siblingCount), nextIndex(tensorIndex.nextIndex) { }
00016                 TensorIndex(const int dimension): dim(dimension), id(nextIndexId++), siblingCount(0), nextIndex(NULL) { }
00017 
00018                 // Functions
00019                 TensorIndex cov()       const { return TensorIndex(*this, true);        }
00020                 TensorIndex covariant() const { return TensorIndex(*this, true);        }
00021 
00022                 const TensorIndex operator*(const TensorIndex &index) const
00023                         { return TensorIndex(index.dim, index.id, 1 + siblingCount, this); }
00024 
00025         private:
00026                 TensorIndex(const int dimension, const int ident, const int sibCount, const TensorIndex * const nextId ):
00027                         dim(dimension), id(ident), siblingCount(sibCount), nextIndex(nextId) { }
00028                 static int nextIndexId;
00029         };
00030 
00031 int TensorIndex::nextIndexId = 1;
00032 
00033 class Tensor
00034         {
00035         public:
00036                 Tensor(const Tensor &tensor)    {}
00037                 Tensor(const TensorIndex &index): numDims(index.siblingCount+1), indexId(new int[numDims]), dimensions(new int[numDims])
00038                         {
00039                         int i;
00040                         TensorIndex const (*indexPtr);
00041                         for (i = numDims -1, indexPtr = &index; i >=0; i--, indexPtr = indexPtr->nextIndex)
00042                                 {
00043                                 indexId[i] = indexPtr->id;
00044                                 dimensions[i] = indexPtr->dim;
00045                                 }
00046                         }
00047 
00048                 int numDims, *indexId;
00049 
00050                 // This can go to a sharedData !!¡¡
00051                 int *dimensions;
00052                 //double *data;
00053         };
00054 
00056 
00057 //#define       ABS(X)  ((X>0)?(X):(-X))
00058 std::ostream& operator<<( std::ostream &os, const TensorIndex &tensorIndex)
00059         {
00060         //if (tensorIndex.nextIndex != NULL)
00061         //      os << *(tensorIndex.nextIndex) << ", ";
00062         os << ((tensorIndex.id<0)?"cov ":"") << tensorIndex.dim;
00063         /*os << ((tensor.indexes[0].id<0)?"cov ":"") << tensor.indexes[0].dim;
00064         for (int i=1; i<tensor.numDims; i++)
00065                 os << ", " << ((tensor.indexes[i].id<0)?"cov ":"") << tensor.indexes[i].dim;*/
00066         return os;
00067         }
00068 
00069 std::ostream& operator<<( std::ostream &os, const Tensor &tensor)
00070         {
00071         os << "Tensor (";
00072         os << tensor.dimensions[0];
00073         for (int i=1; i<tensor.numDims; i++)
00074                 os << ", " << tensor.dimensions[i];
00075         os << ")";
00076         return os;
00077         }
00078 
00079 main ()
00080         {
00081 
00082         TensorIndex i = 7, j = 13, k = 23, l = j.cov();
00083 
00084         Tensor  A(i * j * k * j.cov());
00085 
00086         std::cout << "A = " << A << std::endl;
00087         }
00088 
00090 
00091 /*std::ostream& operator<<( std::ostream &os, const TensorIndex &tensorIndex)
00092         {
00093         //if (tensorIndex.nextIndex != NULL)
00094         //      os << *(tensorIndex.nextIndex) << ", ";
00095         os << ((tensorIndex.id<0)?"cov ":"") << tensorIndex.dim;
00096         }*/
00097 
00098 /*Tensor(const int nDims, TensorIndex *index1, ...): numDims(nDims), indexes(new TensorIndex[numDims])
00099         {
00100         va_list marker;
00101         va_start(marker, index1);
00102 
00103         TensorIndex *idx = index1;
00104         for (int i = 0; i < nDims; i++)
00105                 {
00106                 indexes[i] = *idx;
00107                 idx = va_arg(marker, TensorIndex *);
00108                 }
00109         va_end(marker);
00110         }
00111 
00112 Tensor(const int nDims, const int dim1, ...): numDims(nDims), indexes(new TensorIndex[numDims])
00113         {
00114         va_list marker;
00115         va_start(marker, dim1);
00116 
00117         int actualdim = dim1;
00118         for (int i = 0; i < nDims; i++)
00119                 {
00120                 indexes[i].dim = ABS(actualdim);
00121                 indexes[i].id = (actualdim>0)?1:-1;
00122                 actualdim = va_arg(marker, int);
00123                 }
00124         va_end(marker);
00125         }*/
00126 

Generated on Fri Feb 22 18:26:55 2008 for QVision by  doxygen 1.5.3