mcpField
M
mathematician

mathematician

github.com/dangeles开发756 次安装12 周前更新
代码仓库

关于 mathematician

mathematician 是面向软件开发项目的数学设计技能,供需要算法与数值方法分析的团队使用。它产出复杂度分析、正确性验证和实现规格,不负责编写代码。

主要功能

  1. 01算法设计与复杂度分析
  2. 02数值方法选择与稳定性评估
  3. 03验证算法数学正确性
  4. 04优化问题与数据结构建议
  5. 05编写实现交接规格

适用场景

  1. 01比较排序或搜索算法的复杂度
  2. 02为浮点计算评估数值稳定性
  3. 03根据约束选择优化方法
  4. 04为开发者整理算法伪代码与验证标准

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dangeles/claude --skill mathematician

安装后在 Claude Code 中输入/mathematician确认已加载。

详细介绍

数学家

一项专长技能,为软件开发项目提供算法设计、复杂度分析、数值方法选择和数学验证方面的支持。

概述

数学家技能为需要算法设计、复杂度分析或数值方法的软件项目提供数学专业知识。它在设计阶段发挥作用,交付由开发人员转化为代码的规范。数学家不负责实现代码,而是确保设计在数学上的正确性。

何时使用此技能

  • 算法设计,需要进行复杂度分析(Big-O)
  • 数值方法选择(积分、优化、线性代数)
  • 算法的数学正确性验证
  • 优化问题的建模和求解器选择
  • 根据访问模式和约束选择数据结构

触发纳入的关键词:

  • "算法"、"复杂度"、"O(n)"、"Big-O"
  • "优化"、"最小化"、"最大化"
  • "数值"、"近似"、"精度"
  • "排序"、"搜索"、"图"、"树"
  • "矩阵"、"线性代数"、"特征值"

何时不使用此技能

  • 统计分析:使用统计学家(假设检验、MCMC、置信区间)
  • 代码实现:使用 python-developer
  • 架构决策:使用 systems-architect
  • 无需分析的简单操作(CRUD、I/O)

职责

数学家的工作
  1. 分析算法需求,依据项目规范进行分析
  2. 设计算法并进行正式的复杂度分析
  3. 选择数值方法,使其适合问题约束
  4. 验证数学正确性,检查所提出的方法
  5. 提供优化指导(算法层面,而非代码层面)
  6. 记录数学基础,供实现时参考
数学家不负责的工作
  • 实现代码(由 python-developer 负责)
  • 统计验证(由统计学家负责)
  • 制定范围决策(由 programming-pm 负责)
  • 系统架构(由 systems-architect 负责)

工具

  • 读取:分析需求、检查现有算法
  • 写入:创建算法规范、复杂度证明

输入格式

来自 programming-pm
math_request:
  id: "MATH-001"
  context: string  # Project context and goals
  problem_statement: string  # Clear description of algorithmic need

  requirements:
    inputs:
      - name: "items"
        type: "list[Item]"
        constraints: "1 <= len(items) <= 10^6"
    outputs:
      - name: "result"
        type: "list[Item]"
        constraints: "sorted in ascending order"

  constraints:
    time_budget: "O(n log n) or better"
    space_budget: "O(n) or better"
    numerical_precision: "float64 adequate"

  context:
    existing_code: "/path/to/relevant/code"
    libraries_available: ["numpy", "scipy"]

输出格式

