examples/tensor/tensor.cpp

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00037 #include <iostream>
00038 
00040 
00041 #include <qtensor/qtensor.h>
00042 
00044 
00045 int main ()
00046         {
00047         //for (int t = 0; t < 50000; t++)
00048                 // Several operations
00049                 {
00050                 //QTensorIndex i = 5, j = 20, k = 800, l = 30, m = 60;
00051                 QTensorIndex i = 3, j = 2, k = 8, l = 3, m = 6;
00052                 QTensor T( i * j.cov() * k );
00053 
00054                 double *data = T.getWriteData();
00055                 for (int n=0; n<T.getDataSize(); n++)
00056                         data[n] = n;
00057 
00058                 QTensor A = T;
00059                 QTensor B = A.slice( i[1] & k.range(3,5) & k.range(0,2) & i[0] );
00060                 QTensor C = B.transpose(k * j.cov() * i);
00061                 QTensorIndex i1 = 2, j1 = 3, k1 = 7;
00062                 QTensor D = C(i1 * j1.cov() * k1).slice(i1[1]);
00063                 QTensor E = D.transpose(k1, j1.cov());
00064 
00065                 //std::cout << "B = A.slice( i[1] & k.range(3,5) & k.range(0,2) & i[0] ) = " << B << std::endl;
00066                 //std::cout << "C = B.transpose(k * j.cov() * i) = " << C << std::endl;
00067                 //std::cout << "D = C(i1 * j1.cov() * k1).slice(i1[1]) = " << D << std::endl;
00068                 std::cout << "E = D.transpose(k1, j1.cov()) = " << E << std::endl;
00069                 }
00070 
00071                 // Contract     
00072                 {
00073                 QTensorIndex i1 = 2, j1 = 3, i2 = 2, j2 = 3;
00074                 QTensor T( i1 * j1.cov() * i2 * j2);
00075         
00076                 double *data = T.getWriteData();
00077                 for (int n=0; n<T.getDataSize(); n++)
00078                         data[n] = n;
00079         
00080                 QTensor A = T(i1 * j1 * i1.cov() * j2);
00081                 QTensor B = T(i1 * j1 * i2 * j1.cov());
00082                 QTensor C = T(i1 * j1 * i1.cov() * j1.cov());
00083 
00084                 std::cout << "T = " << T << std::endl;  
00085                 std::cout << "A = T(i * j * i.cov() * l) = " << A << std::endl;
00086                 std::cout << "B = T(i * j * k * j.cov()) = " << B << std::endl;
00087                 std::cout << "C = T(i * j * i.cov() * j.cov()) = " << C << std::endl;
00088                 }
00089 
00090                 // Outer product
00091                 {
00092                 QTensorIndex i = 30, j = 16, k = 30;
00093                 QTensor T1(j*i);
00094                 QTensor T2(j.cov()*k);
00095         
00096                 double *data1 = T1.getWriteData();
00097                 for (int n=0; n<T1.getDataSize(); n++)
00098                         data1[n] = n;
00099 
00100                 double *data2 = T2.getWriteData();
00101                 for (int n=0; n<T2.getDataSize(); n++)
00102                         data2[n] = n;
00103 
00104                 //std::cout << "T1 = " << T1 << std::endl;
00105                 //std::cout << "T2 = " << T2 << std::endl;
00106 
00107                 //for (int i=0; i < 1000; i++)
00108                         {
00109                         //QTensor Y = T1.outerProduct(T2).contract();
00110                         QTensor Y2 = T1 * T2;
00111                         //std::cout << "X =  = " << X << std::endl;
00112                         //std::cout << "Y = T1.outerProduct(T2).X.contract() = " << Y << std::endl;
00113                         //std::cout << "Y2 = T1(i * j) * T2(i.cov() * k) = " << Y2 << std::endl;
00114                         }
00115                 }
00116 
00117                 }

Generated on Thu Mar 13 19:18:16 2008 for QVision by  doxygen 1.5.3