mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
hanzoskills.com/llms.txt now opens with the install line, the API reference index and the company index, so an agent that lands on any one of the three can find the rest. robots.txt claimed 537 documents while llms.txt counts 920. The number is gone from robots.txt; llms.txt derives it. public/ is regenerated from scripts/site.py, which it had fallen behind: the copy-prompt menu and agent bar were in the generator and not in the committed pages. tests/site.mjs passes against the result. Co-authored-by: Hanzo Dev <[email protected]>
779 lines
32 KiB
HTML
779 lines
32 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>math-numerical-methods</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="alternate" type="text/markdown" href="/math-numerical-methods.md">
|
|
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/math-numerical-methods">
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/math-numerical-methods.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>math-numerical-methods</h1><p class="lede">Solving ordinary differential equations (ODEs)</p><h1>Numerical Methods</h1>
|
|
<h2>Metadata</h2>
|
|
<ul><li><strong>Skill ID</strong>: numerical-methods</li><li><strong>Category</strong>: Mathematics/Numerical Analysis</li><li><strong>Complexity</strong>: Advanced</li><li><strong>Last Updated</strong>: 2025-10-18</li></ul>
|
|
<h2>Overview</h2>
|
|
<p>Numerical analysis methods for solving differential equations, integration, root finding, interpolation, and numerical differentiation using SciPy and NumPy. Core techniques for scientific computing and engineering simulations.</p>
|
|
<h2>When to Use This Skill</h2>
|
|
<ul><li>Solving ordinary differential equations (ODEs)</li><li>Solving partial differential equations (PDEs)</li><li>Numerical integration (quadrature)</li><li>Root finding and equation solving</li><li>Function interpolation and approximation</li><li>Numerical differentiation</li><li>Initial value and boundary value problems</li></ul>
|
|
<h2>When NOT to Use This Skill</h2>
|
|
<ul><li>Symbolic mathematics (use SymPy)</li><li>When analytical solution is available</li><li>Discrete/combinatorial problems</li><li>Statistical inference (use probability-statistics.md)</li><li>Pure optimization (use optimization-algorithms.md)</li></ul>
|
|
<h2>Prerequisites</h2>
|
|
<ul><li>Calculus (derivatives, integrals, differential equations)</li><li>Linear algebra basics</li><li>NumPy array operations</li><li>Understanding of numerical stability</li></ul>
|
|
<h2>Core Concepts</h2>
|
|
<h3>1. Solving Ordinary Differential Equations (ODEs)</h3>
|
|
<p><strong>Initial Value Problems (IVP)</strong></p>
|
|
<pre><code class="lang-python">import numpy as np
|
|
from scipy.integrate import solve_ivp, odeint
|
|
import matplotlib.pyplot as plt
|
|
|
|
# Example: Exponential decay dy/dt = -k*y, y(0) = y0
|
|
def exponential_decay(t, y, k=0.5):
|
|
"""
|
|
ODE function in form dy/dt = f(t, y)
|
|
For solve_ivp: signature is f(t, y, *args)
|
|
"""
|
|
return -k * y
|
|
|
|
# Initial condition
|
|
y0 = [1.0]
|
|
t_span = (0, 10)
|
|
t_eval = np.linspace(0, 10, 100)
|
|
|
|
# Solve using RK45 (default, adaptive Runge-Kutta)
|
|
solution = solve_ivp(
|
|
exponential_decay,
|
|
t_span,
|
|
y0,
|
|
t_eval=t_eval,
|
|
method='RK45', # Explicit Runge-Kutta (4,5)
|
|
dense_output=True # For continuous solution
|
|
)
|
|
|
|
print(f"Success: {solution.success}")
|
|
print(f"Message: {solution.message}")
|
|
|
|
# Analytical solution for comparison
|
|
y_exact = y0[0] * np.exp(-0.5 * t_eval)
|
|
error = np.abs(solution.y[0] - y_exact).max()
|
|
print(f"Max error: {error:.2e}")
|
|
|
|
# System of ODEs: Lotka-Volterra (predator-prey)
|
|
def lotka_volterra(t, z, alpha=1.0, beta=0.1, delta=0.075, gamma=1.5):
|
|
"""
|
|
Predator-prey model:
|
|
dx/dt = alpha*x - beta*x*y (prey)
|
|
dy/dt = delta*x*y - gamma*y (predator)
|
|
"""
|
|
x, y = z
|
|
return [
|
|
alpha * x - beta * x * y,
|
|
delta * x * y - gamma * y
|
|
]
|
|
|
|
z0 = [10, 5] # Initial populations
|
|
solution_lv = solve_ivp(
|
|
lotka_volterra,
|
|
(0, 50),
|
|
z0,
|
|
t_eval=np.linspace(0, 50, 1000),
|
|
method='RK45'
|
|
)</code></pre>
|
|
<p><strong>Available ODE Solvers</strong></p>
|
|
<pre><code class="lang-python"># Explicit methods (non-stiff):
|
|
# - 'RK45': Explicit Runge-Kutta (4,5) - default, good general purpose
|
|
# - 'RK23': Explicit Runge-Kutta (2,3) - faster, less accurate
|
|
# - 'DOP853': Explicit Runge-Kutta (8,5,3) - high accuracy
|
|
|
|
# Implicit methods (stiff equations):
|
|
# - 'Radau': Implicit Runge-Kutta (5th order)
|
|
# - 'BDF': Backward Differentiation Formula
|
|
# - 'LSODA': Automatic stiffness detection and switching
|
|
|
|
# Example: Stiff equation (van der Pol oscillator)
|
|
def van_der_pol(t, y, mu=1000):
|
|
"""Stiff for large mu"""
|
|
return [y[1], mu * (1 - y[0]**2) * y[1] - y[0]]
|
|
|
|
# Use stiff solver
|
|
solution_stiff = solve_ivp(
|
|
van_der_pol,
|
|
(0, 3000),
|
|
[2.0, 0.0],
|
|
method='Radau', # Implicit method for stiff problems
|
|
rtol=1e-6,
|
|
atol=1e-9
|
|
)</code></pre>
|
|
<p><strong>ODE Events (Finding Crossings)</strong></p>
|
|
<pre><code class="lang-python">def projectile(t, y, g=9.81):
|
|
"""Projectile motion: y = [x, vx, z, vz]"""
|
|
return [y[1], 0, y[3], -g]
|
|
|
|
def hit_ground(t, y):
|
|
"""Event: projectile hits ground (z=0)"""
|
|
return y[2] # z-coordinate
|
|
|
|
hit_ground.terminal = True # Stop integration when event occurs
|
|
hit_ground.direction = -1 # Only detect decreasing (falling)
|
|
|
|
y0 = [0, 10, 0, 10] # x, vx, z, vz
|
|
solution = solve_ivp(
|
|
projectile,
|
|
(0, 10),
|
|
y0,
|
|
events=hit_ground,
|
|
dense_output=True
|
|
)
|
|
|
|
if solution.t_events[0].size > 0:
|
|
t_impact = solution.t_events[0][0]
|
|
print(f"Projectile hits ground at t={t_impact:.4f}s")</code></pre>
|
|
<p><strong>Alternative: odeint (Legacy Interface)</strong></p>
|
|
<pre><code class="lang-python"># odeint uses different signature: f(y, t, *args)
|
|
def exponential_decay_odeint(y, t, k=0.5):
|
|
return -k * y
|
|
|
|
t = np.linspace(0, 10, 100)
|
|
y = odeint(exponential_decay_odeint, y0, t)</code></pre>
|
|
<h3>2. Numerical Integration (Quadrature)</h3>
|
|
<p><strong>Single Integrals</strong></p>
|
|
<pre><code class="lang-python">from scipy.integrate import quad, fixed_quad, simpson, trapezoid
|
|
|
|
# Example: Integrate f(x) = x^2 from 0 to 1
|
|
def f(x):
|
|
return x**2
|
|
|
|
# Adaptive quadrature (most accurate)
|
|
result, error = quad(f, 0, 1)
|
|
print(f"Integral: {result:.10f}, Error estimate: {error:.2e}")
|
|
# Exact: 1/3 = 0.333333...
|
|
|
|
# With parameters
|
|
def gaussian(x, mu, sigma):
|
|
return np.exp(-0.5 * ((x - mu) / sigma)**2) / (sigma * np.sqrt(2 * np.pi))
|
|
|
|
integral, _ = quad(gaussian, -np.inf, np.inf, args=(0, 1))
|
|
print(f"Gaussian integral: {integral:.10f}") # Should be 1.0
|
|
|
|
# Vector-valued integrands
|
|
def vector_f(x):
|
|
return [x**2, x**3, np.sin(x)]
|
|
|
|
result, _ = quad(vector_f, 0, np.pi, full_output=False, vec_func=True)
|
|
|
|
# Fixed-order Gaussian quadrature (faster, less accurate)
|
|
result_fixed, _ = fixed_quad(f, 0, 1, n=5) # 5-point Gauss-Legendre
|
|
|
|
# Composite rules (for tabulated data)
|
|
x = np.linspace(0, 1, 101)
|
|
y = x**2
|
|
integral_trap = trapezoid(y, x) # Trapezoidal rule
|
|
integral_simp = simpson(y, x) # Simpson's rule (more accurate)</code></pre>
|
|
<p><strong>Multiple Integrals</strong></p>
|
|
<pre><code class="lang-python">from scipy.integrate import dblquad, tplquad, nquad
|
|
|
|
# Double integral: ∫∫ x*y dx dy over [0,1] x [0,1]
|
|
def integrand_2d(y, x):
|
|
return x * y
|
|
|
|
result, error = dblquad(integrand_2d, 0, 1, 0, 1)
|
|
print(f"Double integral: {result:.6f}") # Exact: 0.25
|
|
|
|
# Variable limits: ∫∫ f(x,y) dy dx where y from 0 to x
|
|
def integrand(y, x):
|
|
return x * y
|
|
|
|
result, _ = dblquad(integrand, 0, 1, lambda x: 0, lambda x: x)
|
|
print(f"Result: {result:.6f}") # 1/6
|
|
|
|
# Triple integral: volume of sphere
|
|
def sphere_integrand(phi, theta, r):
|
|
return r**2 * np.sin(theta)
|
|
|
|
R = 1.0
|
|
result, _ = tplquad(
|
|
sphere_integrand,
|
|
0, R, # r from 0 to R
|
|
lambda r: 0, lambda r: np.pi, # theta from 0 to pi
|
|
lambda r, theta: 0, lambda r, theta: 2*np.pi # phi from 0 to 2pi
|
|
)
|
|
print(f"Sphere volume: {result:.6f}") # 4/3 * pi * R^3
|
|
|
|
# n-dimensional integrals
|
|
def nd_integrand(x):
|
|
"""Integral over n-dimensional unit cube"""
|
|
return np.prod(x) # x[0] * x[1] * ... * x[n-1]
|
|
|
|
ranges = [(0, 1)] * 4 # 4D integral
|
|
result, _ = nquad(nd_integrand, ranges)</code></pre>
|
|
<p><strong>Special Integrals</strong></p>
|
|
<pre><code class="lang-python">from scipy.special import erf
|
|
from scipy.integrate import cumulative_trapezoid
|
|
|
|
# Cumulative integral (indefinite integral at points)
|
|
x = np.linspace(0, 2*np.pi, 100)
|
|
y = np.sin(x)
|
|
cumulative = cumulative_trapezoid(y, x, initial=0)
|
|
# cumulative[i] ≈ ∫_0^x[i] sin(t) dt = 1 - cos(x[i])
|
|
|
|
# Monte Carlo integration (high dimensions)
|
|
def monte_carlo_integrate(f, bounds, n_samples=100000):
|
|
"""
|
|
Estimate integral of f over hypercube defined by bounds.
|
|
bounds: [(a1,b1), (a2,b2), ..., (an,bn)]
|
|
"""
|
|
dim = len(bounds)
|
|
samples = np.random.uniform(
|
|
[b[0] for b in bounds],
|
|
[b[1] for b in bounds],
|
|
size=(n_samples, dim)
|
|
)
|
|
|
|
values = np.array([f(s) for s in samples])
|
|
volume = np.prod([b[1] - b[0] for b in bounds])
|
|
|
|
return volume * values.mean(), volume * values.std() / np.sqrt(n_samples)
|
|
|
|
# Example: 10D integral of sum(x_i^2)
|
|
def high_dim_f(x):
|
|
return np.sum(x**2)
|
|
|
|
result, error = monte_carlo_integrate(high_dim_f, [(0, 1)] * 10, n_samples=1000000)
|
|
print(f"10D integral: {result:.4f} ± {error:.4f}")</code></pre>
|
|
<h3>3. Root Finding</h3>
|
|
<p><strong>Scalar Root Finding</strong></p>
|
|
<pre><code class="lang-python">from scipy.optimize import root_scalar, brentq, newton
|
|
|
|
# Example: Find x where f(x) = x^3 - 2x - 5 = 0
|
|
def f(x):
|
|
return x**3 - 2*x - 5
|
|
|
|
def df(x):
|
|
return 3*x**2 - 2
|
|
|
|
# Brent's method (bracketing, no derivative needed)
|
|
result = root_scalar(f, bracket=[0, 3], method='brentq')
|
|
print(f"Root: {result.root:.10f}, Iterations: {result.iterations}")
|
|
|
|
# Newton-Raphson (needs derivative, faster convergence)
|
|
result_newton = root_scalar(f, x0=2, fprime=df, method='newton')
|
|
print(f"Root (Newton): {result_newton.root:.10f}")
|
|
|
|
# Secant method (approximates derivative)
|
|
result_secant = root_scalar(f, x0=2, x1=3, method='secant')
|
|
|
|
# Direct functions for common methods
|
|
root_brent = brentq(f, 0, 3) # Same as brentq method
|
|
root_newton = newton(f, 2, fprime=df) # Newton's method</code></pre>
|
|
<p><strong>Multi-Dimensional Root Finding</strong></p>
|
|
<pre><code class="lang-python">from scipy.optimize import root, fsolve
|
|
|
|
# System of nonlinear equations
|
|
def equations(z):
|
|
x, y = z
|
|
return [
|
|
x**2 + y**2 - 4, # Circle: x^2 + y^2 = 4
|
|
x - y - 1 # Line: x - y = 1
|
|
]
|
|
|
|
def jacobian(z):
|
|
x, y = z
|
|
return [
|
|
[2*x, 2*y],
|
|
[1, -1]
|
|
]
|
|
|
|
# Newton-like methods
|
|
result = root(equations, [1, 1], jac=jacobian, method='hybr')
|
|
print(f"Solution: {result.x}")
|
|
print(f"Residual: {equations(result.x)}")
|
|
|
|
# Alternative: fsolve (older interface)
|
|
solution = fsolve(equations, [1, 1])
|
|
|
|
# Verify solutions
|
|
x, y = solution
|
|
assert np.isclose(x**2 + y**2, 4)
|
|
assert np.isclose(x - y, 1)</code></pre>
|
|
<h3>4. Interpolation</h3>
|
|
<p><strong>1D Interpolation</strong></p>
|
|
<pre><code class="lang-python">from scipy.interpolate import interp1d, CubicSpline, PchipInterpolator
|
|
|
|
# Data points
|
|
x = np.array([0, 1, 2, 3, 4, 5])
|
|
y = np.array([0, 1, 4, 9, 16, 25]) # y = x^2
|
|
|
|
# Linear interpolation
|
|
f_linear = interp1d(x, y, kind='linear')
|
|
|
|
# Cubic spline (smooth)
|
|
f_cubic = interp1d(x, y, kind='cubic')
|
|
|
|
# Natural cubic spline (better extrapolation)
|
|
cs = CubicSpline(x, y)
|
|
|
|
# PCHIP (preserves monotonicity, no overshooting)
|
|
pchip = PchipInterpolator(x, y)
|
|
|
|
# Evaluate
|
|
x_new = np.linspace(0, 5, 100)
|
|
y_linear = f_linear(x_new)
|
|
y_cubic = f_cubic(x_new)
|
|
y_cs = cs(x_new)
|
|
y_pchip = pchip(x_new)
|
|
|
|
# Extrapolation
|
|
f_extrap = interp1d(x, y, kind='cubic', fill_value='extrapolate')
|
|
y_extrap = f_extrap(6.0) # Extrapolate beyond data range</code></pre>
|
|
<p><strong>Multi-Dimensional Interpolation</strong></p>
|
|
<pre><code class="lang-python">from scipy.interpolate import RegularGridInterpolator, griddata
|
|
|
|
# 2D interpolation on regular grid
|
|
x = np.linspace(0, 5, 6)
|
|
y = np.linspace(0, 5, 6)
|
|
X, Y = np.meshgrid(x, y, indexing='ij')
|
|
Z = X**2 + Y**2
|
|
|
|
# Regular grid interpolator
|
|
interp_2d = RegularGridInterpolator((x, y), Z)
|
|
|
|
# Evaluate at new points
|
|
points = np.array([[1.5, 2.5], [3.2, 4.1]])
|
|
values = interp_2d(points)
|
|
|
|
# Irregular (scattered) data interpolation
|
|
np.random.seed(0)
|
|
points_irreg = np.random.rand(100, 2) * 5
|
|
values_irreg = points_irreg[:, 0]**2 + points_irreg[:, 1]**2
|
|
|
|
# Create regular grid
|
|
xi = np.linspace(0, 5, 50)
|
|
yi = np.linspace(0, 5, 50)
|
|
XI, YI = np.meshgrid(xi, yi)
|
|
|
|
# Interpolate
|
|
ZI = griddata(points_irreg, values_irreg, (XI, YI), method='cubic')</code></pre>
|
|
<h3>5. Numerical Differentiation</h3>
|
|
<p><strong>Finite Differences</strong></p>
|
|
<pre><code class="lang-python">from scipy.misc import derivative
|
|
|
|
# Forward difference: f'(x) ≈ (f(x+h) - f(x)) / h
|
|
def forward_diff(f, x, h=1e-5):
|
|
return (f(x + h) - f(x)) / h
|
|
|
|
# Central difference: f'(x) ≈ (f(x+h) - f(x-h)) / (2h)
|
|
def central_diff(f, x, h=1e-5):
|
|
return (f(x + h) - f(x - h)) / (2 * h)
|
|
|
|
# SciPy's derivative (adaptive)
|
|
def f(x):
|
|
return np.sin(x)
|
|
|
|
x = np.pi / 4
|
|
df_numerical = derivative(f, x, dx=1e-5)
|
|
df_exact = np.cos(x)
|
|
print(f"Numerical: {df_numerical:.10f}")
|
|
print(f"Exact: {df_exact:.10f}")
|
|
print(f"Error: {abs(df_numerical - df_exact):.2e}")
|
|
|
|
# Higher-order derivatives
|
|
d2f = derivative(f, x, dx=1e-5, n=2) # Second derivative
|
|
d2f_exact = -np.sin(x)
|
|
|
|
# Gradient (multi-dimensional)
|
|
from scipy.optimize import approx_fprime
|
|
|
|
def g(x):
|
|
return x[0]**2 + x[1]**2 + x[2]**2
|
|
|
|
x0 = np.array([1.0, 2.0, 3.0])
|
|
grad_numerical = approx_fprime(x0, g, epsilon=1e-8)
|
|
grad_exact = 2 * x0
|
|
print(f"Gradient error: {np.linalg.norm(grad_numerical - grad_exact):.2e}")</code></pre>
|
|
<p><strong>Numerical Jacobian and Hessian</strong></p>
|
|
<pre><code class="lang-python">from scipy.optimize import approx_fprime
|
|
|
|
def vector_function(x):
|
|
"""f: R^n -> R^m"""
|
|
return np.array([
|
|
x[0]**2 + x[1],
|
|
x[0] * x[1],
|
|
x[1]**2
|
|
])
|
|
|
|
def jacobian_finite_diff(f, x, epsilon=1e-8):
|
|
"""Compute Jacobian using finite differences"""
|
|
n = len(x)
|
|
f0 = f(x)
|
|
m = len(f0)
|
|
J = np.zeros((m, n))
|
|
|
|
for i in range(n):
|
|
x_plus = x.copy()
|
|
x_plus[i] += epsilon
|
|
J[:, i] = (f(x_plus) - f0) / epsilon
|
|
|
|
return J
|
|
|
|
x = np.array([1.0, 2.0])
|
|
J = jacobian_finite_diff(vector_function, x)
|
|
|
|
# Hessian (second derivatives)
|
|
from scipy.optimize import approx_fprime
|
|
|
|
def hessian_finite_diff(f, x, epsilon=1e-5):
|
|
"""Compute Hessian using finite differences"""
|
|
n = len(x)
|
|
H = np.zeros((n, n))
|
|
|
|
f0 = f(x)
|
|
grad = approx_fprime(x, f, epsilon)
|
|
|
|
for i in range(n):
|
|
x_plus = x.copy()
|
|
x_plus[i] += epsilon
|
|
grad_plus = approx_fprime(x_plus, f, epsilon)
|
|
H[:, i] = (grad_plus - grad) / epsilon
|
|
|
|
# Symmetrize
|
|
H = (H + H.T) / 2
|
|
return H</code></pre>
|
|
<h3>6. Boundary Value Problems (BVP)</h3>
|
|
<p><strong>Solving BVPs with solve_bvp</strong></p>
|
|
<pre><code class="lang-python">from scipy.integrate import solve_bvp
|
|
|
|
# Example: y'' = -y, y(0) = 0, y(pi) = 0
|
|
# Convert to first-order system: y1 = y, y2 = y'
|
|
# y1' = y2, y2' = -y1
|
|
|
|
def bvp_ode(x, y):
|
|
"""System: [y1', y2'] as function of x and [y1, y2]"""
|
|
return np.vstack([y[1], -y[0]])
|
|
|
|
def boundary_conditions(ya, yb):
|
|
"""Residuals at boundaries: should be zero"""
|
|
return np.array([ya[0], yb[0]]) # y(0) = 0, y(pi) = 0
|
|
|
|
# Initial mesh
|
|
x = np.linspace(0, np.pi, 5)
|
|
y_guess = np.zeros((2, x.size))
|
|
y_guess[0] = np.sin(x) # Initial guess for y
|
|
y_guess[1] = np.cos(x) # Initial guess for y'
|
|
|
|
solution = solve_bvp(bvp_ode, boundary_conditions, x, y_guess)
|
|
|
|
# Evaluate on fine mesh
|
|
x_plot = np.linspace(0, np.pi, 100)
|
|
y_plot = solution.sol(x_plot)[0]
|
|
|
|
# Exact solution: y = C * sin(x)</code></pre>
|
|
<h2>Patterns and Best Practices</h2>
|
|
<h3>Pattern 1: Error Estimation</h3>
|
|
<pre><code class="lang-python">def richardson_extrapolation(f, x, h, order=2):
|
|
"""
|
|
Improve finite difference accuracy using Richardson extrapolation.
|
|
"""
|
|
D1 = (f(x + h) - f(x - h)) / (2 * h)
|
|
D2 = (f(x + h/2) - f(x - h/2)) / h
|
|
|
|
# Richardson extrapolation
|
|
D_improved = (2**order * D2 - D1) / (2**order - 1)
|
|
return D_improved</code></pre>
|
|
<h3>Pattern 2: Adaptive Step Size</h3>
|
|
<pre><code class="lang-python">def adaptive_integration(f, a, b, tol=1e-6):
|
|
"""
|
|
Adaptive Simpson's rule: subdivide where error is large.
|
|
"""
|
|
def simpson_rule(f, a, b):
|
|
h = (b - a) / 2
|
|
return h/3 * (f(a) + 4*f(a+h) + f(b))
|
|
|
|
def adaptive_simpson(a, b, tol, whole):
|
|
c = (a + b) / 2
|
|
left = simpson_rule(f, a, c)
|
|
right = simpson_rule(f, c, b)
|
|
|
|
if abs(left + right - whole) < 15 * tol:
|
|
return left + right
|
|
else:
|
|
return (adaptive_simpson(a, c, tol/2, left) +
|
|
adaptive_simpson(c, b, tol/2, right))
|
|
|
|
whole = simpson_rule(f, a, b)
|
|
return adaptive_simpson(a, b, tol, whole)</code></pre>
|
|
<h3>Pattern 3: Stability Analysis</h3>
|
|
<pre><code class="lang-python">def check_stiffness(f, t_span, y0, threshold=100):
|
|
"""
|
|
Estimate stiffness ratio of ODE system.
|
|
"""
|
|
from scipy.linalg import eigvals
|
|
|
|
# Jacobian at initial point
|
|
def jacobian_fd(t, y):
|
|
n = len(y)
|
|
J = np.zeros((n, n))
|
|
eps = 1e-8
|
|
f0 = f(t, y)
|
|
for i in range(n):
|
|
y_plus = y.copy()
|
|
y_plus[i] += eps
|
|
J[:, i] = (f(t, y_plus) - f0) / eps
|
|
return J
|
|
|
|
J = jacobian_fd(t_span[0], y0)
|
|
eigs = eigvals(J)
|
|
|
|
stiffness_ratio = abs(max(eigs.real) / min(eigs.real))
|
|
|
|
if stiffness_ratio > threshold:
|
|
print(f"System is stiff (ratio: {stiffness_ratio:.2e})")
|
|
print("Use implicit solver: 'Radau' or 'BDF'")
|
|
else:
|
|
print("System is non-stiff")
|
|
print("Use explicit solver: 'RK45' or 'DOP853'")
|
|
|
|
return stiffness_ratio</code></pre>
|
|
<h2>Quick Reference</h2>
|
|
<h3>ODE Solver Selection</h3>
|
|
<pre><code class="lang-python"># Non-stiff: RK45 (default), DOP853 (high accuracy)
|
|
# Stiff: Radau, BDF
|
|
# Auto-switching: LSODA
|
|
# rtol, atol: relative and absolute tolerances (default: 1e-3, 1e-6)</code></pre>
|
|
<h3>Integration Method Selection</h3>
|
|
<pre><code class="lang-python"># Smooth functions: quad (adaptive)
|
|
# Tabulated data: simpson (accurate), trapezoid (simple)
|
|
# High dimensions: Monte Carlo
|
|
# Infinite limits: quad with ±np.inf</code></pre>
|
|
<h2>Anti-Patterns</h2>
|
|
<h3>Anti-Pattern 1: Too Large Step Size</h3>
|
|
<pre><code class="lang-python"># WRONG: Using fixed large step in finite differences
|
|
h = 0.1
|
|
df = (f(x + h) - f(x)) / h # Large error!
|
|
|
|
# RIGHT: Use small h or adaptive methods
|
|
from scipy.misc import derivative
|
|
df = derivative(f, x, dx=1e-5)</code></pre>
|
|
<h3>Anti-Pattern 2: Ignoring Stiffness</h3>
|
|
<pre><code class="lang-python"># WRONG: Using explicit method for stiff ODE
|
|
solution = solve_ivp(stiff_ode, t_span, y0, method='RK45') # Will be slow!
|
|
|
|
# RIGHT: Use implicit method
|
|
solution = solve_ivp(stiff_ode, t_span, y0, method='Radau')</code></pre>
|
|
<h3>Anti-Pattern 3: Poor Interpolation Choice</h3>
|
|
<pre><code class="lang-python"># WRONG: Cubic spline for non-smooth data
|
|
f = interp1d(x, y, kind='cubic') # May oscillate!
|
|
|
|
# RIGHT: Use PCHIP for monotonic data
|
|
from scipy.interpolate import PchipInterpolator
|
|
f = PchipInterpolator(x, y) # No overshooting</code></pre>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><code>linear-algebra-computation.md</code> - Linear solvers for numerical methods</li><li><code>optimization-algorithms.md</code> - Optimization for parameter fitting</li><li><code>probability-statistics.md</code> - Statistical analysis of numerical results</li></ul>
|
|
<h2>Learning Resources</h2>
|
|
<ul><li>SciPy Documentation: https://docs.scipy.org/doc/scipy/reference/</li><li>Numerical Recipes (Press et al.)</li><li>Numerical Methods for Engineers (Chapra & Canale)</li></ul>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/math-numerical-methods.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |