1 /*
2  * Routines for generating anything randomly
3  *
4  * Authors:
5  *      Marco Cecchetti <mrcekets at gmail.com>
6  *
7  * Copyright 2008  authors
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it either under the terms of the GNU Lesser General Public
11  * License version 2.1 as published by the Free Software Foundation
12  * (the "LGPL") or, at your option, under the terms of the Mozilla
13  * Public License Version 1.1 (the "MPL"). If you do not alter this
14  * notice, a recipient may use your version of this file under either
15  * the MPL or the LGPL.
16  *
17  * You should have received a copy of the LGPL along with this library
18  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  * You should have received a copy of the MPL along with this library
21  * in the file COPYING-MPL-1.1
22  *
23  * The contents of this file are subject to the Mozilla Public License
24  * Version 1.1 (the "License"); you may not use this file except in
25  * compliance with the License. You may obtain a copy of the License at
26  * http://www.mozilla.org/MPL/
27  *
28  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
29  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
30  * the specific language governing rights and limitations.
31  */
32 
33 #ifndef _GEOM_SL_PICK_H_
34 #define _GEOM_SL_PICK_H_
35 
36 
37 #include <2geom/symbolic/multipoly.h>
38 #include <2geom/symbolic/matrix.h>
39 
40 inline
pick_uint(size_t max)41 size_t pick_uint(size_t max)
42 {
43     return (std::rand() % (max+1));
44 }
45 
46 inline
pick_int(size_t max)47 int pick_int(size_t max)
48 {
49     int s = pick_uint(2);
50     if (s == 0) s = -1;
51     return s * (std::rand() % (max+1));
52 }
53 
54 inline
pick_multi_index(size_t N,size_t max)55 Geom::SL::multi_index_type pick_multi_index(size_t N, size_t max)
56 {
57     Geom::SL::multi_index_type I(N);
58     for (size_t i = 0; i < I.size(); ++i)
59         I[i] = pick_uint(max);
60     return I;
61 }
62 
63 template <size_t N>
64 inline
65 typename Geom::SL::mvpoly<N, double>::type
pick_polyN(size_t d,size_t m)66 pick_polyN(size_t d, size_t m)
67 {
68     typename Geom::SL::mvpoly<N, double>::type  p;
69     size_t d0 = pick_uint(d);
70     for (size_t i = 0; i <= d0; ++i)
71     {
72         p.coefficient(i, pick_polyN<N-1>(d, m));
73     }
74     return p;
75 }
76 
77 template <>
78 inline
79 double pick_polyN<0>(size_t /*d*/, size_t m)
80 {
81     return pick_int(m);
82 }
83 
84 
85 template <size_t N>
86 inline
87 typename Geom::SL::mvpoly<N, double>::type
pick_poly_max(size_t d,size_t m)88 pick_poly_max(size_t d, size_t m)
89 {
90     typename Geom::SL::mvpoly<N, double>::type  p;
91     for (size_t i = 0; i <= d; ++i)
92     {
93         p.coefficient(i, pick_poly_max<N-1>(d-i, m));
94     }
95     return p;
96 }
97 
98 template <>
99 inline
100 double pick_poly_max<0>(size_t /*d*/, size_t m)
101 {
102     return pick_int(m);
103 }
104 
105 
106 template <size_t N>
107 inline
108 Geom::SL::MultiPoly<N, double>
pick_multipoly(size_t d,size_t m)109 pick_multipoly(size_t d, size_t m)
110 {
111     return Geom::SL::MultiPoly<N, double>(pick_polyN<N>(d, m));
112 }
113 
114 template <size_t N>
115 inline
116 Geom::SL::MultiPoly<N, double>
pick_multipoly_max(size_t d,size_t m)117 pick_multipoly_max(size_t d, size_t m)
118 {
119     return Geom::SL::MultiPoly<N, double>(pick_poly_max<N>(d, m));
120 }
121 
122 
123 
124 inline
125 Geom::SL::Matrix< Geom::SL::MultiPoly<2, double> >
pick_matrix(size_t n,size_t d,size_t m)126 pick_matrix(size_t n, size_t d, size_t m)
127 {
128     Geom::SL::Matrix< Geom::SL::MultiPoly<2, double> >  M(n, n);
129     for (size_t i = 0; i < n; ++i)
130     {
131         for (size_t j = 0; j < n; ++j)
132         {
133             M(i,j) = pick_multipoly_max<2>(d, m);
134         }
135     }
136     return M;
137 }
138 
139 
140 inline
141 Geom::SL::Matrix< Geom::SL::MultiPoly<2, double> >
pick_symmetric_matrix(size_t n,size_t d,size_t m)142 pick_symmetric_matrix(size_t n, size_t d, size_t m)
143 {
144     Geom::SL::Matrix< Geom::SL::MultiPoly<2, double> > M(n, n);
145     for (size_t i = 0; i < n; ++i)
146     {
147         for (size_t j = 0; j < i; ++j)
148         {
149             M(i,j) = M(j,i) = pick_multipoly_max<2>(d, m);
150         }
151     }
152     for (size_t i = 0; i < n; ++i)
153     {
154         M(i,i) = pick_multipoly_max<2>(d, m);
155     }
156     return M;
157 }
158 
159 
160 #endif // _GEOM_SL_PICK_H_
161 
162 
163 /*
164   Local Variables:
165   mode:c++
166   c-file-style:"stroustrup"
167   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
168   indent-tabs-mode:nil
169   fill-column:99
170   End:
171 */
172 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
173