-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumVec.cpp
431 lines (352 loc) · 7.66 KB
/
NumVec.cpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
www.mhc-pathway.net/smm
Original file by Bjoern Peters.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "stdafx.h"
#include "NumVec.h"
// #include <gsl/gsl_min.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_blas.h>
#include "BPException.h"
CNumVec::CNumVec(void) : m_vec(NULL)
{
}
CNumVec::~CNumVec(void)
{
if(m_vec!=NULL)
gsl_vector_free(m_vec);
}
CNumVec::CNumVec(const CNumVec &v)
{
if(v.m_vec!=NULL)
{
m_vec=gsl_vector_alloc(v.size());
gsl_vector_memcpy(m_vec,v.m_vec);
}
else
m_vec=NULL;
}
unsigned CNumVec::size() const
{
if(m_vec==NULL)
return(0);
return(m_vec->size);
}
CNumVec::CNumVec(unsigned n)
{
if(n==0)
m_vec=NULL;
else
m_vec=gsl_vector_alloc(n);
}
void CNumVec::resize(unsigned rows)
{
if(m_vec!=NULL)
{
if(size()==rows)
return;
gsl_vector_free(m_vec);
}
if(rows==0)
m_vec=NULL;
else
m_vec=gsl_vector_alloc(rows);
}
void CNumVec::clear()
{
resize(0);
}
void CNumVec::SetTo(const DoubleVec &v)
{
resize(v.size());
for(unsigned i=0; i<v.size(); i++)
gsl_vector_set(m_vec,i,v[i]);
}
double &CNumVec::operator[](unsigned row)
{
assert(m_vec!=NULL);
assert(row<size());
return(*gsl_vector_ptr(m_vec,row));
}
double CNumVec::operator[](unsigned row) const
{
assert(m_vec!=NULL);
assert(row<size());
return(*gsl_vector_ptr(m_vec,row));
}
void CNumVec::operator= (double x)
{
assert(m_vec!=NULL);
gsl_vector_set_all(m_vec,x);
}
void CNumVec::operator= (const CNumVec &v)
{
if(v.m_vec==NULL)
m_vec=NULL;
else
{
resize(v.size());
gsl_vector_memcpy(m_vec,v.m_vec);
}
}
void CNumVec::operator+=(const CNumVec &v)
{
assert(v.size()==size());
gsl_vector_add(m_vec,v.m_vec);
}
void CNumVec::operator-=(const CNumVec &v)
{
assert(v.size()==size());
gsl_vector_sub(m_vec,v.m_vec);
}
void CNumVec::operator*=(double x)
{
assert(m_vec!=NULL);
gsl_vector_scale(m_vec,x);
}
void CNumVec::operator+=(double x)
{
assert(m_vec!=NULL);
if(gsl_vector_add_constant(m_vec,x))
throw BPException("gsl_vector_add_constant");
}
double CNumVec::ScalarProduct(const CNumVec &vec) const
{
assert(size()==vec.size());
double result;
if(gsl_blas_ddot(m_vec,vec.m_vec,&result))
throw BPException("gsl_blas_ddot");
return(result);
}
void CNumVec::SetToProduct(const CNumMat &a, const CNumVec &b, bool transpose_a)
{
unsigned R,N;
if(!transpose_a)
{
R =a.NumRows();
N=a.NumCols();
}
else
{
R =a.NumCols();
N=a.NumRows();
}
assert(N==b.size());
assert(b.m_vec!=m_vec);
resize(R);
CBLAS_TRANSPOSE_t ta;
if(transpose_a)
ta=CblasTrans;
else
ta=CblasNoTrans;
gsl_blas_dgemv(ta,1.0,a.m_mat,b.m_vec,0.0,m_vec);
}
CNumMat::CNumMat(const CNumMat &mat)
{
if(mat.m_mat!=NULL)
{
m_mat=gsl_matrix_alloc(mat.NumRows(),mat.NumCols());
gsl_matrix_memcpy(m_mat,mat.m_mat);
}
else
m_mat=NULL;
}
void CNumMat::operator= (const CNumMat &m)
{
if(m.m_mat==NULL)
m_mat=NULL;
else
{
resize(m.NumRows(),m.NumCols());
gsl_matrix_memcpy(m_mat,m.m_mat);
}
}
CNumMat::CNumMat(unsigned n, unsigned m)
{
assert(n>0 && m>0);
m_mat=gsl_matrix_alloc(n,m);
}
CNumMat::CNumMat(void) : m_mat(NULL)
{
}
CNumMat::~CNumMat(void)
{
if(m_mat!=NULL)
gsl_matrix_free(m_mat);
}
void CNumMat::resize(unsigned rows, unsigned cols)
{
assert(rows>0 && cols>0);
if(m_mat!=NULL)
{
if(NumRows()==rows && NumCols()==cols)
return;
gsl_matrix_free(m_mat);
}
m_mat=gsl_matrix_alloc(rows,cols);
}
void CNumMat::clear()
{
if(m_mat!=NULL)
gsl_matrix_free(m_mat);
}
double &CNumMat::operator()(unsigned row, unsigned col)
{
assert(m_mat!=NULL);
assert(row<NumRows());
assert(col<NumCols());
return(*gsl_matrix_ptr(m_mat,row, col));
}
const double &CNumMat::operator()(unsigned row, unsigned col) const
{
assert(m_mat!=NULL);
assert(row<NumRows());
assert(col<NumCols());
return(*gsl_matrix_ptr(m_mat,row, col));
}
unsigned CNumMat::NumRows() const
{
if(m_mat==NULL)
return(0);
return(m_mat->size1);
}
unsigned CNumMat::NumCols() const
{
if(m_mat==NULL)
return(0);
return(m_mat->size2);
}
void CNumMat::operator= (double x)
{
assert(m_mat!=NULL);
gsl_matrix_set_all(m_mat,x);
}
void CNumMat::operator*=(double x)
{
assert(m_mat!=NULL);
gsl_matrix_scale(m_mat,x);
}
void CNumMat::operator+=(double x)
{
assert(m_mat!=NULL);
if(gsl_matrix_add_constant(m_mat,x))
throw BPException("gsl_matrix_add_constant");
}
void CNumMat::operator+=(const CNumMat &m)
{
assert(NumCols()==m.NumCols());
assert(NumRows()==m.NumRows());
if(gsl_matrix_add(m_mat,m.m_mat))
throw BPException("gsl_matrix_add");
}
void CNumMat::operator-=(const CNumMat &m)
{
assert(NumCols()==m.NumCols());
assert(NumRows()==m.NumRows());
if(gsl_matrix_sub(m_mat,m.m_mat))
throw BPException("gsl_matrix_sub");
}
void CNumMat::SetToInverse(const CNumMat & mat)
{
assert(mat.NumRows()==mat.NumCols());
const unsigned N=mat.NumRows();
CNumMat LU(mat);
gsl_permutation *p=gsl_permutation_alloc(N);
int signum;
if(gsl_linalg_LU_decomp(LU.m_mat, p, &signum))
throw BPException("gsl_linalg_LU_decomp");
resize(N,N);
if(gsl_linalg_LU_invert(LU.m_mat,p,m_mat))
throw BPException("gsl_linalg_LU_invert");
gsl_permutation_free(p);
}
void CNumMat::SetToProduct(const CNumMat &a, const CNumMat &b, bool transpose_a, bool transpose_b)
{
unsigned R,N1,N2,C;
if(!transpose_a)
{
R =a.NumRows();
N1=a.NumCols();
}
else
{
R =a.NumCols();
N1=a.NumRows();
}
if(!transpose_b)
{
N2=b.NumRows();
C =b.NumCols();
}
else
{
N2=b.NumCols();
C =b.NumRows();
}
assert(N1==N2);
assert(a.m_mat!=m_mat && b.m_mat!=m_mat);
resize(R,C);
CBLAS_TRANSPOSE_t ta, tb;
if(transpose_a)
ta=CblasTrans;
else
ta=CblasNoTrans;
if(transpose_b)
tb=CblasTrans;
else
tb=CblasNoTrans;
gsl_blas_dgemm(ta,tb,1.0,a.m_mat,b.m_mat,0.0,m_mat);
}
ostream& operator<< ( ostream& os, const CNumVec & t )
{
os << unsigned(t.size()) << "\t";
for(unsigned i=0; i<t.size(); i++)
os << t[i] << "\t";
if(os.fail())
throw BPException("Unable to save CNumVec");
return os;
}
istream& operator>> ( istream& is, CNumVec & t )
{
unsigned n;
is >> n;
if(!is.fail())
{
t.resize(n);
for(unsigned i=0; i<n; i++)
is >> t[i];
if(is.fail())
throw BPException("Incomplete Vec");
}
return is;
}
ostream& operator<< ( ostream& os, const CNumMat & t )
{
os << t.NumRows() << "\t" << t.NumCols() << "\n";
for(unsigned r=0; r<t.NumRows(); r++)
{
for(unsigned c=0; c<t.NumCols(); c++)
os << t(r,c) << "\t";
os << "\n";
}
if(os.fail())
BPException("Could not save Mat");
return os;
};