<s> from __future__ import print_function <EOL> import unittest <EOL> import sys <EOL> import numpy <EOL> import scipy . linalg <EOL> import theano <EOL> from theano import gof , tensor , scalar <EOL> from theano . tests import unittest_tools as utt <EOL> class Solve ( gof . Op ) : <EOL> """<STR_LIT>""" <EOL> def __eq__ ( self , other ) : <EOL> return type ( self ) == type ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( type ( self ) ) <EOL> def make_node ( self , A , b ) : <EOL> A_ = tensor . as_tensor_variable ( A ) <EOL> b_ = tensor . as_tensor_variable ( b ) <EOL> if A_ . broadcastable != ( False , False ) : <EOL> raise TypeError ( "<STR_LIT>" , A_ . type ) <EOL> if b_ . broadcastable not in ( ( False , ) , ( True , False ) , ( False , False ) ) : <EOL> raise TypeError ( "<STR_LIT>" , b_ . type ) <EOL> odtype = scalar . upcast ( A_ . dtype , b_ . dtype ) <EOL> otype = tensor . TensorType ( broadcastable = b_ . broadcastable , dtype = odtype ) <EOL> return gof . Apply ( op = self , inputs = [ A_ , b_ ] , outputs = [ otype ( ) ] ) <EOL> def perform ( self , node , inp , out ) : <EOL> A , b = inp <EOL> output , = out <EOL> ret = scipy . linalg . solve ( A , b ) <EOL> if ret . dtype != node . outputs [ <NUM_LIT:0> ] . dtype : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> ret = theano . _asarray ( ret , dtype = node . outputs [ <NUM_LIT:0> ] . dtype ) <EOL> output [ <NUM_LIT:0> ] = ret <EOL> solve = Solve ( ) <EOL> class T_solve ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . rng = numpy . random . RandomState ( utt . fetch_seed ( <NUM_LIT> ) ) <EOL> def test0 ( self ) : <EOL> A = self . rng . randn ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> b = numpy . arange ( <NUM_LIT:5> , dtype = float ) <EOL> x = scipy . linalg . solve ( A , b ) <EOL> Ax = numpy . dot ( A , x ) <EOL> are = tensor . numeric_grad . abs_rel_err ( Ax , b ) <EOL> self . assertTrue ( numpy . all ( are < <NUM_LIT> ) , ( are , Ax , b ) ) </s>
