gctl/example/kde_ex.cpp

86 lines
3.1 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 "../lib/core.h"
#include "../lib/algorithm.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
/*
array<double> a(100000);
a.random(0, 0.2, RdNormal, 0);
kde k(0.02, a);
array<double> x;
linespace(-1.0, 1.0, 201, x);
gaussian_para1d g1(0, 0.2);
for (size_t i = 0; i < x.size(); i++)
{
std::cout << x[i] << " "
<< gaussian_dist1d(x[i], g1) << " "
<< k.get_density_at(x[i]) << " "
<< k.get_density_at(x[i], KDE_Epanechnikov) << " "
<< k.get_density_at(x[i], KDE_Rectangular) << " "
<< k.get_density_at(x[i], KDE_Triangular) << "\n";
}
*/
array<double> a(100000), b(100000);
a.random(0, 0.2, RdNormal, 0);
b.random(1, 0.2, RdNormal, 0);
kde2d k(0.02, a, b);
array<double> x, y;
linespace(-1.0, 1.0, 201, x);
linespace(0.0, 2.0, 201, y);
gaussian_para2d g1(0, 1, 0.2, 0.2, 0);
double t, sum = 0;
for (size_t i = 0; i < y.size(); i++)
{
for (size_t j = 0; j < x.size(); j++)
{
t = k.get_density_at(x[j], y[i]);
sum += t*0.01*0.01;
std::cout << x[j] << " " << y[i] << " "
<< gaussian_dist2d(x[j], y[i], g1) << " "
<< t << "\n";
}
}
std::clog << "sum = " << sum << "\n";
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}