You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.8 KiB

2 months ago
  1. Operators {#polynomials_operators}
  2. =====
  3. The classes used to build polynomials are (almost) fully compatible with respect to the following operators, that means that any two objects of these types can be combined if there is a directed path between them within the class hierarchy.
  4. The exception are shown and explained below. All the operators have the usual meaning.
  5. - Comparison operators
  6. - `operator==(lhs, rhs)`
  7. - `operator!=(lhs, rhs)`
  8. - `operator<(lhs, rhs)`
  9. - `operator<=(lhs, rhs)`
  10. - `operator>(lhs, rhs)`
  11. - `operator>=(lhs, rhs)`
  12. - Arithmetic operators
  13. - `operator+(lhs, rhs)`
  14. - `operator+=(lhs, rhs)`
  15. - `operator-(lhs, rhs)`
  16. - `operator-(rhs)`
  17. - `operator-=(lhs, rhs)`
  18. - `operator*(lhs, rhs)`
  19. - `operator*=(lhs, rhs)`
  20. ## Comparison operators
  21. All of these operators are defined for all combination of types.
  22. We use the following ordering:
  23. - For two variables `x` and `y`, `x < y` if the id of `x` is smaller then the id of `y`. The id is generated automatically by the VariablePool.
  24. - For two monomials `a` and `b`, we use a lexicographical ordering with total degree, that is `a < b` if
  25. - the total degree of `a` is smaller than the total degree of `b`, or
  26. - the total degrees are the same and
  27. - the exponent of some variable `v` in `a` is greater than in `b` and
  28. - the exponents of all variables smaller than `v` are the same in `a` and in `b`.
  29. - The intuition is that the monomials are considered as a sorted product of plain variables.
  30. - For two terms `a` and `b`, `a < b` if
  31. - the monomial of `a` is smaller than the monomial of `b`, or
  32. - the monomials of `a` and `b` are the same and the coefficient of `a` is smaller than the coefficient of `b`.
  33. - For two polynomials `a` and `b`, we use a lexicographical ordering, that is `a < b` if
  34. - `term(a,i) < term(b,i)` and
  35. - `term(a,j) = term(b,j)` for all `j=0, ..., i-1`, where `term(a,0)` is the leading term of `a`, that is the largest term with respect to the term ordering.
  36. ## Arithmetic operators
  37. We now give a table for all (classes of) operators with the result type or a reason why it is not implemented for any combination of these types.
  38. ### `operator+(lhs, rhs)`, `operator-(lhs, rhs)`
  39. + | C | V | M | T | MP
  40. -- | -- | -- | -- | -- | --
  41. C | C | MP | MP | MP | MP
  42. V | MP | 1) | 1) | MP | MP
  43. M | MP | 1) | 1) | MP | MP
  44. T | MP | MP | MP | MP | MP
  45. MP | MP | MP | MP | MP | MP
  46. ### `operator-(lhs)` (unary minus)
  47. - | C | V | M | T | MP
  48. -- | -- | -- | -- | -- | --
  49. - | C | 1) | 1) | T | MP
  50. ### operator*(lhs, rhs)
  51. * | C | V | M | T | MP
  52. -- | -- | -- | -- | -- | --
  53. C | C | T | T | T | MP
  54. V | T | M | M | T | MP
  55. M | T | M | M | T | MP
  56. T | T | T | T | T | MP
  57. MP | MP | MP | MP | MP | MP
  58. ### `operator+=(rhs)`, `operator-=(rhs)`
  59. += | C | V | M | T | MP
  60. -- | -- | -- | -- | -- | --
  61. C | C | 2) | 2) | 2) | 2)
  62. V | 2) | 2) | 2) | 2) | 2)
  63. M | 2) | 2) | 2) | 2) | 2)
  64. T | 2) | 2) | 2) | 2) | 2)
  65. MP | MP | MP | MP | MP | MP
  66. ### `operator*=(rhs)`
  67. *= | C | V | M | T | MP
  68. -- | -- | -- | -- | -- | --
  69. C | C | 3) | 3) | 3) | 3)
  70. V | 3) | 3) | 3) | 3) | 3)
  71. M | 3) | M | M | 3) | 3)
  72. T | T | T | T | T | 3)
  73. MP | MP | MP | MP | MP | MP
  74. -# A coefficient type is needed to construct the desired result type, but none can be extracted from the argument types.
  75. -# The type of the left hand side can not represent sums of these objects.
  76. -# The type of the left hand side can not represent products of these objects.
  77. ## UnivariatePolynomial operators
  78. ## Implementation
  79. We follow a few rules when implementing these operators:
  80. - Of the comparison operators, only `operator==` and `operator<` contain a real implementation. The others are implemented like this:
  81. - `operator!=(lhs, rhs)`: `!(lhs == rhs)`
  82. - `operator<=(lhs, rhs)`: `!(rhs < lhs)`
  83. - `operator>(lhs, rhs)`: `rhs < lhs`
  84. - `operator>=(lhs, rhs)`: `rhs <= lhs`
  85. - Of all `operator==`, only those where `lhs` is the most general type contain a real implementation. The others are implemented like this:
  86. - `operator==(lhs, rhs)`: `rhs == lhs`
  87. - They are ordered like in the list above.
  88. - Operators are implemented in the file of the most general type involved (either an argument or the return type).
  89. - Operators are not implemented as friend methods. Those are usually only found by the compiler due to ADL, but as we need to declare `operator+(Term, Term) -> MultivariatePolynomial` next to the MultivariatePolynomial, this will not work. If a friend declaration is necessary, it will be done as a forward declaration.
  90. - Overloaded versions of the same operator are ordered in decreasing lexicographical order, like in this example:
  91. - `operator(Term,Term)`
  92. - `operator(Term,Monomial)`
  93. - `operator(Term,Variable)`
  94. - `operator(Term,Coefficient)`
  95. - `operator(Monomial,Term)`
  96. - `operator(Variable,Term)`
  97. - `operator(Coefficient,Term)`
  98. - Other versions are below those.
  99. ## Testing the operators
  100. There are two stages for testing these operators: a syntactical check that these operators exist and have the correct signature and a semantical check that they actually work as expected.
  101. ### Syntactical checks
  102. The syntactical check for all operators specified here is done in `tests/core/Test_Operators.cpp`.
  103. We use `boost::concept_check` to check the existence of the operators. There are the following concepts:
  104. - `Comparison`: Checks for all comparison operators. (`==`, `!=`, `<`, `<=`, `>`, `>=`)
  105. - `Addition`: Checks for out-of-place addition operators. (`+`, `-`)
  106. - `UnaryMinus`: Checks for unary minus operators. (`-`)
  107. - `Multiplication`: Checks for out-of-place multiplication operators. (`*`)
  108. - `InplaceAddition`: Checks for all in-place addition operators. (`+=`, `-=`)
  109. - `InplaceMultiplication`: Checks for all in-place multiplication operators. (`*=`)
  110. ### Semantical checks
  111. Semantical checking is done within the test for each class.