nonlinear-solvers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNonlinear Solvers
非线性求解器
Goal
目标
Provide a universal workflow to select a nonlinear solver, configure globalization strategies, and diagnose convergence for root-finding, optimization, and least-squares problems.
提供通用工作流,为根求解、优化和最小二乘问题选择非线性求解器、配置全局策略并诊断收敛情况。
Requirements
要求
- Python 3.10+
- NumPy (for Jacobian diagnostics)
- SciPy (optional, for advanced analysis)
- Python 3.10+
- NumPy(用于Jacobian矩阵诊断)
- SciPy(可选,用于高级分析)
Inputs to Gather
需要收集的输入信息
| Input | Description | Example |
|---|---|---|
| Problem type | Root-finding, optimization, least-squares | |
| Problem size | Number of unknowns | |
| Jacobian availability | Analytic, finite-diff, unavailable | |
| Jacobian cost | Cheap or expensive to compute | |
| Constraints | None, bounds, equality, inequality | |
| Smoothness | Is objective/residual smooth? | |
| Residual history | Sequence of residual norms | |
| 输入项 | 描述 | 示例 |
|---|---|---|
| 问题类型 | 根求解、优化、最小二乘 | |
| 问题规模 | 未知量数量 | |
| Jacobian矩阵可用性 | 解析形式、有限差分、不可用 | |
| Jacobian矩阵计算成本 | 低或高 | |
| 约束条件 | 无、边界约束、等式约束、不等式约束 | |
| 平滑性 | 目标函数/残差是否平滑? | |
| 残差历史 | 残差范数序列 | |
Decision Guidance
决策指南
Solver Selection Flowchart
求解器选择流程图
Is Jacobian available and cheap?
├── YES → Problem size?
│ ├── Small (n < 1000) → Newton (full)
│ └── Large (n ≥ 1000) → Newton-Krylov
└── NO → Is objective smooth?
├── YES → Memory limited?
│ ├── YES → L-BFGS or Broyden
│ └── NO → BFGS
└── NO → Anderson acceleration or PicardIs Jacobian available and cheap?
├── YES → Problem size?
│ ├── Small (n < 1000) → Newton (full)
│ └── Large (n ≥ 1000) → Newton-Krylov
└── NO → Is objective smooth?
├── YES → Memory limited?
│ ├── YES → L-BFGS or Broyden
│ └── NO → BFGS
└── NO → Anderson acceleration or PicardQuick Reference
快速参考表
| Problem Type | First Choice | Alternative | Globalization |
|---|---|---|---|
| Small root-finding | Newton | Broyden | Line search |
| Large root-finding | Newton-Krylov | Anderson | Trust region |
| Optimization | L-BFGS | BFGS | Wolfe line search |
| Least-squares | Levenberg-Marquardt | Gauss-Newton | Trust region |
| Bound constrained | L-BFGS-B | Trust-region reflective | Projected |
| 问题类型 | 首选方案 | 替代方案 | 全局策略 |
|---|---|---|---|
| 小规模根求解 | Newton | Broyden | 线搜索 |
| 大规模根求解 | Newton-Krylov | Anderson | 信赖域 |
| 优化问题 | L-BFGS | BFGS | Wolfe线搜索 |
| 最小二乘问题 | Levenberg-Marquardt | Gauss-Newton | 信赖域 |
| 带边界约束问题 | L-BFGS-B | 信赖域反射法 | 投影法 |
Script Outputs (JSON Fields)
脚本输出(JSON字段)
| Script | Key Outputs |
|---|---|
| |
| |
| |
| |
| |
| |
| 脚本 | 核心输出 |
|---|---|
| |
| |
| |
| |
| |
| |
Workflow
工作流
- Characterize problem - Identify type, size, Jacobian availability
- Select solver - Run
scripts/solver_selector.py - Choose globalization - Run
scripts/globalization_advisor.py - Analyze Jacobian - If available, run
scripts/jacobian_diagnostics.py - Monitor residuals - During solve, use
scripts/residual_monitor.py - Analyze convergence - Run
scripts/convergence_analyzer.py - Evaluate steps - For trust region, use
scripts/step_quality.py
- 问题特征分析 - 确定问题类型、规模、Jacobian矩阵可用性
- 选择求解器 - 运行
scripts/solver_selector.py - 选择全局策略 - 运行
scripts/globalization_advisor.py - 分析Jacobian矩阵 - 若矩阵可用,运行
scripts/jacobian_diagnostics.py - 监控残差 - 求解过程中使用
scripts/residual_monitor.py - 分析收敛情况 - 运行
scripts/convergence_analyzer.py - 评估步长质量 - 针对信赖域方法,使用
scripts/step_quality.py
Conversational Workflow Example
对话式工作流示例
User: My Newton solver for a phase-field simulation is converging very slowly. After 50 iterations, the residual only dropped from 1 to 0.1.
Agent workflow:
- Analyze convergence:
bash
python3 scripts/convergence_analyzer.py --residuals 1,0.8,0.6,0.5,0.4,0.3,0.2,0.15,0.12,0.1 --json - Check globalization strategy:
bash
python3 scripts/globalization_advisor.py --problem-type root-finding --jacobian-quality ill-conditioned --previous-failures 0 --json - Recommend: Switch to trust region with Levenberg-Marquardt regularization, or use Newton-Krylov with better preconditioning.
用户:我用于相场模拟的Newton求解器收敛非常慢。50次迭代后,残差仅从1降到0.1。
Agent工作流:
- 分析收敛情况:
bash
python3 scripts/convergence_analyzer.py --residuals 1,0.8,0.6,0.5,0.4,0.3,0.2,0.15,0.12,0.1 --json - 检查全局策略:
bash
python3 scripts/globalization_advisor.py --problem-type root-finding --jacobian-quality ill-conditioned --previous-failures 0 --json - 推荐方案:切换到带Levenberg-Marquardt正则化的信赖域方法,或使用带更好预条件子的Newton-Krylov方法。
Pre-Solve Checklist
求解前检查清单
- Confirm problem type (root-finding, optimization, least-squares)
- Assess Jacobian availability and cost
- Check initial guess quality
- Set appropriate tolerances
- Choose globalization strategy
- Prepare to monitor convergence
- 确认问题类型(根求解、优化、最小二乘)
- 评估Jacobian矩阵的可用性与计算成本
- 检查初始猜测的质量
- 设置合适的容差
- 选择全局策略
- 准备监控收敛情况
CLI Examples
CLI示例
bash
undefinedbash
undefinedSelect solver for large unconstrained optimization
为大规模无约束优化选择求解器
python3 scripts/solver_selector.py --size 50000 --smooth --memory-limited --json
python3 scripts/solver_selector.py --size 50000 --smooth --memory-limited --json
Select solver for a small nonlinear least-squares (data-fitting) problem
为小型非线性最小二乘(数据拟合)问题选择求解器
python3 scripts/solver_selector.py --problem-type least-squares --size 6 --jacobian-available --smooth --json
python3 scripts/solver_selector.py --problem-type least-squares --size 6 --jacobian-available --smooth --json
Analyze convergence from residual history
根据残差历史分析收敛情况
python3 scripts/convergence_analyzer.py --residuals 1,0.1,0.01,0.001,0.0001 --tolerance 1e-6 --json
python3 scripts/convergence_analyzer.py --residuals 1,0.1,0.01,0.001,0.0001 --tolerance 1e-6 --json
Diagnose Jacobian quality
诊断Jacobian矩阵质量
python3 scripts/jacobian_diagnostics.py --matrix jacobian.txt --json
python3 scripts/jacobian_diagnostics.py --matrix jacobian.txt --json
Get globalization recommendation
获取全局策略推荐
python3 scripts/globalization_advisor.py --problem-type optimization --jacobian-quality good --json
python3 scripts/globalization_advisor.py --problem-type optimization --jacobian-quality good --json
Globalization for a distant initial guess (favors trust region)
针对远离解的初始猜测的全局策略(优先信赖域)
python3 scripts/globalization_advisor.py --problem-type root-finding --jacobian-quality good --far-from-solution --json
python3 scripts/globalization_advisor.py --problem-type root-finding --jacobian-quality good --far-from-solution --json
Monitor residual patterns
监控残差模式
python3 scripts/residual_monitor.py --residuals 1,0.8,0.9,0.7,0.75,0.6 --target-tolerance 1e-8 --json
python3 scripts/residual_monitor.py --residuals 1,0.8,0.9,0.7,0.75,0.6 --target-tolerance 1e-8 --json
Evaluate step quality for trust region
评估信赖域方法的步长质量
python3 scripts/step_quality.py --predicted-reduction 0.5 --actual-reduction 0.4 --step-norm 0.8 --gradient-norm 1.0 --trust-radius 1.0 --json
undefinedpython3 scripts/step_quality.py --predicted-reduction 0.5 --actual-reduction 0.4 --step-norm 0.8 --gradient-norm 1.0 --trust-radius 1.0 --json
undefinedError Handling
错误处理
| Error | Cause | Resolution |
|---|---|---|
| Invalid size | Check problem dimension |
| Size above 10 billion cap | Re-check the unit/value |
| Unknown constraint | Use: none, bound, equality, inequality |
| Unknown problem type | Use: root-finding, optimization, least-squares |
| Invalid residual data | Check residual computation |
| NaN/Inf in residual data | Sanitize residual history |
| More than 100,000 entries | Downsample the history |
| Invalid path | Verify Jacobian file exists |
| Matrix file too large | Use a smaller / sparser matrix |
| 错误信息 | 原因 | 解决方法 |
|---|---|---|
| 无效规模值 | 检查问题维度 |
| 规模超过100亿上限 | 重新检查单位/数值 |
| 未知约束类型 | 使用以下值:none, bound, equality, inequality |
| 未知问题类型 | 使用以下值:root-finding, optimization, least-squares |
| 无效残差数据 | 检查残差计算逻辑 |
| 残差数据包含NaN/Inf | 清理残差历史数据 |
| 条目超过10万条 | 对历史数据进行下采样 |
| 路径无效 | 确认Jacobian矩阵文件存在 |
| 矩阵文件过大 | 使用更小/更稀疏的矩阵 |
Interpretation Guidance
解读指南
Convergence Type
收敛类型
| Type | Meaning | Action |
|---|---|---|
| quadratic | Optimal Newton (order p ≈ 2) | Continue, near solution |
| superlinear | Ratios shrinking toward 0 (1 < p < 2); quasi-Newton working | Monitor for stagnation |
| linear | Constant contraction ratio (p ≈ 1); a small constant ratio is fast-linear, not superlinear | May improve with preconditioner |
| sublinear | Too slow (ratio → 1) | Change method or formulation |
| stagnated | No progress | Check Jacobian, preconditioner |
| diverged | Increasing residual | Add globalization, check Jacobian |
| 类型 | 含义 | 操作建议 |
|---|---|---|
| quadratic | 最优Newton收敛(阶数p≈2) | 继续执行,已接近解 |
| superlinear | 收缩比趋近于0(1 < p < 2);拟Newton方法生效 | 监控是否出现停滞 |
| linear | 收缩比恒定(p≈1);小恒定比属于快速线性收敛,而非超线性 | 可考虑添加预条件子 |
| sublinear | 收敛过慢(收缩比趋近于1) | 更换方法或重新构建问题 |
| stagnated | 无进展 | 检查Jacobian矩阵、预条件子 |
| diverged | 残差增大 | 添加全局策略,检查Jacobian矩阵 |
Jacobian Quality
Jacobian矩阵质量
| Quality | Condition Number | Action |
|---|---|---|
| good | < 10⁶ | Standard Newton works |
| moderately-conditioned | 10⁶ - 10¹⁰ | Consider scaling |
| ill-conditioned | > 10¹⁰ | Use regularization |
| near-singular | ∞ | Reformulate or use LM |
| 质量等级 | 条件数 | 操作建议 |
|---|---|---|
| good | < 10⁶ | 标准Newton方法可行 |
| moderately-conditioned | 10⁶ - 10¹⁰ | 考虑进行缩放处理 |
| ill-conditioned | > 10¹⁰ | 使用正则化方法 |
| near-singular | ∞ | 重新构建问题或使用LM方法 |
Step Quality (Trust Region)
步长质量(信赖域)
| Ratio ρ | Quality | Trust Radius |
|---|---|---|
| ρ < 0 | very_poor | Shrink aggressively |
| ρ < 0.25 | marginal | Shrink |
| 0.25 ≤ ρ < 0.75 | good | Maintain |
| ρ ≥ 0.75 | excellent | Expand if at boundary |
| 比值ρ | 质量等级 | 信赖域半径操作 |
|---|---|---|
| ρ < 0 | very_poor | 大幅缩小半径 |
| ρ < 0.25 | marginal | 缩小半径 |
| 0.25 ≤ ρ < 0.75 | good | 保持半径 |
| ρ ≥ 0.75 | excellent | 若步长触达边界则扩大半径 |
Verification checklist
验证检查清单
Do not trust a "solved" claim until these concrete artifacts are recorded:
- Logged the full residual norm history and ran ; recorded
convergence_analyzer.py --residuals <history>andconvergence_type, and confirmedestimated_rateagainst the actual solver tolerance (not the defaultconverged: true).1e-10 - Confirmed the residual sequence is monotone-decreasing or fed it to ; recorded
residual_monitor.pyand verified it does NOT includepatterns_detected,diverging,oscillating, orplateauwhile still above tolerance.slow_convergence - If a Jacobian is available, ran and recorded
jacobian_diagnostics.py --matrix J.txtandcondition_number; for an analytic Jacobian, passedjacobian_qualityand confirmed--finite-diff-matrixis below ~1e-2 (no "Large discrepancy" note).finite_diff_error - Checked from
rank_deficientisjacobian_diagnostics.py(or documented why a rank-deficient/near-singular Jacobian is expected and that Levenberg-Marquardt regularization is in use).false - For a trust-region solve, evaluated accepted steps with and recorded the reduction
step_quality.py; confirmed accepted steps haveratio(notratio >= 0.25/very_poor) and that thepoormatches the recorded ρ.trust_radius_action - Recorded the solver and globalization actually used and confirmed they match and
solver_selector.pyrecommendations for the stated problem type, size, and Jacobian quality (e.g., large/expensive-Jacobian → Newton-Krylov; least-squares → Levenberg-Marquardt trust region).globalization_advisor.py - Re-confirmed convergence after any change to tolerance, initial guess, or preconditioner — the convergence type can flip (e.g., quadratic → linear/stagnated) and must be re-classified, not assumed.
在记录以下具体成果前,不要轻信“已求解”的结论:
- 记录完整的残差范数历史,并运行;记录
convergence_analyzer.py --residuals <history>和convergence_type,并确认estimated_rate与求解器实际容差匹配(而非默认的converged: true)。1e-10 - 确认残差序列单调递减,或已将其输入;记录
residual_monitor.py,并验证在容差之上时不包含patterns_detected、diverging、oscillating或plateau。slow_convergence - 若Jacobian矩阵可用,运行并记录
jacobian_diagnostics.py --matrix J.txt和condition_number;对于解析形式的Jacobian矩阵,传入jacobian_quality并确认--finite-diff-matrix低于约1e-2(无“Large discrepancy”提示)。finite_diff_error - 检查返回的
jacobian_diagnostics.py为rank_deficient(或记录为何Jacobian矩阵是秩亏/近奇异的,并确认已使用Levenberg-Marquardt正则化)。false - 对于信赖域求解,使用评估已接受的步长并记录收缩
step_quality.py;确认已接受步长的ratio(非ratio >= 0.25/very_poor),且poor与记录的ρ匹配。trust_radius_action - 记录实际使用的求解器和全局策略,并确认它们与和
solver_selector.py针对所述问题类型、规模和Jacobian矩阵质量给出的推荐一致(例如,大规模/高成本Jacobian矩阵→Newton-Krylov;最小二乘问题→Levenberg-Marquardt信赖域)。globalization_advisor.py - 在修改容差、初始猜测或预条件子后,重新确认收敛情况——收敛类型可能会变化(例如,二次→线性/停滞),必须重新分类,而非假设不变。
Common pitfalls & rationalizations
常见陷阱与误区
| Tempting shortcut | Why it's wrong / what to do |
|---|---|
| "The residual ratio is a small constant (~0.1), so it's converging superlinearly." | A constant contraction ratio is linear, not superlinear — |
| "It stopped without erroring, so the solver converged." | Run completion is not convergence. Check |
| "Two iterations look like they're shrinking, so the rate is fine." | Order estimation needs at least 3 strictly decreasing positive residuals; with fewer, |
| "I coded the analytic Jacobian, so it must be right." | A wrong Jacobian still produces some step. Run |
| "Newton diverged, so I'll just shrink the global tolerance and call it close enough." | Divergence ( |
| "Trust-region step decreased the objective, so accept and expand the radius." | Acceptance and radius growth depend on the reduction ratio ρ, not just sign. |
| "The Jacobian is large and expensive, but full Newton is the gold standard, so I'll form it anyway." | For n ≥ 1000 or expensive Jacobians, |
| 诱人的捷径 | 错误原因/正确做法 |
|---|---|
| “残差比是一个小常数(≈0.1),所以是超线性收敛。” | 恒定收缩比属于线性收敛,而非超线性—— |
| “求解器未报错就停止了,所以已经收敛。” | 运行完成不等于收敛。检查 |
| “两次迭代看起来在收缩,所以收敛速率没问题。” | 阶数估计至少需要3个严格递减的正残差;若残差数量不足, |
| “我编写了解析形式的Jacobian矩阵,所以肯定是正确的。” | 错误的Jacobian矩阵仍会产生步长。运行 |
| “Newton方法发散了,我只要缩小全局容差就可以认为足够接近了。” | 发散( |
| “信赖域步长降低了目标函数,所以接受并扩大半径。” | 接受步长和扩大半径取决于收缩比ρ,而非仅符号。只有当ρ≥0.75且步长触达边界时, |
| “Jacobian矩阵规模大且计算成本高,但全Newton方法是黄金标准,所以我还是要构建它。” | 当n≥1000或Jacobian矩阵计算成本高时, |
Security
安全说明
Input Validation
输入验证
- (problem size) is validated as a positive integer, bounded at 10 billion
--size - are validated as finite non-negative numbers, capped at 100,000 entries
--residuals - and
--toleranceare validated as finite positive numbers--target-tolerance - and
--problem-typeare validated against fixed allowlists--constraint-type - is validated against a fixed allowlist (
--jacobian-quality,good, etc.)ill-conditioned - Step quality parameters (,
predicted-reduction,actual-reduction,step-norm,gradient-norm) are validated as finite numberstrust-radius
- (问题规模)会被验证为正整数,上限为100亿
--size - 会被验证为有限非负数,条目上限为10万条
--residuals - 和
--tolerance会被验证为有限正数--target-tolerance - 和
--problem-type会与固定允许列表进行验证--constraint-type - 会与固定允许列表(
--jacobian-quality,good等)进行验证ill-conditioned - 步长质量参数(,
predicted-reduction,actual-reduction,step-norm,gradient-norm)会被验证为有限数trust-radius
File Access
文件访问
- reads a single matrix file specified by
jacobian_diagnostics.py; no directory traversal beyond the given path--matrix - Matrix files are size-limited and loaded with to prevent code execution
allow_pickle=False - All other scripts read no external files; inputs are provided via CLI arguments
- Scripts write only to stdout (JSON output)
- 仅读取
jacobian_diagnostics.py指定的单个矩阵文件;不会遍历给定路径之外的目录--matrix - 矩阵文件有大小限制,加载时设置以防止代码执行
allow_pickle=False - 所有其他脚本不读取外部文件;输入通过CLI参数提供
- 脚本仅向标准输出(stdout)写入JSON结果
Tool Restrictions
工具限制
- Read: Used to inspect script source, references, and user configuration files
- Bash: Used to execute the six Python analysis scripts (,
solver_selector.py,convergence_analyzer.py,jacobian_diagnostics.py,globalization_advisor.py,residual_monitor.py) with explicit argument listsstep_quality.py - Write: Used to save analysis results or solver recommendations; writes are scoped to the user's working directory
- Grep/Glob: Used to locate relevant files and search references
- 读取: 用于检查脚本源码、参考资料和用户配置文件
- Bash: 用于执行6个Python分析脚本(,
solver_selector.py,convergence_analyzer.py,jacobian_diagnostics.py,globalization_advisor.py,residual_monitor.py),并使用明确的参数列表step_quality.py - 写入: 用于保存分析结果或求解器推荐;写入范围限定在用户工作目录
- Grep/Glob: 用于定位相关文件和搜索参考资料
Safety Measures
安全措施
- No ,
eval(), or dynamic code generationexec() - All subprocess calls use explicit argument lists (no )
shell=True - Matrix dimension limits prevent memory exhaustion when loading Jacobian files
- Residual history analysis operates on bounded-length numeric arrays only
- 不使用、
eval()或动态代码生成exec() - 所有子进程调用使用明确的参数列表(不使用)
shell=True - 矩阵维度限制可防止加载Jacobian文件时内存耗尽
- 残差历史分析仅对有限长度的数值数组进行操作
Limitations
局限性
- No global convergence guarantee: All methods may fail for pathological problems
- Jacobian accuracy: Finite-difference Jacobian may be inaccurate near discontinuities
- Large dense problems: May require specialized solvers not covered here
- Constrained optimization: Complex constraints need SQP or interior point methods
- 无全局收敛保证: 所有方法在面对病态问题时都可能失败
- Jacobian矩阵精度: 有限差分Jacobian矩阵在不连续点附近可能不准确
- 大规模稠密问题: 可能需要此处未涵盖的专用求解器
- 约束优化: 复杂约束需要SQP或内点法
References
参考资料
- - Problem-based solver selection
references/solver_decision_tree.md - - Method details and parameters
references/method_catalog.md - - Diagnosing convergence issues
references/convergence_diagnostics.md - - Line search and trust region
references/globalization_strategies.md
- - 基于问题的求解器选择指南
references/solver_decision_tree.md - - 方法细节与参数说明
references/method_catalog.md - - 收敛问题诊断指南
references/convergence_diagnostics.md - - 线搜索与信赖域策略
references/globalization_strategies.md
Version History
版本历史
- v1.2.2 (2026-06-24): Added a "Verification checklist" (evidence tied to each script's JSON outputs — convergence type/rate, residual patterns, Jacobian condition/finite-diff error, rank, trust-region step ratio, and solver/globalization agreement) and a "Common pitfalls & rationalizations" table covering constant-ratio-vs-superlinear, run-completion-vs-convergence, too-few-iterations, unverified analytic Jacobians, divergence handling, trust-region acceptance, and large/expensive-Jacobian routing
- v1.2.0 (2026-06-23): Added to
--problem-typewith a nonlinear least-squares path (Levenberg-Marquardt / Gauss-Newton); reordered solver selection so problem size dominates high-accuracy and routes large/expensive-Jacobian problems to Newton-Krylov; addedsolver_selector.pyto--far-from-solutionand surfaced Levenberg-Marquardt as the trust-region type for least-squares; corrected convergence classification so constant-ratio sequences are linear (not superlinear); RFC-8259-safe JSON (noglobalization_advisor.py); input-validation hardening-Infinity - v1.1.0 (2026-03-26): Optimized agent-discovery description, evaluation suite, security review docs, standardized metadata block, CHANGELOG
- v1.0.0: Initial release with 6 analysis scripts
- v1.2.2 (2026-06-24): 添加“验证检查清单”(与各脚本JSON输出绑定的证据——收敛类型/速率、残差模式、Jacobian矩阵条件数/有限差分误差、秩、信赖域步长比、求解器/全局策略一致性)和“常见陷阱与误区”表格,涵盖恒定比值vs超线性、运行完成vs收敛、迭代次数不足、未验证的解析Jacobian矩阵、发散处理、信赖域接受规则、大规模/高成本Jacobian矩阵路由等内容
- v1.2.0 (2026-06-23): 为添加
solver_selector.py参数,支持非线性最小二乘路径(Levenberg-Marquardt / Gauss-Newton);调整求解器选择逻辑,使问题规模优先于高精度需求,将大规模/高成本Jacobian矩阵问题引导至Newton-Krylov;为--problem-type添加globalization_advisor.py参数,并将Levenberg-Marquardt作为最小二乘问题的信赖域类型;修正收敛分类逻辑,将恒定比值序列标记为线性(而非超线性);支持符合RFC-8259标准的JSON(无--far-from-solution);强化输入验证-Infinity - v1.1.0 (2026-03-26): 优化Agent发现描述、评估套件、安全审查文档,标准化元数据块,添加CHANGELOG
- v1.0.0: 初始版本,包含6个分析脚本