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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// Definitions for class BFMatrix
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <boost/shared_ptr.hpp>
#include "newmat.h"
#include "newmatio.h"
#include "bfmatrix.h"
namespace MISCMATHS {
//
// Member functions for BFMatrix
//
void BFMatrix::print(const NEWMAT::Matrix& m,
const std::string& fname) const
{
if (!fname.length()) {
cout << endl << m << endl;
}
else {
try {
ofstream fout(fname.c_str());
fout << setprecision(10) << m;
}
catch(...) {
std::string errmsg("BFMatrix::print: Failed to write to file " + fname);
throw BFMatrixException(errmsg);
}
}
}
//
// Member functions for SparseBFMatrix
//
//
// Concatenation of two matrices returning a third
//
void SparseBFMatrix::HorConcat(const BFMatrix& B, BFMatrix& AB) const
{
try {
const SparseBFMatrix& lB = dynamic_cast<const SparseBFMatrix&>(B);
SparseBFMatrix& lAB = dynamic_cast<SparseBFMatrix&>(AB);
if (Nrows() != lB.Nrows()) {throw BFMatrixException("SparseBFMatrix::HorConcat: Matrices must have same # of rows");}
*(lAB.mp) = *mp | *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("SparseBFMatrix::HorConcat: dynamic cast error");
}
}
void SparseBFMatrix::HorConcat(const NEWMAT::Matrix& B, BFMatrix& AB) const
{
try {
SparseBFMatrix& lAB = dynamic_cast<SparseBFMatrix&>(AB);
if (int(Nrows()) != B.Nrows()) {throw BFMatrixException("SparseBFMatrix::HorConcat: Matrices must have same # of rows");}
*(lAB.mp) = *mp | B;
}
catch (std::bad_cast) {
throw BFMatrixException("SparseBFMatrix::HorConcat: dynamic cast error");
}
}
void SparseBFMatrix::VertConcat(const BFMatrix& B, BFMatrix& AB) const
{
try {
const SparseBFMatrix& lB = dynamic_cast<const SparseBFMatrix&>(B);
SparseBFMatrix& lAB = dynamic_cast<SparseBFMatrix&>(AB);
if (Ncols() != lB.Ncols()) {throw BFMatrixException("SparseBFMatrix::VertConcat: Matrices must have same # of columns");}
*(lAB.mp) = *mp & *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("SparseBFMatrix::VertConcat: dynamic cast error");
}
}
void SparseBFMatrix::VertConcat(const NEWMAT::Matrix& B, BFMatrix& AB) const
{
try {
SparseBFMatrix& lAB = dynamic_cast<SparseBFMatrix&>(AB);
if (int(Ncols()) != B.Ncols()) {throw BFMatrixException("SparseBFMatrix::VertConcat: Matrices must have same # of columns");}
*(lAB.mp) = *mp & B;
}
catch (std::bad_cast) {
throw BFMatrixException("SparseBFMatrix::VertConcat: dynamic cast error");
}
}
//
// Concatenate another matrix to *this
//
void SparseBFMatrix::HorConcat2MyRight(const BFMatrix& B)
{
try {
const SparseBFMatrix& lB = dynamic_cast<const SparseBFMatrix&>(B);
if (Nrows() != lB.Nrows()) {throw BFMatrixException("SparseBFMatrix::HorConcat2MyRight: Matrices must have same # of rows");}
*mp |= *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("SparseBFMatrix::VertConcat: dynamic cast error");
}
}
void SparseBFMatrix::HorConcat2MyRight(const NEWMAT::Matrix& B)
{
if (int(Nrows()) != B.Nrows()) {throw BFMatrixException("SparseBFMatrix::HorConcat2MyRight: Matrices must have same # of rows");}
*mp |= B;
}
void SparseBFMatrix::VertConcatBelowMe(const BFMatrix& B)
{
try {
const SparseBFMatrix& lB = dynamic_cast<const SparseBFMatrix&>(B);
if (Ncols() != lB.Ncols()) {throw BFMatrixException("SparseBFMatrix::VertConcatBelowMe: Matrices must have same # of columns");}
*mp &= *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("SparseBFMatrix::VertConcat: dynamic cast error");
}
}
void SparseBFMatrix::VertConcatBelowMe(const NEWMAT::Matrix& B)
{
if (int(Ncols()) != B.Ncols()) {throw BFMatrixException("SparseBFMatrix::VertConcatBelowMe: Matrices must have same # of columns");}
*mp &= B;
}
// Multiply by vector
NEWMAT::ReturnMatrix SparseBFMatrix::MulByVec(const NEWMAT::ColumnVector& invec) const
{
if (invec.Nrows() != int(Ncols())) {throw BFMatrixException("Matrix-vector size mismatch");}
NEWMAT::ColumnVector outvec = *mp * invec;
outvec.Release();
return(outvec);
}
// Add another matrix to this one
void SparseBFMatrix::AddToMe(const BFMatrix& M, double s)
{
try {
const SparseBFMatrix& lM = dynamic_cast<const SparseBFMatrix&>(M);
if (Ncols() != lM.Ncols() || Nrows() != lM.Nrows()) {
throw BFMatrixException("SparseBFMatrix::AddToMe: Matrix size mismatch");
}
if (s == 1.0) *mp += *(lM.mp);
else *mp += s * *(lM.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("SparseBFMatrix::AddToMe: dynamic cast error");
}
}
// Given A*x=b, solve for x
NEWMAT::ReturnMatrix SparseBFMatrix::SolveForx(const NEWMAT::ColumnVector& b,
MISCMATHS::MatrixType type,
double tol,
int miter) const
{
if (b.Nrows() != int(Nrows())) {
throw BFMatrixException("SparseBFMatrix::SolveForx: Matrix-vector size mismatch");
}
NEWMAT::ColumnVector x = mp->SolveForx(b,type,tol,miter);
x.Release();
return(x);
}
//
// Member functions for FullBFMatrix
//
boost::shared_ptr<BFMatrix> FullBFMatrix::Transpose(boost::shared_ptr<BFMatrix>& pA) const
{
boost::shared_ptr<FullBFMatrix> tmp(new FullBFMatrix);
*(tmp->mp) = mp->t();
return(tmp);
}
//
// Concatenate two matrices yielding a third
//
void FullBFMatrix::HorConcat(const BFMatrix& B, BFMatrix& AB) const
{
try {
const FullBFMatrix& lB = dynamic_cast<const FullBFMatrix&>(B);
FullBFMatrix& lAB = dynamic_cast<FullBFMatrix&>(AB);
if (Nrows() != lB.Nrows()) {throw BFMatrixException("FullBFMatrix::HorConcat: Matrices must have same # of rows");}
*(lAB.mp) = *mp | *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("FullBFMatrix::HorConcat: dynamic cast error");
}
}
void FullBFMatrix::HorConcat(const NEWMAT::Matrix& B, BFMatrix& AB) const
{
try {
FullBFMatrix& lAB = dynamic_cast<FullBFMatrix&>(AB);
if (int(Nrows()) != B.Nrows()) {throw BFMatrixException("FullBFMatrix::HorConcat: Matrices must have same # of rows");}
*(lAB.mp) = *mp | B;
}
catch (std::bad_cast) {
throw BFMatrixException("FullBFMatrix::HorConcat: dynamic cast error");
}
}
void FullBFMatrix::VertConcat(const BFMatrix& B, BFMatrix& AB) const
{
try {
const FullBFMatrix& lB = dynamic_cast<const FullBFMatrix&>(B);
FullBFMatrix& lAB = dynamic_cast<FullBFMatrix&>(AB);
if (Ncols() != lB.Ncols()) {throw BFMatrixException("FullBFMatrix::VertConcat: Matrices must have same # of columns");}
*(lAB.mp) = *mp & *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("FullBFMatrix::VertConcat: dynamic cast error");
}
}
void FullBFMatrix::VertConcat(const NEWMAT::Matrix& B, BFMatrix& AB) const
{
try {
FullBFMatrix& lAB = dynamic_cast<FullBFMatrix&>(AB);
if (int(Ncols()) != B.Ncols()) {throw BFMatrixException("FullBFMatrix::VertConcat: Matrices must have same # of columns");}
*(lAB.mp) = *mp & B;
}
catch (std::bad_cast) {
throw BFMatrixException("FullBFMatrix::VertConcat: dynamic cast error");
}
}
//
// Concatenation of another matrix to *this
//
void FullBFMatrix::HorConcat2MyRight(const BFMatrix& B)
{
try {
const FullBFMatrix& lB = dynamic_cast<const FullBFMatrix&>(B);
if (Nrows() != lB.Nrows()) {throw BFMatrixException("FullBFMatrix::HorConcat2MyRight: Matrices must have same # of rows");}
*mp |= *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("FullBFMatrix::HorConcat2MyRight: dynamic cast error");
}
}
void FullBFMatrix::HorConcat2MyRight(const NEWMAT::Matrix& B)
{
if (int(Nrows()) != B.Nrows()) {throw BFMatrixException("FullBFMatrix::HorConcat2MyRight: Matrices must have same # of rows");}
*mp |= B;
}
void FullBFMatrix::VertConcatBelowMe(const BFMatrix& B)
{
try {
const FullBFMatrix& lB = dynamic_cast<const FullBFMatrix&>(B);
if (Ncols() != lB.Ncols()) {throw BFMatrixException("FullBFMatrix::VertConcatBelowMe: Matrices must have same # of columns");}
*mp &= *(lB.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("FullBFMatrix::HorConcatBelowMe: dynamic cast error");
}
}
void FullBFMatrix::VertConcatBelowMe(const NEWMAT::Matrix& B)
{
if (int(Ncols()) != B.Ncols()) {throw BFMatrixException("FullBFMatrix::VertConcatBelowMe: Matrices must have same # of columns");}
*mp &= B;
}
// Multiply this matrix with scalar
void FullBFMatrix::MulMeByScalar(double s)
{
*mp = s*(*mp);
}
// Multiply by vector
NEWMAT::ReturnMatrix FullBFMatrix::MulByVec(const NEWMAT::ColumnVector& invec) const
{
if (invec.Nrows() != int(Ncols())) {throw BFMatrixException("FullBFMatrix::MulByVec: Matrix-vector size mismatch");}
NEWMAT::ColumnVector ret;
ret = (*mp)*invec;
ret.Release();
return(ret);
}
// Add another matrix to this one
void FullBFMatrix::AddToMe(const BFMatrix& m, double s)
{
try {
const FullBFMatrix& lm = dynamic_cast<const FullBFMatrix&>(m);
if (Ncols() != lm.Ncols() || Nrows() != lm.Nrows()) {
throw BFMatrixException("FullBFMatrix::AddToMe: Matrix size mismatch");
}
*mp += s*(*lm.mp);
}
catch (std::bad_cast) {
throw BFMatrixException("FullBFMatrix::AddToMe: dynamic cast error");
}
}
// Given A*x=b, solve for x
NEWMAT::ReturnMatrix FullBFMatrix::SolveForx(const NEWMAT::ColumnVector& b, // Ignoring all parameters except b
MISCMATHS::MatrixType type,
double tol,
int miter) const
{
if (int(Nrows()) != b.Nrows()) {throw BFMatrixException("FullBFMatrix::SolveForx: Matrix-vector size mismatch");}
NEWMAT::ColumnVector ret;
ret = mp->i()*b;
ret.Release();
return(ret);
}
} // End namespace MISCMATHS