Skip to content

Instantly share code, notes, and snippets.

@jasonmhite
Last active November 7, 2017 18:05
Show Gist options
  • Save jasonmhite/9581b991b34e80576b6c70e07f08ed47 to your computer and use it in GitHub Desktop.
Save jasonmhite/9581b991b34e80576b6c70e07f08ed47 to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.optimize import minimize
def least_squares(A, b, x0): # Least-squares solution for Ax=b, initial guess of x0
return minimize(
lambda x: np.linalg.norm(A.dot(x) - b),
x0,
)
def least_squares_tikhonov(A, b, alpha, x0): # Least squares for Ax=b with simple Tikhonov regularization
return minimize(
lambda x: np.linalg.norm(A.dot(x) - b) + alpha * np.linalg.norm(x),
x0,
)
def least_squares_generalized_tikhonov(A, b, Gamma, x0): # Least squares with generalized Tikhonov regularization
return minimize(
lambda x: np.linalg.norm(A.dot(x) - b) + np.linalg.norm(Gamma.dot(x)),
x0,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment