File: ellipsoid_for_maple.cpp

package info (click to toggle)
cgal 4.13-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 101,504 kB
  • sloc: cpp: 703,154; ansic: 163,044; sh: 674; fortran: 616; python: 411; makefile: 115
file content (88 lines) | stat: -rw-r--r-- 2,688 bytes parent folder | download | duplicates (8)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Usage: ./maple_example > maple.text
// Then enter in Maple 'read "maple.text";'.

#include <CGAL/Cartesian_d.h>
#include <CGAL/MP_Float.h>
#include <CGAL/point_generators_d.h>
#include <CGAL/Approximate_min_ellipsoid_d.h>
#include <CGAL/Approximate_min_ellipsoid_d_traits_d.h>

#include <vector>
#include <iostream>
#include <iomanip>


typedef CGAL::Cartesian_d<double>                              Kernel;
typedef CGAL::MP_Float                                         ET;
typedef CGAL::Approximate_min_ellipsoid_d_traits_d<Kernel, ET> Traits;
typedef Traits::Point                                          Point;
typedef std::vector<Point>                                     Point_list;
typedef CGAL::Approximate_min_ellipsoid_d<Traits>              AME;

int main()
{
  const int      n = 100;                 // number of points
  const int      d = 2;                   // dimension
  const double eps = 0.01;                // approximation ratio is (1+eps)

  // create a set of random points:
  Point_list P;
  CGAL::Random_points_in_cube_d<Point> rpg(d,1.0);
  for (int i = 0; i < n; ++i) {
    P.push_back(*rpg);
    ++rpg;
  }

  // compute approximation:
  Traits traits;
  AME mel(eps, P.begin(), P.end(), traits);

  // output for Maple:
  if (mel.is_full_dimensional() && d == 2) {

    const double alpha = (1+mel.achieved_epsilon())*(d+1);

    // output points:
    using std::cout;
    cout << "restart;\n"
         << "with(LinearAlgebra):\n"
         << "with(plottools):\n"
         << "n:= " << n << ":\n"
         << "P:= Matrix(" << d << "," << n << "):\n";

    for (int i=0; i<n; ++i)
      for (int j=0; j<d; ++j)
        cout << "P[" << j+1 << "," << i+1 << "] := "
             << std::setiosflags(std::ios::scientific)
             << std::setprecision(20) << P[i][j] << ":\n";
    cout << "\n";

    // output defining equation:
    cout << "Mp:= Matrix([\n";
    for (int i=0; i<d; ++i) {
      cout << "  [";
      for (int j=0; j<d; ++j) {
        cout << mel.defining_matrix(i,j)/alpha;
        if (j<d-1)
          cout << ",";
      }
      cout << "]";
      if (i<d-1)
        cout << ",";
      cout << "\n";
    }
    cout << "]);\n" << "mp:= Vector([";
    for (int i=0; i<d; ++i) {
      cout << mel.defining_vector(i)/alpha;
      if (i<d-1)
        cout << ",";
    }
    cout << "]);\n"
         << "eta:= " << (mel.defining_scalar()/alpha-1.0) << ";\n"
         << "v:= Vector([x,y]):\n"
         << "e:= Transpose(v).Mp.v+Transpose(v).mp+eta;\n"
         << "plots[display]({seq(point([P[1,i],P[2,i]]),i=1..n),\n"
         << " plots[implicitplot](e,x=-5..5,y=-5..5,numpoints=10000)},\n"
         << " scaling=CONSTRAINED);\n";
  }
}