Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /Conograph/trunk/src/utility_func/zmath.hh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations) (download) (as text)
Mon Jul 7 02:35:51 2014 UTC (9 years, 8 months ago) by rtomiyasu
File MIME type: text/x-c++hdr
File size: 3282 byte(s)
Source codes of version 0.9.99
1 rtomiyasu 3 /*
2     * The MIT License
3    
4     Conograph (powder auto-indexing program)
5    
6     Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK>
7    
8     Permission is hereby granted, free of charge, to any person obtaining a copy
9     of this software and associated documentation files (the "Software"), to deal
10     in the Software without restriction, including without limitation the rights
11     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12     copies of the Software, and to permit persons to whom the Software is
13     furnished to do so, subject to the following conditions:
14    
15     The above copyright notice and this permission notice shall be included in
16     all copies or substantial portions of the Software.
17    
18     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24     THE SOFTWARE.
25     *
26     */
27     #ifndef _ZMATH_HH_
28     #define _ZMATH_HH_
29    
30     #include <assert.h>
31     #include "../RietveldAnalysisTypes.hh"
32     #include "../zparam/ZParawError.hh"
33    
34     // Gets the solution of the polynomial by Newton-Raphson method.
35     bool cal_polynomial_solution(const Vec_DP& poly_coef, // Coefficients of the polynomial f(x).
36     const Double& goal_Y, // find x s.t. goal_Y = f(x).
37     const Double& init_X, // The initial value of x
38     Double& X // Answer.
39     );
40    
41     // Returns the value of the first argument raised to the power of the second argument.
42     template <class T>
43     inline T uipow(const T& a, const UInt4& k)
44     {
45     T p=1;
46     for (unsigned char bit = 0x80; bit > 0; bit >>= 1) {
47     p *= p;
48     if (k & bit) p *= a;
49     }
50     return p;
51     }
52    
53     // Returns the value of the first argument raised to the power of the second argument.
54     template <class T>
55     inline T ipow(const T& a, const Int4& k)
56     {
57     if(k>=0) return uipow(a, (unsigned char)k);
58     else return uipow(1/a, (unsigned char)(-k));
59     }
60    
61     inline const Double& PI()
62     {
63     static const Double PI = 4.0*atan(1.0);
64     return PI;
65     }
66    
67     inline const Double& PI2()
68     {
69     static const Double PI2 = 2.0 * PI(); // =2 pi.
70     return PI2;
71     }
72    
73     inline const CMPX_DP& PI2i()
74     {
75     static const CMPX_DP PI2i(0, 2.0 * PI()); // =2 pi i
76     return PI2i;
77     }
78    
79     // The units of peak_shift_param are degree.
80     inline Double cal_theta2_deg(const vector<ZParawError>& peak_shift_param_rad, const Double& wlength, const Double& inv_d)
81     {
82     static const Double DegRad = 180.0/PI();
83     assert( peak_shift_param_rad.size() == 1 );
84     const Double w_d2 = 0.5*wlength*inv_d;
85     return ( 2.0*asin(w_d2) - peak_shift_param_rad[0].value )*DegRad;
86     }
87    
88     // Return the polynomial value.
89     inline Double put_polynomial_value(const Vec_DP& poly_coef, const Double& x)
90     {
91     Int4 k = poly_coef.size()-1;
92     Double value = poly_coef[k];
93     for(; k>0;) value = value*x + poly_coef[--k];
94     return value;
95     }
96    
97 rtomiyasu 25 inline Int8 iceil(const Double& num)
98 rtomiyasu 3 {
99 rtomiyasu 25 return lrint( ceil(num) );
100 rtomiyasu 3 }
101    
102 rtomiyasu 25 inline Int8 ifloor(const Double& num)
103 rtomiyasu 3 {
104 rtomiyasu 25 return lrint( floor(num) );
105 rtomiyasu 3 }
106    
107     #endif /*MATH_HH_*/

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26