2
/******************************************************************************
4
* DESCRIPTION: balls with center in C and radius in R (= norm_type (C))
5
* COPYRIGHT : (C) 2006 Joris van der Hoeven
6
*******************************************************************************
7
* This software falls under the GNU general public license version 3 or later.
8
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10
******************************************************************************/
14
#include "properties.hpp"
15
#include "operators.hpp"
16
#define TMPL template<typename C>
17
#define BINARY_TMPL template<typename C1,typename C2>
18
#define R typename properties<C>::norm_type
19
#define M typename binary_properties<C1,C2>::product_type
21
/******************************************************************************
23
******************************************************************************/
31
inline ball (C c=0, R r=0): cen (c), rad (r) {}
34
TMPL inline C center (const ball<C>& b) { return b.cen; }
35
TMPL inline R radius (const ball<C>& b) { return b.rad; }
36
TMPL inline R upper (const ball<C>& b) { return norm (b.cen) + b.rad; }
37
TMPL inline R lower (const ball<C>& b) { return norm (b.cen) - b.rad; }
39
TMPL inline tree as_tree (const ball<C>& b) {
40
return compound ("ball", as_tree (center (b)), as_tree (radius (b))); }
41
TMPL inline ostream& operator << (ostream& out, const ball<C>& b) {
42
return out << as_math_string (as_tree (b)); }
45
class properties<ball<C> > {
47
typedef ball<typename properties<C>::scalar_type> scalar_type;
48
typedef typename properties<C>::norm_type norm_type;
49
typedef typename properties<C>::index_type index_type;
50
static inline tree index_name (index_type i) {
51
return properties<C>::index_name (i); }
52
static inline scalar_type access (ball<C> b, index_type var) {
53
return scalar_type (properties<C>::access (center (b), var), radius (b)); }
57
class binary_properties<ball<C1>,ball<C2> > {
59
typedef ball<M > product_type;
62
/******************************************************************************
63
* Basic ball arithmetic
64
******************************************************************************/
67
operator - (const ball<C>& b) {
68
return ball<C> (-center (b), radius (b));
72
operator + (const ball<C>& b1, const ball<C>& b2) {
73
return ball<C> (center (b1) + center (b2), radius (b1) + radius (b2));
77
operator - (const ball<C>& b1, const ball<C>& b2) {
78
return ball<C> (center (b1) - center (b2), radius (b1) + radius (b2));
82
operator * (const ball<C1>& b1, const ball<C2>& b2) {
83
return ball<M > (center (b1) * center (b2),
84
norm (center (b1)) * radius (b2) +
85
radius (b1) * norm (center (b2)) +
86
radius (b1) * radius (b2));
90
invert (const ball<C>& b) {
91
return ball<C> (invert (center (b)), radius (b) / square (lower (b)));
95
operator / (const ball<C1>& b1, const ball<C2>& b2) {
96
return b1 * invert (b2);
99
/******************************************************************************
101
******************************************************************************/
104
sqrt (const ball<C>& b) {
105
return ball<C> (sqrt (center (b)), radius (b) / (2 * square (loer (b))));
109
exp (const ball<C>& b) {
110
return ball<C> (exp (center (b)), exp (upper (b)) * radius (b));
114
log (const ball<C>& b) {
115
return ball<C> (log (center (b)), radius (b) / lower (b));
119
pow (const ball<C>& b1, const ball<C>& b2) {
120
return exp (b2 * log (b1));
124
cos (const ball<C>& z) {
125
if (radius (z) >= R (3.14159))
126
return ball<C> (C(0), R(1));
128
R u1= sin (lower (z));
129
R u2= sin (upper (z));
131
return ball<C> (cos (center (z)), radius (z));
132
return ball<C> (cos (center (z)),
133
max (norm (u1), norm (u2)) * radius (z));
138
sin (const ball<C>& z) {
139
if (radius (z) >= R (3.14159))
140
return ball<C> (sin (center (z)), radius (z));
142
R u1= cos (lower (z));
143
R u2= cos (upper (z));
145
return ball<C> (sin (center (z)), radius (z));
146
return ball<C> (sin (center (z)),
147
max (norm (u1), norm (u2)) * radius (z));
152
tan (const ball<C>& b) {
153
return sin (b) / cos (b);
156
/******************************************************************************
158
******************************************************************************/
161
norm (const ball<C>& b) {
162
return ball<R> (norm (center (b)), radius (b));
169
#endif // defined BALL_H