PARP Research Group | Universidad de Murcia |
src/qvmath/qvtensor.hGo to the documentation of this file.00001 /* 00002 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012. 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 00024 00025 #ifndef QTENSOR_H 00026 #define QTENSOR_H 00027 00028 #include <iostream> 00029 00030 #include <qvmath/qvblasdatabuffer.h> 00031 #include <qvmath/qvtensorindexator.h> 00032 00234 class QVTensor 00235 { 00236 public: 00237 // Constructors 00238 00242 QVTensor(const QVTensor &tensor): dataSize(tensor.dataSize), dims(tensor.dims), indexIds(tensor.indexIds), data(tensor.data) { } 00243 00250 QVTensor(const QVTensorValence &indexList = QVTensorValence()): 00251 dataSize(1), dims(indexList.size()), indexIds(indexList.size()) 00252 { 00253 for (int n = 0; n < indexList.size(); n++) 00254 { 00255 indexIds[n] = indexList.at(n).id; 00256 dataSize *= (dims[n] = indexList.at(n).dim); 00257 } 00258 00259 // In the case of an empty valence, the tensor will be a scalar 00260 data = new QBlasDataBuffer(dataSize); 00261 } 00262 00263 // Operators 00264 00269 bool operator==(const QVTensor &tensor) const { return equals(tensor); }; 00270 00275 bool operator!=(const QVTensor &tensor) const { return !equals(tensor); }; 00276 00281 QVTensor operator*(const QVTensor &tensor) const { return innerProduct(tensor); }; 00282 00287 QVTensor operator+(const QVTensor &tensor) const { return add(tensor); }; 00288 00293 QVTensor operator-(const QVTensor &tensor) const { return substract(tensor); }; 00294 00298 QVTensor operator^(const QVTensor &tensor) const { return outerProduct(tensor); }; 00299 00304 QVTensor operator()(const QVTensorValence &indexList) const { return renameIndexes(indexList); }; 00305 00306 // Operation methods 00307 00312 int getDataSize() const { return dataSize; } 00313 00317 const double *getReadData() const { return data->getReadData(); } 00318 00322 double *getWriteData() { return data->getWriteData(); } 00323 00328 QVTensorValence getValence() const; 00329 00337 QVTensor slice(const QVTensorIndexValues &indexRangeList) const; 00338 00345 QVTensor transpose(const QVTensorValence &indexList) const; 00346 00352 QVTensor transpose(const QVTensorIndex &i, const QVTensorIndex &j) const; 00353 00358 QVTensor add(const QVTensor &tensor) const; 00359 00364 QVTensor substract(const QVTensor &tensor) const; 00365 00372 QVTensor tensorProduct(const QVTensor &tensor) const; 00373 00379 QVTensor innerProduct(const QVTensor &tensor) const; 00380 00386 QVTensor outerProduct(const QVTensor &tensor) const; 00387 00393 bool equals(const QVTensor &tensor) const; 00394 00401 QVTensor renameIndexes(const QVTensorValence &indexList) const; 00402 00409 QVTensorIndexator getIndexator() { return QVTensorIndexator(dims); }; 00410 00414 double norm2() const; 00415 00420 static QVTensor leviCivita(const int dimension); 00421 00422 #ifndef DOXYGEN_IGNORE_THIS 00423 // These methods won't be documented, but will be available for testing purposes 00424 QVTensor contract() const; 00425 #endif 00426 00427 private: 00428 friend std::ostream& operator << ( std::ostream &os, const QVTensor &tensor ); 00429 00430 // These should be private 00431 QVTensor transpose(const QVector<int> &sorting) const; 00432 QVTensor transpose(const int index1Position, const int index2Position) const; 00433 QVTensor antisymmetrization(const QVTensor &tensor) const; 00434 00435 // Tensor data 00436 int dataSize; 00437 QVector <int> dims; 00438 QVector <int> indexIds; 00439 QSharedDataPointer< QBlasDataBuffer > data; 00440 }; 00441 00442 std::ostream& operator << ( std::ostream &os, const QVTensor &tensor ); 00443 00444 //Q_DECLARE_METATYPE(QVTensor); 00445 00446 #endif |