diff --git a/.gitattributes b/.gitattributes index b76e631f2f2..1dccc3bdd4a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -68,7 +68,9 @@ Algebraic_foundations/doc_tex/Algebraic_foundations/interoperability.tex -text Algebraic_foundations/doc_tex/Algebraic_foundations/real_embeddable.tex -text Algebraic_foundations/doc_tex/Algebraic_foundations/real_number_types.tex -text Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits_Divides.tex -text +Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits_Inverse.tex -text Algebraic_foundations/doc_tex/Algebraic_foundations_ref/Fraction.tex -text +Algebraic_foundations/doc_tex/Algebraic_foundations_ref/inverse.tex -text Algebraic_foundations/examples/Algebraic_foundations/algebraic_structure_dispatch.cpp -text Algebraic_foundations/examples/Algebraic_foundations/fraction_traits.cpp -text Algebraic_foundations/examples/Algebraic_foundations/implicit_interoperable_dispatch.cpp -text diff --git a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits.tex b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits.tex index dbb13dca161..80bb558391f 100644 --- a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits.tex +++ b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits.tex @@ -132,6 +132,11 @@ A model of \ccc{AlgebraicStructureTraits::DivMod}.\\ Required by the concept \ccc{EuclideanRing}. } +\ccNestedType{Inverse}{ +A model of \ccc{AlgebraicStructureTraits::Inverse}.\\ +Required by the concept \ccc{Field}. +} + \ccNestedType{Sqrt}{ A model of \ccc{AlgebraicStructureTraits::Sqrt}.\\ Required by the concept \ccc{FieldWithSqrt}. diff --git a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits_Inverse.tex b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits_Inverse.tex new file mode 100644 index 00000000000..71024b6d0aa --- /dev/null +++ b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/AlgebraicStructureTraits_Inverse.tex @@ -0,0 +1,32 @@ +\begin{ccRefFunctionObjectConcept}{AlgebraicStructureTraits::Inverse} + +\ccDefinition + +\ccc{AdaptableUnaryFunction} providing the inverse element with +respect to multiplication of a \ccc{Field}. + +\ccRefines + +\ccc{AdaptableUnaryFunction} + +\ccTypes +\ccNestedType{result_type} + { Is \ccc{AlgebraicStructureTraits::Type}.} +\ccNestedType{argument_type} + { Is \ccc{AlgebraicStructureTraits::Type}.} + +\ccOperations +\ccThree{xxxxxxxxxxx}{xxxxxxxxxxx}{} +\ccCreationVariable{inverse} +\ccMethod{result_type operator()(argument_type x) const;} + { returns the inverse element of $x$ with respect to multiplication. + \ccPrecond $x \neq 0$ + } + +%\ccHasModels + +\ccSeeAlso + +\ccRefIdfierPage{AlgebraicStructureTraits} + +\end{ccRefFunctionObjectConcept} diff --git a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/Field.tex b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/Field.tex index ece00763c30..64e92bceaef 100644 --- a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/Field.tex +++ b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/Field.tex @@ -12,8 +12,9 @@ operators / and /=. Moreover, \ccc{CGAL::Algebraic_structure_traits< Field >} is a model of \ccc{AlgebraicStructureTraits} providing:\\ -- \ccc{CGAL::Algebraic_structure_traits< Field >::Algebraic_type} derived -from \ccc{Field_tag} \\ +- \ccc{CGAL::Algebraic_structure_traits< Field >::Algebraic_type} derived from \ccc{Field_tag} \\ +- \ccc{CGAL::Algebraic_structure_traits< FieldWithSqrt >::Inverse}\\ + \ccRefines \ccc{IntegralDomain} diff --git a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/intro.tex b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/intro.tex index 9955edeacbe..21e6e304f74 100644 --- a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/intro.tex +++ b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/intro.tex @@ -34,6 +34,7 @@ \ccRefConceptPage{AlgebraicStructureTraits::DivMod}\\ \ccRefConceptPage{AlgebraicStructureTraits::Div}\\ \ccRefConceptPage{AlgebraicStructureTraits::Mod}\\ +\ccRefConceptPage{AlgebraicStructureTraits::Inverse}\\ \ccRefConceptPage{AlgebraicStructureTraits::Sqrt}\\ \ccRefConceptPage{AlgebraicStructureTraits::IsSquare}\\ \ccRefConceptPage{AlgebraicStructureTraits::KthRoot}\\ @@ -61,6 +62,7 @@ \ccRefIdfierPage{CGAL::div_mod}\\ \ccRefIdfierPage{CGAL::div}\\ \ccRefIdfierPage{CGAL::mod}\\ +\ccRefIdfierPage{CGAL::inverse}\\ \ccRefIdfierPage{CGAL::sqrt}\\ \ccRefIdfierPage{CGAL::kth_root}\\ \ccRefIdfierPage{CGAL::root_of}\\ diff --git a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/inverse.tex b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/inverse.tex new file mode 100644 index 00000000000..cf233c2abd6 --- /dev/null +++ b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/inverse.tex @@ -0,0 +1,22 @@ +\begin{ccRefFunction}{inverse} + +\ccDefinition + +The function \ccRefName\ returns the inverse element with respect to multiplication. + +The function is guaranteed to be well defined in case the argument type +is a model of the \ccc{Field} concept. + +\ccInclude{CGAL/number_utils.h} + +\ccFunction{template NT inverse(const NT& x);} +{\ccPrecond $x \neq 0$} + + + +\ccSeeAlso + +\ccRefConceptPage{Field}\\ +\ccRefConceptPage{AlgebraicStructureTraits::Inverse}\\ + +\end{ccRefFunction} diff --git a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/main.tex b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/main.tex index 21bc34646a2..62036e8c1b3 100644 --- a/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/main.tex +++ b/Algebraic_foundations/doc_tex/Algebraic_foundations_ref/main.tex @@ -35,6 +35,7 @@ \input{Algebraic_foundations_ref/AlgebraicStructureTraits_DivMod.tex} \input{Algebraic_foundations_ref/AlgebraicStructureTraits_Div.tex} \input{Algebraic_foundations_ref/AlgebraicStructureTraits_Mod.tex} +\input{Algebraic_foundations_ref/AlgebraicStructureTraits_Inverse.tex} \input{Algebraic_foundations_ref/AlgebraicStructureTraits_Sqrt.tex} \input{Algebraic_foundations_ref/AlgebraicStructureTraits_KthRoot.tex} \input{Algebraic_foundations_ref/AlgebraicStructureTraits_RootOf.tex} @@ -63,6 +64,7 @@ \input{Algebraic_foundations_ref/div_mod.tex} \input{Algebraic_foundations_ref/div.tex} \input{Algebraic_foundations_ref/mod.tex} +\input{Algebraic_foundations_ref/inverse.tex} \input{Algebraic_foundations_ref/sqrt.tex} \input{Algebraic_foundations_ref/kth_root.tex} \input{Algebraic_foundations_ref/root_of.tex} diff --git a/Algebraic_foundations/include/CGAL/Algebraic_structure_traits.h b/Algebraic_foundations/include/CGAL/Algebraic_structure_traits.h index 8ad226de103..c7f663e9e99 100644 --- a/Algebraic_foundations/include/CGAL/Algebraic_structure_traits.h +++ b/Algebraic_foundations/include/CGAL/Algebraic_structure_traits.h @@ -85,6 +85,7 @@ class Algebraic_structure_traits { typedef Null_functor Kth_root; typedef Null_functor Root_of; typedef Null_functor Divides; + typedef Null_functor Inverse; }; // The algebraic structure traits base class @@ -462,6 +463,17 @@ class Algebraic_structure_traits_base< Type_, Field_tag > } CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR( Type ) }; + + // maps to \c 1/x by default. + class Inverse + : public std::unary_function< Type, Type > { + public: + Type operator()( const Type& x ) const { + static const Type& one = Type(1); + return one/x; + } + }; + // Default implementation of Divides functor for Field: // returns always true diff --git a/Algebraic_foundations/include/CGAL/Test/_test_algebraic_structure.h b/Algebraic_foundations/include/CGAL/Test/_test_algebraic_structure.h index d3b147152b3..3ac283bdd34 100644 --- a/Algebraic_foundations/include/CGAL/Test/_test_algebraic_structure.h +++ b/Algebraic_foundations/include/CGAL/Test/_test_algebraic_structure.h @@ -358,6 +358,11 @@ void test_algebraic_structure_intern( const CGAL::Field_tag& ) { assert(!ie || tmp == AS(5)/AS(2)); assert(CGAL_NTS divides(AS(5),AS(2),tmp)); assert(!ie || tmp == AS(2)/AS(5)); + + typename AST::Inverse inverse; + assert(AS(1)/AS(2) == inverse(AS(2))); + assert(AS(1)/AS(2) == CGAL::inverse(AS(2))); + } diff --git a/Algebraic_foundations/include/CGAL/number_utils.h b/Algebraic_foundations/include/CGAL/number_utils.h index 3181c36b879..09775701734 100644 --- a/Algebraic_foundations/include/CGAL/number_utils.h +++ b/Algebraic_foundations/include/CGAL/number_utils.h @@ -74,6 +74,16 @@ square( const AS& x ) { typename Algebraic_structure_traits< AS >::Square square; return square( x ); } + + +template< class AS > +inline +typename Algebraic_structure_traits< AS >::Inverse::result_type +inverse( const AS& x ) { + typename Algebraic_structure_traits< AS >::Inverse inverse; + return inverse( x ); +} + template< class AS > inline typename Algebraic_structure_traits::Is_one::result_type