omgkit 2.12.0 → 2.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -12
- package/package.json +2 -2
- package/plugin/agents/api-designer.md +5 -0
- package/plugin/agents/architect.md +8 -0
- package/plugin/agents/brainstormer.md +4 -0
- package/plugin/agents/cicd-manager.md +6 -0
- package/plugin/agents/code-reviewer.md +6 -0
- package/plugin/agents/copywriter.md +2 -0
- package/plugin/agents/data-engineer.md +255 -0
- package/plugin/agents/database-admin.md +10 -0
- package/plugin/agents/debugger.md +10 -0
- package/plugin/agents/devsecops.md +314 -0
- package/plugin/agents/docs-manager.md +4 -0
- package/plugin/agents/domain-decomposer.md +181 -0
- package/plugin/agents/embedded-systems.md +397 -0
- package/plugin/agents/fullstack-developer.md +12 -0
- package/plugin/agents/game-systems-designer.md +375 -0
- package/plugin/agents/git-manager.md +10 -0
- package/plugin/agents/journal-writer.md +2 -0
- package/plugin/agents/ml-engineer.md +284 -0
- package/plugin/agents/observability-engineer.md +353 -0
- package/plugin/agents/oracle.md +9 -0
- package/plugin/agents/performance-engineer.md +290 -0
- package/plugin/agents/pipeline-architect.md +6 -0
- package/plugin/agents/planner.md +12 -0
- package/plugin/agents/platform-engineer.md +325 -0
- package/plugin/agents/project-manager.md +3 -0
- package/plugin/agents/researcher.md +5 -0
- package/plugin/agents/scientific-computing.md +426 -0
- package/plugin/agents/scout.md +3 -0
- package/plugin/agents/security-auditor.md +7 -0
- package/plugin/agents/sprint-master.md +17 -0
- package/plugin/agents/tester.md +10 -0
- package/plugin/agents/ui-ux-designer.md +12 -0
- package/plugin/agents/vulnerability-scanner.md +6 -0
- package/plugin/commands/data/pipeline.md +47 -0
- package/plugin/commands/data/quality.md +49 -0
- package/plugin/commands/domain/analyze.md +34 -0
- package/plugin/commands/domain/map.md +41 -0
- package/plugin/commands/game/balance.md +56 -0
- package/plugin/commands/game/optimize.md +62 -0
- package/plugin/commands/iot/provision.md +58 -0
- package/plugin/commands/ml/evaluate.md +47 -0
- package/plugin/commands/ml/train.md +48 -0
- package/plugin/commands/perf/benchmark.md +54 -0
- package/plugin/commands/perf/profile.md +49 -0
- package/plugin/commands/platform/blueprint.md +56 -0
- package/plugin/commands/security/audit.md +54 -0
- package/plugin/commands/security/scan.md +55 -0
- package/plugin/commands/sre/dashboard.md +53 -0
- package/plugin/registry.yaml +711 -0
- package/plugin/skills/ai-ml/experiment-tracking/SKILL.md +338 -0
- package/plugin/skills/ai-ml/feature-stores/SKILL.md +340 -0
- package/plugin/skills/ai-ml/llm-ops/SKILL.md +454 -0
- package/plugin/skills/ai-ml/ml-pipelines/SKILL.md +390 -0
- package/plugin/skills/ai-ml/model-monitoring/SKILL.md +398 -0
- package/plugin/skills/ai-ml/model-serving/SKILL.md +386 -0
- package/plugin/skills/event-driven/cqrs-patterns/SKILL.md +348 -0
- package/plugin/skills/event-driven/event-sourcing/SKILL.md +334 -0
- package/plugin/skills/event-driven/kafka-deep/SKILL.md +252 -0
- package/plugin/skills/event-driven/saga-orchestration/SKILL.md +335 -0
- package/plugin/skills/event-driven/schema-registry/SKILL.md +328 -0
- package/plugin/skills/event-driven/stream-processing/SKILL.md +313 -0
- package/plugin/skills/game/game-audio/SKILL.md +446 -0
- package/plugin/skills/game/game-networking/SKILL.md +490 -0
- package/plugin/skills/game/godot-patterns/SKILL.md +413 -0
- package/plugin/skills/game/shader-programming/SKILL.md +492 -0
- package/plugin/skills/game/unity-patterns/SKILL.md +488 -0
- package/plugin/skills/iot/device-provisioning/SKILL.md +405 -0
- package/plugin/skills/iot/edge-computing/SKILL.md +369 -0
- package/plugin/skills/iot/industrial-protocols/SKILL.md +438 -0
- package/plugin/skills/iot/mqtt-deep/SKILL.md +418 -0
- package/plugin/skills/iot/ota-updates/SKILL.md +426 -0
- package/plugin/skills/microservices/api-gateway-patterns/SKILL.md +201 -0
- package/plugin/skills/microservices/circuit-breaker-patterns/SKILL.md +246 -0
- package/plugin/skills/microservices/contract-testing/SKILL.md +284 -0
- package/plugin/skills/microservices/distributed-tracing/SKILL.md +246 -0
- package/plugin/skills/microservices/service-discovery/SKILL.md +304 -0
- package/plugin/skills/microservices/service-mesh/SKILL.md +181 -0
- package/plugin/skills/mobile-advanced/mobile-ci-cd/SKILL.md +407 -0
- package/plugin/skills/mobile-advanced/mobile-security/SKILL.md +403 -0
- package/plugin/skills/mobile-advanced/offline-first/SKILL.md +473 -0
- package/plugin/skills/mobile-advanced/push-notifications/SKILL.md +494 -0
- package/plugin/skills/mobile-advanced/react-native-deep/SKILL.md +374 -0
- package/plugin/skills/simulation/numerical-methods/SKILL.md +434 -0
- package/plugin/skills/simulation/parallel-computing/SKILL.md +382 -0
- package/plugin/skills/simulation/physics-engines/SKILL.md +377 -0
- package/plugin/skills/simulation/validation-verification/SKILL.md +479 -0
- package/plugin/skills/simulation/visualization-scientific/SKILL.md +365 -0
- package/plugin/templates/autonomous/archetypes/event-driven-app.yaml +460 -0
- package/plugin/templates/autonomous/archetypes/microservices-app.yaml +431 -0
- package/plugin/templates/autonomous/state-schema.yaml +1 -1
- package/plugin/workflows/ai-engineering/agent-development.md +3 -3
- package/plugin/workflows/ai-engineering/fine-tuning.md +3 -3
- package/plugin/workflows/ai-engineering/model-evaluation.md +3 -3
- package/plugin/workflows/ai-engineering/prompt-engineering.md +2 -2
- package/plugin/workflows/ai-engineering/rag-development.md +4 -4
- package/plugin/workflows/ai-ml/data-pipeline.md +188 -0
- package/plugin/workflows/ai-ml/experiment-cycle.md +203 -0
- package/plugin/workflows/ai-ml/feature-engineering.md +208 -0
- package/plugin/workflows/ai-ml/model-deployment.md +199 -0
- package/plugin/workflows/ai-ml/monitoring-setup.md +227 -0
- package/plugin/workflows/api/api-design.md +1 -1
- package/plugin/workflows/api/api-testing.md +2 -2
- package/plugin/workflows/content/technical-docs.md +1 -1
- package/plugin/workflows/database/migration.md +1 -1
- package/plugin/workflows/database/optimization.md +1 -1
- package/plugin/workflows/database/schema-design.md +3 -3
- package/plugin/workflows/development/bug-fix.md +3 -3
- package/plugin/workflows/development/code-review.md +2 -1
- package/plugin/workflows/development/feature.md +3 -3
- package/plugin/workflows/development/refactor.md +2 -2
- package/plugin/workflows/event-driven/consumer-groups.md +190 -0
- package/plugin/workflows/event-driven/event-storming.md +172 -0
- package/plugin/workflows/event-driven/replay-testing.md +186 -0
- package/plugin/workflows/event-driven/saga-implementation.md +206 -0
- package/plugin/workflows/event-driven/schema-evolution.md +173 -0
- package/plugin/workflows/fullstack/authentication.md +4 -4
- package/plugin/workflows/fullstack/full-feature.md +4 -4
- package/plugin/workflows/game-dev/content-pipeline.md +218 -0
- package/plugin/workflows/game-dev/platform-submission.md +263 -0
- package/plugin/workflows/game-dev/playtesting.md +237 -0
- package/plugin/workflows/game-dev/prototype-to-production.md +205 -0
- package/plugin/workflows/microservices/contract-first.md +151 -0
- package/plugin/workflows/microservices/distributed-tracing.md +166 -0
- package/plugin/workflows/microservices/domain-decomposition.md +123 -0
- package/plugin/workflows/microservices/integration-testing.md +149 -0
- package/plugin/workflows/microservices/service-mesh-setup.md +153 -0
- package/plugin/workflows/microservices/service-scaffolding.md +151 -0
- package/plugin/workflows/omega/1000x-innovation.md +2 -2
- package/plugin/workflows/omega/100x-architecture.md +2 -2
- package/plugin/workflows/omega/10x-improvement.md +2 -2
- package/plugin/workflows/quality/performance-optimization.md +2 -2
- package/plugin/workflows/research/best-practices.md +1 -1
- package/plugin/workflows/research/technology-research.md +1 -1
- package/plugin/workflows/security/penetration-testing.md +3 -3
- package/plugin/workflows/security/security-audit.md +3 -3
- package/plugin/workflows/sprint/sprint-execution.md +2 -2
- package/plugin/workflows/sprint/sprint-retrospective.md +1 -1
- package/plugin/workflows/sprint/sprint-setup.md +1 -1
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
# Numerical Methods
|
|
2
|
+
|
|
3
|
+
Scientific computing fundamentals including linear algebra, differential equations, optimization, and numerical integration.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Numerical methods provide computational algorithms for solving mathematical problems that cannot be solved analytically, forming the foundation of scientific simulations.
|
|
8
|
+
|
|
9
|
+
## Core Concepts
|
|
10
|
+
|
|
11
|
+
### Problem Categories
|
|
12
|
+
- **Linear Systems**: Ax = b
|
|
13
|
+
- **Nonlinear Equations**: f(x) = 0
|
|
14
|
+
- **Optimization**: min/max f(x)
|
|
15
|
+
- **Differential Equations**: ODEs, PDEs
|
|
16
|
+
- **Integration**: Numerical quadrature
|
|
17
|
+
|
|
18
|
+
### Error Analysis
|
|
19
|
+
- **Truncation Error**: From approximation method
|
|
20
|
+
- **Round-off Error**: From finite precision
|
|
21
|
+
- **Condition Number**: Problem sensitivity
|
|
22
|
+
|
|
23
|
+
## Linear Algebra
|
|
24
|
+
|
|
25
|
+
### Matrix Operations with NumPy
|
|
26
|
+
```python
|
|
27
|
+
import numpy as np
|
|
28
|
+
from scipy import linalg
|
|
29
|
+
from typing import Tuple
|
|
30
|
+
|
|
31
|
+
class LinearSolver:
|
|
32
|
+
@staticmethod
|
|
33
|
+
def solve_direct(A: np.ndarray, b: np.ndarray) -> np.ndarray:
|
|
34
|
+
"""Solve Ax = b using LU decomposition"""
|
|
35
|
+
return np.linalg.solve(A, b)
|
|
36
|
+
|
|
37
|
+
@staticmethod
|
|
38
|
+
def lu_decomposition(A: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
39
|
+
"""LU decomposition with pivoting"""
|
|
40
|
+
P, L, U = linalg.lu(A)
|
|
41
|
+
return P, L, U
|
|
42
|
+
|
|
43
|
+
@staticmethod
|
|
44
|
+
def cholesky(A: np.ndarray) -> np.ndarray:
|
|
45
|
+
"""Cholesky decomposition for symmetric positive definite"""
|
|
46
|
+
return np.linalg.cholesky(A)
|
|
47
|
+
|
|
48
|
+
@staticmethod
|
|
49
|
+
def qr_decomposition(A: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
|
50
|
+
"""QR decomposition"""
|
|
51
|
+
return np.linalg.qr(A)
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def svd(A: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
55
|
+
"""Singular Value Decomposition"""
|
|
56
|
+
return np.linalg.svd(A)
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def eigenvalues(A: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
|
60
|
+
"""Compute eigenvalues and eigenvectors"""
|
|
61
|
+
return np.linalg.eig(A)
|
|
62
|
+
|
|
63
|
+
@staticmethod
|
|
64
|
+
def condition_number(A: np.ndarray) -> float:
|
|
65
|
+
"""Compute condition number"""
|
|
66
|
+
return np.linalg.cond(A)
|
|
67
|
+
|
|
68
|
+
# Iterative solvers for large sparse systems
|
|
69
|
+
from scipy.sparse import csr_matrix
|
|
70
|
+
from scipy.sparse.linalg import cg, gmres, splu
|
|
71
|
+
|
|
72
|
+
class IterativeSolver:
|
|
73
|
+
@staticmethod
|
|
74
|
+
def conjugate_gradient(
|
|
75
|
+
A: csr_matrix,
|
|
76
|
+
b: np.ndarray,
|
|
77
|
+
tol: float = 1e-10,
|
|
78
|
+
maxiter: int = 1000
|
|
79
|
+
) -> Tuple[np.ndarray, int]:
|
|
80
|
+
"""Conjugate gradient for symmetric positive definite"""
|
|
81
|
+
x, info = cg(A, b, tol=tol, maxiter=maxiter)
|
|
82
|
+
return x, info
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
def gmres_solve(
|
|
86
|
+
A: csr_matrix,
|
|
87
|
+
b: np.ndarray,
|
|
88
|
+
tol: float = 1e-10
|
|
89
|
+
) -> Tuple[np.ndarray, int]:
|
|
90
|
+
"""GMRES for general systems"""
|
|
91
|
+
x, info = gmres(A, b, tol=tol)
|
|
92
|
+
return x, info
|
|
93
|
+
|
|
94
|
+
@staticmethod
|
|
95
|
+
def preconditioned_cg(
|
|
96
|
+
A: csr_matrix,
|
|
97
|
+
b: np.ndarray,
|
|
98
|
+
M: csr_matrix = None
|
|
99
|
+
) -> np.ndarray:
|
|
100
|
+
"""PCG with incomplete LU preconditioner"""
|
|
101
|
+
if M is None:
|
|
102
|
+
M = splu(A.tocsc())
|
|
103
|
+
M = lambda x: M.solve(x)
|
|
104
|
+
|
|
105
|
+
x, _ = cg(A, b, M=M)
|
|
106
|
+
return x
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Differential Equations
|
|
110
|
+
|
|
111
|
+
### ODE Solvers
|
|
112
|
+
```python
|
|
113
|
+
import numpy as np
|
|
114
|
+
from scipy.integrate import solve_ivp, odeint
|
|
115
|
+
from typing import Callable, List
|
|
116
|
+
|
|
117
|
+
class ODESolver:
|
|
118
|
+
@staticmethod
|
|
119
|
+
def solve_ode(
|
|
120
|
+
f: Callable,
|
|
121
|
+
y0: np.ndarray,
|
|
122
|
+
t_span: Tuple[float, float],
|
|
123
|
+
t_eval: np.ndarray = None,
|
|
124
|
+
method: str = 'RK45'
|
|
125
|
+
) -> 'OdeResult':
|
|
126
|
+
"""
|
|
127
|
+
Solve ODE: dy/dt = f(t, y)
|
|
128
|
+
|
|
129
|
+
Methods:
|
|
130
|
+
- 'RK45': Runge-Kutta 4(5) (default)
|
|
131
|
+
- 'RK23': Runge-Kutta 2(3)
|
|
132
|
+
- 'DOP853': High-order Dormand-Prince
|
|
133
|
+
- 'Radau': Implicit Runge-Kutta (stiff)
|
|
134
|
+
- 'BDF': Backward differentiation (stiff)
|
|
135
|
+
"""
|
|
136
|
+
return solve_ivp(
|
|
137
|
+
f, t_span, y0,
|
|
138
|
+
method=method,
|
|
139
|
+
t_eval=t_eval,
|
|
140
|
+
dense_output=True
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
@staticmethod
|
|
144
|
+
def runge_kutta_4(
|
|
145
|
+
f: Callable,
|
|
146
|
+
y0: np.ndarray,
|
|
147
|
+
t: np.ndarray
|
|
148
|
+
) -> np.ndarray:
|
|
149
|
+
"""Classic RK4 implementation"""
|
|
150
|
+
n = len(t)
|
|
151
|
+
y = np.zeros((n, len(y0)))
|
|
152
|
+
y[0] = y0
|
|
153
|
+
|
|
154
|
+
for i in range(n - 1):
|
|
155
|
+
h = t[i + 1] - t[i]
|
|
156
|
+
k1 = f(t[i], y[i])
|
|
157
|
+
k2 = f(t[i] + h/2, y[i] + h*k1/2)
|
|
158
|
+
k3 = f(t[i] + h/2, y[i] + h*k2/2)
|
|
159
|
+
k4 = f(t[i] + h, y[i] + h*k3)
|
|
160
|
+
y[i + 1] = y[i] + h * (k1 + 2*k2 + 2*k3 + k4) / 6
|
|
161
|
+
|
|
162
|
+
return y
|
|
163
|
+
|
|
164
|
+
# Example: Lorenz system
|
|
165
|
+
def lorenz(t, state, sigma=10, rho=28, beta=8/3):
|
|
166
|
+
x, y, z = state
|
|
167
|
+
return [
|
|
168
|
+
sigma * (y - x),
|
|
169
|
+
x * (rho - z) - y,
|
|
170
|
+
x * y - beta * z
|
|
171
|
+
]
|
|
172
|
+
|
|
173
|
+
# Solve
|
|
174
|
+
t_span = (0, 50)
|
|
175
|
+
y0 = [1.0, 1.0, 1.0]
|
|
176
|
+
t_eval = np.linspace(0, 50, 10000)
|
|
177
|
+
|
|
178
|
+
solution = ODESolver.solve_ode(lorenz, y0, t_span, t_eval)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### PDE Solvers (Finite Difference)
|
|
182
|
+
```python
|
|
183
|
+
import numpy as np
|
|
184
|
+
from scipy.sparse import diags
|
|
185
|
+
from scipy.sparse.linalg import spsolve
|
|
186
|
+
|
|
187
|
+
class HeatEquationSolver:
|
|
188
|
+
"""
|
|
189
|
+
Solve 1D heat equation: ∂u/∂t = α ∂²u/∂x²
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
def __init__(
|
|
193
|
+
self,
|
|
194
|
+
alpha: float,
|
|
195
|
+
L: float,
|
|
196
|
+
nx: int,
|
|
197
|
+
dt: float
|
|
198
|
+
):
|
|
199
|
+
self.alpha = alpha
|
|
200
|
+
self.L = L
|
|
201
|
+
self.nx = nx
|
|
202
|
+
self.dx = L / (nx - 1)
|
|
203
|
+
self.dt = dt
|
|
204
|
+
self.x = np.linspace(0, L, nx)
|
|
205
|
+
|
|
206
|
+
# Stability check for explicit method
|
|
207
|
+
self.r = alpha * dt / self.dx**2
|
|
208
|
+
if self.r > 0.5:
|
|
209
|
+
print(f"Warning: r={self.r} > 0.5, explicit method unstable")
|
|
210
|
+
|
|
211
|
+
def solve_explicit(
|
|
212
|
+
self,
|
|
213
|
+
u0: np.ndarray,
|
|
214
|
+
nt: int,
|
|
215
|
+
bc: Tuple[float, float] = (0, 0)
|
|
216
|
+
) -> np.ndarray:
|
|
217
|
+
"""FTCS (Forward Time Central Space) explicit method"""
|
|
218
|
+
u = u0.copy()
|
|
219
|
+
u_new = np.zeros_like(u)
|
|
220
|
+
|
|
221
|
+
for n in range(nt):
|
|
222
|
+
u_new[1:-1] = u[1:-1] + self.r * (u[2:] - 2*u[1:-1] + u[:-2])
|
|
223
|
+
u_new[0] = bc[0]
|
|
224
|
+
u_new[-1] = bc[1]
|
|
225
|
+
u = u_new.copy()
|
|
226
|
+
|
|
227
|
+
return u
|
|
228
|
+
|
|
229
|
+
def solve_implicit(
|
|
230
|
+
self,
|
|
231
|
+
u0: np.ndarray,
|
|
232
|
+
nt: int,
|
|
233
|
+
bc: Tuple[float, float] = (0, 0)
|
|
234
|
+
) -> np.ndarray:
|
|
235
|
+
"""Crank-Nicolson implicit method (unconditionally stable)"""
|
|
236
|
+
n = self.nx
|
|
237
|
+
r = self.r
|
|
238
|
+
|
|
239
|
+
# Tridiagonal matrices
|
|
240
|
+
main_diag = (1 + r) * np.ones(n - 2)
|
|
241
|
+
off_diag = -r/2 * np.ones(n - 3)
|
|
242
|
+
A = diags([off_diag, main_diag, off_diag], [-1, 0, 1], format='csr')
|
|
243
|
+
|
|
244
|
+
u = u0.copy()
|
|
245
|
+
|
|
246
|
+
for _ in range(nt):
|
|
247
|
+
# RHS
|
|
248
|
+
b = np.zeros(n - 2)
|
|
249
|
+
b[0] = u[1] + r/2 * (u[2] - 2*u[1] + u[0]) + r/2 * bc[0]
|
|
250
|
+
b[-1] = u[-2] + r/2 * (u[-1] - 2*u[-2] + u[-3]) + r/2 * bc[1]
|
|
251
|
+
b[1:-1] = u[2:-2] + r/2 * (u[3:-1] - 2*u[2:-2] + u[1:-3])
|
|
252
|
+
|
|
253
|
+
# Solve
|
|
254
|
+
u[1:-1] = spsolve(A, b)
|
|
255
|
+
u[0] = bc[0]
|
|
256
|
+
u[-1] = bc[1]
|
|
257
|
+
|
|
258
|
+
return u
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
## Optimization
|
|
262
|
+
|
|
263
|
+
### Optimization Algorithms
|
|
264
|
+
```python
|
|
265
|
+
from scipy.optimize import minimize, minimize_scalar, least_squares
|
|
266
|
+
import numpy as np
|
|
267
|
+
|
|
268
|
+
class Optimizer:
|
|
269
|
+
@staticmethod
|
|
270
|
+
def gradient_descent(
|
|
271
|
+
f: Callable,
|
|
272
|
+
grad_f: Callable,
|
|
273
|
+
x0: np.ndarray,
|
|
274
|
+
lr: float = 0.01,
|
|
275
|
+
tol: float = 1e-6,
|
|
276
|
+
max_iter: int = 10000
|
|
277
|
+
) -> np.ndarray:
|
|
278
|
+
"""Basic gradient descent"""
|
|
279
|
+
x = x0.copy()
|
|
280
|
+
|
|
281
|
+
for _ in range(max_iter):
|
|
282
|
+
g = grad_f(x)
|
|
283
|
+
x_new = x - lr * g
|
|
284
|
+
|
|
285
|
+
if np.linalg.norm(x_new - x) < tol:
|
|
286
|
+
break
|
|
287
|
+
|
|
288
|
+
x = x_new
|
|
289
|
+
|
|
290
|
+
return x
|
|
291
|
+
|
|
292
|
+
@staticmethod
|
|
293
|
+
def newton_method(
|
|
294
|
+
f: Callable,
|
|
295
|
+
grad_f: Callable,
|
|
296
|
+
hess_f: Callable,
|
|
297
|
+
x0: np.ndarray,
|
|
298
|
+
tol: float = 1e-6,
|
|
299
|
+
max_iter: int = 100
|
|
300
|
+
) -> np.ndarray:
|
|
301
|
+
"""Newton's method for optimization"""
|
|
302
|
+
x = x0.copy()
|
|
303
|
+
|
|
304
|
+
for _ in range(max_iter):
|
|
305
|
+
g = grad_f(x)
|
|
306
|
+
H = hess_f(x)
|
|
307
|
+
|
|
308
|
+
# Newton step
|
|
309
|
+
step = np.linalg.solve(H, -g)
|
|
310
|
+
x_new = x + step
|
|
311
|
+
|
|
312
|
+
if np.linalg.norm(g) < tol:
|
|
313
|
+
break
|
|
314
|
+
|
|
315
|
+
x = x_new
|
|
316
|
+
|
|
317
|
+
return x
|
|
318
|
+
|
|
319
|
+
@staticmethod
|
|
320
|
+
def scipy_minimize(
|
|
321
|
+
f: Callable,
|
|
322
|
+
x0: np.ndarray,
|
|
323
|
+
method: str = 'BFGS',
|
|
324
|
+
bounds: List = None,
|
|
325
|
+
constraints: dict = None
|
|
326
|
+
):
|
|
327
|
+
"""
|
|
328
|
+
SciPy optimization wrapper
|
|
329
|
+
|
|
330
|
+
Methods:
|
|
331
|
+
- 'BFGS': Quasi-Newton (unconstrained)
|
|
332
|
+
- 'L-BFGS-B': Limited memory BFGS (box constraints)
|
|
333
|
+
- 'SLSQP': Sequential least squares (constraints)
|
|
334
|
+
- 'trust-constr': Trust region (large scale)
|
|
335
|
+
"""
|
|
336
|
+
return minimize(
|
|
337
|
+
f, x0,
|
|
338
|
+
method=method,
|
|
339
|
+
bounds=bounds,
|
|
340
|
+
constraints=constraints
|
|
341
|
+
)
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## Numerical Integration
|
|
345
|
+
|
|
346
|
+
### Quadrature Methods
|
|
347
|
+
```python
|
|
348
|
+
from scipy.integrate import quad, dblquad, nquad, simpson, trapezoid
|
|
349
|
+
import numpy as np
|
|
350
|
+
|
|
351
|
+
class NumericalIntegration:
|
|
352
|
+
@staticmethod
|
|
353
|
+
def integrate_1d(f: Callable, a: float, b: float) -> Tuple[float, float]:
|
|
354
|
+
"""Adaptive quadrature for 1D integration"""
|
|
355
|
+
result, error = quad(f, a, b)
|
|
356
|
+
return result, error
|
|
357
|
+
|
|
358
|
+
@staticmethod
|
|
359
|
+
def integrate_2d(
|
|
360
|
+
f: Callable,
|
|
361
|
+
x_range: Tuple[float, float],
|
|
362
|
+
y_range: Callable # y bounds as function of x
|
|
363
|
+
) -> Tuple[float, float]:
|
|
364
|
+
"""Double integration"""
|
|
365
|
+
result, error = dblquad(
|
|
366
|
+
f,
|
|
367
|
+
x_range[0], x_range[1],
|
|
368
|
+
y_range[0], y_range[1]
|
|
369
|
+
)
|
|
370
|
+
return result, error
|
|
371
|
+
|
|
372
|
+
@staticmethod
|
|
373
|
+
def simpson_rule(y: np.ndarray, dx: float) -> float:
|
|
374
|
+
"""Simpson's rule for evenly spaced data"""
|
|
375
|
+
return simpson(y, dx=dx)
|
|
376
|
+
|
|
377
|
+
@staticmethod
|
|
378
|
+
def trapezoidal_rule(y: np.ndarray, x: np.ndarray) -> float:
|
|
379
|
+
"""Trapezoidal rule"""
|
|
380
|
+
return trapezoid(y, x)
|
|
381
|
+
|
|
382
|
+
@staticmethod
|
|
383
|
+
def monte_carlo(
|
|
384
|
+
f: Callable,
|
|
385
|
+
bounds: List[Tuple[float, float]],
|
|
386
|
+
n_samples: int = 100000
|
|
387
|
+
) -> Tuple[float, float]:
|
|
388
|
+
"""Monte Carlo integration"""
|
|
389
|
+
dim = len(bounds)
|
|
390
|
+
volume = np.prod([b[1] - b[0] for b in bounds])
|
|
391
|
+
|
|
392
|
+
# Random samples
|
|
393
|
+
samples = np.random.uniform(
|
|
394
|
+
low=[b[0] for b in bounds],
|
|
395
|
+
high=[b[1] for b in bounds],
|
|
396
|
+
size=(n_samples, dim)
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
values = np.array([f(*s) for s in samples])
|
|
400
|
+
estimate = volume * np.mean(values)
|
|
401
|
+
std_error = volume * np.std(values) / np.sqrt(n_samples)
|
|
402
|
+
|
|
403
|
+
return estimate, std_error
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
## Best Practices
|
|
407
|
+
|
|
408
|
+
1. **Validate Against Analytical**: When possible
|
|
409
|
+
2. **Check Convergence**: Refine mesh/timestep
|
|
410
|
+
3. **Monitor Condition Number**: Avoid ill-conditioned problems
|
|
411
|
+
4. **Use Sparse Matrices**: For large systems
|
|
412
|
+
5. **Choose Appropriate Method**: Based on problem type
|
|
413
|
+
|
|
414
|
+
## Anti-Patterns
|
|
415
|
+
|
|
416
|
+
- Ignoring numerical stability
|
|
417
|
+
- Using wrong solver for problem type
|
|
418
|
+
- Not checking convergence
|
|
419
|
+
- Dense matrices for sparse problems
|
|
420
|
+
- Ignoring round-off error
|
|
421
|
+
|
|
422
|
+
## When to Use
|
|
423
|
+
|
|
424
|
+
- Scientific simulations
|
|
425
|
+
- Engineering calculations
|
|
426
|
+
- Data fitting/regression
|
|
427
|
+
- Control systems
|
|
428
|
+
- Financial modeling
|
|
429
|
+
|
|
430
|
+
## When NOT to Use
|
|
431
|
+
|
|
432
|
+
- Analytical solution exists
|
|
433
|
+
- Problem too simple
|
|
434
|
+
- Extreme precision needed (use symbolic)
|