Skip to content

devtsphys/doc-scipy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

SciPy Reference Guide

Overview

SciPy (Scientific Python) is a Python library used for scientific and technical computing. It builds on NumPy and provides additional functionality for optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, statistics, and many other classes of problems.

Core Components

Core Modules

Module Description
scipy.cluster Clustering algorithms
scipy.constants Physical and mathematical constants
scipy.fft Fast Fourier Transform routines
scipy.integrate Integration and ordinary differential equation solvers
scipy.interpolate Interpolation and smoothing splines
scipy.io Data input and output
scipy.linalg Linear algebra operations
scipy.ndimage N-dimensional image processing
scipy.optimize Optimization and root-finding algorithms
scipy.signal Signal processing
scipy.sparse Sparse matrices and algorithms
scipy.spatial Spatial data structures and algorithms
scipy.special Special functions
scipy.stats Statistical distributions and functions

Detailed Function Reference

scipy.optimize

Root Finding

Function Description Example
root General root finding from scipy.optimize import root
result = root(lambda x: x**3 - 1, 0.5)
print(result.x) # [1.0]
fsolve Find roots of a function from scipy.optimize import fsolve
result = fsolve(lambda x: x**2 - 4, 1)
print(result) # [2.]
newton Newton-Raphson method from scipy.optimize import newton
result = newton(lambda x: x**2 - 2, 1)
print(result) # 1.4142135623730951

Minimization

Function Description Example
minimize Unified interface for minimization from scipy.optimize import minimize
result = minimize(lambda x: x**2 + 2*x + 2, 0)
print(result.x) # [-1.]
minimize_scalar Scalar function minimization from scipy.optimize import minimize_scalar
result = minimize_scalar(lambda x: x**2 + 2*x + 2)
print(result.x) # -1.0
differential_evolution Global optimization from scipy.optimize import differential_evolution
result = differential_evolution(lambda x: x[0]**2, [(-10, 10)])
print(result.x) # [~0]

Curve Fitting

Function Description Example
curve_fit Non-linear least squares from scipy.optimize import curve_fit
def f(x, a, b): return a * x + b
params, _ = curve_fit(f, [0, 1, 2], [1, 3, 5])
print(params) # [2. 1.]

scipy.integrate

Function Description Example
quad Single integration from scipy.integrate import quad
result, error = quad(lambda x: x**2, 0, 1)
print(result) # 0.33333333333333337
dblquad Double integration from scipy.integrate import dblquad
result, error = dblquad(lambda y, x: x*y, 0, 1, lambda x: 0, lambda x: 1)
print(result) # 0.25
odeint ODE solver (legacy) from scipy.integrate import odeint
def model(y, t): return -0.5 * y
t = [0, 1, 2]
result = odeint(model, 1, t)
print(result) # [[1.], [0.60653066], [0.36787944]]
solve_ivp Modern ODE solver from scipy.integrate import solve_ivp
def model(t, y): return [-0.5 * y[0]]
result = solve_ivp(model, [0, 2], [1])
print(result.y) # [[1. ... 0.36787944]]

scipy.interpolate

Function Description Example
interp1d 1D interpolation from scipy.interpolate import interp1d
x = [0, 1, 2]
y = [0, 1, 4]
f = interp1d(x, y)
print(f(1.5)) # 2.5
CubicSpline Cubic spline from scipy.interpolate import CubicSpline
x = [0, 1, 2]
y = [0, 1, 4]
cs = CubicSpline(x, y)
print(cs(1.5)) # 2.125
griddata Interpolate unstructured data from scipy.interpolate import griddata
import numpy as np
points = np.array([[0, 0], [1, 0], [0, 1]])
values = np.array([1, 2, 3])
result = griddata(points, values, (0.5, 0.5), method='linear')
print(result) # 2.0

scipy.linalg

Function Description Example
inv Matrix inverse from scipy.linalg import inv
import numpy as np
A = np.array([[1, 2], [3, 4]])
A_inv = inv(A)
print(A_inv) # [[-2. 1. ] [ 1.5 -0.5]]
solve Solve linear system from scipy.linalg import solve
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
x = solve(A, b)
print(x) # [-4. 4.5]
eig Eigenvalues/eigenvectors from scipy.linalg import eig
A = np.array([[1, 2], [3, 4]])
eigenvals, eigenvecs = eig(A)
print(eigenvals) # [-0.37228132 5.37228132]
svd Singular value decomposition from scipy.linalg import svd
A = np.array([[1, 2], [3, 4]])
U, s, Vh = svd(A)
print(s) # [5.4649857 0.36596619]
cholesky Cholesky decomposition from scipy.linalg import cholesky
A = np.array([[4, 2], [2, 5]])
L = cholesky(A, lower=True)
print(L) # [[2. 0.] [1. 2.]]

