pylops
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePyLops - Linear Operators Library
PyLops - 线性算子库
Quick Reference
快速参考
python
import numpy as np
import pylopspython
import numpy as np
import pylopsCreate operator and apply forward/adjoint
Create operator and apply forward/adjoint
A = pylops.FirstDerivative(n=100, dtype='float64')
y = A @ x # Forward: y = A @ x
x_adj = A.H @ y # Adjoint: x = A.H @ y
x_est = A / y # Solve inverse problem
undefinedA = pylops.FirstDerivative(n=100, dtype='float64')
y = A @ x # Forward: y = A @ x
x_adj = A.H @ y # Adjoint: x = A.H @ y
x_est = A / y # Solve inverse problem
undefinedKey Classes
核心类
| Class | Purpose |
|---|---|
| Base class for all operators |
| Vertical/horizontal operator stacking |
| Block diagonal operator composition |
| 类 | 用途 |
|---|---|
| 所有算子的基类 |
| 垂直/水平算子堆叠 |
| 块对角算子组合 |
Essential Operations
关键操作
Basic Operators
基础算子
python
undefinedpython
undefinedDiagonal operator
Diagonal operator
D = pylops.Diagonal(np.array([1., 2., 3.]))
y = D @ x; x_adj = D.H @ y
D = pylops.Diagonal(np.array([1., 2., 3.]))
y = D @ x; x_adj = D.H @ y
Derivatives
Derivatives
D1 = pylops.FirstDerivative(n, dtype='float64')
D2 = pylops.SecondDerivative(n, dtype='float64')
G = pylops.Gradient(dims=(64, 64), dtype='float64')
undefinedD1 = pylops.FirstDerivative(n, dtype='float64')
D2 = pylops.SecondDerivative(n, dtype='float64')
G = pylops.Gradient(dims=(64, 64), dtype='float64')
undefinedConvolution
卷积
python
wavelet = np.sin(np.linspace(0, 2*np.pi, 21)) * np.hanning(21)
C = pylops.signalprocessing.Convolve1D(n, h=wavelet, offset=10)
y = C @ x # Convolve
x_adj = C.H @ y # Correlation (adjoint)python
wavelet = np.sin(np.linspace(0, 2*np.pi, 21)) * np.hanning(21)
C = pylops.signalprocessing.Convolve1D(n, h=wavelet, offset=10)
y = C @ x # Convolve
x_adj = C.H @ y # Correlation (adjoint)Compose and Stack Operators
组合与堆叠算子
python
undefinedpython
undefinedChain: y = C @ B @ A @ x
Chain: y = C @ B @ A @ x
composed = pylops.Smoothing1D(5, n) @ pylops.FirstDerivative(n) @ pylops.Identity(n)
composed = pylops.Smoothing1D(5, n) @ pylops.FirstDerivative(n) @ pylops.Identity(n)
Stack operators
Stack operators
V = pylops.VStack([A, B]) # Vertical: (2n, n)
H = pylops.HStack([A, B]) # Horizontal: (n, 2n)
BD = pylops.BlockDiag([A, B]) # Block diagonal: (2n, 2n)
undefinedV = pylops.VStack([A, B]) # Vertical: (2n, n)
H = pylops.HStack([A, B]) # Horizontal: (n, 2n)
BD = pylops.BlockDiag([A, B]) # Block diagonal: (2n, 2n)
undefinedSolve Inverse Problems
求解逆问题
python
undefinedpython
undefinedSimple least squares
Simple least squares
x_est = A / y
x_est = A / y
Normal equations
Normal equations
x_est = pylops.optimization.leastsquares.NormalEquationsInversion(A, None, y)
x_est = pylops.optimization.leastsquares.NormalEquationsInversion(A, None, y)
Regularized inversion with smoothness
Regularized inversion with smoothness
Reg = pylops.SecondDerivative(n)
x_est = pylops.optimization.leastsquares.RegularizedInversion(
A, [Reg], y, epsRs=[0.1]
)
undefinedReg = pylops.SecondDerivative(n)
x_est = pylops.optimization.leastsquares.RegularizedInversion(
A, [Reg], y, epsRs=[0.1]
)
undefinedIterative Solvers
迭代求解器
python
x_lsqr = pylops.optimization.solver.lsqr(A, y, iter_lim=100)[0]
x_cgls = pylops.optimization.solver.cgls(A, y, niter=100)[0]python
x_lsqr = pylops.optimization.solver.lsqr(A, y, iter_lim=100)[0]
x_cgls = pylops.optimization.solver.cgls(A, y, niter=100)[0]Sparsity-Promoting Inversion
稀疏约束反演
python
x_l1 = pylops.optimization.sparsity.fista(A, y, niter=100, eps=0.1)[0]python
x_l1 = pylops.optimization.sparsity.fista(A, y, niter=100, eps=0.1)[0]Verify Adjoint (Dot Test)
验证伴随算子(点测试)
python
A = pylops.FirstDerivative(100)
pylops.utils.dottest(A, 100, 100, verb=True) # Dot test passed!python
A = pylops.FirstDerivative(100)
pylops.utils.dottest(A, 100, 100, verb=True) # Dot test passed!Common Patterns
常见应用场景
Seismic Deconvolution
地震反卷积
python
C = pylops.signalprocessing.Convolve1D(n, h=wavelet, offset=len(wavelet)//2)
seismic = C @ reflectivitypython
C = pylops.signalprocessing.Convolve1D(n, h=wavelet, offset=len(wavelet)//2)
seismic = C @ reflectivityDeconvolve (regularized)
Deconvolve (regularized)
Reg = pylops.SecondDerivative(n)
reflectivity_est = pylops.optimization.leastsquares.RegularizedInversion(
C, [Reg], seismic, epsRs=[0.01]
)
undefinedReg = pylops.SecondDerivative(n)
reflectivity_est = pylops.optimization.leastsquares.RegularizedInversion(
C, [Reg], seismic, epsRs=[0.01]
)
undefinedImage Denoising with TV
基于总变分(TV)的图像去噪
python
ny, nx = image.shape
G = pylops.Gradient(dims=(ny, nx))
x_tv = pylops.optimization.sparsity.splitbregman(
pylops.Identity(ny*nx), G, image.ravel(),
niter_inner=5, niter_outer=10, mu=1.0, epsRL1s=[0.1]
)[0].reshape(ny, nx)python
ny, nx = image.shape
G = pylops.Gradient(dims=(ny, nx))
x_tv = pylops.optimization.sparsity.splitbregman(
pylops.Identity(ny*nx), G, image.ravel(),
niter_inner=5, niter_outer=10, mu=1.0, epsRL1s=[0.1]
)[0].reshape(ny, nx)When to Use vs Alternatives
适用场景与替代方案对比
| Scenario | Recommendation |
|---|---|
| Matrix-free linear operators for large inverse problems | PyLops - purpose-built, memory efficient |
| Sparse matrix operations with known structure | scipy.sparse - standard, well-documented |
| Simple convolution/deconvolution | PyLops - clean API with |
| Custom operators for small problems | Custom NumPy/SciPy - no extra dependency |
| GPU-accelerated linear algebra | PyLops - pass CuPy arrays for automatic GPU |
| Seismic deconvolution or imaging operators | PyLops - rich signal processing operator library |
Choose PyLops when: You need matrix-free linear operators that scale to large problems
without forming explicit matrices. Its operator algebra (, , ) and
built-in solvers (LSQR, FISTA, Split Bregman) make inverse problem workflows concise.
@VStackBlockDiagAvoid PyLops when: Your problem is small enough for explicit matrices (use NumPy/SciPy),
or you need nonlinear operators (PyLops is strictly linear).
| 场景 | 推荐方案 |
|---|---|
| 大规模逆问题的无矩阵线性算子 | PyLops - 专为该场景打造,内存高效 |
| 具有已知结构的稀疏矩阵运算 | scipy.sparse - 标准库,文档完善 |
| 简单卷积/反卷积 | PyLops - 简洁API,内置 |
| 小问题的自定义算子 | 自定义NumPy/SciPy实现 - 无需额外依赖 |
| GPU加速线性代数 | PyLops - 传入CuPy数组即可自动实现GPU加速 |
| 地震反卷积或成像算子 | PyLops - 丰富的信号处理算子库 |
选择PyLops的场景:当你需要无矩阵线性算子来处理大规模问题,且无需显式构建矩阵时。其算子代数(、、)和内置求解器(LSQR、FISTA、Split Bregman)可让逆问题工作流更简洁。
@VStackBlockDiag避免使用PyLops的场景:当你的问题规模小到可以使用显式矩阵时(使用NumPy/SciPy即可),或者你需要非线性算子时(PyLops仅支持线性算子)。
Common Workflows
常见工作流
Regularized seismic deconvolution
正则化地震反卷积
- Define wavelet array and create operator
Convolve1D - Generate or load seismic trace data
- Run dot test to verify operator adjoint:
pylops.utils.dottest() - Set up regularization operator (e.g., for smoothness)
SecondDerivative - Run
RegularizedInversion(C, [Reg], data, epsRs=[eps]) - Compare estimated reflectivity against true (if available)
- Tune parameter: higher = smoother, lower = sharper
epsRs - For sparse solutions, use instead
pylops.optimization.sparsity.fista()
- 定义子波数组并创建算子
Convolve1D - 生成或加载地震道数据
- 运行点测试验证算子伴随性:
pylops.utils.dottest() - 设置正则化算子(例如,使用实现平滑约束)
SecondDerivative - 运行
RegularizedInversion(C, [Reg], data, epsRs=[eps]) - 对比估计的反射率与真实值(若可用)
- 调整参数:值越大结果越平滑,值越小结果越锐利
epsRs - 若需要稀疏解,改用
pylops.optimization.sparsity.fista()
Tips
提示
- Never form full matrix - Use and
.matvec()for memory efficiency.rmatvec() - Check shapes - Operators have attribute like matrices
.shape - Verify adjoint - Always run for custom operators
pylops.utils.dottest() - Start simple - Test on small problems before scaling up
- Use GPU - Pass CuPy arrays for automatic GPU acceleration
- 永远不要构建完整矩阵 - 使用和
.matvec()以保证内存效率.rmatvec() - 检查形状 - 算子拥有与矩阵类似的属性
.shape - 验证伴随算子 - 自定义算子时务必运行
pylops.utils.dottest() - 从简单案例开始 - 在大规模问题前先在小数据集上测试
- 使用GPU加速 - 传入CuPy数组即可自动实现GPU加速
References
参考资料
- Operators Reference - Complete list of available operators
- Solvers Reference - Solver methods and configuration options
- 算子参考文档 - 所有可用算子的完整列表
- 求解器参考文档 - 求解器方法及配置选项
Scripts
示例脚本
- scripts/deconvolution.py - Seismic deconvolution example
- scripts/deconvolution.py - 地震反卷积示例