<s> import unittest <EOL> from SimPEG import Solver <EOL> from SimPEG . Mesh import TensorMesh <EOL> from SimPEG . Utils import sdiag <EOL> import numpy as np <EOL> import scipy . sparse as sp <EOL> from SimPEG import Optimization <EOL> from SimPEG . Tests import getQuadratic , Rosenbrock <EOL> TOL = <NUM_LIT> <EOL> class TestOptimizers ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . A = sp . identity ( <NUM_LIT:2> ) . tocsr ( ) <EOL> self . b = np . array ( [ - <NUM_LIT:5> , - <NUM_LIT:5> ] ) <EOL> def test_GN_Rosenbrock ( self ) : <EOL> GN = Optimization . GaussNewton ( ) <EOL> xopt = GN . minimize ( Rosenbrock , np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> x_true = np . array ( [ <NUM_LIT:1.> , <NUM_LIT:1.> ] ) <EOL> print '<STR_LIT>' , xopt <EOL> print '<STR_LIT>' , x_true <EOL> self . assertTrue ( np . linalg . norm ( xopt - x_true , <NUM_LIT:2> ) < TOL , True ) <EOL> def test_GN_quadratic ( self ) : <EOL> GN = Optimization . GaussNewton ( ) <EOL> xopt = GN . minimize ( getQuadratic ( self . A , self . b ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> x_true = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> print '<STR_LIT>' , xopt <EOL> print '<STR_LIT>' , x_true <EOL> self . assertTrue ( np . linalg . norm ( xopt - x_true , <NUM_LIT:2> ) < TOL , True ) <EOL> def test_ProjGradient_quadraticBounded ( self ) : <EOL> PG = Optimization . ProjectedGradient ( debug = True ) <EOL> PG . lower , PG . upper = - <NUM_LIT:2> , <NUM_LIT:2> <EOL> xopt = PG . minimize ( getQuadratic ( self . A , self . b ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> x_true = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> print '<STR_LIT>' , xopt <EOL> print '<STR_LIT>' , x_true <EOL> self . assertTrue ( np . linalg . norm ( xopt - x_true , <NUM_LIT:2> ) < TOL , True ) <EOL> def test_ProjGradient_quadratic1Bound ( self ) : <EOL> myB = np . array ( [ - <NUM_LIT:5> , <NUM_LIT:1> ] ) <EOL> PG = Optimization . ProjectedGradient ( ) <EOL> PG . lower , PG . upper = - <NUM_LIT:2> , <NUM_LIT:2> <EOL> xopt = PG . minimize ( getQuadratic ( self . A , myB ) , np . array ( [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> x_true = np . array ( [ <NUM_LIT> , - <NUM_LIT:1.> ] ) <EOL> print '<STR_LIT>' , xopt <EOL> print '<STR_LIT>' , x_true <EOL> self . assertTrue ( np . linalg . norm ( xopt - x_true , <NUM_LIT:2> ) < TOL , True ) <EOL> def test_NewtonRoot ( self ) : <EOL> fun = lambda x , return_g = True : np . sin ( x ) if not return_g else ( np . sin ( x ) , sdiag ( np . cos ( x ) ) ) <EOL> x = np . array ( [ np . pi - <NUM_LIT> , np . pi + <NUM_LIT:0.1> , <NUM_LIT:0> ] ) <EOL> xopt = Optimization . NewtonRoot ( comments = False ) . root ( fun , x ) <EOL> x_true = np . array ( [ np . pi , np . pi , <NUM_LIT:0> ] ) <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' , xopt <EOL> print '<STR_LIT>' , x_true <EOL> self . assertTrue ( np . linalg . norm ( xopt - x_true , <NUM_LIT:2> ) < TOL , True ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