scipy.stats

Distributions

Distribution Description Example
norm Normal distribution from scipy.stats import norm
print(norm.pdf(0)) # 0.3989422804014327
print(norm.cdf(1.96)) # 0.9750021048517795
uniform Uniform distribution from scipy.stats import uniform
print(uniform.rvs(size=5)) # [Array of 5 random samples]
t Student's t-distribution from scipy.stats import t
print(t.ppf(0.975, df=10)) # 2.2281388519649385

Statistical Tests

Function Description Example
ttest_ind t-test for independent samples from scipy.stats import ttest_ind
a = [1, 2, 3, 4, 5]
b = [2, 3, 4, 5, 6]
t_stat, p_val = ttest_ind(a, b)
print(p_val) # 0.1372785765621116
pearsonr Pearson correlation from scipy.stats import pearsonr
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 8, 9]
r, p = pearsonr(x, y)
print(r) # 0.9684134781954834
linregress Linear regression from scipy.stats import linregress
x = [1, 2, 3, 4, 5]
y = [2, 4, 5, 4, 6]
result = linregress(x, y)
print(result.slope) # 0.8

scipy.signal

Function Description Example
convolve Convolution from scipy.signal import convolve
x = [1, 2, 3]
y = [0, 1, 0.5]
result = convolve(x, y, mode='full')
print(result) # [0. 1. 2.5 3.5 1.5]
filtfilt Zero-phase filtering from scipy.signal import filtfilt, butter
b, a = butter(4, 0.2)
x = np.random.randn(100)
y = filtfilt(b, a, x)
butter Butterworth filter design from scipy.signal import butter
b, a = butter(4, 0.2)
print(b[:3]) # [0.0048, 0.0193, 0.0290]
spectrogram Compute spectrogram from scipy.signal import spectrogram
fs = 10e3
x = np.random.randn(8000)
f, t, Sxx = spectrogram(x, fs)

scipy.fft

Function Description Example
fft Fast Fourier Transform from scipy.fft import fft
x = np.array([1, 2, 1, 0])
y = fft(x)
print(abs(y)) # [4. 1.41421356 0. 1.41421356]
ifft Inverse FFT from scipy.fft import ifft
y = np.array([4, 1+1j, 0, 1-1j])
x = ifft(y)
print(x) # [1.+0.j 2.+0.j 1.+0.j 0.+0.j]
fft2 2D FFT from scipy.fft import fft2
x = np.ones((2, 2))
y = fft2(x)
print(y) # [[4.+0.j 0.+0.j] [0.+0.j 0.+0.j]]

scipy.sparse

Function/Class Description Example
csr_matrix Compressed Sparse Row matrix from scipy.sparse import csr_matrix
data = np.array([1, 2, 3])
row = np.array([0, 0, 1])
col = np.array([0, 2, 1])
mat = csr_matrix((data, (row, col)), shape=(2, 3))
print(mat.toarray()) # [[1 0 2] [0 3 0]]
linalg.spsolve Solve sparse system from scipy.sparse import csr_matrix
from scipy.sparse.linalg import spsolve
A = csr_matrix([[3, 2], [1, 1]])
b = np.array([5, 2])
x = spsolve(A, b)
print(x) # [1. 1.]

scipy.spatial

Function Description Example
distance.pdist Pairwise distances from scipy.spatial import distance
x = np.array([[0, 0], [0, 1], [1, 0]])
d = distance.pdist(x, 'euclidean')
print(d) # [1. 1. 1.41421356]
KDTree KD-Tree for quick nearest-neighbor lookup from scipy.spatial import KDTree
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
tree = KDTree(x)
dist, idx = tree.query(np.array([0.5, 0.5]), k=2)
print(idx) # [0 2]
Voronoi Voronoi diagrams from scipy.spatial import Voronoi
points = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
vor = Voronoi(points)
print(vor.vertices) # [[0.5 0.5]]

scipy.ndimage

Function Description Example
gaussian_filter Gaussian smoothing from scipy.ndimage import gaussian_filter
x = np.random.randn(10, 10)
y = gaussian_filter(x, sigma=1)
rotate Rotate array from scipy.ndimage import rotate
x = np.eye(4)
y = rotate(x, 45)
label Label features in image from scipy.ndimage import label
x = np.array([[0, 1, 1], [0, 0, 1], [1, 1, 0]])
labeled, num_features = label(x)
print(num_features) # 2

