import numpy as np
from scipy import optimize

Scalar optimisation

def f(x):
    return -np.exp(-(x-0.7)**2)
result = optimize.minimize_scalar(f)
result.success, type(result)
(True, scipy.optimize.optimize.OptimizeResult)
result
     fun: -1.0
    nfev: 13
     nit: 9
 success: True
       x: 0.6999999997839409
result.x - 0.7
-2.160590595323697e-10
def f(x):   # The rosenbrock function
    return .5*(1 - x[0])**2 + (x[1] - x[0]**2)**2
optimize.minimize(f, [2, -1], method="CG")
     fun: 1.6503729082243953e-11
     jac: array([-6.15347610e-06,  2.53804028e-07])
 message: 'Optimization terminated successfully.'
    nfev: 81
     nit: 13
    njev: 27
  status: 0
 success: True
       x: array([0.99999426, 0.99998863])
def jacobian(x):
    return np.array((-2*.5*(1 - x[0]) - 4*x[0]*(x[1] - x[0]**2), 2*(x[1] - x[0]**2)))
optimize.minimize(f, [2, 1], method="CG", jac=jacobian) 
     fun: 2.957865890641887e-14
     jac: array([ 7.18259502e-07, -2.99030306e-07])
 message: 'Optimization terminated successfully.'
    nfev: 16
     nit: 8
    njev: 16
  status: 0
 success: True
       x: array([1.00000012, 1.00000009])
def f(x):   # The rosenbrock function
    return .5*(1 - x[0])**2 + (x[1] - x[0]**2)**2
def jacobian(x):
    return np.array((-2*.5*(1 - x[0]) - 4*x[0]*(x[1] - x[0]**2), 2*(x[1] - x[0]**2)))
optimize.minimize(f, [2,-1], method="Newton-CG", jac=jacobian)   
     fun: 1.5601357400786612e-15
     jac: array([ 1.05753092e-07, -7.48325277e-08])
 message: 'Optimization terminated successfully.'
    nfev: 11
    nhev: 0
     nit: 10
    njev: 33
  status: 0
 success: True
       x: array([0.99999995, 0.99999988])
def hessian(x): # Computed with sympy
    return np.array(((1 - 4*x[1] + 12*x[0]**2, -4*x[0]), (-4*x[0], 2)))
optimize.minimize(f, [2,-1], method="Newton-CG", jac=jacobian, hess=hessian)
     fun: 1.6277298383706738e-15
     jac: array([ 1.11044158e-07, -7.78093352e-08])
 message: 'Optimization terminated successfully.'
    nfev: 11
    nhev: 10
     nit: 10
    njev: 11
  status: 0
 success: True
       x: array([0.99999994, 0.99999988])