Geophysical Inversion and Modelling Library v1.5.4
Loading...
Searching...
No Matches
numericbase.h
1/******************************************************************************
2 * Copyright (C) 2006-2024 by the GIMLi development team *
3 * Carsten Rücker carsten@resistivity.net *
4 * *
5 * Licensed under the Apache License, Version 2.0 (the "License"); *
6 * you may not use this file except in compliance with the License. *
7 * You may obtain a copy of the License at *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 * See the License for the specific language governing permissions and *
15 * limitations under the License. *
16 * *
17 ******************************************************************************/
18
19#ifndef _GIMLI_NUMERICBASE__H
20#define _GIMLI_NUMERICBASE__H
21
22#include "gimli.h"
23#include <cmath>
24
25namespace GIMLI{
26
27#ifndef PI
28 #define PI 3.141592653589793238462643383279502884197169399375105820974944592308
29 #define PI2 6.2831853071795864769252867665590057683942387987502116419498891846
30 #define PI_2 1.5707963267948967384626433682795028841971193993751058209749445923
31#endif
32
33template < class ValueType > ValueType round(const ValueType & v, ValueType tol){ return ::rint(v / tol) * tol; }
34
36DLLEXPORT RVector logTransDropTol(const RVector & data, double logdrop=1e-6,
37 bool normalize=true);
38
41DLLEXPORT RVector logDropTol(const RVector & data, double logdrop=1e-6,
42 bool normalize=true);
43
45template < class ValueType > ValueType degToRad(const ValueType & deg){ return deg * (2.0 * PI) / 360.0; }
46
48template < class ValueType > ValueType radToDeg(const ValueType & rad){ return rad * 360.0 / (2.0 * PI); }
49
50template < class T > T powInt(const T & a, uint dim){
51 switch (dim){
52 case 0 : return (T)1;
53 case 1 : return a;
54 case 2 : return a*a;
55 case 3 : return a*a*a;
56 case 4 : return a*a*a*a;
57 case 5 : return a*a*a*a*a;
58 case 6 : return a*a*a*a*a*a;
59 default: return (T)std::pow((float)a, (float)dim);
60 }
61}
62
63
68DLLEXPORT void GaussLegendre(double x1, double x2, uint n, RVector & x, RVector & w);
69
74DLLEXPORT void GaussLaguerre(uint n, RVector & x, RVector & w);
75
76
78
79//DLLEXPORT double besselI0(double x);
80template < class ValueType > ValueType besselI0(const ValueType & x) {
81 ValueType ax = std::fabs(x), result = 0.0, y = 0.0;
82
83 if (ax < 3.75) {
84 y = x / 3.75, y = y * y ;
85 result = 1.0 + y * (3.5156229 +
86 y * (3.0899424 +
87 y * (1.2067492 +
88 y * (0.2659732 +
89 y * (0.360768e-1
90 + y * 0.45813e-2)))));
91 } else {
92 y = 3.75 / ax;
93 result = (std::exp(ax) / std::sqrt(ax)) * (0.39894228 +
94 y * (0.1328592e-1 +
95 y * (0.225319e-2 +
96 y * (-0.157565e-2 +
97 y * (0.916281e-2 +
98 y * (-0.2057706e-1 +
99 y * (0.2635537e-1 +
100 y * (-0.1647633e-1 +
101 y * 0.392377e-2))))))));
102 }
103 return result;
104}
105
107
108//DLLEXPORT double besselI1(double x);
109template < class ValueType > ValueType besselI1(const ValueType & x) {
110 ValueType ax = std::fabs(x), result = 0.0, y = 0.0;
111
112 if (ax < 3.75) {
113 y = x / 3.75, y =y * y;
114 result = ax * (0.5 +
115 y * (0.87890594 +
116 y * (0.51498869 +
117 y * (0.15084934 +
118 y * (0.2658733e-1 +
119 y * (0.301532e-2 +
120 y * 0.32411e-3))))));
121 } else {
122 y = 3.75 / ax;
123 result = 0.2282967e-1 + y * (-0.2895312e-1 + y * (0.1787654e-1 -y * 0.420059e-2));
124 result = 0.39894228 + y * (-0.3988024e-1 + y * (-0.362018e-2 +
125 y * (0.163801e-2 + y * (-0.1031555e-1 + y * result)))) ;
126 result *= (std::exp(ax) / std::sqrt(ax));
127 }
128 return x < 0.0 ? -result : result;
129}
130
131
133
134//DLLEXPORT double besselK0(double x);
135template < class ValueType > ValueType besselK0(const ValueType & x){
136 ValueType y = 0.0, result = 0.0;
137
138 if (x <= 2.0) {
139 y = x * x / 4.0;
140 result = (-std::log(x / 2.0) * besselI0(x)) + (-0.57721566
141 + y * (0.42278420
142 + y * (0.23069756
143 + y * (0.3488590e-1
144 + y * (0.262698e-2
145 + y * (0.10750e-3
146 + y * 0.74e-5))))));
147 } else {
148 y = 2.0 / x;
149 result = (std::exp(-x) / std::sqrt(x)) * (1.25331414
150 + y * (-0.7832358e-1 + y * (0.2189568e-1 + y * (-0.1062446e-1 + y * (0.587872e-2
151 + y * (-0.251540e-2
152 + y * 0.53208e-3))))));
153 }
154 return result;
155}
156
158
159//DLLEXPORT double besselK1(double x);
160template < class ValueType > ValueType besselK1(const ValueType & x){
161 ValueType y = 0.0, result = 0.0;
162
163 if (x <= 2.0) {
164 y = x * x / 4.0;
165 result = (std::log(x / 2.0) * besselI1(x)) +
166 (1.0 / x) * (1.0 + y * (0.15443144 + y * (-0.67278579 + y * (-0.18156897 +
167 y * (-0.1919402e-1 +
168 y * (-0.110404e-2 +
169 y * (-0.4686e-4))))))) ;
170 } else {
171 y = 2.0 / x;
172 result = (std::exp(-x) / std::sqrt(x)) * (1.25331414 + y * (0.23498619 +
173 y * (-0.3655620e-1 +
174 y * (0.1504268e-1 +
175 y * (-0.780353e-2 +
176 y * (0.325614e-2 +
177 y * (-0.68245e-3)))))));
178 }
179 return result;
180}
181
182//*!Spherical tangential coordinates to geocentric Cartesian coordinates
215DLLEXPORT RVector3 sphTangential2Initerial(const RVector3 &V, double lat, double lon);
216
218DLLEXPORT void lineIntegralZ_WonBevis(const RVector3 &p1, const RVector3 &p2,
219 RVector3 & dg, RVector3 & dgz);
221DLLEXPORT double lineIntegralZ_WonBevis(const RVector3 &p1, const RVector3 &p2);
222
223} // namespace GIMLI
224
225#endif // _GIMLI_NUMERICBASE__H
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
ValueType degToRad(const ValueType &deg)
Definition numericbase.h:45
RVector logDropTol(const RVector &data, double logdrop, bool normalize)
Definition numericbase.cpp:25
void GaussLegendre(double x1, double x2, uint n, RVector &x, RVector &w)
Definition numericbase.cpp:102
RVector y(const R3Vector &rv)
Definition pos.cpp:114
RVector x(const R3Vector &rv)
Definition pos.cpp:107
ValueType besselI1(const ValueType &x)
Caluculate modified Bessel function of the first kind.
Definition numericbase.h:109
ValueType besselI0(const ValueType &x)
Caluculate modified Bessel function of the first kind.
Definition numericbase.h:80
ValueType besselK0(const ValueType &x)
Caluculate modified Bessel function of the second kind.
Definition numericbase.h:135
void lineIntegralZ_WonBevis(const RVector3 &p1, const RVector3 &p2, RVector3 &dg, RVector3 &dgz)
Definition numericbase.cpp:172
ValueType besselK1(const ValueType &x)
Caluculate modified Bessel function of the second kind.
Definition numericbase.h:160
RVector3 sphTangential2Initerial(const RVector3 &V, double lat, double lon)
Definition numericbase.cpp:158
ValueType radToDeg(const ValueType &rad)
Definition numericbase.h:48
void GaussLaguerre(uint n, RVector &x, RVector &w)
Definition numericbase.cpp:51
RVector logTransDropTol(const RVector &data, double logdrop, bool normalize)
Definition numericbase.cpp:47