agentleFS
orange4664orange4664/research-skills/formula2code/SKILL.md

formula2code

Skill41 starsChanged 8 months agoMITView on GitHub

Convert LaTeX math formulas from papers into executable PyTorch/NumPy code.

  • Installs packages

The file

We split this skill into the rules it gives an agent

6 rules. About 0 are common, 4 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When a LaTeX formula matches a known pattern, the skill returns the exact PyTorch API call instead of generating code from scratch.

    line 62
  2. Distinctive:

    When encountering issues, the agent should fetch the relevant docs:

    line 159
  3. Distinctive:

    Check supported syntax at the official repo

    line 164
  4. Very specific:

    Solution: Use extra_funcs to provide a custom PyTorch implementation.

    line 176
  5. Very specific:

    Solution: Use the modules parameter.

    line 199
  6. Distinctive:

    When stuck on any issue, the agent should fetch these URLs:

    line 215

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Formula2Code Skill

Purpose

Convert mathematical equations from research papers into runnable code. This skill bridges the gap between paper reading (LaTeX formulas) and implementation (PyTorch/NumPy).

When to Use

  • Paper has mathematical formulas that need to be implemented
  • User asks to "convert this equation to code" or "implement this loss function"
  • During code-writer workflow: auto-convert extracted formulas into loss.py / model.py
  • Verifying that a manual implementation matches the paper's formula

Architecture (3-Layer Pipeline)

LaTeX input
    │
    ▼
Layer 1: ML Pattern Matcher ─── Known formula? ──→ Direct PyTorch API mapping
    │ (no match)                                    (softmax, cross_entropy, etc.)
    ▼
Layer 2: latex2sympy2 ────────── Parse to SymPy symbolic expression
    │
    ▼
Layer 3: Code Generator ─────── sympytorch (nn.Module) / lambdify / pycode
    │
    ▼
Layer 4: Validator ──────────── Cross-check SymPy vs NumPy vs PyTorch

Quick Start

Single Formula

python formula2code/convert.py "\frac{1}{N}\sum_{i=1}^{N}(y_i - \hat{y}_i)^2" --to pytorch -v

From Paper JSON (paper-parser output)

python formula2code/convert.py --from-paper workspace/<paper>/paper_content.json --to pytorch -o formulas.json

List Known Patterns

python formula2code/convert.py --list-patterns

Built-in ML Pattern Library (16 patterns)

CategoryPatterns
LossMSE, Cross Entropy, Binary CE, KL Divergence, L1, Huber
ActivationSoftmax, Sigmoid, ReLU, GELU, Tanh
AttentionScaled Dot-Product Attention
NormalizationLayer Norm, Batch Norm
DistributionGaussian/Normal

When a LaTeX formula matches a known pattern, the skill returns the exact PyTorch API call instead of generating code from scratch. This is more reliable for standard operations.

Output Format

For each formula, the converter outputs:

{
  "input_latex": "\\frac{1}{N}\\sum...",
  "method": "pattern_match | sympy_pipeline | failed",
  "outputs": {
    "pytorch_functional": "F.mse_loss(predictions, targets)",
    "pytorch_class": "nn.MSELoss()",
    "pytorch_code": "import torch\ndef formula(N, y, y_hat): ...",
    "numpy_code": "import numpy as np\ndef formula(N, y, y_hat): ...",
    "python_code": "import math\ndef formula(N, y, y_hat): ..."
  },
  "validation": {"passed": true, "summary": "5/5 tests passed"}
}

Integration with Other Skills

With paper-parser

paper-parser outputs JSON with LaTeX formulas
    → formula2code reads them and generates code

With code-writer

code-writer extracts equations from paper
    → calls formula2code to generate loss.py / model.py components
    → inserts generated code into project scaffolding

Examples

See formula2code/examples/ for runnable demos:

ExampleWhat it Shows
01_basic_usage.py3-layer pipeline: pattern match → SymPy → code generation
02_official_library_usage.pyDirect usage of latex2sympy2, sympytorch, lambdify
03_trainable_formula.pyLearn formula coefficients via gradient descent

Quick Example: Pattern Match

from converters.ml_patterns import match_ml_pattern

result = match_ml_pattern(r"\text{softmax}(z)")
# → {'name': 'softmax', 'pytorch_functional': 'F.softmax(x, dim=-1)', ...}

Quick Example: SymPy Pipeline

from converters.latex_parser import parse_latex
from converters.to_python import sympy_to_python_code

