-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMorpheus_Matrix.cpp
More file actions
207 lines (167 loc) · 3.26 KB
/
Morpheus_Matrix.cpp
File metadata and controls
207 lines (167 loc) · 3.26 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @file
* \brief Defines a Matrix class
*
* @author Alicia Klinvex
*/
#include "Morpheus_Matrix.h"
#include <cassert>
#include <cmath>
#include <iostream>
namespace Morpheus {
Matrix::Matrix(const int nrows, const int ncols)
{
nrows_ = nrows;
ncols_ = ncols;
// Make sure the dimensions make sense
assert(nrows_ > 0);
assert(ncols_ > 0);
// Allocate memory for the data
data_ = new double*[nrows_];
for(int r=0; r<nrows_; r++)
data_[r] = new double[ncols_];
}
Matrix::~Matrix()
{
// Free all the memory we allocated
for(int r=0; r<nrows_; r++)
delete[] data_[r];
delete[] data_;
}
double& Matrix::operator()(const int row, const int col)
{
return data_[row][col];
}
void Matrix::multiply(const Vector& X, Vector& Y) const
{
// Make sure the dimensions are consistent
assert(X.getNumElements() == ncols_);
assert(Y.getNumElements() == nrows_);
for(int r=0; r<nrows_; r++)
{
Y[r] = 0;
for(int c=0; c<ncols_; c++)
{
Y[r] = Y[r] + data_[r][c]*X[c];
}
}
}
void Matrix::multiply(const Matrix& X, Matrix& Y) const
{
// Make sure the dimensions are consistent
assert(nrows_ == Y.nrows_);
assert(ncols_ == X.nrows_);
assert(X.ncols_ == Y.ncols_);
for(int r=0; r<Y.nrows_; r++)
{
for(int c=0; c<Y.ncols_; c++)
{
Y(r,c) = 0;
for(int k=0; k<ncols_; k++)
{
Y(r,c) = Y(r,c) + data_[r][k] * X.data_[k][c];
}
}
}
}
bool Matrix::isSymmetric() const
{
if(nrows_ != ncols_)
return false;
for(int r=0; r<nrows_; r++)
{
for(int c=0; c<ncols_; c++)
{
if(data_[r][c] != data_[c][r])
return false;
}
}
return true;
}
bool Matrix::isUpperTriangular() const
{
if(nrows_ != ncols_)
return false;
for(int r=0; r<nrows_; r++)
{
for(int c=r+1; c<ncols_; c++)
{
if(data_[r][c] != 0)
return false;
}
}
return true;
}
// Maximum absolute column sum
double Matrix::norm1() const
{
double curColSum;
double maxColSum = 0;
for(int c=0; c<ncols_; c++)
{
curColSum = 0;
for(int r=0; r<nrows_; r++)
{
curColSum = curColSum + data_[r][c];
}
if(curColSum > maxColSum)
maxColSum = curColSum;
}
return maxColSum;
}
// Maximum absolute row sum
double Matrix::normInf() const
{
double curRowSum;
double maxRowSum = 0;
for(int r=0; r<nrows_; r++)
{
curRowSum = 0;
for(int c=0; c<ncols_; c++)
{
curRowSum = curRowSum + data_[r][c];
}
if(curRowSum > maxRowSum)
maxRowSum = curRowSum;
}
return maxRowSum;
}
int Matrix::getNumRows() const
{
return nrows_;
}
int Matrix::getNumCols() const
{
return ncols_;
}
int Matrix::getNumEntries() const
{
return (nrows_*ncols_);
}
bool Matrix::approxEqual(const Matrix& m, const double tol) const
{
if(nrows_ != m.nrows_ || ncols_ != m.ncols_)
return false;
for(int r=0; r<nrows_; r++)
{
for(int c=0; c<ncols_; c++)
{
if(std::abs(data_[r][c] - m.data_[r][c]) > tol)
return false;
}
}
return true;
}
void Matrix::print() const
{
std::cout << nrows_ << "x" << ncols_ << " Matrix\n";
for(int r=0; r<nrows_; r++)
{
for(int c=0; c<ncols_; c++)
{
std::cout << data_[r][c] << " ";
}
std::cout << std::endl;
}
}
} /* namespace Morpheus */