当前位置: 首页 > news >正文

jax study notes[19]

文章目录

  • simple iteration method
  • references

simple iteration method

  1. the simple iteration method (Fixed-point iteration method) can be used to computate the roots of equation through repeated approaching the correct root more and more closely.
  2. Fixed-point iteration method intends to find a point x' which satisfies f(x′)=x′f(x')=x'f(x)=x.for exmaple,f(x)=x3+2x2−9x+5=0f(x)=x^3+2x^2-9x+5=0f(x)=x3+2x29x+5=0 is equivalent to x=x3+2x2+59x=\frac {x^3+2x^2+5} {9}x=9x3+2x2+5.
  • In the first place an initial root x1′x_1'x1 has be worked out. And in the second place, the x1′x_1'x1 is substituted into the function f(x) to get f(x1′)f(x_1')f(x1) as new value x2′x_2'x2.until the nth attempt, the final value of xn′x_n'xn is almost approximately equal to f(xn′)f(x_n')f(xn) .
  • the basic code can be as follows.
import jax
import jax.numpy as jnp
from jax import jitdef fixed_point_iteration(g, x0, tol=1e-6, max_iter=1000):"""不动点迭代法求解方程 g(x) = x参数:g: 迭代函数x0: 初始猜测值tol: 容差 (默认 1e-6)max_iter: 最大迭代次数 (默认 1000)返回:近似不动点"""def cond_fn(val):x_prev, x, i = valreturn (jnp.abs(x - x_prev) > tol) & (i < max_iter)def body_fn(val):x_prev, x, i = valreturn x, g(x), i+1# 使用 while_loop 进行迭代_, result, _ = jax.lax.while_loop(cond_fn,body_fn,(x0, g(x0), 0))return result# 使用 JIT 编译加速
fixed_point_iteration_jit = jit(fixed_point_iteration, static_argnums=(0,))# 示例使用
if __name__ == "__main__":# 定义迭代函数 (例如求解 x = cos(x))def g(x):return jnp.cos(x)# 调用不动点迭代法x_star = fixed_point_iteration_jit(g, 1.0)print(f"不动点近似值为: {x_star}")print(f"验证 g(x) - x = {g(x_star) - x_star}")

the above code ignore a different situation that the iteration is not convergent.

import jax
import jax.numpy as jnp
from jax import jit
def fixed_point_iteration_advanced(g, x0, tol=1e-6, max_iter=1000):"""带收敛诊断的不动点迭代法返回:(近似不动点, 是否收敛, 迭代次数)"""def cond_fn(val):x_prev, x, i, converged = valreturn (~converged) & (i < max_iter)def body_fn(val):x_prev, x, i, _ = valx_new = g(x)converged = jnp.abs(x_new - x) < tolreturn x, x_new, i+1, converged# 使用 while_loop 进行迭代_, result, iterations, converged = jax.lax.while_loop(cond_fn,body_fn,(x0, g(x0), 0, False))return result, converged, iterations# 使用 JIT 编译加速
fixed_point_iteration_advanced_jit = jit(fixed_point_iteration_advanced, static_argnums=(0,))# 示例使用
if __name__ == "__main__":# 定义迭代函数 (例如求解 x = e^{-x})def g(x):return jnp.exp(-x)# 调用高级版本x_star, converged, iters = fixed_point_iteration_advanced_jit(g, 0.5)print(f"不动点近似值为: {x_star}")print(f"是否收敛: {converged}")print(f"迭代次数: {iters}")

references

  1. deepseek
  2. 《数值计算方法》
http://www.lryc.cn/news/598992.html

相关文章:

  • Python:Matplotlib笔记
  • 季逸超:Manus的上下文工程启示
  • JMeter压测黑马点评优惠券秒杀的配置及请求爆红问题的解决(详细图解)
  • 基于20和28 nm FPGAs的实现多通道、低非线性时间到数字转换器
  • Android15或AndroidU广播的发送流程
  • Redis学习:持久化与事务(Transaction)
  • 如何查看docker实例是否挂载目录,以及挂载了哪些目录
  • 浏览器访问[http://www.taobao.com](http://www.taobao.com/),经历了怎样的过程。
  • NOTEPAD!NPCommand函数分析之comdlg32!GetSaveFileNameW--windows记事本源代码分析
  • Python 程序设计讲义(15):Python 的数据运算——位运算
  • 人形机器人_双足行走动力学:Maxwell模型及在拟合肌腱特性中的应用
  • 深入解析Java微服务架构请求流程:Nginx到Nacos的完整旅程
  • 进阶系统策略
  • 人形机器人双足行走动力学:K-V模型其肌腱特性拟合中的应用
  • 模拟退火算法 (Simulated Annealing, SA)简介
  • 【推荐100个unity插件】Animator 的替代品?—— Animancer Pro插件的使用介绍
  • AD一张原理图分成多张原理图
  • 深入思考【九九八十一难】的意义,试用歌曲能否解释
  • python教程系列1--python001
  • 学习设计模式《十九》——享元模式
  • 【硬件-笔试面试题】硬件/电子工程师,笔试面试题-17,(知识点:PCB布线,传输线阻抗影响因素)
  • ParFlow 模型
  • 【自用】JavaSE--阶段测试
  • vite+vue3 工程-SVG图标配置使用指南——vite-plugin-svg-icons 插件
  • Vitest 用法详解及 Coverage Web 工具介绍
  • 工具篇之开发IDEA插件的实战分享
  • Nvidia Isaac Sim机械臂实验
  • Linux命令基础完结篇
  • Mysql大数据架构设计:当表中数据超过800万时,对数据表进行分表操作,以及分页查询优化详解
  • C++STL系列之set和map系列