gctl/example/kde_ex.cpp
2024-10-09 14:30:46 +08:00

106 lines
3.4 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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 "../lib/core.h"
#include "../lib/algorithm.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
/*
array<double> x(201);
x.sequence(-1.0, 0.01);
kde k(0.02, x);
array<double> d;
array<double> a(100000);
a.random_float(0.2, 0.2, RdNormal, 0);
k.get_distribution(a, d);
gaussian_para1d g1(0, 0.2);
array<double> g(201);
for (size_t i = 0; i < x.size(); i++)
{
g[i] = gaussian_dist1d(x[i], g1);
}
array<double> dm(201);
array<double> am(100000, 0.0);
for (size_t i = 0; i < am.size(); i++)
{
k.get_gradient_at(i, a, dm);
for (size_t j = 0; j < x.size(); j++)
{
am[i] += 2.0*(d[j] - g[j])*dm[j];
}
std::cout << a[i] << " " << am[i] << "\n";
}
for (size_t i = 0; i < x.size(); i++)
{
std::cout << x[i] << " " << d[i] - g[i] << "\n";
}
*/
array<double> x(201), y(301);
x.sequence(-1.0, 0.01);
y.sequence(0.0, 0.01);
kde2d k(0.1, 0.1, x, y);
array<double> a(10000), b(10000);
a.random_float(0, 0.2, RdNormal, 0);
b.random_float(1.5, 0.3, RdNormal, 0);
gaussian_para2d g1(0, 1.5, 0.2, 0.3, 0);
array<double> d(201*301);
//k.get_distribution(a, b, d);
a[0] = 0;
b[0] = 1.5;
k.get_gradient_y_at(0, 0, a, b, d);
double t, sum = 0;
for (size_t i = 0; i < y.size(); i++)
{
for (size_t j = 0; j < x.size(); j++)
{
std::cout << x[j] << " " << y[i] << " "
//<< gaussian_dist2d(x[j], y[i], g1) << " "
<< d[201*i + j] << "\n";
}
}
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}