/
BioFVM_vector.cpp
368 lines (309 loc) · 9.6 KB
/
BioFVM_vector.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
/*
#############################################################################
# If you use BioFVM in your project, please cite BioFVM and the version #
# number, such as below: #
# #
# We solved the diffusion equations using BioFVM (Version 1.0.4) [1] #
# #
# [1] A. Ghaffarizaeh, S.H. Friedman, and P. Macklin, BioFVM: an efficient #
# parallelized diffusive transport solver for 3-D biological simulations,#
# Bioinformatics, 2015. DOI: 10.1093/bioinformatics/btv730 #
#############################################################################
# #
# Copyright 2015 Paul Macklin and the BioFVM Project #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#############################################################################
*/
#include "BioFVM_vector.h"
/* some global BioFVM strings */
namespace BioFVM{
/* faster operator overloading. multiplication and division are element-wise (Hadamard) */
std::vector<double> operator-( const std::vector<double>& v1 , const std::vector<double>& v2 )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] -= v2[i]; }
return v;
}
std::vector<double> operator+( const std::vector<double>& v1 , const std::vector<double>& v2 )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] += v2[i]; }
return v;
}
std::vector<double> operator*( const std::vector<double>& v1 , const std::vector<double>& v2 )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] *= v2[i]; }
return v;
}
std::vector<double> operator/( const std::vector<double>& v1 , const std::vector<double>& v2 )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] /= v2[i]; }
return v;
}
std::vector<double> operator*( double d , const std::vector<double>& v1 )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] *= d; }
return v;
}
std::vector<double> operator+( double d , const std::vector<double>& v1 )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] += d; }
return v;
}
std::vector<double> operator+( const std::vector<double>& v1 , double d )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] += d; }
return v;
}
std::vector<double> operator-( double d , const std::vector<double>& v1 )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] = d - v1[i]; }
return v;
}
std::vector<double> operator-( const std::vector<double>& v1 , double d )
{
std::vector<double> v = v1;
for( int i=0; i < v1.size() ; i++ )
{ v[i] -= d; }
return v;
}
void operator+=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( int i=0; i < v1.size() ; i++ )
{ v1[i] += v2[i]; }
return;
}
void operator-=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( int i=0; i < v1.size() ; i++ )
{ v1[i] -= v2[i]; }
return;
}
void operator/=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( int i=0; i < v1.size() ; i++ )
{ v1[i] /= v2[i]; }
return;
}
void operator*=( std::vector<double>& v1, const double& a )
{
for( int i=0; i < v1.size() ; i++ )
{ v1[i] *= a; }
return;
}
void operator*=( std::vector<double>& v1, const std::vector<double>& v2 )
{
for( int i=0; i < v1.size() ; i++ )
{ v1[i] *= v2[i]; }
return;
}
void operator/=( std::vector<double>& v1, const double& a )
{
for( int i=0; i < v1.size() ; i++ )
{ v1[i] /= a; }
return;
}
/* other commonly needed operations on vectors */
std::ostream& operator<<(std::ostream& os, const std::vector<double>& v )
{
if( v.size() == 3 )
{
os << "x=\"" << v[0] << "\" y=\"" << v[1] << "\" z=\"" << v[2] << "\"" ;
return os;
}
for( int i=0; i < v.size(); i++ )
{ os << v[i] << " " ; }
return os;
}
// this one returns a new vector that has been normalized
std::vector<double> normalize( std::vector<double>& v )
{
std::vector<double> output = v ;
double norm = 0.0;
norm = 0.0;
for( int i=0; i < v.size(); i++ )
{ norm += ( v[i]*v[i] ); }
norm = sqrt( norm );
for( int i=0; i < v.size(); i++ )
{ output[i] /= norm ; }
return output;
}
// this one normalizes v
void normalize( std::vector<double>* v )
{
double norm = 1e-32;
for( int i=0; i < v->size(); i++ )
{ norm += ( (*v)[i] * (*v)[i] ); }
norm = sqrt( norm );
for( int i=0; i < v->size(); i++ )
{ (*v)[i] /= norm ; }
return;
}
double norm_squared( const std::vector<double>& v )
{
double out = 0.0;
for( int i=0 ; i < v.size() ; i++ )
{ out += ( v[i] * v[i] ); }
return out;
}
double norm( const std::vector<double>& v )
{
return sqrt( norm_squared( v ) );
}
double maxabs( const std::vector<double>& v )
{
double out = 0.0;
for( int i=0; i < v.size() ; i++ )
{
if( fabs( v[i] ) > out )
{ out = v[i]; }
}
return out;
}
double max_abs_difference( const std::vector<double>& v1 , const std::vector<double>& v2 )
{
double out = 0.0;
for( int i=0; i < v1.size() ; i++ )
{
if( fabs( v1[i] -v2[i] ) > out )
{ out = fabs( v1[i] - v2[i] ); }
}
return out;
}
std::vector<double> exponentiate( const std::vector<double>& exponent )
{
std::vector<double> out( exponent.size() , 0.0 );
for( int i=0 ; i < out.size() ; i++ )
{ out[i] = exp( exponent[i] ); }
return out;
}
void randomize( std::vector<double>* v )
{
double norm = 1e-32;
static double d1 = 2.0 / (double) RAND_MAX;
for( int i=0; i < v->size(); i++ )
{ (*v)[i] = -1 + d1 * rand(); }
return;
}
/* axpy and related BLAS-type operations */
void axpy( std::vector<double>* y, double& a , std::vector<double>& x )
{
for( int i=0; i < (*y).size() ; i++ )
{
(*y)[i] += a * x[i] ;
}
return ;
}
void axpy( std::vector<double>* y, std::vector<double>& a , std::vector<double>& x )
{
for( int i=0; i < (*y).size() ; i++ )
{
(*y)[i] += a[i] * x[i] ;
}
return;
}
void naxpy( std::vector<double>* y, double& a , std::vector<double>& x )
{
for( int i=0; i < (*y).size() ; i++ )
{
(*y)[i] -= a * x[i] ;
}
return ;
}
void naxpy( std::vector<double>* y, std::vector<double>& a , std::vector<double>& x )
{
for( int i=0; i < (*y).size() ; i++ )
{
(*y)[i] -= a[i] * x[i] ;
}
return;
}
// turn a delimited character array (e.g., csv) into a vector of doubles
void csv_to_vector( const char* buffer , std::vector<double>& vect )
{
vect.resize(0);
int i=0;
while( i < strlen( buffer ) )
{
// churn through delimiters, whitespace, etc. to reach the next numeric term
while( isdigit( buffer[i] ) == false && buffer[i] != '.' && buffer[i] != '-' && buffer[i] != 'e' && buffer[i] != 'E' )
{ i++; }
char* pEnd;
if( i < strlen(buffer) ) // add this extra check in case of a final character, e.g., ']'
{
vect.push_back( strtod( buffer+i , &pEnd ) );
i = pEnd - buffer;
}
}
return;
}
char* vector_to_csv( const std::vector<double>& vect )
{
static int datum_size = 16; // format = %.7e, 1 (sign) + 1 (lead) + 1 (decimal) + 7 (figs) + 2 (e, sign) + 3 (exponent) + 1 (delimiter) = 16
// this is approximately the same at matlab long for single precision.
// If you want better precision, use a binary data format like matlab, or (in the future) HDF
char* buffer;
buffer = new char[ datum_size * vect.size() ];
int position = 0;
for( int j=0; j < vect.size()-1 ; j++ )
{
position += sprintf( buffer+position , "%.7e," , vect[j] );
}
sprintf( buffer + position , "%.7e" , vect[ vect.size()-1 ] );
return buffer;
}
void vector_to_csv_safe( const std::vector<double>& vect , char*& buffer )
{
static int datum_size = 16; // format = %.7e, 1 (sign) + 1 (lead) + 1 (decimal) + 7 (figs) + 2 (e, sign) + 3 (exponent) + 1 (delimiter) = 16
// this is approximately the same at matlab long for single precision.
// If you want better precision, use a binary data format like matlab, or (in the future) HDF
if( buffer )
{ delete [] buffer; }
buffer = new char[ datum_size * vect.size() ];
std::cout << __LINE__ << std::endl;
int position = 0;
for( int j=0; j < vect.size()-1 ; j++ )
{
position += sprintf( buffer+position , "%.7e," , vect[j] );
}
sprintf( buffer + position , "%.7e" , vect[ vect.size()-1 ] );
return;
}
void vector_to_csv( const std::vector<double>& vect , char*& buffer )
{
// %.7e is approximately the same at matlab longe for single precision.
// If you want better precision, use a binary data format like matlab, or (in the future) HDF
int position = 0;
for( int j=0; j < vect.size()-1 ; j++ )
{
position += sprintf( buffer+position , "%.7e," , vect[j] );
}
sprintf( buffer + position , "%.7e" , vect[ vect.size()-1 ] );
return;
}
};