This commit is contained in:
2025-05-12 20:05:54 +08:00
parent aa7f5cc6a3
commit 5f4d6267b6
196 changed files with 6707 additions and 583841 deletions

41
src/core/CMakeLists.txt Normal file
View File

@@ -0,0 +1,41 @@
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
macro(add_example name switch)
if(${switch})
# 添加可执行程序名称
add_executable(${name} ${name}.cpp)
# 设置安装后的动态库调用地址
set_target_properties(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
set_target_properties(${name} PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
target_link_libraries(${name} PRIVATE gctl)
endif()
endmacro()
add_example(spmat_ex OFF)
add_example(eemd_ex OFF)
add_example(ceemdan_ex OFF)
add_example(fft_ex OFF)
add_example(fft2d_ex OFF)
add_example(fir_filter_ex OFF)
add_example(fft_filter_ex OFF)
add_example(windowfunc_ex OFF)
add_example(legendre_ex OFF)
add_example(refellipsoid_ex OFF)
add_example(kde_ex OFF)
add_example(meshio_ex OFF)
add_example(autodiff_ex OFF)
add_example(multinary_ex OFF)
add_example(dsv_io_ex OFF)
add_example(getoption_ex OFF)
add_example(process_ex OFF)
add_example(array_ex OFF)
add_example(gmt_ex OFF)
add_example(gnuplot_ex OFF)
add_example(cliplot_ex OFF)
add_example(stl_io_ex OFF)
add_example(ply_io_ex OFF)
add_example(sparray_ex OFF)
add_example(sparray2d_ex OFF)

97
src/core/array_ex.cpp Normal file
View File

@@ -0,0 +1,97 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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/io.h"
using namespace gctl;
double get_x(const point3dc &p)
{
return p.x;
}
int main(int argc, char const *argv[]) try
{
// create a new array and give initial values
array<double> A(10, 0.0, 1.0);
A.memory_usage();
A.log2linear(2);
A.show();
A.linear2log(2);
A.show();
A.for_each([](double &a){a += 1;});
A.show();
A.sequence(1.0, 0.5, 3, 4, 1);
A.show();
// copy A to a new array
array<double> B = A;
B.normalize();
B.show();
array<double> S = A + B;
std::cout << "B + A = ";
S.show();
S.normalize();
S.show();
array<point3dc> P(5);
P.sequence(point3dc(0, 0, 0), point3dc(2, 1, 0.5));
P.show(std::cout, '\n');
//array<double> Px = P.extract<double>([](const point3dc &p)->double{return p.x;});
array<double> Px = P.extract<double>(get_x);
Px.show();
// create a new 2D array
matrix<int> C(5, 5, 1);
C.sequence(0, 1, 10);
std::cout << "C = \n";
C.show();
// save array to a binary file
save_matrix2binary("tmp/array_ex_out", C, "Int");
// import 2D array to a new object
matrix<int> D;
read_binary2matrix("tmp/array_ex_out", D);
std::cout << "D = \n";
D.show();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

54
src/core/autodiff_ex.cpp Normal file
View File

@@ -0,0 +1,54 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/algorithm.h"
using namespace gctl;
affvar func(const affvar &x1, const affvar &x2)
{
// y(x1, x2) = x1*x2 + 2*cos(x1) - 3*sin(x2)
// dy/dx1 = x2 - 2*sin(x1)
// dy/dx2 = x1 - 3*cos(x2)
affvar y = x1*x2 + 2.0*cos(x1) - 3.0*sin(x2);
return y;
}
int main(int argc, char *argv[])
{
affvar x1 = 2.0;
affvar x2 = 3.0;
affvar y;
y.var({&x1, &x2}, x1);
y = func(x1, x2);
std::cout << y.val() << " " << y.der() << "\n";
y.var({&x1, &x2}, x2);
y = func(x1, x2);
std::cout << y.val() << " " << y.der() << "\n";
return 0;
}

94
src/core/ceemdan_ex.cpp Normal file
View File

@@ -0,0 +1,94 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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.
******************************************************/
/* Copyright 2013 Perttu Luukko
* This file is part of libeemd.
* libeemd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* libeemd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with libeemd. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../lib/core.h"
#include "../lib/io.h"
#include "../lib/algorithm.h"
using namespace gctl;
const size_t ensemble_size = 500;
const unsigned int S_number = 4;
const unsigned int num_siftings = 50;
const double noise_strength = 0.02;
const unsigned long int rng_seed = 0;
const std::string outfile = "ceemdan_example.out";
const size_t N = 512;
int main(int argc, char const *argv[]) try
{
libeemd_error_code err;
// As an example decompose a Dirac signal as in the original CEEMDAN paper
array<double> inp(N, 0.0);
inp[N/2] = 1.0;
// Allocate memory for output data
size_t M = emd_num_imfs(N); // This function is provided by the libeemd
matrix<double> outp;
// Run CEEMDAN
// This is just a warpper of the function ceemdan() from libeemd
// IMFs are stored in rows of the output matrix in the shape of M*N
ceemdan1d(inp, outp, M, ensemble_size, noise_strength, S_number, num_siftings, rng_seed);
std::ofstream ofile;
open_outfile(ofile, outfile);
for (size_t j=0; j<N; j++)
{
ofile << inp[j];
for (size_t i=0; i<M; i++)
{
ofile << " " << outp[i][j];
}
ofile << "\n";
}
ofile.close();
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

47
src/core/cliplot_ex.cpp Normal file
View File

@@ -0,0 +1,47 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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/graphic/cliplot.h"
int main(int argc, char *argv[])
{
double xmin = -3;
double xmax = 3;
double ymin = -1;
double ymax = 1;
gctl::cliplot c(81, 16, xmin, xmax, ymin, ymax);
//c.set_new_screen(true);
c.set_axis(5, 5);
c.set_digs(4);
c.set_wname("Time (s)");
c.set_hname("Value");
c.plot_func([](double x)->double{return sin(x);}, '.', GCTL_BOLDRED);
c.display();
return 0;
}

104
src/core/dsv_io_ex.cpp Normal file
View File

@@ -0,0 +1,104 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
dsv_io tc;
tc.delimeter('|');
tc.head_number(1);
tc.load_text("tmp/world_data", ".txt", ColHead|RowHead);
tc.info(AttInfo|HeadInfo|TagInfo);
tc.filter("America", "Continent_s", ColHead);
//tc.filter("America", "BLZ", RowHead);
//tc.save_text("out");
if (tc.has_column("Continent_s")) std::cout << "Find Column\n";
dsv_io tc2 = tc.export_table();
//tc2.head_records(tc.head_records());
tc2.delimeter('|');
tc2.save_text("out");
/*
geodsv_io tc;
tc.load_text("tmp/topo", ".txt", ColumnHead);
tc.set_column_names({"x (m)", "y (m)", "elev (m)"});
tc.set_column_type(Float, "x (m)");
tc.set_column_type(Float, "y (m)");
tc.set_column_type(Float, "elev (m)");
array<point3dc> topo;
tc.get_column_point3dc(topo, 1, 2, 3);
std::clog << std::setprecision(11) << topo.front().z << "\n";
std::clog << topo.back().x << "," << topo.back().y << "," << topo.back().z << "\n";
display_vector(tc.get_tags(), std::clog, '\n');
display_vector(tc.get_annotoations(), std::clog, '\n');
display_vector(tc.get_head_records(), std::clog, '\n');
//tc.column_output("C3", Disable);
array<double> elev;
tc.get_column(elev, "elev (m)");
elev.for_each([](double &d, size_t i){d += 100.0;});
tc.add_column(2, "elev_plus");
tc.fill_column(elev, "elev_plus");
tc.add_column(-1, "dist");
tc.cal_column("dist := sqrt(C1*C1 + C3*C3)", {"dist", "C1", "C3"});
tc.add_row(3);
tc.fill_row(array<double>{5.5, 4.4, 3.3, 2.2, 1.1}, 3);
geodsv_io out_table;
tc.filt_column("dist > 1000", {"dist"}, {"x (m)", "y (m)", "elev (m)"}, out_table);
out_table.save_text("out2");
_1s_vector s = tc.get_tags();
s.push_back("Elev = 1000");
tc.set_tags(s);
tc.save_csv("out");
double c = 4.25242153654;
tc.cell(c, 2, 1, 12);
std::clog << std::setprecision(12) << tc.cell<double>(2, 1) << "\n";
tc.info();
*/
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

100
src/core/eemd_ex.cpp Normal file
View File

@@ -0,0 +1,100 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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.
******************************************************/
/* Copyright 2013 Perttu Luukko
* This file is part of libeemd.
* libeemd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* libeemd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with libeemd. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../lib/core.h"
#include "../lib/io.h"
#include "../lib/algorithms.h"
using namespace gctl;
const size_t ensemble_size = 250;
const unsigned int S_number = 4;
const unsigned int num_siftings = 50;
const double noise_strength = 0.2;
const unsigned long int rng_seed = 0;
std::string outfile = "eemd_example.out";
// An example signal to decompose
const size_t N = 1024;
static inline double input_signal(double x) {
const double omega = 2*GCTL_Pi/(N-1);
return sin(17*omega*x)+0.5*(1.0-exp(-0.002*x))*sin(51*omega*x+1);
}
int main(int argc, char const *argv[]) try
{
// Define input data
array<double> inp(N);
for (size_t i=0; i<N; i++)
{
inp[i] = input_signal((double)i);
}
// Run eemd
matrix<double> outp;
// This is just a warpper of the function eemd() from libeemd
// IMFs are stored in rows of the output matrix in the shape of M*N
eemd1d(inp, outp, 0, ensemble_size, noise_strength, S_number, num_siftings, rng_seed);
std::ofstream ofile;
open_outfile(ofile, outfile);
size_t M = outp.row_size();
for (size_t j=0; j<N; j++)
{
ofile << inp[j];
for (size_t i=0; i<M; i++)
{
ofile << " " << outp[i][j];
}
ofile << "\n";
}
ofile.close();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

145
src/core/fft2d_ex.cpp Normal file
View File

@@ -0,0 +1,145 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
#include "../lib/algorithm.h"
using namespace gctl;
double module(std::complex<double> a)
{
return sqrt(a.real()*a.real() + a.imag()*a.imag());
}
double sin_wave(double t, double f, double phi = 0, double A = 1.0)
{
return A*sin(2*GCTL_Pi*f*t + phi);
}
void average_radian_spec(const array<double> &in_freq, const array<double> &in_power,
const array<double> &out_freq, array<double> &out_power, array<double> &out_std)
{
int in_num = in_freq.size();
int out_num = out_freq.size();
out_power.resize(out_num, 0);
out_std.resize(out_num, 0);
std::vector<double> bin;
_1d_array bin_arr;
for (size_t i = 0; i < out_num; i++)
{
bin.clear();
for (size_t j = 0; j < in_num; j++)
{
if (in_freq[j] > out_freq[i] - 0.5 && in_freq[j] <= out_freq[i] + 0.5)
{
bin.push_back(in_power[j]);
}
}
if (!bin.empty())
{
bin_arr.import_vector(bin);
out_power[i] = bin_arr.mean();
out_std[i] = bin_arr.std();
}
}
return;
}
int main(int argc, char const *argv[]) try
{
double T = 2.0; // 采样时间
double dT = 0.01; // 采样间隔
double freq = 1.0/dT; // 采样频率
int M = round(T/dT) + 1, N = round(T/dT) + 1;
_2d_matrix sig(M, N);
_1d_array X(N), Y(M);
_1d_array U(N), V(M);
X.sequence(-1.0, dT);
Y.sequence(-1.0, dT);
U.sequence(-1.0*freq, 2.0*freq/(N - 1));
V.sequence(-1.0*freq, 2.0*freq/(M - 1));
double t;
double f1 = 40.0;
double f2 = 20.0;
double f3 = 10.0;
for (size_t i = 0; i < M; i++)
{
for (size_t j = 0; j < N; j++)
{
t = sqrt(X[j]*X[j] + Y[i]*Y[i]);
sig[i][j] = pow(-1, i+j)*(sin_wave(t, f1) + sin_wave(t, f2) + sin_wave(t, f3));
}
}
_1cd_array spectrum;
dft2d(sig, spectrum);
_1d_array spec_real(M*N), spec_imag(M*N), spec_power(M*N);
for (size_t i = 0; i < M*N; i++)
{
spec_real[i] = spectrum[i].real();
spec_imag[i] = spectrum[i].imag();
spec_power[i] = module(spectrum[i]);
}
save_netcdf_grid("fft2d_sig", sig, U, V, "u", "v");
append_netcdf_grid("fft2d_sig", spec_real, "u", "v", "real");
append_netcdf_grid("fft2d_sig", spec_imag, "u", "v", "imag");
append_netcdf_grid("fft2d_sig", spec_power, "u", "v", "power");
_1d_array pro_freq(M*N), pro_spec(M*N);
for (size_t i = 0; i < M; i++)
{
for (size_t j = 0; j < N; j++)
{
pro_freq[j + i*N] = sqrt(U[j]*U[j] + V[i]*V[i]);
pro_spec[j + i*N] = spec_power[j + i*N];
}
}
_1d_array out_freq, out_power, out_std;
out_freq.resize(150, 1.0, 1.0);
average_radian_spec(pro_freq, pro_spec, out_freq, out_power, out_std);
std::ofstream ofile;
open_outfile(ofile, "fft2d_radian_spec", ".txt");
for (size_t i = 0; i < out_freq.size(); i++)
{
ofile << out_freq[i] << " " << out_power[i] << " " << out_std[i] << "\n";
}
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

87
src/core/fft_ex.cpp Normal file
View File

@@ -0,0 +1,87 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
#include "../lib/algorithm.h"
using namespace gctl;
double module(std::complex<double> a)
{
return sqrt(a.real()*a.real() + a.imag()*a.imag());
}
int main(int argc, char const *argv[]) try
{
int fs = 100;
double T = 5;
int n = round(fs*T);
double t;
gctl::array<double> in(n), out;
gctl::array<double> freq_index;
gctl::_1cd_array spectrum;
for (size_t i = 0; i < n; i++)
{
t = 1.0*T*i/n;
in[i] = 2.0*sin(2.0*GCTL_Pi*t*10) + 1.0*sin(2.0*GCTL_Pi*t*5) + 3.0*sin(2.0*GCTL_Pi*t*2);
}
// Run the r2c transform
gctl::dft_r2c_1d(in, spectrum, 1.0*fs, &freq_index);
std::ofstream ofile;
open_outfile(ofile, "fft_example_spec.out");
for (size_t i = 0; i < spectrum.size(); i++)
{
ofile << freq_index[i] << ' ' << module(spectrum[i]) << " ";
if (9.5 < freq_index[i] && freq_index[i] < 10.5) {spectrum[i].real(0.0); spectrum[i].imag(0.0);}
//if (1.5 < freq_index[i] && freq_index[i] < 2.5) {spectrum[i].real(0.0); = spectrum[i].imag(0.0);}
if (4.5 < freq_index[i] && freq_index[i] < 5.5) {spectrum[i].real(0.0); spectrum[i].imag(0.0);}
ofile << module(spectrum[i]) << "\n";
}
ofile.close();
// Run the c2r transform
gctl::dft_c2r_1d(spectrum, out);
open_outfile(ofile, "fft_example_sig.out");
for (size_t i = 0; i < n; i++)
{
t = 1.0*T*i/n;
ofile << t << " " << in[i] << " " << out[i] << "\n";
}
ofile.close();
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

View File

@@ -0,0 +1,81 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
#include "../lib/algorithm.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
int fs = 200;
double f1 = 4.0;
double f2 = 10.0;
// Create filters
fft_filter filter_lp(LowPass, Butterworth, 5, (double) fs, f1);
fft_filter filter_hp(HighPass, Butterworth, 5, (double) fs, f2);
fft_filter filter_bp(BandPass, Butterworth, 5, (double) fs, f1, f2);
fft_filter filter_bs(BandStop, Butterworth, 5, (double) fs, f1, f2);
// Demo
double f1_demo = 1.0; // Hz
double f2_demo = 7.5;
double f3_demo = 15.0;
// Create input signal
int snum = int(10.13*fs);
gctl::array<double> t(snum), signal(snum);
t.sequence(0.0, 10.13/(snum - 1));
for (size_t i = 0; i < t.size(); i++)
{
signal[i] = 2.0*sin(2.0*GCTL_Pi*f1_demo*t[i]) + sin(2.0*GCTL_Pi*f2_demo*t[i]) + 0.5*sin(2.0*GCTL_Pi*f3_demo*t[i]);
}
gctl::array<double> filtered_lp, filtered_hp, filtered_bp, filtered_bs;
// Run the filtersc。
filter_lp.filter(signal, filtered_lp);
filter_hp.filter(signal, filtered_hp);
filter_bp.filter(signal, filtered_bp);
filter_bs.filter(signal, filtered_bs);
std::ofstream ofile;
open_outfile(ofile, "fft_filter_ex.out");
for (size_t i = 0; i < filtered_lp.size(); i++)
{
ofile << t[i] << " " << signal[i] << " " << filtered_lp[i] << " " << filtered_hp[i] << " " << filtered_bp[i] << " " << filtered_bs[i] << "\n";
}
ofile.close();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

View File

@@ -0,0 +1,88 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
#include "../lib/algorithm.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
int fs = 200;
double f1 = 4.0;
double f2 = 10.0;
int m = 600;
// Create filters
fir_filter filter_lp(LowPass, Hamming, m, (double) fs, f1);
fir_filter filter_hp(HighPass, Hamming, m, (double) fs, f2);
fir_filter filter_bp(BandPass, Hamming, m, (double) fs, f1, f2);
fir_filter filter_sb(BandStop, Hamming, m, (double) fs, f1, f2);
// Get coefficient
//array<double> h_lp = filter_lp.get_coefficients();
//array<double> h_hp = filter_hp.get_coefficients();
//array<double> h_bp = filter_bp.get_coefficients();
//array<double> h_sb = filter_sb.get_coefficients();
// Demo
double f1_demo = 1.0; // Hz
double f2_demo = 7.5;
double f3_demo = 15.0;
// Create input signal
int snum = int(10.13*fs);
gctl::array<double> t(snum), signal(snum);
t.sequence(0.0, 10.13/(snum - 1));
for (size_t i = 0; i < t.size(); i++)
{
signal[i] = 2.0*sin(2.0*GCTL_Pi*f1_demo*t[i]) + sin(2.0*GCTL_Pi*f2_demo*t[i]) + 0.5*sin(2.0*GCTL_Pi*f3_demo*t[i]);
}
gctl::array<double> filtered_lp, filtered_hp, filtered_bp, filtered_sb;
// Run the filters
filter_lp.filter(signal, filtered_lp);
filter_hp.filter(signal, filtered_hp);
filter_bp.filter(signal, filtered_bp);
filter_sb.filter(signal, filtered_sb);
std::ofstream ofile;
open_outfile(ofile, "fir_filter_ex.out");
for (size_t i = 0; i < filtered_lp.size(); i++)
{
ofile << t[i] << " " << signal[i] << " " << filtered_lp[i] << " " << filtered_hp[i] << " " << filtered_bp[i] << " " << filtered_sb[i] << "\n";
}
ofile.close();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

84
src/core/getoption_ex.cpp Normal file
View File

@@ -0,0 +1,84 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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/utility.h"
#include "../lib/geometry.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
getoption gopt;
gopt.add_options({"range", "interval", "weight", "model-file", "model-tag", "out-model", "save-model"},
{true, true, true, true, true, true, false});
gopt.set_group(1, {"out-model", "save-model"});
gopt.read_options("tmp/option_sample");
gopt.check_mandatory();
gopt.check_group(1);
// 显示所有读入的键值与参数值
gopt.show_options();
// 通过键值获取参数值
std::cout << gopt.get_value("model-file") << std::endl;
// 查找不存在的命令 返回警告信息与默认值NULL
std::cout << gopt.get_value("model-tag") << std::endl;
std::cout << "---------------------" << std::endl;
// 通过选项的标签值来获取参数值 需要匹配的字符串只需要包含标签值即可
_1d_vector weight = gopt.get_value_t<_1d_vector>("weight|Weight", '|', '/');
std::cout << weight.size() << std::endl;
std::cout << weight[2] << std::endl;
double xmin, xmax, ymin, ymax;
std::string cover_str;
parse_string_to_value(gopt.get_value("range|Range"), '/', true, xmin, xmax, ymin, ymax, cover_str);
std::cout << xmin << " " << xmax << " " << ymin << " " << ymax << " " << cover_str << std::endl;
std::vector<std::string> tags;
parse_string_to_vector(gopt.get_value("model-tag"), ',', tags);
for (int i = 0; i < tags.size(); i++)
{
std::cout << tags[i] << " ";
}
std::cout << std::endl;
_1s_vector s;
std::cin >> s;
std::cout << s.size() << "\n";
std::cout << s << "\n";
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

65
src/core/gmt_ex.cpp Normal file
View File

@@ -0,0 +1,65 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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/graphic/gmt.h"
int main(int argc, char *argv[])
{
gctl::gmt gt;
/*
gt.begin_session("line_plot", "eps,png");
gt.call_module("basemap", "-R0/10/0/10 -JX5i/5i -Baf");
gt.call_module("plot", "tmp/data.txt -R0/10/0/10 -JX5i/5i -W1p,blue");
gt.end_session(false);
gt.save_session("line_plot");
*/
// example 2
gctl::array<double> data(51*41);
double dist;
for (int i = 0; i < 51; ++i)
{
for (int j = 0; j < 41; ++j)
{
dist = sqrt((j-20)*(j-20) + (i-25)*(i-25));
data[j+i*41] = sin(dist/GCTL_Pi);
}
}
gt.begin_session("image_plot", "eps");
gt.call_module("set", "FONT_ANNOT_PRIMARY=10.5p,Times-Roman,black");
std::string gname = gt.create_grid(data, 41, 51, 0.0, 1.0, 0.0, 1.0);
gt.call_module("grd2cpt", gname + " -R0/40/0/50 -Crainbow -Z -D");
gt.call_module("grdimage", gname + " -R0/40/0/50 -JX1.5i/1.5i -X0.5i -Y0.5i -Bxag+l\"x (m)\" -Byag+l\"y (m)\"");
gt.call_module("psscale", "-Bxa -By+lm -Dx0.1i/-0.2i+w1.3i/0.05i+h");
gt.end_session();
gt.save_session("image_plot");
return 0;
}

97
src/core/gnuplot_ex.cpp Normal file
View File

@@ -0,0 +1,97 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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/macro.h"
#include "../lib/graphic/gnuplot.h"
int main(int argc, char *argv[])
{
gctl::gnuplot gt;
//one line test
//gt.send("set terminal dumb");
//gt.send("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))");
//buffer test (bessel animation)
/*
gt.to_buffer();
gt.send("set terminal gif animate delay 10 size 600,400");
gt.send("set output 'bessel.gif'");
gt.send("set palette rgb 3,9,9");
gt.send("unset key; unset colorbox; unset border; unset tics");
gt.send("set lmargin at screen 0.03");
gt.send("set bmargin at screen 0");
gt.send("set rmargin at screen 0.97");
gt.send("set tmargin at screen 1");
gt.send("set parametric", true);
gt.send("bessel(x,t) = besj0(x) * cos(2*pi*t)");
gt.send("n = 6 # number of zeros");
gt.send("k = (n*pi-1.0/4*pi)");
gt.send("u_0 = k + 1/(8*k) - 31/(384*k)**3 + 3779/(15360*k)**5");
gt.send("set urange [0:u_0]");
gt.send("set vrange[0:1.5*pi]");
gt.send("set cbrange [-1:1]");
gt.send("set zrange[-1:1]");
gt.send("set isosamples 200,100");
gt.send("set pm3d depthorder");
gt.send("set view 40,200");
std::string cmd;
for (float t = 0.0f; t < 2.0f; t += 0.02f)
{
cmd = "splot u*sin(v),u*cos(v),bessel(u," + std::to_string(t) + ") w pm3d ls 1";
gt.send(cmd);
}
gt.send("set output");
gt.save_buffer("bessel");
gt.send_buffer();
*/
//data test
std::vector<double> x(101);
std::vector<double> y1(101);
std::vector<double> y2(101);
for (size_t i = 0; i < 101; i++)
{
x[i] = -1.0*GCTL_Pi + 2.0*GCTL_Pi*i/100.0;
y1[i] = sin(x[i]);
y2[i] = cos(x[i]);
}
std::vector<std::vector<double> > data;
data.push_back(x);
data.push_back(y1);
data.push_back(y2);
gt.to_buffer();
gt.send_data("$Data", data);
gt.send("plot $Data using 1:2 with lines title 'y1', '' using 1:3 with lines title 'y2'");
gt.save_buffer("sin_cos");
gt.send_buffer();
return 0;
}

106
src/core/kde_ex.cpp Normal file
View File

@@ -0,0 +1,106 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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, 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);
}

