/usr/include/vigra/multi_math.hxx is in libvigraimpex-dev 1.9.0+dfsg-10+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 432 433 434 435 436 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 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 | /************************************************************************/
/* */
/* Copyright 2010-2011 by Ullrich Koethe */
/* */
/* This file is part of the VIGRA computer vision library. */
/* The VIGRA Website is */
/* http://hci.iwr.uni-heidelberg.de/vigra/ */
/* Please direct questions, bug reports, and contributions to */
/* ullrich.koethe@iwr.uni-heidelberg.de or */
/* vigra@informatik.uni-hamburg.de */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the */
/* Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
/* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
/* OTHER DEALINGS IN THE SOFTWARE. */
/* */
/************************************************************************/
#ifndef VIGRA_MULTI_MATH_HXX
#define VIGRA_MULTI_MATH_HXX
#include "multi_array.hxx"
#include "tinyvector.hxx"
#include "rgbvalue.hxx"
#include "mathutil.hxx"
#include <complex>
namespace vigra {
/** \defgroup MultiMathModule vigra::multi_math
Namespace <tt>vigra::multi_math</tt> holds VIGRA's support for efficient arithmetic and algebraic functions on multi-dimensional arrays (that is, \ref MultiArrayView and its subclasses). All <tt>multi_math</tt> functions operate element-wise. If you need matrix multiplication, use \ref LinearAlgebraModule instead.
In order to avoid overload ambiguities, multi-array arithmetic must be explicitly activated by
\code
using namespace vigra::multi_math;
\endcode
(this should not be done globally, but only in the scope where the functionality is actually used).
You can then use the standard operators in the expected way:
\code
MultiArray<2, float> i(Shape2(100, 100)), j(Shape2(100, 100));
MultiArray<2, float> h = i + 4.0 * j;
h += (i.transpose() - j) / 2.0;
\endcode
etc. (supported operators are <tt>+ - * / ! ~ % && || == != < <= > >= << >> & | ^ = += -= *= /=</tt>, with both scalar and array arguments).
Algebraic functions are available as well:
\code
h = exp(-(sq(i) + sq(j)));
h *= atan2(-i, j);
\endcode
The following functions are implemented: <tt>abs, erf, even, odd, sign, signi, round, roundi, sqrt, sqrti, sq,
norm, squaredNorm, gamma, loggamma, exp, log, log10, sin, sin_pi, cos, cos_pi, asin, acos, tan, atan,
floor, ceil, conj, real, imag, arg, atan2, pow, fmod, min, max</tt>,
provided the array's element type supports the respective function.
Supported element types currently include the built-in numeric types, \ref TinyVector, \ref RGBValue,
<tt>std::complex</tt>, and \ref FFTWComplex.
In addition, <tt>multi_math</tt> supports a number of functions that reduce arrays to scalars:
\code
double s = sum<double>(i); // compute the sum of the elements, using 'double' as accumulator type
double p = product<double>(abs(i)); // compute the product of the elements' absolute values
bool a = any(i < 0.0); // check if any element of i is negative
bool b = all(i > 0.0); // check if all elements of i are positive
\endcode
Expressions are expanded so that no temporary arrays have to be created. To optimize cache locality,
loops are executed in the stride ordering of the left-hand-side array.
<b>\#include</b> \<vigra/multi_math.hxx\>
Namespace: vigra::multi_math
*/
namespace multi_math {
template <class ARG>
struct MultiMathOperand
{
typedef typename ARG::result_type result_type;
static const int ndim = ARG::ndim;
MultiMathOperand(ARG const & a)
: arg_(a)
{}
// Check if all arrays involved in the expression have compatible shapes
// (including transparent expansion of singleton axes).
// 's' is the shape of the LHS array. If 's' is zero (i.e. the LHS is
// not yet initialized), it is set to the maximal RHS shape.
//
template <class SHAPE>
bool checkShape(SHAPE & s) const
{
return arg_.checkShape(s);
}
// increment the pointer of all RHS arrays along the given 'axis'
void inc(unsigned int axis) const
{
arg_.inc(axis);
}
// reset the pointer of all RHS arrays along the given 'axis'
void reset(unsigned int axis) const
{
arg_.reset(axis);
}
// get the value of the expression at the current pointer location
result_type operator*() const
{
return *arg_;
}
// get the value of the expression at an offset of the current pointer location
template <class SHAPE>
result_type operator[](SHAPE const & s) const
{
return arg_[s];
}
ARG arg_;
};
template <unsigned int N, class T, class C>
struct MultiMathOperand<MultiArrayView<N, T, C> >
{
typedef MultiMathOperand AllowOverload;
typedef typename MultiArrayShape<N>::type Shape;
typedef T result_type;
static const int ndim = (int)N;
MultiMathOperand(MultiArrayView<N, T, C> const & a)
: p_(a.data()),
shape_(a.shape()),
strides_(a.stride())
{
// allow for transparent expansion of singleton dimensions
for(unsigned int k=0; k<N; ++k)
if(shape_[k] == 1)
strides_[k] = 0;
}
bool checkShape(Shape & s) const
{
// support:
// * transparent expansion of singleton dimensions
// * determining LHS shape in a constructor
for(unsigned int k=0; k<N; ++k)
{
if(shape_[k] == 0)
{
return false;
}
else if(s[k] <= 1)
{
s[k] = shape_[k];
}
else if(shape_[k] > 1 && shape_[k] != s[k])
{
return false;
}
}
return true;
}
T const & operator[](Shape const & s) const
{
return p_[dot(s, strides_)];
}
void inc(unsigned int axis) const
{
p_ += strides_[axis];
}
void reset(unsigned int axis) const
{
p_ -= shape_[axis]*strides_[axis];
}
result_type operator*() const
{
return *p_;
}
mutable T const * p_;
Shape shape_, strides_;
};
template <unsigned int N, class T, class A>
struct MultiMathOperand<MultiArray<N, T, A> >
: public MultiMathOperand<MultiArrayView<N, T, UnstridedArrayTag> >
{
typedef MultiMathOperand AllowOverload;
MultiMathOperand(MultiArray<N, T, A> const & a)
: MultiMathOperand<MultiArrayView<N, T, UnstridedArrayTag> >(a)
{}
};
template <class T>
struct MultiMathScalarOperand
{
typedef MultiMathOperand<T> AllowOverload;
typedef T result_type;
static const int ndim = 0;
MultiMathScalarOperand(T const & v)
: v_(v)
{}
template <class SHAPE>
bool checkShape(SHAPE const &) const
{
return true;
}
template <class SHAPE>
T const & operator[](SHAPE const &) const
{
return v_;
}
void inc(unsigned int /* axis */) const
{}
void reset(unsigned int /* axis */) const
{}
T const & operator*() const
{
return v_;
}
T v_;
};
#define VIGRA_CONSTANT_OPERAND(template_dcl, type) \
template template_dcl \
struct MultiMathOperand<type > \
: MultiMathScalarOperand<type > \
{ \
MultiMathOperand(type const & v) \
: MultiMathScalarOperand<type >(v) \
{} \
};
VIGRA_CONSTANT_OPERAND(<>, signed char)
VIGRA_CONSTANT_OPERAND(<>, signed short)
VIGRA_CONSTANT_OPERAND(<>, signed int)
VIGRA_CONSTANT_OPERAND(<>, signed long)
VIGRA_CONSTANT_OPERAND(<>, signed long long)
VIGRA_CONSTANT_OPERAND(<>, unsigned char)
VIGRA_CONSTANT_OPERAND(<>, unsigned short)
VIGRA_CONSTANT_OPERAND(<>, unsigned int)
VIGRA_CONSTANT_OPERAND(<>, unsigned long)
VIGRA_CONSTANT_OPERAND(<>, unsigned long long)
VIGRA_CONSTANT_OPERAND(<>, float)
VIGRA_CONSTANT_OPERAND(<>, double)
VIGRA_CONSTANT_OPERAND(<>, long double)
VIGRA_CONSTANT_OPERAND(<class T>, std::complex<T>)
#define VIGRA_TINYVECTOR_ARGS <class T, int N>
#define VIGRA_TINYVECTOR_DECL TinyVector<T, N>
VIGRA_CONSTANT_OPERAND(VIGRA_TINYVECTOR_ARGS, VIGRA_TINYVECTOR_DECL)
#undef VIGRA_TINYVECTOR_ARGS
#undef VIGRA_TINYVECTOR_DECL
#define VIGRA_RGBVALUE_ARGS <class V, unsigned int R, unsigned int G, unsigned int B>
#define VIGRA_RGBVALUE_DECL RGBValue<V, R, G, B>
VIGRA_CONSTANT_OPERAND(VIGRA_RGBVALUE_ARGS, VIGRA_RGBVALUE_DECL)
#undef VIGRA_RGBVALUE_ARGS
#undef VIGRA_RGBVALUE_DECL
#undef VIGRA_CONSTANT_OPERAND
template <class O, class F>
struct MultiMathUnaryOperator
{
typedef typename F::template Result<typename O::result_type>::type result_type;
static const int ndim = O::ndim;
MultiMathUnaryOperator(O const & o)
: o_(o)
{}
template <class SHAPE>
bool checkShape(SHAPE & s) const
{
return o_.checkShape(s);
}
//
void inc(unsigned int axis) const
{
o_.inc(axis);
}
void reset(unsigned int axis) const
{
o_.reset(axis);
}
template <class POINT>
result_type operator[](POINT const & p) const
{
return f_(o_[p]);
}
result_type operator*() const
{
return f_(*o_);
}
O o_;
F f_;
};
#define VIGRA_MULTIMATH_UNARY_OPERATOR(NAME, FCT, OPNAME, RESTYPE) \
namespace detail { \
struct NAME \
{ \
template <class T> \
struct Result \
{ \
typedef RESTYPE type; \
}; \
\
template <class T> \
typename Result<T>::type \
operator()(T const & t) const \
{ \
return FCT(t); \
} \
}; \
} \
\
template <unsigned int N, class T, class C> \
MultiMathOperand<MultiMathUnaryOperator<MultiMathOperand<MultiArrayView<N, T, C> >, \
detail::NAME> > \
OPNAME(MultiArrayView<N, T, C> const & v) \
{ \
typedef MultiMathOperand<MultiArrayView<N, T, C> > O; \
typedef MultiMathUnaryOperator<O, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v)); \
} \
\
template <unsigned int N, class T, class A> \
MultiMathOperand<MultiMathUnaryOperator<MultiMathOperand<MultiArray<N, T, A> >, \
detail::NAME> > \
OPNAME(MultiArray<N, T, A> const & v) \
{ \
typedef MultiMathOperand<MultiArray<N, T, A> > O; \
typedef MultiMathUnaryOperator<O, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v)); \
} \
\
template <class T> \
MultiMathOperand<MultiMathUnaryOperator<MultiMathOperand<T>, \
detail::NAME> > \
OPNAME(MultiMathOperand<T> const & v) \
{ \
typedef MultiMathOperand<T> O; \
typedef MultiMathUnaryOperator<O, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v)); \
}
#define VIGRA_REALPROMOTE typename NumericTraits<T>::RealPromote
#ifndef DOXYGEN // doxygen gets confused by these macros
VIGRA_MULTIMATH_UNARY_OPERATOR(Negate, -, operator-, T)
VIGRA_MULTIMATH_UNARY_OPERATOR(Not, !, operator!, T)
VIGRA_MULTIMATH_UNARY_OPERATOR(BitwiseNot, ~, operator~, T)
using vigra::abs;
VIGRA_MULTIMATH_UNARY_OPERATOR(Abs, vigra::abs, abs, typename NormTraits<T>::NormType)
using vigra::erf;
VIGRA_MULTIMATH_UNARY_OPERATOR(Erf, vigra::erf, erf, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Even, vigra::even, even, bool)
VIGRA_MULTIMATH_UNARY_OPERATOR(Odd, vigra::odd, odd, bool)
VIGRA_MULTIMATH_UNARY_OPERATOR(Sign, vigra::sign, sign, T)
VIGRA_MULTIMATH_UNARY_OPERATOR(Signi, vigra::signi, signi, int)
using vigra::round;
VIGRA_MULTIMATH_UNARY_OPERATOR(Round, vigra::round, round, T)
VIGRA_MULTIMATH_UNARY_OPERATOR(Roundi, vigra::roundi, roundi, int)
VIGRA_MULTIMATH_UNARY_OPERATOR(Sqrti, vigra::sqrti, sqrti, T)
VIGRA_MULTIMATH_UNARY_OPERATOR(Sq, vigra::sq, sq, typename NumericTraits<T>::Promote)
VIGRA_MULTIMATH_UNARY_OPERATOR(Norm, vigra::norm, norm, typename NormTraits<T>::NormType)
VIGRA_MULTIMATH_UNARY_OPERATOR(SquaredNorm, vigra::squaredNorm, squaredNorm,
typename NormTraits<T>::SquaredNormType)
VIGRA_MULTIMATH_UNARY_OPERATOR(Sin_pi, vigra::sin_pi, sin_pi, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Cos_pi, vigra::cos_pi, cos_pi, VIGRA_REALPROMOTE)
using vigra::gamma;
VIGRA_MULTIMATH_UNARY_OPERATOR(Gamma, vigra::gamma, gamma, VIGRA_REALPROMOTE)
using vigra::loggamma;
VIGRA_MULTIMATH_UNARY_OPERATOR(Loggamma, vigra::loggamma, loggamma, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Sqrt, std::sqrt, sqrt, VIGRA_REALPROMOTE)
using vigra::exp;
VIGRA_MULTIMATH_UNARY_OPERATOR(Exp, vigra::exp, exp, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Log, std::log, log, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Log10, std::log10, log10, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Sin, std::sin, sin, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Asin, std::asin, asin, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Cos, std::cos, cos, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Acos, std::acos, acos, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Tan, std::tan, tan, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Atan, std::atan, atan, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Floor, std::floor, floor, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Ceil, std::ceil, ceil, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_UNARY_OPERATOR(Conj, conj, conj, T)
VIGRA_MULTIMATH_UNARY_OPERATOR(Real, real, real, typename T::value_type)
VIGRA_MULTIMATH_UNARY_OPERATOR(Imag, imag, imag, typename T::value_type)
VIGRA_MULTIMATH_UNARY_OPERATOR(Arg, arg, arg, typename T::value_type)
#endif //DOXYGEN
#undef VIGRA_REALPROMOTE
#undef VIGRA_MULTIMATH_UNARY_OPERATOR
template <class O1, class O2, class F>
struct MultiMathBinaryOperator
{
typedef typename F::template Result<typename O1::result_type,
typename O2::result_type>::type result_type;
static const int ndim = O1::ndim > O2::ndim ? O1::ndim : O2::ndim;
MultiMathBinaryOperator(O1 const & o1, O2 const & o2)
: o1_(o1),
o2_(o2)
{}
template <class SHAPE>
bool checkShape(SHAPE & s) const
{
return o1_.checkShape(s) && o2_.checkShape(s);
}
template <class POINT>
result_type operator[](POINT const & p) const
{
return f_(o1_[p], o2_[p]);
}
void inc(unsigned int axis) const
{
o1_.inc(axis);
o2_.inc(axis);
}
void reset(unsigned int axis) const
{
o1_.reset(axis);
o2_.reset(axis);
}
result_type operator*() const
{
return f_(*o1_, *o2_);
}
O1 o1_;
O2 o2_;
F f_;
};
// In the sequel, the nested type 'MultiMathOperand<T>::AllowOverload'
// ensures that template functions only participate in overload
// resolution when this type is defined, i.e. when T is a number
// or array type. It thus prevents 'ambiguous overload' errors.
//
#define VIGRA_MULTIMATH_BINARY_OPERATOR(NAME, FCT, OPNAME, SEP, RESTYPE) \
\
namespace detail { \
struct NAME \
{ \
template <class T1, class T2> \
struct Result \
{ \
typedef RESTYPE type; \
}; \
\
template <class T1, class T2> \
typename Result<T1, T2>::type \
operator()(T1 const & t1, T2 const & t2) const \
{ \
return FCT(t1 SEP t2); \
} \
}; \
} \
\
template <unsigned int N, class T1, class A1, class T2, class A2> \
MultiMathOperand<MultiMathBinaryOperator<MultiMathOperand<MultiArrayView<N, T1> >, \
MultiMathOperand<MultiArrayView<N, T2> >, \
detail::NAME> > \
OPNAME(MultiArray<N, T1, A1> const & v1, MultiArray<N, T2, A2> const & v2) \
{ \
typedef MultiMathOperand<MultiArrayView<N, T1> > O1; \
typedef MultiMathOperand<MultiArrayView<N, T2> > O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP((MultiArrayView<N, T1> const &)v1, (MultiArrayView<N, T2> const &)v2)); \
} \
\
template <unsigned int N, class T1, class C1, class T2, class C2> \
MultiMathOperand<MultiMathBinaryOperator<MultiMathOperand<MultiArrayView<N, T1, C1> >, \
MultiMathOperand<MultiArrayView<N, T2, C2> >, \
detail::NAME> > \
OPNAME(MultiArrayView<N, T1, C1> const & v1, MultiArrayView<N, T2, C2> const & v2) \
{ \
typedef MultiMathOperand<MultiArrayView<N, T1, C1> > O1; \
typedef MultiMathOperand<MultiArrayView<N, T2, C2> > O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
} \
\
template <unsigned int N, class T1, class T2, class C2> \
MultiMathOperand<MultiMathBinaryOperator<typename MultiMathOperand<T1>::AllowOverload, \
MultiMathOperand<MultiArrayView<N, T2, C2> >, \
detail::NAME> > \
OPNAME(T1 const & v1, MultiArrayView<N, T2, C2> const & v2) \
{ \
typedef MultiMathOperand<T1> O1; \
typedef MultiMathOperand<MultiArrayView<N, T2, C2> > O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
} \
\
template <unsigned int N, class T1, class C1, class T2> \
MultiMathOperand<MultiMathBinaryOperator<MultiMathOperand<MultiArrayView<N, T1, C1> >, \
typename MultiMathOperand<T2>::AllowOverload, \
detail::NAME> > \
OPNAME(MultiArrayView<N, T1, C1> const & v1, T2 const & v2) \
{ \
typedef MultiMathOperand<MultiArrayView<N, T1, C1> > O1; \
typedef MultiMathOperand<T2> O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
} \
\
template <unsigned int N, class T1, class T2, class C2> \
MultiMathOperand<MultiMathBinaryOperator<MultiMathOperand<T1>, \
MultiMathOperand<MultiArrayView<N, T2, C2> >, \
detail::NAME> > \
OPNAME(MultiMathOperand<T1> const & v1, MultiArrayView<N, T2, C2> const & v2) \
{ \
typedef MultiMathOperand<T1> O1; \
typedef MultiMathOperand<MultiArrayView<N, T2, C2> > O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
} \
\
template <unsigned int N, class T1, class C1, class T2> \
MultiMathOperand<MultiMathBinaryOperator<MultiMathOperand<MultiArrayView<N, T1, C1> >, \
MultiMathOperand<T2>, \
detail::NAME> > \
OPNAME(MultiArrayView<N, T1, C1> const & v1, MultiMathOperand<T2> const & v2) \
{ \
typedef MultiMathOperand<MultiArrayView<N, T1, C1> > O1; \
typedef MultiMathOperand<T2> O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
} \
\
template <class T1, class T2> \
MultiMathOperand<MultiMathBinaryOperator<MultiMathOperand<T1>, \
MultiMathOperand<T2>, \
detail::NAME> > \
OPNAME(MultiMathOperand<T1> const & v1, MultiMathOperand<T2> const & v2) \
{ \
typedef MultiMathOperand<T1> O1; \
typedef MultiMathOperand<T2> O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
} \
\
template <class T1, class T2> \
MultiMathOperand<MultiMathBinaryOperator<typename MultiMathOperand<T1>::AllowOverload, \
MultiMathOperand<T2>, \
detail::NAME> > \
OPNAME(T1 const & v1, MultiMathOperand<T2> const & v2) \
{ \
typedef MultiMathOperand<T1> O1; \
typedef MultiMathOperand<T2> O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
} \
\
template <class T1, class T2> \
MultiMathOperand<MultiMathBinaryOperator<MultiMathOperand<T1>, \
typename MultiMathOperand<T2>::AllowOverload, \
detail::NAME> > \
OPNAME(MultiMathOperand<T1> const & v1, T2 const & v2) \
{ \
typedef MultiMathOperand<T1> O1; \
typedef MultiMathOperand<T2> O2; \
typedef MultiMathBinaryOperator<O1, O2, detail::NAME> OP; \
return MultiMathOperand<OP>(OP(v1, v2)); \
}
#define VIGRA_NOTHING
#define VIGRA_COMMA ,
#define VIGRA_PROMOTE typename PromoteTraits<T1, T2>::Promote
#define VIGRA_REALPROMOTE typename PromoteTraits<typename NumericTraits<T1>::RealPromote, \
typename NumericTraits<T2>::RealPromote>::Promote
VIGRA_MULTIMATH_BINARY_OPERATOR(Plus, VIGRA_NOTHING, operator+, +, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Minus, VIGRA_NOTHING, operator-, -, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Multiplies, VIGRA_NOTHING, operator*, *, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Divides, VIGRA_NOTHING, operator/, /, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Modulo, VIGRA_NOTHING, operator%, %, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(And, VIGRA_NOTHING, operator&&, &&, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Or, VIGRA_NOTHING, operator||, ||, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Equal, VIGRA_NOTHING, operator==, ==, bool)
VIGRA_MULTIMATH_BINARY_OPERATOR(NotEqual, VIGRA_NOTHING, operator!=, !=, bool)
VIGRA_MULTIMATH_BINARY_OPERATOR(Less, VIGRA_NOTHING, operator<, <, bool)
VIGRA_MULTIMATH_BINARY_OPERATOR(LessEqual, VIGRA_NOTHING, operator<=, <=, bool)
VIGRA_MULTIMATH_BINARY_OPERATOR(Greater, VIGRA_NOTHING, operator>, >, bool)
VIGRA_MULTIMATH_BINARY_OPERATOR(GreaterEqual, VIGRA_NOTHING, operator>=, >=, bool)
VIGRA_MULTIMATH_BINARY_OPERATOR(Leftshift, VIGRA_NOTHING, operator<<, <<, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Rightshift, VIGRA_NOTHING, operator>>, >>, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(BitwiseAnd, VIGRA_NOTHING, operator&, &, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(BitwiseOr, VIGRA_NOTHING, operator|, |, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(BitwiseXor, VIGRA_NOTHING, operator^, ^, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Atan2, std::atan2, atan2, VIGRA_COMMA, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Pow, vigra::pow, pow, VIGRA_COMMA, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Fmod, std::fmod, fmod, VIGRA_COMMA, VIGRA_REALPROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Min, std::min, min, VIGRA_COMMA, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Max, std::max, max, VIGRA_COMMA, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Minimum, std::min, minimum, VIGRA_COMMA, VIGRA_PROMOTE)
VIGRA_MULTIMATH_BINARY_OPERATOR(Maximum, std::max, maximum, VIGRA_COMMA, VIGRA_PROMOTE)
#undef VIGRA_NOTHING
#undef VIGRA_COMMA
#undef VIGRA_PROMOTE
#undef VIGRA_REALPROMOTE
#undef VIGRA_MULTIMATH_BINARY_OPERATOR
namespace detail {
// We pass 'strideOrder' to the recursion in order to make sure
// that the inner loop iterates over the output's major axis.
// Of course, this does not help when the RHS arrays are ordered
// differently -- maybe it is better to find the most common order
// among all arguments (both RHS and LHS)?
//
template <unsigned int N, class Assign>
struct MultiMathExec
{
enum { LEVEL = N-1 };
template <class T, class Shape, class Expression>
static void exec(T * data, Shape const & shape, Shape const & strides,
Shape const & strideOrder, Expression const & e)
{
MultiArrayIndex axis = strideOrder[LEVEL];
for(MultiArrayIndex k=0; k<shape[axis]; ++k, data += strides[axis], e.inc(axis))
{
MultiMathExec<N-1, Assign>::exec(data, shape, strides, strideOrder, e);
}
e.reset(axis);
data -= shape[axis]*strides[axis];
}
};
template <class Assign>
struct MultiMathExec<1, Assign>
{
enum { LEVEL = 0 };
template <class T, class Shape, class Expression>
static void exec(T * data, Shape const & shape, Shape const & strides,
Shape const & strideOrder, Expression const & e)
{
MultiArrayIndex axis = strideOrder[LEVEL];
for(MultiArrayIndex k=0; k<shape[axis]; ++k, data += strides[axis], e.inc(axis))
{
Assign::assign(data, e);
}
e.reset(axis);
data -= shape[axis]*strides[axis];
}
};
#define VIGRA_MULTIMATH_ASSIGN(NAME, OP) \
struct MultiMath##NAME \
{ \
template <class T, class Expression> \
static void assign(T * data, Expression const & e) \
{ \
*data OP vigra::detail::RequiresExplicitCast<T>::cast(*e); \
} \
}; \
\
template <unsigned int N, class T, class C, class Expression> \
void NAME(MultiArrayView<N, T, C> a, MultiMathOperand<Expression> const & e) \
{ \
typename MultiArrayShape<N>::type shape(a.shape()); \
\
vigra_precondition(e.checkShape(shape), \
"multi_math: shape mismatch in expression."); \
\
MultiMathExec<N, MultiMath##NAME>::exec(a.data(), a.shape(), a.stride(), \
a.strideOrdering(), e); \
} \
\
template <unsigned int N, class T, class A, class Expression> \
void NAME##OrResize(MultiArray<N, T, A> & a, MultiMathOperand<Expression> const & e) \
{ \
typename MultiArrayShape<N>::type shape(a.shape()); \
\
vigra_precondition(e.checkShape(shape), \
"multi_math: shape mismatch in expression."); \
\
if(a.size() == 0) \
a.reshape(shape); \
\
MultiMathExec<N, MultiMath##NAME>::exec(a.data(), a.shape(), a.stride(), \
a.strideOrdering(), e); \
}
VIGRA_MULTIMATH_ASSIGN(assign, =)
VIGRA_MULTIMATH_ASSIGN(plusAssign, +=)
VIGRA_MULTIMATH_ASSIGN(minusAssign, -=)
VIGRA_MULTIMATH_ASSIGN(multiplyAssign, *=)
VIGRA_MULTIMATH_ASSIGN(divideAssign, /=)
#undef VIGRA_MULTIMATH_ASSIGN
template <unsigned int N, class Assign>
struct MultiMathReduce
{
enum { LEVEL = N-1 };
template <class T, class Shape, class Expression>
static void exec(T & t, Shape const & shape, Expression const & e)
{
for(MultiArrayIndex k=0; k<shape[LEVEL]; ++k, e.inc(LEVEL))
{
MultiMathReduce<N-1, Assign>::exec(t, shape, e);
}
e.reset(LEVEL);
}
};
template <class Assign>
struct MultiMathReduce<1, Assign>
{
enum { LEVEL = 0 };
template <class T, class Shape, class Expression>
static void exec(T & t, Shape const & shape, Expression const & e)
{
for(MultiArrayIndex k=0; k<shape[0]; ++k, e.inc(0))
{
Assign::assign(&t, e);
}
e.reset(0);
}
};
struct MultiMathReduceAll
{
template <class T, class Expression>
static void assign(T * data, Expression const & e)
{
*data = *data && (*e != NumericTraits<typename Expression::result_type>::zero());
}
};
struct MultiMathReduceAny
{
template <class T, class Expression>
static void assign(T * data, Expression const & e)
{
*data = *data || (*e != NumericTraits<typename Expression::result_type>::zero());
}
};
} // namespace detail
template <class U, class T>
U
sum(MultiMathOperand<T> const & v, U res = NumericTraits<U>::zero())
{
static const int ndim = MultiMathOperand<T>::ndim;
typename MultiArrayShape<ndim>::type shape;
v.checkShape(shape);
detail::MultiMathReduce<ndim, detail::MultiMathplusAssign>::exec(res, shape, v);
return res;
}
template <class U, unsigned int N, class T, class S>
U
sum(MultiArrayView<N, T, S> const & v, U res = NumericTraits<U>::zero())
{
return v.template sum<U>() + res;
}
template <class U, class T>
U
product(MultiMathOperand<T> const & v, U res = NumericTraits<U>::one())
{
static const int ndim = MultiMathOperand<T>::ndim;
typename MultiArrayShape<ndim>::type shape;
v.checkShape(shape);
detail::MultiMathReduce<ndim, detail::MultiMathmultiplyAssign>::exec(res, shape, v);
return res;
}
template <class U, unsigned int N, class T, class S>
U
product(MultiArrayView<N, T, S> const & v, U res = NumericTraits<U>::one())
{
return v.template product<U>() * res;
}
template <class T>
bool
all(MultiMathOperand<T> const & v)
{
static const int ndim = MultiMathOperand<T>::ndim;
typename MultiArrayShape<ndim>::type shape;
v.checkShape(shape);
bool res = true;
detail::MultiMathReduce<ndim, detail::MultiMathReduceAll>::exec(res, shape, v);
return res;
}
template <class T>
bool
any(MultiMathOperand<T> const & v)
{
static const int ndim = MultiMathOperand<T>::ndim;
typename MultiArrayShape<ndim>::type shape;
v.checkShape(shape);
bool res = false;
detail::MultiMathReduce<ndim, detail::MultiMathReduceAny>::exec(res, shape, v);
return res;
}
}} // namespace vigra::multi_math
#endif // VIGRA_MULTI_MATH_HXX
|