vec_functors.hpp 14.8 KB
 Praetorius, Simon committed Aug 06, 2014 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 ``````/****************************************************************************** * * AMDiS - Adaptive multidimensional simulations * * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved. * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis * * Authors: * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al. * * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * * This file is part of AMDiS * * See also license.opensource.txt in the distribution. * ******************************************************************************/ /** \file vec_functors.hpp */ #ifndef AMDIS_VEC_FUNCTORS_HPP #define AMDIS_VEC_FUNCTORS_HPP #include "functor_expr.hpp" #include "operations/norm.hpp" #include "operations/product.hpp" `````` 32 33 34 35 36 37 38 39 40 41 42 43 ``````/** * This file provides expressions for vectors and matrices: * (where v_expr is an expression of vector type, and m_expr an * expression of matrix type) * * two_norm(v_expr) ... the 2-norm of a vector v: result = sqrt(v^H * v) * one_norm(v_expr) ... the 1-norm of a vector v: result = sum_i(abs(v_i)) * one_norm(m_expr) ... the 1-norm of a matrix m: result = max_j(sum_i(abs(m_ij)) * p_norm
(v_expr) .. the P-norm of a vector v: result = [sum_i(abs(v_i)^P)]^(1/P) * **/ `````` Praetorius, Simon committed Aug 06, 2014 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 ``````namespace AMDiS { namespace traits { template struct is_always_true : boost::mpl::true_ {}; } namespace result_of { // helper class for UnaryExpr template class Functor, typename Term, template class Condition1 = traits::is_always_true, template class Condition2 = traits::is_always_true, template class Condition3 = traits::is_always_true> struct UnaryExpr : boost::enable_if < typename boost::mpl::and_ < typename traits::is_expr::type, typename boost::mpl::or_ < typename Condition1::type, typename Condition2::type, typename Condition3::type >::type >, expressions::Function1, Term> > {}; // helper class for UnaryExpr template class Condition1 = traits::is_always_true, template class Condition2 = traits::is_always_true, template class Condition3 = traits::is_always_true> struct UnaryExprFull : boost::enable_if < typename boost::mpl::and_ < typename traits::is_expr::type, typename boost::mpl::or_ < typename Condition1::type, typename Condition2::type, typename Condition3::type >::type >, expressions::Function1 > {}; // helper class for BinaryExpr template class Functor, typename Term1, typename Term2, template class Condition1 = traits::is_always_true, template class Condition2 = traits::is_always_true, template class Condition3 = traits::is_always_true, typename Expr1 = typename traits::to_expr::type, typename Expr2 = typename traits::to_expr::type> struct BinaryExpr : boost::enable_if < typename boost::mpl::and_ < typename traits::is_valid_arg2::type, typename boost::mpl::or_ < typename Condition1::type, typename Condition2::type, typename Condition3::type >::type, typename boost::mpl::or_ < typename Condition1::type, typename Condition2::type, typename Condition3::type >::type >::type, expressions::Function2 < Functor, Expr1, Expr2 > > {}; } // end namespace result_of /// expression with one vector template class Functor, typename Term> inline typename result_of::UnaryExpr::type unary_expr(const Term& t) { `````` Praetorius, Simon committed Mar 17, 2015 137 138 `````` typedef Functor< typename Term::value_type > F; return function_(F(), t); `````` Praetorius, Simon committed Aug 06, 2014 139 140 `````` } `````` Praetorius, Simon committed Mar 17, 2015 141 142 `````` template inline typename result_of::UnaryExprFull::type `````` Praetorius, Simon committed Aug 06, 2014 143 144 `````` unary_expr_full(const Term& t) { `````` Praetorius, Simon committed Mar 17, 2015 145 `````` return function_(F(), t); `````` Praetorius, Simon committed Aug 06, 2014 146 147 148 149 150 151 152 153 154 155 `````` } /// expression with two vectors v1, v2 template class Functor, typename Term1, typename Term2> inline typename result_of::BinaryExpr::type binary_expr(const Term1& t1, const Term2& t2) { typedef typename traits::to_expr::to Expr1; typedef typename traits::to_expr::to Expr2; `````` Praetorius, Simon committed Mar 17, 2015 156 157 `````` typedef Functor< typename Expr1::type::value_type, typename Expr2::type::value_type > F; return function_(F(), Expr1::get(t1), Expr2::get(t2)); `````` Praetorius, Simon committed Aug 06, 2014 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 `````` } // two_norm of vectors // _____________________________________________________________________________ namespace result_of { template struct two_norm : result_of::UnaryExpr {}; } /// the 2-norm of a vector v: result = sqrt(v^H * v) template inline typename result_of::two_norm::type two_norm_dispatch(const Term& t, tag::expression) { return unary_expr< functors::TwoNorm >(t); } // one_norm of vectors and matrices // _____________________________________________________________________________ namespace result_of { template struct one_norm : result_of::UnaryExpr {}; } `````` 188 `````` /// the 1-norm of a vector v: result = max_i(abs(v_i)) `````` Praetorius, Simon committed Aug 06, 2014 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 432 433 434 435 436 `````` template inline typename result_of::one_norm::type one_norm_dispatch(const Term& t, tag::expression) { return unary_expr< functors::OneNorm >(t); } // p_norm
of vectors // _____________________________________________________________________________ namespace result_of { template struct p_norm : result_of::UnaryExprFull, T, traits::is_vector> {}; } /// the 2-norm of a vector v: result = sqrt(v^H * v) template inline typename result_of::p_norm::type p_norm_dispatch(const Term& t, tag::expression) { return unary_expr_full< functors::PNorm >(t); } // unary dot (v' * v) of a vector v // _____________________________________________________________________________ namespace result_of { template struct unary_dot : result_of::UnaryExpr {}; } /// the 2-norm squared of a vector v: result = v^H * v template inline typename result_of::unary_dot::type unary_dot_dispatch(const Term& t, tag::expression) { return unary_expr< functors::UnaryDot >(t); } // inner product of two vectors // _____________________________________________________________________________ namespace result_of { template struct dot : result_of::BinaryExpr {}; template struct dot : result_of::BinaryExpr {}; template struct dot : result_of::BinaryExpr {}; } /// inner/dot product of two vectors v1, v2: result = v1^T * v2 template inline typename boost::enable_if< typename traits::is_expr::type, typename result_of::dot::type >::type dot_dispatch(const Term1& t1, const Term2& t2, tag::expression tag1_, Tag2 tag2_) { return binary_expr(t1, t2); } template inline typename boost::enable_if_c< (traits::is_expr::type::value && !(traits::is_expr::type::value)), typename result_of::dot::type >::type dot_dispatch(const Term1& t1, const Term2& t2, Tag1 tag1_, tag::expression tag2_) { return binary_expr(t1, t2); } // cross-product of two vectors // _____________________________________________________________________________ namespace result_of { template struct cross : result_of::BinaryExpr {}; template struct cross : result_of::BinaryExpr {}; template struct cross : result_of::BinaryExpr {}; } /// cross product of two vectors v1, v2: result = v1 x v2 template inline typename boost::enable_if< typename traits::is_expr::type, typename result_of::cross::type >::type cross_dispatch(const Term1& t1, const Term2& t2, tag::expression tag1_, Tag2 tag2_) { return binary_expr(t1, t2); } template inline typename boost::enable_if_c< (traits::is_expr::type::value && !(traits::is_expr::type::value)), typename result_of::cross::type >::type cross_dispatch(const Term1& t1, const Term2& t2, Tag1 tag1_, tag::expression tag2_) { return binary_expr(t1, t2); } // generator for a diagonal matrix from a vector // _____________________________________________________________________________ namespace expressions { template struct DiagonalMat : public FunctorBase {}; template struct DiagonalMat > : public FunctorBase { typedef WorldMatrix result_type; int getDegree(int d0) const { return d0; } result_type operator()(const WorldVector &v) const { T zero; nullify(zero); result_type matrix(DEFAULT_VALUE, zero); for (int i = 0; i < v.getSize(); ++i) matrix[i][i] = v[i]; return matrix; } }; template struct DiagonalMat > : public FunctorBase { typedef DimMat result_type; int getDegree(int d0) const { return d0; } result_type operator()(const DimVec &v) const { T zero; nullify(zero); result_type matrix(v.getDim(), DEFAULT_VALUE, zero); for (int i = 0; i < v.getSize(); ++i) matrix[i][i] = v[i]; return matrix; } }; template struct DiagonalMat > : public FunctorBase { typedef Matrix result_type; int getDegree(int d0) const { return d0; } result_type operator()(const DimVec &v) const { T zero; nullify(zero); result_type matrix(v.getSize(), v.getSize()); matrix.set(zero); for (int i = 0; i < v.getSize(); ++i) matrix[i][i] = v[i]; return matrix; } }; template struct DiagonalMat > : public FunctorBase { typedef mtl::matrix::compressed2D > result_type; int getDegree(int d0) const { return d0; } template result_type operator()(const Vector &v) const { return diagonal(v); } }; } /// create diagonal matrix from vector template inline typename result_of::UnaryExpr::type diagonal(const Term& t) { return unary_expr(t); } // outer product / dyadic product of two vectors to create a matrix // _____________________________________________________________________________ /// outer product of two vectors v1, v2: result = v1 * v2^T template inline typename result_of::BinaryExpr::type outer(const Term1& t1, const Term2& t2) { return binary_expr(t1, t2); } // extract a component of a vector/matrix // _____________________________________________________________________________ namespace expressions { template struct VecComponent : public FunctorBase { typedef typename traits::category::value_type result_type; VecComponent(int I_) : I(I_) {} int getDegree(int d0) const { return d0; } result_type operator()(const T &v) const { return v[I]; } private: int I; }; template struct MatComponent : public FunctorBase { typedef typename traits::category::value_type result_type; MatComponent(int I_, int J_) : I(I_), J(J_) {} int getDegree(int d0) const { return d0; } result_type operator()(const T &m) const { return m[I][J]; } private: int I; int J; }; } template typename result_of::UnaryExpr::type at(const Term& t, int I) { return function_(expressions::VecComponent(I), t); } template typename result_of::UnaryExpr::type at(const Term& t, int I, int J) { return function_(expressions::MatComponent(I, J), t); } `````` Praetorius, Simon committed Dec 01, 2014 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 `````` // transpose a matrix // _____________________________________________________________________________ namespace expressions { template struct MatTranspose : public FunctorBase { typedef Mat result_type; int getDegree(int d0) const { return d0; } result_type operator()(const Mat &m) const { Mat result; for (size_t r = 0; r < num_rows(m); r++) for (size_t c = 0; c < num_cols(m); c++) result[c][r] = m[r][c]; return result; } }; } template typename result_of::UnaryExpr::type trans(const Term& t) { return function_(expressions::MatTranspose(), t); } `````` Praetorius, Simon committed Aug 06, 2014 466 467 468 ``````} #endif // AMDIS_VEC_FUNCTORS_HPP``````