53
src/core/legendre_ex.cpp Normal file
View File

@@ -0,0 +1,53 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
#include "../lib/algebra.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
std::ofstream ofile;
open_outfile(ofile, "legendre_ex.out");
for (size_t i = 0; i < 101; i++)
{
ofile << -1.0+2.0*i/100;
for (size_t o = 0; o < 10; o++)
{
ofile << " " << legendre_polynomials(o, -1.0+2.0*i/100, false);
ofile << " " << legendre_polynomials(o, -1.0+2.0*i/100, true);
}
ofile << "\n";
}
ofile.close();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

53
src/core/multinary_ex.cpp Normal file
View File

@@ -0,0 +1,53 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/geometry.h"
#include "../lib/algorithm.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
multinary ml;
ml.init(0.1, 0.3, 0.008, 5);
//array<double> std(9, 0.05);
//array<double> xs = {-2.2, -1.8, -1.5, -1.0, -0.7, -0.2, 0.9, 1.3, 1.9, 2.5};
//ml.init(xs, std);
array<double> x(201);
sequence(x, 0.1, 0.001);
for (size_t i = 0; i < x.size(); i++)
{
std::cout << x[i] << " " << ml.val(x[i]) << " " << ml.der(x[i]) << "\n";
}
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

44
src/core/ply_io_ex.cpp Normal file
View File

