VectorOperations.h 19.7 KB
 Praetorius, Simon committed Aug 02, 2012 1 2 3 4 5 6 ``````/** \file VectorOperations.h */ #ifndef VECTOR_OPERATIONS_H #define VECTOR_OPERATIONS_H #include "AMDiS.h" `````` Praetorius, Simon committed Sep 07, 2012 7 ``````#include "ValueTypes.h" `````` Praetorius, Simon committed Aug 02, 2012 8 9 10 11 12 13 14 15 16 17 18 `````` #if HAVE_PARALLEL_DOMAIN_AMDIS #include "parallel/StdMpi.h" #endif #include #include #include #include #include #include `````` Praetorius, Simon committed Sep 07, 2012 19 ``````#include `````` 20 ``````#include `````` Praetorius, Simon committed Aug 02, 2012 21 22 23 24 25 26 27 28 29 30 31 32 33 `````` using namespace AMDiS; template const WorldVector& operator*=(WorldVector& v1, const WorldVector& v2) { T* v1It, *v2It; for (v1It = v1.begin(), v2It = v2.begin(); v1It != v1.end(); v1It++, v2It++) *v1It *= *v2It; return v1; `````` Praetorius, Simon committed Apr 29, 2013 34 ``````} `````` Praetorius, Simon committed Aug 02, 2012 35 36 37 38 39 40 41 42 43 `````` // x ~= y template class compareTol : public binary_function { public: compareTol(double tol_) : tol(tol_) {} bool operator()(const T &x, const T &y) const { return sqrt((x-y)*(x-y)) struct comparePair : public binary_function, std::pair, bool> { bool operator()(const std::pair &x, const std::pair &y) const { return (component==0?(x.first struct compare0 : public binary_function, std::pair, bool> { bool operator()(const std::pair &x, const std::pair &y) const { return x.first struct compare1 : public binary_function, std::pair, bool> { bool operator()(const std::pair &x, const std::pair &y) const { return x.second, double>, std::pair, double>, bool> { public: SortPair(int component_=1,int idx_=0) : component(component_),idx(idx_) {} bool operator()(const std::pair, double> &x, const std::pair, double> &y) const { return (component==1?(x.first)[idx]<(y.first)[idx]:x.second struct num_rows {}; /// size implementation for STL vectors template struct num_rows< std::vector > { typedef std::size_t type; type operator()(const std::vector& v) { return v.size(); } }; /// size implementation for (1D) arrays interpreted as vectors template struct num_rows { typedef std::size_t type; type operator()(const Value[Size]) { return Size; } `````` Praetorius, Simon committed Apr 29, 2013 103 `````` }; `````` Praetorius, Simon committed Sep 07, 2012 104 105 106 107 108 109 110 `````` /// size implementation for (2D and higher) arrays interpreted as matrices template struct num_rows { typedef std::size_t type; type operator()(const Value[Rows][Cols]) { return Rows; } `````` Praetorius, Simon committed Apr 29, 2013 111 `````` }; `````` Praetorius, Simon committed Sep 07, 2012 112 `````` `````` 113 `````` /// size implementation for AMDiS WorldVectors `````` Praetorius, Simon committed Sep 07, 2012 114 115 116 117 118 `````` template struct num_rows< WorldVector > { typedef std::size_t type; type operator()(const WorldVector& v) { return static_cast(v.getSize()); } `````` Praetorius, Simon committed Apr 29, 2013 119 `````` }; `````` Praetorius, Simon committed Sep 07, 2012 120 `````` `````` 121 `````` /// size implementation for AMDiS WorldMatrices `````` Praetorius, Simon committed Sep 07, 2012 122 123 124 125 126 `````` template struct num_rows< WorldMatrix > { typedef std::size_t type; type operator()(const WorldMatrix& v) { return static_cast(v.getNumRows()); } `````` Praetorius, Simon committed Apr 29, 2013 127 `````` }; `````` Praetorius, Simon committed Sep 07, 2012 128 `````` `````` 129 `````` /// size implementation for arithmetic types (double, float, int, ...) `````` Praetorius, Simon committed Sep 07, 2012 130 131 132 133 134 135 136 137 138 139 140 141 142 `````` template struct num_rows< Value, typename boost::enable_if< boost::is_arithmetic >::type > { typedef std::size_t type; type operator()(const Value& v) { return 1; } }; //________________________________________________________________________________ /// General declaration, used to disable unsupported types template struct num_cols {}; `````` 143 `````` /// size implementation for AMDiS WorldMatrices `````` Praetorius, Simon committed Sep 07, 2012 144 145 146 147 148 `````` template struct num_cols< WorldMatrix > { typedef std::size_t type; type operator()(const WorldMatrix& v) { return static_cast(v.getNumCols()); } `````` Praetorius, Simon committed Apr 29, 2013 149 `````` }; `````` Praetorius, Simon committed Sep 07, 2012 150 151 152 153 154 155 156 `````` //________________________________________________________________________________ /// General declaration, used to disable unsupported types template struct resize {}; `````` 157 `````` /// change_dim implementation for AMDiS WorldVectors `````` Praetorius, Simon committed Sep 07, 2012 158 159 160 161 162 163 164 165 `````` template struct resize< WorldVector > { typedef void vector_void_type; void operator()(WorldVector& v, size_t r) { TEST_EXIT(Global::getGeo(WORLD) == r) ("WorldVectors can not be resized!\n"); } `````` Praetorius, Simon committed Apr 29, 2013 166 `````` }; `````` Praetorius, Simon committed Sep 07, 2012 167 `````` `````` 168 `````` /// change_dim implementation for STL vectors `````` Praetorius, Simon committed Sep 07, 2012 169 170 171 172 173 174 175 `````` template struct resize< std::vector > { typedef void vector_void_type; void operator()(std::vector& v, size_t r) { v.resize(r); } `````` Praetorius, Simon committed Apr 29, 2013 176 `````` }; `````` Praetorius, Simon committed Sep 07, 2012 177 `````` `````` 178 `````` /// change_dim implementation for MTL4 vectors `````` Praetorius, Simon committed Sep 07, 2012 179 180 181 182 183 184 185 186 187 188 189 `````` template struct resize< mtl::dense_vector > { typedef void vector_void_type; void operator()(mtl::dense_vector& v, size_t r) { v.change_dim(r); } }; // _________________________________________________________________________________ `````` 190 `````` /// change_dim implementation for AMDiS WorldMatrices `````` Praetorius, Simon committed Sep 07, 2012 191 192 193 194 195 `````` template struct resize< WorldMatrix > { typedef void matrix_void_type; void operator()(WorldMatrix& v, size_t r, size_t c) { `````` Praetorius, Simon committed Mar 05, 2013 196 197 `````` size_t dow = static_cast(Global::getGeo(WORLD)); TEST_EXIT(dow == r && dow == c) `````` Praetorius, Simon committed Sep 07, 2012 198 199 200 201 `````` ("WorldMatrices can not be resized!\n"); } }; `````` 202 `````` /// change_dim implementation for MTL4 matrix `````` Praetorius, Simon committed Sep 07, 2012 203 204 205 206 207 208 209 210 211 `````` template struct resize< mtl::matrix::base_matrix > { typedef void matrix_void_type; void operator()(mtl::matrix::base_matrix& v, size_t r, size_t c) { v.change_dim(r,c); } }; `````` 212 `````` /// change_dim implementation for MTL4 matrix `````` Praetorius, Simon committed Sep 07, 2012 213 214 215 216 217 218 219 220 221 `````` template struct resize< mtl::matrix::dense2D > { typedef void matrix_void_type; void operator()(mtl::matrix::dense2D& v, size_t r, size_t c) { v.change_dim(r,c); } }; `````` 222 `````` /// change_dim implementation for MTL4 matrix `````` Praetorius, Simon committed Sep 07, 2012 223 224 225 226 227 228 229 230 `````` template struct resize< mtl::matrix::compressed2D > { typedef void matrix_void_type; void operator()(mtl::matrix::compressed2D& v, size_t r, size_t c) { v.change_dim(r,c); } }; `````` 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 `````` // _________________________________________________________________________________ /// General declaration, used to disable unsupported types template struct at {}; template struct at< WorldMatrix > { typedef Value type; type& operator()(WorldMatrix& v, size_t r, size_t c) { return v[r][c]; } type const& operator()(const WorldMatrix& v, size_t r, size_t c) { return v[r][c]; } }; template struct at< mtl::matrix::base_matrix > { typedef Value type; type& operator()(mtl::matrix::base_matrix& v, size_t r, size_t c) { return v(r,c); } type const& operator()(const mtl::matrix::base_matrix& v, size_t r, size_t c) { return v(r,c); } }; template struct at< mtl::matrix::dense2D > { typedef Value type; type& operator()(mtl::matrix::dense2D& v, size_t r, size_t c) { return v(r,c); } type const& operator()(const mtl::matrix::dense2D& v, size_t r, size_t c) { return v(r,c); } }; template struct at< mtl::matrix::compressed2D > { typedef Value type; type& operator()(mtl::matrix::compressed2D& v, size_t r, size_t c) { return v(r,c); } type const& operator()(const mtl::matrix::compressed2D& v, size_t r, size_t c) { return v(r,c); } }; `````` Praetorius, Simon committed Sep 07, 2012 286 287 288 289 290 `````` } /// num_rows function for non-MTL types (uses implicit enable_if) template typename traits::num_rows::type `````` 291 `````` num_rows(const Collection& c) `````` Praetorius, Simon committed Sep 07, 2012 292 293 294 `````` { return traits::num_rows()(c); } `````` Praetorius, Simon committed Aug 02, 2012 295 `````` `````` Praetorius, Simon committed Sep 07, 2012 296 297 298 `````` /// num_cols function for non-MTL types (uses implicit enable_if) template typename traits::num_cols::type `````` 299 `````` num_cols(const Collection& c) `````` Praetorius, Simon committed Sep 07, 2012 300 301 302 `````` { return traits::num_cols()(c); } `````` Praetorius, Simon committed Aug 02, 2012 303 `````` `````` Praetorius, Simon committed Sep 07, 2012 304 305 `````` /// resize function for vectors template `````` 306 307 `````` typename traits::resize::vector_void_type resize(Collection& c, size_t rows) `````` Praetorius, Simon committed Sep 07, 2012 308 309 310 `````` { traits::resize()(c, rows); } `````` Praetorius, Simon committed Aug 02, 2012 311 `````` `````` Praetorius, Simon committed Sep 07, 2012 312 313 `````` /// resize function for matrices template `````` 314 315 `````` typename traits::resize::matrix_void_type resize(Collection& c, size_t rows, size_t cols) `````` Praetorius, Simon committed Sep 07, 2012 316 317 318 `````` { traits::resize()(c, rows, cols); } `````` Praetorius, Simon committed Aug 02, 2012 319 `````` `````` 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 `````` /// at function for matrices template typename traits::at::type const& at(const Collection& c, size_t rows, size_t cols) { return traits::at()(c, rows, cols); } /// at function for matrices template typename traits::at::type& at(Collection& c, size_t rows, size_t cols) { return traits::at()(c, rows, cols); } /// 2-norm for vectors template typename boost::enable_if< is_vector, typename ValueType::type >::type norm(const Vector &b) `````` Praetorius, Simon committed Aug 02, 2012 340 `````` { `````` 341 342 343 `````` typename ValueType::type value; nullify(value); for (size_t i = 0; i < num_rows(b); ++i) `````` Praetorius, Simon committed Aug 02, 2012 344 345 `````` value += sqr(b[i]); return sqrt(value); `````` Praetorius, Simon committed Apr 29, 2013 346 `````` } `````` Praetorius, Simon committed Aug 02, 2012 347 `````` `````` 348 349 `````` /// 2-norm for MTL4 vectors template `````` Praetorius, Simon committed Apr 29, 2013 350 `````` T norm(const mtl::dense_vector &b) { return two_norm(b); } `````` 351 352 `````` /// 1-norm for matrices `````` Praetorius, Simon committed Aug 02, 2012 353 `````` template `````` 354 355 `````` typename boost::enable_if< is_matrix, double >::type matrix_norm(const Matrix &M) `````` Praetorius, Simon committed Aug 02, 2012 356 357 358 359 360 361 362 `````` { typename Matrix::value_type asum; asum = 0.0; for (size_t i = 0; i < num_rows(M); ++i) { for (size_t j = 0; j < num_cols(M); ++j) asum += abs(M[i][j]); } `````` 363 `````` return static_cast(asum); `````` Praetorius, Simon committed Apr 29, 2013 364 `````` } `````` Praetorius, Simon committed Aug 02, 2012 365 366 `````` `````` 367 368 369 370 `````` /// 2-norm for vectors with eps-pertubation template typename boost::enable_if< is_vector, double >::type norm(const Vector &b, const double eps) `````` Praetorius, Simon committed Aug 02, 2012 371 `````` { `````` 372 `````` return std::max(static_cast(norm(b)), eps); `````` Praetorius, Simon committed Apr 29, 2013 373 `````` } `````` Praetorius, Simon committed Aug 02, 2012 374 `````` `````` 375 `````` /// trace of a matrix `````` Praetorius, Simon committed Aug 02, 2012 376 `````` template `````` 377 `````` typename boost::enable_if< is_matrix, double >::type `````` Praetorius, Simon committed Sep 07, 2012 378 `````` trace(Matrix &M) `````` Praetorius, Simon committed Aug 02, 2012 379 `````` { `````` 380 `````` TEST_EXIT_DBG(num_rows(M) == num_cols(M)) `````` Praetorius, Simon committed Sep 07, 2012 381 `````` ("Matrix dimensions must be equal!\n"); `````` Praetorius, Simon committed Aug 02, 2012 382 `````` typename Matrix::value_type sum; `````` Praetorius, Simon committed Sep 07, 2012 383 `````` nullify(sum); `````` 384 `````` for (size_t i = 0; i < num_rows(M); ++i) `````` Praetorius, Simon committed Aug 02, 2012 385 386 `````` sum += M[i][i]; return static_cast(sum); `````` Praetorius, Simon committed Sep 07, 2012 387 `````` } `````` Praetorius, Simon committed Aug 02, 2012 388 `````` `````` 389 390 391 392 `````` /// equivalent to matlab .* for vectors template typename boost::enable_if< boost::mpl::and_< is_vector, is_vector >, ResultVector >::type mult(const Vector1& v1, const Vector2& v2) `````` Praetorius, Simon committed Aug 02, 2012 393 `````` { `````` 394 395 396 397 398 `````` TEST_EXIT_DBG(num_rows(v1)==num_rows(v2)) ("Vectors must have the same size!\n"); ResultVector result(num_rows(v1)); for (size_t i = 0; i < num_rows(v1); ++i) result[i] = v1[i] * v2[i]; `````` Praetorius, Simon committed Aug 02, 2012 399 `````` return result; `````` Praetorius, Simon committed Apr 29, 2013 400 `````` } `````` Praetorius, Simon committed Aug 02, 2012 401 `````` `````` 402 403 404 `````` template typename boost::enable_if< boost::mpl::and_< is_matrix, is_vector >, ResultVector >::type mv(const Matrix &A, const Vector &x) `````` Praetorius, Simon committed Aug 02, 2012 405 `````` { `````` 406 407 408 409 `````` TEST_EXIT_DBG(num_cols(A)==num_rows(x)) ("Matrix and vector dimension must fit together!"); ResultVector result(num_rows(A)); for (size_t i = 0; i < num_rows(A); ++i) { `````` Praetorius, Simon committed Aug 02, 2012 410 `````` result[i] = 0.0; `````` 411 412 `````` for (size_t j = 0; j < num_cols(A); ++j) result[i] += at(A,i,j) * x[j]; `````` Praetorius, Simon committed Aug 02, 2012 413 414 `````` } return result; `````` Praetorius, Simon committed Apr 29, 2013 415 `````` } `````` Praetorius, Simon committed Aug 02, 2012 416 `````` `````` 417 418 419 420 `````` template typename boost::enable_if< boost::mpl::and_< is_matrix, is_vector, is_vector >, ResultVector >::type Axpy(const Matrix &A, const Vector1 &x, const Vector2 &y) `````` Praetorius, Simon committed Aug 02, 2012 421 `````` { `````` 422 423 `````` TEST_EXIT_DBG(num_cols(A)==num_row(x) && num_rows(A)==num_rows(y)) ("Matrix and vector dimensions must fit together!"); `````` Praetorius, Simon committed Aug 02, 2012 424 `````` ResultVector result; `````` 425 426 `````` resize(result, num_rows(A)); for (size_t i = 0; i < num_rows(A); ++i) { `````` Praetorius, Simon committed Aug 02, 2012 427 `````` result[i] = y[i]; `````` 428 429 `````` for (size_t j = 0; j < num_cols(A); ++j) result[i] += at(A,i,j) * x[j]; `````` Praetorius, Simon committed Aug 02, 2012 430 431 `````` } return result; `````` Praetorius, Simon committed Apr 29, 2013 432 `````` } `````` Praetorius, Simon committed Aug 02, 2012 433 `````` `````` Praetorius, Simon committed Sep 07, 2012 434 `````` // copy v -> w `````` 435 436 437 `````` template typename boost::enable_if< boost::mpl::and_< is_vector, is_vector >, void >::type copy(const VectorIn &v, VectorOut &w) `````` Praetorius, Simon committed Aug 02, 2012 438 `````` { `````` Praetorius, Simon committed Sep 07, 2012 439 440 `````` for (size_t i = 0; i < std::min(num_rows(v), num_rows(w)); i++) w[i] = v[i]; `````` Praetorius, Simon committed Apr 29, 2013 441 `````` } `````` 442 443 444 445 446 447 448 449 450 451 452 `````` template typename boost::enable_if< boost::mpl::and_< is_matrix, is_matrix >, void >::type fillMatrix(const MatrixIn &m_in, MatrixOut &m) { resize(m, num_rows(m_in), num_cols(m_in)); for (size_t i = 0; i < num_rows(m_in); ++i) { for (size_t j = 0; j < num_cols(m_in); ++j) { at(m,i,j) = at(m_in,i,j); } } `````` Praetorius, Simon committed Apr 29, 2013 453 `````` } `````` 454 455 456 457 458 459 `````` template typename boost::enable_if< boost::mpl::and_< is_matrix, is_matrix >, void >::type copy(const MatrixIn &m_in, MatrixOut &m) { fillMatrix(m_in, m); `````` Praetorius, Simon committed Sep 07, 2012 460 `````` } `````` Praetorius, Simon committed Aug 02, 2012 461 `````` `````` Praetorius, Simon committed Sep 07, 2012 462 463 `````` // v3:=[v1, v2] template `````` 464 465 466 `````` typename boost::enable_if< boost::mpl::and_< is_vector, is_vector, is_vector >, void >::type merge(Vector1 &v1, Vector2 &v2, VectorOut &v3) `````` Praetorius, Simon committed Aug 02, 2012 467 `````` { `````` Praetorius, Simon committed Sep 07, 2012 468 469 470 471 472 `````` resize(v3, num_rows(v1) + num_rows(v2)); for (size_t i = 0; i < num_rows(v1); i++) v3[i] = v1[i]; for (size_t j = 0; j < num_rows(v2); j++) v3[j + num_rows(v1)] = v2[j]; `````` Praetorius, Simon committed Apr 29, 2013 473 `````` } `````` Praetorius, Simon committed Aug 02, 2012 474 `````` `````` Praetorius, Simon committed Sep 07, 2012 475 `````` template `````` 476 `````` void getMin(const Vector &v, typename ValueType::type &minVal, size_t &minIdx) `````` Praetorius, Simon committed Aug 02, 2012 477 `````` { `````` Praetorius, Simon committed Sep 07, 2012 478 479 480 481 482 483 484 485 486 `````` typedef typename ValueType::type T; TEST_EXIT(num_rows(v) > 0)("getMin of empty vector!\n"); minVal = v[0]; minIdx = 0; for (size_t i = 1; i < num_rows(v); i++) { if (v[i] < minVal) { minVal = v[i]; minIdx = i; `````` Praetorius, Simon committed Aug 02, 2012 487 488 `````` } } `````` Praetorius, Simon committed Apr 29, 2013 489 `````` } `````` Praetorius, Simon committed Aug 02, 2012 490 `````` `````` Praetorius, Simon committed Sep 07, 2012 491 `````` template `````` 492 `````` void getMax(const Vector &v, typename ValueType::type &maxVal, size_t &maxIdx) `````` Praetorius, Simon committed Aug 02, 2012 493 `````` { `````` Praetorius, Simon committed Sep 07, 2012 494 495 496 497 498 499 500 501 502 `````` typedef typename ValueType::type T; TEST_EXIT(num_rows(v) > 0)("getMax of empty vector!\n"); maxVal = v[0]; maxIdx = 0; for (size_t i = 1; i < num_rows(v); i++) { if (v[i] > maxVal) { maxVal = v[i]; maxIdx = i; `````` Praetorius, Simon committed Aug 02, 2012 503 504 `````` } } `````` Praetorius, Simon committed Apr 29, 2013 505 `````` } `````` Praetorius, Simon committed Sep 07, 2012 506 507 `````` template `````` 508 `````` typename ValueType::type `````` Praetorius, Simon committed Sep 07, 2012 509 `````` min(const Vector &vec) `````` Praetorius, Simon committed Aug 02, 2012 510 `````` { `````` Praetorius, Simon committed Sep 07, 2012 511 512 513 514 `````` typename ValueType::type minVal; size_t minIdx; getMin(vec, minVal, minIdx); return minVal; `````` Praetorius, Simon committed Apr 29, 2013 515 `````` } `````` Praetorius, Simon committed Aug 02, 2012 516 `````` `````` Praetorius, Simon committed Sep 07, 2012 517 `````` template `````` 518 `````` typename ValueType::type `````` Praetorius, Simon committed Sep 07, 2012 519 `````` max(const Vector &vec) `````` Praetorius, Simon committed Aug 02, 2012 520 `````` { `````` Praetorius, Simon committed Sep 07, 2012 521 522 523 `````` typename ValueType::type maxVal; size_t maxIdx; getMax(vec, maxVal, maxIdx); `````` Praetorius, Simon committed Aug 02, 2012 524 `````` return maxVal; `````` Praetorius, Simon committed Apr 29, 2013 525 `````` } `````` Praetorius, Simon committed Sep 07, 2012 526 527 `````` template `````` 528 `````` typename ValueType::type `````` Praetorius, Simon committed Sep 07, 2012 529 `````` absmin(const Vector &v) `````` Praetorius, Simon committed Aug 02, 2012 530 `````` { `````` Praetorius, Simon committed Sep 07, 2012 531 532 533 534 535 536 537 `````` typedef typename ValueType::type T; TEST_EXIT(num_rows(v) > 0)("absmin of empty vector!\n"); T minVal = abs(v[0]); for(size_t i = 1; i < num_rows(v); i++) { if (std::abs(v[i]) < minVal) { minVal = std::abs(v[i]); `````` Praetorius, Simon committed Aug 02, 2012 538 539 540 `````` } } return minVal; `````` Praetorius, Simon committed Apr 29, 2013 541 `````` } `````` Praetorius, Simon committed Aug 02, 2012 542 `````` `````` Praetorius, Simon committed Sep 07, 2012 543 `````` template `````` 544 `````` typename ValueType::type `````` Praetorius, Simon committed Sep 07, 2012 545 `````` absmax(const Vector &v) `````` Praetorius, Simon committed Aug 02, 2012 546 `````` { `````` Praetorius, Simon committed Sep 07, 2012 547 548 549 550 551 552 553 `````` typedef typename ValueType::type T; TEST_EXIT(num_rows(v) > 0)("absmax of empty vector!\n"); T maxVal = abs(v[0]); for(size_t i = 1; i < num_rows(v); i++) { if (std::abs(v[i]) > maxVal) { maxVal = std::abs(v[i]); `````` Praetorius, Simon committed Aug 02, 2012 554 555 556 `````` } } return maxVal; `````` Praetorius, Simon committed Apr 29, 2013 557 `````` } `````` Praetorius, Simon committed Aug 02, 2012 558 `````` `````` Praetorius, Simon committed Sep 07, 2012 559 `````` template `````` 560 `````` typename ValueType::type `````` Praetorius, Simon committed Sep 07, 2012 561 `````` sum(const Vector &v) `````` Praetorius, Simon committed Aug 02, 2012 562 `````` { `````` Praetorius, Simon committed Sep 07, 2012 563 564 565 566 567 568 569 `````` typedef typename ValueType::type T; size_t N = num_rows(v); T value; nullify(value); if (N > 0) { value = v[0]; for (size_t i = 1; i < N; ++i) value += v[i]; `````` Praetorius, Simon committed Aug 02, 2012 570 571 `````` } return value; `````` Praetorius, Simon committed Apr 29, 2013 572 `````` } `````` Praetorius, Simon committed Aug 02, 2012 573 `````` `````` Praetorius, Simon committed Sep 07, 2012 574 575 576 `````` template inline typename ValueType::type mean(const Vector &v) `````` Praetorius, Simon committed Aug 02, 2012 577 `````` { `````` Praetorius, Simon committed Sep 07, 2012 578 579 `````` double N = static_cast(num_rows(v)); return sum(v) * (1.0 / N); `````` Praetorius, Simon committed Apr 29, 2013 580 `````` } `````` Praetorius, Simon committed Aug 02, 2012 581 `````` `````` Praetorius, Simon committed Sep 07, 2012 582 583 584 585 `````` template void getMean(const Vector &v, typename ValueType::type &meanValue) { meanValue = mean(v); `````` Praetorius, Simon committed Apr 29, 2013 586 `````` } `````` Praetorius, Simon committed Aug 02, 2012 587 588 589 590 591 592 593 `````` template void sort(std::vector &vec1, std::vector &vec2, Compare &comp) { std::vector > order(vec1.size()); // create vector of pairs `````` Praetorius, Simon committed Mar 05, 2013 594 `````` for (size_t i=0; i void sort(std::vector > &vec, Compare &comp) { std::sort(vec.begin(), vec.end(), comp); `````` Praetorius, Simon committed Apr 29, 2013 611 `````` } `````` Praetorius, Simon committed Aug 02, 2012 612 613 614 615 616 617 618 619 620 621 622 623 624 625 `````` inline void unique(std::vector > &vec, double tol, std::vector &ind) { compareTol > comp(tol); unsigned newVec=0; for(unsigned i=0; i void unique(std::vector > &vec, double tol, int uniqueBy=1) { // comparePair comp = comparePair(tol,uniqueBy); // unsigned newVec=0; // for(unsigned i=0; i inline void filter(std::vector &x, const std::vector &filter) `````` Praetorius, Simon committed Aug 02, 2012 646 `````` { `````` 647 648 649 650 651 652 `````` TEST_EXIT_DBG(num_rows(x) == num_rows(filter)) ("number of rows of x and filter must be equal!\n"); size_t j = 0; for (size_t i = 0; i < num_rows(x); ++i) { if (filter[i]) { x[j] = x[i]; `````` Praetorius, Simon committed Aug 02, 2012 653 654 655 656 `````` j++; } } x.erase(x.begin()+j,x.end()); `````` Praetorius, Simon committed Apr 29, 2013 657 `````` } `````` Praetorius, Simon committed Aug 02, 2012 658 `````` `````` 659 660 `````` template void filter(std::vector &x, const std::vector &filter) `````` Praetorius, Simon committed Aug 02, 2012 661 `````` { `````` 662 663 664 665 `````` std::vector x_(num_rows(filter)); for (size_t i = 0; i < num_rows(filter); ++i) x_[i] = x[filter[i]]; swap(x, x_); `````` Praetorius, Simon committed Apr 29, 2013 666 667 `````` } `````` 668 669 670 `````` template typename ProductType::type, typename ValueType::type>::type dot(const Vector1 &vec1, const Vector2 &vec2) `````` Praetorius, Simon committed Aug 02, 2012 671 `````` { `````` 672 673 674 675 676 677 678 679 `````` typedef typename ProductType::type, typename ValueType::type>::type value_type; value_type value; nullify(value); size_t N = num_rows(vec1); if (N > 0) { value = vec1[0] * vec2[0]; for (size_t i = 1; i < N; ++i) value += vec1[i] * vec2[i]; `````` Praetorius, Simon committed Aug 02, 2012 680 `````` } `````` 681 `````` return value; `````` Praetorius, Simon committed Apr 29, 2013 682 `````` } `````` Praetorius, Simon committed Aug 02, 2012 683 684 685 ``````} #endif // VECTOR_OPERATIONS_H``````