Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
basic.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file
11 /// @brief
12 /// @author
13 
14 
15 // Rosetta Headers
16 #include <basic/basic.hh>
17 #include <ObjexxFCL/Fmath.hh> // for mod
18 #include <cassert> // for assert
19 #include <cmath> // for fabs, sqrt
20 
21 // Numeric headers
22 #include <numeric/constants.hh> // for pi_2
23 
24 
25 //Auto using namespaces
26 namespace ObjexxFCL { } using namespace ObjexxFCL; // AUTO USING NS
27 //Auto using namespaces end
28 
29 
30 // C++ Headers
31 
32 namespace basic {
33 
34 
35 // util_basic.cc - general utility functions that don't fit into any
36 // other util*.cc files
37 
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 ///
41 /// @brief calculates quadratic polynomial solutions
42 ///
43 /// @details
44 ///
45 /// solves for solutions of x in the polynomial: a*x^2+b*x+c=0
46 ///
47 /// @param[in] a - in - x^2 term
48 /// @param[in] b - in - x term
49 /// @param[in] c - in - constant term
50 /// @param[out] n1 - out - one solution
51 /// @param[out] n2 - out - another solution
52 ///
53 /// @remarks courtesy of Jerry Tsai
54 ///
55 /// @references
56 ///
57 /// @author ctsa 8=19-03
58 ///
59 /////////////////////////////////////////////////////////////////////////////////
60 void
62  double a,
63  double b,
64  double c,
65  double & n1,
66  double & n2
67 )
68 {
69  //cj
70 
71  double bsq = b*b;
72  double ac4 = 4*a*c;
73  double st = std::sqrt( bsq - ac4 );
74 
75  //cj std::cout << F( 8, 3, a ) << ' ' << F( 8, 3, b ) << ' ' << F( 8, 3, c ) << std::endl;
76  //cj std::cout << F( 8, 3, bsq ) << ' ' << F( 8, 3, ac4 ) << ' ' << F( 8, 3, st ) << std::endl;
77 
78  n1 = ((-b)+st)/(2*a);
79  n2 = ((-b)-st)/(2*a);
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 ///
84 /// @brief subtract angles in degrees, restricting the range of the result
85 ///
86 /// @details
87 ///
88 /// given angles a and b in degrees, get a-b restricted to
89 /// [-180.,180.), assuming that a-b=a-b+n*360, n=any integer
90 ///
91 /// @param[in] a - in - angle in degrees
92 /// @param[in] b - in - angle in degrees
93 ///
94 /// @return angle a-b in degrees, restricted to the specified range
95 ///
96 /// @remarks
97 ///
98 /// @references
99 ///
100 /// @author ctsa 8-19-03
101 ///
102 /////////////////////////////////////////////////////////////////////////////////
103 double
105  double a,
106  double b
107 )
108 {
109  return periodic_range( a - b, double(360.0) );
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 ///
114 /// @brief subtract angles in radians, restricting the range of the result
115 ///
116 /// @details
117 ///
118 /// given angles a and b in degrees, get a-b restricted to
119 /// [-pi,pi), assuming that a-b=a-b+n*2*pi, n=any integer
120 ///
121 /// @param[in] a - in - angle in radians
122 /// @param[in] b - in - angle in radians
123 ///
124 /// @return angle a-b in degrees, restricted to the specified range
125 ///
126 /// @remarks
127 ///
128 /// @references
129 ///
130 /// @author ctsa 8-19-03
131 ///
132 /////////////////////////////////////////////////////////////////////////////////
133 double
135  double a,
136  double b
137 )
138 {
139  using namespace numeric::constants::f;
140  return periodic_range( a - b, pi_2 );
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 ///
145 /// @brief a is restricted to [-x/2,x/2), assuming that a=a+n*x, n=any integer
146 ///
147 /// @details
148 ///
149 /// @param[in] a - in - input value with periodicity x
150 /// @param[in] x - in - periodicity of a
151 ///
152 /// @return a restricted to [-x/2,x/2)
153 ///
154 /// @remarks
155 ///
156 /// @references
157 ///
158 /// @author ctsa 8-19-03
159 ///
160 /////////////////////////////////////////////////////////////////////////////////
161 double
163  double a,
164  double x
165 )
166 {
167  double const halfx = 0.5f * x;
168  return ( ( a >= halfx || a < -halfx ) ? mod( mod( a, x ) + ( x + halfx ), x ) - halfx : a );
169 }
170 
171 ////////////////////////////////////////////////////////////////////////////////
172 ///
173 ///
174 /// @details
175 ///
176 /// @param[in] a - in - input value with periodicity x
177 /// @param[in] x - in - periodicity of a
178 ///
179 /// @return a restricted to [0.,x)
180 ///
181 /// @remarks
182 ///
183 /// @references
184 ///
185 /// @author ctsa 8-19-03
186 ///
187 /////////////////////////////////////////////////////////////////////////////////
188 double
190  double a,
191  double x
192 )
193 {
194  return ( ( a >= x || a < 0.0 ) ? mod( mod( a, x ) + x, x ) : a );
195 }
196 
197 /// @brief taken from wobble.cc
198 void
199 angle_in_range( double & ang )
200 {
201  int const odd = (int)std::fabs( double( mod( static_cast< int >( ang / double(180.0) ), 2 ) ) ); // 1 if ang/180 is odd,0 if ang/180 is even
202  ang = mod( ang, double(180.0) );
203  if ( odd == 0 ) return;
204  if ( ang > 0.0 ) {
205  ang -= double(180.0);
206  return;
207  }
208  ang += double(180.0);
209  assert( ang <= double(180.0) && ang > double(-180.0));
210 }
211 
212 
213 } // namespace basic
double periodic_range(double a, double x)
a is restricted to [-x/2,x/2), assuming that a=a+n*x, n=any integer
Definition: basic.cc:162
double unsigned_periodic_range(double a, double x)
a is restricted to [0.,x), assuming that a=a+n*x,, n=any integer
Definition: basic.cc:189
def x
double subtract_radian_angles(double a, double b)
subtract angles in radians, restricting the range of the result
Definition: basic.cc:134
double subtract_degree_angles(double a, double b)
subtract angles in degrees, restricting the range of the result
Definition: basic.cc:104
T mod(T const &x, T const &y)
x(mod y) computational modulo returning magnitude < | y | and sign of x
Definition: Fmath.hh:475
void calc_quadratic(double a, double b, double c, double &n1, double &n2)
calculates quadratic polynomial solutions
Definition: basic.cc:61
void angle_in_range(double &ang)
taken from wobble.cc
Definition: basic.cc:199
Common numeric constants in varying precisions.