added example algebraic_structure_dispatch.cpp

shows a naive implementation for unit_part
This commit is contained in:
Michael Hemmer 2007-03-15 09:06:16 +00:00
parent cec05315eb
commit 397f22e2ae
3 changed files with 57 additions and 0 deletions

1
.gitattributes vendored
View File

@ -8,6 +8,7 @@ Algebraic_foundations/doc_tex/Algebraic_foundations/fractions.tex -text
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_ref/Fraction.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
Algebraic_foundations/examples/Algebraic_foundations/integralize.cpp -text

View File

@ -109,6 +109,12 @@ arithmetic. We expect that \ccc{Is_numerical_sensitive} is used for dispatching
of algorithms, while \ccc{Is_exact} is useful to enable assertions that can be
check for exact types only.
The following example illustrates a dispatch for \ccc{Field}s using overloaded
functions.
\ccIncludeExampleCode{Algebraic_foundations/algebraic_structure_dispatch.cpp}
\ccIgnore{
\begin{tabular}{|l|l|}
Algebraic Structure Concept & provided functions \\
@ -139,6 +145,10 @@ Algebraic Structure Concept & provided functions \\
}
\ccIgnore {
\subsection{Algebraic Structure Concepts}

View File

@ -0,0 +1,46 @@
#include <CGAL/basic.h>
#include <CGAL/IO/io.h>
#include <CGAL/Algebraic_structure_traits.h>
template< typename NT > NT unit_part(const NT& x);
template< typename NT > NT unit_part_(const NT& x, CGAL::Field_tag);
template< typename NT > NT unit_part_(const NT& x, CGAL::Integral_domain_without_division_tag);
template< typename NT >
NT unit_part(const NT& x){
// the unit part of 0 is defined as 1.
if (x == 0 ) return NT(1);
typedef CGAL::Algebraic_structure_traits<NT> AST;
typedef typename AST::Algebraic_category Algebraic_category;
return unit_part_(x,Algebraic_category());
}
template< typename NT >
NT unit_part_(const NT& x, CGAL::Integral_domain_without_division_tag){
// For many other types the only units are just -1 and +1.
return NT(int(CGAL::sign(x)));
}
template< typename NT >
NT unit_part_(const NT& x, CGAL::Field_tag){
// For Fields every x != 0 is a unit.
// Therefore, every x != 0 is its own unit part.
return x;
}
int main(){
// Function call for a model of EuclideanRing, i.e. int.
std::cout<< "int: unit_part(-3 ): " << unit_part(-3 ) << std::endl;
// Function call for a model of FieldWithSqrt, i.e. double
std::cout<< "double: unit_part(-3.0): " << unit_part(-3.0) << std::endl;
return 0;
}
// Note that this is just an example
// This implementation for unit part won't work for some types, e.g.,
// types that are not RealEmbeddable or types representing structures that have
// more units than just -1 and +1. (e.g. MP_Float representing Z[1/2])
// From there Algebraic_structure_traits provides an functor Unit_part.