example for Algebraic_foundations

This commit is contained in:
Michael Hemmer 2007-01-31 20:08:19 +00:00
parent 1cf3971452
commit c8d44559fa
5 changed files with 179 additions and 0 deletions

4
.gitattributes vendored
View File

@ -8,6 +8,10 @@ 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/fraction_traits.cpp -text
Algebraic_foundations/examples/Algebraic_foundations/implicit_interoperable_dispatch.cpp -text
Algebraic_foundations/examples/Algebraic_foundations/integralize.cpp -text
Algebraic_foundations/examples/Algebraic_foundations/interoperable.cpp -text
Algebraic_kernel_GBRS/config/support/S25-MPFI -text
Algebraic_kernel_GBRS/config/support/S27-RS -text
Algebraic_kernel_GBRS/include/CGAL/Arr_poly_traits_1.h -text

View File

@ -0,0 +1,34 @@
//file: examples/Algebraic_foundations/fraction_traits.cpp
#include <CGAL/Fraction_traits.h>
#include <CGAL/IO/io.h>
#ifdef CGAL_USE_GMP
#include <CGAL/Gmpz.h>
#include <CGAL/Gmpq.h>
int main(){
typedef CGAL::Fraction_traits<CGAL::Gmpq> FT;
typedef FT::Numerator_type Numerator_type;
typedef FT::Denominator_type Denominator_type;
BOOST_STATIC_ASSERT((boost::is_same<Numerator_type,CGAL::Gmpz>::value));
BOOST_STATIC_ASSERT((boost::is_same<Denominator_type,CGAL::Gmpz>::value));
Numerator_type numerator;
Denominator_type denominator;
CGAL::Gmpq fraction(4,5);
FT::Decompose()(fraction,numerator,denominator);
CGAL::set_pretty_mode(std::cout);
std::cout << "decompose fraction: "<< std::endl;
std::cout << "fraction : " << fraction << std::endl;
std::cout << "numerator : " << numerator<< std::endl;
std::cout << "denominator: " << denominator << std::endl;
std::cout << "re-compose fraction: "<< std::endl;
fraction = FT::Compose()(numerator,denominator);
std::cout << "fraction : " << fraction << std::endl;
}
#else
int main(){ std::cout << "This examples needs GMP" << std::endl; }
#endif

View File

@ -0,0 +1,48 @@
//file: examples/Algebraic_foundations/implicit_interoperable_dispatch.cpp
#include <CGAL/Coercion_traits.h>
#include <CGAL/Quotient.h>
#include <CGAL/Sqrt_extension.h>
#include <CGAL/IO/io.h>
// this is the implementation for ExplicitInteroperable types
template <typename A, typename B>
typename CGAL::Coercion_traits<A,B>::Type
binary_function_(const A& a , const B& b, CGAL::Tag_false){
std::cout << "Call for ExplicitInteroperable types: " << std::endl;
typedef CGAL::Coercion_traits<A,B> CT;
typename CT::Cast cast;
return cast(a)*cast(b);
}
// this is the implementation for ImplicitInteroperable types
template <typename A, typename B>
typename CGAL::Coercion_traits<A,B>::Type
binary_function_(const A& a , const B& b, CGAL::Tag_true){
std::cout << "Call for ImpicitInteroperable types: " << std::endl;
return a*b;
}
// this function selects the correct implementation
template <typename A, typename B>
typename CGAL::Coercion_traits<A,B>::Type
binary_function(const A& a , const B& b){
typedef CGAL::Coercion_traits<A,B> CT;
typedef typename CT::Are_implicit_interoperable Are_implicit_interoperable;
return binary_function_(a,b,Are_implicit_interoperable());
}
int main(){
CGAL::set_pretty_mode(std::cout);
// Function call for ImplicitInteroperable types
std::cout<< binary_function(double(3), int(5)) << std::endl;
// Function call for ExplicitInteroperable types
CGAL::Quotient<int> rational(1,3); // == 1/3
CGAL::Sqrt_extension<int,int> extension(1,2,3); // == 1+2*sqrt(3)
CGAL::Sqrt_extension<CGAL::Quotient<int>,int> result = binary_function(rational, extension);
std::cout<< result << std::endl;
return 0;
}

View File

@ -0,0 +1,65 @@
//file: examples/Algebraic_foundations/integralize.cpp
#include <CGAL/Fraction_traits.h>
#include <CGAL/IO/io.h>
#include <vector>
template <class Fraction>
std::vector<typename CGAL::Fraction_traits<Fraction>::Numerator_type >
integralize(
const std::vector<Fraction>& vec,
typename CGAL::Fraction_traits<Fraction>::Denominator_type& d
) {
typedef CGAL::Fraction_traits<Fraction> FT;
typedef typename FT::Numerator_type Numerator_type;
typedef typename FT::Denominator_type Denominator_type;
typename FT::Decompose decompose;
std::vector<Numerator_type> num(vec.size());
std::vector<Denominator_type> den(vec.size());
// decompose each coefficient into integral part and denominator
for (unsigned int i = 0; i < vec.size(); i++) {
decompose(vec[i], num[i], den[i]);
}
// compute 'least' common multiple of all denominator
// We would like to use gcd, so let's think of Common_factor as gcd.
typename FT::Common_factor gcd;
d = 1;
for (unsigned int i = 0; i < vec.size(); i++) {
d *= CGAL::integral_division(den[i], gcd(d, den[i]));
}
// expand each (numerator, denominator) pair to common denominator
for (unsigned int i = 0; i < vec.size(); i++) {
// For simplicity ImplicitInteroperability is expected in this example
num[i] *= CGAL::integral_division(d, den[i]);
}
return num;
}
#ifdef CGAL_USE_GMP
#include <CGAL/Gmpz.h>
#include <CGAL/Gmpq.h>
int main(){
std::vector<CGAL::Gmpq> vec(3);
vec[0]=CGAL::Gmpq(1,4);
vec[1]=CGAL::Gmpq(1,6);
vec[2]=CGAL::Gmpq(1,10);
std::cout<< "compute an integralized vector" << std::endl;
std::cout<<"input vector: ["
<< vec[0] << "," << vec[1] << "," << vec[2] << "]" << std::endl;
CGAL::Gmpz d;
std::vector<CGAL::Gmpz> integral_vec = integralize(vec,d);
std::cout<<"output vector: ["
<< integral_vec[0] << ","
<< integral_vec[1] << ","
<< integral_vec[2] << "]" << std::endl;
std::cout<<"denominator : "<< d <<std::endl;
}
#else
int main(){ std::cout << "This examples needs GMP" << std::endl; }
#endif

View File

@ -0,0 +1,28 @@
//file: examples/Algebraic_foundations/interoperable.cpp
#include <CGAL/Coercion_traits.h>
#include <CGAL/IO/io.h>
// this is an implementation for ExplicitInteroperable types
// the result type is determined via Coercion_traits<A,B>
template <typename A, typename B>
typename CGAL::Coercion_traits<A,B>::Type
binary_function(const A& a , const B& b){
typedef CGAL::Coercion_traits<A,B> CT;
// check for explicit interoperability
BOOST_STATIC_ASSERT((CT::Are_explicit_interoperable::value));
// CT::Cast is used to to convert both types into the coercion type
typename CT::Cast cast;
// all operations are performed in the coercion type
return cast(a)*cast(b);
}
int main(){
// Function call for the interoperable types
std::cout<< binary_function(double(3), int(5)) << std::endl;
// Note that Coercion_traits is symmetric
std::cout<< binary_function(int(3), double(5)) << std::endl;
return 0;
}