expr, meta = parse_latex(r"x^2 + 2x + 1")
code = sympy_to_python_code(expr)
# → "def formula(x):\n    return x**2 + 2*x + 1"

Quick Example: sympytorch (from official README)

import sympy, torch, sympytorch

x = sympy.symbols('x_name')
cosx = 1.0 * sympy.cos(x)
sinx = 2.0 * sympy.sin(x)
mod = sympytorch.SymPyModule(expressions=[cosx, sinx])

x_ = torch.rand(3)
out = mod(x_name=x_)  # shape (3, 2)
# Floats (1.0, 2.0) become trainable nn.Parameters!

Quick Example: latex2sympy2 (from official README)

from latex2sympy2_extended import latex2sympy

latex2sympy(r"\frac{d}{dx}(x^{2}+x)")   # → Derivative(x**2 + x, x)
latex2sympy(r"\sum_{i = 1}^{n} i")        # → Sum(i, (i, 1, n))
latex2sympy(r"\int_{a}^{b} \frac{dt}{t}") # → Integral(1/t, (t, a, b))

Dependencies

pip install -r formula2code/requirements.txt
# Core: sympy
# LaTeX parsing: latex2sympy2_extended[antlr4_13_2]
# PyTorch modules: sympytorch

Troubleshooting & Where to Get Help

When encountering issues, the agent should fetch the relevant docs:

LaTeX Parsing Errors

Problem: latex2sympy fails to parse a formula. Solution:

  1. Check supported syntax at the official repo
  2. Preprocess the LaTeX (our latex_parser.py handles common ML notation like \text{}, \hat{})
  3. For very complex notation, manually simplify to standard LaTeX
# Fetch latest docs/issues:
fetch_url("https://github.com/huggingface/latex2sympy2_extended")
fetch_url("https://github.com/huggingface/latex2sympy2_extended/issues")

sympytorch Unsupported Operation

Problem: sympytorch throws NotImplementedError for a SymPy function. Solution: Use extra_funcs to provide a custom PyTorch implementation.

import sympy, sympytorch

# Check supported operations:
# https://github.com/patrick-kidger/sympytorch/blob/master/sympytorch/sympy_module.py#L12

mod = sympytorch.SymPyModule(
    expressions=[expr],
    extra_funcs={
        sympy.Function('my_custom_op'): lambda x: x.clamp(min=0),
    }
)
# Fetch supported ops list:
fetch_url("https://github.com/patrick-kidger/sympytorch/blob/master/sympytorch/sympy_module.py")

lambdify Module Selection

Problem: Need to generate code for a specific backend (torch/numpy/scipy). Solution: Use the modules parameter.

import sympy
f = sympy.lambdify([x], expr, modules='torch')    # → torch functions
f = sympy.lambdify([x], expr, modules='numpy')    # → numpy functions
f = sympy.lambdify([x], expr, modules='scipy')    # → scipy special
# Fetch lambdify docs:
fetch_url("https://docs.sympy.org/latest/modules/utilities/lambdify.html")

📚 Reference URLs (for agent self-help)

When stuck on any issue, the agent should fetch these URLs:

TopicURL
latex2sympy2 README + exampleshttps://github.com/huggingface/latex2sympy2_extended
latex2sympy2 issueshttps://github.com/huggingface/latex2sympy2_extended/issues
latex2sympy2 grammar (PS.g4)https://github.com/huggingface/latex2sympy2_extended/blob/master/src/latex2sympy2_extended/PS.g4
sympytorch READMEhttps://github.com/patrick-kidger/sympytorch
sympytorch supported opshttps://github.com/patrick-kidger/sympytorch/blob/master/sympytorch/sympy_module.py
sympytorch issueshttps://github.com/patrick-kidger/sympytorch/issues
SymPy lambdify docshttps://docs.sympy.org/latest/modules/utilities/lambdify.html
SymPy pycode docshttps://docs.sympy.org/latest/modules/printing.html
SymPy basic operationshttps://docs.sympy.org/latest/tutorials/intro-tutorial/basic_operations.html

Limitations

  • latex2sympy2 cannot parse all LaTeX notations (very complex nested expressions may fail)
  • sympytorch only supports a subset of SymPy operations (extend via extra_funcs)
  • Pattern matching is regex-based — unusual formatting of standard formulas may not match
  • For complex multi-line algorithm definitions, use code-writer instead

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.