Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Matrix template class source file. -*- C++ -*-
/* Copyright 2008 Mike Williams (mwill@jlab.org)
*
* This file is part of qft++.
*
* qft++ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* qft++ 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with qft++. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _Matrix_TCC
#define _Matrix_TCC
//_____________________________________________________________________________
/** @file Matrix.tcc
* @brief Matrix template class source file.
*/
//_____________________________________________________________________________
template <typename _Tp> template <typename V>
Matrix<_Tp>& Matrix<_Tp>::operator=(const Matrix<V> &__matrix) {
if(!(this->SizeCheck(__matrix))){
cout << "Error! Attempt to use operator= on matricies w/ different sizes"
<< endl;
}
this->_SizeAssert(__matrix);
this->Matrix_Base::operator=(__matrix);
this->_Copy(__matrix);
return *this;
}
//_____________________________________________________________________________
template <typename _Tp> template <typename V>
Matrix<typename AddType<_Tp,V>::Type>
Matrix<_Tp>::operator+(const Matrix<V> &__matrix) const {
if(!this->SizeCheck(__matrix)){
cout << "Error! Attempt to add 2 matricies w/ different sizes." << endl;
}
this->_SizeAssert(__matrix);
Matrix<typename AddType<_Tp,V>::Type> ret(_num_rows,_num_cols);
int size = this->Size();
for(int i = 0; i < size; i++)
ret._data[i] = this->_data[i] + __matrix._data[i];
return ret;
}
//_____________________________________________________________________________
template <typename _Tp> template <typename V>
Matrix<typename SubType<_Tp,V>::Type>
Matrix<_Tp>::operator-(const Matrix<V> &__matrix) const {
if(!this->SizeCheck(__matrix)){
cout << "Error! Attempt to add 2 matricies w/ different sizes." << endl;
}
this->_SizeAssert(__matrix);
Matrix<typename SubType<_Tp,V>::Type> ret(_num_rows,_num_cols);
int size = this->Size();
for(int i = 0; i < size; i++)
ret._data[i] = this->_data[i] - __matrix._data[i];
return ret;
}
//_____________________________________________________________________________
template <typename _Tp> template <typename V>
Matrix<typename MultType<_Tp,V>::Type>
Matrix<_Tp>::operator*(const Matrix<V> &__matrix) const {
if(this->NumCols() != __matrix.NumRows()){
cout << "Error! Attempt to multiply matricies with incompatible sizes."
<< endl;
}
assert(this->NumCols() == __matrix.NumRows());
int matrix_cols = __matrix.NumCols();
Matrix<typename MultType<_Tp,V>::Type> ret(_num_rows,matrix_cols);
for(int i = 0; i < _num_rows; i++){
for(int j = 0; j < matrix_cols; j++){
ret(i,j) = this->Element(i,0) * __matrix(0,j);
for(int k = 1; k < _num_cols; k++)
ret(i,j) += this->Element(i,k) * __matrix(k,j);
}
}
return ret;
}
//_____________________________________________________________________________
template <typename _Tp>
bool Matrix<_Tp>::operator==(const Matrix<_Tp> &__matrix) const {
if(!this->SizeCheck(__matrix)) return false;
for(int i = 0; i < _num_rows; i++){
for(int j = 0; j < _num_rows; j++){
if(this->Element(i,j) != __matrix(i,j)) return false;
}
}
return true;
}
//_____________________________________________________________________________
template <typename _Tp> _Tp Matrix<_Tp>::Trace() const {
if(_num_rows != _num_cols)
cout << "Error! Attempt to get trace of non-square matrix." << endl;
assert(_num_rows == _num_cols);
_Tp ret;
ret = this->Element(0,0);
for(int i = 1; i < _num_rows; i++) ret += this->Element(i,i);
return ret;
}
//_____________________________________________________________________________
template <typename _Tp> Matrix<_Tp> Matrix<_Tp>::Transpose() const {
Matrix<_Tp> ret(_num_cols,_num_rows);
for(int i = 0; i < _num_rows; i++){
for(int j = 0; j < _num_cols; j++){
ret(j,i) = this->Element(i,j);
}
}
return ret;
}
//_____________________________________________________________________________
template <typename _Tp> template <typename V>
Matrix<typename MultType<_Tp,V>::Type>
Matrix<_Tp>::operator%(const Matrix<V> &__matrix) const {
if(this->NumCols() != __matrix.NumRows()){
cout << "Error! Attempt to multiply matricies with incompatible sizes."
<< endl;
}
assert(this->NumCols() == __matrix.NumRows());
int matrix_cols = __matrix.NumCols();
Matrix<typename MultType<_Tp,V>::Type> ret(_num_rows,matrix_cols);
for(int i = 0; i < _num_rows; i++){
for(int j = 0; j < matrix_cols; j++){
ret(i,j) = this->Element(i,0) % __matrix(0,j);
for(int k = 1; k < _num_cols; k++)
ret(i,j) += this->Element(i,k) % __matrix(k,j);
}
}
return ret;
}
//_____________________________________________________________________________
template <typename _Tp> template <typename V>
Matrix<typename MultType<_Tp,V>::Type>
Matrix<_Tp>::operator|(const Matrix<V> &__matrix) const {
if(this->NumCols() != __matrix.NumRows()){
cout << "Error! Attempt to multiply matricies with incompatible sizes."
<< endl;
}
assert(this->NumCols() == __matrix.NumRows());
int matrix_cols = __matrix.NumCols();
Matrix<typename MultType<_Tp,V>::Type> ret(_num_rows,matrix_cols);
for(int i = 0; i < _num_rows; i++){
for(int j = 0; j < matrix_cols; j++){
ret(i,j) = this->Element(i,0) | __matrix(0,j);
for(int k = 1; k < _num_cols; k++)
ret(i,j) += this->Element(i,k) | __matrix(k,j);
}
}
return ret;
}
//_____________________________________________________________________________
#endif /* _Matrix_TCC */