<s> import unittest <EOL> import math <EOL> import ast <EOL> from numpy import array , eye , arange , roll , tile <EOL> from openmdao . main . datatypes . array import Array <EOL> from openmdao . main . expreval import ExprEvaluator , ConnectedExprEvaluator , ExprExaminer <EOL> from openmdao . main . printexpr import ExprPrinter , print_node , transform_expression <EOL> from openmdao . main . api import Assembly , Component , set_as_top <EOL> from openmdao . main . datatypes . api import Float , List , Slot , Dict <EOL> from openmdao . util . testutil import assert_rel_error <EOL> from math import sqrt , sin , cos <EOL> try : <EOL> from math import gamma <EOL> except ImportError as err : <EOL> from scipy . special import gamma <EOL> class A ( Component ) : <EOL> f = Float ( iotype = '<STR_LIT>' ) <EOL> a1d = Array ( array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] ) , iotype = '<STR_LIT>' ) <EOL> a2d = Array ( array ( [ [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) , iotype = '<STR_LIT>' ) <EOL> b1d = Array ( array ( [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] ) , iotype = '<STR_LIT>' ) <EOL> b2d = Array ( array ( [ [ <NUM_LIT:1.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) , iotype = '<STR_LIT>' ) <EOL> c1d = Array ( array ( [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] ) , iotype = '<STR_LIT>' ) <EOL> c2d = Array ( array ( [ [ <NUM_LIT:0.0> , <NUM_LIT:1.0> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) , iotype = '<STR_LIT>' ) <EOL> ext1 = Array ( array ( [ eye ( <NUM_LIT:3> ) , eye ( <NUM_LIT:3> ) , eye ( <NUM_LIT:3> ) ] ) ) <EOL> def execute ( self ) : <EOL> pass <EOL> def some_funct ( self , a , b , op = '<STR_LIT>' ) : <EOL> if op == '<STR_LIT>' : <EOL> return a + b <EOL> elif op == '<STR_LIT>' : <EOL> return a * b <EOL> elif op == '<STR_LIT>' : <EOL> return a - b <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> @ property <EOL> def some_prop ( self ) : <EOL> return <NUM_LIT:7> <EOL> class B ( Component ) : <EOL> in1 = Float ( iotype = "<STR_LIT>" ) <EOL> in11 = Float ( iotype = "<STR_LIT>" ) <EOL> class Comp ( Component ) : <EOL> x = Float ( iotype = '<STR_LIT>' ) <EOL> y = Float ( iotype = '<STR_LIT>' ) <EOL> indct = Dict ( iotype = '<STR_LIT>' ) <EOL> outdct = Dict ( iotype = '<STR_LIT>' ) <EOL> cont = Slot ( A ) <EOL> contlist = List ( Slot ( A ) , iotype = '<STR_LIT>' ) <EOL> def get_cont ( self , i ) : <EOL> return self . contlist [ i ] <EOL> def get_attrib ( self , name ) : <EOL> return getattr ( self , name ) <EOL> class Simple ( Component ) : <EOL> a = Float ( iotype = '<STR_LIT>' ) <EOL> b = Float ( iotype = '<STR_LIT>' ) <EOL> c = Float ( iotype = '<STR_LIT>' ) <EOL> d = Float ( iotype = '<STR_LIT>' ) <EOL> x_array = Array ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , iotype = '<STR_LIT>' ) <EOL> def __init__ ( self ) : <EOL> super ( Simple , self ) . __init__ ( ) <EOL> self . a = <NUM_LIT> <EOL> self . b = <NUM_LIT> <EOL> self . c = <NUM_LIT> <EOL> self . d = <NUM_LIT> <EOL> def execute ( self ) : <EOL> self . c = self . a + self . b <EOL> self . d = self . a - self . b <EOL> self . x_array [ <NUM_LIT:1> ] = self . a * self . b <EOL> def new_text ( expr ) : <EOL> ep = ExprPrinter ( ) <EOL> ep . visit ( expr . _parse_get ( ) [ <NUM_LIT:0> ] ) <EOL> return ep . get_text ( ) <EOL> class ExprPrinterTestCase ( unittest . TestCase ) : <EOL> def test_print_node ( self ) : <EOL> checks = [ <EOL> '<STR_LIT:a>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> for check in checks : <EOL> node = ast . parse ( check , mode = '<STR_LIT>' ) <EOL> self . assertEqual ( check , print_node ( node ) ) <EOL> class ExprEvalTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . top = set_as_top ( Assembly ( ) ) <EOL> self . top . add ( '<STR_LIT:a>' , A ( ) ) <EOL> self . top . a . a1d = [ <NUM_LIT:1.> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] <EOL> self . top . add ( '<STR_LIT>' , Comp ( ) ) <EOL> self . top . comp . x = <NUM_LIT> <EOL> self . top . comp . y = <NUM_LIT> <EOL> def _do_tests ( self , tests , top ) : <EOL> for tst in tests : <EOL> ex = ExprEvaluator ( tst [ <NUM_LIT:0> ] , top ) <EOL> self . assertEqual ( new_text ( ex ) , tst [ <NUM_LIT:1> ] ) <EOL> def test_eq ( self ) : <EOL> ex1 = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> ex2 = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> ex3_bad = "<STR_LIT:test>" <EOL> self . assertTrue ( ex1 == ex2 ) <EOL> self . assertTrue ( ex2 != ex3_bad ) <EOL> def test_simple ( self ) : <EOL> tests = [ <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> self . _do_tests ( tests , self . top ) <EOL> def test_containers ( self ) : <EOL> tests = [ <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> self . _do_tests ( tests , self . top ) <EOL> def test_dicts ( self ) : <EOL> tests = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> self . _do_tests ( tests , self . top ) <EOL> def test_arrays ( self ) : <EOL> tests = [ <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ] <EOL> self . _do_tests ( tests , self . top ) <EOL> def test_calls ( self ) : <EOL> tests = [ <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ] <EOL> self . _do_tests ( tests , self . top ) <EOL> def test_set_evaluate ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> self . assertEqual ( <NUM_LIT> , ex . evaluate ( ) ) <EOL> ex . set ( <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , self . top . comp . x ) <EOL> self . top . comp . contlist = [ A ( ) , A ( ) , A ( ) ] <EOL> self . top . comp . contlist [ <NUM_LIT:1> ] . a1d = [ <NUM_LIT:4> ] * <NUM_LIT:5> <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT:4> ) <EOL> ex . set ( <NUM_LIT> ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT> ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top ) <EOL> self . assertEqual ( ex . evaluate ( ) , - <NUM_LIT:2> ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT:8> ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT:4> ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top . a ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT> ) <EOL> ex . set ( <NUM_LIT> ) <EOL> self . assertEqual ( self . top . a . a2d [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top . a ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT> ) <EOL> ex . set ( <NUM_LIT> ) <EOL> self . assertEqual ( self . top . a . a2d [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top . a ) <EOL> self . assertTrue ( all ( ex . evaluate ( ) == array ( [ <NUM_LIT> , <NUM_LIT> ] ) ) ) <EOL> ex . set ( [ <NUM_LIT:0.1> , <NUM_LIT> ] ) <EOL> self . assertTrue ( all ( self . top . a . a2d [ <NUM_LIT:1> ] == array ( [ <NUM_LIT:0.1> , <NUM_LIT> ] ) ) ) <EOL> self . top . comp . cont = A ( ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT> ) <EOL> ex . set ( <NUM_LIT> ) <EOL> self . assertEqual ( self . top . comp . cont . a2d [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT> ) <EOL> ex . set ( <NUM_LIT> ) <EOL> self . assertEqual ( self . top . comp . cont . a2d [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> try : <EOL> import numpy <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top . a ) <EOL> val = ex . evaluate ( ) <EOL> self . assertTrue ( ( val == numpy . eye ( <NUM_LIT:2> ) ) . all ( ) ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top ) <EOL> self . assertEqual ( list ( ex . evaluate ( ) ) , [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:4> ] ) <EOL> ex = ExprEvaluator ( "<STR_LIT>" , self . top ) <EOL> self . assertEqual ( list ( ex . evaluate ( ) ) , [ <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:4> ] ) <EOL> def test_reparse_on_scope_change ( self ) : <EOL> self . top . comp . x = <NUM_LIT> <EOL> self . top . comp . y = - <NUM_LIT> <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> self . assertEqual ( <NUM_LIT> , ex . evaluate ( ) ) <EOL> self . assertEqual ( new_text ( ex ) , "<STR_LIT>" ) <EOL> ex . scope = self . top . a <EOL> try : <EOL> ex . set ( <NUM_LIT:0.5> ) <EOL> except AttributeError as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertEqual ( new_text ( ex ) , "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT> , ex . evaluate ( self . top ) ) <EOL> self . assertEqual ( new_text ( ex ) , "<STR_LIT>" ) <EOL> ex . text = '<STR_LIT>' <EOL> try : <EOL> ex . evaluate ( self . top . a ) <EOL> except AttributeError as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> ex . scope = self . top <EOL> ex . set ( <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , self . top . comp . y ) <EOL> self . assertEqual ( new_text ( ex ) , "<STR_LIT>" ) <EOL> def test_no_scope ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT> ) <EOL> ex . text = '<STR_LIT>' <EOL> try : <EOL> ex . evaluate ( ) <EOL> except Exception , err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_property ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top . a ) <EOL> self . assertEqual ( ex . evaluate ( ) , <NUM_LIT:7> ) <EOL> def test_assignee ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top . a ) <EOL> self . assertEqual ( ex . is_valid_assignee ( ) , False ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . is_valid_assignee ( ) , True ) <EOL> def test_resolve ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> self . assertEqual ( ex . check_resolve ( ) , True ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . check_resolve ( ) , True ) <EOL> ex . scope = self . top . comp <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . check_resolve ( ) , True ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . check_resolve ( ) , False ) <EOL> def test_get_referenced_varpaths ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top . a ) <EOL> self . assertEqual ( ex . get_referenced_varpaths ( ) , set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_varpaths ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> ex . scope = self . top . comp <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_varpaths ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_varpaths ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_varpaths ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_varpaths ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> def test_get_referenced_compnames ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> self . assertEqual ( ex . get_referenced_compnames ( ) , set ( [ '<STR_LIT>' , '<STR_LIT:a>' ] ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_compnames ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> ex . scope = self . top <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_compnames ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_compnames ( ) , set ( [ '<STR_LIT>' ] ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_compnames ( ) , set ( ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertEqual ( ex . get_referenced_compnames ( ) , set ( [ '<STR_LIT:a>' ] ) ) <EOL> def test_slice ( self ) : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top . a ) <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT:1.> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT:1.> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT:1.> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT:1.> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> ex . text = '<STR_LIT>' <EOL> self . assertTrue ( all ( array ( [ <NUM_LIT:1.> , <NUM_LIT> ] ) == ex . evaluate ( ) ) ) <EOL> def test_ext_slice ( self ) : <EOL> for k in xrange ( <NUM_LIT:3> ) : <EOL> expr = '<STR_LIT>' . format ( k ) <EOL> expr = ExprEvaluator ( expr , self . top . a ) <EOL> comp = ( eye ( <NUM_LIT:3> ) == expr . evaluate ( ) ) . all ( ) <EOL> self . assertTrue ( comp ) <EOL> expr = '<STR_LIT>' . format ( k ) <EOL> expr = ExprEvaluator ( expr , self . top . a ) <EOL> comp = ( tile ( roll ( array ( [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ) , k ) , ( <NUM_LIT:3> , <NUM_LIT:1> ) ) == expr . evaluate ( ) ) . all ( ) <EOL> self . assertTrue ( comp ) <EOL> expr = '<STR_LIT>' . format ( k ) <EOL> expr = ExprEvaluator ( expr , self . top . a ) <EOL> comp = ( tile ( roll ( array ( [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ) , k ) , ( <NUM_LIT:3> , <NUM_LIT:1> ) ) == expr . evaluate ( ) ) . all ( ) <EOL> self . assertTrue ( comp ) <EOL> for j in xrange ( <NUM_LIT:3> ) : <EOL> expr = '<STR_LIT>' . format ( j , k ) <EOL> expr = ExprEvaluator ( expr , self . top . a ) <EOL> if j == k : <EOL> comp = all ( array ( [ <NUM_LIT:1.> , <NUM_LIT:1.> , <NUM_LIT:1.> ] ) == expr . evaluate ( ) ) <EOL> self . assertTrue ( comp ) <EOL> else : <EOL> comp = all ( array ( [ <NUM_LIT:0.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ) == expr . evaluate ( ) ) <EOL> self . assertTrue ( comp ) <EOL> expr = '<STR_LIT>' . format ( j , k ) <EOL> expr = ExprEvaluator ( expr , self . top . a ) <EOL> arr = array ( [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ) <EOL> comp = all ( roll ( arr , k ) == expr . evaluate ( ) ) <EOL> self . assertTrue ( comp ) <EOL> expr = '<STR_LIT>' . format ( j , k ) <EOL> expr = ExprEvaluator ( expr , self . top . a ) <EOL> arr = array ( [ <NUM_LIT:1.> , <NUM_LIT:0.> , <NUM_LIT:0.> ] ) <EOL> comp = all ( roll ( arr , k ) == expr . evaluate ( ) ) <EOL> self . assertTrue ( comp ) <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> expr = '<STR_LIT>' . format ( i , j , k ) <EOL> expr = ExprEvaluator ( expr , self . top . a ) <EOL> if j == k : <EOL> comp = all ( array ( [ <NUM_LIT:1.> ] ) == expr . evaluate ( ) ) <EOL> self . assertTrue ( comp ) <EOL> else : <EOL> comp = all ( array ( [ <NUM_LIT:0.> ] ) == expr . evaluate ( ) ) <EOL> self . assertTrue ( comp ) <EOL> def test_boolean ( self ) : <EOL> comp = self . top . comp <EOL> comp . x = <NUM_LIT:1.> <EOL> comp . y = <NUM_LIT> <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> comp . x = <NUM_LIT> <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . top . a . b = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . top . a . b = [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> def test_builtins ( self ) : <EOL> comp = self . top . comp <EOL> comp . x = <NUM_LIT:1.> <EOL> comp . y = - <NUM_LIT> <EOL> self . assertEqual ( <NUM_LIT> , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertAlmostEqual ( <NUM_LIT:0.> , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> comp . x = <NUM_LIT> <EOL> self . assertEqual ( <NUM_LIT:1.> , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> self . assertEqual ( <NUM_LIT> , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> comp . x = <NUM_LIT:0.> <EOL> self . assertEqual ( True , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> comp . x = math . pi / <NUM_LIT> <EOL> self . assertEqual ( False , ExprEvaluator ( '<STR_LIT>' , self . top ) . evaluate ( ) ) <EOL> def test_multi_object ( self ) : <EOL> try : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> ex . set ( <NUM_LIT:1> ) <EOL> except ValueError , err : <EOL> self . assertEqual ( str ( err ) , <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> def test_bogus ( self ) : <EOL> try : <EOL> ex = ExprEvaluator ( '<STR_LIT>' , self . top ) <EOL> ex . evaluate ( ) <EOL> except AttributeError , err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> def test_get_required_comps ( self ) : <EOL> top = set_as_top ( Assembly ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> top . connect ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_eval_gradient ( self ) : <EOL> top = set_as_top ( Assembly ( ) ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> top . comp1 . a = <NUM_LIT> <EOL> top . comp1 . b = <NUM_LIT> <EOL> top . run ( ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> self . assertEqual ( top . comp1 . c , <NUM_LIT> ) <EOL> top . add ( '<STR_LIT>' , Simple ( ) ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> self . assertEqual ( len ( grad ) , <NUM_LIT:2> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> grad = exp . evaluate_gradient ( scope = top , wrt = [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( grad ) , <NUM_LIT:1> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT:1.> / top . comp2 . a , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> g1 = - sin ( top . comp2 . b ) * cos ( cos ( top . comp2 . b ) ) <EOL> g2 = ( <NUM_LIT:2> * sqrt ( top . comp2 . a ) * top . comp1 . c ) ** - <NUM_LIT:1> <EOL> g3 = - sqrt ( top . comp2 . a ) / top . comp1 . c ** <NUM_LIT:2> <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , g1 , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , g2 , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , g3 , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> from scipy . special import polygamma <EOL> g1 = gamma ( top . comp2 . a ) * polygamma ( <NUM_LIT:0> , top . comp2 . a ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , g1 , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> def test_eval_gradient_array ( self ) : <EOL> top = set_as_top ( Assembly ( ) ) <EOL> top . add ( '<STR_LIT>' , A ( ) ) <EOL> top . run ( ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:1> , <NUM_LIT:1> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:2> , <NUM_LIT:2> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:3> , <NUM_LIT:3> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:1> , <NUM_LIT:1> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:2> , <NUM_LIT:2> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] [ <NUM_LIT:3> , <NUM_LIT:3> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> exp = ExprEvaluator ( '<STR_LIT>' , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> a2d_grad , c2d_grad = grad [ '<STR_LIT>' ] , grad [ '<STR_LIT>' ] <EOL> assert_rel_error ( self , a2d_grad [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , a2d_grad [ <NUM_LIT:1> , <NUM_LIT:1> ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , a2d_grad [ <NUM_LIT:2> , <NUM_LIT:2> ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , a2d_grad [ <NUM_LIT:3> , <NUM_LIT:3> ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , c2d_grad [ <NUM_LIT:0> , <NUM_LIT:0> ] , <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> assert_rel_error ( self , c2d_grad [ <NUM_LIT:1> , <NUM_LIT:1> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , c2d_grad [ <NUM_LIT:2> , <NUM_LIT:2> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , c2d_grad [ <NUM_LIT:3> , <NUM_LIT:3> ] , <NUM_LIT> , <NUM_LIT> ) <EOL> def test_eval_gradient_lots_of_vars ( self ) : <EOL> top = set_as_top ( Assembly ( ) ) <EOL> top . add ( '<STR_LIT>' , B ( ) ) <EOL> expr = "<STR_LIT>" <EOL> exp = ExprEvaluator ( expr , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT> , <NUM_LIT> ) <EOL> expr = "<STR_LIT>" <EOL> exp = ExprEvaluator ( expr , top . driver ) <EOL> grad = exp . evaluate_gradient ( scope = top ) <EOL> assert_rel_error ( self , grad [ '<STR_LIT>' ] , <NUM_LIT:1.0> , <NUM_LIT> ) <EOL> def test_connected_expr ( self ) : <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top ) . _parse ( ) <EOL> try : <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top , is_dest = True ) . _parse ( ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top ) . _parse ( ) <EOL> try : <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top , is_dest = True ) . _parse ( ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top ) . _parse ( ) <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top , is_dest = True ) . _parse ( ) <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top ) . _parse ( ) <EOL> try : <EOL> ConnectedExprEvaluator ( "<STR_LIT>" , self . top , is_dest = True ) . _parse ( ) <EOL> except Exception as err : <EOL> self . assertEqual ( str ( err ) , "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> class ExprExaminerTestCase ( unittest . TestCase ) : <EOL> def _examine ( self , text , simplevar = True , assignable = True , <EOL> const_indices = True , refs = None , const = False ) : <EOL> ee = ExprExaminer ( ast . parse ( text , mode = '<STR_LIT>' ) ) <EOL> self . assertEqual ( ee . simplevar , simplevar ) <EOL> self . assertEqual ( ee . assignable , assignable ) <EOL> self . assertEqual ( ee . const_indices , const_indices ) <EOL> self . assertEqual ( ee . const , const ) <EOL> if refs is None : <EOL> refs = set ( ) <EOL> self . assertEqual ( refs , ee . refs ) <EOL> return ee <EOL> def test_exprs ( self ) : <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , const = True ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , const = True ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , const = True ) <EOL> self . _examine ( "<STR_LIT:x>" , refs = set ( [ '<STR_LIT:x>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , refs = set ( [ '<STR_LIT>' , '<STR_LIT:x>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , <EOL> refs = set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , const_indices = False , refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , const_indices = False , <EOL> refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , const_indices = False , refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , refs = set ( [ '<STR_LIT>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , refs = set ( [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , refs = set ( [ '<STR_LIT:x>' , '<STR_LIT:y>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , refs = set ( [ '<STR_LIT:x>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , refs = set ( [ '<STR_LIT:x>' ] ) ) <EOL> self . _examine ( "<STR_LIT>" , simplevar = False , assignable = False , refs = set ( [ '<STR_LIT:x>' ] ) ) <EOL> class TransformTestCase ( unittest . TestCase ) : <EOL> def test_xforms ( self ) : <EOL> tests = [ <EOL> ( '<STR_LIT>' , { '<STR_LIT:abc>' : '<STR_LIT>' } , '<STR_LIT>' ) , <EOL> ] <EOL> for orig , mapping , expected in tests : <EOL> xformed = transform_expression ( orig , mapping ) <EOL> self . assertEqual ( expected , xformed ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
