1 // -*- C++ -*- 2 // TinyMatrix.h --- Implements a rudimentary matrix class 3 // 4 // Copyright (C) 2001-2011 Jakob Schiotz and Center for Individual 5 // Nanoparticle Functionality, Department of Physics, Technical 6 // University of Denmark. Email: schiotz@fysik.dtu.dk 7 // 8 // This file is part of Asap version 3. 9 // Asap is released under the GNU Lesser Public License (LGPL) version 3. 10 // However, the parts of Asap distributed within the OpenKIM project 11 // (including this file) are also released under the Common Development 12 // and Distribution License (CDDL) version 1.0. 13 // 14 // This program is free software: you can redistribute it and/or 15 // modify it under the terms of the GNU Lesser General Public License 16 // version 3 as published by the Free Software Foundation. Permission 17 // to use other versions of the GNU Lesser General Public License may 18 // granted by Jakob Schiotz or the head of department of the 19 // Department of Physics, Technical University of Denmark, as 20 // described in section 14 of the GNU General Public License. 21 // 22 // This program is distributed in the hope that it will be useful, 23 // but WITHOUT ANY WARRANTY; without even the implied warranty of 24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 // GNU General Public License for more details. 26 // 27 // You should have received a copy of the GNU General Public License 28 // and the GNU Lesser Public License along with this program. If not, 29 // see <http://www.gnu.org/licenses/>. 30 31 32 #ifndef _TINYMATRIX_H 33 #define _TINYMATRIX_H 34 35 namespace ASAPSPACE { 36 37 template<class Type> 38 // A TinyMatrix is a 2D array where size is only known at runtime. 39 class TinyMatrix 40 { 41 public: 42 /// Default constructor: Create an empty TinyMatrix TinyMatrix()43 TinyMatrix() {data = 0;} 44 45 /// Construct a 2D array of known size. TinyMatrix(int rows,int columns)46 TinyMatrix(int rows, int columns) {Allocate(rows, columns);} 47 48 /// Copy constructor TinyMatrix(const TinyMatrix<Type> & original)49 TinyMatrix(const TinyMatrix<Type> &original) {CopyFrom(original);} 50 CopyFrom(const TinyMatrix<Type> & original)51 void CopyFrom(const TinyMatrix<Type> &original) 52 { 53 Allocate(original.r, original.c); 54 for (int i = 0; i < r*c; i++) 55 data[i] = original.data[i]; 56 } 57 Allocate(int rows,int columns)58 void Allocate(int rows, int columns) {r = rows; c = columns; data = new Type[r*c];} ~TinyMatrix()59 ~TinyMatrix() {delete[] data;} 60 Type *operator[](int row) {return data + c*row;} 61 const Type *operator[](int row) const {return data + c*row;} 62 63 void operator=(const TinyMatrix<Type> &from) {CopyFrom(from);} 64 65 protected: 66 int r, c; 67 Type *data; 68 }; 69 70 /// A TinyMatrixOfVector is a TinyMatrix where each element is a vector<X> 71 template<class T> 72 class TinyMatrixOfVector : public TinyMatrix<T> 73 { 74 public: TinyMatrixOfVector(int rows,int columns,int size)75 TinyMatrixOfVector(int rows, int columns, int size) { 76 TinyMatrix<T>::Allocate(rows, columns); 77 for (int i = 0; i < rows * columns; i++) 78 TinyMatrix<T>::data[i].resize(size); 79 } 80 }; 81 82 typedef TinyMatrix<double> TinyDoubleMatrix; 83 84 } // end namespace 85 86 #endif // _TINYMATRIX_H 87