gctl/lib/math/mathfunc.h

343 lines
11 KiB
C
Raw Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_MATHFUNC_H
#define _GCTL_MATHFUNC_H
2025-02-06 21:17:24 +08:00
// system's head files
#include <cmath>
#include <random>
#include <vector>
2024-09-10 15:45:07 +08:00
// library's head files
#include "../core/macro.h"
#include "../core/spmat.h"
namespace gctl
{
2025-04-23 12:39:44 +08:00
template <typename T>
inline int sign(T d)
{
return (T(0) < d) - (d < T(0));
}
template <typename T>
inline T arc(T deg)
{
return deg*GCTL_Pi/180.0L;
}
template <typename T>
inline T deg(T arc)
{
return arc*180.0L/GCTL_Pi;
}
template <typename T>
inline T sind(T deg)
{
return sin(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T cosd(T deg)
{
return cos(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T tand(T deg)
{
return tan(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T power2(T in)
{
return (in)*(in);
}
template <typename T>
inline T power3(T in)
{
return (in)*(in)*(in);
}
template <typename T>
inline T power4(T in)
{
return (in)*(in)*(in)*(in);
}
template <typename T>
inline T power5(T in)
{
return (in)*(in)*(in)*(in)*(in);
}
template <typename T>
inline T jacoby2(T x00, T x01, T x10, T x11)
{
return x00*x11-x01*x10;
}
template <typename T>
inline T jacoby3(T x00, T x01, T x02, T x10, T x11,
T x12, T x20, T x21, T x22)
{
return x00*x11*x22+x01*x12*x20+x10*x21*x02
-x02*x11*x20-x01*x10*x22-x00*x12*x21;
}
/**
* @brief Calculate the inverse matrix of a 3x3 matrix
*
* @tparam T Type name
* @param A Pointer of the input matrix, must be stored in an array of the nine coefficients in a row-major fashion
* @param invA Pointer of the output matrix, must be stored in an array of the nine coefficients in a row-major fashion
* @return true Success
* @return false Fail
*/
template <typename T>
bool inverse3x3(T *A, T *invA)
{
T det = jacoby3(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8]);
if (det <= GCTL_ZERO && det >= -1*GCTL_ZERO) return false;
invA[0] = jacoby2(A[4], A[7], A[5], A[8])/det;
invA[1] = -1.0*jacoby2(A[1], A[7], A[2], A[8])/det;
invA[2] = jacoby2(A[1], A[4], A[2], A[5])/det;
invA[3] = -1.0*jacoby2(A[3], A[6], A[5], A[8])/det;
invA[4] = jacoby2(A[0], A[6], A[2], A[8])/det;
invA[5] = -1.0*jacoby2(A[0], A[3], A[2], A[5])/det;
invA[6] = jacoby2(A[3], A[6], A[4], A[7])/det;
invA[7] = -1.0*jacoby2(A[0], A[6], A[1], A[7])/det;
invA[8] = jacoby2(A[0], A[3], A[1], A[4])/det;
return true;
}
template <typename T>
T arctg(T v)
{
T ang;
if(v>=0) ang=atan(v);
else ang=atan(v)+GCTL_Pi;
return ang;
}
template <typename T>
T arctg2(T v, T f)
{
T ang;
if(f>=0)
{
if(atan(v)>0) ang=atan(v);
else ang=atan(v) + GCTL_Pi;
}
else if(f<0)
{
if(atan(v)<0) ang=atan(v);
else ang=atan(v) - GCTL_Pi;
}
return ang;
}
2024-09-10 15:45:07 +08:00
/**
2024-09-13 10:08:41 +08:00
* @brief Return a random number in the range [low, hig]
*
* @note Call srand(seed) to initiate the random squence before using this function.
*
* @param low Lower bound
* @param hig Higher bound
2024-09-22 12:11:17 +08:00
* @return Random value
2024-09-13 10:08:41 +08:00
*/
int random(int low, int hig);
/**
* @brief Return a random number in the range [low, hig]
*
* @note Call srand(seed) to initiate the random squence before using this function.
*
* @param low Lower bound
* @param hig Higher bound
2024-09-22 12:11:17 +08:00
* @return Random value
2024-09-13 10:08:41 +08:00
*/
double random(double low, double hig);
2024-09-22 12:11:17 +08:00
/**
* @brief Return a random number in the range [low, hig]
*
* @note Call srand(seed) to initiate the random squence before using this function.
*
* @param low Lower bound
* @param hig Higher bound
* @return Random value
*/
std::complex<double> random(std::complex<double> low, std::complex<double> hig);
2024-09-13 10:08:41 +08:00
/**
2024-09-10 15:45:07 +08:00
* @brief eps eps 1e-109
*
* @param f
* @param v
* @param eps
* @return true
* @return false
*/
bool isequal(double f, double v, double eps = GCTL_ZERO);
/**
* @brief
*
* @param[in] a
*
* @return 1 -1 00
*/
double sign(double a);
/**
* @brief n次方根
*
* @param[in] val
* @param[in] order
* @param[in] eps
*
* @return
*/
double sqrtn(double val, int order, double eps = 1e-5);
2024-09-12 07:05:41 +08:00
/**
* @brief
*
* @param lon1
* @param lon2
* @param lat1
* @param lat2
* @param R
* @return
*/
double geographic_area(double lon1, double lon2, double lat1, double lat2, double R);
2024-09-12 07:13:02 +08:00
/**
* @brief
*
* @param lon1
* @param lon2
* @param lat1
* @param lat2
* @param R
* @return
*/
double geographic_distance(double lon1, double lon2, double lat1, double lat2, double R);
2024-09-10 15:45:07 +08:00
/**
* @brief
*
* @param[in] x_len x方向半径
* @param[in] y_len y方向半径
* @param[in] arc x轴正方向的夹角
* @param[in] x_arc x轴正方向绕原点逆时针旋转的角度
*
* @return
*/
double ellipse_radius_2d(double x_len, double y_len, double arc, double x_arc = 0.0);
/**
* @brief
*
* 线线
*
*
* @param x_len x方向半径
* @param y_len y方向半径
* @param arc x轴正方向的夹角
* @param elev
* @param out_arc
* @param out_rad
* @param x_arc x轴正方向绕原点逆时针旋转的角度 0
*/
void ellipse_plus_elevation_2d(double x_len, double y_len, double arc, double elev,
double &out_arc, double &out_rad, double x_arc = 0.0);
/**
* @brief
*
* @param[in] x_len x方向半径
* @param[in] y_len y方向半径
* @param[in] z_len z方向半径
* @param[in] phi x轴正方向的夹角
* @param[in] theta z轴正方向的夹角
*
* @return
*/
double ellipsoid_radius(double x_len, double y_len, double z_len, double phi, double theta);
/**
* @brief 使
*
*
*
* @param[in] in_mat
* @param inverse_mat
* @param[in] epsilon
* @param[in] iter_times 10
* @param[in] initiate
*
* @return
*/
double newton_inverse(const _2d_matrix &in_mat, _2d_matrix &inverse_mat,
double epsilon = 1e-8, int iter_times = 10, bool initiate = true);
/**
* @brief 使
*
*
*
* @param[in] in_mat
* @param inverse_mat
* @param[in] epsilon
* @param[in] iter_times 10
* @param[in] initiate
*
* @return
*/
double newton_inverse(const spmat<double> &in_mat, _2d_matrix &inverse_mat,
double epsilon = 1e-8, int iter_times = 10, bool initiate = true);
/**
* @brief 使线a转换为标准正交向量组e
*
* @note
*
* @param[in] a a_s的线性无关的向量组的指针
* @param e
* @param[in] a_s
*/
void schmidt_orthogonal(const array<double> &a, array<double> &e, int a_s);
2025-02-06 21:17:24 +08:00
};
2024-09-10 15:45:07 +08:00
#endif // _GCTL_MATHFUNC_H