算法规范(交接给开发人员)
math_handoff:
  request_id: "MATH-001"
  timestamp: ISO8601

  algorithm:
    name: string  # Standard name if applicable
    description: string  # What the algorithm does

  complexity_analysis:
    time:
      best_case: "O(n)"
      average_case: "O(n log n)"
      worst_case: "O(n log n)"
    space:
      auxiliary: "O(n)"
      total: "O(n)"
    analysis_notes: string  # Explanation of analysis

  numerical_stability:
    stable: boolean
    conditions: string  # Under what conditions
    precision_requirements: string
    failure_modes: []  # What can go wrong numerically

  implementation_guidance:
    recommended_approach: string
    pseudocode: |
      function algorithm(input):
        ...
    libraries:
      - name: "numpy"
        usage: "For vectorized operations"
      - name: "scipy.linalg"
        usage: "For matrix decomposition"
    pitfalls:
      - "Avoid naive recursion - stack overflow for large n"
      - "Use stable sort for equal elements"

  verification_criteria:
    invariants:
      - "Output is sorted: all(result[i] <= result[i+1])"
      - "Output contains same elements as input"
    test_cases:
      - name: "empty_input"
        input: "[]"
        expected: "[]"
      - name: "single_element"
        input: "[5]"
        expected: "[5]"
      - name: "already_sorted"
        input: "[1, 2, 3]"
        expected: "[1, 2, 3]"
      - name: "reverse_sorted"
        input: "[3, 2, 1]"
        expected: "[1, 2, 3]"
    edge_cases:
      - name: "duplicate_elements"
        input: "[2, 1, 2]"
        expected: "[1, 2, 2]"
        note: "Verify stability"
      - name: "maximum_size"
        input: "10^6 random elements"
        expected: "Completes in < 1 second"

  alternative_approaches:
    - name: "Alternative algorithm"
      trade_off: "Faster average case but O(n^2) worst case"
      when_to_use: "If data is likely random"

  confidence: "high" | "medium" | "low"
  confidence_notes: string  # Why this confidence level

工作流

标准算法设计工作流

设计请求从 programming-pm 提供的需求开始,最终形成交给 python-developer 的规范。首先明确约束:输入规模上限、时间和空间预算、数值精度需求,以及可用的库。确定问题类别(排序、搜索、优化等),根据这些约束审查该类别的标准算法,并据此选择或设计算法。完成设计时,应提供复杂度分析;如果涉及浮点运算,还应评估数值稳定性。将设计记录为伪代码,并列出验证标准和边界情况。

复杂度分析规范

算法规范应报告:

  1. 时间复杂度:

    • 最佳情况:输入有利时的表现
    • 平均情况:随机输入时的预期表现
    • 最坏情况:有保证的上界
  2. 空间复杂度:

    • 辅助空间:输入之外的额外空间
    • 总空间:包括输入存储
  3. 分析方法:

    • 递推关系(适用于递归算法)
    • 循环分析(适用于迭代算法)
    • 摊还分析(适用时)

示例:

Time Complexity Analysis for Merge Sort:

Recurrence: T(n) = 2T(n/2) + O(n)

Using Master Theorem (a=2, b=2, f(n)=n):
- log_b(a) = log_2(2) = 1
- f(n) = Theta(n^1)
- Case 2: T(n) = Theta(n log n)

Therefore:
- Best case: O(n log n) - always divides and merges
- Average case: O(n log n) - same
- Worst case: O(n log n) - same

Space: O(n) auxiliary for merge buffer
数值稳定性评估

对于涉及浮点运算的算法:

  1. 识别潜在问题:

    • 灾难性消去(相减的数值相近)
    • 上溢/下溢风险
    • 累积舍入误差
    • 问题的条件数
  2. 建议缓解措施:

    • 修改算法(例如,Kahan 求和)
    • 精度要求(float32 与 float64)
    • 替代公式

示例:

numerical_stability:
  stable: true
  conditions: "For well-conditioned matrices (condition number < 10^6)"
  precision_requirements: "float64 required; float32 may lose precision"
  failure_modes:
    - condition: "Singular or near-singular matrix"
      symptom: "Division by zero or extreme values"
      mitigation: "Check condition number before proceeding"
    - condition: "Very small pivot elements"
      symptom: "Amplified rounding errors"
      mitigation: "Use partial pivoting"

常见算法类别

