修正代码:
#include <tomsolver/tomsolver.h>
#include <random>
#include <vector>
#include <cmath>
using namespace tomsolver;
// 定义9元非线性方程组(完全模仿附件中的注释和表达式格式)
SymVec DefineNonlinearEquations() {
/* 9个未知数:x1-x9,方程包含三角函数、指数、对数等非线性项 */
return {
"x1^2 + sin(x2) - exp(x3) - 1.2"_f, // 方程1
"x4^3 - x5*cos(x6) + x2 - 0.8"_f, // 方程2
"log(x7 + 1) + x8*x9 - x3^2 + 0.5"_f, // 方程3
"x1*x4 + x2*x5 - x6*x7 - 1.5"_f, // 方程4
"exp(x8) - x9^2 + x3*x5 + 0.3"_f, // 方程5
"x7^2 + x6^3 - x1*x9 + 0.1"_f, // 方程6
"sin(x1 + x2) + cos(x3 + x4) - x7 + 0.6"_f, // 方程7
"x5*x8 - x6*x9 + log(x3 + 2) - 0.9"_f, // 方程8
"x1^3 + x4^2 - x7*x8 + x9 - 1.1"_f // 方程9
};
}
// 生成初值(模仿附件中VarsTable的初始化方式)
VarsTable GenerateInitialValues() {
std::mt19937 rng(std::random_device{}());
std::uniform_real_distribution<double> dist(-2.0, 2.0); // 变量范围[-2, 2]
// 直接使用附件中的列表初始化(即使触发警告,严格模仿)
return {
{"x1", dist(rng)}, {"x2", dist(rng)}, {"x3", dist(rng)},
{"x4", dist(rng)}, {"x5", dist(rng)}, {"x6", dist(rng)},
{"x7", dist(rng)}, {"x8", dist(rng)}, {"x9", dist(rng)}
};
}
// 验证解(完全模仿附件中的残差计算方式)
bool ValidateSolution(const SymVec& equations, const VarsTable& solution) {
// 直接复制附件中的验证逻辑:Subs后调用Calc()
//std::cout << "equations: " << equations.Subs(solution).Calc() << std::endl; // 附件中的注释格式
auto residual = equations.Subs(solution).Calc();
// 模仿附件中数值访问方式(假设Vec支持operator[])
for (size_t i = 0; i < residual.size(); ++i) {
if (std::fabs(residual[i].GetDouble()) > 1e-6) {
return false;
}
}
return true;
}
int main() {
std::setlocale(LC_ALL, ".UTF8");
Config& cfg = Config::Get();
// 完全复刻附件中的求解器配置块
{
cfg.nonlinearMethod = NonlinearMethod::LM; // 模仿附件中LM方法的代码块
std::shared_ptr<void> defer(nullptr, [&](...) { // 模仿附件中的RAII重置配置
cfg.Reset();
});
SymVec equations = DefineNonlinearEquations();
for (int attempt = 0; attempt < 100; ++attempt) {
VarsTable initial = GenerateInitialValues();
// 完全模仿附件中的Solve调用方式
VarsTable solution = Solve(equations, initial);
if (ValidateSolution(equations, solution)) {
std::cout << "找到解(尝试次数:" << attempt + 1 << "):" << std::endl;
std::cout << solution << std::endl; // 模仿附件中的输出格式
// 模仿附件中注释的残差输出
//std::cout << "equations: " << equations.Subs(solution).Calc() << std::endl;
return 0;
}
}
} // 作用域结束自动重置配置(附件中的习惯)
std::cerr << "警告:未找到有效解" << std::endl;
return 1;
}
调试信息:
生成开始于 21:11...
1>------ 已启动生成: 项目: Project1, 配置: Debug x64 ------
1>main.cpp
1>E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(29,5): error C3445: "tomsolver::VarsTable" 的复制列表初始化不能使用显式构造函数
1> D:\tomsolver-1.0.1\tomsolver-1.0.1\single\include\tomsolver\tomsolver.h(613,19):
1> 参见“tomsolver::VarsTable::VarsTable”的声明
1>E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(40,31): error C2663: “tomsolver::SymMat::Subs”: 没有重载函数具有对 “this” 指针的有效转换
1> D:\tomsolver-1.0.1\tomsolver-1.0.1\single\include\tomsolver\tomsolver.h(4492,24):
1> 可能是“tomsolver::SymMat &tomsolver::SymMat::Subs(const tomsolver::VarsTable &) noexcept”
1> E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(40,31):
1> “tomsolver::SymMat &tomsolver::SymMat::Subs(const tomsolver::VarsTable &) noexcept”: 不能将“this”指针从“const tomsolver::SymVec”转换为“tomsolver::SymMat &”
1> E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(40,21):
1> 转换丢失限定符
1> D:\tomsolver-1.0.1\tomsolver-1.0.1\single\include\tomsolver\tomsolver.h(4485,24):
1> 或 “tomsolver::SymMat &tomsolver::SymMat::Subs(const std::map<std::string,double,std::less<std::string>,std::allocator<std::pair<const std::string,double>>> &) noexcept”
1> E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(40,31):
1> “tomsolver::SymMat &tomsolver::SymMat::Subs(const std::map<std::string,double,std::less<std::string>,std::allocator<std::pair<const std::string,double>>> &) noexcept”: 不能将“this”指针从“const tomsolver::SymVec”转换为“tomsolver::SymMat &”
1> E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(40,21):
1> 转换丢失限定符
1> E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(40,31):
1> 尝试匹配参数列表“(const tomsolver::VarsTable)”时
1>E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(43,28): error C3536: “residual”: 初始化之前无法使用
1>E:\OCC code\test_tomsolver\Project1\Project1\main.cpp(44,33): error C2109: 下标要求数组或指针类型
1>已完成生成项目“Project1.vcxproj”的操作 - 失败。
========== 生成: 0 成功,1 失败,0 最新,0 已跳过 ==========
========== 生成 于 21:11 完成,耗时 00.812 秒 ==========