5.4.11 · HinglishScientific Computing (Python)

scipy.linalg — more stable than numpy.linalg, lu, qr, schur

1,811 words8 min readRead in English

5.4.11 · Coding › Scientific Computing (Python)


WHY scipy.linalg, numpy.linalg se behtar kyun

WHY "more stable"? Koi jaadu nahi — dono LAPACK call karte hain. Practical differences yeh hain:

  1. scipy.linalg hamesha ek optimized, complete LAPACK/BLAS ke saath compiled hoti hai; numpy kabhi kabhi build ke hisaab se slower/less-complete routines pe fall back kar sakti hai.
  2. scipy aapko stable algorithm explicitly choose karne deti hai (jaise solve karo LU with partial pivoting se, ya Cholesky se agar SPD hai) ek hi ek general path ke bajaye.
  3. Kabhi inv(A) @ b mat compute karo — wahi asli instability hai. solve/factorizations use karo.

Teen core factorizations

Figure — scipy.linalg — more stable than numpy.linalg, lu, qr, schur

1. LU — solve karne ke liye

import numpy as np
from scipy.linalg import lu, lu_factor, lu_solve, solve
A = np.array([[2.,1.],[6.,5.]]); b = np.array([3.,17.])
P, L, U = lu(A)                 # SciPy convention: P @ A = L @ U  (so A = P.T @ L @ U)
x = solve(A, b)                 # the right way to solve  -> x = [-0.5, 4.]
lu_piv = lu_factor(A)           # reuse factorization for many b's
x2 = lu_solve(lu_piv, b)

2. QR — least squares aur orthogonalization ke liye

from scipy.linalg import qr, lstsq
Q, R = qr(A, mode='economic')   # economic: Q is m×n, R is n×n
x, res, rank, sv = lstsq(A, b)  # robust least squares (uses SVD/QR internally)

3. Schur — eigenvalues aur matrix functions ke liye

from scipy.linalg import schur, eig
T, Z = schur(A)                 # A = Z @ T @ Z.conj().T
w, vl, vr = eig(A, left=True)   # scipy gives left+right eigenvectors
# generalized eigenproblem A x = λ B x  (numpy can't do this!):
w_gen = eig(A, b=np.eye(len(A)))

Recall Feynman: 12 saal ke bacche ko explain karo

Socho ek ulsajhi hui machine hai jo arrows ko ghuma-marod deti hai. Us uljhan ko seedha samajhna mushkil hai. Toh hum us machine ko simple pieces mein todh dete hain: ek piece sirf stretch karta hai (triangular), ek piece sirf spin karta hai bina stretch kiye (orthogonal/unitary). Jab yeh simple pieces mein aa jaaye, tab sawaalon ka jawab dena — "kaunsa arrow khud jaisa hi nikalta hai?" (eigenvalues) ya "machine ko undo karo taaki original arrow wapas mile" (solve ) — aasaan ho jaata hai. scipy.linalg woh toolbox hai jo yeh clean breaks carefully karta hai taaki chhoti rounding mistakes phail na jaayein.

Why use scipy.linalg over numpy.linalg?
Full LAPACK interface (lu, qr, schur, cho_factor, generalized eig), hamesha optimized LAPACK/BLAS se linked, aur stable algorithm explicitly choose karne deta hai.
What is the LU factorization with pivoting?
— P permutation (stability ke liye row swaps), L unit-lower-triangular (elimination multipliers), U upper triangular.
How do you solve Ax=b once you have PA=LU?
Forward-solve , phir back-solve .
Why never compute inv(A)@b?
~3× zyada flops aur rounding error amplify karta hai; solve()/factorizations use karo.
What convention does scipy.linalg.lu return?
P, L, U with P @ A = L @ U (equivalently A = P.T @ L @ U), NOT A = P @ L @ U.
Define QR factorization.
jahan Q orthogonal hai () aur R upper triangular hai.
Why does QR solve least squares stably?
Orthogonal Q length preserve karta hai, toh min reduce ho jaata hai par; normal equations se bachta hai jo condition number square kar dete hain.
What sits on the diagonal of T in the Schur form ?
A ke eigenvalues.
Why prefer Schur over diagonalization numerically?
Schur hamesha exist karta hai aur unitary Z use karta hai (condition number 1), defective/non-diagonalizable matrices ke liye bhi.
Which scipy feature does numpy.linalg lack for eigenproblems?
Generalized eigenproblem via eig(A, b=B).
Relationship between κ(A) and the normal equations?
Normal equations ka condition number hota hai, jo unhe QR se kam accurate banata hai.

Connections

Concept Map

wraps

wraps subset of

exposes more than

provides

enables

preferred over

amplifies rounding error

gives

uses partial pivoting for

LAPACK Fortran

scipy.linalg

numpy.linalg

Matrix factorizations

LU: PA = LU

QR factorization

Schur factorization

solve Ax=b

Compute inv A then multiply

More stable and accurate