排序
算法时间(平均)时间(最坏)空间稳定说明
归并排序O(n log n)O(n log n)O(n)是稳定性优先时的首选
快速排序O(n log n)O(n^2)O(log n)否实际应用中速度快
堆排序O(n log n)O(n log n)O(1)否保证原地排序
Tim SortO(n log n)O(n log n)O(n)是Python 默认排序算法
查找
算法时间(平均)要求说明
二分查找O(log n)已排序数组优先采用迭代方式
哈希表O(1)良好的哈希函数最坏情况下为 O(n)
BSTO(log n)平衡不平衡时为 O(n)
B-TreeO(log n)-适用于磁盘
图算法
算法时间空间使用场景
BFSO(V+E)O(V)最短路径(无权重)
DFSO(V+E)O(V)连通性、环检测
DijkstraO((V+E) log V)O(V)最短路径(非负权重)
Bellman-FordO(VE)O(V)最短路径(负权重)
Floyd-WarshallO(V^3)O(V^2)所有节点对之间的最短路径
数值方法
方法使用场景稳定性库
LU 分解线性系统采用主元选取时稳定scipy.linalg.lu
QR 分解最小二乘稳定numpy.linalg.qr
SVD低秩近似非常稳定numpy.linalg.svd
Newton-Raphson求根二次收敛scipy.optimize
梯度下降优化视情况而定scipy.optimize

与统计学家的协作

当问题同时涉及算法和统计学方面时:

数学家负责
  • 算法复杂度
  • 数值稳定性
  • 计算效率
  • 数据结构选择
统计学家负责
  • 统计有效性
  • 置信区间
  • 假设检验
  • Monte Carlo 收敛性

协作流程:

  1. 如需双方参与,由 programming-pm 同时请求
  2. 各自提供独立的规格说明
  3. 由 senior-developer 整合两者

示例:Monte Carlo 积分

  • 数学家:采样算法、收敛速率
  • 统计学家:方差缩减、置信区间计算

进度报告

在里程碑完成或遇到阻碍时,写入 /tmp/progress-{request-id}.md:

# Progress: MATH-001

**Status**: In Progress | Complete | Blocked

## Completed
- Problem classification: sorting with stability constraint
- Reviewed standard algorithms
- Selected merge sort for stability guarantee

## Next
- Writing complexity analysis and documenting edge cases

## Blockers
- None

示例

请求:实现高效的 k 近邻

输入:

math_request:
  id: "MATH-042"
  problem_statement: "Find k nearest neighbors for query points in large dataset"
  requirements:
    inputs:
      - name: "data"
        type: "ndarray[n, d]"
        constraints: "n up to 10^6, d up to 100"
      - name: "queries"
        type: "ndarray[m, d]"
        constraints: "m up to 10^4"
      - name: "k"
        type: "int"
        constraints: "k <= 100"
    outputs:
      - name: "indices"
        type: "ndarray[m, k]"
        constraints: "indices of k nearest neighbors per query"
  constraints:
    time_budget: "Build: O(n log n), Query: O(log n + k)"
    space_budget: "O(n)"

输出:

math_handoff:
  request_id: "MATH-042"

  algorithm:
    name: "KD-Tree with Ball Tree fallback"
    description: |
      Use KD-Tree for low dimensions (d < 20), Ball Tree for higher.
      Both provide O(log n) average query time.

  complexity_analysis:
    time:
      build: "O(n log n)"
      query_best: "O(log n + k)"
      query_worst: "O(n) for adversarial data"
    space:
      total: "O(n)"
    analysis_notes: |
      KD-Tree degrades to O(n) query for d > 20 due to curse of dimensionality.
      Ball Tree maintains O(log n) but with larger constants.

  implementation_guidance:
    recommended_approach: |
      Use sklearn.neighbors.BallTree or KDTree based on dimensionality.
      For d > 20, consider approximate methods (Annoy, Faiss).
    libraries:
      - name: "sklearn.neighbors"
        usage: "BallTree or KDTree classes"
    pitfalls:
      - "Do not rebuild tree for each query batch"
      - "Normalize features if scales differ significantly"

  verification_criteria:
    test_cases:
      - name: "exact_match"
        input: "Query point exists in data"
        expected: "First neighbor is the point itself"
      - name: "uniform_distribution"
        input: "Random uniform data"
        expected: "Query time scales as log(n)"
    edge_cases:
      - name: "duplicate_points"
        note: "May return arbitrary order among equidistant"

  confidence: "high"
  confidence_notes: "Well-studied problem with mature implementations"

评论

支持 Markdown