@@ -0,0 +1,44 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
array<vertex3dc> nodes;
array<triangle> tris;
read_ply_binary("tmp_binary", nodes, tris);
save_ply_ascii("tmp", nodes, tris);
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

63
src/core/process_ex.cpp Normal file
View File

@@ -0,0 +1,63 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/utility.h"
using namespace gctl;
class process_ex : public process_monitor
{
public:
process_ex(){}
virtual ~process_ex(){}
void process()
{
for (size_t i = 0; i < 10; i++)
{
std::cout << "do something here...\n";
wait_for_kerboard();
if (end_process()) break;
}
set_end_process();
return;
}
};
int main(int argc, char const *argv[]) try
{
process_ex p;
p.start_monitoring();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

View File

@@ -0,0 +1,74 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/geometry.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
refellipsoid ellip;
ellip.set(WGS84);
/*
point3ds ps;
ps.set_io_precision(17);
ps.lon = -110;
ellip.geodetic2spherical(-40, 0, ps.lat, ps.rad);
std::cout << "Geocentric: " << ps << std::endl;
point3dc pc = ps.s2c();
pc.set_io_precision(17);
std::cout << "XYZ: " << pc << std::endl;
ellip.spherical2geodetic(ps, ps.lon, ps.lat, ps.rad);
std::cout << "Geodetic: " << ps << std::endl;
ellip.xyz2geodetic(pc, ps.lon, ps.lat, ps.rad);
std::cout << "Geodetic: " << ps << std::endl;
*/
point3ds ps;
ps.set_io_precision(17);
// 大地坐标 (110,40,0)
ps.lon = 110;
ellip.geodetic2spherical(40.0, 0.0, ps.lat, ps.rad);
// 球心坐标 (110,39.810615323061491,6369344.5441424493)
std::cout << "Geocentric: " << ps << std::endl;
// 转换为大地坐标
point3ds pd;
ellip.xyz2geodetic(ps.s2c(), pd.lon, pd.lat, pd.rad);
std::cout << "Geodetic: " << pd << std::endl;
// 400km高
pd.rad = 400000.0;
ellip.geodetic2spherical(pd.lat, pd.rad, pd.lat, pd.rad);
std::cout << "Geocentric: " << pd << std::endl;
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

43
src/core/sparray2d_ex.cpp Normal file
View File

@@ -0,0 +1,43 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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 "gctl/core.h"
int main(int argc, char const *argv[])
{
gctl::sparray2d<double> M(10, 10, 0.0);
M.at(4)->set(4, 5);
M.at(2)->set(3, 8);
M.at(6)->set(8, 2);
for (int i = 0; i < M.size(); i++)
{
M.at(i)->show_full();
}
return 0;
}

114
src/core/sparray_ex.cpp Normal file
View File

@@ -0,0 +1,114 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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/algorithms.h"
int main(int argc, char const *argv[])
{
srand(time(0));
gctl::sparray<double> M(50, 0.0);
gctl::sparray<double> L;
int tmp_id[13] = {4, 2, 6, 7, 8, 12, 23, 43, 33, 47, 38, 15, 1};
int tmp_size = 13;
for (int i = 0; i < tmp_size; i++)
{
M.set(tmp_id[i], gctl::random(10.0, 20.0));
}
M.show_list();
std::cout << "************" << std::endl;
M.copy_to(L, 2);
L.show_list();
M.set(18, 100);
M.set(38, 100);
M.remove(12);
std::cout << "************" << std::endl;
M.show_list();
gctl::array<double> C(50, -1.0);
M.export_dense(C, 1.0, gctl::AppendVal);
for (int i = 0; i < C.size(); i++)
{
std::cout << C.at(i) << " ";
}
std::cout << std::endl;
gctl::sparray<double> N(C, -1, 1e-10);
N.show_list();
std::cout << "Test 2" << std::endl;
M.clear();
M.malloc(20, 0.0);
M.set(5, 6.5);
M.set(9, 9.1);
M.set(3, 4.3);
M.set(17, 1.4);
M.set(10, 3.5);
M.set(7, 7.4);
M.show_list();
M.remove(17);
M.remove(9);
std::cout << "**********" << std::endl;
M.show_list();
std::cout << "Test 3" << std::endl;
int test_size = 500000;
M.clear();
M.malloc(test_size, 0.0);
clock_t start = clock();
for (int i = 0; i < 300000; i++)
{
M.set(i, gctl::random(10.0, 20.0));
}
for (int i = 300001; i < test_size; i++)
{
M.set(i, gctl::random(10.0, 20.0));
}
M.set(300000, 15.8888);
clock_t end = clock();
std::cout << "sparray set() time: " << 1000.0*(end - start)/(double)CLOCKS_PER_SEC << " ms" << std::endl;
std::cout << "M.at(300000) = " << M.value(300000) << std::endl;
M.show_list(299995, 300005);
return 0;
}

77
src/core/spmat_ex.cpp Normal file
View File

@@ -0,0 +1,77 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
spmat<double> mat(5, 5, 0.0);
std::vector<mat_node<double> > triplts;
triplts.push_back(mat_node(0, 1, 0.23));
triplts.push_back(mat_node(1, 2, 0.52));
triplts.push_back(mat_node(2, 3, 0.31));
triplts.push_back(mat_node(2, 3, 0.19));
triplts.push_back(mat_node(3, 2, 0.65));
triplts.push_back(mat_node(3, 4, 0.8));
triplts.push_back(mat_node(4, 3, 1.6));
triplts.push_back(mat_node(4, 3, 0.4));
mat.set_triplts(triplts, ReplaceVal);
mat.init_pattern();
mat.show_matrix();
std::cout << "=====\n";
// 调用clear会重置pattern
mat.clear(3, RowMajor);
for (size_t i = 0; i < triplts.size(); i++)
{
triplts[i].val = 0.1*(i + 1);
}
mat.set_triplts(triplts, ReplaceVal);
mat.show_matrix();
for (size_t i = 0; i < mat.row_size(); i++)
{
std::cout << "module(" << i << ") = " << mat.module(i, ColMajor) << "\n";
}
for (size_t i = 0; i < mat.row_size(); i++)
{
mat.scale(2.0, i);
}
mat.show_matrix();
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

45
src/core/stl_io_ex.cpp Normal file
View File

@@ -0,0 +1,45 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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/io.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
array<vertex3dc> nodes;
array<triangle> tris;
read_stl_binary("tmp/Stanford_Bunny_Binary", nodes, tris);
save_stl_ascii("tmp", tris, "Stanford_Bunny");
save_stl_binary("tmp_binary", tris, "Stanford_Bunny");
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

View File

@@ -0,0 +1,55 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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"
#include "../lib/io.h"
using namespace gctl;
int main(int argc, char const *argv[]) try
{
int t = 100;
_1d_array w1, w2, w3, w4;
window_hamming(t, w1);
window_hanning(t, w2);
window_triangle(t, w3);
window_blackman(t, w4);
std::ofstream ofile;
open_outfile(ofile, "windowfunc_ex.out");
for (size_t i = 0; i < t; i++)
{
ofile << i << " " << w1[i] << " " << w2[i] << " " << w3[i] << " " << w4[i] << "\n";
}
ofile.close();
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}