This commit is contained in:
张壹 2025-08-06 13:03:03 +08:00
parent d4fb40fda6
commit b25cc550ad
2 changed files with 0 additions and 115 deletions

View File

@ -98,7 +98,6 @@ endif()
if(GCTL_FOUND AND GCTL_OPTIMIZATION_FOUND)
add_example(optimization lcg_solver_ex ON OFF ON OFF)
add_example(optimization geoblas_lcg_solver_ex ON OFF ON OFF)
add_example(optimization lgd_solver_ex ON OFF ON OFF)
add_example(optimization ex3 ON OFF ON OFF)
add_example(optimization ex4 ON OFF ON OFF)

View File

@ -1,114 +0,0 @@
/********************************************************
*
*
*
*
*
*
* 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/array_algorithm.h"
#include "gctl/core/matrix_algorithm.h"
#include "gctl/optimization/lcg_geoblas.h"
#define M 1000
#define N 800
double max_diff(const gctl::array<double> &a, const gctl::array<double> &b)
{
double max = -1.0;
for (size_t i = 0; i < a.size(); i++)
{
max = std::max(sqrt((a[i] - b[i])*(a[i] - b[i])), max);
}
return max;
}
class ex1 : public gctl::geoblas_lcg_solver
{
public:
ex1();
virtual ~ex1();
// 计算共轭梯度的B项
void cal_partb(const geoblas_vec_t dx, geoblas_vec_t dB);
//定义共轭梯度中Ax的算法
virtual void LCG_Ax(const geoblas_vec_t x, geoblas_vec_t ax);
private:
geoblas_mat_t d_kernel; // 普通二维数组做核矩阵
geoblas_vec_t d_tmp; // 中间结果数组
gctl::matrix<double> h_kernel;
};
ex1::ex1()
{
h_kernel.resize(M, N);
random_float(h_kernel, -1.0, 1.0, gctl::RdUniform);
d_kernel = geoblas_mat_new(M, N);
d_tmp = geoblas_vec_new(M);
geoblas_put_mat_data(d_kernel, h_kernel.get());
}
ex1::~ex1()
{
geoblas_mat_free(d_kernel);
geoblas_vec_free(d_tmp);
}
void ex1::cal_partb(const geoblas_vec_t dx, geoblas_vec_t dB)
{
LCG_Ax(dx, dB);
return;
}
void ex1::LCG_Ax(const geoblas_vec_t x, geoblas_vec_t ax)
{
geoblas_gemv('N', M, N, 1.0, d_kernel, x, 0.0, d_tmp);
geoblas_gemv('T', M, N, 1.0, d_kernel, d_tmp, 0.0, ax);
return;
}
int main(int argc, char const *argv[])
{
ex1 test;
gctl::array<double> m(N, 0.0);
gctl::array<double> fm(N);
random_float(fm, 1.0, 2.0, gctl::RdUniform);
geoblas_vec_t dfm = geoblas_vec_new(N);
geoblas_vec_t dm = geoblas_vec_new(N);
geoblas_vec_t dB = geoblas_vec_new(N);
geoblas_put_vec_data(dfm, fm.get());
geoblas_put_vec_data(dm, m.get());
test.cal_partb(dfm, dB);
test.lcg(dm, dB);
geoblas_get_vec_data(dm, m.get());
std::cout << "max-diff = " << max_diff(fm , m) << "\n";
return 0;
}