gctl/lib/maths/gaussfunc.cpp

94 lines
3.9 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.
******************************************************/
#include "gaussfunc.h"
#include "cmath"
gctl::gaussian_para1d::gaussian_para1d(){}
gctl::gaussian_para1d::gaussian_para1d(double in_mu, double in_sigma)
{
if (in_sigma <= 0.0)
{
throw std::runtime_error("[GCTL] Invalid parameters for gctl::gaussian_para1d.");
}
mu = in_mu;
sigma = in_sigma;
}
gctl::gaussian_para2d::gaussian_para2d(){}
gctl::gaussian_para2d::gaussian_para2d(double in_mux, double in_muy, double in_sigmax,
double in_sigmay, double in_rho)
{
if (in_rho >= 1.0 || in_rho <= -1.0 || in_sigmax <= 0.0 || in_sigmay <= 0.0)
{
throw std::runtime_error("[GCTL] Invalid parameters for gctl::gaussian_para2d.");
}
mu_x = in_mux;
mu_y = in_muy;
sigma_x = in_sigmax;
sigma_y = in_sigmay;
rho = in_rho;
}
double gctl::gaussian_dist1d(double x, const gaussian_para1d &para, gradient_type_e mode)
{
if (mode == Value) return exp(-1.0*(x-para.mu)*(x-para.mu)/(2.0*para.sigma*para.sigma))/(sqrt(2.0*M_PI)*para.sigma);
if (mode == Dx) return -1.0*(x-para.mu)/(para.sigma*para.sigma)*exp(-1.0*(x-para.mu)*(x-para.mu)/(2.0*para.sigma*para.sigma))/(sqrt(2.0*M_PI)*para.sigma);
throw std::invalid_argument("[GCTL] Invalid calculating type for gctl::gaussian_dist1d(...)");
}
double gctl::gaussian_dist2d(double x, double y, const gaussian_para2d &p,
gradient_type_e mode)
{
double part = -0.5*(pow((x-p.mu_x)/p.sigma_x,2) -2*p.rho*(x-p.mu_x)*(y-p.mu_y)/(p.sigma_x*p.sigma_y)
+ pow((y-p.mu_y)/p.sigma_y,2)) / (1.0-p.rho*p.rho);
double part2;
if (mode == Value)
{
return exp(part)/(2*GCTL_Pi*p.sigma_x*p.sigma_y*sqrt(1-p.rho*p.rho));
}
if (mode == Dx)
{
part2 = -1.0*((x-p.mu_x)/(p.sigma_x*p.sigma_x) - p.rho*(y-p.mu_y)/(p.sigma_x*p.sigma_y))/(1.0-p.rho*p.rho);
return part2*exp(part)/(2*GCTL_Pi*p.sigma_x*p.sigma_y*sqrt(1-p.rho*p.rho));
}
if (mode == Dy)
{
part2 = -1.0*((y-p.mu_y)/(p.sigma_y*p.sigma_y) - p.rho*(x-p.mu_x)/(p.sigma_x*p.sigma_y))/(1.0-p.rho*p.rho);
return part2*exp(part)/(2*GCTL_Pi*p.sigma_x*p.sigma_y*sqrt(1-p.rho*p.rho));
}
throw std::invalid_argument("[GCTL] Invalid calculating type for gctl::gaussian_dist2d(...)");
}