pylops

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PyLops - Linear Operators Library

PyLops - 线性算子库

Quick Reference

快速参考

python
import numpy as np
import pylops
python
import numpy as np
import pylops

Create 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
undefined
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
undefined

Key Classes

核心类

ClassPurpose
LinearOperator
Base class for all operators
VStack/HStack
Vertical/horizontal operator stacking
BlockDiag
Block diagonal operator composition
用途
LinearOperator
所有算子的基类
VStack/HStack
垂直/水平算子堆叠
BlockDiag
块对角算子组合

Essential Operations

关键操作

Basic Operators

基础算子

python
undefined
python
undefined

Diagonal 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')
undefined
D1 = pylops.FirstDerivative(n, dtype='float64') D2 = pylops.SecondDerivative(n, dtype='float64') G = pylops.Gradient(dims=(64, 64), dtype='float64')
undefined

Convolution

卷积

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
undefined
python
undefined

Chain: 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)
undefined
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)
undefined

Solve Inverse Problems

求解逆问题

python
undefined
python
undefined

Simple 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] )
undefined
Reg = pylops.SecondDerivative(n) x_est = pylops.optimization.leastsquares.RegularizedInversion( A, [Reg], y, epsRs=[0.1] )
undefined

Iterative 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 @ reflectivity
python
C = pylops.signalprocessing.Convolve1D(n, h=wavelet, offset=len(wavelet)//2)
seismic = C @ reflectivity

Deconvolve (regularized)

Deconvolve (regularized)

Reg = pylops.SecondDerivative(n) reflectivity_est = pylops.optimization.leastsquares.RegularizedInversion( C, [Reg], seismic, epsRs=[0.01] )
undefined
Reg = pylops.SecondDerivative(n) reflectivity_est = pylops.optimization.leastsquares.RegularizedInversion( C, [Reg], seismic, epsRs=[0.01] )
undefined

Image 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

适用场景与替代方案对比

ScenarioRecommendation
Matrix-free linear operators for large inverse problemsPyLops - purpose-built, memory efficient
Sparse matrix operations with known structurescipy.sparse - standard, well-documented
Simple convolution/deconvolutionPyLops - clean API with
Convolve1D
Custom operators for small problemsCustom NumPy/SciPy - no extra dependency
GPU-accelerated linear algebraPyLops - pass CuPy arrays for automatic GPU
Seismic deconvolution or imaging operatorsPyLops - 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 (
@
,
VStack
,
BlockDiag
) and built-in solvers (LSQR, FISTA, Split Bregman) make inverse problem workflows concise.
Avoid 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,内置
Convolve1D
小问题的自定义算子自定义NumPy/SciPy实现 - 无需额外依赖
GPU加速线性代数PyLops - 传入CuPy数组即可自动实现GPU加速
地震反卷积或成像算子PyLops - 丰富的信号处理算子库
选择PyLops的场景:当你需要无矩阵线性算子来处理大规模问题,且无需显式构建矩阵时。其算子代数(
@
VStack
BlockDiag
)和内置求解器(LSQR、FISTA、Split Bregman)可让逆问题工作流更简洁。
避免使用PyLops的场景:当你的问题规模小到可以使用显式矩阵时(使用NumPy/SciPy即可),或者你需要非线性算子时(PyLops仅支持线性算子)。

Common Workflows

常见工作流

Regularized seismic deconvolution

正则化地震反卷积

  • Define wavelet array and create
    Convolve1D
    operator
  • Generate or load seismic trace data
  • Run dot test to verify operator adjoint:
    pylops.utils.dottest()
  • Set up regularization operator (e.g.,
    SecondDerivative
    for smoothness)
  • Run
    RegularizedInversion(C, [Reg], data, epsRs=[eps])
  • Compare estimated reflectivity against true (if available)
  • Tune
    epsRs
    parameter: higher = smoother, lower = sharper
  • For sparse solutions, use
    pylops.optimization.sparsity.fista()
    instead
  • 定义子波数组并创建
    Convolve1D
    算子
  • 生成或加载地震道数据
  • 运行点测试验证算子伴随性:
    pylops.utils.dottest()
  • 设置正则化算子(例如,使用
    SecondDerivative
    实现平滑约束)
  • 运行
    RegularizedInversion(C, [Reg], data, epsRs=[eps])
  • 对比估计的反射率与真实值(若可用)
  • 调整
    epsRs
    参数:值越大结果越平滑,值越小结果越锐利
  • 若需要稀疏解,改用
    pylops.optimization.sparsity.fista()

Tips

提示

  1. Never form full matrix - Use
    .matvec()
    and
    .rmatvec()
    for memory efficiency
  2. Check shapes - Operators have
    .shape
    attribute like matrices
  3. Verify adjoint - Always run
    pylops.utils.dottest()
    for custom operators
  4. Start simple - Test on small problems before scaling up
  5. Use GPU - Pass CuPy arrays for automatic GPU acceleration
  1. 永远不要构建完整矩阵 - 使用
    .matvec()
    .rmatvec()
    以保证内存效率
  2. 检查形状 - 算子拥有与矩阵类似的
    .shape
    属性
  3. 验证伴随算子 - 自定义算子时务必运行
    pylops.utils.dottest()
  4. 从简单案例开始 - 在大规模问题前先在小数据集上测试
  5. 使用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 - 地震反卷积示例