VMatrix.h 4.31 KB
Newer Older
1
2
3
4
5
6
7
/*
 * VMatrix.h
 *
 *  Created on: Feb 15, 2012
 *      Author: kleinwrt
 */

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/** \file
 *  VMatrix definition.
 *
 *  \author Claus Kleinwort, DESY, 2011 (Claus.Kleinwort@desy.de)
 *
 *  \copyright
 *  Copyright (c) 2011 - 2016 Deutsches Elektronen-Synchroton,
 *  Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY \n\n
 *  This library is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version. \n\n
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details. \n\n
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program (see the file COPYING.LIB for more
 *  details); if not, write to the Free Software Foundation, Inc.,
 *  675 Mass Ave, Cambridge, MA 02139, USA.
 */

30
31
32
33
#ifndef VMATRIX_H_
#define VMATRIX_H_

#include<iostream>
34
#include<iomanip>
35
#include<vector>
36
#include<cstring>
37
38
#include<math.h>

39
//! Namespace for the general broken lines package
Claus Kleinwort's avatar
Claus Kleinwort committed
40
namespace gbl {
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// Simple Vector based on std::vector<double>
class VVector {
public:
	VVector(const unsigned int nRows = 0);
	VVector(const VVector &aVector);
	virtual ~VVector();
	void resize(const unsigned int nRows);
	VVector getVec(unsigned int len, unsigned int start = 0) const;
	void putVec(const VVector &aVector, unsigned int start = 0);
	inline double &operator()(unsigned int i);
	inline double operator()(unsigned int i) const;
	unsigned int getNumRows() const;
	void print() const;
	VVector operator-(const VVector &aVector) const;
56
	VVector &operator=(const VVector &aVector);
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
private:
	unsigned int numRows; ///< Number of rows
	std::vector<double> theVec; ///< Data
};

/// Simple Matrix based on std::vector<double>
class VMatrix {
public:
	VMatrix(const unsigned int nRows = 0, const unsigned int nCols = 0);
	VMatrix(const VMatrix &aMatrix);
	virtual ~VMatrix();
	void resize(const unsigned int nRows, const unsigned int nCols);
	VMatrix transpose() const;
	inline double &operator()(unsigned int i, unsigned int j);
	inline double operator()(unsigned int i, unsigned int j) const;
	unsigned int getNumRows() const;
	unsigned int getNumCols() const;
	void print() const;
	VVector operator*(const VVector &aVector) const;
	VMatrix operator*(const VMatrix &aMatrix) const;
	VMatrix operator+(const VMatrix &aMatrix) const;
78
	VMatrix &operator=(const VMatrix &aMatrix);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
private:
	unsigned int numRows; ///< Number of rows
	unsigned int numCols; ///< Number of columns
	std::vector<double> theVec; ///< Data
};

/// Simple symmetric Matrix based on std::vector<double>
class VSymMatrix {
public:
	VSymMatrix(const unsigned int nRows = 0);
	virtual ~VSymMatrix();
	void resize(const unsigned int nRows);
	unsigned int invert();
	inline double &operator()(unsigned int i, unsigned int j);
	inline double operator()(unsigned int i, unsigned int j) const;
	unsigned int getNumRows() const;
	void print() const;
	VSymMatrix operator-(const VMatrix &aMatrix) const;
	VVector operator*(const VVector &aVector) const;
	VMatrix operator*(const VMatrix &aMatrix) const;
private:
	unsigned int numRows; ///< Number of rows
	std::vector<double> theVec; ///< Data (symmetric storage)
};

/// access element (i,j)
inline double &VMatrix::operator()(unsigned int iRow, unsigned int iCol) {
	return theVec[numCols * iRow + iCol];
}

/// access element (i,j)
inline double VMatrix::operator()(unsigned int iRow, unsigned int iCol) const {
	return theVec[numCols * iRow + iCol];
}

/// access element (i)
inline double &VVector::operator()(unsigned int iRow) {
	return theVec[iRow];
}

/// access element (i)
inline double VVector::operator()(unsigned int iRow) const {
	return theVec[iRow];
}

/// access element (i,j) assuming i>=j
inline double &VSymMatrix::operator()(unsigned int iRow, unsigned int iCol) {
	return theVec[(iRow * iRow + iRow) / 2 + iCol]; // assuming iCol <= iRow
}

/// access element (i,j) assuming i>=j
inline double VSymMatrix::operator()(unsigned int iRow,
		unsigned int iCol) const {
	return theVec[(iRow * iRow + iRow) / 2 + iCol]; // assuming iCol <= iRow
}
134
}
135
#endif /* VMATRIX_H_ */