00001 #include <iostream>
00002 #include <cstdarg>
00003
00004 class TensorIndex
00005 {
00006 public:
00007 int dim, id, siblingCount;
00008 const TensorIndex *nextIndex;
00009
00010
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
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
00051 int *dimensions;
00052
00053 };
00054
00056
00057
00058 std::ostream& operator<<( std::ostream &os, const TensorIndex &tensorIndex)
00059 {
00060
00061
00062 os << ((tensorIndex.id<0)?"cov ":"") << tensorIndex.dim;
00063
00064
00065
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
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126