# numhsk **Repository Path**: graviton/numhsk ## Basic Information - **Project Name**: numhsk - **Description**: numerical haskell - **Primary Language**: Haskell - **License**: AGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-04-26 - **Last Updated**: 2026-04-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Numerical Analysis - Haskell Implementation 基于 Timothy Sauer 的《Numerical Analysis》(第3版) 的 Haskell 数值算法实现。 ## 项目概述 本项目使用 Haskell 语言实现数值分析中的经典算法,作为学习和教学示例。 目前已实现 Chapter 0-9 的代表性算法: - **Chapter 0**: Fundamentals (基础) - 多项式求值、二进制数、Kahan求和 - **Chapter 1**: Solving Equations (求解方程) - 二分法、不动点迭代、牛顿法、割线法 - **Chapter 2**: Linear Systems (线性方程组) - 高斯消元、LU分解、Jacobi迭代、Cholesky分解 - **Chapter 3**: Interpolation (插值) - 拉格朗日插值、牛顿均差、切比雪夫插值、三次样条、贝塞尔曲线 - **Chapter 4**: Least Squares (最小二乘) - 正规方程、QR分解、指数拟合 - **Chapter 5**: Numerical Differentiation & Integration (数值微分与积分) - 差分、Newton-Cotes、Romberg、自适应Simpson、高斯积分 - **Chapter 6**: ODEs (常微分方程) - Euler方法、Runge-Kutta (RK4)、自适应RK45 - **Chapter 7**: Boundary Value Problems (边值问题) - 打靶法、有限差分法 - **Chapter 8**: PDEs (偏微分方程) - 热方程、波动方程、Laplace方程 - **Chapter 9**: Random Numbers & Applications (随机数及其应用) - LCG、Monte Carlo、Brownian运动、Black-Scholes ## 技术栈 - **Language**: Haskell (GHC 9.12) - **Build Tool**: Cabal (cabal-install 3.16) - **Dependencies**: - `base` - Haskell 标准库 - `containers` - 容器数据结构 - `ansi-terminal` - 终端彩色输出 ## 项目结构 ``` . ├── numhsk.cabal ├── README.md ├── AGENTS.md ├── src/ │ ├── NumHsk.hs # 库入口 │ ├── NumHsk/Chapter0.hs # 基础 │ ├── NumHsk/Chapter1.hs # 求解方程 │ ├── NumHsk/Chapter2.hs # 线性方程组 │ ├── NumHsk/Chapter3.hs # 插值 │ ├── NumHsk/Chapter4.hs # 最小二乘 │ ├── NumHsk/Chapter5.hs # 数值微分与积分 │ ├── NumHsk/Chapter6.hs # 常微分方程 │ ├── NumHsk/Chapter7.hs # 边值问题 │ ├── NumHsk/Chapter8.hs # 偏微分方程 │ └── NumHsk/Chapter9.hs # 随机数及其应用 └── app/Main.hs # 演示程序(带 ANSI 彩色输出) ``` ## 构建与运行 ### 常用 Cabal 命令 ```bash # 构建项目 cabal build # 运行演示程序 cabal run # 进入交互式 REPL cabal repl # 运行测试(如有) cabal test # 生成 Haddock 文档 cabal haddock # 清理构建产物 cabal clean # 更新包索引 cabal update # 锁定依赖版本 cabal freeze ``` ### 常用 GHC 命令 ```bash # 查看 GHC 版本 ghc --version # 直接编译单个文件 ghc -O2 Hello.hs # 交互式环境 ghci # 带优化编译 ghc -O2 -threaded -rtsopts Main.hs ``` ## 代码格式化 推荐使用 **Ormolu** 进行代码格式化: ```bash # 安装 Ormolu cabal install ormolu --allow-newer # 格式化单个文件 ormolu --mode inplace src/NumHsk/Chapter1.hs # 格式化所有 Haskell 文件 find src app -name "*.hs" -exec ormolu --mode inplace {} \; # 检查格式(不修改文件) find src app -name "*.hs" -exec ormolu --mode check {} \; ``` 使用 rust fd-find ```bash fd '\.hs$' src app -x fourmolu --mode inplace fd '\.hs$' src app -x fourmolu --mode check ``` 替代方案 **Fourmolu**(Ormolu 的配置友好版本): ```bash cabal install fourmolu # 使用自定义配置格式化 fourmolu --mode inplace src/NumHsk/Chapter1.hs ``` **stylish-haskell** 用于 import 对齐和语言扩展格式化: ```bash cabal install stylish-haskell stylish-haskell -i src/NumHsk/Chapter1.hs ``` **cabal-fmt** 用于格式化 `.cabal` 文件: ```bash cabal install cabal-fmt ----allow-newer cabal-fmt --inplace numhsk.cabal ``` ## 依赖 - GHC >= 9.12 - cabal-install >= 3.16 - base - containers - ansi-terminal ## License AGPL-3.0