scipy.constants

Constant Value Example
pi 3.141592653589793 from scipy import constants
print(constants.pi) # 3.141592653589793
c Speed of light (m/s) print(constants.c) # 299792458.0
G Gravitational constant print(constants.G) # 6.67430e-11
h Planck constant print(constants.h) # 6.62607015e-34

Advanced Techniques

Optimization Problems

# Global optimization
from scipy.optimize import differential_evolution

# Define objective function
def objective(x):
    return x[0]**2 + x[1]**2

# Define bounds
bounds = [(-5, 5), (-5, 5)]

# Solve the minimization problem
result = differential_evolution(objective, bounds)
print(result.x)  # Should be close to [0, 0]

ODE Solving

# Solving a system of ODEs
from scipy.integrate import solve_ivp
import numpy as np
import matplotlib.pyplot as plt

# Define the ODE system (Lotka-Volterra predator-prey model)
def lotka_volterra(t, z, a, b, c, d):
    x, y = z
    dx_dt = a*x - b*x*y
    dy_dt = -c*y + d*x*y
    return [dx_dt, dy_dt]

# Parameters
a, b, c, d = 1.0, 0.1, 1.5, 0.075
z0 = [10, 5]  # Initial population
t_span = (0, 40)  # Time span
t_eval = np.linspace(0, 40, 1000)  # Points to evaluate

# Solve
sol = solve_ivp(
    lambda t, z: lotka_volterra(t, z, a, b, c, d),
    t_span, z0, method='RK45', t_eval=t_eval
)

# Extract results
t = sol.t
x, y = sol.y

FFT for Signal Processing

import numpy as np
from scipy.fft import fft, fftfreq
import matplotlib.pyplot as plt

# Create a signal with two sine waves
sample_rate = 1000  # Hz
duration = 1  # second
t = np.linspace(0, duration, sample_rate, endpoint=False)
freq1, freq2 = 50, 120  # Hz
signal = np.sin(2*np.pi*freq1*t) + 0.5*np.sin(2*np.pi*freq2*t)

# Add some noise
signal += 0.2 * np.random.randn(len(t))

# Compute FFT
yf = fft(signal)
xf = fftfreq(sample_rate, 1/sample_rate)[:sample_rate//2]
yplot = 2.0/sample_rate * np.abs(yf[:sample_rate//2])

# Plot the spectrum
plt.figure(figsize=(10, 6))
plt.plot(xf, yplot)
plt.grid()
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.xlim(0, 200)

Working with Sparse Matrices

from scipy.sparse import csr_matrix, diags
import numpy as np

# Create a sparse matrix from diagonals
n = 1000
diagonals = [np.ones(n), -2*np.ones(n), np.ones(n)]
offsets = [-1, 0, 1]
A = diags(diagonals, offsets, shape=(n, n), format='csr')

# Create a random sparse matrix
density = 0.01  # 1% of elements are non-zero
B = np.random.random((n, n))
B[B > density] = 0
B_sparse = csr_matrix(B)

# Sparse matrix operations
C = A @ B_sparse  # Matrix multiplication

Spatial Data Processing

from scipy.spatial import Delaunay, ConvexHull
import numpy as np
import matplotlib.pyplot as plt

# Generate random points
points = np.random.rand(30, 2)

# Compute Delaunay triangulation
tri = Delaunay(points)

# Compute the convex hull
hull = ConvexHull(points)

# Plot
plt.figure(figsize=(10, 5))
plt.triplot(points[:,0], points[:,1], tri.simplices)
plt.plot(points[:,0], points[:,1], 'o')
plt.plot(points[hull.vertices,0], points[hull.vertices,1], 'r-', lw=2)

Tips and Best Practices

  1. Import Conventions: Use named imports for clarity:

    from scipy import optimize, stats, signal
  2. Computational Efficiency:

    • Use sparse matrices for large, sparse problems
    • Vectorize operations instead of loops
    • Use appropriate methods for large-scale problems (e.g., scipy.sparse.linalg vs scipy.linalg)
  3. Error Handling:

    • Check return values of optimization functions like result.success
    • Use try/except blocks for numerical routines that might fail
  4. Integration with NumPy and Matplotlib:

    • SciPy works seamlessly with NumPy arrays
    • Results are easily visualized with Matplotlib
  5. Version Compatibility:

    • SciPy functions and behavior can change between versions
    • Check documentation for your specific version

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published