File: integralize.cpp

package info (click to toggle)
cgal 4.5-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 69,700 kB
  • ctags: 118,537
  • sloc: cpp: 571,870; ansic: 110,997; sh: 725; python: 92; makefile: 87
file content (64 lines) | stat: -rw-r--r-- 2,145 bytes parent folder | download | duplicates (6)
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
#include <CGAL/basic.h>
#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