<s> import numpy as np <EOL> from numpy . linalg import lstsq <EOL> from numpy . testing import ( assert_allclose , assert_equal , assert_ , <EOL> run_module_suite , assert_raises ) <EOL> from scipy . sparse import rand <EOL> from scipy . sparse . linalg import aslinearoperator <EOL> from scipy . optimize import lsq_linear <EOL> A = np . array ( [ <EOL> [ <NUM_LIT> , - <NUM_LIT> ] , <EOL> [ - <NUM_LIT> , - <NUM_LIT> ] , <EOL> [ - <NUM_LIT> , <NUM_LIT> ] , <EOL> ] ) <EOL> b = np . array ( [ <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] ) <EOL> class BaseMixin ( object ) : <EOL> def __init__ ( self ) : <EOL> self . rnd = np . random . RandomState ( <NUM_LIT:0> ) <EOL> def test_dense_no_bounds ( self ) : <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , method = self . method , lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , lstsq ( A , b ) [ <NUM_LIT:0> ] ) <EOL> def test_dense_bounds ( self ) : <EOL> lb = np . array ( [ - <NUM_LIT:1> , - <NUM_LIT:10> ] ) <EOL> ub = np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( lb , ub ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , lstsq ( A , b ) [ <NUM_LIT:0> ] ) <EOL> lb = np . array ( [ <NUM_LIT:0.0> , - np . inf ] ) <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( lb , np . inf ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , np . array ( [ <NUM_LIT:0.0> , - <NUM_LIT> ] ) , <EOL> atol = <NUM_LIT> ) <EOL> lb = np . array ( [ - <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( lb , np . inf ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , np . array ( [ <NUM_LIT> , <NUM_LIT:0> ] ) , <EOL> atol = <NUM_LIT> ) <EOL> ub = np . array ( [ np . inf , - <NUM_LIT:5> ] ) <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( - np . inf , ub ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , np . array ( [ - <NUM_LIT> , - <NUM_LIT:5> ] ) ) <EOL> ub = np . array ( [ - <NUM_LIT:1> , np . inf ] ) <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( - np . inf , ub ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , np . array ( [ - <NUM_LIT:1> , - <NUM_LIT> ] ) ) <EOL> lb = np . array ( [ <NUM_LIT:0> , - <NUM_LIT:4> ] ) <EOL> ub = np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( lb , ub ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , np . array ( [ <NUM_LIT> , - <NUM_LIT:4> ] ) ) <EOL> def test_dense_rank_deficient ( self ) : <EOL> A = np . array ( [ [ - <NUM_LIT> , - <NUM_LIT> ] ] ) <EOL> b = np . array ( [ <NUM_LIT> ] ) <EOL> lb = [ - <NUM_LIT:0.1> , - <NUM_LIT:0.1> ] <EOL> ub = [ <NUM_LIT:0.1> , <NUM_LIT:0.1> ] <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( lb , ub ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . x , [ - <NUM_LIT:0.1> , - <NUM_LIT:0.1> ] ) <EOL> A = np . array ( [ <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ - <NUM_LIT> , - <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> ] ) <EOL> b = np . array ( [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> lb = [ <NUM_LIT:0> , - <NUM_LIT:1> ] <EOL> ub = [ <NUM_LIT:1> , - <NUM_LIT:0.5> ] <EOL> for lsq_solver in self . lsq_solvers : <EOL> res = lsq_linear ( A , b , ( lb , ub ) , method = self . method , <EOL> lsq_solver = lsq_solver ) <EOL> assert_allclose ( res . optimality , <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> def test_full_result ( self ) : <EOL> lb = np . array ( [ <NUM_LIT:0> , - <NUM_LIT:4> ] ) <EOL> ub = np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> res = lsq_linear ( A , b , ( lb , ub ) , method = self . method ) <EOL> assert_allclose ( res . x , [ <NUM_LIT> , - <NUM_LIT:4> ] ) <EOL> r = A . dot ( res . x ) - b <EOL> assert_allclose ( res . cost , <NUM_LIT:0.5> * np . dot ( r , r ) ) <EOL> assert_allclose ( res . fun , r ) <EOL> assert_allclose ( res . optimality , <NUM_LIT:0.0> , atol = <NUM_LIT> ) <EOL> assert_equal ( res . active_mask , [ <NUM_LIT:0> , - <NUM_LIT:1> ] ) <EOL> assert_ ( res . nit < <NUM_LIT:15> ) <EOL> assert_ ( res . status == <NUM_LIT:1> or res . status == <NUM_LIT:3> ) <EOL> assert_ ( isinstance ( res . message , str ) ) <EOL> assert_ ( res . success ) <EOL> class SparseMixin ( object ) : <EOL> def test_sparse_and_LinearOperator ( self ) : <EOL> m = <NUM_LIT> <EOL> n = <NUM_LIT:1000> <EOL> A = rand ( m , n , random_state = <NUM_LIT:0> ) <EOL> b = self . rnd . randn ( m ) <EOL> res = lsq_linear ( A , b ) <EOL> assert_allclose ( res . optimality , <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> A = aslinearoperator ( A ) <EOL> res = lsq_linear ( A , b ) <EOL> assert_allclose ( res . optimality , <NUM_LIT:0> , atol = <NUM_LIT> ) <EOL> def test_sparse_bounds ( self ) : <EOL> m = <NUM_LIT> <EOL> n = <NUM_LIT:1000> <EOL> A = rand ( m , n , random_state = <NUM_LIT:0> ) <EOL> b = self . rnd . randn ( m ) <EOL> lb = self . rnd . randn ( n ) <EOL> ub = lb + <NUM_LIT:1> <EOL> res = lsq_linear ( A , b , ( lb , ub ) ) <EOL> assert_allclose ( res . optimality , <NUM_LIT:0.0> , atol = <NUM_LIT> ) <EOL> res = lsq_linear ( A , b , ( lb , ub ) , lsmr_tol = <NUM_LIT> ) <EOL> assert_allclose ( res . optimality , <NUM_LIT:0.0> , atol = <NUM_LIT> ) <EOL> res = lsq_linear ( A , b , ( lb , ub ) , lsmr_tol = '<STR_LIT>' ) <EOL> assert_allclose ( res . optimality , <NUM_LIT:0.0> , atol = <NUM_LIT> ) <EOL> class TestTRF ( BaseMixin , SparseMixin ) : <EOL> method = '<STR_LIT>' <EOL> lsq_solvers = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class TestBVLS ( BaseMixin ) : <EOL> method = '<STR_LIT>' <EOL> lsq_solvers = [ '<STR_LIT>' ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run_module_suite ( ) </s>
